Skip to content

Commit a801a33

Browse files
authored
simplify example with interrupt (#127)
This PR simplifies the example with interrupt. There is a flag interrupt_sent that isn't required since the execution loop will terminate after interrupt with the following message: `{"content": [{"type": "text", "text": "[Request interrupted by user]"}]}`
1 parent f794e17 commit a801a33

File tree

1 file changed

+2
-12
lines changed

1 file changed

+2
-12
lines changed

examples/streaming_mode.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,19 @@ async def example_with_interrupt():
144144

145145
# Create a background task to consume messages
146146
messages_received = []
147-
interrupt_sent = False
148147

149148
async def consume_messages():
150149
"""Consume messages in the background to enable interrupt processing."""
151-
async for message in client.receive_messages():
150+
async for message in client.receive_response():
152151
messages_received.append(message)
153-
if isinstance(message, AssistantMessage):
154-
for block in message.content:
155-
if isinstance(block, TextBlock):
156-
# Print first few numbers
157-
print(f"Claude: {block.text[:50]}...")
158-
elif isinstance(message, ResultMessage):
159-
display_message(message)
160-
if interrupt_sent:
161-
break
152+
display_message(message)
162153

163154
# Start consuming messages in the background
164155
consume_task = asyncio.create_task(consume_messages())
165156

166157
# Wait 2 seconds then send interrupt
167158
await asyncio.sleep(2)
168159
print("\n[After 2 seconds, sending interrupt...]")
169-
interrupt_sent = True
170160
await client.interrupt()
171161

172162
# Wait for the consume task to finish processing the interrupt

0 commit comments

Comments
 (0)