Files
OmniRoute/tests/unit/web-fetch-interception.test.ts
Diego Rodrigues de Sa e Souza a95da4a902 feat(routing): wire interceptFetch tool interception into the chat pipeline (#7339) (#7736)
Phases 3-4 of #3384 (Phases 1-2 shipped DB schema + resolveInterceptSearch in
release/v3.8.47). Adds resolveInterceptFetch(provider, model) as a structural
twin of resolveInterceptSearch, and open-sse/services/webFetchInterception.ts
(mirroring webSearchFallback.ts) to rewrite a provider-native web_fetch tool
declaration into a synthetic omniroute_web_fetch function tool. The synthetic
tool call is dispatched through the existing handleToolCallExecution path
(same as omniroute_web_search) to a new web_fetch builtin skill handler that
resolves credentials and calls handleWebFetch() against /v1/web/fetch.

Strictly opt-in: with no interceptFetch DB row configured (the default), the
outgoing request body is byte-identical to pre-change behavior — no heuristic
default bypass like the interceptSearch sibling, to guarantee zero overhead
when disabled (Hard Rule #20).

Also ships the dashboard toggle (owner decision, overriding the plan's
backend-only recommendation): ProviderInterceptionSection.tsx on the provider
detail page, backed by GET/PUT/DELETE /api/providers/[id]/interception-rules,
covering both interceptSearch and interceptFetch from one control.

chatCore.ts touch is minimal (frozen file): one resolver call + one
prepareWebFetchFallbackBody call mirroring the existing interceptSearch block,
plus threading provider/model into the existing handleToolCallExecution call.
2026-07-19 14:37:30 -03:00

140 lines
5.1 KiB
TypeScript

import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
OMNIROUTE_WEB_FETCH_FALLBACK_TOOL_NAME,
prepareWebFetchFallbackBody,
supportsNativeWebFetchFallbackBypass,
} from "../../open-sse/services/webFetchInterception.ts";
import { FORMATS } from "../../open-sse/translator/formats.ts";
// #7339 — webFetchInterception.ts, a structural twin of webSearchFallback.ts
// (Phase 3-4 of #3384). Pure body/tool-array transformation only.
describe("services/webFetchInterception — prepareWebFetchFallbackBody (#7339)", () => {
const baseOptions = {
provider: "openai",
sourceFormat: FORMATS.OPENAI,
targetFormat: FORMATS.OPENAI,
nativeCodexPassthrough: false,
};
it("is a no-op (byte-identical body, enabled:false) when no tools array is present", () => {
const body = { model: "gpt-5" };
const { body: nextBody, fallback } = prepareWebFetchFallbackBody(body, baseOptions);
assert.deepEqual(nextBody, body);
assert.equal(fallback.enabled, false);
assert.equal(fallback.toolName, null);
assert.equal(fallback.convertedToolCount, 0);
});
it("is a no-op when no web_fetch-shaped tool is present in the tools array", () => {
const body = { tools: [{ type: "function", function: { name: "get_weather" } }] };
const { body: nextBody, fallback } = prepareWebFetchFallbackBody(body, baseOptions);
assert.deepEqual(nextBody, body);
assert.equal(fallback.enabled, false);
assert.equal(fallback.convertedToolCount, 0);
});
it("converts a native web_fetch tool into the synthetic omniroute_web_fetch tool when interceptFetchOverride is true", () => {
const body = { tools: [{ type: "web_fetch" }] };
const { body: nextBody, fallback } = prepareWebFetchFallbackBody(body, {
...baseOptions,
interceptFetchOverride: true,
});
assert.equal(fallback.enabled, true);
assert.equal(fallback.toolName, OMNIROUTE_WEB_FETCH_FALLBACK_TOOL_NAME);
assert.equal(fallback.convertedToolCount, 1);
assert.equal(nextBody.tools.length, 1);
assert.equal(
(nextBody.tools[0] as { function: { name: string } }).function.name,
OMNIROUTE_WEB_FETCH_FALLBACK_TOOL_NAME
);
});
it("uses a flat function-tool shape for the Responses API target", () => {
const body = { tools: [{ type: "web_fetch" }] };
const { body: nextBody } = prepareWebFetchFallbackBody(body, {
...baseOptions,
targetFormat: FORMATS.OPENAI_RESPONSES,
interceptFetchOverride: true,
});
const tool = nextBody.tools[0] as { type: string; name: string; function?: unknown };
assert.equal(tool.type, "function");
assert.equal(tool.name, OMNIROUTE_WEB_FETCH_FALLBACK_TOOL_NAME);
assert.equal(tool.function, undefined);
});
it("leaves the native web_fetch tool untouched when interceptFetchOverride is false", () => {
const body = { tools: [{ type: "web_fetch" }] };
const { body: nextBody, fallback } = prepareWebFetchFallbackBody(body, {
...baseOptions,
interceptFetchOverride: false,
});
assert.deepEqual(nextBody, body);
assert.equal(fallback.enabled, false);
});
it("preserves other tools alongside the converted synthetic tool", () => {
const body = {
tools: [{ type: "web_fetch" }, { type: "function", function: { name: "get_weather" } }],
};
const { body: nextBody, fallback } = prepareWebFetchFallbackBody(body, {
...baseOptions,
interceptFetchOverride: true,
});
assert.equal(fallback.convertedToolCount, 1);
assert.equal(nextBody.tools.length, 2);
const names = (nextBody.tools as Array<{ function?: { name: string } }>).map(
(tool) => tool.function?.name
);
assert.ok(names.includes(OMNIROUTE_WEB_FETCH_FALLBACK_TOOL_NAME));
assert.ok(names.includes("get_weather"));
});
});
describe("services/webFetchInterception — supportsNativeWebFetchFallbackBypass (#7339)", () => {
it("interceptFetchOverride true always forces interception (no bypass)", () => {
assert.equal(
supportsNativeWebFetchFallbackBypass({
targetFormat: FORMATS.CLAUDE,
sourceFormat: FORMATS.CLAUDE,
nativeCodexPassthrough: false,
interceptFetchOverride: true,
}),
false
);
});
it("interceptFetchOverride false always forces native bypass", () => {
assert.equal(
supportsNativeWebFetchFallbackBypass({
targetFormat: FORMATS.OPENAI,
sourceFormat: FORMATS.OPENAI,
nativeCodexPassthrough: false,
interceptFetchOverride: false,
}),
true
);
});
it("bypasses natively for Codex passthrough with no override set", () => {
assert.equal(
supportsNativeWebFetchFallbackBypass({
targetFormat: FORMATS.OPENAI_RESPONSES,
nativeCodexPassthrough: true,
}),
true
);
});
it("bypasses by default (strictly opt-in, no heuristic default) when no override is set at all", () => {
assert.equal(
supportsNativeWebFetchFallbackBypass({
targetFormat: FORMATS.OPENAI,
sourceFormat: FORMATS.OPENAI,
nativeCodexPassthrough: false,
}),
true
);
});
});