Files
OmniRoute/scripts/ops/sync-alibaba-allowlist.mjs
Andrew B. 21fd0a94f8 feat(alibaba): free-tier routing with live quota sync (#8893)
* 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>
2026-08-11 04:25:23 -03:00

102 lines
3.5 KiB
JavaScript

#!/usr/bin/env node
/**
* @file sync-alibaba-allowlist.mjs
* @description Build config/alibaba-free-tier-allowlist.json from Bailian console quota JSON exports.
*
* Usage:
* node --import tsx/esm scripts/ops/sync-alibaba-allowlist.mjs path/to/quota.json [...]
* node --import tsx/esm scripts/ops/sync-alibaba-allowlist.mjs --from-samples
*
* Writes:
* - config/alibaba-free-tier-allowlist.json (repo baseline)
* - ~/.omniroute/alibaba-free-tier-allowlist.json when DATA_DIR unset
*/
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import {
classifyAlibabaFreeTierQuotaEntries,
parseAlibabaFreeTierQuotaEntries,
} from "../../open-sse/services/alibabaFreeTierQuotaFetcher.ts";
import { isDashscopeTextModelId } from "../../open-sse/services/dashscopeTextModels.ts";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(__dirname, "../..");
const SAMPLE_FILES = [
"scripts/ops/alibabafreeaudio-quota.sample.json",
"scripts/ops/alibabafreemultimodal-quota.sample.json",
"scripts/ops/alibabafreevision-quota.sample.json",
].map((relativePath) => path.join(repoRoot, relativePath));
function readJsonFile(filePath) {
return JSON.parse(fs.readFileSync(filePath, "utf8"));
}
function collectInputs(argv) {
if (argv.includes("--from-samples")) {
return SAMPLE_FILES.filter((filePath) => fs.existsSync(filePath));
}
return argv.filter((arg) => !arg.startsWith("-"));
}
function classifyTextEntries(allEntries) {
const capable = new Set();
const noFreeTier = new Set();
for (const entry of allEntries) {
if (!isDashscopeTextModelId(entry.model)) continue;
const classified = classifyAlibabaFreeTierQuotaEntries([entry], { textOnly: true });
for (const modelId of classified.capableModels) capable.add(modelId);
for (const modelId of classified.noFreeTierModels) noFreeTier.add(modelId);
}
return {
capable: [...capable].sort(),
noFreeTier: [...noFreeTier].sort(),
};
}
function main() {
const inputs = collectInputs(process.argv.slice(2));
if (inputs.length === 0) {
console.error("Usage: sync-alibaba-allowlist.mjs <quota.json> [...] | --from-samples");
process.exit(1);
}
const allEntries = [];
for (const inputPath of inputs) {
const payload = readJsonFile(inputPath);
allEntries.push(...parseAlibabaFreeTierQuotaEntries(payload));
}
const { capable, noFreeTier } = classifyTextEntries(allEntries);
if (capable.length === 0) {
console.error("No text free-tier models found in input payloads.");
process.exit(1);
}
const asOf = new Date().toISOString().slice(0, 10);
const validUntil = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString().slice(0, 10);
const pack = { asOf, validUntil, capable, noFreeTier };
const serialized = `${JSON.stringify(pack, null, 2)}\n`;
const configPath = path.join(repoRoot, "config", "alibaba-free-tier-allowlist.json");
fs.mkdirSync(path.dirname(configPath), { recursive: true });
fs.writeFileSync(configPath, serialized);
const dataDir = process.env.DATA_DIR?.trim() || path.join(os.homedir(), ".omniroute");
const runtimePath = path.join(dataDir, "alibaba-free-tier-allowlist.json");
fs.mkdirSync(dataDir, { recursive: true });
fs.writeFileSync(runtimePath, serialized);
console.log(`Wrote ${capable.length} capable + ${noFreeTier.length} blocked models`);
console.log(` config: ${configPath}`);
console.log(` runtime: ${runtimePath}`);
console.log(` validUntil: ${validUntil}`);
}
main();