mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 20:32:20 +03:00
* chore(config): ignore additional agent workflow command files Add newly introduced agent workflow and Claude command files to .gitignore so proprietary automation assets are not committed. * feat(deepseek-web): fix auth to use userToken + WASM PoW solver Rewrite deepseek-web executor from broken cookie auth to userToken Bearer flow (like Chat2API). Replace pure JS Keccak PoW with WASM solver (5.8s → 86ms). Add 14 models, validation, and dashboard UX. * fix(deepseek-web): update target_path to use challenge property * refactor(deepseek-web): streamline token handling and implement cache eviction * fix(deepseek-web): fix SSE parser, prompt format, and error handling - Handle all 3 DeepSeek SSE stream formats: initial fragments, APPEND operations, and bare string tokens (fixes truncated responses) - Simplify prompt builder to send system + last user message only (DeepSeek web API is single-turn, full history caused marker leakage) - Check json.code before token extraction (fixes "did not return access token: Authorization" on code 40003 with HTTP 200) - Clear session cache alongside token cache on auth errors - Add dev origin for remote testing Co-authored-by: Cursor <cursoragent@cursor.com> * chore: ignore memory-bank and cursor agent rules from tracking Co-authored-by: Cursor <cursoragent@cursor.com> * feat: enhance documentation and configuration for Fumadocs integration - Added Fumadocs MDX support in the Next.js configuration. - Updated transpile packages to include fumadocs-ui and fumadocs-core. - Implemented a comprehensive set of redirects for documentation paths to improve navigation. - Removed the generate-docs-index script as it is no longer needed. - Updated various documentation titles for consistency and clarity. - Enhanced global styles to incorporate Fumadocs UI themes and styles. * refactor(docs): cleanup fumadocs PR — revert deepseek, add i18n fallback, restore LanguageSelector - Revert unrelated deepseek-web.ts changes (should be separate PR) - Add .source/ to .gitignore (Fumadocs generated files) - Remove contributor IP from allowedDevOrigins - Add i18n runtime fallback: reads NEXT_LOCALE cookie, loads translated .md from docs/i18n/<locale>/docs/ (preserves existing translation pipeline) - Restore LanguageSelector in Fumadocs layout nav - Restore SEO metadata (title template, description, robots) * fix(codex): use allowlist to strip non-Responses-API fields in non-passthrough path (#2608) (#2615) Integrated into release/v3.8.3 — fix(codex): allowlist-based sanitization for gpt-5.5 Responses API * fix(deepseek-web): fix SSE parser, prompt format, error handling, and cache keys (#2616) Integrated into release/v3.8.3 — fix(deepseek-web): SSE parser (APPEND + bare tokens), prompt builder, error handling, session cache cleanup * chore(config): ignore additional agent workflow command files Add newly introduced agent workflow and Claude command files to .gitignore so proprietary automation assets are not committed. * feat(docs): migrate /docs to Fumadocs MDX with nested routes (#2614) Integrated into release/v3.8.3 — Fumadocs MDX migration with nested routes, search API, and 50+ URL redirects * fix(catalog): skip static PROVIDER_MODELS when synced models exist (#2625) Integrated into release/v3.8.3 * fix(qoder): Cosy auth fallback for PAT tokens + vision support for qwen3-vl-plus (#2629) Integrated into release/v3.8.3 * fix(cli): register tsx loader and add opencode config subcommand (#2631) Integrated into release/v3.8.3 * feat(dashboard): add search and filters to /dashboard/api-manager (#2628) Integrated into release/v3.8.3 * fix(claude): improve Pi and OpenCode compatibility (#2621) Integrated into release/v3.8.3 * fix: restore semantic passthrough system-role-only extraction instead of full normalization (#2620) Integrated into release/v3.8.3 * fix(kiro): stabilize conversationId across prompt compression (#2630) Integrated into release/v3.8.3 * fix(deepseek-web): SSE thinking/search routing and session lifecycle (#2624) Integrated into release/v3.8.3 — DeepSeek Web SSE thinking/search routing overhaul * feat(dashboard): free-tier grouping with symbolic link in /providers (#2632) Integrated into release/v3.8.3 * fix: close implementation gaps — t3-chat-web, stream_options, combo_strategy, batch config (#2634) Integrated into release/v3.8.3 * feat(dashboard): risk notice modal for sensitive providers (#2633) Integrated into release/v3.8.3 * fix(reasoning): extend reasoning_content injection to Kimi K2 and other replay models (#2639) Integrated into release/v3.8.3 * fix(cli): Linux autostart via systemd user service (fixes #2627) (#2635) Integrated into release/v3.8.3 * Refactor/providers free tier (#2640) Integrated into release/v3.8.3 * fix(tests): remove duplicate assertion in schema coercion & fix(cli): ignore system vars in env check * fix(combo): preserve omniModel tag in streaming output for round-trip context pinning (#2646) Integrated into release/v3.8.3 * feat(dashboard): media providers pages + Web Fetch category (#2645) Integrated into release/v3.8.3 * Feature provider adapta org com tutorial de conexão em modal (#2643) Integrated into release/v3.8.3 * fix(rtk): skip content-based filter matching for non-shell tool results (#2642) Integrated into release/v3.8.3 * fix(translator): enable Claude extended thinking for Copilot Responses-API requests (#2647) Integrated into release/v3.8.3 * feat(dashboard): add search and filters to /dashboard/api-manager (#2641) Integrated into release/v3.8.3 * feat(dashboard): risk notice modal for sensitive providers (#2638) Integrated into release/v3.8.3 * feat(dashboard): mini-playground inline (Phase 4) (#2648) Integrated into release/v3.8.3 * fix(settings): fix Require Login modal Cancel button text and dismissal (#2649) Integrated into release/v3.8.3 * feat(combos): universal context handoff for cross-model conversation continuity (#2653) Integrated into release/v3.8.3 * chore(release): bump to v3.8.3 — changelog, docs, version sync * feat(i18n): complete zh-CN translations for 1220 missing keys (#2655) Integrated into release/v3.8.3 * chore(release): include electron package changes in v3.8.3 * docs(changelog): integrate PR #2655 into v3.8.3 * feat(i18n): translate 377 additional zh-CN entries (81 new keys + 296 same-as-en) (#2659) Integrated into release/v3.8.3 * feat(dashboard): add Cmd+K / Ctrl+K command palette for sidebar navigation (#2656) Integrated into release/v3.8.3 * docs: update changelog for PR integrations under v3.8.3 * feat(cli): integrate native updates, autostart and headless CLI mode (#2662) Integrated into release/v3.8.3 * fix(proxy): save dashboard custom proxies in registry (#2661) Integrated into release/v3.8.3 * feat(dashboard): chat-first test slide-over (Option A) (#2660) Integrated into release/v3.8.3 * docs: update changelog with Batch 2 PR merges for v3.8.3 * fix: add xhigh+max to effortLevel schema; add opencode-plugin publish job (#2666) Integrated into release/v3.8.3 * docs: update changelog with Batch 3 PR #2666 merge for v3.8.3 * feat(quota+providers): card-grid layout, provider group headers, Codex race fix (#2667) Integrated into release/v3.8.3 * feat(dashboard): real-time live WebSocket monitoring (#2668) Integrated into release/v3.8.3 * feat(copilot): AI assistant with CodeGraph + CLI + knowledge base (#2669) Integrated into release/v3.8.3 * feat(pipeline): pre-request middleware hooks (#2670) Integrated into release/v3.8.3 * feat(resilience): credential health check + adaptive circuit breaker (#2671) Integrated into release/v3.8.3 * feat(playground): combo routing visual simulator (#2672) Integrated into release/v3.8.3 * feat(auth): API key groups with model-level permissions (#2673) Integrated into release/v3.8.3 * feat(pwa): enhanced manifest + push notification support (#2674) Integrated into release/v3.8.3 * feat(proxy): serverless relay endpoints with rate limiting (#2675) Integrated into release/v3.8.3 * docs(changelog): update changelog for PRs 2667-2675 & fix: resolve typescript compile-time errors * fix(db): remove transactions from migrations Remove explicit transaction wrappers from recent migrations and correct the API key groups migration metadata. Also fix codegraph path resolution for ESM environments and refresh generated fumadocs source output. --------- Co-authored-by: Ömer Vehbe <ovehbe@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Mr. Meowgi <mr@meowgi.dev> Co-authored-by: Hernan Javier Ardila Sanchez <hjasgr@gmail.com> Co-authored-by: amogus22877769 <y.lev357@gmail.com> Co-authored-by: Halil Tezcan KARABULUT <info@hlltzcnkb.com> Co-authored-by: Tentoxa <53821604+Tentoxa@users.noreply.github.com> Co-authored-by: HALDRO <121296348+HALDRO@users.noreply.github.com> Co-authored-by: Paijo <14921983+oyi77@users.noreply.github.com> Co-authored-by: janeza2 <49841619+janeza2@users.noreply.github.com> Co-authored-by: df4p <38404+df4p@users.noreply.github.com> Co-authored-by: ivan-mezentsev <ivan@mezentsev.me> Co-authored-by: Chewji <126886556+Chewji9875@users.noreply.github.com> Co-authored-by: L-aros <107354918+L-aros@users.noreply.github.com> Co-authored-by: M.M <mr.maatoug@gmail.com> Co-authored-by: Benson K B <bensonkbmca@gmail.com> Co-authored-by: terence71-glitch <mcdowellterence71@gmail.com>
919 lines
27 KiB
TypeScript
919 lines
27 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-db-settings-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const ORIGINAL_INITIAL_PASSWORD = process.env.INITIAL_PASSWORD;
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const combosDb = await import("../../src/lib/db/combos.ts");
|
|
const providersDb = await import("../../src/lib/db/providers.ts");
|
|
const proxiesDb = await import("../../src/lib/db/proxies.ts");
|
|
const settingsDb = await import("../../src/lib/db/settings.ts");
|
|
|
|
async function resetStorage() {
|
|
core.resetDbInstance();
|
|
|
|
for (let attempt = 0; attempt < 10; attempt++) {
|
|
try {
|
|
if (fs.existsSync(TEST_DATA_DIR)) {
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
}
|
|
break;
|
|
} catch (error: any) {
|
|
if ((error?.code === "EBUSY" || error?.code === "EPERM") && attempt < 9) {
|
|
await new Promise((resolve) => setTimeout(resolve, 50 * (attempt + 1)));
|
|
} else {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
delete process.env.INITIAL_PASSWORD;
|
|
}
|
|
|
|
test.beforeEach(async () => {
|
|
await resetStorage();
|
|
});
|
|
|
|
test.after(async () => {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
|
|
if (ORIGINAL_INITIAL_PASSWORD === undefined) {
|
|
delete process.env.INITIAL_PASSWORD;
|
|
} else {
|
|
process.env.INITIAL_PASSWORD = ORIGINAL_INITIAL_PASSWORD;
|
|
}
|
|
});
|
|
|
|
test("getSettings exposes defaults and updateSettings persists typed values", async () => {
|
|
const defaults = await settingsDb.getSettings();
|
|
const updated = await settingsDb.updateSettings({
|
|
requireLogin: false,
|
|
cloudEnabled: true,
|
|
stickyRoundRobinLimit: 7,
|
|
requestRetry: 5,
|
|
maxRetryIntervalSec: 12,
|
|
label: "task-303",
|
|
});
|
|
|
|
assert.equal(defaults.cloudEnabled, true);
|
|
assert.equal(defaults.requireLogin, true);
|
|
assert.deepEqual(defaults.hiddenSidebarItems, []);
|
|
assert.equal(defaults.idempotencyWindowMs, 5000);
|
|
assert.equal(defaults.requestRetry, 3);
|
|
assert.equal(defaults.maxRetryIntervalSec, 30);
|
|
assert.equal(defaults.antigravitySignatureCacheMode, "enabled");
|
|
assert.equal(defaults.comboConfigMode, "guided");
|
|
assert.equal(defaults.mcpEnabled, false);
|
|
assert.equal(defaults.a2aEnabled, false);
|
|
assert.equal(updated.requireLogin, false);
|
|
assert.equal(updated.cloudEnabled, true);
|
|
assert.equal(updated.stickyRoundRobinLimit, 7);
|
|
assert.equal(updated.requestRetry, 5);
|
|
assert.equal(updated.maxRetryIntervalSec, 12);
|
|
assert.equal(updated.antigravitySignatureCacheMode, "enabled");
|
|
assert.equal(updated.label, "task-303");
|
|
assert.equal(await settingsDb.isCloudEnabled(), true);
|
|
});
|
|
|
|
test("INITIAL_PASSWORD marks onboarding as complete on first read", async () => {
|
|
process.env.INITIAL_PASSWORD = "bootstrap-secret";
|
|
|
|
const settings = await settingsDb.getSettings();
|
|
const stored = await settingsDb.getSettings();
|
|
|
|
assert.equal(settings.setupComplete, true);
|
|
assert.equal(settings.requireLogin, true);
|
|
assert.equal(stored.setupComplete, true);
|
|
});
|
|
|
|
test("pricing layers merge synced, models.dev and user overrides", async () => {
|
|
const db = core.getDbInstance();
|
|
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"pricing_synced",
|
|
"layered-provider",
|
|
JSON.stringify({
|
|
"model-a": { prompt: 1, completion: 2 },
|
|
})
|
|
);
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"models_dev_pricing",
|
|
"layered-provider",
|
|
JSON.stringify({
|
|
"model-a": { completion: 5, cached: 3 },
|
|
})
|
|
);
|
|
|
|
await settingsDb.updatePricing({
|
|
"layered-provider": {
|
|
"model-a": { prompt: 9, custom: 42 },
|
|
"model-b": { prompt: 7 },
|
|
},
|
|
});
|
|
|
|
const pricing = await settingsDb.getPricing();
|
|
const direct = await settingsDb.getPricingForModel("layered-provider", "model-a");
|
|
const cnFallback = await settingsDb.getPricingForModel("openai-cn", "gpt-4o");
|
|
|
|
assert.deepEqual(pricing["layered-provider"]["model-a"], {
|
|
prompt: 9,
|
|
completion: 5,
|
|
cached: 3,
|
|
custom: 42,
|
|
});
|
|
assert.deepEqual(direct, {
|
|
prompt: 9,
|
|
completion: 5,
|
|
cached: 3,
|
|
custom: 42,
|
|
});
|
|
assert.ok(cnFallback);
|
|
|
|
const afterModelReset = await settingsDb.resetPricing("layered-provider", "model-a");
|
|
assert.equal(afterModelReset["layered-provider"]["model-a"], undefined);
|
|
|
|
const afterProviderReset = await settingsDb.resetPricing("layered-provider");
|
|
assert.equal(afterProviderReset["layered-provider"], undefined);
|
|
|
|
await settingsDb.updatePricing({
|
|
temp: { model: { prompt: 1 } },
|
|
});
|
|
assert.deepEqual(await settingsDb.resetAllPricing(), {});
|
|
});
|
|
|
|
test("getPricingWithSources reports the winning layer for each provider/model", async () => {
|
|
const db = core.getDbInstance();
|
|
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"pricing_synced",
|
|
"layer-source",
|
|
JSON.stringify({
|
|
"model-litellm": { prompt: 1, completion: 2 },
|
|
"model-user": { prompt: 3 },
|
|
})
|
|
);
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"models_dev_pricing",
|
|
"layer-source",
|
|
JSON.stringify({
|
|
"model-modelsdev": { prompt: 4, completion: 5 },
|
|
"model-user": { completion: 6 },
|
|
})
|
|
);
|
|
|
|
await settingsDb.updatePricing({
|
|
"layer-source": {
|
|
"model-user": { cached: 7 },
|
|
},
|
|
});
|
|
|
|
const { pricing, sourceMap } = await settingsDb.getPricingWithSources();
|
|
|
|
assert.deepEqual(pricing["layer-source"]["model-litellm"], {
|
|
prompt: 1,
|
|
completion: 2,
|
|
});
|
|
assert.deepEqual(pricing["layer-source"]["model-modelsdev"], {
|
|
prompt: 4,
|
|
completion: 5,
|
|
});
|
|
assert.deepEqual(pricing["layer-source"]["model-user"], {
|
|
prompt: 3,
|
|
completion: 6,
|
|
cached: 7,
|
|
});
|
|
assert.equal(sourceMap["layer-source"]["model-litellm"], "litellm");
|
|
assert.equal(sourceMap["layer-source"]["model-modelsdev"], "modelsDev");
|
|
assert.equal(sourceMap["layer-source"]["model-user"], "user");
|
|
assert.equal(sourceMap.openai["gpt-4o"], "default");
|
|
});
|
|
|
|
test("LKGP values can be set, read and cleared", async () => {
|
|
assert.equal(await settingsDb.getLKGP("combo-a", "model-a"), null);
|
|
|
|
await settingsDb.setLKGP("combo-a", "model-a", "openai");
|
|
await settingsDb.setLKGP("combo-a", "model-b", "anthropic");
|
|
|
|
assert.deepEqual(await settingsDb.getLKGP("combo-a", "model-a"), { provider: "openai" });
|
|
assert.deepEqual(await settingsDb.getLKGP("combo-a", "model-b"), { provider: "anthropic" });
|
|
|
|
settingsDb.clearAllLKGP();
|
|
|
|
assert.equal(await settingsDb.getLKGP("combo-a", "model-a"), null);
|
|
});
|
|
|
|
test("LKGP stores and retrieves connectionId", async () => {
|
|
await settingsDb.setLKGP("combo-c", "model-c", "openai", "conn-abc123");
|
|
|
|
const record = await settingsDb.getLKGP("combo-c", "model-c");
|
|
assert.deepEqual(record, { provider: "openai", connectionId: "conn-abc123" });
|
|
});
|
|
|
|
test("LKGP without connectionId omits the field", async () => {
|
|
await settingsDb.setLKGP("combo-d", "model-d", "anthropic");
|
|
|
|
const record = await settingsDb.getLKGP("combo-d", "model-d");
|
|
assert.deepEqual(record, { provider: "anthropic" });
|
|
assert.equal("connectionId" in (record as object), false);
|
|
});
|
|
|
|
test("LKGP overwrites connectionId when updated without one", async () => {
|
|
await settingsDb.setLKGP("combo-e", "model-e", "openai", "conn-old");
|
|
await settingsDb.setLKGP("combo-e", "model-e", "openai");
|
|
|
|
const record = await settingsDb.getLKGP("combo-e", "model-e");
|
|
assert.deepEqual(record, { provider: "openai" });
|
|
});
|
|
|
|
test("pricing helpers ignore malformed synced data and LKGP falls back to raw values", async () => {
|
|
const db = core.getDbInstance();
|
|
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"models_dev_pricing",
|
|
"broken-provider",
|
|
"{not-json"
|
|
);
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"pricing",
|
|
"alias-provider",
|
|
JSON.stringify({
|
|
"model-a": { prompt: 7 },
|
|
})
|
|
);
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"lkgp",
|
|
"combo-raw:model-raw",
|
|
"raw-provider-id"
|
|
);
|
|
|
|
const pricing = await settingsDb.getPricing();
|
|
|
|
assert.equal(pricing["broken-provider"], undefined);
|
|
assert.equal(await settingsDb.getPricingForModel("alias-provider", "missing-model"), null);
|
|
assert.deepEqual(await settingsDb.getLKGP("combo-raw", "model-raw"), {
|
|
provider: "raw-provider-id",
|
|
});
|
|
});
|
|
|
|
test("pricing helpers resolve aliased providers and tolerate no-op resets", async () => {
|
|
const db = core.getDbInstance();
|
|
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"pricing",
|
|
"cc",
|
|
JSON.stringify({
|
|
"claude-3-5-sonnet": { prompt: 4, completion: 6 },
|
|
})
|
|
);
|
|
|
|
const aliasPricing = await settingsDb.getPricingForModel("claude", "claude-3-5-sonnet");
|
|
const missingPricing = await settingsDb.getPricingForModel("missing-provider", "missing-model");
|
|
const afterUnknownReset = await settingsDb.resetPricing("missing-provider", "missing-model");
|
|
|
|
assert.deepEqual(aliasPricing, { prompt: 4, completion: 6 });
|
|
assert.equal(missingPricing, null);
|
|
assert.equal(afterUnknownReset["missing-provider"], undefined);
|
|
});
|
|
|
|
test("settings and pricing readers skip malformed rows while merging surviving layers", async () => {
|
|
const db = core.getDbInstance();
|
|
const originalPrepare = db.prepare.bind(db);
|
|
|
|
db.prepare = (sql) => {
|
|
const text = String(sql);
|
|
|
|
if (text.includes("namespace = 'settings'")) {
|
|
return {
|
|
all: () => [
|
|
123,
|
|
{ key: 456, value: "true" },
|
|
{ key: "cloudEnabled", value: "true" },
|
|
{ key: "requireLogin", value: null },
|
|
],
|
|
};
|
|
}
|
|
|
|
if (text === "SELECT key, value FROM key_value WHERE namespace = ?") {
|
|
return {
|
|
all: (namespace) => {
|
|
if (namespace === "pricing_synced") {
|
|
return [
|
|
123,
|
|
{ key: 456, value: JSON.stringify({ ignored: true }) },
|
|
{
|
|
key: "layered-provider",
|
|
value: JSON.stringify({
|
|
"model-a": { prompt: 1, completion: 2 },
|
|
}),
|
|
},
|
|
];
|
|
}
|
|
|
|
if (namespace === "models_dev_pricing") {
|
|
return [
|
|
{ key: "broken-provider", value: "{bad" },
|
|
{ key: "missing-value", value: null },
|
|
{
|
|
key: "layered-provider",
|
|
value: JSON.stringify({
|
|
"model-a": { cached: 3 },
|
|
}),
|
|
},
|
|
];
|
|
}
|
|
|
|
if (namespace === "pricing") {
|
|
return [
|
|
{
|
|
key: "layered-provider",
|
|
value: JSON.stringify({
|
|
"model-a": { prompt: 9, custom: 42 },
|
|
"model-b": { prompt: 7 },
|
|
}),
|
|
},
|
|
{ key: null, value: JSON.stringify({ ignored: true }) },
|
|
];
|
|
}
|
|
|
|
return originalPrepare(sql).all(namespace);
|
|
},
|
|
};
|
|
}
|
|
|
|
return originalPrepare(sql);
|
|
};
|
|
|
|
try {
|
|
const settings = await settingsDb.getSettings();
|
|
const pricing = await settingsDb.getPricing();
|
|
const modelPricing = await settingsDb.getPricingForModel("layered-provider", "model-a");
|
|
|
|
assert.equal(settings.cloudEnabled, true);
|
|
assert.equal(settings.requireLogin, true);
|
|
assert.deepEqual(pricing["layered-provider"]["model-a"], {
|
|
prompt: 9,
|
|
completion: 2,
|
|
cached: 3,
|
|
custom: 42,
|
|
});
|
|
assert.deepEqual(modelPricing, {
|
|
prompt: 9,
|
|
completion: 2,
|
|
cached: 3,
|
|
custom: 42,
|
|
});
|
|
assert.equal(pricing["broken-provider"], undefined);
|
|
} finally {
|
|
db.prepare = originalPrepare;
|
|
}
|
|
});
|
|
|
|
test("proxy config migrates legacy strings and supports bulk merge updates", async () => {
|
|
const db = core.getDbInstance();
|
|
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"proxyConfig",
|
|
"global",
|
|
JSON.stringify("http://user:pass@global.local:8080")
|
|
);
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"proxyConfig",
|
|
"providers",
|
|
JSON.stringify({
|
|
openai: "https://provider.local:8443",
|
|
})
|
|
);
|
|
|
|
const migrated = await settingsDb.getProxyConfig();
|
|
assert.deepEqual(migrated.global, {
|
|
type: "http",
|
|
host: "global.local",
|
|
port: "8080",
|
|
username: "user",
|
|
password: "pass",
|
|
});
|
|
assert.deepEqual(migrated.providers.openai, {
|
|
type: "https",
|
|
host: "provider.local",
|
|
port: "8443",
|
|
username: "",
|
|
password: "",
|
|
});
|
|
|
|
const merged = await settingsDb.setProxyConfig({
|
|
providers: {
|
|
openai: null,
|
|
anthropic: {
|
|
type: "http",
|
|
host: "anthropic.local",
|
|
port: 9000,
|
|
},
|
|
},
|
|
keys: {
|
|
key123: {
|
|
type: "socks5",
|
|
host: "key.local",
|
|
port: 1080,
|
|
},
|
|
},
|
|
});
|
|
|
|
assert.equal(merged.providers.openai, undefined);
|
|
assert.equal(merged.providers.anthropic.host, "anthropic.local");
|
|
assert.equal((await settingsDb.getProxyForLevel("key", "key123")).host, "key.local");
|
|
|
|
await settingsDb.deleteProxyForLevel("key", "key123");
|
|
|
|
assert.equal(await settingsDb.getProxyForLevel("key", "key123"), null);
|
|
});
|
|
|
|
test("proxy config migrates socks5 and host-only entries while preserving plural lookups", async () => {
|
|
const db = core.getDbInstance();
|
|
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"proxyConfig",
|
|
"global",
|
|
JSON.stringify("fallback-only-host")
|
|
);
|
|
db.prepare("INSERT INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run(
|
|
"proxyConfig",
|
|
"providers",
|
|
JSON.stringify({
|
|
claude: "socks5://sockshost",
|
|
})
|
|
);
|
|
|
|
const migrated = await settingsDb.getProxyConfig();
|
|
assert.deepEqual(migrated.global, {
|
|
type: "http",
|
|
host: "fallback-only-host",
|
|
port: "8080",
|
|
username: "",
|
|
password: "",
|
|
});
|
|
assert.deepEqual(migrated.providers.claude, {
|
|
type: "socks5",
|
|
host: "sockshost",
|
|
port: "1080",
|
|
username: "",
|
|
password: "",
|
|
});
|
|
assert.equal((await settingsDb.getProxyForLevel("providers", "claude")).host, "sockshost");
|
|
|
|
const updated = await settingsDb.setProxyConfig({
|
|
global: null,
|
|
providers: {},
|
|
});
|
|
|
|
assert.equal(updated.global, null);
|
|
assert.equal(await settingsDb.getProxyForLevel("global"), null);
|
|
|
|
await settingsDb.deleteProxyForLevel("provider", null);
|
|
|
|
assert.equal((await settingsDb.getProxyForLevel("provider", "claude")).host, "sockshost");
|
|
});
|
|
|
|
test("proxy helpers resolve key, provider, global, and direct paths while tolerating malformed combo rows", async () => {
|
|
const db = core.getDbInstance();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "openai",
|
|
authType: "apikey",
|
|
name: "Proxy Resolution Target",
|
|
apiKey: "sk-proxy-resolution",
|
|
});
|
|
|
|
await settingsDb.setProxyConfig({
|
|
level: "global",
|
|
proxy: {
|
|
type: "http",
|
|
host: "global.local",
|
|
port: 8080,
|
|
},
|
|
});
|
|
await settingsDb.setProxyForLevel("provider", "openai", {
|
|
type: "https",
|
|
host: "provider.local",
|
|
port: 8443,
|
|
});
|
|
await settingsDb.setProxyForLevel("combo", "combo-broken", {
|
|
type: "socks5",
|
|
host: "combo.local",
|
|
port: 1080,
|
|
});
|
|
const combo = await combosDb.createCombo({
|
|
name: "combo-broken",
|
|
models: ["openai/gpt-4o-mini"],
|
|
strategy: "priority",
|
|
});
|
|
db.prepare("UPDATE combos SET data = ? WHERE id = ?").run("{not-json", combo.id);
|
|
|
|
const providerResolved = await settingsDb.resolveProxyForConnection((connection as any).id);
|
|
|
|
assert.equal(providerResolved.level, "provider");
|
|
assert.equal(providerResolved.proxy.host, "provider.local");
|
|
assert.deepEqual(await settingsDb.getProxyForLevel("combo", "combo-broken"), {
|
|
type: "socks5",
|
|
host: "combo.local",
|
|
port: 1080,
|
|
});
|
|
|
|
await settingsDb.deleteProxyForLevel("provider", "openai");
|
|
|
|
const globalResolved = await settingsDb.resolveProxyForConnection((connection as any).id);
|
|
|
|
assert.equal(globalResolved.level, "global");
|
|
assert.equal(globalResolved.proxy.host, "global.local");
|
|
|
|
await settingsDb.setProxyForLevel("key", (connection as any).id, {
|
|
type: "http",
|
|
host: "key.local",
|
|
port: 3128,
|
|
});
|
|
|
|
const keyResolved = await settingsDb.resolveProxyForConnection((connection as any).id);
|
|
|
|
assert.equal(keyResolved.level, "key");
|
|
assert.equal(keyResolved.proxy.host, "key.local");
|
|
|
|
await settingsDb.deleteProxyForLevel("key", (connection as any).id);
|
|
await settingsDb.deleteProxyForLevel("global", null);
|
|
|
|
const directResolved = await settingsDb.resolveProxyForConnection((connection as any).id);
|
|
|
|
assert.equal(directResolved.level, "direct");
|
|
assert.equal(directResolved.proxy, null);
|
|
});
|
|
|
|
test("proxy resolution skips combos without serialized data and falls back to provider proxies", async () => {
|
|
const db = core.getDbInstance();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "claude",
|
|
authType: "apikey",
|
|
name: "Proxy Null Combo",
|
|
apiKey: "sk-claude-proxy",
|
|
});
|
|
|
|
await settingsDb.setProxyForLevel("provider", "claude", {
|
|
type: "https",
|
|
host: "provider-claude.local",
|
|
port: 443,
|
|
});
|
|
|
|
const combo = await combosDb.createCombo({
|
|
name: "combo-null-data",
|
|
models: ["claude/claude-3-5-sonnet"],
|
|
strategy: "priority",
|
|
});
|
|
await settingsDb.setProxyForLevel("combo" as any, (combo as any).id, {
|
|
type: "http",
|
|
host: "combo-null.local",
|
|
port: 8080,
|
|
});
|
|
db.prepare("UPDATE combos SET data = ? WHERE id = ?").run(0, combo.id);
|
|
|
|
const resolved = await settingsDb.resolveProxyForConnection((connection as any).id);
|
|
|
|
assert.equal(resolved.level, "provider");
|
|
assert.equal(resolved.proxy.host, "provider-claude.local");
|
|
});
|
|
|
|
test("proxy resolution matches combo proxies through aliased model entries", async () => {
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "claude",
|
|
authType: "apikey",
|
|
name: "Proxy Alias Combo",
|
|
apiKey: "sk-claude-alias",
|
|
});
|
|
|
|
const combo = await combosDb.createCombo({
|
|
name: "combo-aliased-model",
|
|
models: [{ model: "cc/claude-3-5-sonnet" }],
|
|
strategy: "priority",
|
|
});
|
|
await settingsDb.setProxyForLevel("combo", (combo as any).id, {
|
|
type: "https",
|
|
host: "combo-alias.local",
|
|
port: 443,
|
|
});
|
|
|
|
const resolved = await settingsDb.resolveProxyForConnection((connection as any).id);
|
|
|
|
assert.equal(resolved.level, "combo");
|
|
assert.equal(resolved.levelId, combo.id);
|
|
assert.equal(resolved.proxy.host, "combo-alias.local");
|
|
});
|
|
|
|
test("proxy resolution prefers legacy key and provider proxies over registry global fallback (#2601)", async () => {
|
|
const keyConnection = await providersDb.createProviderConnection({
|
|
provider: "openai",
|
|
authType: "apikey",
|
|
name: "Legacy Key Override",
|
|
apiKey: "sk-key-override",
|
|
});
|
|
const providerConnection = await providersDb.createProviderConnection({
|
|
provider: "claude",
|
|
authType: "apikey",
|
|
name: "Legacy Provider Override",
|
|
apiKey: "sk-provider-override",
|
|
});
|
|
|
|
const registryGlobal = await proxiesDb.createProxy({
|
|
name: "Registry Global Fallback",
|
|
type: "http",
|
|
host: "registry-global.local",
|
|
port: 8080,
|
|
});
|
|
await proxiesDb.assignProxyToScope("global", null, registryGlobal.id);
|
|
|
|
await settingsDb.setProxyForLevel("key", (keyConnection as any).id, {
|
|
type: "http",
|
|
host: "legacy-key-override.local",
|
|
port: 3128,
|
|
});
|
|
await settingsDb.setProxyForLevel("provider", "claude", {
|
|
type: "https",
|
|
host: "legacy-provider-override.local",
|
|
port: 443,
|
|
});
|
|
|
|
const keyResolved = await settingsDb.resolveProxyForConnection((keyConnection as any).id);
|
|
const providerResolved = await settingsDb.resolveProxyForConnection(
|
|
(providerConnection as any).id
|
|
);
|
|
|
|
assert.equal(keyResolved.level, "key");
|
|
assert.equal(keyResolved.proxy.host, "legacy-key-override.local");
|
|
assert.equal(providerResolved.level, "provider");
|
|
assert.equal(providerResolved.proxy.host, "legacy-provider-override.local");
|
|
});
|
|
|
|
test("proxy readers normalize legacy rows, skip malformed entries, and coerce invalid globals to null", async () => {
|
|
const db = core.getDbInstance();
|
|
const originalPrepare = db.prepare.bind(db);
|
|
|
|
db.prepare = (sql) => {
|
|
const text = String(sql);
|
|
|
|
if (text.includes("namespace = 'proxyConfig'") && text.startsWith("SELECT")) {
|
|
return {
|
|
all: () => [
|
|
123,
|
|
{ key: 456, value: JSON.stringify({ ignored: true }) },
|
|
{ key: "global", value: JSON.stringify("https://user%40name:pass%2Fword@proxy.example") },
|
|
{
|
|
key: "providers",
|
|
value: JSON.stringify({
|
|
openai: "http://provider.example",
|
|
}),
|
|
},
|
|
{ key: "combos", value: JSON.stringify("not-a-map") },
|
|
{ key: "keys", value: null },
|
|
],
|
|
};
|
|
}
|
|
|
|
if (text.includes("namespace = 'proxyConfig'") && text.startsWith("INSERT OR REPLACE")) {
|
|
return { run: () => ({ changes: 1 }) };
|
|
}
|
|
|
|
return originalPrepare(sql);
|
|
};
|
|
|
|
try {
|
|
const config = await settingsDb.getProxyConfig();
|
|
assert.deepEqual(config.global, {
|
|
type: "https",
|
|
host: "proxy.example",
|
|
port: "443",
|
|
username: "user@name",
|
|
password: "pass/word",
|
|
});
|
|
assert.deepEqual(config.providers.openai, {
|
|
type: "http",
|
|
host: "provider.example",
|
|
port: "8080",
|
|
username: "",
|
|
password: "",
|
|
});
|
|
assert.equal(await settingsDb.getProxyForLevel("combo", "missing"), null);
|
|
} finally {
|
|
db.prepare = originalPrepare;
|
|
}
|
|
|
|
const updated = await settingsDb.setProxyConfig({ level: 123, id: 456, proxy: 0 });
|
|
assert.equal(updated.global, null);
|
|
assert.equal(await settingsDb.getProxyForLevel("global"), null);
|
|
});
|
|
|
|
test("cache metrics, trend and no-op update/reset methods read from usage_history", async () => {
|
|
const db = core.getDbInstance();
|
|
const now = new Date().toISOString();
|
|
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000).toISOString();
|
|
|
|
const insertUsage = db.prepare(`
|
|
INSERT INTO usage_history (
|
|
provider, model, connection_id, api_key_id, api_key_name,
|
|
tokens_input, tokens_output, tokens_cache_read, tokens_cache_creation,
|
|
tokens_reasoning, status, success, latency_ms, ttft_ms, error_code, timestamp
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
`);
|
|
|
|
insertUsage.run(
|
|
"openai",
|
|
"gpt-4.1",
|
|
"conn-1",
|
|
"key-1",
|
|
"Primary",
|
|
1000,
|
|
400,
|
|
300,
|
|
120,
|
|
0,
|
|
"200",
|
|
1,
|
|
100,
|
|
40,
|
|
null,
|
|
oneHourAgo
|
|
);
|
|
insertUsage.run(
|
|
"anthropic",
|
|
"claude-3-7-sonnet",
|
|
"conn-2",
|
|
"key-2",
|
|
"Secondary",
|
|
700,
|
|
280,
|
|
200,
|
|
80,
|
|
0,
|
|
"200",
|
|
1,
|
|
90,
|
|
30,
|
|
null,
|
|
now
|
|
);
|
|
|
|
const metrics = await settingsDb.getCacheMetrics();
|
|
const trend = await settingsDb.getCacheTrend(4);
|
|
const updateNoOp = await settingsDb.updateCacheMetrics({ anything: true });
|
|
const resetNoOp = await settingsDb.resetCacheMetrics();
|
|
|
|
assert.ok(metrics.totalRequests >= 2);
|
|
assert.ok(metrics.requestsWithCacheControl >= 2);
|
|
assert.ok(metrics.byProvider.openai);
|
|
assert.ok(metrics.byProvider.anthropic);
|
|
assert.ok(trend.length >= 1);
|
|
assert.equal(updateNoOp.totalCachedTokens, metrics.totalCachedTokens);
|
|
assert.equal(resetNoOp.totalCachedTokens, metrics.totalCachedTokens);
|
|
});
|
|
|
|
test("cache metric helpers degrade gracefully when SQLite aggregation fails", async () => {
|
|
const db = core.getDbInstance();
|
|
const originalPrepare = db.prepare.bind(db);
|
|
db.prepare = () => {
|
|
throw new Error("db offline");
|
|
};
|
|
|
|
try {
|
|
const metrics = await settingsDb.getCacheMetrics();
|
|
const updated = await settingsDb.updateCacheMetrics({ force: true });
|
|
const trend = await settingsDb.getCacheTrend(6);
|
|
const reset = await settingsDb.resetCacheMetrics();
|
|
|
|
assert.equal(metrics.totalRequests, 0);
|
|
assert.equal(updated.totalCachedTokens, 0);
|
|
assert.deepEqual(trend, []);
|
|
assert.equal(reset.requestsWithCacheControl, 0);
|
|
} finally {
|
|
db.prepare = originalPrepare;
|
|
}
|
|
});
|
|
|
|
test("cache metrics and trend coerce null aggregate fields to zero", async () => {
|
|
const db = core.getDbInstance();
|
|
const originalPrepare = db.prepare.bind(db);
|
|
|
|
db.prepare = (sql) => {
|
|
const text = String(sql);
|
|
|
|
if (
|
|
text.includes("COUNT(*) as totalRequests") &&
|
|
text.includes("SUM(tokens_input) as totalInputTokens") &&
|
|
!text.includes("GROUP BY")
|
|
) {
|
|
return {
|
|
get: () => ({
|
|
totalRequests: 2,
|
|
totalInputTokens: null,
|
|
totalCachedTokens: null,
|
|
totalCacheCreationTokens: null,
|
|
}),
|
|
};
|
|
}
|
|
|
|
if (text.match(/SELECT\s+COUNT\(\*\)\s+as\s+totalRequests\s+FROM\s+usage_history\s*$/)) {
|
|
return {
|
|
get: () => ({
|
|
totalRequests: 5,
|
|
}),
|
|
};
|
|
}
|
|
|
|
if (text.includes("GROUP BY provider")) {
|
|
return {
|
|
all: () => [
|
|
{
|
|
provider: "openai",
|
|
totalRequests: 1,
|
|
cachedRequests: 1,
|
|
inputTokens: null,
|
|
cachedTokens: null,
|
|
cacheCreationTokens: null,
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
if (text.includes("GROUP BY combo_strategy")) {
|
|
return {
|
|
all: () => [
|
|
{
|
|
strategy: "direct",
|
|
requests: 2,
|
|
inputTokens: null,
|
|
cachedTokens: null,
|
|
cacheCreationTokens: null,
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
if (text.includes("strftime('%Y-%m-%dT%H:00:00Z', timestamp) as hour")) {
|
|
return {
|
|
all: () => [
|
|
{
|
|
hour: "2026-01-01T10:00:00Z",
|
|
requests: 3,
|
|
cachedRequests: 1,
|
|
inputTokens: null,
|
|
cachedTokens: null,
|
|
cacheCreationTokens: null,
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
return originalPrepare(sql);
|
|
};
|
|
|
|
try {
|
|
const metrics = await settingsDb.getCacheMetrics();
|
|
const updated = await settingsDb.updateCacheMetrics({ force: true });
|
|
const trend = await settingsDb.getCacheTrend(2);
|
|
const reset = await settingsDb.resetCacheMetrics();
|
|
|
|
assert.equal(metrics.totalRequests, 5);
|
|
assert.equal(metrics.totalInputTokens, 0);
|
|
assert.equal(metrics.totalCachedTokens, 0);
|
|
assert.equal(metrics.totalCacheCreationTokens, 0);
|
|
assert.deepEqual(metrics.byProvider.openai, {
|
|
requests: 1,
|
|
totalRequests: 1,
|
|
cachedRequests: 1,
|
|
inputTokens: 0,
|
|
cachedTokens: 0,
|
|
cacheCreationTokens: 0,
|
|
});
|
|
assert.deepEqual(metrics.byStrategy.direct, {
|
|
requests: 2,
|
|
inputTokens: 0,
|
|
cachedTokens: 0,
|
|
cacheCreationTokens: 0,
|
|
});
|
|
assert.deepEqual(trend, [
|
|
{
|
|
timestamp: "2026-01-01T10:00:00Z",
|
|
requests: 3,
|
|
cachedRequests: 1,
|
|
inputTokens: 0,
|
|
cachedTokens: 0,
|
|
cacheCreationTokens: 0,
|
|
},
|
|
]);
|
|
assert.equal(updated.totalCachedTokens, 0);
|
|
assert.equal(reset.totalCachedTokens, 0);
|
|
} finally {
|
|
db.prepare = originalPrepare;
|
|
}
|
|
});
|