|
| 1 | +# Tencent is pleased to support the open source community by making tRPC-Agent-Python available. |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Tencent. All rights reserved. |
| 4 | +# |
| 5 | +# tRPC-Agent-Python is licensed under Apache-2.0. |
| 6 | +"""Demo: streaming progress events from a long-running tool. |
| 7 | +
|
| 8 | +Run with:: |
| 9 | +
|
| 10 | + cd examples/llmagent_with_streaming_progress_tool |
| 11 | + python run_agent.py |
| 12 | +
|
| 13 | +Make sure ``TRPC_AGENT_API_KEY``, ``TRPC_AGENT_BASE_URL`` and |
| 14 | +``TRPC_AGENT_MODEL_NAME`` are set in your environment or .env file. |
| 15 | +""" |
| 16 | + |
| 17 | +import asyncio |
| 18 | +import sys |
| 19 | +import uuid |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +from dotenv import load_dotenv |
| 23 | +from trpc_agent_sdk.runners import Runner |
| 24 | +from trpc_agent_sdk.sessions import InMemorySessionService |
| 25 | +from trpc_agent_sdk.types import Content |
| 26 | +from trpc_agent_sdk.types import Part |
| 27 | + |
| 28 | +load_dotenv() |
| 29 | + |
| 30 | +sys.path.append(str(Path(__file__).parent)) |
| 31 | + |
| 32 | + |
| 33 | +async def run_streaming_progress_demo() -> None: |
| 34 | + """Issue one query and pretty-print every event surfaced by the runner. |
| 35 | +
|
| 36 | + Three event kinds matter here: |
| 37 | + 1. ``event.partial`` with ``custom_metadata.tool_progress`` → a *progress* |
| 38 | + chunk from the streaming tool. Print it live; do NOT treat it as a |
| 39 | + tool response. |
| 40 | + 2. ``event.partial`` text from the LLM (no ``tool_progress`` marker) → |
| 41 | + streaming model output. |
| 42 | + 3. ``partial=False`` events with a ``function_response`` part → the final |
| 43 | + tool result; with a ``text`` part → the model's final reply. |
| 44 | + """ |
| 45 | + |
| 46 | + app_name = "streaming_progress_demo" |
| 47 | + from agent.agent import root_agent |
| 48 | + |
| 49 | + session_service = InMemorySessionService() |
| 50 | + runner = Runner(app_name=app_name, agent=root_agent, session_service=session_service) |
| 51 | + |
| 52 | + user_id = "demo_user" |
| 53 | + session_id = str(uuid.uuid4()) |
| 54 | + await session_service.create_session(app_name=app_name, user_id=user_id, session_id=session_id) |
| 55 | + |
| 56 | + query = "Please crawl https://example.com and fetch the first 5 pages." |
| 57 | + print("=" * 60) |
| 58 | + print(f"User: {query}") |
| 59 | + print("=" * 60) |
| 60 | + |
| 61 | + user_content = Content(parts=[Part.from_text(text=query)]) |
| 62 | + |
| 63 | + async for event in runner.run_async(user_id=user_id, session_id=session_id, new_message=user_content): |
| 64 | + meta = event.custom_metadata or {} |
| 65 | + |
| 66 | + # --- 1. Tool progress (partial, comes from StreamingProgressTool) --- |
| 67 | + if event.partial and meta.get("tool_progress"): |
| 68 | + payload = meta.get("payload") |
| 69 | + tool_name = meta.get("tool_name", "?") |
| 70 | + print(f"[{tool_name}] ⏳ {payload if payload is not None else event.get_text()}") |
| 71 | + continue |
| 72 | + |
| 73 | + if not event.content or not event.content.parts: |
| 74 | + continue |
| 75 | + |
| 76 | + # --- 2. Streaming LLM text --- |
| 77 | + if event.partial: |
| 78 | + for part in event.content.parts: |
| 79 | + if part.text: |
| 80 | + print(part.text, end="", flush=True) |
| 81 | + continue |
| 82 | + |
| 83 | + # --- 3. Final events --- |
| 84 | + for part in event.content.parts: |
| 85 | + if part.function_call: |
| 86 | + print(f"\n[tool-call] {part.function_call.name}({part.function_call.args})") |
| 87 | + elif part.function_response: |
| 88 | + print(f"\n[tool-result] {part.function_response.name} → " |
| 89 | + f"{part.function_response.response}") |
| 90 | + elif part.text: |
| 91 | + print(f"\nAssistant: {part.text}") |
| 92 | + |
| 93 | + print("\n" + "-" * 60) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + print(""" |
| 98 | ++--------------------------------------------------------------+ |
| 99 | +| StreamingProgressTool Demo (long-running tool) | |
| 100 | +| | |
| 101 | +| Watch the tool yield progress events live, then the LLM | |
| 102 | +| summarises the final result. | |
| 103 | ++--------------------------------------------------------------+ |
| 104 | +""") |
| 105 | + asyncio.run(run_streaming_progress_demo()) |
0 commit comments