Spaces:
Runtime error
Runtime error
Download src/backend/core/tools.py from HAK-Tech/ciro-backend: direct link, hf CLI and curl.
- Browser
- Download file 3.33 kB
-
https://huggingface.co/spaces/HAK-Tech/ciro-backend/resolve/main/src/backend/core/tools.py
- Command line
-
hf download hf://spaces/HAK-Tech/ciro-backend/src/backend/core/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/HAK-Tech/ciro-backend/resolve/main/src/backend/core/tools.py
3.33 kB
| """ | |
| Tool registration pattern for CIRO agents. | |
| This module owns the *pattern* for tool registration, not the tools themselves. | |
| Each downstream phase ships its own tools: | |
| Phase 4 weather_api / social_feed | |
| Phase 8 maps_directions / dispatch_actions | |
| Phase 10 fcm_push / firestore_lookup | |
| β¦and registers them by exporting a `@function_tool`-decorated function from | |
| its module. The pipeline picks which subset to hand to each agent at call time | |
| (see `agents/pipeline.py`). There is intentionally no global tool registry β | |
| keeping the surface explicit per agent reduces the "which tool do I pick?" | |
| ambiguity that hurts model accuracy in multi-tool setups. | |
| Conventions every CIRO tool must follow | |
| --------------------------------------- | |
| 1. **Verb-first name.** `get_weather`, `fetch_routes`, `send_push` β not | |
| `weather` or `routes`. Verbs make the agent's tool-selection step | |
| unambiguous. | |
| 2. **Docstring shape: what / when / args / returns / errors.** The decorator | |
| feeds the docstring straight to the LLM as the tool description, so | |
| treat it as prompt engineering, not API docs. | |
| 3. **Return JSON strings, not Python objects.** The SDK serializes whatever | |
| you return, but explicit `json.dumps()` keeps the agent-visible payload | |
| deterministic and lets you embed `error` + `suggestion` fields for | |
| recovery (see `_ping_impl` below). | |
| 4. **Separate `_impl` from the decorated wrapper** for any tool with real | |
| business logic. Lets unit tests call `_impl` directly without going | |
| through the SDK. | |
| The dummy `ping` tool below is the reference implementation. Copy its shape | |
| when you add a real tool. | |
| """ | |
| import json | |
| from agents import function_tool | |
| # ββ Reference dummy tool ββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # | |
| # `ping` exists to (a) document the convention above and (b) give the Phase 3 | |
| # smoke test a deterministic tool to invoke without touching any external API. | |
| # Real tools (weather, maps, FCM) will replace it from their own modules. | |
| async def _ping_impl(message: str) -> str: | |
| """Pure logic for `ping` β separated so it can be unit-tested without | |
| going through the OpenAI Agents SDK. | |
| """ | |
| if not message: | |
| return json.dumps( | |
| { | |
| "error": "INVALID_INPUT", | |
| "message": "message must be a non-empty string", | |
| "suggestion": "Call ping again with a short non-empty message.", | |
| } | |
| ) | |
| return json.dumps({"echo": message, "status": "ok"}) | |
| async def ping(message: str) -> str: | |
| """Echo a short message back. Reference tool used to verify the agent | |
| pipeline is wired up correctly. | |
| Use when: | |
| - You are explicitly asked to verify tool calling works. | |
| - You need a no-op tool to confirm an agent can invoke external code. | |
| Do NOT use this for any real crisis reasoning. | |
| Args: | |
| message: Any short non-empty string. The tool echoes it back verbatim. | |
| Returns: | |
| JSON string of the form `{"echo": "<message>", "status": "ok"}`, | |
| or `{"error": "INVALID_INPUT", "message": ..., "suggestion": ...}` | |
| if `message` was empty. | |
| """ | |
| return await _ping_impl(message) | |