wcgw 2.8.3__py3-none-any.whl → 2.8.5__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 -2
- wcgw/client/mcp_server/server.py +15 -3
- wcgw/client/modes.py +3 -3
- wcgw/client/tools.py +5 -8
- {wcgw-2.8.3.dist-info → wcgw-2.8.5.dist-info}/METADATA +2 -2
- {wcgw-2.8.3.dist-info → wcgw-2.8.5.dist-info}/RECORD +9 -14
- {wcgw-2.8.3.dist-info → wcgw-2.8.5.dist-info}/entry_points.txt +1 -1
- wcgw/client/__main__.py +0 -3
- wcgw/client/anthropic_client.py +0 -514
- wcgw/client/cli.py +0 -42
- wcgw/client/openai_client.py +0 -462
- wcgw/client/openai_utils.py +0 -78
- {wcgw-2.8.3.dist-info → wcgw-2.8.5.dist-info}/WHEEL +0 -0
- {wcgw-2.8.3.dist-info → wcgw-2.8.5.dist-info}/licenses/LICENSE +0 -0
wcgw/__init__.py
CHANGED
wcgw/client/mcp_server/server.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import importlib
|
|
2
2
|
import json
|
|
3
|
+
import logging
|
|
3
4
|
import os
|
|
4
5
|
from typing import Any
|
|
5
6
|
|
|
6
|
-
from pydantic import AnyUrl, ValidationError
|
|
7
|
-
|
|
8
7
|
import mcp_wcgw.server.stdio
|
|
9
8
|
import mcp_wcgw.types as types
|
|
10
9
|
from mcp_wcgw.server import NotificationOptions, Server
|
|
11
10
|
from mcp_wcgw.server.models import InitializationOptions
|
|
12
11
|
from mcp_wcgw.types import Tool as ToolParam
|
|
12
|
+
from pydantic import AnyUrl, ValidationError
|
|
13
13
|
|
|
14
14
|
from ...types_ import (
|
|
15
15
|
BashCommand,
|
|
@@ -35,6 +35,18 @@ COMPUTER_USE_ON_DOCKER_ENABLED = False
|
|
|
35
35
|
|
|
36
36
|
server = Server("wcgw")
|
|
37
37
|
|
|
38
|
+
# Log only time stamp
|
|
39
|
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s: %(message)s")
|
|
40
|
+
logger = logging.getLogger("wcgw")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Console:
|
|
44
|
+
def print(self, msg: str, *args: Any, **kwargs: Any) -> None:
|
|
45
|
+
logger.info(msg)
|
|
46
|
+
|
|
47
|
+
def log(self, msg: str, *args: Any, **kwargs: Any) -> None:
|
|
48
|
+
logger.info(msg)
|
|
49
|
+
|
|
38
50
|
|
|
39
51
|
@server.list_resources() # type: ignore
|
|
40
52
|
async def handle_list_resources() -> list[types.Resource]:
|
|
@@ -301,7 +313,7 @@ async def main(computer_use: bool) -> None:
|
|
|
301
313
|
tools.TIMEOUT = SLEEP_TIME_MAX_S
|
|
302
314
|
tools.TIMEOUT_WHILE_OUTPUT = 55
|
|
303
315
|
tools.OUTPUT_WAIT_PATIENCE = 5
|
|
304
|
-
tools.console =
|
|
316
|
+
tools.console = Console()
|
|
305
317
|
|
|
306
318
|
if computer_use:
|
|
307
319
|
COMPUTER_USE_ON_DOCKER_ENABLED = True
|
wcgw/client/modes.py
CHANGED
|
@@ -51,7 +51,7 @@ def code_writer_prompt(
|
|
|
51
51
|
allowed_commands: Literal["all"] | list[str],
|
|
52
52
|
) -> str:
|
|
53
53
|
base = """
|
|
54
|
-
You
|
|
54
|
+
You are now running in "code_writer" mode.
|
|
55
55
|
"""
|
|
56
56
|
|
|
57
57
|
path_prompt = """
|
|
@@ -134,7 +134,7 @@ Additional instructions:
|
|
|
134
134
|
|
|
135
135
|
|
|
136
136
|
"""
|
|
137
|
-
ARCHITECT_PROMPT = """You
|
|
137
|
+
ARCHITECT_PROMPT = """You are now running in "architect" mode. This means
|
|
138
138
|
- You are not allowed to edit or update any file. You are not allowed to create any file.
|
|
139
139
|
- You are not allowed to run any commands that may change disk, system configuration, packages or environment. Only read-only commands are allowed.
|
|
140
140
|
- Only run commands that allows you to explore the repository, understand the system or read anything of relevance.
|
|
@@ -203,7 +203,7 @@ Format the `description` field using Markdown with the following sections.
|
|
|
203
203
|
- "# Objective" section containing project and task objective.
|
|
204
204
|
- "# All user instructions" section should be provided containing all instructions user shared in the conversation.
|
|
205
205
|
- "# Current status of the task" should be provided containing only what is already achieved, not what's remaining.
|
|
206
|
-
- "#
|
|
206
|
+
- "# Pending issues with snippets" section containing snippets of pending errors, traceback, file snippets, commands, etc. But no comments or solutions.
|
|
207
207
|
- Be very verbose in the all issues with snippets section providing as much error context as possible.
|
|
208
208
|
- "# Build and development instructions" section containing instructions to build or run project or run tests, or envrionment related information. Only include what's known. Leave empty if unknown.
|
|
209
209
|
- Any other relevant sections following the above.
|
wcgw/client/tools.py
CHANGED
|
@@ -20,6 +20,7 @@ from typing import (
|
|
|
20
20
|
Literal,
|
|
21
21
|
Optional,
|
|
22
22
|
ParamSpec,
|
|
23
|
+
Protocol,
|
|
23
24
|
Type,
|
|
24
25
|
TypeVar,
|
|
25
26
|
)
|
|
@@ -73,17 +74,13 @@ from .repo_ops.repo_context import get_repo_context
|
|
|
73
74
|
from .sys_utils import command_run
|
|
74
75
|
|
|
75
76
|
|
|
76
|
-
class
|
|
77
|
-
def print(self, *args, **kwargs
|
|
78
|
-
pass
|
|
77
|
+
class Console(Protocol):
|
|
78
|
+
def print(self, msg: str, *args: Any, **kwargs: Any) -> None: ...
|
|
79
79
|
|
|
80
|
-
def log(self, *args, **kwargs
|
|
81
|
-
pass
|
|
80
|
+
def log(self, msg: str, *args: Any, **kwargs: Any) -> None: ...
|
|
82
81
|
|
|
83
82
|
|
|
84
|
-
console:
|
|
85
|
-
style="magenta", highlight=False, markup=False
|
|
86
|
-
)
|
|
83
|
+
console: Console = rich.console.Console(style="magenta", highlight=False, markup=False)
|
|
87
84
|
|
|
88
85
|
TIMEOUT = 5
|
|
89
86
|
TIMEOUT_WHILE_OUTPUT = 20
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: wcgw
|
|
3
|
-
Version: 2.8.
|
|
3
|
+
Version: 2.8.5
|
|
4
4
|
Summary: Shell and coding agent on claude and chatgpt
|
|
5
5
|
Project-URL: Homepage, https://github.com/rusiaaman/wcgw
|
|
6
6
|
Author-email: Aman Rusia <gapypi@arcfu.com>
|
|
@@ -39,6 +39,7 @@ Description-Content-Type: text/markdown
|
|
|
39
39
|
[](https://github.com/rusiaaman/wcgw/actions/workflows/python-types.yml)
|
|
40
40
|
[](https://github.com/rusiaaman/wcgw/actions/workflows/python-publish.yml)
|
|
41
41
|
[](https://codecov.io/gh/rusiaaman/wcgw)
|
|
42
|
+
[](https://smithery.ai/server/wcgw)
|
|
42
43
|
|
|
43
44
|
## Updates
|
|
44
45
|
- [15 Jan 2025] Modes introduced: architect, code-writer, and all powerful wcgw mode.
|
|
@@ -128,7 +129,6 @@ _If there's an error in setting up_
|
|
|
128
129
|
- Debug the mcp server using `npx @modelcontextprotocol/inspector@0.1.7 uv tool run --from wcgw@latest --python 3.12 wcgw_mcp`
|
|
129
130
|
|
|
130
131
|
### Alternative configuration using smithery (npx required)
|
|
131
|
-
[](https://smithery.ai/server/wcgw)
|
|
132
132
|
|
|
133
133
|
You need to first install uv using homebrew. `brew install uv`
|
|
134
134
|
|
|
@@ -1,23 +1,18 @@
|
|
|
1
|
-
wcgw/__init__.py,sha256=
|
|
1
|
+
wcgw/__init__.py,sha256=sQZHC74HPFyE9XqWZDCVmgdBXVK1xkHrMtMSLBZ5BOY,90
|
|
2
2
|
wcgw/types_.py,sha256=D3518a2azSKeW3D-ACYWJwsaqo7Oj-8BRRR2IhCUtNU,3414
|
|
3
3
|
wcgw/client/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
wcgw/client/__main__.py,sha256=wcCrL4PjG51r5wVKqJhcoJPTLfHW0wNbD31DrUN0MWI,28
|
|
5
|
-
wcgw/client/anthropic_client.py,sha256=TLPQCc96cxLGGMHg1sYqX3_IxTm_-WQ1e-XPQD6i6xU,20958
|
|
6
|
-
wcgw/client/cli.py,sha256=-z0kpDAW3mzfQrQeZfaVJhBCAQY3HXnt9GdgQ8s-u0Y,1003
|
|
7
4
|
wcgw/client/common.py,sha256=OCH7Tx64jojz3M3iONUrGMadE07W21DiZs5sOxWX1Qc,1456
|
|
8
5
|
wcgw/client/computer_use.py,sha256=35NKAlMrxwD0TBlMMRnbCwz4g8TBRGOlcy-cmS-yJ_A,15247
|
|
9
6
|
wcgw/client/diff-instructions.txt,sha256=tmJ9Fu9XdO_72lYXQQNY9RZyx91bjxrXJf9d_KBz57k,1611
|
|
10
7
|
wcgw/client/memory.py,sha256=8LdYsOhvCOoC1kfvDr85kNy07WnhPMvE6B2FRM2w85Y,2902
|
|
11
|
-
wcgw/client/modes.py,sha256=
|
|
12
|
-
wcgw/client/openai_client.py,sha256=WvCnTij5QOPhe0VCdsk5Qo1Ob6yBdxFSvsCmnF0Jozw,17858
|
|
13
|
-
wcgw/client/openai_utils.py,sha256=KfMB1-p2zDiA7pPWwAVarochf7-qeL1UMgtlDV9DtKA,2662
|
|
8
|
+
wcgw/client/modes.py,sha256=FkDJIgjKrlJEufLq3abWfqV25BdF2pH-HnoHafy9LrA,10484
|
|
14
9
|
wcgw/client/sys_utils.py,sha256=GajPntKhaTUMn6EOmopENWZNR2G_BJyuVbuot0x6veI,1376
|
|
15
|
-
wcgw/client/tools.py,sha256=
|
|
10
|
+
wcgw/client/tools.py,sha256=lQhWn-syIX3zLLDxNNK5GSowbU2NV3G7ytRKmj-LFlc,51248
|
|
16
11
|
wcgw/client/file_ops/diff_edit.py,sha256=o0ucArVwn5p6QTDgYsjLfMy4TJXxUG3qcppFBNF3bbQ,16751
|
|
17
12
|
wcgw/client/file_ops/search_replace.py,sha256=89ieDC9fTsIKPDx7CJwnwpX32dRdSlMKoBtKVXc7VWI,3971
|
|
18
13
|
wcgw/client/mcp_server/Readme.md,sha256=I8N4dHkTUVGNQ63BQkBMBhCCBTgqGOSF_pUR6iOEiUk,2495
|
|
19
14
|
wcgw/client/mcp_server/__init__.py,sha256=hyPPwO9cabAJsOMWhKyat9yl7OlSmIobaoAZKHu3DMc,381
|
|
20
|
-
wcgw/client/mcp_server/server.py,sha256=
|
|
15
|
+
wcgw/client/mcp_server/server.py,sha256=CBOS_DSDcBf5VlHQMJJbDL-wnn_DAPDvxX3dO8yodRo,13144
|
|
21
16
|
wcgw/client/repo_ops/display_tree.py,sha256=5FD4hfMkM2cIZnXlu7WfJswJLthj0SkuHlkGH6dpWQU,4632
|
|
22
17
|
wcgw/client/repo_ops/path_prob.py,sha256=SWf0CDn37rtlsYRQ51ufSxay-heaQoVIhr1alB9tZ4M,2144
|
|
23
18
|
wcgw/client/repo_ops/paths_model.vocab,sha256=M1pXycYDQehMXtpp-qAgU7rtzeBbCOiJo4qcYFY0kqk,315087
|
|
@@ -47,8 +42,8 @@ mcp_wcgw/shared/memory.py,sha256=dBsOghxHz8-tycdSVo9kSujbsC8xb_tYsGmuJobuZnw,281
|
|
|
47
42
|
mcp_wcgw/shared/progress.py,sha256=ymxOsb8XO5Mhlop7fRfdbmvPodANj7oq6O4dD0iUcnw,1048
|
|
48
43
|
mcp_wcgw/shared/session.py,sha256=e44a0LQOW8gwdLs9_DE9oDsxqW2U8mXG3d5KT95bn5o,10393
|
|
49
44
|
mcp_wcgw/shared/version.py,sha256=d2LZii-mgsPIxpshjkXnOTUmk98i0DT4ff8VpA_kAvE,111
|
|
50
|
-
wcgw-2.8.
|
|
51
|
-
wcgw-2.8.
|
|
52
|
-
wcgw-2.8.
|
|
53
|
-
wcgw-2.8.
|
|
54
|
-
wcgw-2.8.
|
|
45
|
+
wcgw-2.8.5.dist-info/METADATA,sha256=N2LJBm7hqi1ZFPiNIVbkzuVqXRuB1XC_2pSSxq0cuE0,11944
|
|
46
|
+
wcgw-2.8.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
47
|
+
wcgw-2.8.5.dist-info/entry_points.txt,sha256=vd3tj1_Kzfp55LscJ8-6WFMM5hm9cWTfNGFCrWBnH3Q,124
|
|
48
|
+
wcgw-2.8.5.dist-info/licenses/LICENSE,sha256=BvY8xqjOfc3X2qZpGpX3MZEmF-4Dp0LqgKBbT6L_8oI,11142
|
|
49
|
+
wcgw-2.8.5.dist-info/RECORD,,
|
wcgw/client/__main__.py
DELETED
wcgw/client/anthropic_client.py
DELETED
|
@@ -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()
|
wcgw/client/openai_client.py
DELETED
|
@@ -1,462 +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 DefaultDict, Optional, cast
|
|
11
|
-
|
|
12
|
-
import openai
|
|
13
|
-
import petname # type: ignore[import-untyped]
|
|
14
|
-
import rich
|
|
15
|
-
import tokenizers # type: ignore[import-untyped]
|
|
16
|
-
from dotenv import load_dotenv
|
|
17
|
-
from openai import OpenAI
|
|
18
|
-
from openai.types.chat import (
|
|
19
|
-
ChatCompletionContentPartParam,
|
|
20
|
-
ChatCompletionMessageParam,
|
|
21
|
-
ChatCompletionUserMessageParam,
|
|
22
|
-
)
|
|
23
|
-
from pydantic import BaseModel
|
|
24
|
-
from typer import Typer
|
|
25
|
-
|
|
26
|
-
from ..types_ import (
|
|
27
|
-
BashCommand,
|
|
28
|
-
BashInteraction,
|
|
29
|
-
ContextSave,
|
|
30
|
-
FileEdit,
|
|
31
|
-
ReadFiles,
|
|
32
|
-
ReadImage,
|
|
33
|
-
ResetShell,
|
|
34
|
-
WriteIfEmpty,
|
|
35
|
-
)
|
|
36
|
-
from .common import CostData, History, Models, discard_input
|
|
37
|
-
from .memory import load_memory
|
|
38
|
-
from .openai_utils import get_input_cost, get_output_cost
|
|
39
|
-
from .tools import (
|
|
40
|
-
DoneFlag,
|
|
41
|
-
ImageData,
|
|
42
|
-
default_enc,
|
|
43
|
-
get_tool_output,
|
|
44
|
-
initialize,
|
|
45
|
-
which_tool,
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
class Config(BaseModel):
|
|
50
|
-
model: Models
|
|
51
|
-
cost_limit: float
|
|
52
|
-
cost_file: dict[Models, CostData]
|
|
53
|
-
cost_unit: str = "$"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
def text_from_editor(console: rich.console.Console) -> str:
|
|
57
|
-
# First consume all the input till now
|
|
58
|
-
discard_input()
|
|
59
|
-
console.print("\n---------------------------------------\n# User message")
|
|
60
|
-
data = input()
|
|
61
|
-
if data:
|
|
62
|
-
return data
|
|
63
|
-
editor = os.environ.get("EDITOR", "vim")
|
|
64
|
-
with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
|
|
65
|
-
subprocess.run([editor, tf.name], check=True)
|
|
66
|
-
with open(tf.name, "r") as f:
|
|
67
|
-
data = f.read()
|
|
68
|
-
console.print(data)
|
|
69
|
-
return data
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def save_history(history: History, session_id: str) -> None:
|
|
73
|
-
myid = str(history[1]["content"]).replace("/", "_").replace(" ", "_").lower()[:60]
|
|
74
|
-
myid += "_" + session_id
|
|
75
|
-
myid = myid + ".json"
|
|
76
|
-
|
|
77
|
-
mypath = Path(".wcgw") / myid
|
|
78
|
-
mypath.parent.mkdir(parents=True, exist_ok=True)
|
|
79
|
-
with open(mypath, "w") as f:
|
|
80
|
-
json.dump(history, f, indent=3)
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
def parse_user_message_special(msg: str) -> ChatCompletionUserMessageParam:
|
|
84
|
-
# Search for lines starting with `%` and treat them as special commands
|
|
85
|
-
parts: list[ChatCompletionContentPartParam] = []
|
|
86
|
-
for line in msg.split("\n"):
|
|
87
|
-
if line.startswith("%"):
|
|
88
|
-
args = line[1:].strip().split(" ")
|
|
89
|
-
command = args[0]
|
|
90
|
-
assert command == "image"
|
|
91
|
-
image_path = " ".join(args[1:])
|
|
92
|
-
with open(image_path, "rb") as f:
|
|
93
|
-
image_bytes = f.read()
|
|
94
|
-
image_b64 = base64.b64encode(image_bytes).decode("utf-8")
|
|
95
|
-
image_type = mimetypes.guess_type(image_path)[0]
|
|
96
|
-
dataurl = f"data:{image_type};base64,{image_b64}"
|
|
97
|
-
parts.append(
|
|
98
|
-
{"type": "image_url", "image_url": {"url": dataurl, "detail": "auto"}}
|
|
99
|
-
)
|
|
100
|
-
else:
|
|
101
|
-
if len(parts) > 0 and parts[-1]["type"] == "text":
|
|
102
|
-
parts[-1]["text"] += "\n" + line
|
|
103
|
-
else:
|
|
104
|
-
parts.append({"type": "text", "text": line})
|
|
105
|
-
return {"role": "user", "content": parts}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
app = Typer(pretty_exceptions_show_locals=False)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
@app.command()
|
|
112
|
-
def loop(
|
|
113
|
-
first_message: Optional[str] = None,
|
|
114
|
-
limit: Optional[float] = None,
|
|
115
|
-
resume: Optional[str] = None,
|
|
116
|
-
computer_use: bool = False,
|
|
117
|
-
) -> tuple[str, float]:
|
|
118
|
-
load_dotenv()
|
|
119
|
-
|
|
120
|
-
session_id = str(uuid.uuid4())[:6]
|
|
121
|
-
|
|
122
|
-
history: History = []
|
|
123
|
-
waiting_for_assistant = False
|
|
124
|
-
|
|
125
|
-
memory = None
|
|
126
|
-
if resume:
|
|
127
|
-
try:
|
|
128
|
-
_, memory, _ = load_memory(
|
|
129
|
-
resume,
|
|
130
|
-
8000,
|
|
131
|
-
lambda x: default_enc.encode(x).ids,
|
|
132
|
-
lambda x: default_enc.decode(x),
|
|
133
|
-
)
|
|
134
|
-
except OSError:
|
|
135
|
-
if resume == "latest":
|
|
136
|
-
resume_path = sorted(Path(".wcgw").iterdir(), key=os.path.getmtime)[-1]
|
|
137
|
-
else:
|
|
138
|
-
resume_path = Path(resume)
|
|
139
|
-
if not resume_path.exists():
|
|
140
|
-
raise FileNotFoundError(f"File {resume} not found")
|
|
141
|
-
with resume_path.open() as f:
|
|
142
|
-
history = json.load(f)
|
|
143
|
-
if len(history) <= 2:
|
|
144
|
-
raise ValueError("Invalid history file")
|
|
145
|
-
first_message = ""
|
|
146
|
-
waiting_for_assistant = history[-1]["role"] != "assistant"
|
|
147
|
-
|
|
148
|
-
my_dir = os.path.dirname(__file__)
|
|
149
|
-
|
|
150
|
-
config = Config(
|
|
151
|
-
model=cast(Models, os.getenv("OPENAI_MODEL", "gpt-4o-2024-08-06").lower()),
|
|
152
|
-
cost_limit=0.1,
|
|
153
|
-
cost_unit="$",
|
|
154
|
-
cost_file={
|
|
155
|
-
"gpt-4o-2024-08-06": CostData(
|
|
156
|
-
cost_per_1m_input_tokens=5, cost_per_1m_output_tokens=15
|
|
157
|
-
),
|
|
158
|
-
},
|
|
159
|
-
)
|
|
160
|
-
|
|
161
|
-
if limit is not None:
|
|
162
|
-
config.cost_limit = limit
|
|
163
|
-
limit = config.cost_limit
|
|
164
|
-
|
|
165
|
-
enc = tokenizers.Tokenizer.from_pretrained("Xenova/gpt-4o")
|
|
166
|
-
|
|
167
|
-
tools = [
|
|
168
|
-
openai.pydantic_function_tool(
|
|
169
|
-
BashCommand,
|
|
170
|
-
description="""
|
|
171
|
-
- Execute a bash command. This is stateful (beware with subsequent calls).
|
|
172
|
-
- Do not use interactive commands like nano. Prefer writing simpler commands.
|
|
173
|
-
- Status of the command and the current working directory will always be returned at the end.
|
|
174
|
-
- Optionally `exit shell has restarted` is the output, in which case environment resets, you can run fresh commands.
|
|
175
|
-
- The first or the last line might be `(...truncated)` if the output is too long.
|
|
176
|
-
- Always run `pwd` if you get any file or directory not found error to make sure you're not lost.
|
|
177
|
-
- 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.
|
|
178
|
-
- Run long running commands in background using screen instead of "&".
|
|
179
|
-
- Do not use 'cat' to read files, use ReadFiles tool instead.
|
|
180
|
-
""",
|
|
181
|
-
),
|
|
182
|
-
openai.pydantic_function_tool(
|
|
183
|
-
BashInteraction,
|
|
184
|
-
description="""
|
|
185
|
-
- Interact with running program using this tool
|
|
186
|
-
- Special keys like arrows, interrupts, enter, etc.
|
|
187
|
-
- Send text input to the running program.
|
|
188
|
-
- Send send_specials=["Enter"] to recheck status of a running program.
|
|
189
|
-
- Only one of send_text, send_specials, send_ascii should be provided.""",
|
|
190
|
-
),
|
|
191
|
-
openai.pydantic_function_tool(
|
|
192
|
-
ReadFiles,
|
|
193
|
-
description="""
|
|
194
|
-
- Read full file content of one or more files.
|
|
195
|
-
- Provide absolute file paths only
|
|
196
|
-
""",
|
|
197
|
-
),
|
|
198
|
-
openai.pydantic_function_tool(
|
|
199
|
-
WriteIfEmpty,
|
|
200
|
-
description="""
|
|
201
|
-
- Write content to an empty or non-existent file. Provide file path and content. Use this instead of BashCommand for writing new files.
|
|
202
|
-
- Provide absolute file path only.
|
|
203
|
-
- For editing existing files, use FileEdit instead of this tool.""",
|
|
204
|
-
),
|
|
205
|
-
openai.pydantic_function_tool(
|
|
206
|
-
FileEdit,
|
|
207
|
-
description="""
|
|
208
|
-
- Use absolute file path only.
|
|
209
|
-
- Use ONLY SEARCH/REPLACE blocks to edit the file.
|
|
210
|
-
- file_edit_using_search_replace_blocks should start with <<<<<<< SEARCH
|
|
211
|
-
""",
|
|
212
|
-
),
|
|
213
|
-
openai.pydantic_function_tool(
|
|
214
|
-
ReadImage, description="Read an image from the shell."
|
|
215
|
-
),
|
|
216
|
-
openai.pydantic_function_tool(
|
|
217
|
-
ResetShell,
|
|
218
|
-
description="Resets the shell. Use only if all interrupts and prompt reset attempts have failed repeatedly.",
|
|
219
|
-
),
|
|
220
|
-
openai.pydantic_function_tool(
|
|
221
|
-
ContextSave,
|
|
222
|
-
description="""
|
|
223
|
-
|
|
224
|
-
Saves provided description and file contents of all the relevant file paths or globs in a single text file.
|
|
225
|
-
- Provide random unqiue id or whatever user provided.
|
|
226
|
-
- Leave project path as empty string if no project path""",
|
|
227
|
-
),
|
|
228
|
-
]
|
|
229
|
-
|
|
230
|
-
system = initialize(
|
|
231
|
-
os.getcwd(),
|
|
232
|
-
[],
|
|
233
|
-
resume if (memory and resume) else "",
|
|
234
|
-
max_tokens=8000,
|
|
235
|
-
mode="wcgw",
|
|
236
|
-
)
|
|
237
|
-
|
|
238
|
-
with open(os.path.join(os.path.dirname(__file__), "diff-instructions.txt")) as f:
|
|
239
|
-
system += f.read()
|
|
240
|
-
|
|
241
|
-
if not history:
|
|
242
|
-
history = [{"role": "system", "content": system}]
|
|
243
|
-
else:
|
|
244
|
-
if history[-1]["role"] == "tool":
|
|
245
|
-
waiting_for_assistant = True
|
|
246
|
-
|
|
247
|
-
client = OpenAI()
|
|
248
|
-
|
|
249
|
-
cost: float = 0
|
|
250
|
-
input_toks = 0
|
|
251
|
-
output_toks = 0
|
|
252
|
-
system_console = rich.console.Console(style="blue", highlight=False, markup=False)
|
|
253
|
-
error_console = rich.console.Console(style="red", highlight=False, markup=False)
|
|
254
|
-
user_console = rich.console.Console(
|
|
255
|
-
style="bright_black", highlight=False, markup=False
|
|
256
|
-
)
|
|
257
|
-
assistant_console = rich.console.Console(
|
|
258
|
-
style="white bold", highlight=False, markup=False
|
|
259
|
-
)
|
|
260
|
-
while True:
|
|
261
|
-
if cost > limit:
|
|
262
|
-
system_console.print(
|
|
263
|
-
f"\nCost limit exceeded. Current cost: {cost}, input tokens: {input_toks}, output tokens: {output_toks}"
|
|
264
|
-
)
|
|
265
|
-
break
|
|
266
|
-
|
|
267
|
-
if not waiting_for_assistant:
|
|
268
|
-
if first_message:
|
|
269
|
-
msg = first_message
|
|
270
|
-
first_message = ""
|
|
271
|
-
else:
|
|
272
|
-
msg = text_from_editor(user_console)
|
|
273
|
-
|
|
274
|
-
history.append(parse_user_message_special(msg))
|
|
275
|
-
else:
|
|
276
|
-
waiting_for_assistant = False
|
|
277
|
-
|
|
278
|
-
cost_, input_toks_ = get_input_cost(
|
|
279
|
-
config.cost_file[config.model], enc, history
|
|
280
|
-
)
|
|
281
|
-
cost += cost_
|
|
282
|
-
input_toks += input_toks_
|
|
283
|
-
|
|
284
|
-
stream = client.chat.completions.create(
|
|
285
|
-
messages=history,
|
|
286
|
-
model=config.model,
|
|
287
|
-
stream=True,
|
|
288
|
-
tools=tools,
|
|
289
|
-
)
|
|
290
|
-
|
|
291
|
-
system_console.print(
|
|
292
|
-
"\n---------------------------------------\n# Assistant response",
|
|
293
|
-
style="bold",
|
|
294
|
-
)
|
|
295
|
-
tool_call_args_by_id = DefaultDict[str, DefaultDict[int, str]](
|
|
296
|
-
lambda: DefaultDict(str)
|
|
297
|
-
)
|
|
298
|
-
_histories: History = []
|
|
299
|
-
item: ChatCompletionMessageParam
|
|
300
|
-
full_response: str = ""
|
|
301
|
-
image_histories: History = []
|
|
302
|
-
try:
|
|
303
|
-
for chunk in stream:
|
|
304
|
-
if chunk.choices[0].finish_reason == "tool_calls":
|
|
305
|
-
assert tool_call_args_by_id
|
|
306
|
-
item = {
|
|
307
|
-
"role": "assistant",
|
|
308
|
-
"content": full_response,
|
|
309
|
-
"tool_calls": [
|
|
310
|
-
{
|
|
311
|
-
"id": tool_call_id + str(toolindex),
|
|
312
|
-
"type": "function",
|
|
313
|
-
"function": {
|
|
314
|
-
"arguments": tool_args,
|
|
315
|
-
"name": type(which_tool(tool_args)).__name__,
|
|
316
|
-
},
|
|
317
|
-
}
|
|
318
|
-
for tool_call_id, toolcallargs in tool_call_args_by_id.items()
|
|
319
|
-
for toolindex, tool_args in toolcallargs.items()
|
|
320
|
-
],
|
|
321
|
-
}
|
|
322
|
-
cost_, output_toks_ = get_output_cost(
|
|
323
|
-
config.cost_file[config.model], enc, item
|
|
324
|
-
)
|
|
325
|
-
cost += cost_
|
|
326
|
-
system_console.print(
|
|
327
|
-
f"\n---------------------------------------\n# Assistant invoked tools: {[which_tool(tool['function']['arguments']) for tool in item['tool_calls']]}"
|
|
328
|
-
)
|
|
329
|
-
system_console.print(f"\nTotal cost: {config.cost_unit}{cost:.3f}")
|
|
330
|
-
output_toks += output_toks_
|
|
331
|
-
|
|
332
|
-
_histories.append(item)
|
|
333
|
-
for tool_call_id, toolcallargs in tool_call_args_by_id.items():
|
|
334
|
-
for toolindex, tool_args in toolcallargs.items():
|
|
335
|
-
try:
|
|
336
|
-
output_or_dones, cost_ = get_tool_output(
|
|
337
|
-
json.loads(tool_args),
|
|
338
|
-
enc,
|
|
339
|
-
limit - cost,
|
|
340
|
-
loop,
|
|
341
|
-
max_tokens=8000,
|
|
342
|
-
)
|
|
343
|
-
output_or_done = output_or_dones[0]
|
|
344
|
-
except Exception as e:
|
|
345
|
-
output_or_done = (
|
|
346
|
-
f"GOT EXCEPTION while calling tool. Error: {e}"
|
|
347
|
-
)
|
|
348
|
-
tb = traceback.format_exc()
|
|
349
|
-
error_console.print(output_or_done + "\n" + tb)
|
|
350
|
-
cost_ = 0
|
|
351
|
-
cost += cost_
|
|
352
|
-
system_console.print(
|
|
353
|
-
f"\nTotal cost: {config.cost_unit}{cost:.3f}"
|
|
354
|
-
)
|
|
355
|
-
|
|
356
|
-
if isinstance(output_or_done, DoneFlag):
|
|
357
|
-
system_console.print(
|
|
358
|
-
f"\n# Task marked done, with output {output_or_done.task_output}",
|
|
359
|
-
)
|
|
360
|
-
system_console.print(
|
|
361
|
-
f"\nTotal cost: {config.cost_unit}{cost:.3f}"
|
|
362
|
-
)
|
|
363
|
-
return output_or_done.task_output, cost
|
|
364
|
-
|
|
365
|
-
output = output_or_done
|
|
366
|
-
|
|
367
|
-
if isinstance(output, ImageData):
|
|
368
|
-
randomId = petname.Generate(2, "-")
|
|
369
|
-
if not image_histories:
|
|
370
|
-
image_histories.extend(
|
|
371
|
-
[
|
|
372
|
-
{
|
|
373
|
-
"role": "assistant",
|
|
374
|
-
"content": f"Share images with ids: {randomId}",
|
|
375
|
-
},
|
|
376
|
-
{
|
|
377
|
-
"role": "user",
|
|
378
|
-
"content": [
|
|
379
|
-
{
|
|
380
|
-
"type": "image_url",
|
|
381
|
-
"image_url": {
|
|
382
|
-
"url": output.dataurl,
|
|
383
|
-
"detail": "auto",
|
|
384
|
-
},
|
|
385
|
-
}
|
|
386
|
-
],
|
|
387
|
-
},
|
|
388
|
-
]
|
|
389
|
-
)
|
|
390
|
-
else:
|
|
391
|
-
image_histories[0]["content"] += ", " + randomId
|
|
392
|
-
second_content = image_histories[1]["content"]
|
|
393
|
-
assert isinstance(second_content, list)
|
|
394
|
-
second_content.append(
|
|
395
|
-
{
|
|
396
|
-
"type": "image_url",
|
|
397
|
-
"image_url": {
|
|
398
|
-
"url": output.dataurl,
|
|
399
|
-
"detail": "auto",
|
|
400
|
-
},
|
|
401
|
-
}
|
|
402
|
-
)
|
|
403
|
-
|
|
404
|
-
item = {
|
|
405
|
-
"role": "tool",
|
|
406
|
-
"content": f"Ask user for image id: {randomId}",
|
|
407
|
-
"tool_call_id": tool_call_id + str(toolindex),
|
|
408
|
-
}
|
|
409
|
-
else:
|
|
410
|
-
item = {
|
|
411
|
-
"role": "tool",
|
|
412
|
-
"content": str(output),
|
|
413
|
-
"tool_call_id": tool_call_id + str(toolindex),
|
|
414
|
-
}
|
|
415
|
-
cost_, output_toks_ = get_output_cost(
|
|
416
|
-
config.cost_file[config.model], enc, item
|
|
417
|
-
)
|
|
418
|
-
cost += cost_
|
|
419
|
-
output_toks += output_toks_
|
|
420
|
-
|
|
421
|
-
_histories.append(item)
|
|
422
|
-
waiting_for_assistant = True
|
|
423
|
-
break
|
|
424
|
-
elif chunk.choices[0].finish_reason:
|
|
425
|
-
assistant_console.print("")
|
|
426
|
-
item = {
|
|
427
|
-
"role": "assistant",
|
|
428
|
-
"content": full_response,
|
|
429
|
-
}
|
|
430
|
-
cost_, output_toks_ = get_output_cost(
|
|
431
|
-
config.cost_file[config.model], enc, item
|
|
432
|
-
)
|
|
433
|
-
cost += cost_
|
|
434
|
-
output_toks += output_toks_
|
|
435
|
-
|
|
436
|
-
system_console.print(f"\nTotal cost: {config.cost_unit}{cost:.3f}")
|
|
437
|
-
_histories.append(item)
|
|
438
|
-
break
|
|
439
|
-
|
|
440
|
-
if chunk.choices[0].delta.tool_calls:
|
|
441
|
-
tool_call = chunk.choices[0].delta.tool_calls[0]
|
|
442
|
-
if tool_call.function and tool_call.function.arguments:
|
|
443
|
-
tool_call_args_by_id[tool_call.id or ""][tool_call.index] += (
|
|
444
|
-
tool_call.function.arguments
|
|
445
|
-
)
|
|
446
|
-
|
|
447
|
-
chunk_str = chunk.choices[0].delta.content or ""
|
|
448
|
-
assistant_console.print(chunk_str, end="")
|
|
449
|
-
full_response += chunk_str
|
|
450
|
-
except KeyboardInterrupt:
|
|
451
|
-
waiting_for_assistant = False
|
|
452
|
-
input("Interrupted...enter to redo the current turn")
|
|
453
|
-
else:
|
|
454
|
-
history.extend(_histories)
|
|
455
|
-
history.extend(image_histories)
|
|
456
|
-
save_history(history, session_id)
|
|
457
|
-
|
|
458
|
-
return "Couldn't finish the task", cost
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
if __name__ == "__main__":
|
|
462
|
-
app()
|
wcgw/client/openai_utils.py
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import json
|
|
2
|
-
from pathlib import Path
|
|
3
|
-
import select
|
|
4
|
-
import sys
|
|
5
|
-
import termios
|
|
6
|
-
import traceback
|
|
7
|
-
import tty
|
|
8
|
-
from typing import Callable, DefaultDict, Literal, Optional, cast
|
|
9
|
-
import openai
|
|
10
|
-
from openai import OpenAI
|
|
11
|
-
from openai.types.chat import (
|
|
12
|
-
ChatCompletionMessageParam,
|
|
13
|
-
ChatCompletionAssistantMessageParam,
|
|
14
|
-
ChatCompletionMessage,
|
|
15
|
-
ParsedChatCompletionMessage,
|
|
16
|
-
)
|
|
17
|
-
import rich
|
|
18
|
-
from tokenizers import Tokenizer # type: ignore[import-untyped]
|
|
19
|
-
from typer import Typer
|
|
20
|
-
import uuid
|
|
21
|
-
|
|
22
|
-
from .common import CostData, History
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def get_input_cost(
|
|
26
|
-
cost_map: CostData, enc: Tokenizer, history: History
|
|
27
|
-
) -> tuple[float, int]:
|
|
28
|
-
input_tokens = 0
|
|
29
|
-
for msg in history:
|
|
30
|
-
content = msg["content"]
|
|
31
|
-
refusal = msg.get("refusal")
|
|
32
|
-
if isinstance(content, list):
|
|
33
|
-
for part in content:
|
|
34
|
-
if "text" in part:
|
|
35
|
-
input_tokens += len(enc.encode(part["text"]))
|
|
36
|
-
elif content is None:
|
|
37
|
-
if refusal is None:
|
|
38
|
-
raise ValueError("Expected content or refusal to be present")
|
|
39
|
-
input_tokens += len(enc.encode(str(refusal)))
|
|
40
|
-
elif not isinstance(content, str):
|
|
41
|
-
raise ValueError(f"Expected content to be string, got {type(content)}")
|
|
42
|
-
else:
|
|
43
|
-
input_tokens += len(enc.encode(content))
|
|
44
|
-
cost = input_tokens * cost_map.cost_per_1m_input_tokens / 1_000_000
|
|
45
|
-
return cost, input_tokens
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def get_output_cost(
|
|
49
|
-
cost_map: CostData,
|
|
50
|
-
enc: Tokenizer,
|
|
51
|
-
item: ChatCompletionMessage | ChatCompletionMessageParam,
|
|
52
|
-
) -> tuple[float, int]:
|
|
53
|
-
if isinstance(item, ChatCompletionMessage):
|
|
54
|
-
content = item.content
|
|
55
|
-
if not isinstance(content, str):
|
|
56
|
-
raise ValueError(f"Expected content to be string, got {type(content)}")
|
|
57
|
-
else:
|
|
58
|
-
if not isinstance(item["content"], str):
|
|
59
|
-
raise ValueError(
|
|
60
|
-
f"Expected content to be string, got {type(item['content'])}"
|
|
61
|
-
)
|
|
62
|
-
content = item["content"]
|
|
63
|
-
if item["role"] == "tool":
|
|
64
|
-
return 0, 0
|
|
65
|
-
output_tokens = len(enc.encode(content))
|
|
66
|
-
|
|
67
|
-
if "tool_calls" in item:
|
|
68
|
-
item = cast(ChatCompletionAssistantMessageParam, item)
|
|
69
|
-
toolcalls = item["tool_calls"]
|
|
70
|
-
for tool_call in toolcalls or []:
|
|
71
|
-
output_tokens += len(enc.encode(tool_call["function"]["arguments"]))
|
|
72
|
-
elif isinstance(item, ParsedChatCompletionMessage):
|
|
73
|
-
if item.tool_calls:
|
|
74
|
-
for tool_callf in item.tool_calls:
|
|
75
|
-
output_tokens += len(enc.encode(tool_callf.function.arguments))
|
|
76
|
-
|
|
77
|
-
cost = output_tokens * cost_map.cost_per_1m_output_tokens / 1_000_000
|
|
78
|
-
return cost, output_tokens
|
|
File without changes
|
|
File without changes
|