-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathtools.py
More file actions
55 lines (45 loc) · 1.83 KB
/
Copy pathtools.py
File metadata and controls
55 lines (45 loc) · 1.83 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
# 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.
"""A long-running tool that streams progress events to the user.
The tool simulates a multi-step site crawl. Each step yields a structured
progress payload that the framework surfaces as a partial Event in real time.
The **last** yielded value is also the final tool response fed back to the LLM.
"""
from __future__ import annotations
import asyncio
import random
from typing import AsyncIterator
async def crawl_site(url: str, max_pages: int = 5) -> AsyncIterator[dict]:
"""Crawl ``url`` and stream progress for every page fetched.
Use this for long-running fetches where the user benefits from seeing
incremental progress instead of staring at a spinner.
Args:
url: The site URL to crawl (any string for demo purposes).
max_pages: How many pages to simulate fetching. Defaults to 5.
Yields:
dict: One progress payload per step. The final payload is also the
return value the LLM sees.
"""
yield {"status": "started", "url": url, "max_pages": max_pages}
fetched_titles: list[str] = []
for page_index in range(1, max_pages + 1):
# Simulate variable per-page latency so the streaming is observable.
await asyncio.sleep(random.uniform(0.4, 1.2))
title = f"{url} - page {page_index}"
fetched_titles.append(title)
yield {
"status": "fetched",
"page": page_index,
"total": max_pages,
"title": title,
"progress": round(page_index / max_pages, 2),
}
yield {
"status": "done",
"url": url,
"pages_fetched": len(fetched_titles),
"titles": fetched_titles,
}