-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (74 loc) · 2.72 KB
/
Copy pathmain.py
File metadata and controls
89 lines (74 loc) · 2.72 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
import argparse
import asyncio
import sys
from pathlib import Path
from qoder_agent_sdk import (
AssistantMessage,
QoderAgentOptions,
QoderSDKClient,
ResultMessage,
StreamEvent,
TextBlock,
access_token_from_env,
)
async def read_prompt() -> str | None:
value = (await asyncio.to_thread(input, "you> ")).strip()
return value if value and value != "/exit" else None
async def receive_turn(client: QoderSDKClient) -> None:
streamed_text = False
completed = False
async for message in client.receive_response():
if isinstance(message, StreamEvent):
delta = message.event.get("delta")
if isinstance(delta, dict) and delta.get("type") == "text_delta":
text = delta.get("text")
if isinstance(text, str):
sys.stdout.write(text)
sys.stdout.flush()
streamed_text = True
elif isinstance(message, AssistantMessage) and not streamed_text:
for block in message.content:
if isinstance(block, TextBlock):
print(block.text, end="", flush=True)
elif isinstance(message, ResultMessage):
if message.subtype != "success":
raise RuntimeError("\n".join(message.errors or [message.subtype]))
completed = True
if not completed:
raise RuntimeError("The turn ended without a success result.")
print()
async def run(workspace: Path, scripted_prompts: list[str]) -> None:
options = QoderAgentOptions(
auth=access_token_from_env(),
cwd=workspace,
model="auto",
tools=["Read", "Glob", "Grep"],
allowed_tools=["Read", "Glob", "Grep"],
include_partial_messages=True,
max_turns=6,
)
async with QoderSDKClient(options=options) as client:
if scripted_prompts:
for prompt in scripted_prompts:
await client.query(prompt)
await receive_turn(client)
return
while True:
interactive_prompt = await read_prompt()
if interactive_prompt is None:
break
await client.query(interactive_prompt)
await receive_turn(client)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--workspace", type=Path, default=Path.cwd())
parser.add_argument("prompts", nargs="*")
return parser.parse_args()
def main() -> None:
args = parse_args()
asyncio.run(run(args.workspace.resolve(), args.prompts))
if __name__ == "__main__":
try:
main()
except (RuntimeError, OSError, EOFError, KeyboardInterrupt) as error:
raise SystemExit(str(error)) from error