wcgw 2.8.4__py3-none-any.whl → 2.8.6__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.

@@ -1,514 +0,0 @@
1
- import base64
2
- import json
3
- import mimetypes
4
- import os
5
- import subprocess
6
- import tempfile
7
- import traceback
8
- import uuid
9
- from pathlib import Path
10
- from typing import Literal, Optional, cast
11
-
12
- import rich
13
- from anthropic import Anthropic
14
- from anthropic.types import (
15
- ImageBlockParam,
16
- MessageParam,
17
- TextBlockParam,
18
- ToolParam,
19
- ToolResultBlockParam,
20
- ToolUseBlockParam,
21
- )
22
- from dotenv import load_dotenv
23
- from typer import Typer
24
-
25
- from ..types_ import (
26
- BashCommand,
27
- BashInteraction,
28
- ContextSave,
29
- FileEdit,
30
- GetScreenInfo,
31
- Keyboard,
32
- Mouse,
33
- ReadFiles,
34
- ReadImage,
35
- ResetShell,
36
- ScreenShot,
37
- WriteIfEmpty,
38
- )
39
- from .common import discard_input
40
- from .memory import load_memory
41
- from .tools import (
42
- DoneFlag,
43
- ImageData,
44
- default_enc,
45
- get_tool_output,
46
- initialize,
47
- which_tool_name,
48
- )
49
-
50
- History = list[MessageParam]
51
-
52
-
53
- def text_from_editor(console: rich.console.Console) -> str:
54
- # First consume all the input till now
55
- discard_input()
56
- console.print("\n---------------------------------------\n# User message")
57
- data = input()
58
- if data:
59
- return data
60
- editor = os.environ.get("EDITOR", "vim")
61
- with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
62
- subprocess.run([editor, tf.name], check=True)
63
- with open(tf.name, "r") as f:
64
- data = f.read()
65
- console.print(data)
66
- return data
67
-
68
-
69
- def save_history(history: History, session_id: str) -> None:
70
- myid = str(history[1]["content"]).replace("/", "_").replace(" ", "_").lower()[:60]
71
- myid += "_" + session_id
72
- myid = myid + ".json"
73
-
74
- mypath = Path(".wcgw") / myid
75
- mypath.parent.mkdir(parents=True, exist_ok=True)
76
- with open(mypath, "w") as f:
77
- json.dump(history, f, indent=3)
78
-
79
-
80
- def parse_user_message_special(msg: str) -> MessageParam:
81
- # Search for lines starting with `%` and treat them as special commands
82
- parts: list[ImageBlockParam | TextBlockParam] = []
83
- for line in msg.split("\n"):
84
- if line.startswith("%"):
85
- args = line[1:].strip().split(" ")
86
- command = args[0]
87
- assert command == "image"
88
- image_path = " ".join(args[1:])
89
- with open(image_path, "rb") as f:
90
- image_bytes = f.read()
91
- image_b64 = base64.b64encode(image_bytes).decode("utf-8")
92
- image_type = mimetypes.guess_type(image_path)[0]
93
- parts.append(
94
- {
95
- "type": "image",
96
- "source": {
97
- "type": "base64",
98
- "media_type": cast(
99
- 'Literal["image/jpeg", "image/png", "image/gif", "image/webp"]',
100
- image_type or "image/png",
101
- ),
102
- "data": image_b64,
103
- },
104
- }
105
- )
106
- else:
107
- if len(parts) > 0 and parts[-1]["type"] == "text":
108
- parts[-1]["text"] += "\n" + line
109
- else:
110
- parts.append({"type": "text", "text": line})
111
- return {"role": "user", "content": parts}
112
-
113
-
114
- app = Typer(pretty_exceptions_show_locals=False)
115
-
116
-
117
- @app.command()
118
- def loop(
119
- first_message: Optional[str] = None,
120
- limit: Optional[float] = None,
121
- resume: Optional[str] = None,
122
- computer_use: bool = False,
123
- ) -> tuple[str, float]:
124
- load_dotenv()
125
-
126
- session_id = str(uuid.uuid4())[:6]
127
-
128
- history: History = []
129
- waiting_for_assistant = False
130
- memory = None
131
- if resume:
132
- try:
133
- _, memory, _ = load_memory(
134
- resume,
135
- 8000,
136
- lambda x: default_enc.encode(x).ids,
137
- lambda x: default_enc.decode(x),
138
- )
139
- except OSError:
140
- if resume == "latest":
141
- resume_path = sorted(Path(".wcgw").iterdir(), key=os.path.getmtime)[-1]
142
- else:
143
- resume_path = Path(resume)
144
- if not resume_path.exists():
145
- raise FileNotFoundError(f"File {resume} not found")
146
- with resume_path.open() as f:
147
- history = json.load(f)
148
- if len(history) <= 2:
149
- raise ValueError("Invalid history file")
150
- first_message = ""
151
- waiting_for_assistant = history[-1]["role"] != "assistant"
152
-
153
- limit = 1
154
-
155
- tools = [
156
- ToolParam(
157
- input_schema=BashCommand.model_json_schema(),
158
- name="BashCommand",
159
- description="""
160
- - Execute a bash command. This is stateful (beware with subsequent calls).
161
- - Do not use interactive commands like nano. Prefer writing simpler commands.
162
- - Status of the command and the current working directory will always be returned at the end.
163
- - Optionally `exit shell has restarted` is the output, in which case environment resets, you can run fresh commands.
164
- - The first or the last line might be `(...truncated)` if the output is too long.
165
- - Always run `pwd` if you get any file or directory not found error to make sure you're not lost.
166
- - 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.
167
- - Run long running commands in background using screen instead of "&".
168
- - Use longer wait_for_seconds if the command is expected to run for a long time.
169
- - Do not use 'cat' to read files, use ReadFiles tool instead.
170
- """,
171
- ),
172
- ToolParam(
173
- input_schema=BashInteraction.model_json_schema(),
174
- name="BashInteraction",
175
- description="""
176
- - Interact with running program using this tool
177
- - Special keys like arrows, interrupts, enter, etc.
178
- - Send text input to the running program.
179
- - Send send_specials=["Enter"] to recheck status of a running program.
180
- - Only one of send_text, send_specials, send_ascii should be provided.
181
- - This returns within 5 seconds, for heavy programs keep checking status for upto 10 turns before asking user to continue checking again.
182
- - 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"].
183
- - Do not send Ctrl-c before checking for status till 10 minutes or whatever is appropriate for the program to finish.
184
- - Set longer wait_for_seconds when program is expected to run for a long time.
185
- """,
186
- ),
187
- ToolParam(
188
- input_schema=ReadFiles.model_json_schema(),
189
- name="ReadFiles",
190
- description="""
191
- - Read full file content of one or more files.
192
- - Provide absolute file paths only
193
- """,
194
- ),
195
- ToolParam(
196
- input_schema=WriteIfEmpty.model_json_schema(),
197
- name="WriteIfEmpty",
198
- description="""
199
- - Write content to an empty or non-existent file. Provide file path and content. Use this instead of BashCommand for writing new files.
200
- - Provide absolute file path only.
201
- - For editing existing files, use FileEdit instead of this tool.
202
- """,
203
- ),
204
- ToolParam(
205
- input_schema=ReadImage.model_json_schema(),
206
- name="ReadImage",
207
- description="Read an image from the shell.",
208
- ),
209
- ToolParam(
210
- input_schema=ResetShell.model_json_schema(),
211
- name="ResetShell",
212
- 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",
213
- ),
214
- ToolParam(
215
- input_schema=FileEdit.model_json_schema(),
216
- name="FileEdit",
217
- description="""
218
- - Use absolute file path only.
219
- - Use SEARCH/REPLACE blocks to edit the file.
220
- - 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.
221
- """,
222
- ),
223
- ToolParam(
224
- input_schema=ContextSave.model_json_schema(),
225
- name="ContextSave",
226
- description="""
227
- Saves provided description and file contents of all the relevant file paths or globs in a single text file.
228
- - Provide random unqiue id or whatever user provided.
229
- - Leave project path as empty string if no project path
230
- """,
231
- ),
232
- ]
233
-
234
- if computer_use:
235
- tools += [
236
- ToolParam(
237
- input_schema=GetScreenInfo.model_json_schema(),
238
- name="GetScreenInfo",
239
- description="""
240
- - Important: call this first in the conversation before ScreenShot, Mouse, and Keyboard tools.
241
- - Get display information of a linux os running on docker using image "ghcr.io/anthropics/anthropic-quickstarts:computer-use-demo-latest"
242
- - If user hasn't provided docker image id, check using `docker ps` and provide the id.
243
- - If the docker is not running, run using `docker run -d -p 6080:6080 ghcr.io/anthropics/anthropic-quickstarts:computer-use-demo-latest`
244
- - Connects shell to the docker environment.
245
- - Note: once this is called, the shell enters the docker environment. All bash commands will run over there.
246
- """,
247
- ),
248
- ToolParam(
249
- input_schema=ScreenShot.model_json_schema(),
250
- name="ScreenShot",
251
- description="""
252
- - Capture screenshot of the linux os on docker.
253
- - All actions on UI using mouse and keyboard return within 0.5 seconds.
254
- * 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.
255
- * Notice for smallest of the loading icons to check if your action worked.
256
- * After 10 turns of no change, ask user for permission to keep checking.
257
- * If you don't notice even slightest of the change, it's likely you clicked on the wrong place.
258
-
259
- """,
260
- ),
261
- ToolParam(
262
- input_schema=Mouse.model_json_schema(),
263
- name="Mouse",
264
- description="""
265
- - Interact with the linux os on docker using mouse.
266
- - Uses xdotool
267
- - 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.
268
- - The output of this command has the screenshot after doing this action. Use this to verify if the action was successful.
269
- """,
270
- ),
271
- ToolParam(
272
- input_schema=Keyboard.model_json_schema(),
273
- name="Keyboard",
274
- description="""
275
- - Interact with the linux os on docker using keyboard.
276
- - Emulate keyboard input to the screen
277
- - Uses xdootool to send keyboard input, keys like Return, BackSpace, Escape, Page_Up, etc. can be used.
278
- - Do not use it to interact with Bash tool.
279
- - Make sure you've selected a text area or an editable element before sending text.
280
- - The output of this command has the screenshot after doing this action. Use this to verify if the action was successful.
281
- """,
282
- ),
283
- ]
284
-
285
- system = initialize(
286
- os.getcwd(),
287
- [],
288
- resume if (memory and resume) else "",
289
- max_tokens=8000,
290
- mode="wcgw",
291
- )
292
-
293
- with open(os.path.join(os.path.dirname(__file__), "diff-instructions.txt")) as f:
294
- system += f.read()
295
-
296
- if history:
297
- if (
298
- (last_msg := history[-1])["role"] == "user"
299
- and isinstance((content := last_msg["content"]), dict)
300
- and content["type"] == "tool_result"
301
- ):
302
- waiting_for_assistant = True
303
-
304
- client = Anthropic()
305
-
306
- cost: float = 0
307
- input_toks = 0
308
- output_toks = 0
309
- system_console = rich.console.Console(style="blue", highlight=False, markup=False)
310
- error_console = rich.console.Console(style="red", highlight=False, markup=False)
311
- user_console = rich.console.Console(
312
- style="bright_black", highlight=False, markup=False
313
- )
314
- assistant_console = rich.console.Console(
315
- style="white bold", highlight=False, markup=False
316
- )
317
- while True:
318
- if cost > limit:
319
- system_console.print(
320
- f"\nCost limit exceeded. Current cost: {cost}, input tokens: {input_toks}, output tokens: {output_toks}"
321
- )
322
- break
323
-
324
- if not waiting_for_assistant:
325
- if first_message:
326
- msg = first_message
327
- first_message = ""
328
- else:
329
- msg = text_from_editor(user_console)
330
-
331
- history.append(parse_user_message_special(msg))
332
- else:
333
- waiting_for_assistant = False
334
-
335
- cost_, input_toks_ = 0, 0
336
- cost += cost_
337
- input_toks += input_toks_
338
-
339
- stream = client.messages.stream(
340
- model="claude-3-5-sonnet-20241022",
341
- messages=history,
342
- tools=tools,
343
- max_tokens=8096,
344
- system=system,
345
- )
346
-
347
- system_console.print(
348
- "\n---------------------------------------\n# Assistant response",
349
- style="bold",
350
- )
351
- _histories: History = []
352
- full_response: str = ""
353
-
354
- tool_calls = []
355
- tool_results: list[ToolResultBlockParam] = []
356
- try:
357
- with stream as stream_:
358
- for chunk in stream_:
359
- type_ = chunk.type
360
- if type_ in {"message_start", "message_stop"}:
361
- continue
362
- elif type_ == "content_block_start" and hasattr(
363
- chunk, "content_block"
364
- ):
365
- content_block = chunk.content_block
366
- if (
367
- hasattr(content_block, "type")
368
- and content_block.type == "text"
369
- and hasattr(content_block, "text")
370
- ):
371
- chunk_str = content_block.text
372
- assistant_console.print(chunk_str, end="")
373
- full_response += chunk_str
374
- elif content_block.type == "tool_use":
375
- if (
376
- hasattr(content_block, "input")
377
- and hasattr(content_block, "name")
378
- and hasattr(content_block, "id")
379
- ):
380
- assert content_block.input == {}
381
- tool_calls.append(
382
- {
383
- "name": str(content_block.name),
384
- "input": str(""),
385
- "done": False,
386
- "id": str(content_block.id),
387
- }
388
- )
389
- else:
390
- error_console.log(
391
- f"Ignoring unknown content block type {content_block.type}"
392
- )
393
- elif type_ == "content_block_delta" and hasattr(chunk, "delta"):
394
- delta = chunk.delta
395
- if hasattr(delta, "type"):
396
- delta_type = str(delta.type)
397
- if delta_type == "text_delta" and hasattr(delta, "text"):
398
- chunk_str = delta.text
399
- assistant_console.print(chunk_str, end="")
400
- full_response += chunk_str
401
- elif delta_type == "input_json_delta" and hasattr(
402
- delta, "partial_json"
403
- ):
404
- partial_json = delta.partial_json
405
- if isinstance(tool_calls[-1]["input"], str):
406
- tool_calls[-1]["input"] += partial_json
407
- else:
408
- error_console.log(
409
- f"Ignoring unknown content block delta type {delta_type}"
410
- )
411
- else:
412
- raise ValueError("Content block delta has no type")
413
- elif type_ == "content_block_stop":
414
- if tool_calls and not tool_calls[-1]["done"]:
415
- tc = tool_calls[-1]
416
- tool_name = str(tc["name"])
417
- tool_input = str(tc["input"])
418
- tool_id = str(tc["id"])
419
-
420
- tool_parsed = which_tool_name(
421
- tool_name
422
- ).model_validate_json(tool_input)
423
-
424
- system_console.print(
425
- f"\n---------------------------------------\n# Assistant invoked tool: {tool_parsed}"
426
- )
427
-
428
- _histories.append(
429
- {
430
- "role": "assistant",
431
- "content": [
432
- ToolUseBlockParam(
433
- id=tool_id,
434
- name=tool_name,
435
- input=tool_parsed.model_dump(),
436
- type="tool_use",
437
- )
438
- ],
439
- }
440
- )
441
- try:
442
- output_or_dones, _ = get_tool_output(
443
- tool_parsed,
444
- default_enc,
445
- limit - cost,
446
- loop,
447
- max_tokens=8000,
448
- )
449
- except Exception as e:
450
- output_or_dones = [
451
- (f"GOT EXCEPTION while calling tool. Error: {e}")
452
- ]
453
- tb = traceback.format_exc()
454
- error_console.print(str(output_or_dones) + "\n" + tb)
455
-
456
- if any(isinstance(x, DoneFlag) for x in output_or_dones):
457
- return "", cost
458
-
459
- tool_results_content: list[
460
- TextBlockParam | ImageBlockParam
461
- ] = []
462
- for output in output_or_dones:
463
- assert not isinstance(output, DoneFlag)
464
- if isinstance(output, ImageData):
465
- tool_results_content.append(
466
- {
467
- "type": "image",
468
- "source": {
469
- "type": "base64",
470
- "media_type": output.media_type,
471
- "data": output.data,
472
- },
473
- }
474
- )
475
-
476
- else:
477
- tool_results_content.append(
478
- {
479
- "type": "text",
480
- "text": output,
481
- },
482
- )
483
- tool_results.append(
484
- ToolResultBlockParam(
485
- type="tool_result",
486
- tool_use_id=str(tc["id"]),
487
- content=tool_results_content,
488
- )
489
- )
490
- else:
491
- _histories.append(
492
- {
493
- "role": "assistant",
494
- "content": full_response
495
- if full_response.strip()
496
- else "...",
497
- } # Fixes anthropic issue of non empty response only
498
- )
499
-
500
- except KeyboardInterrupt:
501
- waiting_for_assistant = False
502
- input("Interrupted...enter to redo the current turn")
503
- else:
504
- history.extend(_histories)
505
- if tool_results:
506
- history.append({"role": "user", "content": tool_results})
507
- waiting_for_assistant = True
508
- save_history(history, session_id)
509
-
510
- return "Couldn't finish the task", cost
511
-
512
-
513
- if __name__ == "__main__":
514
- app()
wcgw/client/cli.py DELETED
@@ -1,42 +0,0 @@
1
- import importlib
2
- from typing import Optional
3
- from typer import Typer
4
- import typer
5
-
6
- from .openai_client import loop as openai_loop
7
- from .anthropic_client import loop as claude_loop
8
-
9
-
10
- app = Typer(pretty_exceptions_show_locals=False)
11
-
12
-
13
- @app.command()
14
- def loop(
15
- claude: bool = False,
16
- first_message: Optional[str] = None,
17
- limit: Optional[float] = None,
18
- resume: Optional[str] = None,
19
- computer_use: bool = False,
20
- version: bool = typer.Option(False, "--version", "-v"),
21
- ) -> tuple[str, float]:
22
- if version:
23
- version_ = importlib.metadata.version("wcgw")
24
- print(f"wcgw version: {version_}")
25
- exit()
26
- if claude:
27
- return claude_loop(
28
- first_message=first_message,
29
- limit=limit,
30
- resume=resume,
31
- computer_use=computer_use,
32
- )
33
- else:
34
- return openai_loop(
35
- first_message=first_message,
36
- limit=limit,
37
- resume=resume,
38
- )
39
-
40
-
41
- if __name__ == "__main__":
42
- app()