-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_streaming.py
More file actions
43 lines (33 loc) · 946 Bytes
/
Copy pathlive_streaming.py
File metadata and controls
43 lines (33 loc) · 946 Bytes
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
import atexit
from typing import cast
from opencode import Opencode, Session, _opencode
_state = _opencode._opencode_state
def _cleanup() -> None:
if _state:
ai = _state.get("ai")
if ai:
ai.close()
_state.clear()
atexit.register(_cleanup)
print("Streaming dialog with opencode (Enter — send, Ctrl+C — exit)")
print()
while True:
try:
prompt = input(">>> ")
except (EOFError, KeyboardInterrupt):
break
if not prompt:
continue
# Start server and session on first call, reuse afterwards
ai = _state.get("ai")
if not ai:
ai = Opencode(port=4096)
ai.start()
_state["ai"] = ai
session = ai.create_session()
_state["session"] = session
else:
session = cast(Session, _state.get("session"))
for chunk in ai.ask_stream(prompt, session=session):
print(chunk, end="", flush=True)
print()