Files
Sybil-2/web/src/lib/reasoning-effort.ts
T

44 lines
2.2 KiB
TypeScript

import type { ModelCatalogResponse, Provider } from "./api";
export const REASONING_EFFORTS = ["none", "minimal", "low", "medium", "high", "xhigh", "max"] as const;
export type ReasoningEffort = (typeof REASONING_EFFORTS)[number];
export type ReasoningCapabilities = { efforts: ReasoningEffort[]; defaultEffort: ReasoningEffort | null };
export type ReasoningPreferences = Record<string, ReasoningEffort | null>;
export const REASONING_PREFERENCES_STORAGE_KEY = "sybil:reasoningEffortByModel";
export function reasoningPreferenceKey(provider: Provider, model: string) {
return JSON.stringify([provider, model.trim()]);
}
export function getReasoningCapabilities(catalog: ModelCatalogResponse["providers"], provider: Provider, model: string): ReasoningCapabilities | null {
// Missing/old catalogs and unrecognized values must fail closed.
const entry = catalog[provider]?.reasoningCapabilities?.[model.trim()];
if (!entry || !Array.isArray(entry.efforts) || !entry.efforts.length
|| !entry.efforts.every((effort) => REASONING_EFFORTS.includes(effort))
|| new Set(entry.efforts).size !== entry.efforts.length
|| (entry.defaultEffort !== null && !entry.efforts.includes(entry.defaultEffort))) return null;
return entry;
}
export function normalizeReasoningEffort(capabilities: ReasoningCapabilities | null, effort: unknown): ReasoningEffort | null {
return capabilities?.efforts.includes(effort as ReasoningEffort) ? effort as ReasoningEffort : null;
}
export function reasoningEffortLabel(effort: ReasoningEffort) {
if (effort === "xhigh") return "Extra high";
return effort.charAt(0).toUpperCase() + effort.slice(1);
}
export function loadReasoningPreferences(): ReasoningPreferences {
try {
const parsed: unknown = JSON.parse(window.localStorage.getItem(REASONING_PREFERENCES_STORAGE_KEY) ?? "{}");
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
return Object.fromEntries(Object.entries(parsed).filter(([, value]) => value === null || REASONING_EFFORTS.includes(value)));
} catch { return {}; }
}
export function saveReasoningPreferences(preferences: ReasoningPreferences) {
try { window.localStorage.setItem(REASONING_PREFERENCES_STORAGE_KEY, JSON.stringify(preferences)); return true; }
catch { return false; }
}