Files
OmniRoute/open-sse/services/dashscopeTextModels.ts
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

110 lines
4.0 KiB
TypeScript

/**
* @file dashscopeTextModels.ts
* @description DashScope / Alibaba Model Studio text and vision model ID heuristics.
*
* @changes
* - [2026-07-25] [Composer] - Add alibabafree text combo name detection for strict allowlist routing
* - [2026-07-25] [Composer] - Add multimodal and audio model detection for free-tier combos
* - [2026-07-25] [Composer] - Add vision/media model detection for alibabafreevision
* - [2026-07-25] [Composer] - Extract DashScope text-model filter for open-sse consumers
*/
const DASHSCOPE_TEXT_MODEL_PREFIXES = [
"qwen",
"qwq-",
"deepseek-",
"glm-",
"kimi-",
"minimax-",
] as const;
const DASHSCOPE_VISION_MODEL_PREFIXES = ["wan", "qwen-image", "happyhorse", "z-image"] as const;
const DASHSCOPE_NON_TEXT_MODEL_TOKEN =
/(?:^|[-_.\/])(?:asr|audio|captioner|embedding|image|livetranslate|omni|ocr|realtime|rerank|s2s|speech|tts|video|vl)(?:$|[-_.\/])/i;
const DASHSCOPE_VISION_MODEL_TOKEN =
/(?:^|[-_.\/])(?:i2v|t2v|r2v|vace|kf2v|videoedit|animate|image-edit)(?:$|[-_.\/])/i;
export function isDashscopeTextModelId(value: unknown): boolean {
if (typeof value !== "string") return false;
const modelId = value.trim().toLowerCase();
if (!modelId || DASHSCOPE_NON_TEXT_MODEL_TOKEN.test(modelId)) return false;
return DASHSCOPE_TEXT_MODEL_PREFIXES.some((prefix) => modelId.startsWith(prefix));
}
export function isDashscopeVisionModelId(value: unknown): value is string {
if (typeof value !== "string") return false;
const modelId = value.trim().toLowerCase();
if (!modelId || isDashscopeTextModelId(modelId)) return false;
return (
DASHSCOPE_VISION_MODEL_PREFIXES.some((prefix) => modelId.startsWith(prefix)) ||
DASHSCOPE_VISION_MODEL_TOKEN.test(modelId)
);
}
const DASHSCOPE_AUDIO_PREFIXES = [
"cosyvoice",
"fun-asr",
"qwen-audio",
"qwen-voice",
"voice-enrollment",
] as const;
const DASHSCOPE_AUDIO_MODEL_TOKEN =
/(?:^|[-_.\/])(?:asr|tts|livetranslate|captioner|speech|voice-design|voice-enrollment)(?:$|[-_.\/])/i;
const DASHSCOPE_MULTIMODAL_MODEL_TOKEN = /(?:^|[-_.\/])omni(?:$|[-_.\/])/i;
export function isDashscopeAudioModelId(value: unknown): value is string {
if (typeof value !== "string") return false;
const modelId = value.trim().toLowerCase();
if (!modelId) return false;
return (
DASHSCOPE_AUDIO_PREFIXES.some((prefix) => modelId.startsWith(prefix)) ||
DASHSCOPE_AUDIO_MODEL_TOKEN.test(modelId)
);
}
export function isDashscopeMultimodalModelId(value: unknown): value is string {
if (typeof value !== "string") return false;
const modelId = value.trim().toLowerCase();
if (!modelId || isDashscopeAudioModelId(modelId) || isDashscopeVisionModelId(modelId)) {
return false;
}
return DASHSCOPE_MULTIMODAL_MODEL_TOKEN.test(modelId);
}
export function isAlibabaFreeTierTextComboName(comboName: string | null | undefined): boolean {
if (!comboName) return false;
const normalized = comboName.trim().toLowerCase();
if (
isAlibabaFreeTierVisionComboName(normalized) ||
isAlibabaFreeTierMultimodalComboName(normalized) ||
isAlibabaFreeTierAudioComboName(normalized)
) {
return false;
}
return normalized === "alibabafree" || normalized.endsWith("alibabafree");
}
export function isAlibabaFreeTierVisionComboName(comboName: string | null | undefined): boolean {
if (!comboName) return false;
const normalized = comboName.trim().toLowerCase();
return normalized === "alibabafreevision" || normalized.endsWith("freevision");
}
export function isAlibabaFreeTierMultimodalComboName(
comboName: string | null | undefined
): boolean {
if (!comboName) return false;
const normalized = comboName.trim().toLowerCase();
return normalized === "alibabafreemultimodal" || normalized.endsWith("freemultimodal");
}
export function isAlibabaFreeTierAudioComboName(comboName: string | null | undefined): boolean {
if (!comboName) return false;
const normalized = comboName.trim().toLowerCase();
return normalized === "alibabafreeaudio" || normalized.endsWith("freeaudio");
}