-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_chat.py
More file actions
116 lines (96 loc) · 3.97 KB
/
interactive_chat.py
File metadata and controls
116 lines (96 loc) · 3.97 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
#!/usr/bin/env python3
"""
Interactive chat with multi-turn memory and session introspection.
SDK features shown:
- Multi-turn conversation in a single session (context preserved automatically)
- Streaming via session.on() event handlers
- session.get_messages() to retrieve conversation history
- Slash-commands implemented in Python to show hybrid local/AI workflows
"""
import asyncio
from copilot import CopilotClient
HELP_TEXT = """\
Commands:
/history - Show conversation turns so far
/model - Show the model used for this session
/clear - End session and start a fresh one
/help - Show this help message
exit - Quit
"""
async def main():
"""Run an interactive multi-turn chat loop."""
client = CopilotClient()
await client.start()
model = "gpt-5-mini"
turn_count = 0
done = asyncio.Event()
def on_event(event):
if event.type.value == "assistant.message_delta":
print(event.data.delta_content, end="", flush=True)
elif event.type.value == "session.idle":
done.set()
try:
session = await client.create_session({"model": model, "streaming": True})
session.on(on_event)
print("Copilot Interactive Chat")
print(f"Model: {model} | Type /help for commands\n")
while True:
try:
user_input = input("You: ").strip()
except (EOFError, KeyboardInterrupt):
print("\nGoodbye!")
break
if not user_input:
continue
# ── Local slash-commands ──────────────────────────
if user_input.lower() in ("exit", "quit"):
print("Goodbye!")
break
if user_input == "/help":
print(HELP_TEXT)
continue
if user_input == "/model":
print(f" Model: {model}\n")
continue
if user_input == "/history":
try:
events = await session.get_messages()
if not events:
print(" (no messages yet)\n")
else:
shown = 0
for ev in events:
ev_type = getattr(getattr(ev, "type", None), "value", None)
if ev_type not in ("user.message", "assistant.message"):
continue
data = getattr(ev, "data", None)
role = getattr(data, "role", None) or ("user" if ev_type == "user.message" else "assistant")
content = getattr(data, "content", "") or ""
preview = (content[:120] + "...") if len(content) > 120 else content
print(f" [{role}] {preview}")
shown += 1
if shown == 0:
print(" (no user/assistant messages yet)\n")
print()
except Exception as e:
print(f" Could not retrieve history: {e}\n")
continue
if user_input == "/clear":
await session.destroy()
session = await client.create_session({"model": model, "streaming": True})
session.on(on_event)
turn_count = 0
print(" Session cleared.\n")
continue
# ── Send to Copilot ───────────────────────────────
turn_count += 1
print("Copilot: ", end="", flush=True)
done.clear()
await session.send({"prompt": user_input})
await done.wait()
print(f"\n (turn {turn_count})\n")
await session.destroy()
finally:
await client.stop()
if __name__ == "__main__":
asyncio.run(main())