auto-coder 0.1.197__py3-none-any.whl → 0.1.198__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.
Potentially problematic release.
This version of auto-coder might be problematic. Click here for more details.
- {auto_coder-0.1.197.dist-info → auto_coder-0.1.198.dist-info}/METADATA +1 -1
- {auto_coder-0.1.197.dist-info → auto_coder-0.1.198.dist-info}/RECORD +10 -10
- autocoder/agent/coder.py +1234 -17
- autocoder/common/__init__.py +18 -1
- autocoder/common/code_auto_merge_editblock.py +14 -8
- autocoder/version.py +1 -1
- {auto_coder-0.1.197.dist-info → auto_coder-0.1.198.dist-info}/LICENSE +0 -0
- {auto_coder-0.1.197.dist-info → auto_coder-0.1.198.dist-info}/WHEEL +0 -0
- {auto_coder-0.1.197.dist-info → auto_coder-0.1.198.dist-info}/entry_points.txt +0 -0
- {auto_coder-0.1.197.dist-info → auto_coder-0.1.198.dist-info}/top_level.txt +0 -0
autocoder/agent/coder.py
CHANGED
|
@@ -1,24 +1,1241 @@
|
|
|
1
|
+
from autocoder.common import detect_env
|
|
2
|
+
from typing import Dict, List, Literal, Union, Tuple, Generator, AsyncGenerator
|
|
3
|
+
import pydantic
|
|
4
|
+
from enum import Enum
|
|
5
|
+
import os
|
|
6
|
+
import asyncio
|
|
7
|
+
import subprocess
|
|
8
|
+
import re
|
|
9
|
+
from prompt_toolkit import PromptSession
|
|
10
|
+
from prompt_toolkit.shortcuts import prompt
|
|
11
|
+
from prompt_toolkit.styles import Style
|
|
12
|
+
from rich.console import Console
|
|
13
|
+
from rich.panel import Panel
|
|
14
|
+
from prompt_toolkit import PromptSession
|
|
1
15
|
import byzerllm
|
|
2
|
-
from pydantic import BaseModel,Field
|
|
3
16
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
17
|
+
|
|
18
|
+
class TextContent(pydantic.BaseModel):
|
|
19
|
+
type: Literal["text"]
|
|
20
|
+
content: str
|
|
21
|
+
partial: bool
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ToolUseName(str, Enum):
|
|
25
|
+
execute_command = "execute_command"
|
|
26
|
+
read_file = "read_file"
|
|
27
|
+
write_to_file = "write_to_file"
|
|
28
|
+
search_files = "search_files"
|
|
29
|
+
list_files = "list_files"
|
|
30
|
+
list_code_definition_names = "list_code_definition_names"
|
|
31
|
+
browser_action = "browser_action"
|
|
32
|
+
ask_followup_question = "ask_followup_question"
|
|
33
|
+
attempt_completion = "attempt_completion"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ToolParamName(str, Enum):
|
|
37
|
+
command = "command"
|
|
38
|
+
path = "path"
|
|
39
|
+
content = "content"
|
|
40
|
+
regex = "regex"
|
|
41
|
+
file_pattern = "file_pattern"
|
|
42
|
+
recursive = "recursive"
|
|
43
|
+
action = "action"
|
|
44
|
+
url = "url"
|
|
45
|
+
coordinate = "coordinate"
|
|
46
|
+
text = "text"
|
|
47
|
+
question = "question"
|
|
48
|
+
result = "result"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class BaseTool(pydantic.BaseModel):
|
|
52
|
+
type: Literal["tool_use"]
|
|
53
|
+
name: ToolUseName
|
|
54
|
+
params: Dict[ToolParamName, str]
|
|
55
|
+
partial: bool
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class ExecuteCommandToolUse(BaseTool):
|
|
59
|
+
name: Literal[ToolUseName.execute_command]
|
|
60
|
+
params: Dict[Literal[ToolParamName.command], str]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class ReadFileToolUse(BaseTool):
|
|
64
|
+
name: Literal[ToolUseName.read_file]
|
|
65
|
+
params: Dict[Literal[ToolParamName.path], str]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class WriteToFileToolUse(BaseTool):
|
|
69
|
+
name: Literal[ToolUseName.write_to_file]
|
|
70
|
+
params: Dict[Union[Literal[ToolParamName.path],
|
|
71
|
+
Literal[ToolParamName.content]], str]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class SearchFilesToolUse(BaseTool):
|
|
75
|
+
name: Literal[ToolUseName.search_files]
|
|
76
|
+
params: Dict[Union[Literal[ToolParamName.path],
|
|
77
|
+
Literal[ToolParamName.regex], Literal[ToolParamName.file_pattern]], str]
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class ListFilesToolUse(BaseTool):
|
|
81
|
+
name: Literal[ToolUseName.list_files]
|
|
82
|
+
params: Dict[Union[Literal[ToolParamName.path],
|
|
83
|
+
Literal[ToolParamName.recursive]], str]
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class ListCodeDefinitionNamesToolUse(BaseTool):
|
|
87
|
+
name: Literal[ToolUseName.list_code_definition_names]
|
|
88
|
+
params: Dict[Literal[ToolParamName.path], str]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class BrowserActionToolUse(BaseTool):
|
|
92
|
+
name: Literal[ToolUseName.browser_action]
|
|
93
|
+
params: Dict[Union[Literal[ToolParamName.action], Literal[ToolParamName.url],
|
|
94
|
+
Literal[ToolParamName.coordinate], Literal[ToolParamName.text]], str]
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class AskFollowupQuestionToolUse(BaseTool):
|
|
98
|
+
name: Literal[ToolUseName.ask_followup_question]
|
|
99
|
+
params: Dict[Literal[ToolParamName.question], str]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class AttemptCompletionToolUse(BaseTool):
|
|
103
|
+
name: Literal[ToolUseName.attempt_completion]
|
|
104
|
+
params: Dict[Union[Literal[ToolParamName.result],
|
|
105
|
+
Literal[ToolParamName.command]], str]
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
ToolUse = Union[
|
|
109
|
+
ExecuteCommandToolUse,
|
|
110
|
+
ReadFileToolUse,
|
|
111
|
+
WriteToFileToolUse,
|
|
112
|
+
SearchFilesToolUse,
|
|
113
|
+
ListFilesToolUse,
|
|
114
|
+
ListCodeDefinitionNamesToolUse,
|
|
115
|
+
BrowserActionToolUse,
|
|
116
|
+
AskFollowupQuestionToolUse,
|
|
117
|
+
AttemptCompletionToolUse
|
|
118
|
+
]
|
|
119
|
+
|
|
120
|
+
AssistantMessageContent = Union[TextContent, ToolUse]
|
|
121
|
+
|
|
122
|
+
|
|
8
123
|
class Coder:
|
|
9
|
-
def __init__(self,llm:byzerllm.ByzerLLM) -> None:
|
|
10
|
-
self.llm = llm
|
|
124
|
+
def __init__(self, llm: byzerllm.ByzerLLM) -> None:
|
|
125
|
+
self.llm = llm
|
|
11
126
|
self.memory = []
|
|
127
|
+
self.env = detect_env()
|
|
128
|
+
self.current_streaming_content_index = 0
|
|
129
|
+
self.assistant_message_content = []
|
|
130
|
+
self.user_message_content = []
|
|
131
|
+
self.user_message_content_ready = False
|
|
132
|
+
self.did_reject_tool = False
|
|
133
|
+
self.did_already_use_tool = False
|
|
134
|
+
self.did_complete_reading_stream = False
|
|
135
|
+
self.present_assistant_message_locked = False
|
|
136
|
+
self.present_assistant_message_has_pending_updates = False
|
|
137
|
+
self.consecutive_mistake_count = 0
|
|
12
138
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
139
|
+
def format_response(self, content_type: str, text: str = None, images: List[str] = None):
|
|
140
|
+
"""Format responses similar to Cline.ts formatResponse"""
|
|
141
|
+
if content_type == "tool_denied":
|
|
142
|
+
return "The user denied this operation."
|
|
143
|
+
elif content_type == "tool_denied_with_feedback":
|
|
144
|
+
return f"The user denied this operation and provided the following feedback:\n<feedback>\n{text}\n</feedback>"
|
|
145
|
+
elif content_type == "tool_error":
|
|
146
|
+
return f"The tool execution failed with the following error:\n<error>\n{text}\n</error>"
|
|
147
|
+
elif content_type == "no_tools_used":
|
|
148
|
+
return "[ERROR] You did not use a tool in your previous response! Please retry with a tool use."
|
|
149
|
+
elif content_type == "too_many_mistakes":
|
|
150
|
+
return f"You seem to be having trouble proceeding. The user has provided the following feedback:\n<feedback>\n{text}\n</feedback>"
|
|
151
|
+
|
|
152
|
+
def format_tool_result(self, text: str, images: List[str] = None):
|
|
153
|
+
"""Format tool execution results"""
|
|
154
|
+
if images and len(images) > 0:
|
|
155
|
+
return {"text": text, "images": images}
|
|
156
|
+
return text
|
|
157
|
+
|
|
158
|
+
def present_assistant_message(self):
|
|
159
|
+
"""Present and handle assistant messages and tool executions"""
|
|
160
|
+
if self.present_assistant_message_locked:
|
|
161
|
+
self.present_assistant_message_has_pending_updates = True
|
|
162
|
+
return
|
|
163
|
+
|
|
164
|
+
self.present_assistant_message_locked = True
|
|
165
|
+
self.present_assistant_message_has_pending_updates = False
|
|
166
|
+
|
|
167
|
+
if self.current_streaming_content_index >= len(self.assistant_message_content):
|
|
168
|
+
if self.did_complete_reading_stream:
|
|
169
|
+
self.user_message_content_ready = True
|
|
170
|
+
self.present_assistant_message_locked = False
|
|
171
|
+
return
|
|
172
|
+
|
|
173
|
+
block = self.assistant_message_content[self.current_streaming_content_index]
|
|
174
|
+
|
|
175
|
+
if block["type"] == "text":
|
|
176
|
+
if not (self.did_reject_tool or self.did_already_use_tool):
|
|
177
|
+
content = block.get("content", "")
|
|
178
|
+
# Handle text content similar to Cline.ts
|
|
179
|
+
# Remove thinking tags and format content
|
|
180
|
+
if content:
|
|
181
|
+
content = re.sub(r'<thinking>\s?', '', content)
|
|
182
|
+
content = re.sub(r'\s?</thinking>', '', content)
|
|
183
|
+
|
|
184
|
+
elif block["type"] == "tool_use":
|
|
185
|
+
# Handle tool execution similar to Cline.ts
|
|
186
|
+
# Execute appropriate tool and handle response
|
|
187
|
+
if not self.did_reject_tool and not self.did_already_use_tool:
|
|
188
|
+
tool_name = block.get("name")
|
|
189
|
+
params = block.get("params", {})
|
|
190
|
+
|
|
191
|
+
# Execute tool and handle response
|
|
192
|
+
try:
|
|
193
|
+
result = self.execute_tool(tool_name, params)
|
|
194
|
+
self.user_message_content.append({
|
|
195
|
+
"type": "text",
|
|
196
|
+
"text": f"[{tool_name}] Result:\n{result}"
|
|
197
|
+
})
|
|
198
|
+
self.did_already_use_tool = True
|
|
199
|
+
except Exception as e:
|
|
200
|
+
self.user_message_content.append({
|
|
201
|
+
"type": "text",
|
|
202
|
+
"text": self.format_response("tool_error", str(e))
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
self.present_assistant_message_locked = False
|
|
206
|
+
|
|
207
|
+
if not block.get("partial", False) or self.did_reject_tool or self.did_already_use_tool:
|
|
208
|
+
if self.current_streaming_content_index == len(self.assistant_message_content) - 1:
|
|
209
|
+
self.user_message_content_ready = True
|
|
210
|
+
|
|
211
|
+
self.current_streaming_content_index += 1
|
|
212
|
+
|
|
213
|
+
if self.current_streaming_content_index < len(self.assistant_message_content):
|
|
214
|
+
self.present_assistant_message()
|
|
215
|
+
|
|
216
|
+
if self.present_assistant_message_has_pending_updates:
|
|
217
|
+
self.present_assistant_message()
|
|
218
|
+
|
|
219
|
+
def parse_assistant_message(self, msg: str) -> List[Dict]:
|
|
220
|
+
"""Parse assistant message into content blocks similar to Cline.ts parseAssistantMessage"""
|
|
221
|
+
content_blocks = []
|
|
222
|
+
current_text_content = None
|
|
223
|
+
current_text_content_start_index = 0
|
|
224
|
+
current_tool_use = None
|
|
225
|
+
current_tool_use_start_index = 0
|
|
226
|
+
current_param_name = None
|
|
227
|
+
current_param_value_start_index = 0
|
|
228
|
+
accumulator = ""
|
|
229
|
+
|
|
230
|
+
for i, char in enumerate(msg):
|
|
231
|
+
accumulator += char
|
|
232
|
+
|
|
233
|
+
# Handle param value if we're in a tool use and have a param name
|
|
234
|
+
if current_tool_use is not None and current_param_name is not None:
|
|
235
|
+
current_param_value = accumulator[current_param_value_start_index:]
|
|
236
|
+
param_closing_tag = f"</{current_param_name}>"
|
|
237
|
+
if current_param_value.endswith(param_closing_tag):
|
|
238
|
+
# End of param value
|
|
239
|
+
current_tool_use["params"][current_param_name] = current_param_value[:-len(
|
|
240
|
+
param_closing_tag)].strip()
|
|
241
|
+
current_param_name = None
|
|
242
|
+
continue
|
|
243
|
+
|
|
244
|
+
# Handle tool use
|
|
245
|
+
if current_tool_use is not None:
|
|
246
|
+
current_tool_value = accumulator[current_tool_use_start_index:]
|
|
247
|
+
tool_use_closing_tag = f"</{current_tool_use['name']}>"
|
|
248
|
+
if current_tool_value.endswith(tool_use_closing_tag):
|
|
249
|
+
# End of tool use
|
|
250
|
+
current_tool_use["partial"] = False
|
|
251
|
+
content_blocks.append(current_tool_use)
|
|
252
|
+
current_tool_use = None
|
|
253
|
+
continue
|
|
254
|
+
else:
|
|
255
|
+
# Check for param opening tags
|
|
256
|
+
for param_name in [p.value for p in ToolParamName]:
|
|
257
|
+
param_opening_tag = f"<{param_name}>"
|
|
258
|
+
if accumulator.endswith(param_opening_tag):
|
|
259
|
+
current_param_name = param_name
|
|
260
|
+
current_param_value_start_index = len(accumulator)
|
|
261
|
+
break
|
|
262
|
+
|
|
263
|
+
# Special case for write_to_file content param
|
|
264
|
+
if (current_tool_use["name"] == ToolUseName.write_to_file.value and
|
|
265
|
+
accumulator.endswith("</content>")):
|
|
266
|
+
tool_content = accumulator[current_tool_use_start_index:]
|
|
267
|
+
content_start_tag = "<content>"
|
|
268
|
+
content_end_tag = "</content>"
|
|
269
|
+
content_start_index = tool_content.find(
|
|
270
|
+
content_start_tag) + len(content_start_tag)
|
|
271
|
+
content_end_index = tool_content.rfind(content_end_tag)
|
|
272
|
+
if (content_start_index != -1 and content_end_index != -1
|
|
273
|
+
and content_end_index > content_start_index):
|
|
274
|
+
current_tool_use["params"]["content"] = tool_content[
|
|
275
|
+
content_start_index:content_end_index].strip()
|
|
276
|
+
continue
|
|
277
|
+
|
|
278
|
+
# Check for start of new tool use
|
|
279
|
+
did_start_tool_use = False
|
|
280
|
+
for tool_name in [t.value for t in ToolUseName]:
|
|
281
|
+
tool_use_opening_tag = f"<{tool_name}>"
|
|
282
|
+
if accumulator.endswith(tool_use_opening_tag):
|
|
283
|
+
current_tool_use = {
|
|
284
|
+
"type": "tool_use",
|
|
285
|
+
"name": tool_name,
|
|
286
|
+
"params": {},
|
|
287
|
+
"partial": True
|
|
288
|
+
}
|
|
289
|
+
current_tool_use_start_index = len(accumulator)
|
|
290
|
+
if current_text_content is not None:
|
|
291
|
+
current_text_content["partial"] = False
|
|
292
|
+
current_text_content["content"] = current_text_content["content"][
|
|
293
|
+
:-len(tool_use_opening_tag[:-1])].strip()
|
|
294
|
+
content_blocks.append(current_text_content)
|
|
295
|
+
current_text_content = None
|
|
296
|
+
did_start_tool_use = True
|
|
297
|
+
break
|
|
298
|
+
|
|
299
|
+
if not did_start_tool_use:
|
|
300
|
+
if current_text_content is None:
|
|
301
|
+
current_text_content_start_index = i
|
|
302
|
+
current_text_content = {
|
|
303
|
+
"type": "text",
|
|
304
|
+
"content": accumulator[current_text_content_start_index:].strip(),
|
|
305
|
+
"partial": True
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
# Handle any incomplete blocks
|
|
309
|
+
if current_tool_use is not None:
|
|
310
|
+
if current_param_name is not None:
|
|
311
|
+
current_tool_use["params"][current_param_name] = accumulator[
|
|
312
|
+
current_param_value_start_index:].strip()
|
|
313
|
+
content_blocks.append(current_tool_use)
|
|
314
|
+
elif current_text_content is not None:
|
|
315
|
+
content_blocks.append(current_text_content)
|
|
316
|
+
|
|
317
|
+
return content_blocks
|
|
318
|
+
|
|
319
|
+
def execute_tool(self, tool_name: str, params: Dict[str, str]) -> str:
|
|
320
|
+
"""Execute tools similar to Cline.ts tool execution"""
|
|
321
|
+
try:
|
|
322
|
+
if tool_name == ToolUseName.execute_command.value:
|
|
323
|
+
command = params.get("command")
|
|
324
|
+
if not command:
|
|
325
|
+
return self.format_response("tool_error", "Command parameter is required")
|
|
326
|
+
# Execute command implementation
|
|
327
|
+
return self.execute_command_tool(command)
|
|
328
|
+
|
|
329
|
+
elif tool_name == ToolUseName.read_file.value:
|
|
330
|
+
path = params.get("path")
|
|
331
|
+
if not path:
|
|
332
|
+
return self.format_response("tool_error", "Path parameter is required")
|
|
333
|
+
with open(os.path.join(self.env.cwd, path), 'r') as f:
|
|
334
|
+
return f.read()
|
|
335
|
+
|
|
336
|
+
elif tool_name == ToolUseName.write_to_file.value:
|
|
337
|
+
path = params.get("path")
|
|
338
|
+
content = params.get("content")
|
|
339
|
+
if not path or not content:
|
|
340
|
+
return self.format_response("tool_error",
|
|
341
|
+
"Both path and content parameters are required")
|
|
342
|
+
full_path = os.path.join(self.env.cwd, path)
|
|
343
|
+
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
|
344
|
+
with open(full_path, 'w') as f:
|
|
345
|
+
f.write(content)
|
|
346
|
+
return f"Content successfully written to {path}"
|
|
347
|
+
|
|
348
|
+
elif tool_name == ToolUseName.search_files.value:
|
|
349
|
+
path = params.get("path")
|
|
350
|
+
regex = params.get("regex")
|
|
351
|
+
if not path or not regex:
|
|
352
|
+
return self.format_response("tool_error",
|
|
353
|
+
"Both path and regex parameters are required")
|
|
354
|
+
# Implement search functionality
|
|
355
|
+
return self.search_files_tool(path, regex)
|
|
356
|
+
|
|
357
|
+
elif tool_name == ToolUseName.list_files.value:
|
|
358
|
+
path = params.get("path")
|
|
359
|
+
recursive = params.get("recursive", "false").lower() == "true"
|
|
360
|
+
if not path:
|
|
361
|
+
return self.format_response("tool_error", "Path parameter is required")
|
|
362
|
+
# Implement list files functionality
|
|
363
|
+
return self.list_files_tool(path, recursive)
|
|
364
|
+
|
|
365
|
+
elif tool_name == ToolUseName.attempt_completion.value:
|
|
366
|
+
result = params.get("result")
|
|
367
|
+
command = params.get("command")
|
|
368
|
+
if not result:
|
|
369
|
+
return self.format_response("tool_error", "Result parameter is required")
|
|
370
|
+
completion_response = self.attempt_completion_tool(
|
|
371
|
+
result, command)
|
|
372
|
+
return completion_response
|
|
373
|
+
|
|
374
|
+
else:
|
|
375
|
+
return self.format_response("tool_error", f"Unknown tool: {tool_name}")
|
|
376
|
+
|
|
377
|
+
except Exception as e:
|
|
378
|
+
return self.format_response("tool_error", str(e))
|
|
379
|
+
|
|
380
|
+
def execute_command_tool(self, command: str) -> str:
|
|
381
|
+
"""Execute command tool implementation with interactive support"""
|
|
382
|
+
try:
|
|
383
|
+
# Create rich console for pretty output
|
|
384
|
+
console = Console()
|
|
385
|
+
# Create prompt session for input
|
|
386
|
+
session = PromptSession()
|
|
387
|
+
|
|
388
|
+
# Create and configure the process
|
|
389
|
+
process = subprocess.Popen(
|
|
390
|
+
command,
|
|
391
|
+
shell=True,
|
|
392
|
+
cwd=self.env.cwd,
|
|
393
|
+
stdout=subprocess.PIPE,
|
|
394
|
+
stderr=subprocess.PIPE,
|
|
395
|
+
stdin=subprocess.PIPE,
|
|
396
|
+
bufsize=1,
|
|
397
|
+
universal_newlines=True
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
console.print(Panel(f"[bold blue]Executing command:[/] {command}"))
|
|
401
|
+
|
|
402
|
+
# Initialize output buffer
|
|
403
|
+
output = []
|
|
404
|
+
|
|
405
|
+
while True:
|
|
406
|
+
# Check for process completion
|
|
407
|
+
if process.poll() is not None:
|
|
408
|
+
break
|
|
409
|
+
|
|
410
|
+
# Read stdout
|
|
411
|
+
stdout_line = process.stdout.readline()
|
|
412
|
+
if stdout_line:
|
|
413
|
+
console.print(stdout_line.strip())
|
|
414
|
+
output.append(stdout_line)
|
|
415
|
+
|
|
416
|
+
# Read stderr
|
|
417
|
+
stderr_line = process.stderr.readline()
|
|
418
|
+
if stderr_line:
|
|
419
|
+
console.print(f"[red]{stderr_line.strip()}[/]")
|
|
420
|
+
output.append(stderr_line)
|
|
421
|
+
|
|
422
|
+
# Check if process is waiting for input
|
|
423
|
+
if not stdout_line and not stderr_line and process.poll() is None:
|
|
424
|
+
try:
|
|
425
|
+
# Get user input
|
|
426
|
+
user_input = session.prompt("» ")
|
|
427
|
+
# Send input to process
|
|
428
|
+
process.stdin.write(f"{user_input}\n")
|
|
429
|
+
process.stdin.flush()
|
|
430
|
+
output.append(f"Input: {user_input}\n")
|
|
431
|
+
except (EOFError, KeyboardInterrupt):
|
|
432
|
+
process.terminate()
|
|
433
|
+
break
|
|
434
|
+
|
|
435
|
+
# Get any remaining output
|
|
436
|
+
remaining_stdout, remaining_stderr = process.communicate()
|
|
437
|
+
if remaining_stdout:
|
|
438
|
+
console.print(remaining_stdout)
|
|
439
|
+
output.append(remaining_stdout)
|
|
440
|
+
if remaining_stderr:
|
|
441
|
+
console.print(f"[red]{remaining_stderr}[/]")
|
|
442
|
+
output.append(remaining_stderr)
|
|
443
|
+
|
|
444
|
+
if process.returncode == 0:
|
|
445
|
+
return "".join(output)
|
|
446
|
+
else:
|
|
447
|
+
return self.format_response("tool_error", "".join(output))
|
|
448
|
+
|
|
449
|
+
except Exception as e:
|
|
450
|
+
return self.format_response("tool_error", str(e))
|
|
451
|
+
|
|
452
|
+
def search_files_tool(self, path: str, regex: str) -> str:
|
|
453
|
+
"""Search files tool implementation"""
|
|
454
|
+
results = []
|
|
455
|
+
full_path = os.path.join(self.env.cwd, path)
|
|
456
|
+
pattern = re.compile(regex)
|
|
457
|
+
|
|
458
|
+
for root, _, files in os.walk(full_path):
|
|
459
|
+
for file in files:
|
|
460
|
+
file_path = os.path.join(root, file)
|
|
461
|
+
try:
|
|
462
|
+
with open(file_path, 'r') as f:
|
|
463
|
+
for i, line in enumerate(f, 1):
|
|
464
|
+
if pattern.search(line):
|
|
465
|
+
rel_path = os.path.relpath(
|
|
466
|
+
file_path, self.env.cwd)
|
|
467
|
+
results.append(
|
|
468
|
+
f"{rel_path}:{i}: {line.strip()}")
|
|
469
|
+
except Exception:
|
|
470
|
+
continue
|
|
471
|
+
|
|
472
|
+
return "\n".join(results) if results else "No matches found"
|
|
473
|
+
|
|
474
|
+
def list_files_tool(self, path: str, recursive: bool) -> str:
|
|
475
|
+
"""List files tool implementation"""
|
|
476
|
+
full_path = os.path.join(self.env.cwd, path)
|
|
477
|
+
results = []
|
|
478
|
+
|
|
479
|
+
if recursive:
|
|
480
|
+
for root, _, files in os.walk(full_path):
|
|
481
|
+
for file in files:
|
|
482
|
+
file_path = os.path.join(root, file)
|
|
483
|
+
rel_path = os.path.relpath(file_path, self.env.cwd)
|
|
484
|
+
results.append(rel_path)
|
|
485
|
+
else:
|
|
486
|
+
try:
|
|
487
|
+
results = [f for f in os.listdir(full_path)
|
|
488
|
+
if os.path.isfile(os.path.join(full_path, f))]
|
|
489
|
+
except Exception as e:
|
|
490
|
+
return self.format_response("tool_error", str(e))
|
|
491
|
+
|
|
492
|
+
return "\n".join(results) if results else "No files found"
|
|
493
|
+
|
|
494
|
+
def attempt_completion_tool(self, result: str, command: str = None) -> str:
|
|
495
|
+
"""Handle task completion attempts"""
|
|
496
|
+
completion_msg = f"Task completed:\n{result}"
|
|
497
|
+
if command:
|
|
498
|
+
try:
|
|
499
|
+
cmd_result = self.execute_command_tool(command)
|
|
500
|
+
completion_msg += f"\nCommand execution result:\n{cmd_result}"
|
|
501
|
+
except Exception as e:
|
|
502
|
+
completion_msg += f"\nCommand execution failed: {str(e)}"
|
|
503
|
+
return completion_msg
|
|
504
|
+
|
|
505
|
+
async def recursively_make_cline_requests(self, user_content: List[Dict], include_file_details: bool = False) -> bool:
|
|
506
|
+
"""Handle recursive requests similar to Cline.ts recursivelyMakeClineRequests"""
|
|
507
|
+
if self.consecutive_mistake_count >= 3:
|
|
508
|
+
feedback = "You seem to be having trouble. Consider breaking down the task into smaller steps."
|
|
509
|
+
user_content.append({
|
|
510
|
+
"type": "text",
|
|
511
|
+
"text": self.format_response("too_many_mistakes", feedback)
|
|
512
|
+
})
|
|
513
|
+
self.consecutive_mistake_count = 0
|
|
514
|
+
|
|
515
|
+
# Reset streaming state
|
|
516
|
+
self.current_streaming_content_index = 0
|
|
517
|
+
self.assistant_message_content = []
|
|
518
|
+
self.did_complete_reading_stream = False
|
|
519
|
+
self.user_message_content = []
|
|
520
|
+
self.user_message_content_ready = False
|
|
521
|
+
self.did_reject_tool = False
|
|
522
|
+
self.did_already_use_tool = False
|
|
523
|
+
self.present_assistant_message_locked = False
|
|
524
|
+
self.present_assistant_message_has_pending_updates = False
|
|
525
|
+
|
|
526
|
+
try:
|
|
527
|
+
# Load environment details
|
|
528
|
+
loaded_content, env_details = await self.load_context(user_content, include_file_details)
|
|
529
|
+
user_content = loaded_content
|
|
530
|
+
user_content.append({"type": "text", "text": env_details})
|
|
531
|
+
|
|
532
|
+
# Stream from LLM
|
|
533
|
+
assistant_message = ""
|
|
534
|
+
async for chunk in self.stream_llm_response(user_content):
|
|
535
|
+
if isinstance(chunk, dict) and chunk.get("type") == "text":
|
|
536
|
+
assistant_message += chunk.get("text", "")
|
|
537
|
+
# Parse raw assistant message into content blocks
|
|
538
|
+
prev_length = len(self.assistant_message_content)
|
|
539
|
+
self.assistant_message_content = self.parse_assistant_message(
|
|
540
|
+
assistant_message)
|
|
541
|
+
if len(self.assistant_message_content) > prev_length:
|
|
542
|
+
self.user_message_content_ready = False
|
|
543
|
+
# Present content to user
|
|
544
|
+
self.present_assistant_message_in_terminal()
|
|
545
|
+
|
|
546
|
+
if self.did_reject_tool:
|
|
547
|
+
assistant_message += "\n\n[Response interrupted by user feedback]"
|
|
548
|
+
break
|
|
549
|
+
|
|
550
|
+
if self.did_already_use_tool:
|
|
551
|
+
assistant_message += "\n\n[Response interrupted by tool use result]"
|
|
552
|
+
break
|
|
553
|
+
|
|
554
|
+
self.did_complete_reading_stream = True
|
|
555
|
+
|
|
556
|
+
# Set remaining blocks to complete
|
|
557
|
+
for block in self.assistant_message_content:
|
|
558
|
+
if block.get("partial"):
|
|
559
|
+
block["partial"] = False
|
|
560
|
+
self.present_assistant_message_in_terminal()
|
|
561
|
+
|
|
562
|
+
# Check for tool usage
|
|
563
|
+
did_end_loop = False
|
|
564
|
+
if assistant_message:
|
|
565
|
+
await self.save_conversation_history("assistant", assistant_message)
|
|
566
|
+
await self._wait_for_user_message_ready()
|
|
567
|
+
|
|
568
|
+
did_tool_use = any(block["type"] == "tool_use"
|
|
569
|
+
for block in self.assistant_message_content)
|
|
570
|
+
if not did_tool_use:
|
|
571
|
+
self.user_message_content.append({
|
|
572
|
+
"type": "text",
|
|
573
|
+
"text": self.format_response("no_tools_used")
|
|
574
|
+
})
|
|
575
|
+
self.consecutive_mistake_count += 1
|
|
576
|
+
|
|
577
|
+
rec_did_end_loop = await self.recursively_make_cline_requests(self.user_message_content)
|
|
578
|
+
did_end_loop = rec_did_end_loop
|
|
579
|
+
else:
|
|
580
|
+
# Error if no assistant message
|
|
581
|
+
await self.save_conversation_history(
|
|
582
|
+
"assistant",
|
|
583
|
+
"Failure: No response was provided."
|
|
584
|
+
)
|
|
585
|
+
|
|
586
|
+
return did_end_loop
|
|
587
|
+
|
|
588
|
+
except Exception as e:
|
|
589
|
+
return True
|
|
590
|
+
|
|
591
|
+
async def stream_llm_response(self, user_content: List[Dict]):
|
|
592
|
+
"""Stream LLM responses with error handling"""
|
|
593
|
+
try:
|
|
594
|
+
# first_chunk = True
|
|
595
|
+
content = "\n".join([item["text"] for item in user_content])
|
|
596
|
+
async for (chunk, metadata) in self.llm.async_stream_chat_oai(
|
|
597
|
+
conversations=self.conversation_history +
|
|
598
|
+
[{"role": "user", "content": content}],
|
|
599
|
+
delta_mode=True
|
|
600
|
+
):
|
|
601
|
+
# if first_chunk:
|
|
602
|
+
# first_chunk = False
|
|
603
|
+
# Handle potential first chunk errors
|
|
604
|
+
# if chunk.get("error"):
|
|
605
|
+
# yield {"type": "error", "text": str(chunk["error"])}
|
|
606
|
+
# return
|
|
607
|
+
yield {"type": "text", "text": chunk}
|
|
608
|
+
except Exception as e:
|
|
609
|
+
yield {"type": "error", "text": str(e)}
|
|
610
|
+
|
|
611
|
+
async def load_context(self, user_content: List[Dict], include_file_details: bool) -> Tuple[List[Dict], str]:
|
|
612
|
+
"""Load context and environment details similar to Cline.ts loadContext"""
|
|
613
|
+
# Process user content
|
|
614
|
+
processed_content = []
|
|
615
|
+
for block in user_content:
|
|
616
|
+
if block["type"] == "text":
|
|
617
|
+
block["text"] = await self._process_mentions(block["text"])
|
|
618
|
+
processed_content.append(block)
|
|
619
|
+
|
|
620
|
+
# Get environment details
|
|
621
|
+
env_details = await self._get_environment_details(include_file_details)
|
|
622
|
+
|
|
623
|
+
return processed_content, env_details
|
|
624
|
+
|
|
625
|
+
async def _get_environment_details(self, include_file_details: bool) -> str:
|
|
626
|
+
"""Build environment details string"""
|
|
627
|
+
details = []
|
|
628
|
+
if include_file_details:
|
|
629
|
+
details.append(
|
|
630
|
+
f"\n# Current Working Directory ({self.env.cwd}) Files")
|
|
631
|
+
try:
|
|
632
|
+
files = self.list_files_tool(self.env.cwd, recursive=True)
|
|
633
|
+
details.append(files)
|
|
634
|
+
except Exception:
|
|
635
|
+
details.append("(Error listing files)")
|
|
636
|
+
|
|
637
|
+
return "\n".join(details)
|
|
638
|
+
|
|
639
|
+
async def _wait_for_user_message_ready(self):
|
|
640
|
+
"""Wait for user message content to be ready"""
|
|
641
|
+
while not self.user_message_content_ready:
|
|
642
|
+
await asyncio.sleep(0.1)
|
|
643
|
+
|
|
644
|
+
async def save_conversation_history(self, role: str, content: str):
|
|
645
|
+
"""Save message to conversation history"""
|
|
646
|
+
self.conversation_history.append({
|
|
647
|
+
"role": role,
|
|
648
|
+
"content": content
|
|
649
|
+
})
|
|
650
|
+
|
|
651
|
+
async def _process_mentions(self, text: str) -> str:
|
|
652
|
+
"""Process any mentions or special tags in text"""
|
|
653
|
+
# Implement mention processing if needed
|
|
654
|
+
return text
|
|
655
|
+
|
|
656
|
+
async def _get_system_prompt(self) -> str:
|
|
657
|
+
return self._run.prompt(custom_instructions="", support_computer_use=False)
|
|
18
658
|
|
|
19
|
-
|
|
20
|
-
|
|
659
|
+
|
|
660
|
+
async def start_task(self, task: str, images: List[str] = None):
|
|
661
|
+
"""Start a new task with initial message"""
|
|
662
|
+
self.conversation_history = []
|
|
663
|
+
await self.save_conversation_history("system", await self._get_system_prompt())
|
|
664
|
+
|
|
665
|
+
# Initial message
|
|
666
|
+
user_content = [{
|
|
667
|
+
"type": "text",
|
|
668
|
+
"text": f"<task>\n{task}\n</task>"
|
|
669
|
+
}]
|
|
670
|
+
|
|
671
|
+
if images:
|
|
672
|
+
for image in images:
|
|
673
|
+
user_content.append({
|
|
674
|
+
"type": "image",
|
|
675
|
+
"url": image
|
|
676
|
+
})
|
|
677
|
+
|
|
678
|
+
await self.initiate_task_loop(user_content)
|
|
679
|
+
|
|
680
|
+
async def initiate_task_loop(self, user_content: List[Dict]):
|
|
681
|
+
"""Control the main task execution loop"""
|
|
682
|
+
next_user_content = user_content
|
|
683
|
+
include_file_details = True
|
|
684
|
+
|
|
685
|
+
while True:
|
|
686
|
+
did_end_loop = await self.recursively_make_cline_requests(
|
|
687
|
+
next_user_content,
|
|
688
|
+
include_file_details
|
|
689
|
+
)
|
|
690
|
+
include_file_details = False
|
|
691
|
+
|
|
692
|
+
if did_end_loop:
|
|
693
|
+
break
|
|
694
|
+
|
|
695
|
+
next_user_content = [{
|
|
696
|
+
"type": "text",
|
|
697
|
+
"text": self.format_response("no_tools_used")
|
|
698
|
+
}]
|
|
699
|
+
self.consecutive_mistake_count += 1
|
|
700
|
+
|
|
701
|
+
def present_assistant_message_in_terminal(self):
|
|
702
|
+
"""Present and handle assistant messages with rich terminal UI"""
|
|
703
|
+
if self.present_assistant_message_locked:
|
|
704
|
+
self.present_assistant_message_has_pending_updates = True
|
|
705
|
+
return
|
|
706
|
+
|
|
707
|
+
self.present_assistant_message_locked = True
|
|
708
|
+
self.present_assistant_message_has_pending_updates = False
|
|
709
|
+
|
|
710
|
+
console = Console()
|
|
711
|
+
session = PromptSession()
|
|
712
|
+
|
|
713
|
+
if self.current_streaming_content_index >= len(self.assistant_message_content):
|
|
714
|
+
if self.did_complete_reading_stream:
|
|
715
|
+
self.user_message_content_ready = True
|
|
716
|
+
self.present_assistant_message_locked = False
|
|
717
|
+
return
|
|
718
|
+
|
|
719
|
+
block = self.assistant_message_content[self.current_streaming_content_index]
|
|
720
|
+
|
|
721
|
+
if block["type"] == "text":
|
|
722
|
+
if not (self.did_reject_tool or self.did_already_use_tool):
|
|
723
|
+
content = block.get("content", "")
|
|
724
|
+
if content:
|
|
725
|
+
# Handle thinking tags with special formatting
|
|
726
|
+
thinking_pattern = r'<thinking>(.*?)</thinking>'
|
|
727
|
+
matches = re.findall(thinking_pattern, content, re.DOTALL)
|
|
728
|
+
if matches:
|
|
729
|
+
for thinking in matches:
|
|
730
|
+
console.print(Panel(thinking.strip(), title="[bold blue]Thinking", border_style="blue"))
|
|
731
|
+
content = re.sub(thinking_pattern, '', content)
|
|
732
|
+
|
|
733
|
+
if content.strip():
|
|
734
|
+
console.print(content.strip(),end="")
|
|
735
|
+
|
|
736
|
+
elif block["type"] == "tool_use":
|
|
737
|
+
if not self.did_reject_tool and not self.did_already_use_tool:
|
|
738
|
+
tool_name = block.get("name")
|
|
739
|
+
params = block.get("params", {})
|
|
740
|
+
|
|
741
|
+
# Show tool usage confirmation dialog
|
|
742
|
+
console.print(Panel(
|
|
743
|
+
f"[bold]Tool: {tool_name}[/bold]\nParameters:\n" +
|
|
744
|
+
"\n".join([f"- {k}: {v}" for k, v in params.items()]),
|
|
745
|
+
title="[bold yellow]Tool Usage Confirmation",
|
|
746
|
+
border_style="yellow"
|
|
747
|
+
))
|
|
748
|
+
|
|
749
|
+
try:
|
|
750
|
+
confirm = session.prompt(
|
|
751
|
+
"Proceed with tool execution? (y/n) > ",
|
|
752
|
+
style=Style.from_dict({
|
|
753
|
+
'prompt': 'bold yellow',
|
|
754
|
+
})
|
|
755
|
+
).lower().strip()
|
|
756
|
+
|
|
757
|
+
if confirm == 'y':
|
|
758
|
+
console.print("[bold green]Executing tool...[/bold green]")
|
|
759
|
+
result = self.execute_tool(tool_name, params)
|
|
760
|
+
self.user_message_content.append({
|
|
761
|
+
"type": "text",
|
|
762
|
+
"text": f"[{tool_name}] Result:\n{result}"
|
|
763
|
+
})
|
|
764
|
+
self.did_already_use_tool = True
|
|
765
|
+
console.print(Panel(str(result), title="[bold green]Tool Result", border_style="green"))
|
|
766
|
+
else:
|
|
767
|
+
self.did_reject_tool = True
|
|
768
|
+
self.user_message_content.append({
|
|
769
|
+
"type": "text",
|
|
770
|
+
"text": "The user denied this operation."
|
|
771
|
+
})
|
|
772
|
+
console.print("[bold red]Tool execution cancelled[/bold red]")
|
|
773
|
+
|
|
774
|
+
except Exception as e:
|
|
775
|
+
error_msg = str(e)
|
|
776
|
+
self.user_message_content.append({
|
|
777
|
+
"type": "text",
|
|
778
|
+
"text": self.format_response("tool_error", error_msg)
|
|
779
|
+
})
|
|
780
|
+
console.print(Panel(error_msg, title="[bold red]Error", border_style="red"))
|
|
781
|
+
|
|
782
|
+
self.present_assistant_message_locked = False
|
|
783
|
+
|
|
784
|
+
if not block.get("partial", False) or self.did_reject_tool or self.did_already_use_tool:
|
|
785
|
+
if self.current_streaming_content_index == len(self.assistant_message_content) - 1:
|
|
786
|
+
self.user_message_content_ready = True
|
|
787
|
+
|
|
788
|
+
self.current_streaming_content_index += 1
|
|
789
|
+
|
|
790
|
+
if self.current_streaming_content_index < len(self.assistant_message_content):
|
|
791
|
+
self.present_assistant_message_in_terminal()
|
|
792
|
+
|
|
793
|
+
if self.present_assistant_message_has_pending_updates:
|
|
794
|
+
self.present_assistant_message_in_terminal()
|
|
795
|
+
|
|
796
|
+
def abort_task(self):
|
|
797
|
+
"""Handle task abortion"""
|
|
798
|
+
self.did_abort = True
|
|
799
|
+
# Clean up any resources
|
|
800
|
+
if hasattr(self, 'diff_viewer'):
|
|
801
|
+
self.diff_viewer.close()
|
|
802
|
+
# Close any open file handles
|
|
803
|
+
# Terminate any running processes
|
|
804
|
+
|
|
805
|
+
async def resume_task(self):
|
|
806
|
+
"""Resume a previously interrupted task"""
|
|
807
|
+
# Load previous conversation history
|
|
808
|
+
if not self.conversation_history:
|
|
809
|
+
raise Exception("No conversation history to resume")
|
|
810
|
+
|
|
811
|
+
# Get last message before interruption
|
|
812
|
+
last_message = self.conversation_history[-1]
|
|
813
|
+
|
|
814
|
+
# Prepare resume message
|
|
815
|
+
resume_content = [{
|
|
816
|
+
"type": "text",
|
|
817
|
+
"text": "[TASK RESUMPTION] This task was interrupted. Please reassess the current state and continue."
|
|
818
|
+
}]
|
|
819
|
+
|
|
820
|
+
await self.initiate_task_loop(resume_content)
|
|
821
|
+
|
|
822
|
+
def create_diff(self, filename: str = "file", old_str: str = "", new_str: str = "") -> str:
|
|
823
|
+
"""Create a diff between two strings"""
|
|
824
|
+
import difflib
|
|
825
|
+
differ = difflib.Differ()
|
|
826
|
+
diff = list(differ.compare(old_str.splitlines(), new_str.splitlines()))
|
|
827
|
+
|
|
828
|
+
# Format diff output
|
|
829
|
+
formatted_diff = []
|
|
830
|
+
for line in diff:
|
|
831
|
+
if line.startswith('+'):
|
|
832
|
+
formatted_diff.append(f"+ {line[2:]}")
|
|
833
|
+
elif line.startswith('-'):
|
|
834
|
+
formatted_diff.append(f"- {line[2:]}")
|
|
835
|
+
elif line.startswith('?'):
|
|
836
|
+
continue
|
|
837
|
+
else:
|
|
838
|
+
formatted_diff.append(f" {line[2:]}")
|
|
839
|
+
|
|
840
|
+
return f"Diff for {filename}:\n" + "\n".join(formatted_diff)
|
|
841
|
+
|
|
842
|
+
@byzerllm.prompt()
|
|
843
|
+
def _run(self, custom_instructions: str, support_computer_use: bool = True) -> str:
|
|
844
|
+
'''
|
|
845
|
+
You are auto-coder, a highly skilled software engineer with extensive knowledge in many programming languages, frameworks, design patterns, and best practices.
|
|
846
|
+
|
|
847
|
+
====
|
|
848
|
+
|
|
849
|
+
TOOL USE
|
|
850
|
+
|
|
851
|
+
You have access to a set of tools that are executed upon the user's approval. You can use one tool per message, and will receive the result of that tool use in the user's response. You use tools step-by-step to accomplish a given task, with each tool use informed by the result of the previous tool use.
|
|
852
|
+
|
|
853
|
+
# Tool Use Formatting
|
|
854
|
+
|
|
855
|
+
Tool use is formatted using XML-style tags. The tool name is enclosed in opening and closing tags, and each parameter is similarly enclosed within its own set of tags. Here's the structure:
|
|
856
|
+
|
|
857
|
+
<tool_name>
|
|
858
|
+
<parameter1_name>value1</parameter1_name>
|
|
859
|
+
<parameter2_name>value2</parameter2_name>
|
|
860
|
+
...
|
|
861
|
+
</tool_name>
|
|
862
|
+
|
|
863
|
+
For example:
|
|
864
|
+
|
|
865
|
+
<read_file>
|
|
866
|
+
<path>src/main.js</path>
|
|
867
|
+
</read_file>
|
|
868
|
+
|
|
869
|
+
Always adhere to this format for the tool use to ensure proper parsing and execution.
|
|
870
|
+
|
|
871
|
+
# Tools
|
|
872
|
+
|
|
873
|
+
## execute_command
|
|
874
|
+
Description: Request to execute a CLI command on the system. Use this when you need to perform system operations or run specific commands to accomplish any step in the user's task. You must tailor your command to the user's system and provide a clear explanation of what the command does. Prefer to execute complex CLI commands over creating executable scripts, as they are more flexible and easier to run. Commands will be executed in the current working directory: {{ cwd }}
|
|
875
|
+
Parameters:
|
|
876
|
+
- command: (required) The CLI command to execute. This should be valid for the current operating system. Ensure the command is properly formatted and does not contain any harmful instructions.
|
|
877
|
+
Usage:
|
|
878
|
+
<execute_command>
|
|
879
|
+
<command>Your command here</command>
|
|
880
|
+
</execute_command>
|
|
881
|
+
|
|
882
|
+
## read_file
|
|
883
|
+
Description: Request to read the contents of a file at the specified path. Use this when you need to examine the contents of an existing file you do not know the contents of, for example to analyze code, review text files, or extract information from configuration files. Automatically extracts raw text from PDF and DOCX files. May not be suitable for other types of binary files, as it returns the raw content as a string.
|
|
884
|
+
Parameters:
|
|
885
|
+
- path: (required) The path of the file to read (relative to the current working directory {{ cwd }})
|
|
886
|
+
Usage:
|
|
887
|
+
<read_file>
|
|
888
|
+
<path>File path here</path>
|
|
889
|
+
</read_file>
|
|
890
|
+
|
|
891
|
+
## write_to_file
|
|
892
|
+
Description: Request to write content to a file at the specified path. If the file exists, it will be overwritten with the provided content. If the file doesn't exist, it will be created. This tool will automatically create any directories needed to write the file.
|
|
893
|
+
Parameters:
|
|
894
|
+
- path: (required) The path of the file to write to (relative to the current working directory {{ cwd }})
|
|
895
|
+
- content: (required) The content to write to the file. ALWAYS provide the COMPLETE intended content of the file, without any truncation or omissions. You MUST include ALL parts of the file, even if they haven't been modified.
|
|
896
|
+
Usage:
|
|
897
|
+
<write_to_file>
|
|
898
|
+
<path>File path here</path>
|
|
899
|
+
<content>
|
|
900
|
+
Your file content here
|
|
901
|
+
</content>
|
|
902
|
+
</write_to_file>
|
|
903
|
+
|
|
904
|
+
## search_files
|
|
905
|
+
Description: Request to perform a regex search across files in a specified directory, providing context-rich results. This tool searches for patterns or specific content across multiple files, displaying each match with encapsulating context.
|
|
906
|
+
Parameters:
|
|
907
|
+
- path: (required) The path of the directory to search in (relative to the current working directory {{ cwd }}). This directory will be recursively searched.
|
|
908
|
+
- regex: (required) The regular expression pattern to search for. Uses Rust regex syntax.
|
|
909
|
+
- file_pattern: (optional) Glob pattern to filter files (e.g., '*.ts' for TypeScript files). If not provided, it will search all files (*).
|
|
910
|
+
Usage:
|
|
911
|
+
<search_files>
|
|
912
|
+
<path>Directory path here</path>
|
|
913
|
+
<regex>Your regex pattern here</regex>
|
|
914
|
+
<file_pattern>file pattern here (optional)</file_pattern>
|
|
915
|
+
</search_files>
|
|
916
|
+
|
|
917
|
+
## list_files
|
|
918
|
+
Description: Request to list files and directories within the specified directory. If recursive is true, it will list all files and directories recursively. If recursive is false or not provided, it will only list the top-level contents. Do not use this tool to confirm the existence of files you may have created, as the user will let you know if the files were created successfully or not.
|
|
919
|
+
Parameters:
|
|
920
|
+
- path: (required) The path of the directory to list contents for (relative to the current working directory {{ cwd }})
|
|
921
|
+
- recursive: (optional) Whether to list files recursively. Use true for recursive listing, false or omit for top-level only.
|
|
922
|
+
Usage:
|
|
923
|
+
<list_files>
|
|
924
|
+
<path>Directory path here</path>
|
|
925
|
+
<recursive>true or false (optional)</recursive>
|
|
926
|
+
</list_files>
|
|
927
|
+
|
|
928
|
+
## list_code_definition_names
|
|
929
|
+
Description: Request to list definition names (classes, functions, methods, etc.) used in source code files at the top level of the specified directory. This tool provides insights into the codebase structure and important constructs, encapsulating high-level concepts and relationships that are crucial for understanding the overall architecture.
|
|
930
|
+
Parameters:
|
|
931
|
+
- path: (required) The path of the directory (relative to the current working directory {{ cwd }}) to list top level source code definitions for.
|
|
932
|
+
Usage:
|
|
933
|
+
<list_code_definition_names>
|
|
934
|
+
<path>Directory path here</path>
|
|
935
|
+
</list_code_definition_names>
|
|
936
|
+
|
|
937
|
+
{%- if support_computer_use -%}
|
|
938
|
+
## browser_action
|
|
939
|
+
Description: Request to interact with a Puppeteer-controlled browser. Every action, except \`close\`, will be responded to with a screenshot of the browser's current state, along with any new console logs. You may only perform one browser action per message, and wait for the user's response including a screenshot and logs to determine the next action.
|
|
940
|
+
- The sequence of actions **must always start with** launching the browser at a URL, and **must always end with** closing the browser. If you need to visit a new URL that is not possible to navigate to from the current webpage, you must first close the browser, then launch again at the new URL.
|
|
941
|
+
- While the browser is active, only the \`browser_action\` tool can be used. No other tools should be called during this time. You may proceed to use other tools only after closing the browser. For example if you run into an error and need to fix a file, you must close the browser, then use other tools to make the necessary changes, then re-launch the browser to verify the result.
|
|
942
|
+
- The browser window has a resolution of **900x600** pixels. When performing any click actions, ensure the coordinates are within this resolution range.
|
|
943
|
+
- Before clicking on any elements such as icons, links, or buttons, you must consult the provided screenshot of the page to determine the coordinates of the element. The click should be targeted at the **center of the element**, not on its edges.
|
|
944
|
+
Parameters:
|
|
945
|
+
- action: (required) The action to perform. The available actions are:
|
|
946
|
+
* launch: Launch a new Puppeteer-controlled browser instance at the specified URL. This **must always be the first action**.
|
|
947
|
+
- Use with the \`url\` parameter to provide the URL.
|
|
948
|
+
- Ensure the URL is valid and includes the appropriate protocol (e.g. http://localhost:3000/page, file:///path/to/file.html, etc.)
|
|
949
|
+
* click: Click at a specific x,y coordinate.
|
|
950
|
+
- Use with the \`coordinate\` parameter to specify the location.
|
|
951
|
+
- Always click in the center of an element (icon, button, link, etc.) based on coordinates derived from a screenshot.
|
|
952
|
+
* type: Type a string of text on the keyboard. You might use this after clicking on a text field to input text.
|
|
953
|
+
- Use with the \`text\` parameter to provide the string to type.
|
|
954
|
+
* scroll_down: Scroll down the page by one page height.
|
|
955
|
+
* scroll_up: Scroll up the page by one page height.
|
|
956
|
+
* close: Close the Puppeteer-controlled browser instance. This **must always be the final browser action**.
|
|
957
|
+
- Example: \`<action>close</action>\`
|
|
958
|
+
- url: (optional) Use this for providing the URL for the \`launch\` action.
|
|
959
|
+
* Example: <url>https://example.com</url>
|
|
960
|
+
- coordinate: (optional) The X and Y coordinates for the \`click\` action. Coordinates should be within the **900x600** resolution.
|
|
961
|
+
* Example: <coordinate>450,300</coordinate>
|
|
962
|
+
- text: (optional) Use this for providing the text for the \`type\` action.
|
|
963
|
+
* Example: <text>Hello, world!</text>
|
|
964
|
+
Usage:
|
|
965
|
+
<browser_action>
|
|
966
|
+
<action>Action to perform (e.g., launch, click, type, scroll_down, scroll_up, close)</action>
|
|
967
|
+
<url>URL to launch the browser at (optional)</url>
|
|
968
|
+
<coordinate>x,y coordinates (optional)</coordinate>
|
|
969
|
+
<text>Text to type (optional)</text>
|
|
970
|
+
</browser_action>
|
|
971
|
+
{%- endif -%}
|
|
972
|
+
|
|
973
|
+
## ask_followup_question
|
|
974
|
+
Description: Ask the user a question to gather additional information needed to complete the task. This tool should be used when you encounter ambiguities, need clarification, or require more details to proceed effectively. It allows for interactive problem-solving by enabling direct communication with the user. Use this tool judiciously to maintain a balance between gathering necessary information and avoiding excessive back-and-forth.
|
|
975
|
+
Parameters:
|
|
976
|
+
- question: (required) The question to ask the user. This should be a clear, specific question that addresses the information you need.
|
|
977
|
+
Usage:
|
|
978
|
+
<ask_followup_question>
|
|
979
|
+
<question>Your question here</question>
|
|
980
|
+
</ask_followup_question>
|
|
981
|
+
|
|
982
|
+
## attempt_completion
|
|
983
|
+
Description: After each tool use, the user will respond with the result of that tool use, i.e. if it succeeded or failed, along with any reasons for failure. Once you've received the results of tool uses and can confirm that the task is complete, use this tool to present the result of your work to the user. Optionally you may provide a CLI command to showcase the result of your work. The user may respond with feedback if they are not satisfied with the result, which you can use to make improvements and try again.
|
|
984
|
+
IMPORTANT NOTE: This tool CANNOT be used until you've confirmed from the user that any previous tool uses were successful. Failure to do so will result in code corruption and system failure. Before using this tool, you must ask yourself in <thinking></thinking> tags if you've confirmed from the user that any previous tool uses were successful. If not, then DO NOT use this tool.
|
|
985
|
+
Parameters:
|
|
986
|
+
- result: (required) The result of the task. Formulate this result in a way that is final and does not require further input from the user. Don't end your result with questions or offers for further assistance.
|
|
987
|
+
- command: (optional) A CLI command to execute to show a live demo of the result to the user. For example, use \`open index.html\` to display a created html website, or \`open localhost:3000\` to display a locally running development server. But DO NOT use commands like \`echo\` or \`cat\` that merely print text. This command should be valid for the current operating system. Ensure the command is properly formatted and does not contain any harmful instructions.
|
|
988
|
+
Usage:
|
|
989
|
+
<attempt_completion>
|
|
990
|
+
<result>
|
|
991
|
+
Your final result description here
|
|
992
|
+
</result>
|
|
993
|
+
<command>Command to demonstrate result (optional)</command>
|
|
994
|
+
</attempt_completion>
|
|
995
|
+
|
|
996
|
+
# Tool Use Examples
|
|
997
|
+
|
|
998
|
+
## Example 1: Requesting to execute a command
|
|
999
|
+
|
|
1000
|
+
<execute_command>
|
|
1001
|
+
<command>npm run dev</command>
|
|
1002
|
+
</execute_command>
|
|
1003
|
+
|
|
1004
|
+
## Example 2: Requesting to write to a file
|
|
1005
|
+
|
|
1006
|
+
<write_to_file>
|
|
1007
|
+
<path>frontend-config.json</path>
|
|
1008
|
+
<content>
|
|
1009
|
+
{
|
|
1010
|
+
"apiEndpoint": "https://api.example.com",
|
|
1011
|
+
"theme": {
|
|
1012
|
+
"primaryColor": "#007bff",
|
|
1013
|
+
"secondaryColor": "#6c757d",
|
|
1014
|
+
"fontFamily": "Arial, sans-serif"
|
|
1015
|
+
},
|
|
1016
|
+
"features": {
|
|
1017
|
+
"darkMode": true,
|
|
1018
|
+
"notifications": true,
|
|
1019
|
+
"analytics": false
|
|
1020
|
+
},
|
|
1021
|
+
"version": "1.0.0"
|
|
1022
|
+
}
|
|
1023
|
+
</content>
|
|
1024
|
+
</write_to_file>
|
|
1025
|
+
|
|
1026
|
+
# Tool Use Guidelines
|
|
1027
|
+
|
|
1028
|
+
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
|
|
1029
|
+
2. Choose the most appropriate tool based on the task and the tool descriptions provided. Assess if you need additional information to proceed, and which of the available tools would be most effective for gathering this information. For example using the list_files tool is more effective than running a command like \`ls\` in the terminal. It's critical that you think about each available tool and use the one that best fits the current step in the task.
|
|
1030
|
+
3. If multiple actions are needed, use one tool at a time per message to accomplish the task iteratively, with each tool use being informed by the result of the previous tool use. Do not assume the outcome of any tool use. Each step must be informed by the previous step's result.
|
|
1031
|
+
4. Formulate your tool use using the XML format specified for each tool.
|
|
1032
|
+
5. After each tool use, the user will respond with the result of that tool use. This result will provide you with the necessary information to continue your task or make further decisions. This response may include:
|
|
1033
|
+
- Information about whether the tool succeeded or failed, along with any reasons for failure.
|
|
1034
|
+
- Linter errors that may have arisen due to the changes you made, which you'll need to address.
|
|
1035
|
+
- New terminal output in reaction to the changes, which you may need to consider or act upon.
|
|
1036
|
+
- Any other relevant feedback or information related to the tool use.
|
|
1037
|
+
6. ALWAYS wait for user confirmation after each tool use before proceeding. Never assume the success of a tool use without explicit confirmation of the result from the user.
|
|
1038
|
+
|
|
1039
|
+
It is crucial to proceed step-by-step, waiting for the user's message after each tool use before moving forward with the task. This approach allows you to:
|
|
1040
|
+
1. Confirm the success of each step before proceeding.
|
|
1041
|
+
2. Address any issues or errors that arise immediately.
|
|
1042
|
+
3. Adapt your approach based on new information or unexpected results.
|
|
1043
|
+
4. Ensure that each action builds correctly on the previous ones.
|
|
1044
|
+
|
|
1045
|
+
By waiting for and carefully considering the user's response after each tool use, you can react accordingly and make informed decisions about how to proceed with the task. This iterative process helps ensure the overall success and accuracy of your work.
|
|
1046
|
+
|
|
1047
|
+
====
|
|
1048
|
+
|
|
1049
|
+
CAPABILITIES
|
|
1050
|
+
|
|
1051
|
+
- You have access to tools that let you execute CLI commands on the user's computer, list files, view source code definitions, regex search, {%- if support_computer_use -%}use the browser{%- endif -%}, read and write files, and ask follow-up questions. These tools help you effectively accomplish a wide range of tasks, such as writing code, making edits or improvements to existing files, understanding the current state of a project, performing system operations, and much more.
|
|
1052
|
+
- When the user initially gives you a task, a recursive list of all filepaths in the current working directory ('{{ cwd }}') will be included in environment_details. This provides an overview of the project's file structure, offering key insights into the project from directory/file names (how developers conceptualize and organize their code) and file extensions (the language used). This can also guide decision-making on which files to explore further. If you need to further explore directories such as outside the current working directory, you can use the list_files tool. If you pass 'true' for the recursive parameter, it will list files recursively. Otherwise, it will list files at the top level, which is better suited for generic directories where you don't necessarily need the nested structure, like the Desktop.
|
|
1053
|
+
- You can use search_files to perform regex searches across files in a specified directory, outputting context-rich results that include surrounding lines. This is particularly useful for understanding code patterns, finding specific implementations, or identifying areas that need refactoring.
|
|
1054
|
+
- You can use the list_code_definition_names tool to get an overview of source code definitions for all files at the top level of a specified directory. This can be particularly useful when you need to understand the broader context and relationships between certain parts of the code. You may need to call this tool multiple times to understand various parts of the codebase related to the task.
|
|
1055
|
+
- For example, when asked to make edits or improvements you might analyze the file structure in the initial environment_details to get an overview of the project, then use list_code_definition_names to get further insight using source code definitions for files located in relevant directories, then read_file to examine the contents of relevant files, analyze the code and suggest improvements or make necessary edits, then use the write_to_file tool to implement changes. If you refactored code that could affect other parts of the codebase, you could use search_files to ensure you update other files as needed.
|
|
1056
|
+
- You can use the execute_command tool to run commands on the user's computer whenever you feel it can help accomplish the user's task. When you need to execute a CLI command, you must provide a clear explanation of what the command does. Prefer to execute complex CLI commands over creating executable scripts, since they are more flexible and easier to run. Interactive and long-running commands are allowed, since the commands are run in the user's VSCode terminal. The user may keep commands running in the background and you will be kept updated on their status along the way. Each command you execute is run in a new terminal instance.{%- if support_computer_use -%}
|
|
1057
|
+
\n- You can use the browser_action tool to interact with websites (including html files and locally running development servers) through a Puppeteer-controlled browser when you feel it is necessary in accomplishing the user's task. This tool is particularly useful for web development tasks as it allows you to launch a browser, navigate to pages, interact with elements through clicks and keyboard input, and capture the results through screenshots and console logs. This tool may be useful at key stages of web development tasks-such as after implementing new features, making substantial changes, when troubleshooting issues, or to verify the result of your work. You can analyze the provided screenshots to ensure correct rendering or identify errors, and review console logs for runtime issues.\n - For example, if asked to add a component to a react website, you might create the necessary files, use execute_command to run the site locally, then use browser_action to launch the browser, navigate to the local server, and verify the component renders & functions correctly before closing the browser.
|
|
1058
|
+
{%- endif -%}
|
|
1059
|
+
|
|
1060
|
+
====
|
|
1061
|
+
|
|
1062
|
+
RULES
|
|
1063
|
+
|
|
1064
|
+
- Your current working directory is: {{ cwd }}
|
|
1065
|
+
- You cannot \`cd\` into a different directory to complete a task. You are stuck operating from '{{ cwd }}', so be sure to pass in the correct 'path' parameter when using tools that require a path.
|
|
1066
|
+
- Do not use the ~ character or $HOME to refer to the home directory.
|
|
1067
|
+
- Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '{{ cwd }}', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '{{ cwd }}'). For example, if you needed to run \`npm install\` in a project outside of '{{ cwd }}', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`.
|
|
1068
|
+
- When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes.
|
|
1069
|
+
- When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser.
|
|
1070
|
+
- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write.
|
|
1071
|
+
- When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices.
|
|
1072
|
+
- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool.
|
|
1073
|
+
- Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again.
|
|
1074
|
+
- You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves.
|
|
1075
|
+
- When executing commands, if you don't see the expected output, assume the terminal executed the command successfully and proceed with the task. The user's terminal may be unable to stream the output back properly. If you absolutely need to see the actual terminal output, use the ask_followup_question tool to request the user to copy and paste it back to you.
|
|
1076
|
+
- The user may provide a file's contents directly in their message, in which case you shouldn't use the read_file tool to get the file contents again since you already have it.
|
|
1077
|
+
- Your goal is to try to accomplish the user's task, NOT engage in a back and forth conversation.{%- if support_computer_use -%}
|
|
1078
|
+
\n- The user may ask generic non-development tasks, such as "what\'s the latest news" or "look up the weather in San Diego", in which case you might use the browser_action tool to complete the task if it makes sense to do so, rather than trying to create a website or using curl to answer the question.
|
|
1079
|
+
{%- endif -%}
|
|
1080
|
+
- NEVER end attempt_completion result with a question or request to engage in further conversation! Formulate the end of your result in a way that is final and does not require further input from the user.
|
|
1081
|
+
- You are STRICTLY FORBIDDEN from starting your messages with "Great", "Certainly", "Okay", "Sure". You should NOT be conversational in your responses, but rather direct and to the point. For example you should NOT say "Great, I've updated the CSS" but instead something like "I've updated the CSS". It is important you be clear and technical in your messages.
|
|
1082
|
+
- When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task.
|
|
1083
|
+
- At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details.
|
|
1084
|
+
- Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal.
|
|
1085
|
+
- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project.
|
|
1086
|
+
- It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc.{%- if support_computer_use -%} Then if you want to test your work, you might use browser_action to launch the site, wait for the user's response confirming the site was launched along with a screenshot, then perhaps e.g., click a button to test functionality if needed, wait for the user's response confirming the button was clicked along with a screenshot of the new state, before finally closing the browser.{%- endif -%}
|
|
1087
|
+
|
|
1088
|
+
====
|
|
1089
|
+
|
|
1090
|
+
SYSTEM INFORMATION
|
|
1091
|
+
|
|
1092
|
+
Operating System: {{ osName }}
|
|
1093
|
+
Default Shell: {{ defaultShell }}
|
|
1094
|
+
Home Directory: {{ homedir }}
|
|
1095
|
+
Current Working Directory: {{ cwd }}
|
|
1096
|
+
|
|
1097
|
+
====
|
|
1098
|
+
|
|
1099
|
+
OBJECTIVE
|
|
1100
|
+
|
|
1101
|
+
You accomplish a given task iteratively, breaking it down into clear steps and working through them methodically.
|
|
1102
|
+
|
|
1103
|
+
1. Analyze the user's task and set clear, achievable goals to accomplish it. Prioritize these goals in a logical order.
|
|
1104
|
+
2. Work through these goals sequentially, utilizing available tools one at a time as necessary. Each goal should correspond to a distinct step in your problem-solving process. You will be informed on the work completed and what's remaining as you go.
|
|
1105
|
+
3. Remember, you have extensive capabilities with access to a wide range of tools that can be used in powerful and clever ways as necessary to accomplish each goal. Before calling a tool, do some analysis within <thinking></thinking> tags. First, analyze the file structure provided in environment_details to gain context and insights for proceeding effectively. Then, think about which of the provided tools is the most relevant tool to accomplish the user's task. Next, go through each of the required parameters of the relevant tool and determine if the user has directly provided or given enough information to infer a value. When deciding if the parameter can be inferred, carefully consider all the context to see if it supports a specific value. If all of the required parameters are present or can be reasonably inferred, close the thinking tag and proceed with the tool use. BUT, if one of the values for a required parameter is missing, DO NOT invoke the tool (not even with fillers for the missing params) and instead, ask the user to provide the missing parameters using the ask_followup_question tool. DO NOT ask for more information on optional parameters if it is not provided.
|
|
1106
|
+
4. Once you've completed the user's task, you must use the attempt_completion tool to present the result of the task to the user. You may also provide a CLI command to showcase the result of your task; this can be particularly useful for web development tasks, where you can run e.g. \`open index.html\` to show the website you've built.
|
|
1107
|
+
5. The user may provide feedback, which you can use to make improvements and try again. But DO NOT continue in pointless back and forth conversations, i.e. don't end your responses with questions or offers for further assistance.`
|
|
1108
|
+
|
|
1109
|
+
{%- if custom_instructions -%}
|
|
1110
|
+
====
|
|
1111
|
+
|
|
1112
|
+
USER'S CUSTOM INSTRUCTIONS
|
|
1113
|
+
|
|
1114
|
+
The following additional instructions are provided by the user, and should be followed to the best of your ability without interfering with the TOOL USE guidelines.
|
|
1115
|
+
|
|
1116
|
+
{{ custom_instructions }}
|
|
1117
|
+
{%- endif -%}
|
|
21
1118
|
'''
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
1119
|
+
env = detect_env()
|
|
1120
|
+
res = {
|
|
1121
|
+
"cwd": env.cwd,
|
|
1122
|
+
"customInstructions": custom_instructions,
|
|
1123
|
+
"osName": env.os_name,
|
|
1124
|
+
"defaultShell": env.default_shell,
|
|
1125
|
+
"homedir": env.home_dir
|
|
1126
|
+
}
|
|
1127
|
+
return res
|
|
1128
|
+
|
|
1129
|
+
def parse_assistant_message(self, msg: str):
|
|
1130
|
+
content_blocks = []
|
|
1131
|
+
current_text_content = None
|
|
1132
|
+
current_text_content_start_index = 0
|
|
1133
|
+
current_tool_use = None
|
|
1134
|
+
current_tool_use_start_index = 0
|
|
1135
|
+
current_param_name = None
|
|
1136
|
+
current_param_value_start_index = 0
|
|
1137
|
+
accumulator = ""
|
|
1138
|
+
|
|
1139
|
+
for i, char in enumerate(msg):
|
|
1140
|
+
accumulator += char
|
|
1141
|
+
|
|
1142
|
+
# there should not be a param without a tool use
|
|
1143
|
+
if current_tool_use is not None and current_param_name is not None:
|
|
1144
|
+
current_param_value = accumulator[current_param_value_start_index:]
|
|
1145
|
+
param_closing_tag = f"</{current_param_name}>"
|
|
1146
|
+
if current_param_value.endswith(param_closing_tag):
|
|
1147
|
+
# end of param value
|
|
1148
|
+
current_tool_use["params"][current_param_name] = current_param_value[:-len(
|
|
1149
|
+
param_closing_tag)].strip()
|
|
1150
|
+
current_param_name = None
|
|
1151
|
+
continue
|
|
1152
|
+
else:
|
|
1153
|
+
# partial param value is accumulating
|
|
1154
|
+
continue
|
|
1155
|
+
|
|
1156
|
+
# no currentParamName
|
|
1157
|
+
if current_tool_use is not None:
|
|
1158
|
+
current_tool_value = accumulator[current_tool_use_start_index:]
|
|
1159
|
+
tool_use_closing_tag = f"</{current_tool_use['name']}>"
|
|
1160
|
+
if current_tool_value.endswith(tool_use_closing_tag):
|
|
1161
|
+
# end of a tool use
|
|
1162
|
+
current_tool_use["partial"] = False
|
|
1163
|
+
content_blocks.append(current_tool_use)
|
|
1164
|
+
current_tool_use = None
|
|
1165
|
+
continue
|
|
1166
|
+
else:
|
|
1167
|
+
# Check for possible param opening tags
|
|
1168
|
+
for param_name in ["command", "path", "content", "regex", "file_pattern",
|
|
1169
|
+
"recursive", "action", "url", "coordinate", "text",
|
|
1170
|
+
"question", "result"]:
|
|
1171
|
+
param_opening_tag = f"<{param_name}>"
|
|
1172
|
+
if accumulator.endswith(param_opening_tag):
|
|
1173
|
+
# start of a new parameter
|
|
1174
|
+
current_param_name = param_name
|
|
1175
|
+
current_param_value_start_index = len(accumulator)
|
|
1176
|
+
break
|
|
1177
|
+
|
|
1178
|
+
# Special case for write_to_file content param
|
|
1179
|
+
if current_tool_use["name"] == "write_to_file" and accumulator.endswith("</content>"):
|
|
1180
|
+
tool_content = accumulator[current_tool_use_start_index:]
|
|
1181
|
+
content_start_tag = "<content>"
|
|
1182
|
+
content_end_tag = "</content>"
|
|
1183
|
+
content_start_index = tool_content.find(
|
|
1184
|
+
content_start_tag) + len(content_start_tag)
|
|
1185
|
+
content_end_index = tool_content.rfind(content_end_tag)
|
|
1186
|
+
if content_start_index != -1 and content_end_index != -1 and content_end_index > content_start_index:
|
|
1187
|
+
current_tool_use["params"]["content"] = tool_content[content_start_index:content_end_index].strip(
|
|
1188
|
+
)
|
|
1189
|
+
|
|
1190
|
+
continue
|
|
1191
|
+
|
|
1192
|
+
# no currentToolUse
|
|
1193
|
+
did_start_tool_use = False
|
|
1194
|
+
for tool_name in ["execute_command", "read_file", "write_to_file", "search_files",
|
|
1195
|
+
"list_files", "list_code_definition_names", "browser_action",
|
|
1196
|
+
"ask_followup_question", "attempt_completion"]:
|
|
1197
|
+
tool_use_opening_tag = f"<{tool_name}>"
|
|
1198
|
+
if accumulator.endswith(tool_use_opening_tag):
|
|
1199
|
+
# start of a new tool use
|
|
1200
|
+
current_tool_use = {
|
|
1201
|
+
"type": "tool_use",
|
|
1202
|
+
"name": tool_name,
|
|
1203
|
+
"params": {},
|
|
1204
|
+
"partial": True
|
|
1205
|
+
}
|
|
1206
|
+
current_tool_use_start_index = len(accumulator)
|
|
1207
|
+
# this also indicates the end of the current text content
|
|
1208
|
+
if current_text_content is not None:
|
|
1209
|
+
current_text_content["partial"] = False
|
|
1210
|
+
# remove the partially accumulated tool use tag from the end of text
|
|
1211
|
+
current_text_content["content"] = current_text_content["content"][:-len(
|
|
1212
|
+
tool_use_opening_tag[:-1])].strip()
|
|
1213
|
+
content_blocks.append(current_text_content)
|
|
1214
|
+
current_text_content = None
|
|
1215
|
+
|
|
1216
|
+
did_start_tool_use = True
|
|
1217
|
+
break
|
|
1218
|
+
|
|
1219
|
+
if not did_start_tool_use:
|
|
1220
|
+
# no tool use, so it must be text either at the beginning or between tools
|
|
1221
|
+
if current_text_content is None:
|
|
1222
|
+
current_text_content_start_index = i
|
|
1223
|
+
current_text_content = {
|
|
1224
|
+
"type": "text",
|
|
1225
|
+
"content": accumulator[current_text_content_start_index:].strip(),
|
|
1226
|
+
"partial": True
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
# Handle incomplete blocks at the end of message
|
|
1230
|
+
if current_tool_use is not None:
|
|
1231
|
+
# stream did not complete tool call
|
|
1232
|
+
if current_param_name is not None:
|
|
1233
|
+
# tool call has a parameter that was not completed
|
|
1234
|
+
current_tool_use["params"][current_param_name] = accumulator[current_param_value_start_index:].strip(
|
|
1235
|
+
)
|
|
1236
|
+
content_blocks.append(current_tool_use)
|
|
1237
|
+
elif current_text_content is not None:
|
|
1238
|
+
# stream did not complete text content
|
|
1239
|
+
content_blocks.append(current_text_content)
|
|
1240
|
+
|
|
1241
|
+
return content_blocks
|