Spaces:
Runtime error
Runtime error
File size: 7,213 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | type JsonRecord = Record<string, unknown>;
export const INTELLIGENT_STRATEGIES = ["auto", "lkgp"] as const;
export const INTELLIGENT_ROUTING_FILTERS = ["all", "intelligent", "deterministic"] as const;
export type IntelligentRoutingFilter = (typeof INTELLIGENT_ROUTING_FILTERS)[number];
export type IntelligentRoutingWeights = {
quota: number;
health: number;
costInv: number;
latencyInv: number;
taskFit: number;
stability: number;
tierPriority: number;
tierAffinity: number;
specificityMatch: number;
contextAffinity: number;
resetWindowAffinity: number;
};
export type IntelligentRoutingConfig = {
candidatePool: string[];
explorationRate: number;
modePack: string;
budgetCap?: number;
weights: IntelligentRoutingWeights;
routerStrategy: string;
slaTargetP95Ms?: number;
slaMaxErrorRate?: number;
slaMaxCostPer1MTokens?: number;
slaHardConstraints: boolean;
};
export type IntelligentProviderScore = {
provider: string;
model: string;
score: number;
factors: IntelligentRoutingWeights;
};
export const DEFAULT_INTELLIGENT_WEIGHTS: IntelligentRoutingWeights = {
quota: 0.16,
health: 0.2,
costInv: 0.16,
latencyInv: 0.12,
taskFit: 0.08,
stability: 0.05,
tierPriority: 0.05,
tierAffinity: 0.05,
specificityMatch: 0.05,
contextAffinity: 0.08,
resetWindowAffinity: 0,
};
export const MODE_PACK_OPTIONS = [
{ id: "ship-fast", label: "Ship Fast", emoji: "rocket_launch" },
{ id: "cost-saver", label: "Cost Saver", emoji: "savings" },
{ id: "quality-first", label: "Quality First", emoji: "target" },
{ id: "offline-friendly", label: "Offline Friendly", emoji: "cloud_off" },
] as const;
export const ROUTER_STRATEGY_OPTIONS = [
{ id: "rules", label: "Rules (6-Factor Scoring)" },
{ id: "cost", label: "Cost Optimized" },
{ id: "latency", label: "Latency Optimized" },
{ id: "sla-aware", label: "SLA-aware" },
{ id: "lkgp", label: "Last Known Good Provider" },
] as const;
export const FACTOR_LABELS: Record<keyof IntelligentRoutingWeights, string> = {
quota: "Quota",
health: "Health",
costInv: "Cost",
latencyInv: "Latency",
taskFit: "Task Fit",
stability: "Stability",
tierPriority: "Tier",
tierAffinity: "Tier Affinity",
specificityMatch: "Specificity",
contextAffinity: "Context Affinity",
resetWindowAffinity: "Reset Window",
};
function isRecord(value: unknown): value is JsonRecord {
return !!value && typeof value === "object" && !Array.isArray(value);
}
function toFiniteNumber(value: unknown): number | null {
const numericValue = Number(value);
return Number.isFinite(numericValue) ? numericValue : null;
}
function toPositiveNumber(value: unknown): number | undefined {
const numericValue = toFiniteNumber(value);
return numericValue !== null && numericValue > 0 ? numericValue : undefined;
}
export function isIntelligentStrategy(strategy: unknown): boolean {
return typeof strategy === "string" && INTELLIGENT_STRATEGIES.includes(strategy as never);
}
export function getStrategyCategory(strategy: unknown): "intelligent" | "deterministic" {
return isIntelligentStrategy(strategy) ? "intelligent" : "deterministic";
}
export function normalizeIntelligentRoutingFilter(value: unknown): IntelligentRoutingFilter {
if (typeof value === "string" && INTELLIGENT_ROUTING_FILTERS.includes(value as never)) {
return value as IntelligentRoutingFilter;
}
return "all";
}
export function filterCombosByStrategyCategory<T extends { strategy?: unknown }>(
combos: T[],
filter: IntelligentRoutingFilter
): T[] {
if (filter === "all") return combos;
return combos.filter((combo) => getStrategyCategory(combo?.strategy) === filter);
}
export function normalizeIntelligentRoutingConfig(config: unknown): IntelligentRoutingConfig {
const configRecord = isRecord(config) ? config : {};
const rawWeights = isRecord(configRecord.weights) ? configRecord.weights : {};
const rawSla = isRecord(configRecord.sla) ? configRecord.sla : {};
const slaTargetP95Ms = configRecord.slaTargetP95Ms ?? rawSla.targetP95Ms;
const slaMaxErrorRate = toFiniteNumber(configRecord.slaMaxErrorRate ?? rawSla.maxErrorRate);
const slaMaxCostPer1MTokens = configRecord.slaMaxCostPer1MTokens ?? rawSla.maxCostPer1MTokens;
const slaHardConstraints = configRecord.slaHardConstraints ?? rawSla.hardConstraints;
return {
candidatePool: Array.isArray(configRecord.candidatePool)
? configRecord.candidatePool.filter((value): value is string => typeof value === "string")
: [],
explorationRate: Math.min(1, Math.max(0, toFiniteNumber(configRecord.explorationRate) ?? 0.05)),
modePack:
typeof configRecord.modePack === "string" && configRecord.modePack.trim().length > 0
? configRecord.modePack
: "ship-fast",
budgetCap: toPositiveNumber(configRecord.budgetCap),
weights: {
quota: toFiniteNumber(rawWeights.quota) ?? DEFAULT_INTELLIGENT_WEIGHTS.quota,
health: toFiniteNumber(rawWeights.health) ?? DEFAULT_INTELLIGENT_WEIGHTS.health,
costInv: toFiniteNumber(rawWeights.costInv) ?? DEFAULT_INTELLIGENT_WEIGHTS.costInv,
latencyInv: toFiniteNumber(rawWeights.latencyInv) ?? DEFAULT_INTELLIGENT_WEIGHTS.latencyInv,
taskFit: toFiniteNumber(rawWeights.taskFit) ?? DEFAULT_INTELLIGENT_WEIGHTS.taskFit,
stability: toFiniteNumber(rawWeights.stability) ?? DEFAULT_INTELLIGENT_WEIGHTS.stability,
tierPriority:
toFiniteNumber(rawWeights.tierPriority) ?? DEFAULT_INTELLIGENT_WEIGHTS.tierPriority,
tierAffinity:
toFiniteNumber(rawWeights.tierAffinity) ?? DEFAULT_INTELLIGENT_WEIGHTS.tierAffinity,
specificityMatch:
toFiniteNumber(rawWeights.specificityMatch) ?? DEFAULT_INTELLIGENT_WEIGHTS.specificityMatch,
contextAffinity:
toFiniteNumber(rawWeights.contextAffinity) ?? DEFAULT_INTELLIGENT_WEIGHTS.contextAffinity,
resetWindowAffinity:
toFiniteNumber(rawWeights.resetWindowAffinity) ??
DEFAULT_INTELLIGENT_WEIGHTS.resetWindowAffinity,
},
routerStrategy:
typeof configRecord.routerStrategy === "string" &&
configRecord.routerStrategy.trim().length > 0
? configRecord.routerStrategy
: "rules",
slaTargetP95Ms: toPositiveNumber(slaTargetP95Ms),
slaMaxErrorRate:
slaMaxErrorRate !== null ? Math.min(1, Math.max(0, slaMaxErrorRate)) : undefined,
slaMaxCostPer1MTokens: toPositiveNumber(slaMaxCostPer1MTokens),
slaHardConstraints: slaHardConstraints === true,
};
}
export function buildIntelligentProviderScores(combo: {
config?: unknown;
weights?: unknown;
}): IntelligentProviderScore[] {
const configRecord = normalizeIntelligentRoutingConfig(combo?.config);
const comboWeights = isRecord(combo?.weights) ? combo.weights : combo?.config;
const weights = normalizeIntelligentRoutingConfig({
...(isRecord(comboWeights) ? comboWeights : {}),
weights: isRecord(combo?.weights) ? combo.weights : configRecord.weights,
}).weights;
const pool = configRecord.candidatePool;
const baseScore = pool.length > 0 ? 1 / pool.length : 0;
return pool.map((provider) => ({
provider,
model: "auto",
score: baseScore,
factors: weights,
}));
}
|