-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_async.py
More file actions
57 lines (42 loc) · 1.38 KB
/
Copy pathlive_async.py
File metadata and controls
57 lines (42 loc) · 1.38 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
import asyncio
import atexit
import sys
from typing import cast
from opencode._async_opencode import AsyncOpendcode
from opencode._models import SessionMessage
from opencode._opencode import _extract_text, _resolve_model
_ai: AsyncOpendcode | None = None
_pid: int | None = None
def _cleanup() -> None:
global _ai, _pid
if _pid is not None:
print(f"\n[cleanup] killing PID {_pid}...", file=sys.stderr)
import subprocess
subprocess.run(
["taskkill", "/F", "/PID", str(_pid)], capture_output=True
)
_pid = None
atexit.register(_cleanup)
async def main() -> None:
global _ai, _pid
_ai = AsyncOpendcode(port=4096)
_ai.start()
_pid = _ai.server.pid
print(f"[server started] PID {_pid}", file=sys.stderr)
print("Async dialog with opencode (Enter — send, Ctrl+C — exit)")
print()
while True:
loop = asyncio.get_running_loop()
try:
prompt = await loop.run_in_executor(None, lambda: input(">>> "))
except (EOFError, KeyboardInterrupt):
break
if not prompt:
continue
session = await _ai.create_session()
msg = await session.prompt(
prompt, model=_resolve_model(model=_ai._model, config=_ai._config)
)
print(_extract_text(cast("SessionMessage", msg)))
if __name__ == "__main__":
asyncio.run(main())