mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-10 17:02:22 +03:00
Rebased onto the current release/v3.8.51 tip as part of a combined provider-retirement/provenance merge batch (Designer Web, Felo Web, Runtime, GPL-derived removal, Qwen Web already landed). Large conflict set (this is the biggest PR in the batch — the common ChatGPT Web provider touches chat, images, count-tokens, session leases, and combos). Conflicts resolved: - `open-sse/config/providers/registry/chatgpt-web/*`, `open-sse/executors/chatgpt-web*`, `open-sse/handlers/imageGeneration/providers/chatgptWeb.ts`, and their tests: kept deleted, matching the PR's stated scope. - `open-sse/config/providers/registry/minimax/web/index.ts`, `open-sse/handlers/imageGeneration/providers/geminiWeb.ts`, `open-sse/executors/gemini-web.ts`'s stale image-mode branch: base-drift collisions against already-merged sibling retirements (#11691, #11708) — kept deleted / dropped the dead code, since this PR's own branch forked before those merged. - `src/shared/constants/reservedProviderPrefixes.ts`, `open-sse/executors/index.ts`, `executorProxy.ts`, `virtualFactory.ts`, `autoStrategy.ts`, `src/lib/db/providers.ts`, `src/sse/handlers/chat.ts`: combined the Designer + Runtime (Felo/Qwen) + common-ChatGPT-Web retirement guard calls at each shared chokepoint — compute-once-then-OR pattern, consistent with prior combinations in this batch. - `src/sse/services/model.ts` / `src/sse/handlers/chatHelpers.ts`: adopted this PR's new `getModelInfoOrRetirementResponse()` central wrapper (a real improvement over ad-hoc try/catch), and extended it to also catch the Designer + Runtime retirement errors it didn't originally cover, so the consolidation doesn't regress the other two mechanisms. - `src/app/api/v1/images/edits/route.ts`: this PR moved the retirement check earlier (before `enforceApiKeyPolicy`) but left the old later call+catch block in place from base drift — removed the now-redundant duplicate `resolveImageRouteModel()` call and merged the Designer catch into the earlier one. - `open-sse/config/imageRegistry.ts`, `tests/snapshots/executors/executor-map.json` (`keyCount` recomputed to 133), `tests/snapshots/provider/translate-path.json`: same "both sides inserted a different retired provider at the same slot" pattern — resolved by dropping both. - `tests/unit/chatcore-executor-proxy.test.ts`, `provider-node-reserved-prefix.test.ts`, `combo-auto-candidate-expansion.test.ts`, `messages-count-tokens-route.test.ts`, `virtual-auto-combo.test.ts`: split into independent per-mechanism test blocks (established pattern); `virtual-auto-combo.test.ts`'s old "includes cookie web-session providers" positive-inclusion test (which used chatgpt-web as its example) was retired along with the provider and replaced by this PR's negative-exclusion test for the same slot. - `docs/architecture/ARCHITECTURE.md`, `CODEBASE_DOCUMENTATION.md` (+ 4 i18n mirrors), `README.md`, `FREE-TIERS-GUIDE.md`, `docs/diagrams/free-tier-budget.svg`, `docs/screenshots/free-tier-budget-card.svg`, `docs/reference/PROVIDER_REFERENCE.md`: recomputed every stale count from the real merged state — 104 executors (`countFiles` gate logic), 351 providers (regenerated via `gen:provider-reference`), 152/351 `hasFree` entries, 445/438/7 free-tier catalog rows, 13 ToS-avoid providers, budget-card regenerated via its real generator script. One doc conflict (`oauth/` module list) needed picking HEAD's side specifically — theirs still listed the already-removed `raycast` module instead of the real `openference`. - `config/quality/test-masking-allowlist.json`: additive merge of the PR's 17 `_deletedWithReplacement` entries alongside the batch's existing ones (one real duplicate-key mistake in my first pass, caught and fixed via a `object_pairs_hook` duplicate-key check before finalizing). Also fixed two real, unrelated-to-my-merge issues surfaced by the focused suite: - `tests/unit/resolve-web-provider-host.test.ts`: the PR's own test had a typo — it asserted `perplexity-web`'s resolved host as `"perplexity.ai"`, but the provider's registered `website` is `"https://www.perplexity.ai"` and the resolver returns the URL's `host` verbatim (no www-stripping), so the correct value is `"www.perplexity.ai"` (consistent with the same test's own `url` assertion). - `tests/unit/hard-session-lease-bypass-inventory.test.ts`: this golden call-site inventory was already stale on the pristine post-#11713 tip (confirmed via a throwaway probe worktree) — `src/lib/db/providers.ts`'s 3 connection-fallback sites and a third `src/app/api/providers/route.ts` site were never added to the golden list by the earlier-merged #11698/#11720 PRs. Updated it to the real current inventory (dated inline comments explain each delta and which PR introduced it), plus this PR's own legitimate deltas (image-edits duplicate-call removal, `ChatGptWebExecutor.execute()` site removed). Focused suite green (433/433 across executor-proxy, reserved-prefix, hard-session-lease-bypass-inventory, resolve-web-provider-host, retirement/runtime-block/source-retirement/management-retirement/image-handler-retirement, migration-168, combo-auto-candidate-expansion, virtual-auto-combo, executor-map-golden and siblings), plus `typecheck:core`, `check-file-size`, and `check-changelog-integrity` clean. Thanks for the thorough provenance-hold retirement work — appreciated.
275 lines
10 KiB
TypeScript
275 lines
10 KiB
TypeScript
/**
|
|
* #5696 — Layer A capability filter unit tests.
|
|
*
|
|
* Tests the pure `checkRequestCapabilityFit` function and the
|
|
* `deriveRequestCapabilityRequirements` helper. The chatCore integration
|
|
* gate is tested via the feature flag assertion below.
|
|
*
|
|
* Note: `getResolvedModelCapabilities` requires a database connection, so
|
|
* the full integration path (capabilities → filter → error response) is
|
|
* tested by verifying the filter function's behavior with mock capabilities.
|
|
*/
|
|
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
checkRequestCapabilityFit,
|
|
deriveRequestCapabilityRequirements,
|
|
type RequestCapabilityRequirements,
|
|
type CapabilityFilterResult,
|
|
} from "../../src/shared/constants/capabilities/capabilityFilter.ts";
|
|
|
|
// ── Helpers ───────────────────────────────────────────────────────────────
|
|
|
|
/** Minimal capabilities shape for filter testing. */
|
|
function caps(
|
|
overrides: Partial<{
|
|
supportsTools: boolean | null;
|
|
toolCalling: boolean;
|
|
supportsVision: boolean | null;
|
|
structuredOutput: boolean | null;
|
|
contextWindow: number | null;
|
|
maxInputTokens: number | null;
|
|
maxOutputTokens: number | null;
|
|
}> = {}
|
|
) {
|
|
return {
|
|
supportsTools: overrides.supportsTools ?? null,
|
|
toolCalling: overrides.toolCalling ?? true,
|
|
supportsVision: overrides.supportsVision ?? null,
|
|
structuredOutput: overrides.structuredOutput ?? null,
|
|
contextWindow: overrides.contextWindow ?? null,
|
|
maxInputTokens: overrides.maxInputTokens ?? null,
|
|
maxOutputTokens: overrides.maxOutputTokens ?? null,
|
|
};
|
|
}
|
|
|
|
function req(
|
|
overrides: Partial<RequestCapabilityRequirements> = {}
|
|
): RequestCapabilityRequirements {
|
|
return {
|
|
requiresTools: false,
|
|
requiresVision: false,
|
|
requiresStructuredOutput: false,
|
|
requiredContextTokens: 0,
|
|
toolCount: 0,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
// ── Tests ─────────────────────────────────────────────────────────────────
|
|
|
|
test("checkRequestCapabilityFit: compatible when no requirements", () => {
|
|
const result = checkRequestCapabilityFit(caps(), req());
|
|
assert.equal(result.compatible, true);
|
|
assert.deepEqual(result.failures, []);
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: vision failure when model lacks vision", () => {
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ supportsVision: false }),
|
|
req({ requiresVision: true })
|
|
);
|
|
assert.equal(result.compatible, false);
|
|
assert.deepEqual(result.failures, ["vision"]);
|
|
assert.equal(result.terminalReason, "vision");
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: vision failure when model vision is unknown (null)", () => {
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ supportsVision: null }),
|
|
req({ requiresVision: true })
|
|
);
|
|
assert.equal(result.compatible, false);
|
|
assert.deepEqual(result.failures, ["vision"]);
|
|
assert.equal(result.terminalReason, "vision");
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: vision OK when model supports vision", () => {
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ supportsVision: true }),
|
|
req({ requiresVision: true })
|
|
);
|
|
assert.equal(result.compatible, true);
|
|
assert.deepEqual(result.failures, []);
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: tools failure when model has no tool support", () => {
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ supportsTools: false, toolCalling: false }),
|
|
req({ requiresTools: true }),
|
|
"openai"
|
|
);
|
|
assert.equal(result.compatible, false);
|
|
assert.deepEqual(result.failures, ["tools"]);
|
|
assert.equal(result.terminalReason, "tools");
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: tools OK when model supports tools", () => {
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ supportsTools: true, toolCalling: true }),
|
|
req({ requiresTools: true }),
|
|
"openai"
|
|
);
|
|
assert.equal(result.compatible, true);
|
|
assert.deepEqual(result.failures, []);
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: tools bypassed for emulated-tool provider", () => {
|
|
// gemini-web has toolCalling: "emulated" in the provider registry,
|
|
// so the filter must not reject it even when capabilities report false.
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ supportsTools: false, toolCalling: false }),
|
|
req({ requiresTools: true }),
|
|
"gemini-web"
|
|
);
|
|
assert.equal(result.compatible, true);
|
|
assert.deepEqual(result.failures, []);
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: structured output failure when model does not support", () => {
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ structuredOutput: false }),
|
|
req({ requiresStructuredOutput: true })
|
|
);
|
|
assert.equal(result.compatible, false);
|
|
assert.deepEqual(result.failures, ["structured_output"]);
|
|
assert.equal(result.terminalReason, "structured_output");
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: structured output OK when model supports", () => {
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ structuredOutput: true }),
|
|
req({ requiresStructuredOutput: true })
|
|
);
|
|
assert.equal(result.compatible, true);
|
|
assert.deepEqual(result.failures, []);
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: context window failure when tokens exceed window", () => {
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ contextWindow: 1000, maxInputTokens: 1000 }),
|
|
req({ requiredContextTokens: 2000 })
|
|
);
|
|
assert.equal(result.compatible, false);
|
|
assert.deepEqual(result.failures, ["context_window"]);
|
|
assert.equal(result.terminalReason, "context_window");
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: context window OK when tokens fit", () => {
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ contextWindow: 10000, maxInputTokens: 10000 }),
|
|
req({ requiredContextTokens: 2000 })
|
|
);
|
|
assert.equal(result.compatible, true);
|
|
assert.deepEqual(result.failures, []);
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: multiple failures reported", () => {
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ supportsVision: false, supportsTools: false, toolCalling: false }),
|
|
req({ requiresVision: true, requiresTools: true }),
|
|
"openai"
|
|
);
|
|
assert.equal(result.compatible, false);
|
|
// vision is checked first, so it's the terminalReason
|
|
assert.ok(result.failures.length >= 1);
|
|
assert.ok(result.failures.includes("vision"));
|
|
});
|
|
|
|
test("checkRequestCapabilityFit: context window returns null (unknown) when no window data", () => {
|
|
// When contextWindow and maxInputTokens are both null, evaluateContextLimit
|
|
// returns null, which means compatible (no data to judge).
|
|
const result = checkRequestCapabilityFit(
|
|
caps({ contextWindow: null, maxInputTokens: null }),
|
|
req({ requiredContextTokens: 2000 })
|
|
);
|
|
assert.equal(result.compatible, true);
|
|
assert.deepEqual(result.failures, []);
|
|
});
|
|
|
|
test("deriveRequestCapabilityRequirements: no requirements from empty body", () => {
|
|
const requirements = deriveRequestCapabilityRequirements({});
|
|
assert.equal(requirements.requiresTools, false);
|
|
assert.equal(requirements.requiresVision, false);
|
|
assert.equal(requirements.requiresStructuredOutput, false);
|
|
assert.equal(requirements.requiredContextTokens, 0);
|
|
assert.equal(requirements.toolCount, 0);
|
|
});
|
|
|
|
test("deriveRequestCapabilityRequirements: detects tools from body", () => {
|
|
const requirements = deriveRequestCapabilityRequirements({
|
|
tools: [{ type: "function", function: { name: "test" } }],
|
|
});
|
|
assert.equal(requirements.requiresTools, true);
|
|
assert.equal(requirements.toolCount, 1);
|
|
});
|
|
|
|
test("deriveRequestCapabilityRequirements: detects vision from image_url", () => {
|
|
const requirements = deriveRequestCapabilityRequirements({
|
|
messages: [
|
|
{
|
|
role: "user",
|
|
content: [{ type: "image_url", image_url: { url: "https://example.com/img.jpg" } }],
|
|
},
|
|
],
|
|
});
|
|
assert.equal(requirements.requiresVision, true);
|
|
});
|
|
|
|
test("deriveRequestCapabilityRequirements: detects structured output from response_format", () => {
|
|
const requirements = deriveRequestCapabilityRequirements({
|
|
response_format: { type: "json_object" },
|
|
});
|
|
assert.equal(requirements.requiresStructuredOutput, true);
|
|
});
|
|
|
|
test("deriveRequestCapabilityRequirements: detects json_schema structured output", () => {
|
|
const requirements = deriveRequestCapabilityRequirements({
|
|
response_format: { type: "json_schema", json_schema: { name: "test", schema: {} } },
|
|
});
|
|
assert.equal(requirements.requiresStructuredOutput, true);
|
|
});
|
|
|
|
test("feature flag CAPABILITY_FILTER_ENABLED defaults to false", () => {
|
|
// This test verifies the feature flag definition ensures the gate is
|
|
// opt-in. The default value must be "false" per the plan.
|
|
import("../../src/shared/constants/featureFlagDefinitions.ts").then(
|
|
({ FEATURE_FLAG_DEFINITIONS }) => {
|
|
const flag = FEATURE_FLAG_DEFINITIONS.find((d) => d.key === "CAPABILITY_FILTER_ENABLED");
|
|
assert.ok(flag, "CAPABILITY_FILTER_ENABLED flag must be defined");
|
|
assert.equal(flag.defaultValue, "false");
|
|
assert.equal(flag.type, "boolean");
|
|
assert.equal(flag.category, "policies");
|
|
}
|
|
);
|
|
});
|
|
|
|
test("error responses use buildErrorBody and do not leak stack traces", () => {
|
|
// Verify that capability mismatch errors route through buildErrorBody
|
|
// (createErrorResult) and never contain stack traces.
|
|
import("../../open-sse/utils/error.ts").then(({ createErrorResult }) => {
|
|
const result = createErrorResult(
|
|
400,
|
|
"Provider 'test' does not support vision for this image request",
|
|
null,
|
|
"vision",
|
|
"invalid_request_error"
|
|
);
|
|
assert.equal(result.status, 400);
|
|
assert.equal(result.error, "Provider 'test' does not support vision for this image request");
|
|
assert.equal(result.errorType, "invalid_request_error");
|
|
assert.equal(result.errorCode, "vision");
|
|
|
|
// Parse the response body and assert no stack leak
|
|
result.response.text().then((text) => {
|
|
const body = JSON.parse(text);
|
|
assert.ok(body.error.message, "error message must exist");
|
|
assert.equal(body.error.message.includes("at /"), false, "must not leak stack traces");
|
|
assert.equal(body.error.code, "vision");
|
|
assert.equal(body.error.type, "invalid_request_error");
|
|
});
|
|
});
|
|
});
|