wcgw 2.8.10__py3-none-any.whl → 3.0.1__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_cli/cli.py CHANGED
@@ -16,7 +16,6 @@ def loop(
16
16
  first_message: Optional[str] = None,
17
17
  limit: Optional[float] = None,
18
18
  resume: Optional[str] = None,
19
- computer_use: bool = False,
20
19
  version: bool = typer.Option(False, "--version", "-v"),
21
20
  ) -> tuple[str, float]:
22
21
  if version:
@@ -28,7 +27,6 @@ def loop(
28
27
  first_message=first_message,
29
28
  limit=limit,
30
29
  resume=resume,
31
- computer_use=computer_use,
32
30
  )
33
31
  else:
34
32
  return openai_loop(
wcgw_cli/openai_client.py CHANGED
@@ -23,25 +23,18 @@ from openai.types.chat import (
23
23
  from pydantic import BaseModel
24
24
  from typer import Typer
25
25
 
26
+ from wcgw.client.bash_state.bash_state import BashState
26
27
  from wcgw.client.common import CostData, History, Models, discard_input
27
28
  from wcgw.client.memory import load_memory
29
+ from wcgw.client.tool_prompts import TOOL_PROMPTS
28
30
  from wcgw.client.tools import (
29
- DoneFlag,
31
+ Context,
30
32
  ImageData,
31
33
  default_enc,
32
34
  get_tool_output,
33
35
  initialize,
34
36
  which_tool,
35
- )
36
- from wcgw.types_ import (
37
- BashCommand,
38
- BashInteraction,
39
- ContextSave,
40
- FileEdit,
41
- ReadFiles,
42
- ReadImage,
43
- ResetShell,
44
- WriteIfEmpty,
37
+ which_tool_name,
45
38
  )
46
39
 
47
40
  from .openai_utils import get_input_cost, get_output_cost
@@ -114,7 +107,6 @@ def loop(
114
107
  first_message: Optional[str] = None,
115
108
  limit: Optional[float] = None,
116
109
  resume: Optional[str] = None,
117
- computer_use: bool = False,
118
110
  ) -> tuple[str, float]:
119
111
  load_dotenv()
120
112
 
@@ -129,8 +121,8 @@ def loop(
129
121
  _, memory, _ = load_memory(
130
122
  resume,
131
123
  8000,
132
- lambda x: default_enc.encode(x).ids,
133
- lambda x: default_enc.decode(x),
124
+ lambda x: default_enc.encoder(x),
125
+ lambda x: default_enc.decoder(x),
134
126
  )
135
127
  except OSError:
136
128
  if resume == "latest":
@@ -167,89 +159,11 @@ def loop(
167
159
 
168
160
  tools = [
169
161
  openai.pydantic_function_tool(
170
- BashCommand,
171
- description="""
172
- - Execute a bash command. This is stateful (beware with subsequent calls).
173
- - Do not use interactive commands like nano. Prefer writing simpler commands.
174
- - Status of the command and the current working directory will always be returned at the end.
175
- - Optionally `exit shell has restarted` is the output, in which case environment resets, you can run fresh commands.
176
- - The first or the last line might be `(...truncated)` if the output is too long.
177
- - Always run `pwd` if you get any file or directory not found error to make sure you're not lost.
178
- - 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.
179
- - Run long running commands in background using screen instead of "&".
180
- - Do not use 'cat' to read files, use ReadFiles tool instead.
181
- """,
182
- ),
183
- openai.pydantic_function_tool(
184
- BashInteraction,
185
- description="""
186
- - Interact with running program using this tool
187
- - Special keys like arrows, interrupts, enter, etc.
188
- - Send text input to the running program.
189
- - Send send_specials=["Enter"] to recheck status of a running program.
190
- - Only one of send_text, send_specials, send_ascii should be provided.""",
191
- ),
192
- openai.pydantic_function_tool(
193
- ReadFiles,
194
- description="""
195
- - Read full file content of one or more files.
196
- - Provide absolute file paths only
197
- """,
198
- ),
199
- openai.pydantic_function_tool(
200
- WriteIfEmpty,
201
- description="""
202
- - Write content to an empty or non-existent file. Provide file path and content. Use this instead of BashCommand for writing new files.
203
- - Provide absolute file path only.
204
- - For editing existing files, use FileEdit instead of this tool.""",
205
- ),
206
- openai.pydantic_function_tool(
207
- FileEdit,
208
- description="""
209
- - Use absolute file path only.
210
- - Use ONLY SEARCH/REPLACE blocks to edit the file.
211
- - file_edit_using_search_replace_blocks should start with <<<<<<< SEARCH
212
- """,
213
- ),
214
- openai.pydantic_function_tool(
215
- ReadImage, description="Read an image from the shell."
216
- ),
217
- openai.pydantic_function_tool(
218
- ResetShell,
219
- description="Resets the shell. Use only if all interrupts and prompt reset attempts have failed repeatedly.",
220
- ),
221
- openai.pydantic_function_tool(
222
- ContextSave,
223
- description="""
224
-
225
- Saves provided description and file contents of all the relevant file paths or globs in a single text file.
226
- - Provide random unqiue id or whatever user provided.
227
- - Leave project path as empty string if no project path""",
228
- ),
229
- ]
230
-
231
- system = initialize(
232
- os.getcwd(),
233
- [],
234
- resume if (memory and resume) else "",
235
- max_tokens=8000,
236
- mode="wcgw",
237
- )
238
-
239
- with open(
240
- os.path.join(
241
- os.path.dirname(__file__), "..", "wcgw", "client", "diff-instructions.txt"
162
+ which_tool_name(tool.name), description=tool.description
242
163
  )
243
- ) as f:
244
- system += f.read()
245
-
246
- if not history:
247
- history = [{"role": "system", "content": system}]
248
- else:
249
- if history[-1]["role"] == "tool":
250
- waiting_for_assistant = True
251
-
252
- client = OpenAI()
164
+ for tool in TOOL_PROMPTS
165
+ if tool.name != "Initialize"
166
+ ]
253
167
 
254
168
  cost: float = 0
255
169
  input_toks = 0
@@ -262,203 +176,233 @@ Saves provided description and file contents of all the relevant file paths or g
262
176
  assistant_console = rich.console.Console(
263
177
  style="white bold", highlight=False, markup=False
264
178
  )
265
- while True:
266
- if cost > limit:
267
- system_console.print(
268
- f"\nCost limit exceeded. Current cost: {cost}, input tokens: {input_toks}, output tokens: {output_toks}"
269
- )
270
- break
271
179
 
272
- if not waiting_for_assistant:
273
- if first_message:
274
- msg = first_message
275
- first_message = ""
276
- else:
277
- msg = text_from_editor(user_console)
180
+ with BashState(
181
+ system_console, os.getcwd(), None, None, None, None, True, None
182
+ ) as bash_state:
183
+ context = Context(bash_state, system_console)
184
+ system, context = initialize(
185
+ "first_call",
186
+ context,
187
+ os.getcwd(),
188
+ [],
189
+ resume if (memory and resume) else "",
190
+ max_tokens=8000,
191
+ mode="wcgw",
192
+ )
278
193
 
279
- history.append(parse_user_message_special(msg))
194
+ with open(
195
+ os.path.join(
196
+ os.path.dirname(__file__),
197
+ "..",
198
+ "wcgw",
199
+ "client",
200
+ "diff-instructions.txt",
201
+ )
202
+ ) as f:
203
+ system += f.read()
204
+
205
+ if not history:
206
+ history = [{"role": "system", "content": system}]
280
207
  else:
281
- waiting_for_assistant = False
208
+ if history[-1]["role"] == "tool":
209
+ waiting_for_assistant = True
210
+
211
+ client = OpenAI()
212
+
213
+ while True:
214
+ if cost > limit:
215
+ system_console.print(
216
+ f"\nCost limit exceeded. Current cost: {cost}, input tokens: {input_toks}, output tokens: {output_toks}"
217
+ )
218
+ break
219
+
220
+ if not waiting_for_assistant:
221
+ if first_message:
222
+ msg = first_message
223
+ first_message = ""
224
+ else:
225
+ msg = text_from_editor(user_console)
226
+
227
+ history.append(parse_user_message_special(msg))
228
+ else:
229
+ waiting_for_assistant = False
282
230
 
283
- cost_, input_toks_ = get_input_cost(
284
- config.cost_file[config.model], enc, history
285
- )
286
- cost += cost_
287
- input_toks += input_toks_
288
-
289
- stream = client.chat.completions.create(
290
- messages=history,
291
- model=config.model,
292
- stream=True,
293
- tools=tools,
294
- )
231
+ cost_, input_toks_ = get_input_cost(
232
+ config.cost_file[config.model], enc, history
233
+ )
234
+ cost += cost_
235
+ input_toks += input_toks_
236
+
237
+ stream = client.chat.completions.create(
238
+ messages=history,
239
+ model=config.model,
240
+ stream=True,
241
+ tools=tools,
242
+ )
295
243
 
296
- system_console.print(
297
- "\n---------------------------------------\n# Assistant response",
298
- style="bold",
299
- )
300
- tool_call_args_by_id = DefaultDict[str, DefaultDict[int, str]](
301
- lambda: DefaultDict(str)
302
- )
303
- _histories: History = []
304
- item: ChatCompletionMessageParam
305
- full_response: str = ""
306
- image_histories: History = []
307
- try:
308
- for chunk in stream:
309
- if chunk.choices[0].finish_reason == "tool_calls":
310
- assert tool_call_args_by_id
311
- item = {
312
- "role": "assistant",
313
- "content": full_response,
314
- "tool_calls": [
315
- {
316
- "id": tool_call_id + str(toolindex),
317
- "type": "function",
318
- "function": {
319
- "arguments": tool_args,
320
- "name": type(which_tool(tool_args)).__name__,
321
- },
322
- }
323
- for tool_call_id, toolcallargs in tool_call_args_by_id.items()
324
- for toolindex, tool_args in toolcallargs.items()
325
- ],
326
- }
327
- cost_, output_toks_ = get_output_cost(
328
- config.cost_file[config.model], enc, item
329
- )
330
- cost += cost_
331
- system_console.print(
332
- f"\n---------------------------------------\n# Assistant invoked tools: {[which_tool(tool['function']['arguments']) for tool in item['tool_calls']]}"
333
- )
334
- system_console.print(f"\nTotal cost: {config.cost_unit}{cost:.3f}")
335
- output_toks += output_toks_
336
-
337
- _histories.append(item)
338
- for tool_call_id, toolcallargs in tool_call_args_by_id.items():
339
- for toolindex, tool_args in toolcallargs.items():
340
- try:
341
- output_or_dones, cost_ = get_tool_output(
342
- json.loads(tool_args),
343
- enc,
344
- limit - cost,
345
- loop,
346
- max_tokens=8000,
347
- )
348
- output_or_done = output_or_dones[0]
349
- except Exception as e:
350
- output_or_done = (
351
- f"GOT EXCEPTION while calling tool. Error: {e}"
352
- )
353
- tb = traceback.format_exc()
354
- error_console.print(output_or_done + "\n" + tb)
355
- cost_ = 0
356
- cost += cost_
357
- system_console.print(
358
- f"\nTotal cost: {config.cost_unit}{cost:.3f}"
359
- )
360
-
361
- if isinstance(output_or_done, DoneFlag):
362
- system_console.print(
363
- f"\n# Task marked done, with output {output_or_done.task_output}",
364
- )
244
+ system_console.print(
245
+ "\n---------------------------------------\n# Assistant response",
246
+ style="bold",
247
+ )
248
+ tool_call_args_by_id = DefaultDict[str, DefaultDict[int, str]](
249
+ lambda: DefaultDict(str)
250
+ )
251
+ _histories: History = []
252
+ item: ChatCompletionMessageParam
253
+ full_response: str = ""
254
+ image_histories: History = []
255
+ try:
256
+ for chunk in stream:
257
+ if chunk.choices[0].finish_reason == "tool_calls":
258
+ assert tool_call_args_by_id
259
+ item = {
260
+ "role": "assistant",
261
+ "content": full_response,
262
+ "tool_calls": [
263
+ {
264
+ "id": tool_call_id + str(toolindex),
265
+ "type": "function",
266
+ "function": {
267
+ "arguments": tool_args,
268
+ "name": type(which_tool(tool_args)).__name__,
269
+ },
270
+ }
271
+ for tool_call_id, toolcallargs in tool_call_args_by_id.items()
272
+ for toolindex, tool_args in toolcallargs.items()
273
+ ],
274
+ }
275
+ cost_, output_toks_ = get_output_cost(
276
+ config.cost_file[config.model], enc, item
277
+ )
278
+ cost += cost_
279
+ system_console.print(
280
+ f"\n---------------------------------------\n# Assistant invoked tools: {[which_tool(tool['function']['arguments']) for tool in item['tool_calls']]}"
281
+ )
282
+ system_console.print(
283
+ f"\nTotal cost: {config.cost_unit}{cost:.3f}"
284
+ )
285
+ output_toks += output_toks_
286
+
287
+ _histories.append(item)
288
+ for tool_call_id, toolcallargs in tool_call_args_by_id.items():
289
+ for toolindex, tool_args in toolcallargs.items():
290
+ try:
291
+ output_or_dones, cost_ = get_tool_output(
292
+ context,
293
+ json.loads(tool_args),
294
+ enc,
295
+ limit - cost,
296
+ loop,
297
+ max_tokens=8000,
298
+ )
299
+ output_or_done = output_or_dones[0]
300
+ except Exception as e:
301
+ output_or_done = (
302
+ f"GOT EXCEPTION while calling tool. Error: {e}"
303
+ )
304
+ tb = traceback.format_exc()
305
+ error_console.print(output_or_done + "\n" + tb)
306
+ cost_ = 0
307
+ cost += cost_
365
308
  system_console.print(
366
309
  f"\nTotal cost: {config.cost_unit}{cost:.3f}"
367
310
  )
368
- return output_or_done.task_output, cost
369
311
 
370
- output = output_or_done
371
-
372
- if isinstance(output, ImageData):
373
- randomId = petname.Generate(2, "-")
374
- if not image_histories:
375
- image_histories.extend(
376
- [
377
- {
378
- "role": "assistant",
379
- "content": f"Share images with ids: {randomId}",
380
- },
312
+ output = output_or_done
313
+
314
+ if isinstance(output, ImageData):
315
+ randomId = petname.Generate(2, "-")
316
+ if not image_histories:
317
+ image_histories.extend(
318
+ [
319
+ {
320
+ "role": "assistant",
321
+ "content": f"Share images with ids: {randomId}",
322
+ },
323
+ {
324
+ "role": "user",
325
+ "content": [
326
+ {
327
+ "type": "image_url",
328
+ "image_url": {
329
+ "url": output.dataurl,
330
+ "detail": "auto",
331
+ },
332
+ }
333
+ ],
334
+ },
335
+ ]
336
+ )
337
+ else:
338
+ image_histories[0]["content"] += ", " + randomId
339
+ second_content = image_histories[1]["content"]
340
+ assert isinstance(second_content, list)
341
+ second_content.append(
381
342
  {
382
- "role": "user",
383
- "content": [
384
- {
385
- "type": "image_url",
386
- "image_url": {
387
- "url": output.dataurl,
388
- "detail": "auto",
389
- },
390
- }
391
- ],
392
- },
393
- ]
394
- )
343
+ "type": "image_url",
344
+ "image_url": {
345
+ "url": output.dataurl,
346
+ "detail": "auto",
347
+ },
348
+ }
349
+ )
350
+
351
+ item = {
352
+ "role": "tool",
353
+ "content": f"Ask user for image id: {randomId}",
354
+ "tool_call_id": tool_call_id + str(toolindex),
355
+ }
395
356
  else:
396
- image_histories[0]["content"] += ", " + randomId
397
- second_content = image_histories[1]["content"]
398
- assert isinstance(second_content, list)
399
- second_content.append(
400
- {
401
- "type": "image_url",
402
- "image_url": {
403
- "url": output.dataurl,
404
- "detail": "auto",
405
- },
406
- }
407
- )
408
-
409
- item = {
410
- "role": "tool",
411
- "content": f"Ask user for image id: {randomId}",
412
- "tool_call_id": tool_call_id + str(toolindex),
413
- }
414
- else:
415
- item = {
416
- "role": "tool",
417
- "content": str(output),
418
- "tool_call_id": tool_call_id + str(toolindex),
419
- }
420
- cost_, output_toks_ = get_output_cost(
421
- config.cost_file[config.model], enc, item
422
- )
423
- cost += cost_
424
- output_toks += output_toks_
425
-
426
- _histories.append(item)
427
- waiting_for_assistant = True
428
- break
429
- elif chunk.choices[0].finish_reason:
430
- assistant_console.print("")
431
- item = {
432
- "role": "assistant",
433
- "content": full_response,
434
- }
435
- cost_, output_toks_ = get_output_cost(
436
- config.cost_file[config.model], enc, item
437
- )
438
- cost += cost_
439
- output_toks += output_toks_
440
-
441
- system_console.print(f"\nTotal cost: {config.cost_unit}{cost:.3f}")
442
- _histories.append(item)
443
- break
444
-
445
- if chunk.choices[0].delta.tool_calls:
446
- tool_call = chunk.choices[0].delta.tool_calls[0]
447
- if tool_call.function and tool_call.function.arguments:
448
- tool_call_args_by_id[tool_call.id or ""][tool_call.index] += (
449
- tool_call.function.arguments
357
+ item = {
358
+ "role": "tool",
359
+ "content": str(output),
360
+ "tool_call_id": tool_call_id + str(toolindex),
361
+ }
362
+ cost_, output_toks_ = get_output_cost(
363
+ config.cost_file[config.model], enc, item
364
+ )
365
+ cost += cost_
366
+ output_toks += output_toks_
367
+
368
+ _histories.append(item)
369
+ waiting_for_assistant = True
370
+ break
371
+ elif chunk.choices[0].finish_reason:
372
+ assistant_console.print("")
373
+ item = {
374
+ "role": "assistant",
375
+ "content": full_response,
376
+ }
377
+ cost_, output_toks_ = get_output_cost(
378
+ config.cost_file[config.model], enc, item
450
379
  )
380
+ cost += cost_
381
+ output_toks += output_toks_
451
382
 
452
- chunk_str = chunk.choices[0].delta.content or ""
453
- assistant_console.print(chunk_str, end="")
454
- full_response += chunk_str
455
- except KeyboardInterrupt:
456
- waiting_for_assistant = False
457
- input("Interrupted...enter to redo the current turn")
458
- else:
459
- history.extend(_histories)
460
- history.extend(image_histories)
461
- save_history(history, session_id)
383
+ system_console.print(
384
+ f"\nTotal cost: {config.cost_unit}{cost:.3f}"
385
+ )
386
+ _histories.append(item)
387
+ break
388
+
389
+ if chunk.choices[0].delta.tool_calls:
390
+ tool_call = chunk.choices[0].delta.tool_calls[0]
391
+ if tool_call.function and tool_call.function.arguments:
392
+ tool_call_args_by_id[tool_call.id or ""][
393
+ tool_call.index
394
+ ] += tool_call.function.arguments
395
+
396
+ chunk_str = chunk.choices[0].delta.content or ""
397
+ assistant_console.print(chunk_str, end="")
398
+ full_response += chunk_str
399
+ except KeyboardInterrupt:
400
+ waiting_for_assistant = False
401
+ input("Interrupted...enter to redo the current turn")
402
+ else:
403
+ history.extend(_histories)
404
+ history.extend(image_histories)
405
+ save_history(history, session_id)
462
406
 
463
407
  return "Couldn't finish the task", cost
464
408