vibe/vibe/acp/tools/builtins/read_file.py
Mathias Gesbert 5103019b01
v2.5.0 (#495)
Co-authored-by: Quentin Torroba <quentin.torroba@mistral.ai>
Co-authored-by: Clement Sirieix <clem.sirieix@gmail.com>
Co-authored-by: Kim-Adeline Miguel <kimadeline.miguel@mistral.ai>
Co-authored-by: Simon Van de Kerckhove <simon.vandekerckhove@mistral.ai>
Co-authored-by: Vincent Guilloux <vincent.guilloux@mistral.ai>
Co-authored-by: Michel Thomazo <michel.thomazo@mistral.ai>
Co-authored-by: Mistral Vibe <vibe@mistral.ai>
2026-03-16 17:51:47 +01:00

54 lines
1.6 KiB
Python

from __future__ import annotations
from pathlib import Path
from vibe import VIBE_ROOT
from vibe.acp.tools.base import AcpToolState, BaseAcpTool
from vibe.core.tools.base import ToolError
from vibe.core.tools.builtins.read_file import (
ReadFile as CoreReadFileTool,
ReadFileArgs,
ReadFileResult,
ReadFileState,
_ReadResult,
)
ReadFileResult = ReadFileResult
class AcpReadFileState(ReadFileState, AcpToolState):
pass
class ReadFile(CoreReadFileTool, BaseAcpTool[AcpReadFileState]):
state: AcpReadFileState
prompt_path = VIBE_ROOT / "core" / "tools" / "builtins" / "prompts" / "read_file.md"
@classmethod
def _get_tool_state_class(cls) -> type[AcpReadFileState]:
return AcpReadFileState
async def _read_file(self, args: ReadFileArgs, file_path: Path) -> _ReadResult:
client, session_id, _ = self._load_state()
line = args.offset + 1 if args.offset > 0 else None
limit = args.limit
await self._send_in_progress_session_update()
try:
response = await client.read_text_file(
session_id=session_id, path=str(file_path), line=line, limit=limit
)
except Exception as e:
raise ToolError(f"Error reading {file_path}: {e}") from e
content_lines = response.content.splitlines(keepends=True)
lines_read = len(content_lines)
bytes_read = sum(len(line.encode("utf-8")) for line in content_lines)
was_truncated = args.limit is not None and lines_read >= args.limit
return _ReadResult(
lines=content_lines, bytes_read=bytes_read, was_truncated=was_truncated
)