Spaces:
Runtime error
Runtime error
File size: 2,444 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 | /**
* planResolver.ts — Resolve the quota plan for a provider connection.
*
* Precedence (highest to lowest):
* 1. Manual DB override (provider_plans table via getProviderPlan)
* 2. Known catalog (planRegistry.ts)
* 3. Empty plan (no dimensions — manual configuration required)
*
* Runtime signals (upstream response headers) are accepted for future
* extensibility but ignored in v1.
*
* Part of: Group B — Quota Sharing Engine (plan 22, frente F6).
*/
import { getProviderPlan } from "@/lib/localDb";
import { getKnownPlan } from "./planRegistry";
import type { ProviderPlan } from "./dimensions";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface RuntimeSignals {
/** Headers from upstream response (e.g. anthropic-ratelimit-unified-5h-utilization). */
headers?: Record<string, string>;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
* Resolve the effective ProviderPlan for a connection.
*
* @param connectionId Unique provider connection ID (from DB).
* @param provider Provider name (e.g. "codex", "kimi").
* @param _runtimeSignals Optional upstream headers / signals (v1: ignored, reserved for future use).
* @returns The effective ProviderPlan (never throws).
*/
export function resolvePlan(
connectionId: string,
provider: string,
_runtimeSignals?: RuntimeSignals
): ProviderPlan {
// 1. Manual DB override
try {
const dbPlan = getProviderPlan(connectionId);
if (dbPlan && dbPlan.dimensions.length > 0) {
return {
connectionId: dbPlan.connectionId,
provider: dbPlan.provider,
dimensions: dbPlan.dimensions as ProviderPlan["dimensions"],
source: dbPlan.source,
};
}
} catch {
// DB not available (e.g. test env without migration) — fall through
}
// 2. Known catalog
const catalogPlan = getKnownPlan(provider);
if (catalogPlan) {
return {
connectionId: null,
provider: catalogPlan.provider,
dimensions: catalogPlan.dimensions,
source: "auto",
};
}
// 3. Empty (manual configuration required)
return {
connectionId: null,
provider,
dimensions: [],
source: "manual",
};
}
|