Commit a65b4cf
mios-agent-pipe: router + dispatch + SurrealDB writes (step 2 of 5)
Ports the chain that gives Discord (and every other gateway)
tool-understanding parity with OWUI. Step 1 stood up the FastAPI
scaffold as a transparent proxy; this commit replaces the proxy
with the actual router-classified chain.
Per the Step 1 commit's plan, the parts ported now:
* Router (Layer-1 micro-LLM classifier)
- _ROUTER_SYSTEM verbatim from the OWUI pipe (verb table +
action enum + verb-pick priority + general routing rules)
- classify_intent() async helper using httpx (OWUI pipe
used aiohttp; the standalone service uses httpx for
consistency with the rest of the FastAPI app)
- Best-effort SurrealDB event row for every router verdict
(kind=classify, source=mios-agent-pipe, payload=<verdict>)
* Dispatch (verb -> broker -> structured tool_result)
- _build_dispatch_cmd() maps the full verb table -> shell
cmd line, in lockstep with the OWUI pipe's
_dispatch_mios_verb. Every verb shipped to date is
supported: open_app, launch_app, focus_window,
move_window, close_window, list_windows, screen_layout,
open_url, mios_find, mios_apps, everything_search,
fs_search, system_status, service_status, service_restart,
process_list, container_status, container_restart.
- dispatch_mios_verb() async helper that talks to the
launcher broker via /run/mios-launcher/launcher.sock
using the CAPTURE_JSON: protocol (so stdout / stderr / exit
come back cleanly split -- foundation laid in commit
24c258d).
- Returns a structured dict {success, tool, args, output,
stderr, exit_code, latency_ms} the chat handler wraps
into the OpenAI tool_call + tool_result envelope.
* /v1/chat/completions chain
- Session row in SurrealDB opens at the start of every turn
(platform=mios-agent-pipe) so downstream tool_call rows
link back via session = <record_id>.
- Extract last user message (handles both string and
OpenAI's array-of-content-parts shape).
- classify_intent() -> three branches:
dispatch -> run broker, build tool_calls envelope,
emit either SSE chunks (streaming) or a
single JSON response (non-streaming).
chat -> emit short reply as SSE chunks or JSON.
agent/? -> fall through to the backend (hermes) proxy.
- tool_call row written fire-and-forget per dispatch (so
Discord chats also populate the SurrealDB audit trail
every other gateway gets).
* SSE chunk shape
OpenAI-compat streaming protocol -- any gateway with an
OpenAI client (OWUI's pipe shim, Hermes Discord gateway,
Slack/Telegram, MCP, ...) consumes the response unchanged.
The dispatch fast-path emits the structured tool_calls
envelope as a <details type="tool_calls"> markdown block
inside a single content delta; gateways with markdown
rendering get a collapsible block, agents reading the chat
history see canonical structured JSON inside.
* /health
Now reports router (enabled/model/endpoint) + broker
presence + db_url so an operator probing the service knows
at a glance which subsystems are reachable.
Deliberately skipped (can be Step 2b):
* REFINE pass (CPU rewrite of the user message before forward)
* CRITIC pass + recompose loop
* POLISH pass + narration collapse
These add quality but not tool-understanding -- Discord's
parity-with-OWUI need is satisfied without them. Operator-
directed if Discord chats need any of them after live testing.
Live-verified on podman-MiOS-DEV:
GET /health -> json with router/broker/db all
reachable.
POST /v1/chat/completions:
"hello there" -> action=chat -> "Hello! How can I assist
you today?" (no hermes
round-trip)
"open notepad" -> action=dispatch -> open_app(name=notepad)
-> broker -> [mios-windows] launched via
cmd /c start: C:\Windows\System32\notepad.exe
-> tool_calls envelope returned
SurrealDB writes confirmed via journal: 3 session opens + 3
classify events + tool_call rows all HTTP 200.
Next: Step 3 collapses OWUI's pipe to a thin forwarder pointing
at :8640; Step 4 re-points Hermes Discord gateway at :8640.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent e2b5acb commit a65b4cf
1 file changed
Lines changed: 642 additions & 49 deletions
0 commit comments