forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
68 lines (56 loc) · 2.05 KB
/
Copy pathchat.py
File metadata and controls
68 lines (56 loc) · 2.05 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
import asyncio
import os
from copilot import CopilotClient, SubprocessConfig
from copilot.generated.session_events import (
AssistantMessageData,
AssistantReasoningData,
ToolExecutionStartData,
)
from copilot.session import PermissionHandler
nr_license_key = os.environ["NEW_RELIC_LICENSE_KEY"]
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://otlp.nr-data.net:4318"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"api-key={nr_license_key}"
os.environ["OTEL_EXPORTER_OTLP_PROTOCOL"] = "http/protobuf"
os.environ["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] = "true"
COPILOT_CLI_PATH=os.environ.get("COPILOT_CLI_PATH", "copilot")
BLUE = "\033[34m"
RESET = "\033[0m"
async def main():
client = CopilotClient(SubprocessConfig(
telemetry={"exporter_type": "otlp-http"},
cli_path=COPILOT_CLI_PATH,
session_idle_timeout_seconds=300 # 5 minutes
))
await client.start()
session = await client.create_session(on_permission_request=PermissionHandler.approve_all)
def on_event(event):
output = None
match event.data:
case AssistantReasoningData() as data:
output = f"[reasoning: {data.content}]"
case ToolExecutionStartData() as data:
output = f"[tool: {data.tool_name}]"
if output:
print(f"{BLUE}{output}{RESET}")
session.on(on_event)
print("Chat with Copilot (Ctrl+C to exit)\n")
try:
while True:
user_input = input("You: ").strip()
if not user_input:
continue
print()
reply = await session.send_and_wait(user_input, timeout=2400)
assistant_output = None
if reply:
match reply.data:
case AssistantMessageData() as data:
assistant_output = data.content
print(f"\nAssistant: {assistant_output}\n")
finally:
await client.stop()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nBye!")