export const LUNA_CHAT_MODEL = "openai-codex/gpt-6-luna"; export const NATIVE_CLASSIFIER_BUDGET_MS = 500; export const LUNA_CLASSIFIER_BUDGET_MS = 2_000; export const TURN_FALLBACK_MS = 1_500; export const TURN_HARD_CAP_MS = 3_000; export const MAX_NATIVE_INPUT_CHARS = 100_000; export type TurnClassification = { available: boolean; choice?: "complete" | "trailing" | "mid-clause"; pComplete?: number; scores?: Record; latencyMs?: number; source?: "native" | "host-chat"; model?: string; }; export type TurnPolicy = { waitMs: number; probabilityMilli: number; outcome: "scored" | "labelled" | "fallback"; description: string; }; const labelWaits = { complete: 300, trailing: 2_000, "mid-clause": 2_000, } as const; const MAX_ENDPOINT_AGE_MS = 1_000; export function endpointSilenceAnchor( now: number, endpointAgeMs: unknown, observedAnchor: number, ): number { const anchor = now === 0 ? -Number.EPSILON : now; if ( typeof endpointAgeMs !== "number" || !Number.isFinite(endpointAgeMs) || endpointAgeMs < 0 || endpointAgeMs > MAX_ENDPOINT_AGE_MS ) return anchor; const credited = now - endpointAgeMs; const candidate = credited === 0 ? -Number.EPSILON : credited; return observedAnchor === 0 ? candidate : Math.min(observedAnchor, candidate); } export function turnPolicy(value: unknown): TurnPolicy { if (!value || typeof value !== "object" || Array.isArray(value)) return fallback("Classifier result malformed; using fallback wait."); const result = value as Record; if (result["available"] !== true) return fallback("Classifier unavailable; using fallback wait."); if (result["source"] === "host-chat") { const choice = result["choice"]; if ( choice === "complete" || choice === "trailing" || choice === "mid-clause" ) return { waitMs: labelWaits[choice], probabilityMilli: -1, outcome: "labelled", description: `Host-chat label ${choice}; non-native, uncalibrated; no confidence score.`, }; return fallback("Host-chat label invalid; using fallback wait."); } if (result["source"] !== undefined && result["source"] !== "native") return fallback("Classifier source invalid; using fallback wait."); const probability = result["pComplete"]; if ( typeof probability === "number" && Number.isFinite(probability) && probability >= 0 && probability <= 1 ) return { waitMs: Math.round(300 + 1_700 * (1 - probability)), probabilityMilli: Math.round(probability * 1_000), outcome: "scored", description: `Native classifier probability ${probability.toFixed(3)}.`, }; return fallback("Classifier probability invalid; using fallback wait."); } function fallback(description: string): TurnPolicy { return { waitMs: TURN_FALLBACK_MS, probabilityMilli: -1, outcome: "fallback", description, }; }