-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_agent.py
More file actions
211 lines (178 loc) · 7.51 KB
/
run_agent.py
File metadata and controls
211 lines (178 loc) · 7.51 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
# 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.
"""Graph interrupt example runner."""
import asyncio
import uuid
from typing import Optional
from dotenv import load_dotenv
from trpc_agent_sdk.dsl.graph import STATE_KEY_LAST_RESPONSE
from trpc_agent_sdk.dsl.graph import EventUtils
from trpc_agent_sdk.dsl.graph import ExecutionPhase
from trpc_agent_sdk.dsl.graph import ModelExecutionMetadata
from trpc_agent_sdk.dsl.graph import NodeExecutionMetadata
from trpc_agent_sdk.dsl.graph import ToolExecutionMetadata
from trpc_agent_sdk.events import LongRunningEvent
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 FunctionResponse
from trpc_agent_sdk.types import Part
load_dotenv()
def normalize_author(author: str | None) -> str:
return author if author else "unknown"
async def get_last_response_from_session(
session_service: InMemorySessionService,
app_name: str,
user_id: str,
session_id: str,
) -> str:
session = await session_service.get_session(
app_name=app_name,
user_id=user_id,
session_id=session_id,
)
if session is None or session.state is None:
return ""
response = session.state.get(STATE_KEY_LAST_RESPONSE, "")
return response if isinstance(response, str) else ""
async def stream_once(
root_agent,
session_service: InMemorySessionService,
app_name: str,
user_id: str,
session_id: str,
content: Content,
) -> Optional[LongRunningEvent]:
"""Run one invocation and return captured LongRunningEvent if interrupted."""
streaming = False
captured_interrupt_event: Optional[LongRunningEvent] = None
runner = Runner(app_name=app_name, agent=root_agent, session_service=session_service)
def end_stream_line() -> None:
nonlocal streaming
if streaming:
print()
streaming = False
try:
async for event in runner.run_async(user_id=user_id, session_id=session_id, new_message=content):
if isinstance(event, LongRunningEvent):
end_stream_line()
captured_interrupt_event = event
print("[interrupt] LongRunningEvent received")
print(f"[interrupt] function={event.function_call.name}")
print(f"[interrupt] args={event.function_call.args}")
print(f"[interrupt] response={event.function_response.response}")
continue
node_meta = NodeExecutionMetadata.from_event(event)
if node_meta:
end_stream_line()
if node_meta.phase == ExecutionPhase.START:
print(f"[Node start] node_type={node_meta.node_type}, node_name={node_meta.node_id}")
elif node_meta.phase == ExecutionPhase.COMPLETE:
print(f"[Node done ] node_type={node_meta.node_type}, node_name={node_meta.node_id}")
elif node_meta.phase == ExecutionPhase.ERROR:
print(f"[Node error] node_type={node_meta.node_type}, node_name={node_meta.node_id}")
if node_meta.error:
print(f" Error: {node_meta.error}")
tool_meta = ToolExecutionMetadata.from_event(event)
if tool_meta:
end_stream_line()
if tool_meta.phase == ExecutionPhase.START:
print(f"[Tool start] {tool_meta.tool_name} (id={tool_meta.tool_id})")
elif tool_meta.phase == ExecutionPhase.COMPLETE:
print(f"[Tool done ] {tool_meta.tool_name} (id={tool_meta.tool_id})")
model_meta = ModelExecutionMetadata.from_event(event)
if model_meta:
end_stream_line()
if model_meta.phase == ExecutionPhase.START:
print(f"[Model start] {model_meta.model_name} ({model_meta.node_id})")
elif model_meta.phase == ExecutionPhase.COMPLETE:
print(f"[Model done ] {model_meta.model_name} ({model_meta.node_id})")
if not EventUtils.is_graph_event(event) and event.content and event.content.parts:
current_author = normalize_author(event.author)
if event.partial:
for part in event.content.parts:
if part.text:
if not streaming:
end_stream_line()
print(f"[{current_author}] ", end="", flush=True)
streaming = True
print(part.text, end="", flush=True)
continue
end_stream_line()
for part in event.content.parts:
if part.thought:
continue
if part.function_call:
print(
f"[{current_author}] [Function call] {part.function_call.name}({part.function_call.args})")
elif part.function_response:
print(f"[{current_author}] [Function result] {part.function_response.response}")
if captured_interrupt_event is None:
final_output = await get_last_response_from_session(
session_service=session_service,
app_name=app_name,
user_id=user_id,
session_id=session_id,
)
if final_output:
end_stream_line()
print(final_output)
finally:
await runner.close()
return captured_interrupt_event
async def run_graph_with_interrupt() -> None:
app_name = "graph_with_interrupt_demo"
user_id = "demo_user"
from agent.agent import root_agent
session_service = InMemorySessionService()
session_id = str(uuid.uuid4())
await session_service.create_session(
app_name=app_name,
user_id=user_id,
session_id=session_id,
state={},
)
print("=" * 44)
print("Graph Interrupt Demo")
print(f"Session: {session_id[:8]}...")
print("-" * 44)
request_text = "Draft one practical action for migrating this graph project safely."
print(f"[user] {request_text}")
first_content = Content(parts=[Part.from_text(text=request_text)])
interrupt_event = await stream_once(
root_agent=root_agent,
session_service=session_service,
app_name=app_name,
user_id=user_id,
session_id=session_id,
content=first_content,
)
if interrupt_event is None:
print("No interrupt occurred. Check node logic or model output.")
return
user_decision = {
"status": "approved",
"note": "Looks good, proceed with this action.",
}
print("-" * 44)
print(f"[user decision] {user_decision}")
resume_response = FunctionResponse(
id=interrupt_event.function_response.id,
name=interrupt_event.function_response.name,
response=user_decision,
)
resume_content = Content(role="user", parts=[Part(function_response=resume_response)])
await stream_once(
root_agent=root_agent,
session_service=session_service,
app_name=app_name,
user_id=user_id,
session_id=session_id,
content=resume_content,
)
print("-" * 44)
if __name__ == "__main__":
asyncio.run(run_graph_with_interrupt())