mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-02 21:32:10 +03:00
* chore(release): open v3.8.34 development cycle * chore(quality): release-green pre-flight validator + nightly signal (C+D) (#4622) C — scripts/quality/validate-release-green.mjs (npm run check:release-green): reproduces the release-equivalent validation (typecheck, eslint, db-rules, public-creds, full unit, vitest, ratchets, optional --with-build package-artifact) against the current working tree and classifies each red as HARD (real defect, exit 1) vs DRIFT (ratchet — reported, never affects exit / never blocks). Pure helpers exported + orchestration behind a direct-run guard; unit-tested. D — .github/workflows/nightly-release-green.yml: runs C on the active release branch nightly (and on workflow_dispatch) and opens/updates a single tracking issue on HARD failures. Never a required check, never touches a contributor PR. Closes the gap where the full gate (ci.yml) only ran on the release PR, so reds accrued silently on release/** and surfaced in 40-min layers at release time. Non-blocking by construction; drift is the maintainer's to rebaseline at release. Co-authored-by: Diego Rodrigues de Sa e Souza <diego.souza@cdwasolutions.com.br> * fix(providers): show revealed connection API keys (#4583) Integrated into release/v3.8.34 * fix(resilience): respect upstream retry hint toggle (#4585) Integrated into release/v3.8.34 * feat(settings): expose stream recovery feature flags (#4586) Integrated into release/v3.8.34 * fix(logs): make active request stale sweep configurable (#4599) Integrated into release/v3.8.34 * fix(plugin): auto-prefix providerId with 'opencode-' for OC 1.17.8+ native gate (#4527) Integrated into release/v3.8.34 (supersedes #4445) * fix(models): treat unknown output caps as unset (#4584) Integrated into release/v3.8.34 * fix(executors): strip temperature for GitHub Copilot gpt-5.4 family (#4564) Integrated into release/v3.8.34 (rebuilt onto tip) * fix(oauth): update Qwen OAuth URLs from chat.qwen.ai to qwen.ai (#4561) Integrated into release/v3.8.34 (rebuilt onto tip) * fix(api/settings): prevent cached /api/settings responses (port from 9router#951) (#4566) Integrated into release/v3.8.34 (rebuilt onto tip) * feat(audio): MiniMax T2A v2 TTS dispatch in audioSpeech (port #1043) (#4553) Integrated into release/v3.8.34 (rebuilt onto tip) * fix(dashboard): surface manual config CTA when Open Claw CLI auto-detect fails (#4562) Integrated into release/v3.8.34 (rebuilt onto tip) * feat(providers): optional model ID for custom API-key validation (#4555) Integrated into release/v3.8.34 (rebuilt onto tip) * fix(cli): align data dir and env loading with runtime (#4607) Integrated into release/v3.8.34 (rebuilt onto tip) * fix(quota): expose Bailian quota windows (#4610) Integrated into release/v3.8.34 (rebuilt onto tip) * fix: retain provider cooldowns for configured max window (#4588) Integrated into release/v3.8.34 (rebuilt — bundled commits stripped) * fix: reject invalid provider cooldown bounds (#4589) Integrated into release/v3.8.34 (rebuilt — bundled commits stripped) * fix: preserve production combo metrics on shadow eviction (#4590) Integrated into release/v3.8.34 (rebuilt — bundled commits stripped) * fix(stream): estimate input tokens when upstream reports prompt_tokens=0 (#4615) Integrated into release/v3.8.34 (rebuilt onto tip) * fix(catalog): shorten no-thinking gateway prefix to no-think/ (#4525) Integrated into release/v3.8.34 (rebuilt — kept only the prefix rename, dropped stale-base reverts) * fix(relay): apply IP rate limit to bifrost sidecar (#4593) Integrated into release/v3.8.34 (rebuilt onto tip; merge before #4612) * fix(bifrost): finalize SSE relay usage after stream (#4612) Integrated into release/v3.8.34 (rebuilt + reconciled with #4593) * feat(compression): per-request `x-omniroute-compression` header (Phase 3) (#4645) * docs(compression): Phase 3 per-request header design spec Approved brainstorming output for the x-omniroute-compression header: header-first precedence, name-first combo matching (Decision A), explicit value bypasses auto-trigger (Decision B), DerivedPlan.source, and the X-OmniRoute-Compression response header. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs(compression): Phase 3 per-request header implementation plan 4-task TDD plan (resolver header-first + source, parser, chatCore wiring + response header, docs/file-size) with full code and exact commands. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(compression): header-first resolver + plan source (Phase 3 core) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(compression): resolveCompressionHeader parser (Phase 3) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(compression): wire x-omniroute-compression header + response header (Phase 3) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * refactor(compression): extract plan-resolution leaf (planResolution.ts) under size cap (Phase 3) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs(compression): document x-omniroute-compression header (Phase 3) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(compression): harden named-combo map + trim engine: header id (Phase 3 review) Addresses gemini-code-assist review on #4645: - Extract buildNamedComboLookup (pure) so a blank/whitespace/null combo name contributes only its id key (no '' key, no throw that disables all combos). - Trim the engine:<id> header value so 'engine: rtk' resolves. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Diego Rodrigues de Sa e Souza <diego.souza@cdwasolutions.com.br> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Diego Rodrigues de Sa e Souza <souzamiriamrodrigues790@gmail.com> * fix: exclude exhausted connections from auto scoring (#4592) Integrated into release/v3.8.34 (rebuilt + opt-in gate fix) * fix(dashboard): memoize compatible provider groups (#4613) Integrated into release/v3.8.34 (rebuilt + test added) * fix(dashboard): isolate quota widget refresh clock (#4611) Integrated into release/v3.8.34 (rebuilt + jsdom test) * fix(dashboard): gate topology side effects behind widget visibility (#4606) Integrated into release/v3.8.34 (rebuilt + jsdom test) * fix(dashboard): keep play_arrow spinning on provider Test All buttons (#4563) Integrated into release/v3.8.34 (rebuilt onto tip; UI-cosmetic per owner) * fix(db): schedule retention cleanup + fix cleanup table/column names (extracted from #4428) (#4691) Integrated into release/v3.8.34 (cleanup core extracted from #4428, credit @oyi77) * fix(telemetry): back off live-WS event forwarding when the sidecar is unreachable (#4604) (#4687) Co-authored-by: Diego Rodrigues de Sa e Souza <souzamiriamrodrigues790@gmail.com> * fix(api): serve GET /v1/models/{model} as JSON, not the HTML dashboard (#4674) (#4677) Co-authored-by: Diego Rodrigues de Sa e Souza <souzamiriamrodrigues790@gmail.com> * feat(opencode): add go deepseek reasoning variants (#4647) Integrated into release/v3.8.34 * fix(executors): robust deepseek-web tool-call parsing and agentic context retention (#4644) Integrated into release/v3.8.34 * fix(cli): authenticate `omniroute logs` and honor active context (#4638) Integrated into release/v3.8.34 (authored by Rahul Sharma, AI co-author trailer stripped per project policy) * fix(proxy): apply pipelining:0 + connections cap to the direct dispatcher (#4580) (#4684) Co-authored-by: Diego Rodrigues de Sa e Souza <souzamiriamrodrigues790@gmail.com> * fix(executors): Firecrawl web_fetch 500 with include_metadata=true (#4692) Integrated into release/v3.8.34 * fix(routing): include all noAuth models in auto-combos + add reka-flash + best-free template (#4621) Integrated into release/v3.8.34 (dead getFirstRegistryModelId dropped, rebuilt onto tip) * fix(dashboard): gate home topology live-WS networking (#4596) (#4618) Integrated into release/v3.8.34 (adapted onto #4606's extracted topology section: default-hidden flip + enabled gate on useLiveDashboard) * fix(cli): align `omniroute` env loading with the runtime data dir (#4597) (#4619) Integrated into release/v3.8.34 (data-dir.mjs refactor reconciled with #4607; loadEnvFile aligned to getDefaultDataDir) * chore(quality): reconcile file-size baseline for #4644 (deepseek-web.ts 1117->1125) (#4695) file-size reconcile for #4644 * Support quota scraping for OpenCode Go and Ollama Cloud (#4642) Integrated into release/v3.8.34 (Ollama Cloud + OpenCode Go dashboard quota scraping; rebuilt onto tip, gates green: typecheck/public-creds/file-size/lint/docs-sync + 31 tests) * feat(executors): land M365 Copilot pure framing + connection helpers (#4042) (#4696) Land M365 pure modules ahead of draft #4400 * deps: bump production + development groups; migrate js-yaml to v5 ESM (#4697) Incorporates Dependabot #4667 + #4668 + js-yaml v5 ESM migration into release/v3.8.34 * fix: noAuth provider validation + kimi executor routing (#4699) Integrated into release/v3.8.34 (noAuth in NOAUTH_PROVIDERS dynamic check + remove misrouted kimi web alias; 9 tests) * refactor(imageGeneration): extract 8 provider families to co-located files (#4609) Integrated into release/v3.8.34 (extraction completed: added missing imports/exports per module, main imports handlers locally; 145 image-gen tests pass, typecheck/cycles/file-size green) * chore(release): v3.8.34 — finalize changelog, rebaseline drift, fix release-green reds - Finalize CHANGELOG [3.8.34] (43 bullets, full contributor attribution) + seed i18n mirrors - Rebaseline inherited cycle drift surfaced by release-green pre-flight: eslint warnings 3900->3907, cognitive-complexity 797->801 (release-finalize touches no prod code; all drift is from this cycle's contributor merges) - fix(providers): keep reka-flash-3 as the Reka provider default. #4621 inserted reka-flash at the head of the model list, silently changing the default from reka-flash-3 (the free-tier model) to reka-flash; reorder so reka-flash-3 stays default, reka-flash retained. - test: align provider-models-config / provider-models-route / web-cookie-providers-new with #4621 (reka-flash now in the Reka catalog) and #4699 (the `kimi` API-key provider correctly falls through to DefaultExecutor instead of KimiWebExecutor) - chore(quality): allowlist the COMPRESSION_GUIDE doc name in check-fabricated-docs (false-positive env-var match; docs/compression/COMPRESSION_GUIDE.md exists) * fix(release-green): resolve release-PR full-CI reds for v3.8.34 Surfaced only on the release PR (these gates don't run on PR->release fast-gates): - fix(quota): complete HTML-comment sanitization in opencodeOllamaUsage SSR reset-time parsing — strip any <!--...--> generically instead of the two literal React hydration markers, so no partial "<!--" can survive (CodeQL js/incomplete-multi-character- sanitization, HIGH, introduced by #4642). Regression test added. - test(codex): correct the Codex-fingerprint body key order assertion to match the canonical bodyFieldOrder (prompt_cache_key precedes include); #4584 flipped the two and integration tests don't run on fast-gates so it never executed until the release PR. - chore(quality): rebaseline inherited cycle drift surfaced by full CI — zizmorFindings 152->155 (+3 unpinned-uses in nightly-release-green.yml from #4622, same @vN convention as ci.yml) and openapiCoverage.pct 38.4->37.8 (-0.6, contributor routes added faster than openapi docs). Release-finalize touches no prod routes. * fix(release-green): complete CodeQL sanitization + rebaseline complexity drift - fix(quota): handle unterminated HTML comments in opencodeOllamaUsage SSR reset-time parsing — the `(?:-->|$)` arm consumes a trailing "<!--" with no closing "-->", so no partial "<!--" can survive (CodeQL js/incomplete-multi-character-sanitization persisted with the plain <!--...--> form because an unclosed comment could still leave "<!--"). - chore(quality): rebaseline cyclomatic complexity 1915->1916 (+1) — inherited v3.8.34 cycle drift (contributor feature branches); check:complexity does not run on PR->release fast-gates so it surfaced only on the release PR. Release-finalize adds 0 complexity (measured 1916 with/without the regex tweak). dead-code/cognitive/type-coverage/ compression-budget/codeql ratchets all pass. --------- Co-authored-by: Diego Rodrigues de Sa e Souza <diego.souza@cdwasolutions.com.br> Co-authored-by: Randi <55005611+rdself@users.noreply.github.com> Co-authored-by: Hernan Javier Ardila Sanchez <hjasgr@gmail.com> Co-authored-by: KooshaPari <42529354+KooshaPari@users.noreply.github.com> Co-authored-by: Abhishek Divekar <adivekar@utexas.edu> Co-authored-by: Rahul sharma <sharmaR0810@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Diego Rodrigues de Sa e Souza <souzamiriamrodrigues790@gmail.com> Co-authored-by: Ronald Estacion <DevEstacion@users.noreply.github.com> Co-authored-by: Igor <60442260+BugsBag@users.noreply.github.com> Co-authored-by: Oonishi <275808243+ponkcore@users.noreply.github.com> Co-authored-by: Paijo <14921983+oyi77@users.noreply.github.com> Co-authored-by: Jan Leon <Jan.gaschler@gmail.com>
1179 lines
38 KiB
TypeScript
1179 lines
38 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-cc-compatible-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const compliance = await import("../../src/lib/compliance/index.ts");
|
|
const providersDb = await import("../../src/lib/db/providers.ts");
|
|
const { DefaultExecutor } = await import("../../open-sse/executors/default.ts");
|
|
const {
|
|
buildClaudeCodeCompatibleRequest,
|
|
CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH,
|
|
CLAUDE_CODE_COMPATIBLE_DEFAULT_MAX_TOKENS,
|
|
CLAUDE_CODE_COMPATIBLE_DEFAULT_MODELS_PATH,
|
|
joinClaudeCodeCompatibleUrl,
|
|
} = await import("../../open-sse/services/claudeCodeCompatible.ts");
|
|
const { getModelsByProviderId, supportsXHighEffort } =
|
|
await import("../../open-sse/config/providerModels.ts");
|
|
const { handleChatCore } = await import("../../open-sse/handlers/chatCore.ts");
|
|
const { validateProviderApiKey } = await import("../../src/lib/providers/validation.ts");
|
|
const providerNodesRoute = await import("../../src/app/api/provider-nodes/route.ts");
|
|
const providerNodesValidateRoute =
|
|
await import("../../src/app/api/provider-nodes/validate/route.ts");
|
|
const providerModelsRoute = await import("../../src/app/api/providers/[id]/models/route.ts");
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const originalFlag = process.env.ENABLE_CC_COMPATIBLE_PROVIDER;
|
|
const originalAllowPrivateProviderUrls = process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS;
|
|
|
|
async function resetStorage() {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
test.afterEach(async () => {
|
|
globalThis.fetch = originalFetch;
|
|
if (originalFlag === undefined) {
|
|
delete process.env.ENABLE_CC_COMPATIBLE_PROVIDER;
|
|
} else {
|
|
process.env.ENABLE_CC_COMPATIBLE_PROVIDER = originalFlag;
|
|
}
|
|
if (originalAllowPrivateProviderUrls === undefined) {
|
|
delete process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS;
|
|
} else {
|
|
process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS = originalAllowPrivateProviderUrls;
|
|
}
|
|
await resetStorage();
|
|
});
|
|
|
|
test.after(() => {
|
|
globalThis.fetch = originalFetch;
|
|
if (originalFlag === undefined) {
|
|
delete process.env.ENABLE_CC_COMPATIBLE_PROVIDER;
|
|
} else {
|
|
process.env.ENABLE_CC_COMPATIBLE_PROVIDER = originalFlag;
|
|
}
|
|
if (originalAllowPrivateProviderUrls === undefined) {
|
|
delete process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS;
|
|
} else {
|
|
process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS = originalAllowPrivateProviderUrls;
|
|
}
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
});
|
|
|
|
test("buildClaudeCodeCompatibleRequest keeps prior role history while dropping trailing assistant prefill", () => {
|
|
const payload = buildClaudeCodeCompatibleRequest({
|
|
sourceBody: {
|
|
reasoning_effort: "xhigh",
|
|
tool_choice: "required",
|
|
},
|
|
normalizedBody: {
|
|
tools: [
|
|
{
|
|
type: "function",
|
|
function: {
|
|
name: "lookup_weather",
|
|
description: "Fetch weather",
|
|
parameters: {
|
|
type: "object",
|
|
properties: {
|
|
city: { type: "string" },
|
|
},
|
|
required: ["city"],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
messages: [
|
|
{ role: "system", content: "sys" },
|
|
{ role: "user", content: [{ type: "text", text: "u1" }, { type: "image_url" }] },
|
|
{ role: "model", content: "a1" },
|
|
{ role: "user", content: [{ type: "text", text: "u2" }, { type: "tool_result" }] },
|
|
{ role: "model", content: "prefill" },
|
|
],
|
|
},
|
|
model: "claude-sonnet-4-6",
|
|
cwd: "/tmp/work",
|
|
now: new Date("2026-04-01T12:00:00.000Z"),
|
|
sessionId: "session-1",
|
|
});
|
|
|
|
assert.equal(payload.max_tokens, CLAUDE_CODE_COMPATIBLE_DEFAULT_MAX_TOKENS);
|
|
assert.equal(payload.output_config.effort, "high");
|
|
assert.deepEqual(
|
|
payload.messages.map((message) => ({
|
|
role: message.role,
|
|
text: message.content.map((block) => block.text).join("\n"),
|
|
})),
|
|
[
|
|
{ role: "user", text: "u1" },
|
|
{ role: "assistant", text: "a1" },
|
|
{ role: "user", text: "u2" },
|
|
]
|
|
);
|
|
assert.equal((payload.messages[0].content.at(-1) as any).cache_control, undefined);
|
|
assert.equal((payload.messages[1] as any).content.at(-1).cache_control, undefined);
|
|
assert.equal((payload.messages as any)[2].content.at(-1).cache_control, undefined);
|
|
assert.equal(payload.system.length, 2);
|
|
assert.match((payload as any).system[0].text, /Claude Agent SDK/);
|
|
(assert as any).equal((payload.system[0] as any).cache_control, undefined);
|
|
assert.equal((payload.system[1] as any).cache_control, undefined);
|
|
assert.equal(payload.system[1].text, "sys");
|
|
assert.equal((payload.system[1] as any).cache_control, undefined);
|
|
assert.equal(payload.tools.length, 1);
|
|
assert.deepEqual(payload.tools[0], {
|
|
name: "lookup_weather",
|
|
description: "Fetch weather",
|
|
input_schema: {
|
|
type: "object",
|
|
properties: {
|
|
city: { type: "string" },
|
|
},
|
|
required: ["city"],
|
|
},
|
|
});
|
|
assert.deepEqual(payload.tool_choice, { type: "any" });
|
|
assert.equal(payload.context_management, undefined as any);
|
|
assert.equal(JSON.parse((payload as any).metadata.user_id).session_id, "session-1");
|
|
});
|
|
|
|
test("buildClaudeCodeCompatibleRequest preserves xhigh for Claude models that support it", () => {
|
|
const xhighModel = getModelsByProviderId("claude").find((model) =>
|
|
supportsXHighEffort("claude", model.id)
|
|
);
|
|
assert.ok(xhighModel, "expected at least one Claude model with xhigh support");
|
|
const payload = buildClaudeCodeCompatibleRequest({
|
|
sourceBody: {
|
|
reasoning_effort: "xhigh",
|
|
},
|
|
normalizedBody: {
|
|
messages: [{ role: "user", content: "u1" }],
|
|
},
|
|
model: xhighModel.id,
|
|
cwd: "/tmp/work",
|
|
now: new Date("2026-04-01T12:00:00.000Z"),
|
|
});
|
|
|
|
assert.equal(payload.output_config.effort, "xhigh");
|
|
assert.equal(payload.thinking.type, "adaptive");
|
|
});
|
|
|
|
test("buildClaudeCodeCompatibleRequest preserves Claude cache markers when requested", () => {
|
|
const payload = buildClaudeCodeCompatibleRequest({
|
|
sourceBody: {
|
|
max_tokens: 64,
|
|
},
|
|
normalizedBody: {
|
|
max_tokens: 64,
|
|
messages: [{ role: "user", content: "fallback" }],
|
|
},
|
|
claudeBody: {
|
|
system: [{ type: "text", text: "sys", cache_control: { type: "ephemeral", ttl: "5m" } }],
|
|
messages: [
|
|
{
|
|
role: "user",
|
|
content: [{ type: "text", text: "u1", cache_control: { type: "ephemeral" } }],
|
|
},
|
|
{
|
|
role: "assistant",
|
|
content: [
|
|
{
|
|
type: "text",
|
|
text: "a1",
|
|
cache_control: { type: "ephemeral", ttl: "10m" },
|
|
},
|
|
],
|
|
},
|
|
{ role: "user", content: [{ type: "text", text: "u2" }] },
|
|
],
|
|
tools: [
|
|
{
|
|
name: "lookup_weather",
|
|
description: "Fetch weather",
|
|
input_schema: {
|
|
type: "object",
|
|
properties: {
|
|
city: { type: "string" },
|
|
},
|
|
required: ["city"],
|
|
},
|
|
cache_control: { type: "ephemeral", ttl: "30m" },
|
|
},
|
|
],
|
|
},
|
|
model: "claude-sonnet-4-6",
|
|
sessionId: "session-preserve",
|
|
preserveCacheControl: true,
|
|
});
|
|
|
|
assert.match((payload.system[0] as any).text, /Claude Agent SDK/);
|
|
assert.equal((payload.system[0] as any).cache_control, undefined);
|
|
assert.deepEqual((payload.system[1] as any).cache_control, { type: "ephemeral", ttl: "5m" });
|
|
(assert as any).deepEqual((payload.messages[0].content[0] as any).cache_control, {
|
|
type: "ephemeral",
|
|
});
|
|
assert.deepEqual((payload.messages[1].content[0] as any).cache_control, {
|
|
type: "ephemeral",
|
|
ttl: "10m",
|
|
});
|
|
assert.equal((payload.messages[2].content[0] as any).cache_control, undefined);
|
|
assert.deepEqual(payload.tools[0].cache_control, { type: "ephemeral", ttl: "30m" });
|
|
});
|
|
|
|
test("buildClaudeCodeCompatibleRequest does not supplement missing Claude cache markers in preserve mode", () => {
|
|
const payload = buildClaudeCodeCompatibleRequest({
|
|
sourceBody: {
|
|
max_tokens: 64,
|
|
},
|
|
normalizedBody: {
|
|
max_tokens: 64,
|
|
messages: [{ role: "user", content: "fallback" }],
|
|
},
|
|
claudeBody: {
|
|
system: [{ type: "text", text: "sys" }],
|
|
messages: [
|
|
{
|
|
role: "user",
|
|
content: [{ type: "text", text: "u1" }],
|
|
},
|
|
{
|
|
role: "assistant",
|
|
content: [{ type: "text", text: "a1" }],
|
|
},
|
|
{ role: "user", content: [{ type: "text", text: "u2" }] },
|
|
],
|
|
tools: [
|
|
{
|
|
name: "lookup_weather",
|
|
description: "Fetch weather",
|
|
input_schema: {
|
|
type: "object",
|
|
properties: {
|
|
city: { type: "string" },
|
|
},
|
|
required: ["city"],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
model: "claude-sonnet-4-6",
|
|
sessionId: "session-preserve-defaults",
|
|
preserveCacheControl: true,
|
|
});
|
|
|
|
assert.equal((payload.system[0] as any).cache_control, undefined);
|
|
assert.equal((payload.messages[0].content[0] as any).cache_control, undefined);
|
|
assert.equal((payload.messages[1].content[0] as any).cache_control, undefined);
|
|
assert.equal((payload.messages[2].content[0] as any).cache_control, undefined);
|
|
assert.equal((payload.system.at(-1) as any).cache_control, undefined);
|
|
assert.equal(payload.tools[0].cache_control, undefined);
|
|
});
|
|
|
|
test("buildClaudeCodeCompatibleRequest keeps built-in system blocks untagged when preserved system uses 1h", () => {
|
|
const payload = buildClaudeCodeCompatibleRequest({
|
|
sourceBody: {
|
|
max_tokens: 64,
|
|
},
|
|
normalizedBody: {
|
|
max_tokens: 64,
|
|
messages: [{ role: "user", content: "fallback" }],
|
|
},
|
|
claudeBody: {
|
|
system: [
|
|
{ type: "text", text: "prefix", cache_control: { type: "ephemeral" } },
|
|
{ type: "text", text: "stable", cache_control: { type: "ephemeral", ttl: "1h" } },
|
|
],
|
|
messages: [{ role: "user", content: [{ type: "text", text: "u1" }] }],
|
|
tools: [],
|
|
},
|
|
model: "claude-sonnet-4-6",
|
|
sessionId: "session-preserve-system-upgrade",
|
|
preserveCacheControl: true,
|
|
});
|
|
|
|
assert.match((payload.system[0] as any).text, /Claude Agent SDK/);
|
|
assert.equal((payload.system[0] as any).cache_control, undefined);
|
|
assert.deepEqual((payload.system[1] as any).cache_control, { type: "ephemeral" });
|
|
assert.deepEqual((payload.system[2] as any).cache_control, { type: "ephemeral", ttl: "1h" });
|
|
});
|
|
|
|
test("buildClaudeCodeCompatibleRequest does not add cache markers in non-preserve mode", () => {
|
|
const largeUserPrompt = Array.from(
|
|
{ length: 200 },
|
|
(_, index) => `Context line ${index + 1}: repeated stable context for cache testing.`
|
|
).join("\n");
|
|
|
|
const payload = buildClaudeCodeCompatibleRequest({
|
|
sourceBody: {
|
|
max_tokens: 64,
|
|
},
|
|
normalizedBody: {
|
|
max_tokens: 64,
|
|
messages: [
|
|
{ role: "system", content: "Follow the house style exactly." },
|
|
{ role: "user", content: "[Start a new chat]" },
|
|
{ role: "assistant", content: "Hello short ack" },
|
|
{ role: "user", content: largeUserPrompt },
|
|
],
|
|
},
|
|
model: "claude-sonnet-4-6",
|
|
sessionId: "session-last-user-cache",
|
|
preserveCacheControl: false,
|
|
});
|
|
|
|
(assert as any).equal(payload.system.length, 2);
|
|
assert.equal((payload as any).system[0].cache_control, undefined);
|
|
assert.equal((payload as any).system[1].cache_control, undefined);
|
|
assert.equal((payload as any).messages[0].content[0].cache_control, undefined);
|
|
assert.equal((payload as any).messages[1].content[0].cache_control, undefined);
|
|
assert.equal((payload.messages[2].content[0] as any).cache_control, undefined);
|
|
});
|
|
|
|
test("buildClaudeCodeCompatibleRequest falls back to a user turn when the source only has assistant/model text", () => {
|
|
const payload = buildClaudeCodeCompatibleRequest({
|
|
sourceBody: {
|
|
messages: [{ role: "model", content: "draft" }],
|
|
},
|
|
normalizedBody: {
|
|
messages: [{ role: "model", content: "draft" }],
|
|
},
|
|
model: "claude-sonnet-4-6",
|
|
sessionId: "session-only-assistant",
|
|
});
|
|
|
|
assert.deepEqual(payload.messages, [
|
|
{
|
|
role: "user",
|
|
content: [{ type: "text", text: "draft" }],
|
|
},
|
|
]);
|
|
});
|
|
|
|
test("buildClaudeCodeCompatibleRequest honors token priority fields", () => {
|
|
const payload = buildClaudeCodeCompatibleRequest({
|
|
sourceBody: { max_completion_tokens: 321 },
|
|
normalizedBody: {
|
|
max_tokens: 123,
|
|
max_output_tokens: 456,
|
|
messages: [{ role: "user", content: "hi" }],
|
|
},
|
|
model: "claude-sonnet-4-6",
|
|
sessionId: "session-2",
|
|
});
|
|
|
|
assert.equal(payload.max_tokens, 321);
|
|
assert.deepEqual(payload.tools, []);
|
|
assert.equal(payload.tool_choice, undefined);
|
|
});
|
|
|
|
test("buildClaudeCodeCompatibleRequest omits auto tool_choice while preserving tools", () => {
|
|
const payload = buildClaudeCodeCompatibleRequest({
|
|
sourceBody: { tool_choice: "auto" },
|
|
normalizedBody: {
|
|
messages: [{ role: "user", content: "hi" }],
|
|
tools: [
|
|
{
|
|
type: "function",
|
|
function: {
|
|
name: "ping",
|
|
parameters: { type: "object" },
|
|
},
|
|
},
|
|
],
|
|
},
|
|
model: "claude-sonnet-4-6",
|
|
sessionId: "session-4",
|
|
});
|
|
|
|
assert.equal(payload.tools.length, 1);
|
|
assert.equal(payload.tools[0].cache_control, undefined);
|
|
assert.equal(payload.tool_choice, undefined);
|
|
});
|
|
|
|
test("joinClaudeCodeCompatibleUrl preserves a single /v1 segment for CC paths", () => {
|
|
assert.equal(
|
|
joinClaudeCodeCompatibleUrl(
|
|
"https://proxy.example.com",
|
|
CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH
|
|
),
|
|
"https://proxy.example.com/v1/messages?beta=true"
|
|
);
|
|
assert.equal(
|
|
joinClaudeCodeCompatibleUrl(
|
|
"https://proxy.example.com/v1",
|
|
CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH
|
|
),
|
|
"https://proxy.example.com/v1/messages?beta=true"
|
|
);
|
|
assert.equal(
|
|
joinClaudeCodeCompatibleUrl(
|
|
"https://proxy.example.com/v1/messages?beta=true",
|
|
CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH
|
|
),
|
|
"https://proxy.example.com/v1/messages?beta=true"
|
|
);
|
|
});
|
|
|
|
test("DefaultExecutor uses CC-compatible path and headers", () => {
|
|
const executor = new DefaultExecutor("anthropic-compatible-cc-test");
|
|
const credentials = {
|
|
apiKey: "sk-test",
|
|
providerSpecificData: {
|
|
baseUrl: "https://proxy.example.com/v1/",
|
|
chatPath: "",
|
|
ccSessionId: "session-3",
|
|
},
|
|
};
|
|
|
|
assert.equal(
|
|
executor.buildUrl("claude-sonnet-4-6", true, 0, credentials),
|
|
"https://proxy.example.com/v1/messages?beta=true"
|
|
);
|
|
|
|
const headers = executor.buildHeaders(credentials, true);
|
|
assert.equal(headers.Authorization, "Bearer sk-test");
|
|
assert.equal(headers["x-api-key"], undefined);
|
|
assert.equal(headers["X-Claude-Code-Session-Id"], "session-3");
|
|
assert.equal(headers.Accept, "application/json");
|
|
});
|
|
|
|
test("validateProviderApiKey uses CC skeleton request after /models fallback", async () => {
|
|
const calls = [];
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
calls.push({
|
|
url: String(url),
|
|
method: init.method || "GET",
|
|
headers: init.headers,
|
|
body: init.body ? JSON.parse(String(init.body)) : null,
|
|
});
|
|
|
|
if (String(url).endsWith(CLAUDE_CODE_COMPATIBLE_DEFAULT_MODELS_PATH)) {
|
|
return new Response(JSON.stringify({ error: "missing models" }), { status: 500 });
|
|
}
|
|
|
|
return new Response(JSON.stringify({ error: "bad model" }), { status: 400 });
|
|
};
|
|
|
|
const result = await validateProviderApiKey({
|
|
provider: "anthropic-compatible-cc-test",
|
|
apiKey: "sk-test",
|
|
providerSpecificData: {
|
|
baseUrl: "https://proxy.example.com/v1/messages?beta=true",
|
|
validationModelId: "claude-sonnet-4-6",
|
|
},
|
|
});
|
|
|
|
assert.equal(result.valid, true);
|
|
assert.equal(result.method, "cc_bridge_request");
|
|
assert.match(result.warning, /reached upstream/i);
|
|
assert.deepEqual(
|
|
calls.map((call) => `${call.method} ${call.url}`),
|
|
["GET https://proxy.example.com/models", "POST https://proxy.example.com/v1/messages?beta=true"]
|
|
);
|
|
assert.equal(calls[1].body.model, "claude-sonnet-4-6");
|
|
assert.equal(calls[1].body.messages[0].role, "user");
|
|
assert.equal(calls[1].body.stream, true);
|
|
assert.equal(calls[1].headers.Authorization, "Bearer sk-test");
|
|
assert.equal(calls[1].headers["x-api-key"], undefined);
|
|
assert.equal(calls[1].headers.Accept, "application/json");
|
|
});
|
|
|
|
test("handleChatCore forces SSE upstream for CC compatible providers while returning JSON to non-stream clients", async () => {
|
|
const calls = [];
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
calls.push({
|
|
url: String(url),
|
|
method: init.method || "GET",
|
|
headers: init.headers,
|
|
body: init.body ? JSON.parse(String(init.body)) : null,
|
|
});
|
|
|
|
return new Response(
|
|
[
|
|
"event: message_start",
|
|
'data: {"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","model":"claude-sonnet-4-6","usage":{"input_tokens":7,"output_tokens":0}}}',
|
|
"",
|
|
"event: content_block_start",
|
|
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}',
|
|
"",
|
|
"event: content_block_delta",
|
|
'data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello from CC"}}',
|
|
"",
|
|
"event: message_delta",
|
|
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5}}',
|
|
"",
|
|
"event: message_stop",
|
|
'data: {"type":"message_stop"}',
|
|
"",
|
|
].join("\n"),
|
|
{
|
|
status: 200,
|
|
headers: {
|
|
"content-type": "text/event-stream",
|
|
},
|
|
}
|
|
);
|
|
};
|
|
|
|
const result = await handleChatCore({
|
|
body: {
|
|
model: "claude-sonnet-4-6",
|
|
messages: [{ role: "user", content: "Ping" }],
|
|
stream: false,
|
|
},
|
|
modelInfo: {
|
|
provider: "anthropic-compatible-cc-test",
|
|
model: "claude-sonnet-4-6",
|
|
extendedContext: false,
|
|
},
|
|
credentials: {
|
|
apiKey: "sk-test",
|
|
providerSpecificData: {
|
|
baseUrl: "https://proxy.example.com",
|
|
chatPath: CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH,
|
|
},
|
|
},
|
|
clientRawRequest: {
|
|
endpoint: "/v1/chat/completions",
|
|
body: {
|
|
model: "claude-sonnet-4-6",
|
|
messages: [{ role: "user", content: "Ping" }],
|
|
stream: false,
|
|
},
|
|
headers: new Headers({ accept: "application/json" }),
|
|
},
|
|
userAgent: "unit-test",
|
|
log: {
|
|
debug() {},
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
});
|
|
|
|
assert.equal(result.success, true);
|
|
assert.equal(calls.length, 1);
|
|
assert.equal(calls[0].headers.Accept, "application/json");
|
|
assert.equal(calls[0].body.stream, true);
|
|
assert.equal(calls[0].body.stream_options, undefined);
|
|
assert.equal(JSON.stringify(calls[0].body).includes('"cache_control"'), false);
|
|
|
|
const payload = (await result.response.json()) as any;
|
|
assert.equal(payload.choices[0].message.content, "Hello from CC");
|
|
assert.equal(payload.choices[0].finish_reason, "stop");
|
|
assert.equal(payload.usage.prompt_tokens, 2007);
|
|
assert.equal(payload.usage.completion_tokens, 5);
|
|
});
|
|
|
|
test("handleChatCore stops buffering CC-compatible SSE once a non-stream response completes", async () => {
|
|
const encoder = new TextEncoder();
|
|
let upstreamCancelled = false;
|
|
const upstreamChunks = [
|
|
"data:\n\n",
|
|
[
|
|
"event: message_start",
|
|
'data: {"type":"message_start","message":{"id":"msg_3","type":"message","role":"assistant","model":"claude-sonnet-4-6","usage":{"input_tokens":4,"output_tokens":0}}}',
|
|
"",
|
|
"event: content_block_delta",
|
|
'data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Finished but connection stayed open"}}',
|
|
"",
|
|
"event: message_delta",
|
|
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":6}}',
|
|
"",
|
|
"event: message_stop",
|
|
'data: {"type":"message_stop"}',
|
|
"",
|
|
].join("\n"),
|
|
];
|
|
let chunkIndex = 0;
|
|
|
|
globalThis.fetch = async () =>
|
|
new Response(
|
|
new ReadableStream<Uint8Array>({
|
|
pull(controller) {
|
|
if (chunkIndex < upstreamChunks.length) {
|
|
controller.enqueue(encoder.encode(upstreamChunks[chunkIndex++]));
|
|
}
|
|
},
|
|
cancel() {
|
|
upstreamCancelled = true;
|
|
},
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: {
|
|
"content-type": "text/event-stream",
|
|
},
|
|
}
|
|
);
|
|
|
|
const result = await handleChatCore({
|
|
body: {
|
|
model: "claude-sonnet-4-6",
|
|
messages: [{ role: "user", content: "Ping" }],
|
|
stream: false,
|
|
},
|
|
modelInfo: {
|
|
provider: "anthropic-compatible-cc-test",
|
|
model: "claude-sonnet-4-6",
|
|
extendedContext: false,
|
|
},
|
|
credentials: {
|
|
apiKey: "sk-test",
|
|
providerSpecificData: {
|
|
baseUrl: "https://proxy.example.com",
|
|
chatPath: CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH,
|
|
},
|
|
},
|
|
clientRawRequest: {
|
|
endpoint: "/v1/chat/completions",
|
|
body: {
|
|
model: "claude-sonnet-4-6",
|
|
messages: [{ role: "user", content: "Ping" }],
|
|
stream: false,
|
|
},
|
|
headers: new Headers({ accept: "application/json" }),
|
|
},
|
|
userAgent: "unit-test",
|
|
log: {
|
|
debug() {},
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
});
|
|
|
|
assert.equal(result.success, true);
|
|
assert.equal(upstreamCancelled, true);
|
|
const payload = (await result.response.json()) as any;
|
|
assert.equal(payload.choices[0].message.content, "Finished but connection stayed open");
|
|
assert.equal(payload.usage.completion_tokens, 6);
|
|
});
|
|
|
|
test("handleChatCore preserves client cache markers for Claude Code requests to CC-compatible providers", async () => {
|
|
const calls = [];
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
calls.push({
|
|
url: String(url),
|
|
method: init.method || "GET",
|
|
headers: init.headers,
|
|
body: init.body ? JSON.parse(String(init.body)) : null,
|
|
});
|
|
|
|
return new Response(
|
|
[
|
|
"event: message_start",
|
|
'data: {"type":"message_start","message":{"id":"msg_2","type":"message","role":"assistant","model":"claude-sonnet-4-6","usage":{"input_tokens":12,"output_tokens":0}}}',
|
|
"",
|
|
"event: content_block_start",
|
|
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}',
|
|
"",
|
|
"event: content_block_delta",
|
|
'data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Preserved"}}',
|
|
"",
|
|
"event: message_delta",
|
|
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":3}}',
|
|
"",
|
|
"event: message_stop",
|
|
'data: {"type":"message_stop"}',
|
|
"",
|
|
].join("\n"),
|
|
{
|
|
status: 200,
|
|
headers: {
|
|
"content-type": "text/event-stream",
|
|
},
|
|
}
|
|
);
|
|
};
|
|
|
|
const claudeBody = {
|
|
model: "claude-sonnet-4-6",
|
|
max_tokens: 64,
|
|
system: [{ type: "text", text: "system", cache_control: { type: "ephemeral", ttl: "5m" } }],
|
|
messages: [
|
|
{
|
|
role: "user",
|
|
content: [{ type: "text", text: "u1", cache_control: { type: "ephemeral" } }],
|
|
},
|
|
{
|
|
role: "assistant",
|
|
content: [
|
|
{
|
|
type: "text",
|
|
text: "a1",
|
|
cache_control: { type: "ephemeral", ttl: "10m" },
|
|
},
|
|
],
|
|
},
|
|
{ role: "user", content: [{ type: "text", text: "u2" }] },
|
|
],
|
|
tools: [
|
|
{
|
|
name: "lookup_weather",
|
|
description: "Fetch weather",
|
|
input_schema: {
|
|
type: "object",
|
|
properties: {
|
|
city: { type: "string" },
|
|
},
|
|
},
|
|
cache_control: { type: "ephemeral", ttl: "30m" },
|
|
},
|
|
],
|
|
};
|
|
|
|
const result = await handleChatCore({
|
|
body: claudeBody,
|
|
modelInfo: {
|
|
provider: "anthropic-compatible-cc-test",
|
|
model: "claude-sonnet-4-6",
|
|
extendedContext: false,
|
|
},
|
|
credentials: {
|
|
apiKey: "sk-test",
|
|
providerSpecificData: {
|
|
baseUrl: "https://proxy.example.com",
|
|
chatPath: CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH,
|
|
},
|
|
},
|
|
clientRawRequest: {
|
|
endpoint: "/v1/messages",
|
|
body: claudeBody,
|
|
headers: new Headers({ accept: "application/json" }),
|
|
},
|
|
userAgent: "Claude-Code/1.0.0",
|
|
log: {
|
|
debug() {},
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
});
|
|
|
|
assert.equal(result.success, true);
|
|
assert.equal(calls.length, 1);
|
|
assert.match(calls[0].body.system[0].text, /Claude Agent SDK/);
|
|
assert.equal(calls[0].body.system[0].cache_control, undefined);
|
|
assert.deepEqual(calls[0].body.system[1].cache_control, {
|
|
type: "ephemeral",
|
|
ttl: "5m",
|
|
});
|
|
assert.deepEqual(calls[0].body.messages[0].content[0].cache_control, {
|
|
type: "ephemeral",
|
|
});
|
|
assert.deepEqual(calls[0].body.messages[1].content[0].cache_control, {
|
|
type: "ephemeral",
|
|
ttl: "10m",
|
|
});
|
|
assert.equal(calls[0].body.messages[2].content[0].cache_control, undefined);
|
|
assert.deepEqual(calls[0].body.tools[0].cache_control, {
|
|
type: "ephemeral",
|
|
ttl: "30m",
|
|
});
|
|
});
|
|
|
|
test("provider-nodes create route rejects CC mode when feature flag is disabled", async () => {
|
|
delete process.env.ENABLE_CC_COMPATIBLE_PROVIDER;
|
|
|
|
const response = await providerNodesRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
name: "Hidden CC",
|
|
prefix: "cc",
|
|
baseUrl: "https://proxy.example.com/v1",
|
|
type: "anthropic-compatible",
|
|
compatMode: "cc",
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(response.status, 403);
|
|
});
|
|
|
|
test("provider-nodes create route creates CC node with dedicated prefix when enabled", async () => {
|
|
process.env.ENABLE_CC_COMPATIBLE_PROVIDER = "true";
|
|
|
|
const response = await providerNodesRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
name: "Hidden CC",
|
|
prefix: "cc",
|
|
baseUrl: "https://proxy.example.com/v1/messages?beta=true",
|
|
type: "anthropic-compatible",
|
|
compatMode: "cc",
|
|
chatPath: CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH,
|
|
modelsPath: CLAUDE_CODE_COMPATIBLE_DEFAULT_MODELS_PATH,
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(response.status, 201);
|
|
const data = (await response.json()) as any;
|
|
assert.match(data.node.id, /^anthropic-compatible-cc-/);
|
|
assert.equal(data.node.baseUrl, "https://proxy.example.com");
|
|
assert.equal(data.node.chatPath, CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH);
|
|
assert.equal(data.node.modelsPath, null);
|
|
});
|
|
|
|
test("provider-nodes validate route rejects CC mode when feature flag is disabled", async () => {
|
|
delete process.env.ENABLE_CC_COMPATIBLE_PROVIDER;
|
|
|
|
const response = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
baseUrl: "https://proxy.example.com/v1",
|
|
apiKey: "sk-test",
|
|
type: "anthropic-compatible",
|
|
compatMode: "cc",
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(response.status, 403);
|
|
});
|
|
|
|
test("provider-nodes validate route rejects invalid JSON and schema errors", async () => {
|
|
const invalidJsonResponse = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: "{",
|
|
})
|
|
);
|
|
|
|
assert.equal(invalidJsonResponse.status, 400);
|
|
assert.deepEqual(await invalidJsonResponse.json(), {
|
|
error: {
|
|
message: "Invalid request",
|
|
details: [{ field: "body", message: "Invalid JSON body" }],
|
|
},
|
|
});
|
|
|
|
const invalidBodyResponse = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
baseUrl: "",
|
|
apiKey: "",
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(invalidBodyResponse.status, 400);
|
|
const invalidBodyPayload = (await invalidBodyResponse.json()) as any;
|
|
assert.equal(invalidBodyPayload.error.message, "Invalid request");
|
|
assert.equal(invalidBodyPayload.error.details.length >= 1, true);
|
|
});
|
|
|
|
test("provider-nodes validate route blocks private provider hosts before fetch", async () => {
|
|
delete process.env.OMNIROUTE_ALLOW_PRIVATE_PROVIDER_URLS;
|
|
|
|
let called = false;
|
|
globalThis.fetch = async () => {
|
|
called = true;
|
|
return Response.json({ data: [] });
|
|
};
|
|
|
|
const response = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
baseUrl: "http://127.0.0.1:11434/v1",
|
|
apiKey: "sk-private-test",
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(response.status, 503);
|
|
assert.deepEqual(await response.json(), {
|
|
error: "Blocked private or local provider URL",
|
|
});
|
|
assert.equal(called, false);
|
|
const auditEntries = compliance.getAuditLog({
|
|
action: "provider.validation.ssrf_blocked",
|
|
resourceType: "provider_validation",
|
|
});
|
|
assert.equal(auditEntries.length, 1);
|
|
assert.equal(auditEntries[0].target, "provider-node");
|
|
assert.equal(auditEntries[0].status, "blocked");
|
|
assert.deepEqual(auditEntries[0].metadata, {
|
|
route: "/api/provider-nodes/validate",
|
|
reason: "Blocked private or local provider URL",
|
|
baseUrl: "http://127.0.0.1:11434/v1",
|
|
});
|
|
});
|
|
|
|
test("provider-nodes validate route validates anthropic compatible providers against the models endpoint", async () => {
|
|
const calls = [];
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
calls.push({ url, init });
|
|
return new Response(JSON.stringify({ data: [] }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
};
|
|
|
|
const response = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
baseUrl: "https://proxy.example.com/v1/messages?beta=true",
|
|
apiKey: "sk-anthropic-test",
|
|
type: "anthropic-compatible",
|
|
modelsPath: "/catalog",
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.deepEqual(await response.json(), {
|
|
valid: true,
|
|
error: null,
|
|
});
|
|
assert.equal(calls.length, 1);
|
|
assert.equal(calls[0].url, "https://proxy.example.com/v1/catalog");
|
|
assert.equal(calls[0].init.method, "GET");
|
|
assert.equal(calls[0].init.headers["x-api-key"], "sk-anthropic-test");
|
|
assert.equal(calls[0].init.headers["anthropic-version"], "2023-06-01");
|
|
assert.equal(calls[0].init.headers.Authorization, "Bearer sk-anthropic-test");
|
|
});
|
|
|
|
test("provider-nodes validate route supports enabled CC validation and OpenAI-style failures", async () => {
|
|
process.env.ENABLE_CC_COMPATIBLE_PROVIDER = "true";
|
|
|
|
const calls = [];
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
calls.push({ url, init });
|
|
if (calls.length === 1) {
|
|
return new Response(JSON.stringify({ data: [] }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}
|
|
return new Response(JSON.stringify({ error: "Unauthorized" }), {
|
|
status: 401,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
};
|
|
|
|
const ccResponse = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
baseUrl: "https://proxy.example.com/v1/messages?beta=true",
|
|
apiKey: "sk-cc-test",
|
|
type: "anthropic-compatible",
|
|
compatMode: "cc",
|
|
chatPath: CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH,
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(ccResponse.status, 200);
|
|
assert.deepEqual(await ccResponse.json(), {
|
|
valid: true,
|
|
error: null,
|
|
warning: null,
|
|
method: "models_endpoint",
|
|
});
|
|
assert.equal(String(calls[0].url).includes("/v1/messages"), false);
|
|
assert.equal(calls[0].init.method, "GET");
|
|
|
|
const openAiResponse = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
baseUrl: "https://proxy.example.com/",
|
|
apiKey: "sk-openai-test",
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(openAiResponse.status, 200);
|
|
assert.deepEqual(await openAiResponse.json(), {
|
|
valid: false,
|
|
error: "API key unauthorized",
|
|
});
|
|
assert.equal(calls[1].url, "https://proxy.example.com/models");
|
|
assert.equal(calls[1].init.headers.Authorization, "Bearer sk-openai-test");
|
|
});
|
|
|
|
test("provider-nodes validate route covers default CC paths, null method, anthropic failures, and OpenAI success", async () => {
|
|
process.env.ENABLE_CC_COMPATIBLE_PROVIDER = "true";
|
|
|
|
const ccCalls = [];
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
ccCalls.push({ url, init });
|
|
if (ccCalls.length === 1) {
|
|
throw new Error("models unavailable");
|
|
}
|
|
return new Response(JSON.stringify({ error: "Unauthorized" }), {
|
|
status: 401,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
};
|
|
|
|
const ccResponse = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
baseUrl: "https://proxy.example.com/v1/messages",
|
|
apiKey: "sk-cc-invalid",
|
|
type: "anthropic-compatible",
|
|
compatMode: "cc",
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(ccResponse.status, 200);
|
|
assert.deepEqual(await ccResponse.json(), {
|
|
valid: false,
|
|
error: "Invalid API key",
|
|
warning: null,
|
|
method: null,
|
|
});
|
|
assert.equal(
|
|
ccCalls[0].url,
|
|
`https://proxy.example.com${CLAUDE_CODE_COMPATIBLE_DEFAULT_MODELS_PATH}`
|
|
);
|
|
assert.equal(
|
|
ccCalls[1].url,
|
|
`https://proxy.example.com${CLAUDE_CODE_COMPATIBLE_DEFAULT_MODELS_PATH}`
|
|
);
|
|
assert.equal(ccCalls.length, 2);
|
|
|
|
const anthropicCalls = [];
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
anthropicCalls.push({ url, init });
|
|
return new Response(JSON.stringify({ error: "Unauthorized" }), {
|
|
status: 401,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
};
|
|
|
|
const anthropicResponse = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
baseUrl: "https://proxy.example.com/v1/messages",
|
|
apiKey: "sk-anthropic-invalid",
|
|
type: "anthropic-compatible",
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(anthropicResponse.status, 200);
|
|
assert.deepEqual(await anthropicResponse.json(), {
|
|
valid: false,
|
|
error: "API key unauthorized",
|
|
});
|
|
assert.equal(anthropicCalls[0].url, "https://proxy.example.com/v1/models");
|
|
assert.equal(anthropicCalls[0].init.method, "GET");
|
|
|
|
const openAiCalls = [];
|
|
globalThis.fetch = async (url, init = {}) => {
|
|
openAiCalls.push({ url, init });
|
|
return new Response(JSON.stringify({ data: [] }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
};
|
|
|
|
const openAiResponse = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
baseUrl: "https://proxy.example.com/",
|
|
apiKey: "sk-openai-valid",
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(openAiResponse.status, 200);
|
|
assert.deepEqual(await openAiResponse.json(), {
|
|
valid: true,
|
|
error: null,
|
|
});
|
|
assert.equal(openAiCalls[0].url, "https://proxy.example.com/models");
|
|
assert.equal(openAiCalls[0].init.headers.Authorization, "Bearer sk-openai-valid");
|
|
});
|
|
|
|
test("provider-nodes validate route reports unexpected upstream failures", async () => {
|
|
globalThis.fetch = async () => {
|
|
throw new Error("boom");
|
|
};
|
|
|
|
const response = await providerNodesValidateRoute.POST(
|
|
new Request("http://localhost/api/provider-nodes/validate", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
baseUrl: "https://proxy.example.com",
|
|
apiKey: "sk-openai-test",
|
|
}),
|
|
})
|
|
);
|
|
|
|
assert.equal(response.status, 500);
|
|
assert.deepEqual(await response.json(), {
|
|
error: "Validation failed",
|
|
});
|
|
});
|
|
|
|
test("provider-nodes list route exposes CC flag state from server env", async () => {
|
|
process.env.ENABLE_CC_COMPATIBLE_PROVIDER = "true";
|
|
|
|
const response = await providerNodesRoute.GET();
|
|
assert.equal(response.status, 200);
|
|
|
|
const data = (await response.json()) as any;
|
|
assert.equal(data.ccCompatibleProviderEnabled, true);
|
|
});
|
|
|
|
test("provider models route reports CC compatible providers do not support models listing", async () => {
|
|
process.env.ENABLE_CC_COMPATIBLE_PROVIDER = "true";
|
|
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "anthropic-compatible-cc-test",
|
|
authType: "apikey",
|
|
name: "cc-live",
|
|
apiKey: "sk-test",
|
|
providerSpecificData: {
|
|
baseUrl: "https://proxy.example.com",
|
|
chatPath: CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH,
|
|
},
|
|
});
|
|
|
|
const response = await providerModelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
|
|
assert.equal(response.status, 400);
|
|
assert.deepEqual(await response.json(), {
|
|
error: "Provider anthropic-compatible-cc-test does not support models listing",
|
|
});
|
|
});
|