forked from trpc-group/trpc-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_agent.py
More file actions
304 lines (237 loc) · 10 KB
/
Copy pathrun_agent.py
File metadata and controls
304 lines (237 loc) · 10 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
"""
LangGraph Calculator Agent with Cancellation Demo
This example demonstrates the agent cancellation feature for LangGraphAgent,
showing two realistic scenarios:
1. Cancel during LLM streaming (using sleep)
2. Cancel during tool execution (using event synchronization)
Each scenario contains 2 queries:
- First query: Ask to perform calculation/analysis
- Second query: Ask "what happened?" to see if session state is maintained
"""
import asyncio
import uuid
from typing import Awaitable
from typing import Callable
from typing import Optional
from dotenv import load_dotenv
from trpc_agent_sdk.events import AgentCancelledEvent
from trpc_agent_sdk.runners import Runner
from trpc_agent_sdk.sessions import InMemorySessionService
from trpc_agent_sdk.types import Content
from trpc_agent_sdk.types import Part
load_dotenv()
async def run_agent(
runner: Runner,
user_id: str,
session_id: str,
query: str,
tool_call_callback: Optional[Callable[[], Awaitable[None]]] = None,
event_count_callback: Optional[Callable[[int], Awaitable[None]]] = None,
) -> None:
"""Run agent with a single query and handle events.
Args:
runner: The runner instance
user_id: User identifier
session_id: Session identifier
query: User query text
tool_call_callback: Optional async callback triggered when tool call is detected
event_count_callback: Optional async callback triggered for each event with count
"""
user_content = Content(parts=[Part.from_text(text=query)])
print("🤖 Assistant: ", end="", flush=True)
event_count = 0
try:
async for event in runner.run_async(user_id=user_id, session_id=session_id, new_message=user_content):
# Increment event count and trigger callback if provided
event_count += 1
if event_count_callback:
await event_count_callback(event_count)
# Check for cancellation using AgentCancelledEvent
if isinstance(event, AgentCancelledEvent):
print(f"\n❌ Run was cancelled: {event.error_message}")
break
if not event.content or not event.content.parts:
continue
# Print streaming text
if event.partial:
for part in event.content.parts:
if part.text:
print(part.text, end="", flush=True)
continue
# Print tool calls and responses
for part in event.content.parts:
if part.thought:
continue
if part.function_call:
print(f"\n🔧 [Invoke Tool: {part.function_call.name}({part.function_call.args})]")
if tool_call_callback:
await tool_call_callback()
elif part.function_response:
print(f"📊 [Tool Result: {part.function_response.response}]")
# # Print final text response
# for part in event.content.parts:
# if part.text and not event.partial:
# print(part.text, end="", flush=True)
except Exception as e:
print(f"\n⚠️ Error: {e}")
print() # New line after response
def create_runner(app_name: str, session_service: InMemorySessionService) -> Runner:
"""Create a new runner instance.
Args:
app_name: Application name
session_service: Session service instance
Returns:
A new Runner instance
"""
from agent.agent import root_agent
return Runner(app_name=app_name, agent=root_agent, session_service=session_service)
async def scenario_1_cancel_during_streaming() -> None:
"""Scenario 1: Cancel while LangGraph agent is streaming response.
This scenario uses asyncio.Event to trigger cancellation after receiving
the first 10 events during streaming.
Each query creates a new runner instance to avoid state issues.
Queries:
1. Ask for introduction (will be cancelled after 10 events)
2. Ask "what happened?" to verify session state
"""
print("📋 Scenario 1: Cancel During LLM Streaming (LangGraph)")
print("-" * 80)
# Common settings for this scenario
app_name = "langgraph_calculator_cancel_demo"
user_id = "demo_user"
session_id = str(uuid.uuid4())
# Create session service (shared across queries)
session_service = InMemorySessionService()
await session_service.create_session(
app_name=app_name,
user_id=user_id,
session_id=session_id,
state={},
)
# Query 1: Ask for introduction (will be cancelled)
query1 = "Please introduce yourself and explain what you can do in detail."
print(f"🆔 Session ID: {session_id[:8]}...")
print(f"📝 User Query 1: {query1}")
print()
# Event to signal when we've received 10 events
event_threshold_reached = asyncio.Event()
# Create callback for event counting
async def on_event_count(count: int) -> None:
if count == 10:
print(f"\n⏳ [Received {count} events, triggering cancellation...]")
event_threshold_reached.set()
# Create new runner for query 1
runner1 = create_runner(app_name, session_service)
# Create background task to run agent
async def run_query1() -> None:
await run_agent(runner1, user_id, session_id, query1, event_count_callback=on_event_count)
# Start agent in background
task = asyncio.create_task(run_query1())
# Wait for 10 events to be received
print("⏳ Waiting for first 10 events...")
await event_threshold_reached.wait()
# Cancel the run after receiving 10 events
runner2 = create_runner(app_name, session_service)
print("\n⏸️ Requesting cancellation after 10 events...")
success = await runner2.cancel_run_async(user_id=user_id, session_id=session_id)
print(f"✓ Cancellation requested: {success}")
# Wait for task to complete
await task
print()
print("💡 Result: The partial response was saved to session with cancellation message")
print()
# Query 2: Ask "what happened?" to verify session state
query2 = "what happened?"
print(f"📝 User Query 2: {query2}")
print()
# Create new runner for query 2
runner2 = create_runner(app_name, session_service)
await run_agent(runner2, user_id, session_id, query2)
print("💡 Result: Agent can still respond with session context maintained")
print("-" * 80)
print()
async def scenario_2_cancel_during_tool_execution() -> None:
"""Scenario 2: Cancel while LangGraph agent is executing tools.
This scenario uses asyncio.Event to synchronize cancellation exactly when
tool execution starts. Each query creates a new runner instance to
avoid state issues.
Queries:
1. Ask for calculation (will be cancelled during tool execution)
2. Ask "what happened?" to verify session state
"""
print("📋 Scenario 2: Cancel During Tool Execution (LangGraph)")
print("-" * 80)
# Common settings for this scenario
app_name = "langgraph_calculator_cancel_demo"
user_id = "demo_user"
session_id = str(uuid.uuid4())
# Create session service (shared across queries)
session_service = InMemorySessionService()
await session_service.create_session(
app_name=app_name,
user_id=user_id,
session_id=session_id,
state={},
)
# Query 1: Ask for calculation (will be cancelled)
query1 = "Please calculate 123 multiply 456 and then analyze sales data with sample size 1000."
print(f"🆔 Session ID: {session_id[:8]}...")
print(f"📝 User Query 1: {query1}")
print()
# Event to signal when function_call is received
tool_call_detected = asyncio.Event()
# Create callback for tool call detection
async def on_tool_call() -> None:
print("⏳ [Tool call detected...]")
tool_call_detected.set()
# Create new runner for query 1
runner1 = create_runner(app_name, session_service)
# Create background task to run agent
async def run_query1() -> None:
await run_agent(runner1, user_id, session_id, query1, tool_call_callback=on_tool_call)
# Start agent in background
task = asyncio.create_task(run_query1())
# Wait for tool call to be detected
print("⏳ Waiting for tool call to be detected...")
await tool_call_detected.wait()
# Now cancel immediately after tool call is detected (tool is still executing)
runner2 = create_runner(app_name, session_service)
print("\n⏸️ Tool call detected! Requesting cancellation during tool execution...")
success = await runner2.cancel_run_async(user_id=user_id, session_id=session_id)
print(f"✓ Cancellation requested: {success}")
# Wait for task to complete
await task
print()
print("💡 Result: Incomplete function calls were cleaned up from session")
print()
# Query 2: Ask "what happened?" to verify session state
query2 = "what happened?"
print(f"📝 User Query 2: {query2}")
print()
# Create new runner for query 2
runner2 = create_runner(app_name, session_service)
await run_agent(runner2, user_id, session_id, query2)
print("💡 Result: Agent can still respond with session context maintained")
print("-" * 80)
print()
async def run_with_cancellation_demo() -> None:
"""Demonstrate LangGraph agent cancellation in realistic scenarios."""
print("=" * 80)
print("🎯 LangGraph Agent Cancellation Demo")
print("=" * 80)
print()
# Scenario 1: Cancel during LLM streaming
await scenario_1_cancel_during_streaming()
# Scenario 2: Cancel during tool execution
await scenario_2_cancel_during_tool_execution()
print()
print("=" * 80)
print("✅ Demo completed!")
print("=" * 80)
if __name__ == "__main__":
asyncio.run(run_with_cancellation_demo())