Repository navigation
feat(sse): route web_search requests to a configured model (#4481) #4509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 2 commits into
release/v3.8.33
from
feat/websearch-tool-routing-4481
Jun 21, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * #4481 layer 2 — per-tool web-search model routing (CCR-style `Router.webSearch`). | ||
| * | ||
| * Some providers expose an Anthropic-compatible (Claude-format) endpoint but do NOT | ||
| * implement Anthropic's typed server tools, so forwarding `web_search_20250305` to them | ||
| * makes the upstream 400 (e.g. MiniMax: `invalid params ... (2013)`). Layer 1 | ||
| * (`webSearchFallback.ts`) already converts the tool to OmniRoute's own `/v1/search` | ||
| * fallback for such providers. Layer 2 is the alternative an operator can opt into: | ||
| * route the WHOLE request to a model that natively runs web search, the way | ||
| * claude-code-router's `Router.webSearch` does, while leaving every non-search request on | ||
| * the default model. | ||
| * | ||
| * Pure (no DB / no I/O) so it can be unit-tested and wired into the request entrypoint | ||
| * BEFORE combo/auto routing and before the layer-1 fallback is computed for the target. | ||
| */ | ||
|
|
||
| interface WebSearchRouteResult { | ||
| wasRouted: boolean; | ||
| model: string; | ||
| } | ||
|
|
||
| function asString(value: unknown): string { | ||
| return typeof value === "string" ? value : ""; | ||
| } | ||
|
|
||
| /** | ||
| * True when the request body declares a NATIVE web-search server tool — `web_search`, | ||
| * `web_search_preview`, or any Anthropic dated variant (`web_search_20250305`, future | ||
| * `web_search_YYYYMMDD`). A custom *function* tool that merely happens to be named | ||
| * "web_search" (it carries a `function` field) is NOT a native server tool and is ignored. | ||
| * | ||
| * Uses a `web_search` prefix match on purpose: at the request entrypoint the client's tool | ||
| * is still in its raw form (Claude Code sends the versioned `web_search_20250305`), which | ||
| * the exact-set check in `webSearchFallback.ts` only sees after normalization. | ||
| */ | ||
| export function hasNativeWebSearchTool(body: unknown): boolean { | ||
| if (!body || typeof body !== "object") return false; | ||
| const tools = (body as { tools?: unknown }).tools; | ||
| if (!Array.isArray(tools)) return false; | ||
| return tools.some((tool) => { | ||
| if (!tool || typeof tool !== "object") return false; | ||
| const record = tool as { type?: unknown; function?: unknown }; | ||
| if (record.function) return false; // a custom function tool, not the server tool | ||
| return asString(record.type).startsWith("web_search"); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve whether this request should be routed to the operator-configured web-search | ||
| * model. Routes only when (a) the request carries a native web-search tool, (b) | ||
| * `webSearchRouteModel` is a non-empty string, and (c) it differs from the current model | ||
| * (so re-entry on the already-chosen target is a no-op). The returned `model` is a model | ||
| * string (`provider,model` / `provider/model` / alias / combo name) resolved downstream | ||
| * by the normal routing pipeline. | ||
| */ | ||
| export function resolveWebSearchRouteOverride( | ||
| currentModel: string, | ||
| body: unknown, | ||
| settings: Record<string, unknown> | null | undefined | ||
| ): WebSearchRouteResult { | ||
| const fallthrough: WebSearchRouteResult = { wasRouted: false, model: currentModel }; | ||
| if (!hasNativeWebSearchTool(body)) return fallthrough; | ||
|
|
||
| const configured = asString(settings?.webSearchRouteModel).trim(); | ||
| if (!configured || configured === currentModel) return fallthrough; | ||
|
|
||
| return { wasRouted: true, model: configured }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security/Policy Bypass Risk
The web-search routing override occurs after the API key policy enforcement (
enforceApiKeyPolicyat line 300). If a client requests a cheap/allowed model but includes a native web-search tool, the request will bypass the policy check and be routed to the configuredwebSearchRouteModel(which could be an expensive or restricted model like Claude 3.5 Sonnet).To prevent this policy bypass, we should re-evaluate the API key policy against the new target model if a route override occurs.