Spaces:
Running
Running
Download core/agent/router.py from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/agent/router.py
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/core/agent/router.py
-
curl -L -o router.py https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/agent/router.py
2.72 kB
| """Router: events with a known type route deterministically; operator chat is classified by | |
| the cheap model into one known intent, a compound request (supervisor) or unsupported.""" | |
| from __future__ import annotations | |
| from typing import Literal | |
| from pydantic import BaseModel, Field | |
| from core.agent.models import LLM | |
| from core.agent.state import Event | |
| from core.packs.loader import IntentInfo | |
| SUPERVISOR = "supervisor" | |
| UNSUPPORTED = "unsupported" | |
| class RouteDecision(BaseModel): | |
| kind: Literal["single", "compound", "unsupported"] | |
| intent: str | None = Field(default=None, description="intent name when kind is single") | |
| def router_prompt(intents: list[IntentInfo]) -> str: | |
| lines = "\n".join(f"- {i.name}: {i.description}" for i in intents if i.chat_enabled) | |
| return ( | |
| "You route a business owner's request to exactly one handler.\n" | |
| f"Available intents:\n{lines}\n" | |
| "Answer kind=single with the intent name if one intent covers the whole request; " | |
| "kind=compound if it needs several intents in sequence; kind=unsupported if none " | |
| "fits or the request is unrelated to running this business." | |
| ) | |
| async def route( | |
| event: Event, | |
| intents: list[IntentInfo], | |
| llm: LLM, | |
| aliases: dict[str, str] | None = None, | |
| *, | |
| context: str | None = None, | |
| ) -> str: | |
| """``aliases`` maps external event types to intents (packs' ``event_aliases()``, e.g. | |
| ``shopify.orders.create`` -> ``ecommerce.order.created``; per tenant since Phase 15). | |
| Deterministic: no LLM. ``context`` (Phase 15, G6) is a business date/time-zone line | |
| appended to the chat classifier's system prompt.""" | |
| names = {i.name for i in intents} | |
| if event.type in names: | |
| return event.type | |
| aliased = (aliases or {}).get(event.type) | |
| if aliased in names: | |
| return str(aliased) | |
| if event.type != "chat.message": | |
| return UNSUPPORTED | |
| if not event.trusted or not event.text: | |
| # Untrusted free text never chooses a workflow; it only feeds the extractor. | |
| return UNSUPPORTED | |
| text = event.text | |
| if event.normalized_text and event.normalized_text != event.text: # Phase 13 | |
| text = f"{event.text}\n(normalized spelling: {event.normalized_text})" | |
| system = router_prompt(intents) | |
| if context: | |
| system = f"{system}\n{context}" | |
| decision = await llm.structured(RouteDecision, system, text) | |
| if decision.kind == "compound": | |
| return SUPERVISOR | |
| chat_names = {i.name for i in intents if i.chat_enabled} | |
| if decision.kind == "single" and decision.intent in chat_names: | |
| return decision.intent | |
| return UNSUPPORTED | |