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
107 lines (87 loc) · 3.74 KB
/
Copy pathrun_agent.py
File metadata and controls
107 lines (87 loc) · 3.74 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
#!/usr/bin/env python3
# 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.
"""
Hierarchical Team example demonstrating TeamAgent as member.
This example shows nested TeamAgent structure:
- project_manager (TeamAgent) delegates to:
- dev_team (TeamAgent) which further delegates to backend_dev and frontend_dev
- doc_writer (LlmAgent) for documentation
"""
import asyncio
import uuid
from dotenv import load_dotenv
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_hierarchical_team_demo():
"""Run the hierarchical team demo with nested TeamAgent."""
app_name = "hierarchical_team_demo"
from agent.agent import root_agent
session_service = InMemorySessionService()
runner = Runner(app_name=app_name, agent=root_agent, session_service=session_service)
user_id = "demo_user"
session_id = str(uuid.uuid4())
# Demo conversation - demonstrates nested team delegation
demo_queries = [
"Please implement a user authentication feature with login UI and API",
]
print("=" * 70)
print("Hierarchical Team Demo - TeamAgent as Member")
print("=" * 70)
print(f"\nSession ID: {session_id[:8]}...")
print("\nThis demo shows nested TeamAgent structure:")
print(" project_manager (TeamAgent)")
print(" -> dev_team (TeamAgent as member)")
print(" -> backend_dev (LlmAgent)")
print(" -> frontend_dev (LlmAgent)")
print(" -> doc_writer (LlmAgent)")
print("\n" + "-" * 70)
for i, query in enumerate(demo_queries, 1):
print(f"\n[Turn {i}] User: {query}")
print("-" * 50)
user_content = Content(parts=[Part.from_text(text=query)])
author = None
async for event in runner.run_async(
user_id=user_id,
session_id=session_id,
new_message=user_content,
):
if event.content and event.content.parts:
if not event.partial:
for part in event.content.parts:
if part.function_call:
print(f"\n[{event.author}] Tool: {part.function_call.name}, "
f"Args: {part.function_call.args}")
author = event.author
elif part.function_response:
author = event.author
# Truncate long responses for readability
response_str = str(part.function_response)
if len(response_str) > 100:
response_str = response_str[:100] + "..."
print(f"\n[{event.author}] Tool Response: {response_str}")
else:
for part in event.content.parts:
if part.text:
if author != event.author:
author = event.author
print(f"\n[{author}] ", end="")
print(f"{part.text}", end="", flush=True)
print("\n")
print("=" * 70)
print("Demo completed!")
print("=" * 70)
await runner.close()
if __name__ == "__main__":
print("Hierarchical Team Example")
print("Demonstrates TeamAgent as member of another TeamAgent")
print("Structure: project_manager -> dev_team -> [backend_dev, frontend_dev]")
print(" -> doc_writer")
print()
asyncio.run(run_hierarchical_team_demo())