mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-18 21:02:50 +03:00
Merged as part of the 39-PR owner batch of 2026-09-11, validated as a unit. Boarded into one consolidated worktree cut from `release/v3.8.51` with the other 38 — zero conflicts between them. - ESLint over every changed file: no errors (the only finding was one suppression entry the batch emptied, pruned on #13243) - `typecheck:core` clean; `check:dashboard-typecheck` OK (206 pre-existing, within baseline); `check:changelog-integrity` OK - complexity 2821 / baseline 3218 and cognitive-complexity 1272 / baseline 1437 — both under baseline - 256 assertions green: 246 under node:test and 10 under vitest, which is where `tests/unit/**/*.test.tsx` actually runs - `check-file-size`: `chatCore.ts` rebaselined 6144 → 6146 for #13278 and #13276, annotated and landed on #13243 ⚠️ base-red inherited: #12732 — the provider count (356 in the docs vs the 358 the modules define) and `open-sse/utils/stream.ts` at 3115 > frozen 3098 both reproduce on the pure tip with zero contribution from this batch.
295 lines
9.8 KiB
TypeScript
295 lines
9.8 KiB
TypeScript
import { normalizeComboModels, type ComboStep } from "./steps";
|
|
import { resolveComboTargetModelStr } from "../../../open-sse/services/combo/opencodeTargetAlias.ts";
|
|
import { resolveProviderAlias } from "../../../open-sse/services/model.ts";
|
|
|
|
type JsonRecord = Record<string, unknown>;
|
|
|
|
export type ComboControlCenterHealthState = "healthy" | "warning" | "critical" | "idle";
|
|
|
|
export interface ComboControlCenterCombo {
|
|
id?: string;
|
|
name?: string;
|
|
strategy?: string | null;
|
|
models?: unknown[];
|
|
isActive?: boolean | null;
|
|
config?: JsonRecord | null;
|
|
}
|
|
|
|
export interface ComboControlCenterMetrics {
|
|
totalRequests?: number;
|
|
totalSuccesses?: number;
|
|
totalFailures?: number;
|
|
totalFallbacks?: number;
|
|
avgLatencyMs?: number;
|
|
successRate?: number;
|
|
fallbackRate?: number;
|
|
lastUsedAt?: string | null;
|
|
}
|
|
|
|
export interface ComboControlCenterHealth {
|
|
performance?: {
|
|
avgLatencyMs?: number;
|
|
successRate?: number;
|
|
totalRequests?: number;
|
|
};
|
|
quotaHealth?: {
|
|
worstRemainingPct?: number | null;
|
|
providers?: Array<{
|
|
provider: string;
|
|
remainingPct: number | null;
|
|
isExhausted: boolean;
|
|
trend: "improving" | "stable" | "declining";
|
|
}>;
|
|
};
|
|
usageSkew?: {
|
|
giniCoefficient?: number;
|
|
modelDistribution?: Array<{ model: string; requestShare: number; tokenShare: number }>;
|
|
};
|
|
targetHealth?: ComboControlCenterTargetHealth[];
|
|
}
|
|
|
|
export interface ComboControlCenterTargetHealth {
|
|
executionKey?: string;
|
|
stepId?: string | null;
|
|
model?: string;
|
|
provider?: string;
|
|
connectionId?: string | null;
|
|
label?: string | null;
|
|
requests?: number;
|
|
successRate?: number;
|
|
avgLatencyMs?: number;
|
|
lastStatus?: "ok" | "error" | null;
|
|
lastUsedAt?: string | null;
|
|
quotaRemainingPct?: number | null;
|
|
quotaIsExhausted?: boolean | null;
|
|
quotaTrend?: "improving" | "stable" | "declining" | null;
|
|
quotaScope?: "connection" | "provider" | "none";
|
|
}
|
|
|
|
export interface ComboControlCenterTarget {
|
|
id: string;
|
|
kind: "model" | "combo-ref" | "provider-wildcard";
|
|
index: number;
|
|
label: string;
|
|
model: string;
|
|
provider: string | null;
|
|
connectionId: string | null;
|
|
weight: number;
|
|
tags: string[];
|
|
health: ComboControlCenterTargetHealth | null;
|
|
}
|
|
|
|
export interface ComboControlCenterSummary {
|
|
strategy: string;
|
|
isActive: boolean;
|
|
targetCount: number;
|
|
modelTargetCount: number;
|
|
nestedComboCount: number;
|
|
providerCount: number;
|
|
totalRequests: number;
|
|
successRate: number;
|
|
avgLatencyMs: number;
|
|
fallbackRate: number;
|
|
worstQuotaRemainingPct: number | null;
|
|
usageSkew: number;
|
|
healthState: ComboControlCenterHealthState;
|
|
healthReasons: string[];
|
|
}
|
|
|
|
function isRecord(value: unknown): value is JsonRecord {
|
|
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
}
|
|
|
|
function toNumber(value: unknown, fallback = 0): number {
|
|
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
}
|
|
|
|
function toString(value: unknown): string | null {
|
|
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
|
}
|
|
|
|
function providerFromModel(model: string | null | undefined): string | null {
|
|
if (!model) return null;
|
|
// #11912: resolve through the same "opencode" -> "oc" combo-target alias
|
|
// treatment (and then the general alias table) that target resolution
|
|
// applies before dispatch, so this label matches what actually executed
|
|
// upstream instead of a raw, un-aliased prefix slice.
|
|
const normalized = resolveComboTargetModelStr(model);
|
|
const slashIndex = normalized.indexOf("/");
|
|
if (slashIndex <= 0) return null;
|
|
const prefix = normalized.slice(0, slashIndex);
|
|
return resolveProviderAlias(prefix) || prefix;
|
|
}
|
|
|
|
function normalizeSuccessRate(value: unknown): number {
|
|
const rate = toNumber(value, 0);
|
|
if (rate <= 1) return Math.round(rate * 100);
|
|
return Math.round(rate);
|
|
}
|
|
|
|
function getMetricRequests(metrics?: ComboControlCenterMetrics | null): number {
|
|
return toNumber(metrics?.totalRequests, 0);
|
|
}
|
|
|
|
function getHealthRequests(health?: ComboControlCenterHealth | null): number {
|
|
return toNumber(health?.performance?.totalRequests, 0);
|
|
}
|
|
|
|
function getStepTags(step: ComboStep): string[] {
|
|
return step.kind === "model" && Array.isArray(step.tags) ? step.tags : [];
|
|
}
|
|
|
|
function getStepLabel(step: ComboStep): string {
|
|
if (step.label) return step.label;
|
|
if (step.kind === "combo-ref") return `Combo → ${step.comboName}`;
|
|
if (step.kind === "provider-wildcard") {
|
|
return `All ${step.providerId}/${step.modelPattern}`;
|
|
}
|
|
return step.model;
|
|
}
|
|
|
|
export function getComboControlCenterTargets(
|
|
combo: ComboControlCenterCombo,
|
|
health?: ComboControlCenterHealth | null
|
|
): ComboControlCenterTarget[] {
|
|
const steps = normalizeComboModels(combo.models || [], { comboName: combo.name || null });
|
|
const healthByStepId = new Map<string, ComboControlCenterTargetHealth>();
|
|
const healthByModel = new Map<string, ComboControlCenterTargetHealth>();
|
|
|
|
for (const target of health?.targetHealth || []) {
|
|
const stepId = toString(target.stepId);
|
|
const model = toString(target.model);
|
|
if (stepId) healthByStepId.set(stepId, target);
|
|
if (model && !healthByModel.has(model)) healthByModel.set(model, target);
|
|
}
|
|
|
|
return steps.map((step, index) => {
|
|
const model =
|
|
step.kind === "combo-ref"
|
|
? step.comboName
|
|
: step.kind === "provider-wildcard"
|
|
? `${step.providerId}/${step.modelPattern}`
|
|
: step.model;
|
|
const healthEntry = healthByStepId.get(step.id) || healthByModel.get(model) || null;
|
|
const provider =
|
|
step.kind === "model"
|
|
? step.providerId || providerFromModel(step.model) || healthEntry?.provider || null
|
|
: step.kind === "provider-wildcard"
|
|
? step.providerId
|
|
: null;
|
|
|
|
return {
|
|
id: step.id,
|
|
kind: step.kind,
|
|
index,
|
|
label: getStepLabel(step),
|
|
model,
|
|
provider,
|
|
connectionId:
|
|
step.kind === "model" || step.kind === "provider-wildcard"
|
|
? step.connectionId || null
|
|
: null,
|
|
weight: step.weight || 0,
|
|
tags: getStepTags(step),
|
|
health: healthEntry,
|
|
};
|
|
});
|
|
}
|
|
|
|
export function getResolvedComboControlCenterTargets(
|
|
health?: ComboControlCenterHealth | null
|
|
): ComboControlCenterTargetHealth[] {
|
|
return Array.isArray(health?.targetHealth) ? health.targetHealth : [];
|
|
}
|
|
|
|
export function summarizeComboControlCenter(
|
|
combo: ComboControlCenterCombo,
|
|
metrics?: ComboControlCenterMetrics | null,
|
|
health?: ComboControlCenterHealth | null
|
|
): ComboControlCenterSummary {
|
|
const targets = getComboControlCenterTargets(combo, health);
|
|
const resolvedTargets = getResolvedComboControlCenterTargets(health);
|
|
const providers = new Set<string>();
|
|
for (const target of targets) {
|
|
if (target.provider) providers.add(target.provider);
|
|
}
|
|
for (const target of resolvedTargets) {
|
|
const provider = toString(target.provider);
|
|
if (provider) providers.add(provider);
|
|
}
|
|
|
|
const healthRequests = getHealthRequests(health);
|
|
const metricRequests = getMetricRequests(metrics);
|
|
const totalRequests = healthRequests || metricRequests;
|
|
const successRate =
|
|
healthRequests > 0
|
|
? normalizeSuccessRate(health?.performance?.successRate)
|
|
: normalizeSuccessRate(metrics?.successRate);
|
|
const avgLatencyMs =
|
|
toNumber(health?.performance?.avgLatencyMs, 0) || toNumber(metrics?.avgLatencyMs, 0);
|
|
const fallbackRate = normalizeSuccessRate(metrics?.fallbackRate);
|
|
const worstQuotaRemainingPct =
|
|
typeof health?.quotaHealth?.worstRemainingPct === "number"
|
|
? health.quotaHealth.worstRemainingPct
|
|
: null;
|
|
const usageSkew = toNumber(health?.usageSkew?.giniCoefficient, 0);
|
|
const hasExhaustedQuota = Boolean(
|
|
health?.quotaHealth?.providers?.some((provider) => provider.isExhausted)
|
|
);
|
|
|
|
const healthReasons: string[] = [];
|
|
if (totalRequests === 0) healthReasons.push("No recent combo traffic");
|
|
if (successRate > 0 && successRate < 80) healthReasons.push("Low success rate");
|
|
else if (successRate > 0 && successRate < 95) healthReasons.push("Success rate below target");
|
|
if (fallbackRate >= 20) healthReasons.push("High fallback rate");
|
|
else if (fallbackRate >= 10) healthReasons.push("Elevated fallback rate");
|
|
if (hasExhaustedQuota) healthReasons.push("At least one quota is exhausted");
|
|
else if (worstQuotaRemainingPct !== null && worstQuotaRemainingPct < 10) {
|
|
healthReasons.push("Quota is nearly exhausted");
|
|
} else if (worstQuotaRemainingPct !== null && worstQuotaRemainingPct < 25) {
|
|
healthReasons.push("Quota is getting low");
|
|
}
|
|
if (usageSkew >= 0.5) healthReasons.push("Traffic distribution is highly skewed");
|
|
|
|
let healthState: ComboControlCenterHealthState = "healthy";
|
|
if (totalRequests === 0 && worstQuotaRemainingPct === null) {
|
|
healthState = "idle";
|
|
} else if (
|
|
hasExhaustedQuota ||
|
|
(successRate > 0 && successRate < 80) ||
|
|
(worstQuotaRemainingPct !== null && worstQuotaRemainingPct < 10)
|
|
) {
|
|
healthState = "critical";
|
|
} else if (
|
|
(successRate > 0 && successRate < 95) ||
|
|
fallbackRate >= 10 ||
|
|
(worstQuotaRemainingPct !== null && worstQuotaRemainingPct < 25) ||
|
|
usageSkew >= 0.5
|
|
) {
|
|
healthState = "warning";
|
|
}
|
|
|
|
return {
|
|
strategy: combo.strategy || "priority",
|
|
isActive: combo.isActive !== false,
|
|
targetCount: targets.length,
|
|
modelTargetCount: targets.filter(
|
|
(target) => target.kind === "model" || target.kind === "provider-wildcard"
|
|
).length,
|
|
nestedComboCount: targets.filter((target) => target.kind === "combo-ref").length,
|
|
providerCount: providers.size,
|
|
totalRequests,
|
|
successRate,
|
|
avgLatencyMs,
|
|
fallbackRate,
|
|
worstQuotaRemainingPct,
|
|
usageSkew,
|
|
healthState,
|
|
healthReasons: healthReasons.length > 0 ? healthReasons : ["Combo looks healthy"],
|
|
};
|
|
}
|
|
|
|
export function extractComboRuntimeConfig(combo: ComboControlCenterCombo): JsonRecord {
|
|
return isRecord(combo.config) ? combo.config : {};
|
|
}
|