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
91 lines (73 loc) · 2.99 KB
/
Copy pathrun_agent.py
File metadata and controls
91 lines (73 loc) · 2.99 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
#!/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.
"""
Example demonstrating the Skill Hub (`trpc_agent_sdk.skills.hub`): fetching a
skill from GitHub on demand and using it like any locally-installed Agent
Skill.
"""
import asyncio
import shutil
import uuid
from pathlib import Path
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
EXAMPLE_DIR = Path(__file__).resolve().parent
load_dotenv(EXAMPLE_DIR / ".env")
QUERY = """
Load the skill-creator skill and, without running any of its scripts,
summarize in a few bullet points: what it is for, and what top-level
files/folders it ships with.
"""
async def run_skill_hub_demo() -> None:
"""Fetch a skill via the Skill Hub, then run the demo agent against it."""
from agent.agent import create_agent
data_dir = EXAMPLE_DIR / "data"
skills_dir = data_dir / "skills"
# Remove any skill installed by a previous run so this demo always
# re-downloads it via the Skill Hub.
if data_dir.exists():
shutil.rmtree(data_dir)
skills_dir.mkdir(parents=True, exist_ok=True)
root_agent = create_agent(skills_dir)
app_name = "skill_hub_demo"
user_id = "demo_user"
session_id = str(uuid.uuid4())
session_service = InMemorySessionService()
runner = Runner(app_name=app_name, agent=root_agent, session_service=session_service)
print(f"🆔 Session ID: {session_id[:8]}...")
print(f"📝 User: {QUERY.strip()}")
print("🤖 Assistant: ", end="", flush=True)
user_content = Content(parts=[Part.from_text(text=QUERY)])
async for event in runner.run_async(user_id=user_id, session_id=session_id, new_message=user_content):
if not event.content or not event.content.parts:
continue
if event.partial:
for part in event.content.parts:
if part.text:
print(part.text, end="", flush=True)
continue
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})]")
elif part.function_response:
print(f"📊 [Tool Result: {part.function_response.response}]")
print("\n" + "-" * 40)
install_root = skills_dir / ".downloaded"
print(f"\nSkill files fetched from GitHub via the Skill Hub into {install_root}:")
skill_files = [f for f in sorted(install_root.rglob("*")) if f.is_file()]
if skill_files:
for skill_file in skill_files:
print(f"- {skill_file.relative_to(install_root)}")
else:
print("- <none>")
if __name__ == "__main__":
asyncio.run(run_skill_hub_demo())