wcgw 2.8.10__py3-none-any.whl → 3.0.1rc1__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 wcgw might be problematic. Click here for more details.
- wcgw/__init__.py +1 -1
- wcgw/client/bash_state/bash_state.py +768 -0
- wcgw/client/encoder/__init__.py +47 -0
- wcgw/client/mcp_server/Readme.md +2 -88
- wcgw/client/mcp_server/__init__.py +2 -2
- wcgw/client/mcp_server/server.py +46 -198
- wcgw/client/modes.py +3 -9
- wcgw/client/repo_ops/display_tree.py +1 -12
- wcgw/client/tool_prompts.py +105 -0
- wcgw/client/tools.py +230 -1084
- wcgw/py.typed +0 -0
- wcgw/relay/client.py +95 -0
- wcgw/relay/serve.py +11 -45
- wcgw/types_.py +51 -61
- {wcgw-2.8.10.dist-info → wcgw-3.0.1rc1.dist-info}/METADATA +2 -3
- {wcgw-2.8.10.dist-info → wcgw-3.0.1rc1.dist-info}/RECORD +22 -19
- wcgw_cli/anthropic_client.py +253 -365
- wcgw_cli/cli.py +0 -2
- wcgw_cli/openai_client.py +223 -280
- wcgw/client/computer_use.py +0 -435
- wcgw/client/sys_utils.py +0 -41
- {wcgw-2.8.10.dist-info → wcgw-3.0.1rc1.dist-info}/WHEEL +0 -0
- {wcgw-2.8.10.dist-info → wcgw-3.0.1rc1.dist-info}/entry_points.txt +0 -0
- {wcgw-2.8.10.dist-info → wcgw-3.0.1rc1.dist-info}/licenses/LICENSE +0 -0
wcgw_cli/anthropic_client.py
CHANGED
|
@@ -10,11 +10,12 @@ from pathlib import Path
|
|
|
10
10
|
from typing import Literal, Optional, cast
|
|
11
11
|
|
|
12
12
|
import rich
|
|
13
|
-
from anthropic import Anthropic
|
|
13
|
+
from anthropic import Anthropic, MessageStopEvent
|
|
14
14
|
from anthropic.types import (
|
|
15
15
|
ImageBlockParam,
|
|
16
16
|
MessageParam,
|
|
17
17
|
ModelParam,
|
|
18
|
+
RawMessageStartEvent,
|
|
18
19
|
TextBlockParam,
|
|
19
20
|
ToolParam,
|
|
20
21
|
ToolResultBlockParam,
|
|
@@ -24,30 +25,18 @@ from dotenv import load_dotenv
|
|
|
24
25
|
from pydantic import BaseModel
|
|
25
26
|
from typer import Typer
|
|
26
27
|
|
|
28
|
+
from wcgw.client.bash_state.bash_state import BashState
|
|
27
29
|
from wcgw.client.common import CostData, discard_input
|
|
28
30
|
from wcgw.client.memory import load_memory
|
|
31
|
+
from wcgw.client.tool_prompts import TOOL_PROMPTS
|
|
29
32
|
from wcgw.client.tools import (
|
|
30
|
-
|
|
33
|
+
Context,
|
|
31
34
|
ImageData,
|
|
32
35
|
default_enc,
|
|
33
36
|
get_tool_output,
|
|
34
37
|
initialize,
|
|
35
38
|
which_tool_name,
|
|
36
39
|
)
|
|
37
|
-
from wcgw.types_ import (
|
|
38
|
-
BashCommand,
|
|
39
|
-
BashInteraction,
|
|
40
|
-
ContextSave,
|
|
41
|
-
FileEdit,
|
|
42
|
-
GetScreenInfo,
|
|
43
|
-
Keyboard,
|
|
44
|
-
Mouse,
|
|
45
|
-
ReadFiles,
|
|
46
|
-
ReadImage,
|
|
47
|
-
ResetShell,
|
|
48
|
-
ScreenShot,
|
|
49
|
-
WriteIfEmpty,
|
|
50
|
-
)
|
|
51
40
|
|
|
52
41
|
|
|
53
42
|
class Config(BaseModel):
|
|
@@ -129,7 +118,6 @@ def loop(
|
|
|
129
118
|
first_message: Optional[str] = None,
|
|
130
119
|
limit: Optional[float] = None,
|
|
131
120
|
resume: Optional[str] = None,
|
|
132
|
-
computer_use: bool = False,
|
|
133
121
|
) -> tuple[str, float]:
|
|
134
122
|
load_dotenv()
|
|
135
123
|
|
|
@@ -143,8 +131,8 @@ def loop(
|
|
|
143
131
|
_, memory, _ = load_memory(
|
|
144
132
|
resume,
|
|
145
133
|
8000,
|
|
146
|
-
lambda x: default_enc.
|
|
147
|
-
lambda x: default_enc.
|
|
134
|
+
lambda x: default_enc.encoder(x),
|
|
135
|
+
lambda x: default_enc.decoder(x),
|
|
148
136
|
)
|
|
149
137
|
except OSError:
|
|
150
138
|
if resume == "latest":
|
|
@@ -208,162 +196,14 @@ def loop(
|
|
|
208
196
|
|
|
209
197
|
tools = [
|
|
210
198
|
ToolParam(
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
- Optionally `exit shell has restarted` is the output, in which case environment resets, you can run fresh commands.
|
|
218
|
-
- The first or the last line might be `(...truncated)` if the output is too long.
|
|
219
|
-
- Always run `pwd` if you get any file or directory not found error to make sure you're not lost.
|
|
220
|
-
- The control will return to you in 5 seconds regardless of the status. For heavy commands, keep checking status using BashInteraction till they are finished.
|
|
221
|
-
- Run long running commands in background using screen instead of "&".
|
|
222
|
-
- Use longer wait_for_seconds if the command is expected to run for a long time.
|
|
223
|
-
- Do not use 'cat' to read files, use ReadFiles tool instead.
|
|
224
|
-
""",
|
|
225
|
-
),
|
|
226
|
-
ToolParam(
|
|
227
|
-
input_schema=BashInteraction.model_json_schema(),
|
|
228
|
-
name="BashInteraction",
|
|
229
|
-
description="""
|
|
230
|
-
- Interact with running program using this tool
|
|
231
|
-
- Special keys like arrows, interrupts, enter, etc.
|
|
232
|
-
- Send text input to the running program.
|
|
233
|
-
- Send send_specials=["Enter"] to recheck status of a running program.
|
|
234
|
-
- Only one of send_text, send_specials, send_ascii should be provided.
|
|
235
|
-
- This returns within 5 seconds, for heavy programs keep checking status for upto 10 turns before asking user to continue checking again.
|
|
236
|
-
- Programs don't hang easily, so most likely explanation for no output is usually that the program is still running, and you need to check status again using ["Enter"].
|
|
237
|
-
- Do not send Ctrl-c before checking for status till 10 minutes or whatever is appropriate for the program to finish.
|
|
238
|
-
- Set longer wait_for_seconds when program is expected to run for a long time.
|
|
239
|
-
""",
|
|
240
|
-
),
|
|
241
|
-
ToolParam(
|
|
242
|
-
input_schema=ReadFiles.model_json_schema(),
|
|
243
|
-
name="ReadFiles",
|
|
244
|
-
description="""
|
|
245
|
-
- Read full file content of one or more files.
|
|
246
|
-
- Provide absolute file paths only
|
|
247
|
-
""",
|
|
248
|
-
),
|
|
249
|
-
ToolParam(
|
|
250
|
-
input_schema=WriteIfEmpty.model_json_schema(),
|
|
251
|
-
name="WriteIfEmpty",
|
|
252
|
-
description="""
|
|
253
|
-
- Write content to an empty or non-existent file. Provide file path and content. Use this instead of BashCommand for writing new files.
|
|
254
|
-
- Provide absolute file path only.
|
|
255
|
-
- For editing existing files, use FileEdit instead of this tool.
|
|
256
|
-
""",
|
|
257
|
-
),
|
|
258
|
-
ToolParam(
|
|
259
|
-
input_schema=ReadImage.model_json_schema(),
|
|
260
|
-
name="ReadImage",
|
|
261
|
-
description="Read an image from the shell.",
|
|
262
|
-
),
|
|
263
|
-
ToolParam(
|
|
264
|
-
input_schema=ResetShell.model_json_schema(),
|
|
265
|
-
name="ResetShell",
|
|
266
|
-
description="Resets the shell. Use only if all interrupts and prompt reset attempts have failed repeatedly.\nAlso exits the docker environment.\nYou need to call GetScreenInfo again",
|
|
267
|
-
),
|
|
268
|
-
ToolParam(
|
|
269
|
-
input_schema=FileEdit.model_json_schema(),
|
|
270
|
-
name="FileEdit",
|
|
271
|
-
description="""
|
|
272
|
-
- Use absolute file path only.
|
|
273
|
-
- Use SEARCH/REPLACE blocks to edit the file.
|
|
274
|
-
- If the edit fails due to block not matching, please retry with correct block till it matches. Re-read the file to ensure you've all the lines correct.
|
|
275
|
-
""",
|
|
276
|
-
),
|
|
277
|
-
ToolParam(
|
|
278
|
-
input_schema=ContextSave.model_json_schema(),
|
|
279
|
-
name="ContextSave",
|
|
280
|
-
description="""
|
|
281
|
-
Saves provided description and file contents of all the relevant file paths or globs in a single text file.
|
|
282
|
-
- Provide random unqiue id or whatever user provided.
|
|
283
|
-
- Leave project path as empty string if no project path
|
|
284
|
-
""",
|
|
285
|
-
),
|
|
199
|
+
name=tool.name,
|
|
200
|
+
description=tool.description,
|
|
201
|
+
input_schema=tool.inputSchema,
|
|
202
|
+
)
|
|
203
|
+
for tool in TOOL_PROMPTS
|
|
204
|
+
if tool.name != "Initialize"
|
|
286
205
|
]
|
|
287
206
|
|
|
288
|
-
if computer_use:
|
|
289
|
-
tools += [
|
|
290
|
-
ToolParam(
|
|
291
|
-
input_schema=GetScreenInfo.model_json_schema(),
|
|
292
|
-
name="GetScreenInfo",
|
|
293
|
-
description="""
|
|
294
|
-
- Important: call this first in the conversation before ScreenShot, Mouse, and Keyboard tools.
|
|
295
|
-
- Get display information of a linux os running on docker using image "ghcr.io/anthropics/anthropic-quickstarts:computer-use-demo-latest"
|
|
296
|
-
- If user hasn't provided docker image id, check using `docker ps` and provide the id.
|
|
297
|
-
- If the docker is not running, run using `docker run -d -p 6080:6080 ghcr.io/anthropics/anthropic-quickstarts:computer-use-demo-latest`
|
|
298
|
-
- Connects shell to the docker environment.
|
|
299
|
-
- Note: once this is called, the shell enters the docker environment. All bash commands will run over there.
|
|
300
|
-
""",
|
|
301
|
-
),
|
|
302
|
-
ToolParam(
|
|
303
|
-
input_schema=ScreenShot.model_json_schema(),
|
|
304
|
-
name="ScreenShot",
|
|
305
|
-
description="""
|
|
306
|
-
- Capture screenshot of the linux os on docker.
|
|
307
|
-
- All actions on UI using mouse and keyboard return within 0.5 seconds.
|
|
308
|
-
* So if you're doing something that takes longer for UI to update like heavy page loading, keep checking UI for update using ScreenShot upto 10 turns.
|
|
309
|
-
* Notice for smallest of the loading icons to check if your action worked.
|
|
310
|
-
* After 10 turns of no change, ask user for permission to keep checking.
|
|
311
|
-
* If you don't notice even slightest of the change, it's likely you clicked on the wrong place.
|
|
312
|
-
|
|
313
|
-
""",
|
|
314
|
-
),
|
|
315
|
-
ToolParam(
|
|
316
|
-
input_schema=Mouse.model_json_schema(),
|
|
317
|
-
name="Mouse",
|
|
318
|
-
description="""
|
|
319
|
-
- Interact with the linux os on docker using mouse.
|
|
320
|
-
- Uses xdotool
|
|
321
|
-
- About left_click_drag: the current mouse position will be used as the starting point, click and drag to the given x, y coordinates. Useful in things like sliders, moving things around, etc.
|
|
322
|
-
- The output of this command has the screenshot after doing this action. Use this to verify if the action was successful.
|
|
323
|
-
""",
|
|
324
|
-
),
|
|
325
|
-
ToolParam(
|
|
326
|
-
input_schema=Keyboard.model_json_schema(),
|
|
327
|
-
name="Keyboard",
|
|
328
|
-
description="""
|
|
329
|
-
- Interact with the linux os on docker using keyboard.
|
|
330
|
-
- Emulate keyboard input to the screen
|
|
331
|
-
- Uses xdootool to send keyboard input, keys like Return, BackSpace, Escape, Page_Up, etc. can be used.
|
|
332
|
-
- Do not use it to interact with Bash tool.
|
|
333
|
-
- Make sure you've selected a text area or an editable element before sending text.
|
|
334
|
-
- The output of this command has the screenshot after doing this action. Use this to verify if the action was successful.
|
|
335
|
-
""",
|
|
336
|
-
),
|
|
337
|
-
]
|
|
338
|
-
|
|
339
|
-
system = initialize(
|
|
340
|
-
os.getcwd(),
|
|
341
|
-
[],
|
|
342
|
-
resume if (memory and resume) else "",
|
|
343
|
-
max_tokens=8000,
|
|
344
|
-
mode="wcgw",
|
|
345
|
-
)
|
|
346
|
-
|
|
347
|
-
with open(
|
|
348
|
-
os.path.join(
|
|
349
|
-
os.path.dirname(__file__), "..", "wcgw", "client", "diff-instructions.txt"
|
|
350
|
-
)
|
|
351
|
-
) as f:
|
|
352
|
-
system += f.read()
|
|
353
|
-
|
|
354
|
-
if history:
|
|
355
|
-
if (
|
|
356
|
-
(last_msg := history[-1])["role"] == "user"
|
|
357
|
-
and isinstance((content := last_msg["content"]), dict)
|
|
358
|
-
and content["type"] == "tool_result"
|
|
359
|
-
):
|
|
360
|
-
waiting_for_assistant = True
|
|
361
|
-
|
|
362
|
-
client = Anthropic()
|
|
363
|
-
|
|
364
|
-
cost: float = 0
|
|
365
|
-
input_toks = 0
|
|
366
|
-
output_toks = 0
|
|
367
207
|
system_console = rich.console.Console(style="blue", highlight=False, markup=False)
|
|
368
208
|
error_console = rich.console.Console(style="red", highlight=False, markup=False)
|
|
369
209
|
user_console = rich.console.Console(
|
|
@@ -372,216 +212,264 @@ Saves provided description and file contents of all the relevant file paths or g
|
|
|
372
212
|
assistant_console = rich.console.Console(
|
|
373
213
|
style="white bold", highlight=False, markup=False
|
|
374
214
|
)
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
215
|
+
|
|
216
|
+
with BashState(
|
|
217
|
+
system_console, os.getcwd(), None, None, None, None, False, None
|
|
218
|
+
) as bash_state:
|
|
219
|
+
context = Context(bash_state, system_console)
|
|
220
|
+
|
|
221
|
+
system, context = initialize(
|
|
222
|
+
context,
|
|
223
|
+
os.getcwd(),
|
|
224
|
+
[],
|
|
225
|
+
resume if (memory and resume) else "",
|
|
226
|
+
max_tokens=8000,
|
|
227
|
+
mode="wcgw",
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
with open(
|
|
231
|
+
os.path.join(
|
|
232
|
+
os.path.dirname(__file__),
|
|
233
|
+
"..",
|
|
234
|
+
"wcgw",
|
|
235
|
+
"client",
|
|
236
|
+
"diff-instructions.txt",
|
|
386
237
|
)
|
|
238
|
+
) as f:
|
|
239
|
+
system += f.read()
|
|
240
|
+
|
|
241
|
+
if history:
|
|
242
|
+
if (
|
|
243
|
+
(last_msg := history[-1])["role"] == "user"
|
|
244
|
+
and isinstance((content := last_msg["content"]), dict)
|
|
245
|
+
and content["type"] == "tool_result"
|
|
246
|
+
):
|
|
247
|
+
waiting_for_assistant = True
|
|
387
248
|
|
|
388
|
-
|
|
389
|
-
if first_message:
|
|
390
|
-
msg = first_message
|
|
391
|
-
first_message = ""
|
|
392
|
-
else:
|
|
393
|
-
msg = text_from_editor(user_console)
|
|
249
|
+
client = Anthropic()
|
|
394
250
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
stream = client.messages.stream(
|
|
399
|
-
model=config.model,
|
|
400
|
-
messages=history,
|
|
401
|
-
tools=tools,
|
|
402
|
-
max_tokens=8096,
|
|
403
|
-
system=system,
|
|
404
|
-
)
|
|
251
|
+
cost: float = 0
|
|
252
|
+
input_toks = 0
|
|
253
|
+
output_toks = 0
|
|
405
254
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
255
|
+
while True:
|
|
256
|
+
if cost > limit:
|
|
257
|
+
system_console.print(
|
|
258
|
+
f"\nCost limit exceeded. Current cost: {config.cost_unit}{cost:.4f}, "
|
|
259
|
+
f"input tokens: {input_toks}"
|
|
260
|
+
f"output tokens: {output_toks}"
|
|
261
|
+
)
|
|
262
|
+
break
|
|
263
|
+
else:
|
|
264
|
+
system_console.print(
|
|
265
|
+
f"\nTotal cost: {config.cost_unit}{cost:.4f}, input tokens: {input_toks}, output tokens: {output_toks}"
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
if not waiting_for_assistant:
|
|
269
|
+
if first_message:
|
|
270
|
+
msg = first_message
|
|
271
|
+
first_message = ""
|
|
272
|
+
else:
|
|
273
|
+
msg = text_from_editor(user_console)
|
|
274
|
+
|
|
275
|
+
history.append(parse_user_message_special(msg))
|
|
276
|
+
else:
|
|
277
|
+
waiting_for_assistant = False
|
|
278
|
+
|
|
279
|
+
stream = client.messages.stream(
|
|
280
|
+
model=config.model,
|
|
281
|
+
messages=history,
|
|
282
|
+
tools=tools,
|
|
283
|
+
max_tokens=8096,
|
|
284
|
+
system=system,
|
|
285
|
+
)
|
|
412
286
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
287
|
+
system_console.print(
|
|
288
|
+
"\n---------------------------------------\n# Assistant response",
|
|
289
|
+
style="bold",
|
|
290
|
+
)
|
|
291
|
+
_histories: History = []
|
|
292
|
+
full_response: str = ""
|
|
293
|
+
|
|
294
|
+
tool_calls = []
|
|
295
|
+
tool_results: list[ToolResultBlockParam] = []
|
|
296
|
+
try:
|
|
297
|
+
with stream as stream_:
|
|
298
|
+
for chunk in stream_:
|
|
299
|
+
type_ = chunk.type
|
|
300
|
+
if isinstance(chunk, RawMessageStartEvent):
|
|
301
|
+
message_start = chunk.message
|
|
302
|
+
# Update cost based on token usage from the API response
|
|
303
|
+
input_tokens = message_start.usage.input_tokens
|
|
304
|
+
input_toks += input_tokens
|
|
305
|
+
cost += (
|
|
306
|
+
input_tokens
|
|
307
|
+
* config.cost_file[
|
|
308
|
+
config.model
|
|
309
|
+
].cost_per_1m_input_tokens
|
|
310
|
+
) / 1_000_000
|
|
311
|
+
elif isinstance(chunk, MessageStopEvent):
|
|
312
|
+
message_stop = chunk.message
|
|
313
|
+
# Update cost based on output tokens
|
|
314
|
+
output_tokens = message_stop.usage.output_tokens
|
|
315
|
+
output_toks += output_tokens
|
|
316
|
+
cost += (
|
|
317
|
+
output_tokens
|
|
318
|
+
* config.cost_file[
|
|
319
|
+
config.model
|
|
320
|
+
].cost_per_1m_output_tokens
|
|
321
|
+
) / 1_000_000
|
|
322
|
+
continue
|
|
323
|
+
elif type_ == "content_block_start" and hasattr(
|
|
324
|
+
chunk, "content_block"
|
|
446
325
|
):
|
|
447
|
-
|
|
448
|
-
assistant_console.print(chunk_str, end="")
|
|
449
|
-
full_response += chunk_str
|
|
450
|
-
elif content_block.type == "tool_use":
|
|
326
|
+
content_block = chunk.content_block
|
|
451
327
|
if (
|
|
452
|
-
hasattr(content_block, "
|
|
453
|
-
and
|
|
454
|
-
and hasattr(content_block, "
|
|
328
|
+
hasattr(content_block, "type")
|
|
329
|
+
and content_block.type == "text"
|
|
330
|
+
and hasattr(content_block, "text")
|
|
455
331
|
):
|
|
456
|
-
|
|
457
|
-
tool_calls.append(
|
|
458
|
-
{
|
|
459
|
-
"name": str(content_block.name),
|
|
460
|
-
"input": str(""),
|
|
461
|
-
"done": False,
|
|
462
|
-
"id": str(content_block.id),
|
|
463
|
-
}
|
|
464
|
-
)
|
|
465
|
-
else:
|
|
466
|
-
error_console.log(
|
|
467
|
-
f"Ignoring unknown content block type {content_block.type}"
|
|
468
|
-
)
|
|
469
|
-
elif type_ == "content_block_delta" and hasattr(chunk, "delta"):
|
|
470
|
-
delta = chunk.delta
|
|
471
|
-
if hasattr(delta, "type"):
|
|
472
|
-
delta_type = str(delta.type)
|
|
473
|
-
if delta_type == "text_delta" and hasattr(delta, "text"):
|
|
474
|
-
chunk_str = delta.text
|
|
332
|
+
chunk_str = content_block.text
|
|
475
333
|
assistant_console.print(chunk_str, end="")
|
|
476
334
|
full_response += chunk_str
|
|
477
|
-
elif
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
335
|
+
elif content_block.type == "tool_use":
|
|
336
|
+
if (
|
|
337
|
+
hasattr(content_block, "input")
|
|
338
|
+
and hasattr(content_block, "name")
|
|
339
|
+
and hasattr(content_block, "id")
|
|
340
|
+
):
|
|
341
|
+
assert content_block.input == {}
|
|
342
|
+
tool_calls.append(
|
|
343
|
+
{
|
|
344
|
+
"name": str(content_block.name),
|
|
345
|
+
"input": str(""),
|
|
346
|
+
"done": False,
|
|
347
|
+
"id": str(content_block.id),
|
|
348
|
+
}
|
|
349
|
+
)
|
|
483
350
|
else:
|
|
484
351
|
error_console.log(
|
|
485
|
-
f"Ignoring unknown content block
|
|
352
|
+
f"Ignoring unknown content block type {content_block.type}"
|
|
486
353
|
)
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
loop,
|
|
523
|
-
max_tokens=8000,
|
|
354
|
+
elif type_ == "content_block_delta" and hasattr(chunk, "delta"):
|
|
355
|
+
delta = chunk.delta
|
|
356
|
+
if hasattr(delta, "type"):
|
|
357
|
+
delta_type = str(delta.type)
|
|
358
|
+
if delta_type == "text_delta" and hasattr(
|
|
359
|
+
delta, "text"
|
|
360
|
+
):
|
|
361
|
+
chunk_str = delta.text
|
|
362
|
+
assistant_console.print(chunk_str, end="")
|
|
363
|
+
full_response += chunk_str
|
|
364
|
+
elif delta_type == "input_json_delta" and hasattr(
|
|
365
|
+
delta, "partial_json"
|
|
366
|
+
):
|
|
367
|
+
partial_json = delta.partial_json
|
|
368
|
+
if isinstance(tool_calls[-1]["input"], str):
|
|
369
|
+
tool_calls[-1]["input"] += partial_json
|
|
370
|
+
else:
|
|
371
|
+
error_console.log(
|
|
372
|
+
f"Ignoring unknown content block delta type {delta_type}"
|
|
373
|
+
)
|
|
374
|
+
else:
|
|
375
|
+
raise ValueError("Content block delta has no type")
|
|
376
|
+
elif type_ == "content_block_stop":
|
|
377
|
+
if tool_calls and not tool_calls[-1]["done"]:
|
|
378
|
+
tc = tool_calls[-1]
|
|
379
|
+
tool_name = str(tc["name"])
|
|
380
|
+
tool_input = str(tc["input"])
|
|
381
|
+
tool_id = str(tc["id"])
|
|
382
|
+
|
|
383
|
+
tool_parsed = which_tool_name(
|
|
384
|
+
tool_name
|
|
385
|
+
).model_validate_json(tool_input)
|
|
386
|
+
|
|
387
|
+
system_console.print(
|
|
388
|
+
f"\n---------------------------------------\n# Assistant invoked tool: {tool_parsed}"
|
|
524
389
|
)
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
390
|
+
|
|
391
|
+
_histories.append(
|
|
392
|
+
{
|
|
393
|
+
"role": "assistant",
|
|
394
|
+
"content": [
|
|
395
|
+
ToolUseBlockParam(
|
|
396
|
+
id=tool_id,
|
|
397
|
+
name=tool_name,
|
|
398
|
+
input=tool_parsed.model_dump(),
|
|
399
|
+
type="tool_use",
|
|
400
|
+
)
|
|
401
|
+
],
|
|
402
|
+
}
|
|
403
|
+
)
|
|
404
|
+
try:
|
|
405
|
+
output_or_dones, _ = get_tool_output(
|
|
406
|
+
context,
|
|
407
|
+
tool_parsed,
|
|
408
|
+
default_enc,
|
|
409
|
+
limit - cost,
|
|
410
|
+
loop,
|
|
411
|
+
max_tokens=8000,
|
|
412
|
+
)
|
|
413
|
+
except Exception as e:
|
|
414
|
+
output_or_dones = [
|
|
415
|
+
(
|
|
416
|
+
f"GOT EXCEPTION while calling tool. Error: {e}"
|
|
417
|
+
)
|
|
418
|
+
]
|
|
419
|
+
tb = traceback.format_exc()
|
|
420
|
+
error_console.print(
|
|
421
|
+
str(output_or_dones) + "\n" + tb
|
|
550
422
|
)
|
|
551
423
|
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
424
|
+
tool_results_content: list[
|
|
425
|
+
TextBlockParam | ImageBlockParam
|
|
426
|
+
] = []
|
|
427
|
+
for output in output_or_dones:
|
|
428
|
+
if isinstance(output, ImageData):
|
|
429
|
+
tool_results_content.append(
|
|
430
|
+
{
|
|
431
|
+
"type": "image",
|
|
432
|
+
"source": {
|
|
433
|
+
"type": "base64",
|
|
434
|
+
"media_type": output.media_type,
|
|
435
|
+
"data": output.data,
|
|
436
|
+
},
|
|
437
|
+
}
|
|
438
|
+
)
|
|
439
|
+
|
|
440
|
+
else:
|
|
441
|
+
tool_results_content.append(
|
|
442
|
+
{
|
|
443
|
+
"type": "text",
|
|
444
|
+
"text": output,
|
|
445
|
+
},
|
|
446
|
+
)
|
|
447
|
+
tool_results.append(
|
|
448
|
+
ToolResultBlockParam(
|
|
449
|
+
type="tool_result",
|
|
450
|
+
tool_use_id=str(tc["id"]),
|
|
451
|
+
content=tool_results_content,
|
|
558
452
|
)
|
|
559
|
-
tool_results.append(
|
|
560
|
-
ToolResultBlockParam(
|
|
561
|
-
type="tool_result",
|
|
562
|
-
tool_use_id=str(tc["id"]),
|
|
563
|
-
content=tool_results_content,
|
|
564
453
|
)
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
save_history(history, session_id)
|
|
454
|
+
else:
|
|
455
|
+
_histories.append(
|
|
456
|
+
{
|
|
457
|
+
"role": "assistant",
|
|
458
|
+
"content": full_response
|
|
459
|
+
if full_response.strip()
|
|
460
|
+
else "...",
|
|
461
|
+
} # Fixes anthropic issue of non empty response only
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
except KeyboardInterrupt:
|
|
465
|
+
waiting_for_assistant = False
|
|
466
|
+
input("Interrupted...enter to redo the current turn")
|
|
467
|
+
else:
|
|
468
|
+
history.extend(_histories)
|
|
469
|
+
if tool_results:
|
|
470
|
+
history.append({"role": "user", "content": tool_results})
|
|
471
|
+
waiting_for_assistant = True
|
|
472
|
+
save_history(history, session_id)
|
|
585
473
|
|
|
586
474
|
return "Couldn't finish the task", cost
|
|
587
475
|
|