mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-12 18:22:48 +03:00
* feat(alibaba): add free-tier routing with console quota and builtin allowlist Classify DashScope free vs paid models via console quota API, a hardcoded operator allowlist fallback, and per-connection drained tracking. Wire wildcard combo expansion, model refresh, combo exhaustion, and audit redaction for Alibaba console credentials. * fix(routing): reset forced connection pin and persist Alibaba free-tier drain Drop session affinity pins when a forced connection is excluded after 429, and record Alibaba free-tier exhaustion on upstream 403 so per-key drained lists stay accurate without blocking sibling keys. * fix(alibaba): prefer live quota sync over static free-tier allowlist Stop unioning the builtin text allowlist when a console quota snapshot exists, treat expired quotaValidityPeriod as not_capable, and add a dated JSON pack plus sync-alibaba-allowlist script for operator refresh without code edits. * docs(alibaba): document free-tier console path + allowlist env overrides Adds the 4 ALIBABA_FREE_TIER_*_FE_PATH / ALIBABA_FREE_TIER_ALLOWLIST_PATH env vars (referenced by alibabaFreeTierQuotaFetcher.ts and alibabaFreeTierAllowlist.ts) to .env.example and docs/reference/ENVIRONMENT.md so the env/docs contract check passes. Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> * refactor(open-sse): split alibabaFreeTierQuotaFetcher.ts under file-size cap Extract pure parsing/classification/eligibility-filtering logic into alibabaFreeTierQuotaClassify.ts and shared types/primitives into alibabaFreeTierQuotaTypes.ts, leaving the HTTP/console-fetch flow in the original file. Public API is unchanged (re-exported), behavior is identical. Co-authored-by: AndrianBalanescu <AndrianBalanescu@users.noreply.github.com> * fix: resolve typecheck errors in alibaba-free-tier routing --------- Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com> Co-authored-by: AndrianBalanescu <AndrianBalanescu@users.noreply.github.com> Co-authored-by: AndrianBalanescu <andrian@balanescu.dev>
226 lines
7.1 KiB
TypeScript
226 lines
7.1 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-dashscope-models-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const providersDb = await import("../../src/lib/db/providers.ts");
|
|
const modelsDb = await import("../../src/lib/db/models.ts");
|
|
const modelsRoute = await import("../../src/app/api/providers/[id]/models/route.ts");
|
|
|
|
const ALIBABA_MODEL_STUDIO_MODEL_IDS = [
|
|
"qwen3.7-max",
|
|
"qwen3.7-plus",
|
|
"qwen3.6-plus",
|
|
"qwen3.6-27b",
|
|
"qwen3.6-35b-a3b",
|
|
"qwen3.5-plus",
|
|
"qwen3.5-122b-a10b",
|
|
"qwen3.5-397b-a17b",
|
|
"glm-5.2",
|
|
"glm-5.2-fast-preview",
|
|
"deepseek-v4-pro",
|
|
"deepseek-v4-flash",
|
|
"kimi-k2.7-code",
|
|
];
|
|
|
|
const QWEN_CLOUD_TEXT_MODEL_IDS = [
|
|
"qwen3.7-max-2026-06-08",
|
|
"qwen3.7-plus",
|
|
"qwen3.6-plus",
|
|
"qwen3.6-27b",
|
|
"qwen3.6-35b-a3b",
|
|
"qwen3.5-plus-2026-04-20",
|
|
"qwen3.5-122b-a10b",
|
|
"qwen3.5-397b-a17b",
|
|
"glm-5.2",
|
|
"glm-5.2-fast-preview",
|
|
"deepseek-v4-pro",
|
|
"deepseek-v4-flash",
|
|
"kimi-k2.7-code",
|
|
];
|
|
|
|
const ALL_DASHSCOPE_TEXT_MODEL_IDS = [
|
|
...QWEN_CLOUD_TEXT_MODEL_IDS,
|
|
...ALIBABA_MODEL_STUDIO_MODEL_IDS,
|
|
"qwen3-coder-next",
|
|
"qwen-mt-plus",
|
|
"MiniMax-M2.5",
|
|
];
|
|
const UPSTREAM_TEXT_MODEL_IDS = [...ALL_DASHSCOPE_TEXT_MODEL_IDS].reverse();
|
|
|
|
const MIXED_DASHSCOPE_MODELS = [
|
|
...UPSTREAM_TEXT_MODEL_IDS.map((id) => ({
|
|
id,
|
|
object: "model",
|
|
owned_by: "system",
|
|
})),
|
|
{ id: "qwen-image-2.0-pro", object: "model", owned_by: "system" },
|
|
{ id: "wan2.7-image-pro", object: "model", owned_by: "system" },
|
|
{ id: "qwen3-tts-flash", object: "model", owned_by: "system" },
|
|
{ id: "qwen3-asr-flash", object: "model", owned_by: "system" },
|
|
{ id: "qwen3.5-omni-plus", object: "model", owned_by: "system" },
|
|
{ id: "qwen3-vl-plus", object: "model", owned_by: "system" },
|
|
{ id: "qwen3-rerank", object: "model", owned_by: "system" },
|
|
{ id: "text-embedding-v4", object: "model", owned_by: "system" },
|
|
{ id: "qvq-max", object: "model", owned_by: "system" },
|
|
{ id: "ccai-pro", object: "model", owned_by: "system" },
|
|
];
|
|
|
|
async function resetStorage() {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
async function assertTextOnlyDiscovery({
|
|
provider,
|
|
region,
|
|
expectedUrl,
|
|
expectedModelIds,
|
|
}: {
|
|
provider: "alibaba" | "alibaba-cn" | "qwen-cloud";
|
|
region?: "global-sg" | "china-beijing";
|
|
expectedUrl: string;
|
|
expectedModelIds: string[];
|
|
}) {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider,
|
|
authType: "apikey",
|
|
name: `${provider}-live-text`,
|
|
apiKey: "test-dashscope-key",
|
|
...(region ? { providerSpecificData: { region } } : {}),
|
|
});
|
|
await modelsDb.replaceSyncedAvailableModelsForConnection(provider, connection.id, [
|
|
{ id: "qwen-image-stale", name: "Stale image model", source: "imported" },
|
|
]);
|
|
|
|
let fetchCalls = 0;
|
|
let requestedUrl = "";
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = (async (input) => {
|
|
fetchCalls += 1;
|
|
requestedUrl = typeof input === "string" ? input : input.toString();
|
|
return new Response(JSON.stringify({ object: "list", data: MIXED_DASHSCOPE_MODELS }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}) as typeof globalThis.fetch;
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models?refresh=true`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
const body = await response.json();
|
|
assert.equal(body.source, "api");
|
|
assert.deepEqual(
|
|
body.models.map((model: { id: string }) => model.id),
|
|
expectedModelIds
|
|
);
|
|
assert.equal(requestedUrl, expectedUrl);
|
|
assert.equal(fetchCalls, 1);
|
|
|
|
const persisted = await modelsDb.getSyncedAvailableModelsForConnection(provider, connection.id);
|
|
assert.deepEqual(
|
|
persisted.map((model) => model.id),
|
|
expectedModelIds
|
|
);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
}
|
|
|
|
test.after(() => {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
});
|
|
|
|
test("Qwen Cloud syncs only text models from the selected Beijing region", async () => {
|
|
await assertTextOnlyDiscovery({
|
|
provider: "qwen-cloud",
|
|
region: "china-beijing",
|
|
expectedUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1/models",
|
|
expectedModelIds: QWEN_CLOUD_TEXT_MODEL_IDS,
|
|
});
|
|
});
|
|
|
|
test("Alibaba syncs only text models from the selected Global region", async () => {
|
|
await assertTextOnlyDiscovery({
|
|
provider: "alibaba",
|
|
region: "global-sg",
|
|
expectedUrl: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/models",
|
|
expectedModelIds: ALIBABA_MODEL_STUDIO_MODEL_IDS,
|
|
});
|
|
});
|
|
|
|
test("Alibaba syncs only text models from the selected Beijing region", async () => {
|
|
await assertTextOnlyDiscovery({
|
|
provider: "alibaba",
|
|
region: "china-beijing",
|
|
expectedUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1/models",
|
|
expectedModelIds: ALIBABA_MODEL_STUDIO_MODEL_IDS,
|
|
});
|
|
});
|
|
|
|
test("legacy Alibaba China connections also sync only Beijing text models", async () => {
|
|
await assertTextOnlyDiscovery({
|
|
provider: "alibaba-cn",
|
|
expectedUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1/models",
|
|
expectedModelIds: ALIBABA_MODEL_STUDIO_MODEL_IDS,
|
|
});
|
|
});
|
|
|
|
test("Alibaba free billing mode syncs all live text models, not only the curated catalog", async () => {
|
|
await resetStorage();
|
|
const connection = await providersDb.createProviderConnection({
|
|
provider: "alibaba",
|
|
authType: "apikey",
|
|
name: "alibaba-free-live",
|
|
apiKey: "test-dashscope-key",
|
|
providerSpecificData: { region: "global-sg", alibabaBillingMode: "free" },
|
|
});
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = (async () =>
|
|
new Response(JSON.stringify({ object: "list", data: MIXED_DASHSCOPE_MODELS }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
})) as typeof globalThis.fetch;
|
|
|
|
try {
|
|
const response = await modelsRoute.GET(
|
|
new Request(`http://localhost/api/providers/${connection.id}/models?refresh=true`),
|
|
{ params: { id: connection.id } }
|
|
);
|
|
assert.equal(response.status, 200);
|
|
const body = await response.json();
|
|
assert.ok(
|
|
body.models.length > ALIBABA_MODEL_STUDIO_MODEL_IDS.length,
|
|
"free billing mode should expose more than the curated catalog"
|
|
);
|
|
assert.ok(
|
|
body.models.some((model: { id: string }) => model.id === "qwen3-coder-next"),
|
|
"expected upstream-only free-tier model to be included"
|
|
);
|
|
assert.equal(
|
|
body.models.some((model: { id: string }) => model.id === "qwen-image-2.0-pro"),
|
|
false,
|
|
"non-text models must still be filtered out"
|
|
);
|
|
assert.equal(
|
|
body.models.some((model: { id: string }) => model.id === "kimi-k2.7-code"),
|
|
false,
|
|
"third-party paid-only models must be excluded from free billing discovery"
|
|
);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|