Files
OmniRoute/open-sse/services/autoCombo/autoPrefix.ts
oyi77 e1ab7c9273 feat(auto): complete zero-config auto-routing feature
- Add auto-prefix parser (autoPrefix.ts) for auto/Cvariant detection
- Add virtual auto-combo factory (virtualFactory.ts) building combos from active providers
- Integrate auto/ prefix into chat routing (chat.ts) - supports bare 'auto' and 'auto/variant'
- Add system provider 'auto' in providers.ts (systemOnly)
- Add AutoRoutingBanner component with localStorage dismissal
- Add auto-routing settings in RoutingTab (toggle + variant selector)
- Add auto-routing analytics tab (AutoRoutingAnalyticsTab) + API endpoint
- Add Case 0 zero-config documentation to README.md
- Add autoRoutingEnabled/enforcement and autoRoutingDefaultVariant settings
- Add analytics endpoint auth via requireManagementAuth
- Add empty-pool graceful handling in virtualFactory
- Add dynamic import error handling with try/catch
- Tests: 126/126 passing
2026-05-11 01:49:10 +07:00

55 lines
1.9 KiB
TypeScript

export type AutoVariant = "coding" | "fast" | "cheap" | "offline" | "smart" | "lkgp";
export interface AutoPrefixParseResult {
valid: boolean;
variant?: AutoVariant;
error?: string;
}
const VALID_VARIANTS: AutoVariant[] = ["coding", "fast", "cheap", "offline", "smart", "lkgp"];
/**
* Parses a model name to determine if it's an auto-prefixed model and extracts the variant.
*
* Examples:
* - "auto" -> { valid: true, variant: undefined } (default)
* - "auto/coding" -> { valid: true, variant: "coding" }
* - "auto/lkgp" -> { valid: true, variant: "lkgp" }
* - "auto/" -> { valid: true, variant: undefined } (default)
* - "autocoding" -> { valid: false, error: "Invalid auto prefix format" }
* - "otherModel" -> { valid: false, error: "Not an auto-prefixed model" }
*/
export function parseAutoPrefix(model: string | null | undefined): AutoPrefixParseResult {
// Guard against null/undefined (called with non-string inputs)
if (typeof model !== "string") {
return { valid: false, error: "Not an auto-prefixed model" };
}
if (!model.startsWith("auto")) {
return { valid: false, error: "Not an auto-prefixed model" };
}
const parts = model.split("/");
if (parts.length === 1) {
if (parts[0] === "auto") {
return { valid: true, variant: undefined }; // Default auto
} else {
return { valid: false, error: "Invalid auto prefix format" };
}
}
if (parts.length === 2) {
if (parts[0] !== "auto") {
return { valid: false, error: "Invalid auto prefix format" };
}
const variantStr: string = parts[1];
if (variantStr === "" || VALID_VARIANTS.includes(variantStr as AutoVariant)) {
return { valid: true, variant: variantStr === "" ? undefined : (variantStr as AutoVariant) };
} else {
return { valid: false, error: `Invalid auto variant: ${variantStr}` };
}
}
return { valid: false, error: "Invalid auto prefix format" };
}