Stateless tool executor for search / visit / python. Parameters ---------- serper_api_key : str Serper API key (for both search and scrape). summarize_client : AsyncOpenAI The *same* client the agent uses — avoid creating a second one. summarize_model : str
| 343 | |
| 344 | |
| 345 | class ToolKit: |
| 346 | """Stateless tool executor for search / visit / python. |
| 347 | |
| 348 | Parameters |
| 349 | ---------- |
| 350 | serper_api_key : str |
| 351 | Serper API key (for both search and scrape). |
| 352 | summarize_client : AsyncOpenAI |
| 353 | The *same* client the agent uses — avoid creating a second one. |
| 354 | summarize_model : str |
| 355 | Model name for the summarization LLM calls. |
| 356 | sandbox_url : str or None |
| 357 | HTTP endpoint for the Python sandbox. ``None`` disables python. |
| 358 | """ |
| 359 | |
| 360 | def __init__( |
| 361 | self, |
| 362 | serper_api_key: str, |
| 363 | summarize_client: Any, |
| 364 | summarize_model: str, |
| 365 | sandbox_url: Optional[str] = None, |
| 366 | ): |
| 367 | self._api_key = serper_api_key |
| 368 | self._llm = summarize_client |
| 369 | self._llm_model = summarize_model |
| 370 | self._sandbox_url = sandbox_url |
| 371 | |
| 372 | async def call(self, fn_name: str, arguments: dict) -> str: |
| 373 | """Dispatch a tool call by name. Returns plain-text result.""" |
| 374 | if fn_name == "search": |
| 375 | return await self._do_search(arguments) |
| 376 | if fn_name == "scholar_search": |
| 377 | return await self._do_scholar_search(arguments) |
| 378 | if fn_name == "visit": |
| 379 | return await self._do_visit(arguments) |
| 380 | if fn_name == "python": |
| 381 | return await self._do_python(arguments) |
| 382 | return f"Unknown tool: {fn_name}" |
| 383 | |
| 384 | # ---- search ---- |
| 385 | |
| 386 | async def _do_search(self, args: dict) -> str: |
| 387 | queries = args.get("query", "") |
| 388 | if isinstance(queries, str): |
| 389 | queries = [queries] |
| 390 | topn = args.get("topn", 10) |
| 391 | return await _retry( |
| 392 | lambda: _serper_search(queries, self._api_key, topn=topn), |
| 393 | label="search", |
| 394 | ) |
| 395 | |
| 396 | # ---- scholar_search ---- |
| 397 | |
| 398 | async def _do_scholar_search(self, args: dict) -> str: |
| 399 | queries = args.get("query", "") |
| 400 | if isinstance(queries, str): |
| 401 | queries = [queries] |
| 402 | return await _retry( |