WolfStack icon indicating copy to clipboard operation
WolfStack copied to clipboard

Collapse 3 provider-specific tool loops in wolfagents::agent_loop.rs into one that calls ai_gateway (~1,083-line refactor)

Open tonytouch opened this issue 5 months ago • 1 comments

Problem

src/wolfagents/agent_loop.rs (1,238 lines) contains three near-duplicate provider-specific tool loops:

Loop Start line Span (lines) Talks to
openai_tool_loop 148 148–376 (≈229) OpenAI / OpenRouter / local
gemini_tool_loop 377 377–790 (≈414) Google Gemini
claude_tool_loop 791 791–≈1,230 (≈440) Anthropic Claude
Total ≈1,083 lines of duplication

A single dispatch site in the same file (≈L98–118) picks one of the three at runtime based on which provider AiConfig is configured for. The duplication shows up in: system-prompt assembly, request body encoding (3 different shapes), tool-call extraction (3 different parsers), the actual dispatch::dispatch(agent, …) call (identical in all three), and error reporting / budget enforcement.

Why now

The Phase-0 scaffold of the new ai_gateway module just landed. ai_gateway is the unified LLM caller that replaces mod ai::simple_chat as the call site and collapses the three tool loops into one. Once Phase 1 of the design doc lands concrete Provider impls (openai-compat, anthropic, gemini), this refactor becomes a tractable drop-in.

Refactor target

  • Delete openai_tool_loop (L148–376), gemini_tool_loop (L377–790), claude_tool_loop (L791–≈1,230) — ≈1,083 lines
  • Replace the dispatch site (≈L98–118) with a single call to the new ai_gateway::chat_unified
  • agent_loop.rs shrinks from 1,238 → ≈155 lines (≈87% reduction)
  • src/ai/mod.rs (4,493 lines) — simple_chat at L2406, chat at L825, AiConfig at L92 — can shrink to ≈500 lines once the gateway is the only LLM call site

Approach (3 phases)

A. Side-by-side — ai_gateway::chat_unified added; dispatch picks between old and new based on a feature flag (default: new). Both code paths live and run in production; results must match.

B. Cutover — flag flips to new-only after 1–2 weeks of parity confirmation; the three legacy loops are deleted.

C. Clean up — ai/mod.rs audited for non-LLM helpers (prompt templating, config loading); moved into ai_gateway/ or a smaller ai_core/; simple_chat and chat deleted.

Acceptance criteria

  • [ ] src/wolfagents/agent_loop.rs is ≤200 lines (down from 1,238)
  • [ ] The three *_tool_loop functions are deleted; no callers reference them
  • [ ] src/ai/mod.rs is ≤1,000 lines (down from 4,493)
  • [ ] ai_gateway::chat_unified is the only LLM call site used by wolfagents::dispatch
  • [ ] Discord / Telegram / WhatsApp agent bindings work end-to-end against all three providers
  • [ ] cargo check --bin wolfstack clean (0 warnings, 0 errors)
  • [ ] No behavioural regressions

Dependencies

Blocked on Phase 1 of the design doc (concrete Provider impls + chat_unified). Unblocked work: drop-in OpenAI SDK surface for external clients, virtual keys + per-key budgets, fallback chain.

Canonical tracking doc

Full design + refactor plan + decision log lives in the tonysplace mirror at docs/issues/rust-ai-gateway-collapse-tool-loops.md (cross-linked from docs/rust-ai-gateway.md). This GitHub issue is the mirror for wolfstack maintainer visibility.

tonytouch avatar Jun 15 '26 00:06 tonytouch

Added to Roadmap.

intelligentwolf avatar Jun 15 '26 05:06 intelligentwolf