← Back to PRs

#7770: feat(routing): Smart Router V2 - Configuration-driven model dispatching

by zzjj7000 open 2026-02-03 05:56 View on GitHub →
agents stale
Note: AI-assisted 🤖 | Verified locally ✅ Overview This PR introduces Smart Router V2, an architectural upgrade to the model routing engine. It shifts the logic from hardcoded rules to a flexible, configuration-driven system (router_config.json), allowing for more granular control over model selection based on task type and context length. Key Changes SMRP-style Configuration: Routing is now managed via router_config.json. Users can define custom priority chains for different task categories (e.g., coding, creative, architecture). Context Waterline Guard: Implements a "50% safety threshold" logic. The router automatically filters out models that are nearing their context limits to prevent overflow errors and optimize costs. Improved Dispatching Logic: Re-orders model selection to prioritize best-fit models (like Kimi for logic/coding) while maintaining Gemini as a robust fallback for massive contexts. Code Hygiene: Fully compliant with project Lint rules (including mandatory curly braces for control flow). Testing Done pnpm build: Verified that TypeScript compilation passes without errors. pnpm check: Linted via oxlint to ensure 100% compliance with style guidelines. Local Runtime Test: Verified on a live Windows instance. Confirmed successful model switching between Gemini, Kimi, and DeepSeek based on real conversation prompts. Validated that router_config.json hot-reloads correctly without gateway restarts. Transparency This PR was developed with AI assistance (OpenClaw + Gemini 2.0). I have reviewed the generated code for security and architectural alignment. Importantly, this PR is focused solely on routing logic and contains no private configurations or experimental caching implementations. <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR adds a new configuration-driven “Smart Router V2” layer (`src/agents/smart-router.ts`) plus a persistent “model experience” store (`src/agents/model-experience.ts`) intended to categorize tasks and maintain per-model performance/usage stats under `CONFIG_DIR`. The router supports manual prompt prefixes (e.g. `!kimi`, `!pro`) and a config-defined `routes` priority chain, with a context “waterline” filter to drop models that are near their configured context limit. `pi-embedded-runner/run.ts` was touched to import the router, but it is not currently wired into the request flow. Key concerns are around correctness of hot-reload behavior (stale config on invalid JSON), persistence robustness (experience save doesn’t ensure parent dir exists), and a shallow merge that can unintentionally discard default model data when a user file contains partial structures. <h3>Confidence Score: 3/5</h3> - This PR is moderately safe to merge but has a few correctness and integration gaps worth fixing first. - The core additions are self-contained, but there are a couple of issues that will likely bite in real use: the embedded runner imports the router without using it, config hot-reload can silently keep stale state on parse errors, and experience persistence/merging can behave unexpectedly on first run or with partial user files. None look like security-critical vulnerabilities, but they affect expected behavior and reliability. - src/agents/model-experience.ts, src/agents/smart-router.ts, src/agents/pi-embedded-runner/run.ts <!-- greptile_other_comments_section --> <sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub> **Context used:** - Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8)) - Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13)) <!-- /greptile_comment -->

Most Similar PRs