klaude-code 1.2.6__py3-none-any.whl → 1.8.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- klaude_code/auth/__init__.py +24 -0
- klaude_code/auth/codex/__init__.py +20 -0
- klaude_code/auth/codex/exceptions.py +17 -0
- klaude_code/auth/codex/jwt_utils.py +45 -0
- klaude_code/auth/codex/oauth.py +229 -0
- klaude_code/auth/codex/token_manager.py +84 -0
- klaude_code/cli/auth_cmd.py +73 -0
- klaude_code/cli/config_cmd.py +91 -0
- klaude_code/cli/cost_cmd.py +338 -0
- klaude_code/cli/debug.py +78 -0
- klaude_code/cli/list_model.py +307 -0
- klaude_code/cli/main.py +233 -134
- klaude_code/cli/runtime.py +309 -117
- klaude_code/{version.py → cli/self_update.py} +114 -5
- klaude_code/cli/session_cmd.py +37 -21
- klaude_code/command/__init__.py +88 -27
- klaude_code/command/clear_cmd.py +8 -7
- klaude_code/command/command_abc.py +31 -31
- klaude_code/command/debug_cmd.py +79 -0
- klaude_code/command/export_cmd.py +19 -53
- klaude_code/command/export_online_cmd.py +154 -0
- klaude_code/command/fork_session_cmd.py +267 -0
- klaude_code/command/help_cmd.py +7 -8
- klaude_code/command/model_cmd.py +60 -10
- klaude_code/command/model_select.py +84 -0
- klaude_code/command/prompt-jj-describe.md +32 -0
- klaude_code/command/prompt_command.py +19 -11
- klaude_code/command/refresh_cmd.py +8 -10
- klaude_code/command/registry.py +139 -40
- klaude_code/command/release_notes_cmd.py +84 -0
- klaude_code/command/resume_cmd.py +111 -0
- klaude_code/command/status_cmd.py +104 -60
- klaude_code/command/terminal_setup_cmd.py +7 -9
- klaude_code/command/thinking_cmd.py +98 -0
- klaude_code/config/__init__.py +14 -6
- klaude_code/config/assets/__init__.py +1 -0
- klaude_code/config/assets/builtin_config.yaml +303 -0
- klaude_code/config/builtin_config.py +38 -0
- klaude_code/config/config.py +378 -109
- klaude_code/config/select_model.py +117 -53
- klaude_code/config/thinking.py +269 -0
- klaude_code/{const/__init__.py → const.py} +50 -19
- klaude_code/core/agent.py +20 -28
- klaude_code/core/executor.py +327 -112
- klaude_code/core/manager/__init__.py +2 -4
- klaude_code/core/manager/llm_clients.py +1 -15
- klaude_code/core/manager/llm_clients_builder.py +10 -11
- klaude_code/core/manager/sub_agent_manager.py +37 -6
- klaude_code/core/prompt.py +63 -44
- klaude_code/core/prompts/prompt-claude-code.md +2 -13
- klaude_code/core/prompts/prompt-codex-gpt-5-1-codex-max.md +117 -0
- klaude_code/core/prompts/prompt-codex-gpt-5-2-codex.md +117 -0
- klaude_code/core/prompts/prompt-codex.md +9 -42
- klaude_code/core/prompts/prompt-minimal.md +12 -0
- klaude_code/core/prompts/{prompt-subagent-explore.md → prompt-sub-agent-explore.md} +16 -3
- klaude_code/core/prompts/{prompt-subagent-oracle.md → prompt-sub-agent-oracle.md} +1 -2
- klaude_code/core/prompts/prompt-sub-agent-web.md +51 -0
- klaude_code/core/reminders.py +283 -95
- klaude_code/core/task.py +113 -75
- klaude_code/core/tool/__init__.py +24 -31
- klaude_code/core/tool/file/_utils.py +36 -0
- klaude_code/core/tool/file/apply_patch.py +17 -25
- klaude_code/core/tool/file/apply_patch_tool.py +57 -77
- klaude_code/core/tool/file/diff_builder.py +151 -0
- klaude_code/core/tool/file/edit_tool.py +50 -63
- klaude_code/core/tool/file/move_tool.md +41 -0
- klaude_code/core/tool/file/move_tool.py +435 -0
- klaude_code/core/tool/file/read_tool.md +1 -1
- klaude_code/core/tool/file/read_tool.py +86 -86
- klaude_code/core/tool/file/write_tool.py +59 -69
- klaude_code/core/tool/report_back_tool.py +84 -0
- klaude_code/core/tool/shell/bash_tool.py +265 -22
- klaude_code/core/tool/shell/command_safety.py +3 -6
- klaude_code/core/tool/{memory → skill}/skill_tool.py +16 -26
- klaude_code/core/tool/sub_agent_tool.py +13 -2
- klaude_code/core/tool/todo/todo_write_tool.md +0 -157
- klaude_code/core/tool/todo/todo_write_tool.py +1 -1
- klaude_code/core/tool/todo/todo_write_tool_raw.md +182 -0
- klaude_code/core/tool/todo/update_plan_tool.py +1 -1
- klaude_code/core/tool/tool_abc.py +18 -0
- klaude_code/core/tool/tool_context.py +27 -12
- klaude_code/core/tool/tool_registry.py +7 -7
- klaude_code/core/tool/tool_runner.py +44 -36
- klaude_code/core/tool/truncation.py +29 -14
- klaude_code/core/tool/web/mermaid_tool.md +43 -0
- klaude_code/core/tool/web/mermaid_tool.py +2 -5
- klaude_code/core/tool/web/web_fetch_tool.md +1 -1
- klaude_code/core/tool/web/web_fetch_tool.py +112 -22
- klaude_code/core/tool/web/web_search_tool.md +23 -0
- klaude_code/core/tool/web/web_search_tool.py +130 -0
- klaude_code/core/turn.py +168 -66
- klaude_code/llm/__init__.py +2 -10
- klaude_code/llm/anthropic/client.py +190 -178
- klaude_code/llm/anthropic/input.py +39 -15
- klaude_code/llm/bedrock/__init__.py +3 -0
- klaude_code/llm/bedrock/client.py +60 -0
- klaude_code/llm/client.py +7 -21
- klaude_code/llm/codex/__init__.py +5 -0
- klaude_code/llm/codex/client.py +149 -0
- klaude_code/llm/google/__init__.py +3 -0
- klaude_code/llm/google/client.py +309 -0
- klaude_code/llm/google/input.py +215 -0
- klaude_code/llm/input_common.py +3 -9
- klaude_code/llm/openai_compatible/client.py +72 -164
- klaude_code/llm/openai_compatible/input.py +6 -4
- klaude_code/llm/openai_compatible/stream.py +273 -0
- klaude_code/llm/openai_compatible/tool_call_accumulator.py +17 -1
- klaude_code/llm/openrouter/client.py +89 -160
- klaude_code/llm/openrouter/input.py +18 -30
- klaude_code/llm/openrouter/reasoning.py +118 -0
- klaude_code/llm/registry.py +39 -7
- klaude_code/llm/responses/client.py +184 -171
- klaude_code/llm/responses/input.py +20 -1
- klaude_code/llm/usage.py +17 -12
- klaude_code/protocol/commands.py +17 -1
- klaude_code/protocol/events.py +31 -4
- klaude_code/protocol/llm_param.py +13 -10
- klaude_code/protocol/model.py +232 -29
- klaude_code/protocol/op.py +90 -1
- klaude_code/protocol/op_handler.py +35 -1
- klaude_code/protocol/sub_agent/__init__.py +117 -0
- klaude_code/protocol/sub_agent/explore.py +63 -0
- klaude_code/protocol/sub_agent/oracle.py +91 -0
- klaude_code/protocol/sub_agent/task.py +61 -0
- klaude_code/protocol/sub_agent/web.py +79 -0
- klaude_code/protocol/tools.py +4 -2
- klaude_code/session/__init__.py +2 -2
- klaude_code/session/codec.py +71 -0
- klaude_code/session/export.py +293 -86
- klaude_code/session/selector.py +89 -67
- klaude_code/session/session.py +320 -309
- klaude_code/session/store.py +220 -0
- klaude_code/session/templates/export_session.html +595 -83
- klaude_code/session/templates/mermaid_viewer.html +926 -0
- klaude_code/skill/__init__.py +27 -0
- klaude_code/skill/assets/deslop/SKILL.md +17 -0
- klaude_code/skill/assets/dev-docs/SKILL.md +108 -0
- klaude_code/skill/assets/handoff/SKILL.md +39 -0
- klaude_code/skill/assets/jj-workspace/SKILL.md +20 -0
- klaude_code/skill/assets/skill-creator/SKILL.md +139 -0
- klaude_code/{core/tool/memory/skill_loader.py → skill/loader.py} +55 -15
- klaude_code/skill/manager.py +70 -0
- klaude_code/skill/system_skills.py +192 -0
- klaude_code/trace/__init__.py +20 -2
- klaude_code/trace/log.py +150 -5
- klaude_code/ui/__init__.py +4 -9
- klaude_code/ui/core/input.py +1 -1
- klaude_code/ui/core/stage_manager.py +7 -7
- klaude_code/ui/modes/debug/display.py +2 -1
- klaude_code/ui/modes/repl/__init__.py +3 -48
- klaude_code/ui/modes/repl/clipboard.py +5 -5
- klaude_code/ui/modes/repl/completers.py +487 -123
- klaude_code/ui/modes/repl/display.py +5 -4
- klaude_code/ui/modes/repl/event_handler.py +370 -117
- klaude_code/ui/modes/repl/input_prompt_toolkit.py +552 -105
- klaude_code/ui/modes/repl/key_bindings.py +146 -23
- klaude_code/ui/modes/repl/renderer.py +189 -99
- klaude_code/ui/renderers/assistant.py +9 -2
- klaude_code/ui/renderers/bash_syntax.py +178 -0
- klaude_code/ui/renderers/common.py +78 -0
- klaude_code/ui/renderers/developer.py +104 -48
- klaude_code/ui/renderers/diffs.py +87 -6
- klaude_code/ui/renderers/errors.py +11 -6
- klaude_code/ui/renderers/mermaid_viewer.py +57 -0
- klaude_code/ui/renderers/metadata.py +112 -76
- klaude_code/ui/renderers/sub_agent.py +92 -7
- klaude_code/ui/renderers/thinking.py +40 -18
- klaude_code/ui/renderers/tools.py +405 -227
- klaude_code/ui/renderers/user_input.py +73 -13
- klaude_code/ui/rich/__init__.py +10 -1
- klaude_code/ui/rich/cjk_wrap.py +228 -0
- klaude_code/ui/rich/code_panel.py +131 -0
- klaude_code/ui/rich/live.py +17 -0
- klaude_code/ui/rich/markdown.py +305 -170
- klaude_code/ui/rich/searchable_text.py +10 -13
- klaude_code/ui/rich/status.py +190 -49
- klaude_code/ui/rich/theme.py +135 -39
- klaude_code/ui/terminal/__init__.py +55 -0
- klaude_code/ui/terminal/color.py +1 -1
- klaude_code/ui/terminal/control.py +13 -22
- klaude_code/ui/terminal/notifier.py +44 -4
- klaude_code/ui/terminal/selector.py +658 -0
- klaude_code/ui/utils/common.py +0 -18
- klaude_code-1.8.0.dist-info/METADATA +377 -0
- klaude_code-1.8.0.dist-info/RECORD +219 -0
- {klaude_code-1.2.6.dist-info → klaude_code-1.8.0.dist-info}/entry_points.txt +1 -0
- klaude_code/command/diff_cmd.py +0 -138
- klaude_code/command/prompt-dev-docs-update.md +0 -56
- klaude_code/command/prompt-dev-docs.md +0 -46
- klaude_code/config/list_model.py +0 -162
- klaude_code/core/manager/agent_manager.py +0 -127
- klaude_code/core/prompts/prompt-subagent-webfetch.md +0 -46
- klaude_code/core/tool/file/multi_edit_tool.md +0 -42
- klaude_code/core/tool/file/multi_edit_tool.py +0 -199
- klaude_code/core/tool/memory/memory_tool.md +0 -16
- klaude_code/core/tool/memory/memory_tool.py +0 -462
- klaude_code/llm/openrouter/reasoning_handler.py +0 -209
- klaude_code/protocol/sub_agent.py +0 -348
- klaude_code/ui/utils/debouncer.py +0 -42
- klaude_code-1.2.6.dist-info/METADATA +0 -178
- klaude_code-1.2.6.dist-info/RECORD +0 -167
- /klaude_code/core/prompts/{prompt-subagent.md → prompt-sub-agent.md} +0 -0
- /klaude_code/core/tool/{memory → skill}/__init__.py +0 -0
- /klaude_code/core/tool/{memory → skill}/skill_tool.md +0 -0
- {klaude_code-1.2.6.dist-info → klaude_code-1.8.0.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import contextlib
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from pydantic import BaseModel, Field
|
|
9
|
+
|
|
10
|
+
from klaude_code.core.tool.file._utils import file_exists, hash_text_sha256, is_directory, read_text, write_text
|
|
11
|
+
from klaude_code.core.tool.file.diff_builder import build_structured_diff
|
|
12
|
+
from klaude_code.core.tool.tool_abc import ToolABC, load_desc
|
|
13
|
+
from klaude_code.core.tool.tool_context import get_current_file_tracker
|
|
14
|
+
from klaude_code.core.tool.tool_registry import register
|
|
15
|
+
from klaude_code.protocol import llm_param, model, tools
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class MoveArguments(BaseModel):
|
|
19
|
+
source_file_path: str
|
|
20
|
+
start_line: int = Field(ge=1, description="Start line number (1-indexed, inclusive)")
|
|
21
|
+
end_line: int = Field(ge=1, description="End line number (1-indexed, inclusive)")
|
|
22
|
+
target_file_path: str
|
|
23
|
+
insert_line: int = Field(ge=1, description="Line number to insert before (1-indexed)")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _build_context_snippet(
|
|
27
|
+
all_lines: list[str],
|
|
28
|
+
start_line: int,
|
|
29
|
+
end_line: int,
|
|
30
|
+
context_lines: int = 3,
|
|
31
|
+
marker: str = "cut here",
|
|
32
|
+
) -> str:
|
|
33
|
+
"""Build a snippet showing context around a cut/insert point.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
all_lines: All lines of the file (after modification).
|
|
37
|
+
start_line: 1-indexed start line of the context focus area.
|
|
38
|
+
end_line: 1-indexed end line of the context focus area.
|
|
39
|
+
context_lines: Number of context lines before and after.
|
|
40
|
+
marker: Text to show in the separator line.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
Formatted snippet with context and separator.
|
|
44
|
+
"""
|
|
45
|
+
result: list[str] = []
|
|
46
|
+
|
|
47
|
+
# Context before
|
|
48
|
+
ctx_start = max(1, start_line - context_lines)
|
|
49
|
+
for line_no in range(ctx_start, start_line):
|
|
50
|
+
idx = line_no - 1
|
|
51
|
+
if idx < len(all_lines):
|
|
52
|
+
content = all_lines[idx].rstrip("\n")
|
|
53
|
+
result.append(f"{line_no:>6}\t{content}")
|
|
54
|
+
|
|
55
|
+
# Separator
|
|
56
|
+
result.append(f" -------- {marker} --------")
|
|
57
|
+
|
|
58
|
+
# Context after
|
|
59
|
+
ctx_end = min(len(all_lines), end_line + context_lines)
|
|
60
|
+
for line_no in range(end_line, ctx_end + 1):
|
|
61
|
+
idx = line_no - 1
|
|
62
|
+
if idx < len(all_lines):
|
|
63
|
+
content = all_lines[idx].rstrip("\n")
|
|
64
|
+
result.append(f"{line_no:>6}\t{content}")
|
|
65
|
+
|
|
66
|
+
return "\n".join(result)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _build_insert_context_snippet(
|
|
70
|
+
all_lines: list[str],
|
|
71
|
+
insert_line: int,
|
|
72
|
+
inserted_count: int,
|
|
73
|
+
context_lines: int = 3,
|
|
74
|
+
) -> str:
|
|
75
|
+
"""Build a snippet showing context around inserted content.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
all_lines: All lines of the file (after insertion).
|
|
79
|
+
insert_line: 1-indexed line where content was inserted.
|
|
80
|
+
inserted_count: Number of lines that were inserted.
|
|
81
|
+
context_lines: Number of context lines before and after.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
Formatted snippet with context and inserted content highlighted.
|
|
85
|
+
"""
|
|
86
|
+
result: list[str] = []
|
|
87
|
+
insert_end = insert_line + inserted_count - 1
|
|
88
|
+
|
|
89
|
+
# Context before
|
|
90
|
+
ctx_start = max(1, insert_line - context_lines)
|
|
91
|
+
for line_no in range(ctx_start, insert_line):
|
|
92
|
+
idx = line_no - 1
|
|
93
|
+
if idx < len(all_lines):
|
|
94
|
+
content = all_lines[idx].rstrip("\n")
|
|
95
|
+
result.append(f"{line_no:>6}\t{content}")
|
|
96
|
+
|
|
97
|
+
# Start separator
|
|
98
|
+
result.append(" -------- inserted --------")
|
|
99
|
+
|
|
100
|
+
# Inserted content
|
|
101
|
+
for line_no in range(insert_line, insert_end + 1):
|
|
102
|
+
idx = line_no - 1
|
|
103
|
+
if idx < len(all_lines):
|
|
104
|
+
content = all_lines[idx].rstrip("\n")
|
|
105
|
+
result.append(f"{line_no:>6}\t{content}")
|
|
106
|
+
|
|
107
|
+
# End separator
|
|
108
|
+
result.append(" -------- end --------")
|
|
109
|
+
|
|
110
|
+
# Context after
|
|
111
|
+
ctx_end = min(len(all_lines), insert_end + context_lines)
|
|
112
|
+
for line_no in range(insert_end + 1, ctx_end + 1):
|
|
113
|
+
idx = line_no - 1
|
|
114
|
+
if idx < len(all_lines):
|
|
115
|
+
content = all_lines[idx].rstrip("\n")
|
|
116
|
+
result.append(f"{line_no:>6}\t{content}")
|
|
117
|
+
|
|
118
|
+
return "\n".join(result)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@register(tools.MOVE)
|
|
122
|
+
class MoveTool(ToolABC):
|
|
123
|
+
@classmethod
|
|
124
|
+
def schema(cls) -> llm_param.ToolSchema:
|
|
125
|
+
return llm_param.ToolSchema(
|
|
126
|
+
name=tools.MOVE,
|
|
127
|
+
type="function",
|
|
128
|
+
description=load_desc(Path(__file__).parent / "move_tool.md"),
|
|
129
|
+
parameters={
|
|
130
|
+
"type": "object",
|
|
131
|
+
"properties": {
|
|
132
|
+
"source_file_path": {
|
|
133
|
+
"type": "string",
|
|
134
|
+
"description": "The absolute path to the source file to cut from",
|
|
135
|
+
},
|
|
136
|
+
"start_line": {
|
|
137
|
+
"type": "integer",
|
|
138
|
+
"description": "Start line number (1-indexed, inclusive)",
|
|
139
|
+
},
|
|
140
|
+
"end_line": {
|
|
141
|
+
"type": "integer",
|
|
142
|
+
"description": "End line number (1-indexed, inclusive)",
|
|
143
|
+
},
|
|
144
|
+
"target_file_path": {
|
|
145
|
+
"type": "string",
|
|
146
|
+
"description": "The absolute path to the target file to paste into",
|
|
147
|
+
},
|
|
148
|
+
"insert_line": {
|
|
149
|
+
"type": "integer",
|
|
150
|
+
"description": "Line number to insert before (1-indexed)",
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
"required": ["source_file_path", "start_line", "end_line", "target_file_path", "insert_line"],
|
|
154
|
+
"additionalProperties": False,
|
|
155
|
+
},
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
@classmethod
|
|
159
|
+
async def call(cls, arguments: str) -> model.ToolResultItem:
|
|
160
|
+
try:
|
|
161
|
+
args = MoveArguments.model_validate_json(arguments)
|
|
162
|
+
except ValueError as e:
|
|
163
|
+
return model.ToolResultItem(status="error", output=f"Invalid arguments: {e}")
|
|
164
|
+
|
|
165
|
+
source_path = os.path.abspath(args.source_file_path)
|
|
166
|
+
target_path = os.path.abspath(args.target_file_path)
|
|
167
|
+
same_file = source_path == target_path
|
|
168
|
+
|
|
169
|
+
# Validate paths
|
|
170
|
+
if is_directory(source_path):
|
|
171
|
+
return model.ToolResultItem(
|
|
172
|
+
status="error",
|
|
173
|
+
output="<tool_use_error>Source path is a directory, not a file.</tool_use_error>",
|
|
174
|
+
)
|
|
175
|
+
if is_directory(target_path):
|
|
176
|
+
return model.ToolResultItem(
|
|
177
|
+
status="error",
|
|
178
|
+
output="<tool_use_error>Target path is a directory, not a file.</tool_use_error>",
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
# Validate line range
|
|
182
|
+
if args.start_line > args.end_line:
|
|
183
|
+
return model.ToolResultItem(
|
|
184
|
+
status="error",
|
|
185
|
+
output="<tool_use_error>start_line must be <= end_line.</tool_use_error>",
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
# Check file tracker
|
|
189
|
+
file_tracker = get_current_file_tracker()
|
|
190
|
+
source_exists = file_exists(source_path)
|
|
191
|
+
target_exists = file_exists(target_path)
|
|
192
|
+
|
|
193
|
+
if not source_exists:
|
|
194
|
+
return model.ToolResultItem(
|
|
195
|
+
status="error",
|
|
196
|
+
output="<tool_use_error>Source file does not exist.</tool_use_error>",
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
source_status: model.FileStatus | None = None
|
|
200
|
+
target_status: model.FileStatus | None = None
|
|
201
|
+
|
|
202
|
+
if file_tracker is not None:
|
|
203
|
+
source_status = file_tracker.get(source_path)
|
|
204
|
+
if source_status is None:
|
|
205
|
+
return model.ToolResultItem(
|
|
206
|
+
status="error",
|
|
207
|
+
output="Source file has not been read yet. Read it first.",
|
|
208
|
+
)
|
|
209
|
+
if target_exists:
|
|
210
|
+
target_status = file_tracker.get(target_path)
|
|
211
|
+
if target_status is None:
|
|
212
|
+
return model.ToolResultItem(
|
|
213
|
+
status="error",
|
|
214
|
+
output="Target file has not been read yet. Read it first before writing to it.",
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
# Read source file
|
|
218
|
+
try:
|
|
219
|
+
source_content = await asyncio.to_thread(read_text, source_path)
|
|
220
|
+
except OSError as e:
|
|
221
|
+
return model.ToolResultItem(
|
|
222
|
+
status="error", output=f"<tool_use_error>Failed to read source: {e}</tool_use_error>"
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
# Verify source hasn't been modified externally
|
|
226
|
+
if source_status is not None and source_status.content_sha256 is not None:
|
|
227
|
+
current_sha256 = hash_text_sha256(source_content)
|
|
228
|
+
if current_sha256 != source_status.content_sha256:
|
|
229
|
+
return model.ToolResultItem(
|
|
230
|
+
status="error",
|
|
231
|
+
output="Source file has been modified externally. Read it first before editing.",
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
source_lines = source_content.splitlines(keepends=True)
|
|
235
|
+
|
|
236
|
+
# Validate line numbers against actual file
|
|
237
|
+
if args.start_line > len(source_lines):
|
|
238
|
+
return model.ToolResultItem(
|
|
239
|
+
status="error",
|
|
240
|
+
output=f"<tool_use_error>start_line {args.start_line} exceeds file length {len(source_lines)}.</tool_use_error>",
|
|
241
|
+
)
|
|
242
|
+
if args.end_line > len(source_lines):
|
|
243
|
+
return model.ToolResultItem(
|
|
244
|
+
status="error",
|
|
245
|
+
output=f"<tool_use_error>end_line {args.end_line} exceeds file length {len(source_lines)}.</tool_use_error>",
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
# Extract the lines to move (convert to 0-indexed)
|
|
249
|
+
cut_lines = source_lines[args.start_line - 1 : args.end_line]
|
|
250
|
+
|
|
251
|
+
# Read target file content (if exists)
|
|
252
|
+
target_before = ""
|
|
253
|
+
if target_exists:
|
|
254
|
+
try:
|
|
255
|
+
target_before = await asyncio.to_thread(read_text, target_path)
|
|
256
|
+
except OSError as e:
|
|
257
|
+
return model.ToolResultItem(
|
|
258
|
+
status="error", output=f"<tool_use_error>Failed to read target: {e}</tool_use_error>"
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
# Verify target hasn't been modified externally
|
|
262
|
+
if target_status is not None and target_status.content_sha256 is not None:
|
|
263
|
+
current_sha256 = hash_text_sha256(target_before)
|
|
264
|
+
if current_sha256 != target_status.content_sha256:
|
|
265
|
+
return model.ToolResultItem(
|
|
266
|
+
status="error",
|
|
267
|
+
output="Target file has been modified externally. Read it first before writing to it.",
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
# For new target file, only allow insert_line = 1
|
|
271
|
+
if not target_exists and args.insert_line != 1:
|
|
272
|
+
return model.ToolResultItem(
|
|
273
|
+
status="error",
|
|
274
|
+
output="<tool_use_error>Target file does not exist. Use insert_line=1 to create new file.</tool_use_error>",
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
# Build new content for both files
|
|
278
|
+
source_before = source_content
|
|
279
|
+
|
|
280
|
+
if same_file:
|
|
281
|
+
# Same file move: more complex logic
|
|
282
|
+
# First remove the cut lines, then insert at adjusted position
|
|
283
|
+
new_lines = source_lines[: args.start_line - 1] + source_lines[args.end_line :]
|
|
284
|
+
|
|
285
|
+
# Adjust insert position if it was after the cut region
|
|
286
|
+
adjusted_insert = args.insert_line
|
|
287
|
+
if args.insert_line > args.end_line:
|
|
288
|
+
adjusted_insert -= args.end_line - args.start_line + 1
|
|
289
|
+
elif args.insert_line > args.start_line:
|
|
290
|
+
# Insert position is within the cut region - error
|
|
291
|
+
return model.ToolResultItem(
|
|
292
|
+
status="error",
|
|
293
|
+
output="<tool_use_error>insert_line cannot be within the cut range for same-file move.</tool_use_error>",
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
# Validate adjusted insert line
|
|
297
|
+
if adjusted_insert > len(new_lines) + 1:
|
|
298
|
+
return model.ToolResultItem(
|
|
299
|
+
status="error",
|
|
300
|
+
output=f"<tool_use_error>insert_line {args.insert_line} is out of bounds after cut.</tool_use_error>",
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
# Insert at adjusted position
|
|
304
|
+
final_lines = new_lines[: adjusted_insert - 1] + cut_lines + new_lines[adjusted_insert - 1 :]
|
|
305
|
+
source_after = "".join(final_lines)
|
|
306
|
+
target_after = source_after # Same file
|
|
307
|
+
|
|
308
|
+
# Write the file once
|
|
309
|
+
try:
|
|
310
|
+
await asyncio.to_thread(write_text, source_path, source_after)
|
|
311
|
+
except OSError as e:
|
|
312
|
+
return model.ToolResultItem(
|
|
313
|
+
status="error", output=f"<tool_use_error>Failed to write: {e}</tool_use_error>"
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
# Update tracker
|
|
317
|
+
if file_tracker is not None:
|
|
318
|
+
with contextlib.suppress(Exception):
|
|
319
|
+
existing = file_tracker.get(source_path)
|
|
320
|
+
is_mem = existing.is_memory if existing else False
|
|
321
|
+
file_tracker[source_path] = model.FileStatus(
|
|
322
|
+
mtime=Path(source_path).stat().st_mtime,
|
|
323
|
+
content_sha256=hash_text_sha256(source_after),
|
|
324
|
+
is_memory=is_mem,
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
ui_extra = build_structured_diff(source_before, source_after, file_path=source_path)
|
|
328
|
+
cut_count = args.end_line - args.start_line + 1
|
|
329
|
+
|
|
330
|
+
# Build context snippets for same-file move
|
|
331
|
+
final_lines = source_after.splitlines(keepends=True)
|
|
332
|
+
# Show context around cut location (now joined)
|
|
333
|
+
cut_context = _build_context_snippet(final_lines, args.start_line, args.start_line, marker="cut here")
|
|
334
|
+
# Show context around insert location
|
|
335
|
+
insert_context = _build_insert_context_snippet(final_lines, adjusted_insert, cut_count)
|
|
336
|
+
|
|
337
|
+
output = (
|
|
338
|
+
f"Moved {cut_count} lines within {source_path} "
|
|
339
|
+
f"(from lines {args.start_line}-{args.end_line} to line {args.insert_line}).\n\n"
|
|
340
|
+
f"Source context (after cut):\n{cut_context}\n\n"
|
|
341
|
+
f"Insert context:\n{insert_context}"
|
|
342
|
+
)
|
|
343
|
+
return model.ToolResultItem(
|
|
344
|
+
status="success",
|
|
345
|
+
output=output,
|
|
346
|
+
ui_extra=ui_extra,
|
|
347
|
+
)
|
|
348
|
+
else:
|
|
349
|
+
# Different files
|
|
350
|
+
# Remove lines from source
|
|
351
|
+
new_source_lines = source_lines[: args.start_line - 1] + source_lines[args.end_line :]
|
|
352
|
+
source_after = "".join(new_source_lines)
|
|
353
|
+
|
|
354
|
+
# Insert into target
|
|
355
|
+
target_lines = target_before.splitlines(keepends=True) if target_before else []
|
|
356
|
+
|
|
357
|
+
# Validate insert_line for existing target
|
|
358
|
+
if target_exists and args.insert_line > len(target_lines) + 1:
|
|
359
|
+
return model.ToolResultItem(
|
|
360
|
+
status="error",
|
|
361
|
+
output=f"<tool_use_error>insert_line {args.insert_line} exceeds target file length + 1.</tool_use_error>",
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
new_target_lines = target_lines[: args.insert_line - 1] + cut_lines + target_lines[args.insert_line - 1 :]
|
|
365
|
+
target_after = "".join(new_target_lines)
|
|
366
|
+
|
|
367
|
+
# Ensure target ends with newline if source content did
|
|
368
|
+
if cut_lines and not target_after.endswith("\n"):
|
|
369
|
+
target_after += "\n"
|
|
370
|
+
|
|
371
|
+
# Write both files
|
|
372
|
+
try:
|
|
373
|
+
await asyncio.to_thread(write_text, source_path, source_after)
|
|
374
|
+
await asyncio.to_thread(write_text, target_path, target_after)
|
|
375
|
+
except OSError as e:
|
|
376
|
+
return model.ToolResultItem(
|
|
377
|
+
status="error", output=f"<tool_use_error>Failed to write: {e}</tool_use_error>"
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
# Update tracker for both files
|
|
381
|
+
if file_tracker is not None:
|
|
382
|
+
with contextlib.suppress(Exception):
|
|
383
|
+
existing = file_tracker.get(source_path)
|
|
384
|
+
is_mem = existing.is_memory if existing else False
|
|
385
|
+
file_tracker[source_path] = model.FileStatus(
|
|
386
|
+
mtime=Path(source_path).stat().st_mtime,
|
|
387
|
+
content_sha256=hash_text_sha256(source_after),
|
|
388
|
+
is_memory=is_mem,
|
|
389
|
+
)
|
|
390
|
+
with contextlib.suppress(Exception):
|
|
391
|
+
existing = file_tracker.get(target_path)
|
|
392
|
+
is_mem = existing.is_memory if existing else False
|
|
393
|
+
file_tracker[target_path] = model.FileStatus(
|
|
394
|
+
mtime=Path(target_path).stat().st_mtime,
|
|
395
|
+
content_sha256=hash_text_sha256(target_after),
|
|
396
|
+
is_memory=is_mem,
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
# Build UI extra with diffs for both files
|
|
400
|
+
source_diff = build_structured_diff(source_before, source_after, file_path=source_path)
|
|
401
|
+
target_diff = build_structured_diff(target_before, target_after, file_path=target_path)
|
|
402
|
+
|
|
403
|
+
ui_extra: model.ToolResultUIExtra | None = None
|
|
404
|
+
if source_diff and target_diff:
|
|
405
|
+
ui_extra = model.MultiUIExtra(items=[source_diff, target_diff])
|
|
406
|
+
elif source_diff:
|
|
407
|
+
ui_extra = source_diff
|
|
408
|
+
elif target_diff:
|
|
409
|
+
ui_extra = target_diff
|
|
410
|
+
|
|
411
|
+
cut_count = args.end_line - args.start_line + 1
|
|
412
|
+
action = "created" if not target_exists else "updated"
|
|
413
|
+
|
|
414
|
+
# Build context snippets for different-file move
|
|
415
|
+
source_after_lines = source_after.splitlines(keepends=True)
|
|
416
|
+
target_after_lines = target_after.splitlines(keepends=True)
|
|
417
|
+
|
|
418
|
+
# Show context around cut location in source file
|
|
419
|
+
source_context = _build_context_snippet(
|
|
420
|
+
source_after_lines, args.start_line, args.start_line, marker="cut here"
|
|
421
|
+
)
|
|
422
|
+
# Show context around insert location in target file
|
|
423
|
+
target_context = _build_insert_context_snippet(target_after_lines, args.insert_line, cut_count)
|
|
424
|
+
|
|
425
|
+
output = (
|
|
426
|
+
f"Moved {cut_count} lines from {source_path} (lines {args.start_line}-{args.end_line}) "
|
|
427
|
+
f"to {target_path} ({action}) at line {args.insert_line}.\n\n"
|
|
428
|
+
f"Source file context (after move):\n{source_context}\n\n"
|
|
429
|
+
f"Target file context (after insert):\n{target_context}"
|
|
430
|
+
)
|
|
431
|
+
return model.ToolResultItem(
|
|
432
|
+
status="success",
|
|
433
|
+
output=output,
|
|
434
|
+
ui_extra=ui_extra,
|
|
435
|
+
)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Reads a file from the local filesystem. You can access any file directly by using this tool.
|
|
2
2
|
Assume this tool is able to read all files on the machine. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned.
|
|
3
|
+
When you need to read an image, use this tool.
|
|
3
4
|
|
|
4
5
|
Usage:
|
|
5
6
|
- The file_path parameter must be an absolute path, not a relative path
|
|
@@ -11,4 +12,3 @@ Usage:
|
|
|
11
12
|
- This tool can only read files, not directories. To read a directory, use an ls command via the Bash tool.
|
|
12
13
|
- You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful.
|
|
13
14
|
- If you read a file that exists but has empty contents you will receive a system reminder warning in place of file contents.
|
|
14
|
-
- This tool does NOT support reading PDF files. Use a Python script with `pdfplumber` (for text/tables) or `pypdf` (for basic operations) to extract content from PDFs.
|