forked from metauto-ai/NeuralComputer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer_use.py
More file actions
204 lines (171 loc) Β· 6.35 KB
/
Copy pathcomputer_use.py
File metadata and controls
204 lines (171 loc) Β· 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
"""Run a single computer-use session with recording."""
import argparse
import asyncio
import contextlib
import os
import sys
import time
from pathlib import Path
RUNTIME_DIR = Path(__file__).resolve().parent
GUI_DIR = RUNTIME_DIR.parent
for search_path in (GUI_DIR, RUNTIME_DIR):
if str(search_path) not in sys.path:
sys.path.insert(0, str(search_path))
from anthropic.types.beta import BetaContentBlockParam, BetaMessageParam, BetaTextBlockParam
from computer_use_agent.action_recorder import (
get_recorder,
save_tool_actions,
start_tool_recording,
)
from computer_use_agent.loop import APIProvider, sampling_loop
from computer_use_agent.tools import ToolResult
from record_agent_actions import AgentActionRecorder
def output_callback(content: BetaContentBlockParam) -> None:
"""Handle Claude's output."""
if content["type"] == "text":
print(f"π€ Claude: {content['text']}")
elif content["type"] == "tool_use":
print(f"π§ Using tool: {content['name']}")
def tool_output_callback(result: ToolResult, _tool_id: str) -> None:
"""Handle tool execution results."""
if result.error:
print(f"β Tool error: {result.error}")
else:
print(f"β
Tool completed")
def api_response_callback(_request, _response, error) -> None:
"""Handle API response for debugging."""
if error:
print(f"β οΈ API Error: {error}")
async def computer_use(
api_key: str,
*,
instruction: str,
model: str,
fps: int,
max_tokens: int,
session_name: str | None = None,
allow_prompt: bool = True,
) -> bool:
"""Run a single instruction with screen and tool recording."""
print("π Computer Control Mode - Single Execution with Action Recording")
print("=" * 50)
session_name = session_name or f"agent_session_{int(time.time())}"
recorder = AgentActionRecorder(save_dir="", save_name=session_name, fps=fps)
messages: list[BetaMessageParam] = []
try:
if not instruction:
if not allow_prompt:
print("β No instruction provided")
return False
instruction = input("\n㪠Enter your instruction: ").strip()
if not instruction:
print("β No instruction provided")
return False
print(f"\nπ€ You: {instruction}")
print("-" * 50)
print("π¬ Starting action recording...")
recorder.start_session()
screen_start_time = getattr(recorder, "start_time", None)
try:
start_tool_recording(session_name)
tool_recorder = get_recorder()
if tool_recorder and screen_start_time is not None:
tool_recorder.sync_with_screen_recorder(screen_start_time)
if hasattr(recorder, "get_video_time"):
tool_recorder.set_video_time_provider(recorder.get_video_time)
print("β
Tool recorder synced with screen recorder")
else:
print("β οΈ Tool recorder not available; action CSV/JSON may be missing")
except Exception as e:
print(f"β οΈ Failed to initialize tool recorder: {e}")
messages.append({
"role": "user",
"content": [BetaTextBlockParam(type="text", text=instruction)]
})
try:
async def screen_recording_task():
while recorder.session_active:
recorder._capture_frame()
await asyncio.sleep(1 / max(1, recorder.fps))
screen_task = asyncio.create_task(screen_recording_task())
messages = await sampling_loop(
model=model,
provider=APIProvider.ANTHROPIC,
system_prompt_suffix="",
messages=messages,
output_callback=output_callback,
tool_output_callback=tool_output_callback,
api_response_callback=api_response_callback,
api_key=api_key,
tool_version="computer_use_20250124",
max_tokens=max_tokens,
)
screen_task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await screen_task
except Exception as e:
print(f"β Error during execution: {e}")
return False
finally:
try:
save_tool_actions(session_name)
except Exception as e:
print(f"Warning: Failed to save tool actions: {e}")
print("π¬ Stopping action recording...")
recorder.stop_recording()
print("-" * 50)
print("β
Execution completed")
return True
except KeyboardInterrupt:
print("\nπ Goodbye!")
if recorder.session_active:
recorder.stop_recording()
return False
except EOFError:
print("\nπ Goodbye!")
if recorder.session_active:
recorder.stop_recording()
return False
def main():
"""Main function."""
parser = argparse.ArgumentParser(description="Run a single computer-use instruction with recording.")
parser.add_argument(
"-i",
"--instruction",
default="",
help="Instruction text (if empty, prompt interactively).",
)
parser.add_argument(
"--model",
default=os.getenv("ANTHROPIC_MODEL", "claude-sonnet-4-20250514"),
help="Anthropic model name.",
)
parser.add_argument("--fps", type=int, default=15, help="Recording FPS.")
parser.add_argument("--max-tokens", type=int, default=4096, help="Max tokens per model call.")
parser.add_argument(
"--session-name",
default="",
help="Optional session name for output files.",
)
args = parser.parse_args()
api_key = os.getenv("ANTHROPIC_API_KEY")
if not api_key:
api_key = input("π Enter your Anthropic API key: ").strip()
if not api_key:
print("β API key is required")
sys.exit(1)
success = asyncio.run(
computer_use(
api_key,
instruction=args.instruction,
model=args.model,
fps=args.fps,
max_tokens=args.max_tokens,
session_name=args.session_name or None,
allow_prompt=True,
)
)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()