@@ -16,41 +16,36 @@ You need to handle various error conditions like connection failures, timeouts,
1616# # Basic try-except
1717
1818` ` ` python
19- from copilot import CopilotClient
19+ import asyncio
20+ from copilot import CopilotClient, SessionConfig, MessageOptions
2021
21- client = CopilotClient ()
22+ async def main ():
23+ client = CopilotClient ()
2224
23- try:
24- client.start ()
25- session = client.create_session(model=" gpt-5" )
25+ try:
26+ await client.start ()
27+ session = await client.create_session(SessionConfig( model=" gpt-5" ) )
2628
27- response = None
28- def handle_message(event):
29- nonlocal response
30- if event[" type" ] == " assistant.message" :
31- response = event[" data" ][" content" ]
29+ response = await session.send_and_wait(MessageOptions(prompt=" Hello!" ))
3230
33- session.on(handle_message)
34- session.send(prompt=" Hello!" )
35- session.wait_for_idle ()
31+ if response:
32+ print(response.data.content)
3633
37- if response:
38- print(response)
34+ await session.destroy ()
35+ except Exception as e:
36+ print(f" Error: {e}" )
37+ finally:
38+ await client.stop ()
3939
40- session.destroy ()
41- except Exception as e:
42- print(f" Error: {e}" )
43- finally:
44- client.stop ()
40+ if __name__ == " __main__" :
41+ asyncio.run(main ())
4542` ` `
4643
4744# # Handling specific error types
4845
4946` ` ` python
50- import subprocess
51-
5247try:
53- client.start ()
48+ await client.start ()
5449except FileNotFoundError:
5550 print(" Copilot CLI not found. Please install it first." )
5651except ConnectionError:
@@ -62,31 +57,14 @@ except Exception as e:
6257# # Timeout handling
6358
6459` ` ` python
65- import signal
66- from contextlib import contextmanager
67-
68- @contextmanager
69- def timeout(seconds):
70- def timeout_handler(signum, frame):
71- raise TimeoutError(" Request timed out" )
72-
73- old_handler = signal.signal(signal.SIGALRM, timeout_handler)
74- signal.alarm(seconds)
75- try:
76- yield
77- finally:
78- signal.alarm(0)
79- signal.signal(signal.SIGALRM, old_handler)
80-
81- session = client.create_session(model=" gpt-5" )
60+ session = await client.create_session(SessionConfig(model=" gpt-5" ))
8261
8362try:
84- session.send(prompt=" Complex question..." )
85-
86- # Wait with timeout (30 seconds)
87- with timeout(30):
88- session.wait_for_idle ()
89-
63+ # send_and_wait accepts an optional timeout in seconds
64+ response = await session.send_and_wait(
65+ MessageOptions(prompt=" Complex question..." ),
66+ timeout=30.0
67+ )
9068 print(" Response received" )
9169except TimeoutError:
9270 print(" Request timed out" )
@@ -95,21 +73,15 @@ except TimeoutError:
9573# # Aborting a request
9674
9775` ` ` python
98- import threading
99-
100- session = client.create_session(model=" gpt-5" )
76+ session = await client.create_session(SessionConfig(model=" gpt-5" ))
10177
102- # Start a request
103- session.send(prompt=" Write a very long story..." )
78+ # Start a request (non-blocking send)
79+ await session.send(MessageOptions( prompt=" Write a very long story..." ) )
10480
10581# Abort it after some condition
106- def abort_later ():
107- import time
108- time.sleep(5)
109- session.abort ()
110- print(" Request aborted" )
111-
112- threading.Thread(target=abort_later).start()
82+ await asyncio.sleep(5)
83+ await session.abort ()
84+ print(" Request aborted" )
11385` ` `
11486
11587# # Graceful shutdown
@@ -120,31 +92,19 @@ import sys
12092
12193def signal_handler(sig, frame):
12294 print(" \nShutting down..." )
123- errors = client.stop ()
124- if errors:
125- print(f" Cleanup errors: {errors}" )
95+ try:
96+ loop = asyncio.get_running_loop ()
97+ loop.create_task(client.stop ())
98+ except RuntimeError:
99+ asyncio.run(client.stop ())
126100 sys.exit(0)
127101
128102signal.signal(signal.SIGINT, signal_handler)
129103` ` `
130104
131- # # Context manager for automatic cleanup
132-
133- ` ` ` python
134- from copilot import CopilotClient
135-
136- with CopilotClient () as client:
137- client.start ()
138- session = client.create_session(model=" gpt-5" )
139-
140- # ... do work ...
141-
142- # client.stop() is automatically called when exiting context
143- ` ` `
144-
145105# # Best practices
146106
147- 1. ** Always clean up** : Use try-finally or context managers to ensure ` stop()` is called
107+ 1. ** Always clean up** : Use try-finally to ensure ` await client. stop ()` is called
1481082. ** Handle connection errors** : The CLI might not be installed or running
149- 3. ** Set appropriate timeouts** : Long-running requests should have timeouts
109+ 3. ** Set appropriate timeouts** : Use the ` timeout ` parameter on ` send_and_wait() `
1501104. ** Log errors** : Capture error details for debugging
0 commit comments