From d63c8bb4e470d643c00f3ed3ebceadd999f07fc2 Mon Sep 17 00:00:00 2001 From: Camiel van Schoonhoven Date: Tue, 17 Nov 2026 16:46:47 -0700 Subject: [PATCH 1/3] feat(ai): consume optional deployment model catalogs --- src/config/aiConfig.ts | 34 ++++++- src/config/modelCatalog.test.ts | 114 +++++++++++++++++++++++ src/config/modelCatalog.ts | 63 +++++++++++++ src/hooks/useAiProviderSettings.ts | 37 +++++++- src/services/projects/startingSession.ts | 17 ++++ 5 files changed, 258 insertions(+), 7 deletions(-) create mode 100644 src/config/modelCatalog.test.ts create mode 100644 src/config/modelCatalog.ts diff --git a/src/config/aiConfig.ts b/src/config/aiConfig.ts index 4f8fefbbcd..64ed194376 100644 --- a/src/config/aiConfig.ts +++ b/src/config/aiConfig.ts @@ -7,10 +7,16 @@ import { isRecord } from "@/utils/typeGuards"; export interface AiModelOption extends AiModelCapabilities { label?: string; description?: string; + tangentModelId?: string; + api?: "openai-responses" | "anthropic-messages"; + thinkingLevelMap?: Record; } interface AiModelOptionsConfig { models?: AiModelOption[]; + catalogUrl?: string; + defaultModel?: string; + replacements?: Record; } export const AI_CONFIG = { @@ -112,6 +118,20 @@ function readModelOption(value: unknown): AiModelOption | null { return { id, + ...(typeof value.tangentModelId === "string" + ? { tangentModelId: value.tangentModelId } + : {}), + ...(value.api === "openai-responses" || value.api === "anthropic-messages" + ? { api: value.api } + : {}), + ...(isRecord(value.thinkingLevelMap) + ? { + thinkingLevelMap: value.thinkingLevelMap as Record< + string, + string | null + >, + } + : {}), ...(typeof value.label === "string" && value.label.trim() ? { label: value.label.trim() } : {}), @@ -130,6 +150,9 @@ function readInjectedModelOptions(): AiModelOptionsConfig | null { if (!isRecord(config)) return null; return { + ...(typeof config.defaultModel === "string" + ? { defaultModel: config.defaultModel } + : {}), ...(Array.isArray(config.models) ? { models: config.models.map(readModelOption).filter((v) => v !== null) } : {}), @@ -144,7 +167,9 @@ export function getAiModelOptions(): AiModelOption[] { } export function getDefaultAiModelId(): string { - return getAiModelOptions()[0].id; + const options = getAiModelOptions(); + const preferred = readInjectedModelOptions()?.defaultModel; + return options.find((m) => m.id === preferred)?.id ?? options[0].id; } export function supportsAiEmbeddings(apiBase: string): boolean { @@ -156,7 +181,12 @@ export function supportsAiEmbeddings(apiBase: string): boolean { } export function getAiProviderConfig(modelId: string) { - return AI_CONFIG.providers.anthropic.modelPattern.test(modelId) + const protocol = getAiModelOptions().find((m) => m.id === modelId)?.api; + return ( + protocol + ? protocol === "anthropic-messages" + : AI_CONFIG.providers.anthropic.modelPattern.test(modelId) + ) ? AI_CONFIG.providers.anthropic : AI_CONFIG.providers.openai; } diff --git a/src/config/modelCatalog.test.ts b/src/config/modelCatalog.test.ts new file mode 100644 index 0000000000..c7c7013b66 --- /dev/null +++ b/src/config/modelCatalog.test.ts @@ -0,0 +1,114 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; + +import { + AI_CONFIG, + getAiModelOptions, + getAiProviderConfig, + getDefaultAiModelId, +} from "@/config/aiConfig"; +import { + getStartingSessionModel, + getStartingSessionThinkingDepth, +} from "@/services/projects/startingSession"; + +import { loadModelCatalog } from "./modelCatalog"; + +const model = { + id: "example-model", + label: "Example", + api: "anthropic-messages", + tangentModelId: "example/example-model", + reasoningEfforts: ["low", "high", "max"], + thinkingLevelMap: { + off: null, + minimal: null, + low: "low", + medium: null, + high: "high", + xhigh: "max", + max: "max", + }, +}; +const fixture = { + schemaVersion: 1, + defaults: { browser: model.id }, + replacements: { "old-example": model.id }, + models: [model], +}; +describe("deployment model catalog", () => { + afterEach(() => { + delete window.__TANGLE_AI_MODELS__; + vi.unstubAllGlobals(); + }); + it("does not fetch a catalog for standalone installations", async () => { + const fetch = vi.fn(); + vi.stubGlobal("fetch", fetch); + expect(await loadModelCatalog()).toBeNull(); + expect(fetch).not.toHaveBeenCalled(); + }); + it("loads authenticated choices and explicit model/thinking handoff", async () => { + window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models" }; + const fetch = vi + .fn() + .mockResolvedValue(new Response(JSON.stringify(fixture))); + vi.stubGlobal("fetch", fetch); + await loadModelCatalog(); + expect(fetch).toHaveBeenCalledWith( + "/api/example-models", + expect.objectContaining({ credentials: "include" }), + ); + expect(getDefaultAiModelId()).toBe(model.id); + expect(getAiModelOptions()).toHaveLength(1); + expect(getAiProviderConfig(model.id)).toBe(AI_CONFIG.providers.anthropic); + expect(getStartingSessionModel({ model: model.id })).toBe( + model.tangentModelId, + ); + expect( + getStartingSessionThinkingDepth({ + model: model.id, + reasoningEffortPreference: "max", + }), + ).toBe("xhigh"); + }); + it("does not offer agent-only Chat Completions models for browser execution", async () => { + window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models" }; + const agentOnly = { + ...model, + id: "agent-only", + api: "openai-completions", + browserAvailable: false, + }; + vi.stubGlobal( + "fetch", + vi + .fn() + .mockResolvedValue( + new Response( + JSON.stringify({ ...fixture, models: [model, agentOnly] }), + ), + ), + ); + await loadModelCatalog(); + expect(getAiModelOptions().map((m) => m.id)).toEqual([model.id]); + }); + + it("rejects failed and invalid catalogs without installing a partial list", async () => { + window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models" }; + vi.stubGlobal( + "fetch", + vi.fn().mockResolvedValue(new Response("{}", { status: 401 })), + ); + await expect(loadModelCatalog()).rejects.toThrow(); + expect(window.__TANGLE_AI_MODELS__.models).toBeUndefined(); + vi.stubGlobal( + "fetch", + vi + .fn() + .mockResolvedValue( + new Response(JSON.stringify({ ...fixture, schemaVersion: 2 })), + ), + ); + await expect(loadModelCatalog()).rejects.toThrow(); + expect(window.__TANGLE_AI_MODELS__.models).toBeUndefined(); + }); +}); diff --git a/src/config/modelCatalog.ts b/src/config/modelCatalog.ts new file mode 100644 index 0000000000..e632c5f633 --- /dev/null +++ b/src/config/modelCatalog.ts @@ -0,0 +1,63 @@ +import { getAiModelOptions, isAiReasoningEffort } from "@/config/aiConfig"; +import { isRecord } from "@/utils/typeGuards"; + +/** Optional, deployment-selected endpoint. Standalone installs do not call it. */ +export function getModelCatalogUrl(): string | undefined { + return typeof window !== "undefined" + ? window.__TANGLE_AI_MODELS__?.catalogUrl + : undefined; +} + +export async function loadModelCatalog(signal?: AbortSignal) { + const url = getModelCatalogUrl(); + if (!url) return null; + const response = await fetch(url, { credentials: "include", signal }); + if (!response.ok) throw new Error("Unable to load deployment model catalog"); + const value: unknown = await response.json(); + if ( + !isRecord(value) || + value.schemaVersion !== 1 || + !Array.isArray(value.models) || + !value.models.length || + !isRecord(value.defaults) || + typeof value.defaults.browser !== "string" + ) + throw new Error("Invalid model catalog"); + const browserModels = value.models.filter( + (model) => !isRecord(model) || model.browserAvailable !== false, + ); + for (const model of browserModels) { + if ( + !isRecord(model) || + typeof model.id !== "string" || + !model.id.trim() || + typeof model.label !== "string" || + typeof model.tangentModelId !== "string" || + !["openai-responses", "anthropic-messages"].includes(String(model.api)) || + !Array.isArray(model.reasoningEfforts) || + !model.reasoningEfforts.every(isAiReasoningEffort) || + !isRecord(model.thinkingLevelMap) + ) + throw new Error("Invalid catalog model"); + } + const models = browserModels as NonNullable< + Window["__TANGLE_AI_MODELS__"] + >["models"]; + const defaultModel = value.defaults.browser; + if ( + !models?.some((m) => m.id === defaultModel) || + new Set(models.map((m) => m.id)).size !== models.length + ) + throw new Error("Invalid catalog default"); + const replacements = isRecord(value.replacements) + ? (value.replacements as Record) + : {}; + window.__TANGLE_AI_MODELS__ = { + catalogUrl: url, + models, + defaultModel, + replacements, + }; + window.dispatchEvent(new Event("tangle:model-catalog")); + return getAiModelOptions(); +} diff --git a/src/hooks/useAiProviderSettings.ts b/src/hooks/useAiProviderSettings.ts index 9a7e388cfa..175f09f648 100644 --- a/src/hooks/useAiProviderSettings.ts +++ b/src/hooks/useAiProviderSettings.ts @@ -1,4 +1,4 @@ -import { useSyncExternalStore } from "react"; +import { useEffect, useState, useSyncExternalStore } from "react"; import { AI_CONFIG, @@ -7,6 +7,7 @@ import { getEffectiveReasoningEffort, isAiReasoningEffort, } from "@/config/aiConfig"; +import { getModelCatalogUrl, loadModelCatalog } from "@/config/modelCatalog"; import { useBackend } from "@/providers/BackendProvider"; import type { AiProviderConfig } from "@/types/aiProvider"; import { getStorage } from "@/utils/typedStorage"; @@ -76,7 +77,13 @@ const REPLACED_MODEL_IDS = new Map([["gpt-6-sol", "gpt-6.1-sol"]]); function readModelId(record: Record, key: string): string { const model = readTrimmedString(record, key); - return REPLACED_MODEL_IDS.get(model) ?? model; + return ( + (typeof window !== "undefined" + ? window.__TANGLE_AI_MODELS__?.replacements?.[model] + : undefined) ?? + REPLACED_MODEL_IDS.get(model) ?? + model + ); } function readReasoningPreference(record: Record) { @@ -155,7 +162,11 @@ function subscribe(callback: () => void): () => void { } }; window.addEventListener("storage", handler); - return () => window.removeEventListener("storage", handler); + window.addEventListener("tangle:model-catalog", callback); + return () => { + window.removeEventListener("storage", handler); + window.removeEventListener("tangle:model-catalog", callback); + }; } let cachedJSON = ""; @@ -165,7 +176,7 @@ function getSnapshot(): AiProviderSettings { customConfig: readStoredConfig(), proxyConfig: readProxySettings(), }; - const json = JSON.stringify(fresh); + const json = JSON.stringify({ fresh, models: getAiModelOptions() }); if (json !== cachedJSON) { cachedJSON = json; cachedConfig = fresh; @@ -179,6 +190,22 @@ function getServerSnapshot(): AiProviderSettings { export function useAiProviderSettings() { const { backendUrl } = useBackend(); + const catalogUrl = getModelCatalogUrl(); + const [catalogReady, setCatalogReady] = useState(!catalogUrl); + useEffect(() => { + if (!catalogUrl) return; + let active = true; + void loadModelCatalog() + .then(() => { + if (active) setCatalogReady(true); + }) + .catch(() => { + if (active) setCatalogReady(false); + }); + return () => { + active = false; + }; + }, [catalogUrl]); const backendBase = backendUrl.trim().replace(/\/+$/, ""); const { customConfig, proxyConfig } = useSyncExternalStore( subscribe, @@ -204,7 +231,7 @@ export function useAiProviderSettings() { const config: AiProviderConfig = { apiBase: isOwnKeyEnabled ? customConfig.apiBase - : backendBase + : backendBase && catalogReady ? `${backendBase}/api/experimental/ai/v1` : "", apiKey: isOwnKeyEnabled ? customConfig.apiKey : "", diff --git a/src/services/projects/startingSession.ts b/src/services/projects/startingSession.ts index 7b9b76dbb8..f7964442ec 100644 --- a/src/services/projects/startingSession.ts +++ b/src/services/projects/startingSession.ts @@ -60,6 +60,19 @@ export function getStartingSessionThinkingDepth( "model" | "reasoningEffortPreference" | "modelCapabilities" >, ): ThinkingLevel | undefined { + const selected = (config.modelCapabilities ?? getAiModelOptions()).find( + (m) => m.id === config.model, + ) as import("@/config/aiConfig").AiModelOption | undefined; + if (selected?.thinkingLevelMap) { + const effort = getEffectiveReasoningEffort( + config.model, + config.reasoningEffortPreference, + config.modelCapabilities, + ); + return THINKING_LEVELS.find( + (level) => selected.thinkingLevelMap?.[level] === effort, + ); + } const effort = getEffectiveReasoningEffort( config.model, config.reasoningEffortPreference, @@ -78,6 +91,10 @@ export function getStartingSessionModel( (option) => option.id === config.model, ); if (!isCatalogModel) return undefined; + const selected = (config.modelCapabilities ?? getAiModelOptions()).find( + (m) => m.id === config.model, + ) as import("@/config/aiConfig").AiModelOption | undefined; + if (selected?.tangentModelId) return selected.tangentModelId; const provider = getAiProviderConfig(config.model) === AI_CONFIG.providers.anthropic ? "anthropic" From 9c8d89017c3009360d130e8bf7baa64f4a7f56d2 Mon Sep 17 00:00:00 2001 From: Camiel van Schoonhoven Date: Wed, 18 Nov 2026 12:06:56 -0700 Subject: [PATCH 2/3] refactor(ai): validate catalog data with type guards Replace the `as` assertions in the catalog loader, the host-config reader and the session handoff with type guards, and move `AiModelOption` to the provider types so `modelCapabilities` can carry catalog fields without a cast. Read catalog replacements through the sanitized host config instead of the raw window object, name the catalog event once, drop the unused abort signal, and hand off the nearest mapped thinking level when the preferred effort has no Pi level. Co-Authored-By: Claude Fable 5.1 --- src/config/aiConfig.ts | 52 +++++++++++------- src/config/modelCatalog.test.ts | 40 ++++++++++++++ src/config/modelCatalog.ts | 70 ++++++++++++++---------- src/hooks/useAiProviderSettings.ts | 19 +++---- src/services/projects/startingSession.ts | 35 ++++++------ src/types/aiProvider.ts | 12 +++- src/utils/typeGuards.ts | 9 +++ 7 files changed, 161 insertions(+), 76 deletions(-) diff --git a/src/config/aiConfig.ts b/src/config/aiConfig.ts index 64ed194376..7140e175e0 100644 --- a/src/config/aiConfig.ts +++ b/src/config/aiConfig.ts @@ -1,16 +1,10 @@ import type { + AiModelApi, AiModelCapabilities, + AiModelOption, AiReasoningEffort, } from "@/types/aiProvider"; -import { isRecord } from "@/utils/typeGuards"; - -export interface AiModelOption extends AiModelCapabilities { - label?: string; - description?: string; - tangentModelId?: string; - api?: "openai-responses" | "anthropic-messages"; - thinkingLevelMap?: Record; -} +import { isRecord, isStringRecord } from "@/utils/typeGuards"; interface AiModelOptionsConfig { models?: AiModelOption[]; @@ -104,6 +98,26 @@ export function isAiReasoningEffort( return AI_CONFIG.reasoningLevels.some((level) => level.value === value); } +const AI_MODEL_APIS: readonly AiModelApi[] = [ + "openai-responses", + "anthropic-messages", +]; + +export function isAiModelApi(value: unknown): value is AiModelApi { + return AI_MODEL_APIS.some((api) => api === value); +} + +export function isThinkingLevelMap( + value: unknown, +): value is Record { + return ( + isRecord(value) && + Object.values(value).every( + (effort) => effort === null || isAiReasoningEffort(effort), + ) + ); +} + declare global { interface Window { __TANGLE_AI_MODELS__?: AiModelOptionsConfig; @@ -121,16 +135,9 @@ function readModelOption(value: unknown): AiModelOption | null { ...(typeof value.tangentModelId === "string" ? { tangentModelId: value.tangentModelId } : {}), - ...(value.api === "openai-responses" || value.api === "anthropic-messages" - ? { api: value.api } - : {}), - ...(isRecord(value.thinkingLevelMap) - ? { - thinkingLevelMap: value.thinkingLevelMap as Record< - string, - string | null - >, - } + ...(isAiModelApi(value.api) ? { api: value.api } : {}), + ...(isThinkingLevelMap(value.thinkingLevelMap) + ? { thinkingLevelMap: value.thinkingLevelMap } : {}), ...(typeof value.label === "string" && value.label.trim() ? { label: value.label.trim() } @@ -153,6 +160,9 @@ function readInjectedModelOptions(): AiModelOptionsConfig | null { ...(typeof config.defaultModel === "string" ? { defaultModel: config.defaultModel } : {}), + ...(isStringRecord(config.replacements) + ? { replacements: config.replacements } + : {}), ...(Array.isArray(config.models) ? { models: config.models.map(readModelOption).filter((v) => v !== null) } : {}), @@ -166,6 +176,10 @@ export function getAiModelOptions(): AiModelOption[] { : AI_CONFIG.models; } +export function getAiModelReplacement(modelId: string): string | undefined { + return readInjectedModelOptions()?.replacements?.[modelId]; +} + export function getDefaultAiModelId(): string { const options = getAiModelOptions(); const preferred = readInjectedModelOptions()?.defaultModel; diff --git a/src/config/modelCatalog.test.ts b/src/config/modelCatalog.test.ts index c7c7013b66..7826bb5dfa 100644 --- a/src/config/modelCatalog.test.ts +++ b/src/config/modelCatalog.test.ts @@ -92,6 +92,34 @@ describe("deployment model catalog", () => { expect(getAiModelOptions().map((m) => m.id)).toEqual([model.id]); }); + it("hands off the nearest mapped level when the preferred effort has no Pi level", async () => { + window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models" }; + const gappy = { + ...model, + reasoningEfforts: ["low", "medium", "high"], + thinkingLevelMap: { ...model.thinkingLevelMap, xhigh: null, max: null }, + }; + vi.stubGlobal( + "fetch", + vi.fn().mockResolvedValue( + new Response( + JSON.stringify({ + ...fixture, + defaults: { browser: gappy.id }, + models: [gappy], + }), + ), + ), + ); + await loadModelCatalog(); + expect( + getStartingSessionThinkingDepth({ + model: gappy.id, + reasoningEffortPreference: "medium", + }), + ).toBe("low"); + }); + it("rejects failed and invalid catalogs without installing a partial list", async () => { window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models" }; vi.stubGlobal( @@ -110,5 +138,17 @@ describe("deployment model catalog", () => { ); await expect(loadModelCatalog()).rejects.toThrow(); expect(window.__TANGLE_AI_MODELS__.models).toBeUndefined(); + vi.stubGlobal( + "fetch", + vi + .fn() + .mockResolvedValue( + new Response( + JSON.stringify({ ...fixture, replacements: { old: 42 } }), + ), + ), + ); + await expect(loadModelCatalog()).rejects.toThrow(); + expect(window.__TANGLE_AI_MODELS__.models).toBeUndefined(); }); }); diff --git a/src/config/modelCatalog.ts b/src/config/modelCatalog.ts index e632c5f633..2c0df20e1a 100644 --- a/src/config/modelCatalog.ts +++ b/src/config/modelCatalog.ts @@ -1,5 +1,16 @@ -import { getAiModelOptions, isAiReasoningEffort } from "@/config/aiConfig"; -import { isRecord } from "@/utils/typeGuards"; +import { + getAiModelOptions, + isAiModelApi, + isAiReasoningEffort, + isThinkingLevelMap, +} from "@/config/aiConfig"; +import type { AiModelOption } from "@/types/aiProvider"; +import { isRecord, isStringRecord } from "@/utils/typeGuards"; + +export const MODEL_CATALOG_EVENT = "tangle:model-catalog"; + +type CatalogModel = AiModelOption & + Required>; /** Optional, deployment-selected endpoint. Standalone installs do not call it. */ export function getModelCatalogUrl(): string | undefined { @@ -8,10 +19,28 @@ export function getModelCatalogUrl(): string | undefined { : undefined; } -export async function loadModelCatalog(signal?: AbortSignal) { +function isAgentOnly(model: unknown): boolean { + return isRecord(model) && model.browserAvailable === false; +} + +function isCatalogModel(value: unknown): value is CatalogModel { + return ( + isRecord(value) && + typeof value.id === "string" && + value.id.trim() !== "" && + typeof value.label === "string" && + typeof value.tangentModelId === "string" && + isAiModelApi(value.api) && + Array.isArray(value.reasoningEfforts) && + value.reasoningEfforts.every(isAiReasoningEffort) && + isThinkingLevelMap(value.thinkingLevelMap) + ); +} + +export async function loadModelCatalog() { const url = getModelCatalogUrl(); if (!url) return null; - const response = await fetch(url, { credentials: "include", signal }); + const response = await fetch(url, { credentials: "include" }); if (!response.ok) throw new Error("Unable to load deployment model catalog"); const value: unknown = await response.json(); if ( @@ -23,41 +52,22 @@ export async function loadModelCatalog(signal?: AbortSignal) { typeof value.defaults.browser !== "string" ) throw new Error("Invalid model catalog"); - const browserModels = value.models.filter( - (model) => !isRecord(model) || model.browserAvailable !== false, - ); - for (const model of browserModels) { - if ( - !isRecord(model) || - typeof model.id !== "string" || - !model.id.trim() || - typeof model.label !== "string" || - typeof model.tangentModelId !== "string" || - !["openai-responses", "anthropic-messages"].includes(String(model.api)) || - !Array.isArray(model.reasoningEfforts) || - !model.reasoningEfforts.every(isAiReasoningEffort) || - !isRecord(model.thinkingLevelMap) - ) - throw new Error("Invalid catalog model"); - } - const models = browserModels as NonNullable< - Window["__TANGLE_AI_MODELS__"] - >["models"]; + const models = value.models.filter((model) => !isAgentOnly(model)); + if (!models.every(isCatalogModel)) throw new Error("Invalid catalog model"); const defaultModel = value.defaults.browser; if ( - !models?.some((m) => m.id === defaultModel) || + !models.some((m) => m.id === defaultModel) || new Set(models.map((m) => m.id)).size !== models.length ) throw new Error("Invalid catalog default"); - const replacements = isRecord(value.replacements) - ? (value.replacements as Record) - : {}; + if (value.replacements !== undefined && !isStringRecord(value.replacements)) + throw new Error("Invalid catalog replacements"); window.__TANGLE_AI_MODELS__ = { catalogUrl: url, models, defaultModel, - replacements, + replacements: value.replacements ?? {}, }; - window.dispatchEvent(new Event("tangle:model-catalog")); + window.dispatchEvent(new Event(MODEL_CATALOG_EVENT)); return getAiModelOptions(); } diff --git a/src/hooks/useAiProviderSettings.ts b/src/hooks/useAiProviderSettings.ts index 175f09f648..fa96da1499 100644 --- a/src/hooks/useAiProviderSettings.ts +++ b/src/hooks/useAiProviderSettings.ts @@ -3,11 +3,16 @@ import { useEffect, useState, useSyncExternalStore } from "react"; import { AI_CONFIG, getAiModelOptions, + getAiModelReplacement, getDefaultAiModelId, getEffectiveReasoningEffort, isAiReasoningEffort, } from "@/config/aiConfig"; -import { getModelCatalogUrl, loadModelCatalog } from "@/config/modelCatalog"; +import { + getModelCatalogUrl, + loadModelCatalog, + MODEL_CATALOG_EVENT, +} from "@/config/modelCatalog"; import { useBackend } from "@/providers/BackendProvider"; import type { AiProviderConfig } from "@/types/aiProvider"; import { getStorage } from "@/utils/typedStorage"; @@ -77,13 +82,7 @@ const REPLACED_MODEL_IDS = new Map([["gpt-6-sol", "gpt-6.1-sol"]]); function readModelId(record: Record, key: string): string { const model = readTrimmedString(record, key); - return ( - (typeof window !== "undefined" - ? window.__TANGLE_AI_MODELS__?.replacements?.[model] - : undefined) ?? - REPLACED_MODEL_IDS.get(model) ?? - model - ); + return getAiModelReplacement(model) ?? REPLACED_MODEL_IDS.get(model) ?? model; } function readReasoningPreference(record: Record) { @@ -162,10 +161,10 @@ function subscribe(callback: () => void): () => void { } }; window.addEventListener("storage", handler); - window.addEventListener("tangle:model-catalog", callback); + window.addEventListener(MODEL_CATALOG_EVENT, callback); return () => { window.removeEventListener("storage", handler); - window.removeEventListener("tangle:model-catalog", callback); + window.removeEventListener(MODEL_CATALOG_EVENT, callback); }; } diff --git a/src/services/projects/startingSession.ts b/src/services/projects/startingSession.ts index f7964442ec..184c8395ef 100644 --- a/src/services/projects/startingSession.ts +++ b/src/services/projects/startingSession.ts @@ -8,7 +8,7 @@ import { getEffectiveReasoningEffort, isAiReasoningEffort, } from "@/config/aiConfig"; -import type { AiProviderConfig } from "@/types/aiProvider"; +import type { AiModelOption, AiProviderConfig } from "@/types/aiProvider"; import { HOURS } from "@/utils/constants"; const PROMPT = "startingPrompt"; @@ -54,24 +54,32 @@ const asText = (value: unknown) => const asThinkingLevel = (value: unknown) => THINKING_LEVELS.find((level) => level === value); +function selectedCatalogModel( + config: Pick, +): AiModelOption | undefined { + return (config.modelCapabilities ?? getAiModelOptions()).find( + (option) => option.id === config.model, + ); +} + export function getStartingSessionThinkingDepth( config: Pick< AiProviderConfig, "model" | "reasoningEffortPreference" | "modelCapabilities" >, ): ThinkingLevel | undefined { - const selected = (config.modelCapabilities ?? getAiModelOptions()).find( - (m) => m.id === config.model, - ) as import("@/config/aiConfig").AiModelOption | undefined; - if (selected?.thinkingLevelMap) { + const thinkingLevelMap = selectedCatalogModel(config)?.thinkingLevelMap; + if (thinkingLevelMap) { + const reachableEfforts = THINKING_LEVELS.map( + (level) => thinkingLevelMap[level], + ).filter(isAiReasoningEffort); const effort = getEffectiveReasoningEffort( config.model, config.reasoningEffortPreference, config.modelCapabilities, + reachableEfforts, ); - return THINKING_LEVELS.find( - (level) => selected.thinkingLevelMap?.[level] === effort, - ); + return THINKING_LEVELS.find((level) => thinkingLevelMap[level] === effort); } const effort = getEffectiveReasoningEffort( config.model, @@ -87,14 +95,9 @@ export function getStartingSessionThinkingDepth( export function getStartingSessionModel( config: Pick, ): string | undefined { - const isCatalogModel = (config.modelCapabilities ?? getAiModelOptions()).some( - (option) => option.id === config.model, - ); - if (!isCatalogModel) return undefined; - const selected = (config.modelCapabilities ?? getAiModelOptions()).find( - (m) => m.id === config.model, - ) as import("@/config/aiConfig").AiModelOption | undefined; - if (selected?.tangentModelId) return selected.tangentModelId; + const selected = selectedCatalogModel(config); + if (!selected) return undefined; + if (selected.tangentModelId) return selected.tangentModelId; const provider = getAiProviderConfig(config.model) === AI_CONFIG.providers.anthropic ? "anthropic" diff --git a/src/types/aiProvider.ts b/src/types/aiProvider.ts index 377a0c310d..2e12e3ea18 100644 --- a/src/types/aiProvider.ts +++ b/src/types/aiProvider.ts @@ -6,12 +6,22 @@ export interface AiModelCapabilities { reasoningEfforts?: AiReasoningEffort[]; } +export type AiModelApi = "openai-responses" | "anthropic-messages"; + +export interface AiModelOption extends AiModelCapabilities { + label?: string; + description?: string; + tangentModelId?: string; + api?: AiModelApi; + thinkingLevelMap?: Record; +} + export interface AiProviderConfig { apiBase: string; apiKey: string; model: string; reasoningEffort?: AiReasoningEffort; reasoningEffortPreference?: AiReasoningEffort; - modelCapabilities?: AiModelCapabilities[]; + modelCapabilities?: AiModelOption[]; credentials?: RequestCredentials; } diff --git a/src/utils/typeGuards.ts b/src/utils/typeGuards.ts index 35f1d60d9a..d64091a921 100644 --- a/src/utils/typeGuards.ts +++ b/src/utils/typeGuards.ts @@ -1,3 +1,12 @@ export function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } + +export function isStringRecord( + value: unknown, +): value is Record { + return ( + isRecord(value) && + Object.values(value).every((entry) => typeof entry === "string") + ); +} From 6cdfec4871a35dbb2a51057fb6e3f4c0dd985a64 Mon Sep 17 00:00:00 2001 From: Camiel van Schoonhoven Date: Wed, 18 Nov 2026 12:47:59 -0700 Subject: [PATCH 3/3] fix(ai): load the deployment catalog once per page Every consumer of useAiProviderSettings used to start its own catalog fetch and keep its own ready flag, so a page with a dozen AI surfaces fanned out into a dozen requests and re-announced the catalog each time. The catalog module now owns one in-flight load and a loading/ready/failed status that the hook reads through its existing external store subscription. Co-Authored-By: Claude Fable 5.1 --- src/config/modelCatalog.test.ts | 41 +++++++++++++++++--------- src/config/modelCatalog.ts | 47 ++++++++++++++++++++++++++++-- src/hooks/useAiProviderSettings.ts | 26 +++++++---------- 3 files changed, 83 insertions(+), 31 deletions(-) diff --git a/src/config/modelCatalog.test.ts b/src/config/modelCatalog.test.ts index 7826bb5dfa..a43b6e4bae 100644 --- a/src/config/modelCatalog.test.ts +++ b/src/config/modelCatalog.test.ts @@ -11,7 +11,7 @@ import { getStartingSessionThinkingDepth, } from "@/services/projects/startingSession"; -import { loadModelCatalog } from "./modelCatalog"; +import { ensureModelCatalog, getModelCatalogStatus } from "./modelCatalog"; const model = { id: "example-model", @@ -43,18 +43,18 @@ describe("deployment model catalog", () => { it("does not fetch a catalog for standalone installations", async () => { const fetch = vi.fn(); vi.stubGlobal("fetch", fetch); - expect(await loadModelCatalog()).toBeNull(); + expect(await ensureModelCatalog()).toBeNull(); expect(fetch).not.toHaveBeenCalled(); }); it("loads authenticated choices and explicit model/thinking handoff", async () => { - window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models" }; + window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models/handoff" }; const fetch = vi .fn() .mockResolvedValue(new Response(JSON.stringify(fixture))); vi.stubGlobal("fetch", fetch); - await loadModelCatalog(); + await ensureModelCatalog(); expect(fetch).toHaveBeenCalledWith( - "/api/example-models", + "/api/example-models/handoff", expect.objectContaining({ credentials: "include" }), ); expect(getDefaultAiModelId()).toBe(model.id); @@ -70,8 +70,23 @@ describe("deployment model catalog", () => { }), ).toBe("xhigh"); }); + it("shares one request between every consumer on the page", async () => { + window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models/shared" }; + const fetch = vi + .fn() + .mockResolvedValue(new Response(JSON.stringify(fixture))); + vi.stubGlobal("fetch", fetch); + expect(getModelCatalogStatus()).toBe("loading"); + await Promise.all([ensureModelCatalog(), ensureModelCatalog()]); + await ensureModelCatalog(); + expect(fetch).toHaveBeenCalledTimes(1); + expect(getModelCatalogStatus()).toBe("ready"); + }); + it("does not offer agent-only Chat Completions models for browser execution", async () => { - window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models" }; + window.__TANGLE_AI_MODELS__ = { + catalogUrl: "/api/example-models/agent-only", + }; const agentOnly = { ...model, id: "agent-only", @@ -88,12 +103,12 @@ describe("deployment model catalog", () => { ), ), ); - await loadModelCatalog(); + await ensureModelCatalog(); expect(getAiModelOptions().map((m) => m.id)).toEqual([model.id]); }); it("hands off the nearest mapped level when the preferred effort has no Pi level", async () => { - window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models" }; + window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models/gappy" }; const gappy = { ...model, reasoningEfforts: ["low", "medium", "high"], @@ -111,7 +126,7 @@ describe("deployment model catalog", () => { ), ), ); - await loadModelCatalog(); + await ensureModelCatalog(); expect( getStartingSessionThinkingDepth({ model: gappy.id, @@ -121,12 +136,12 @@ describe("deployment model catalog", () => { }); it("rejects failed and invalid catalogs without installing a partial list", async () => { - window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models" }; + window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models/invalid" }; vi.stubGlobal( "fetch", vi.fn().mockResolvedValue(new Response("{}", { status: 401 })), ); - await expect(loadModelCatalog()).rejects.toThrow(); + await expect(ensureModelCatalog()).rejects.toThrow(); expect(window.__TANGLE_AI_MODELS__.models).toBeUndefined(); vi.stubGlobal( "fetch", @@ -136,7 +151,7 @@ describe("deployment model catalog", () => { new Response(JSON.stringify({ ...fixture, schemaVersion: 2 })), ), ); - await expect(loadModelCatalog()).rejects.toThrow(); + await expect(ensureModelCatalog()).rejects.toThrow(); expect(window.__TANGLE_AI_MODELS__.models).toBeUndefined(); vi.stubGlobal( "fetch", @@ -148,7 +163,7 @@ describe("deployment model catalog", () => { ), ), ); - await expect(loadModelCatalog()).rejects.toThrow(); + await expect(ensureModelCatalog()).rejects.toThrow(); expect(window.__TANGLE_AI_MODELS__.models).toBeUndefined(); }); }); diff --git a/src/config/modelCatalog.ts b/src/config/modelCatalog.ts index 2c0df20e1a..2e84449b0a 100644 --- a/src/config/modelCatalog.ts +++ b/src/config/modelCatalog.ts @@ -9,6 +9,8 @@ import { isRecord, isStringRecord } from "@/utils/typeGuards"; export const MODEL_CATALOG_EVENT = "tangle:model-catalog"; +export type ModelCatalogStatus = "loading" | "ready" | "failed"; + type CatalogModel = AiModelOption & Required>; @@ -37,7 +39,7 @@ function isCatalogModel(value: unknown): value is CatalogModel { ); } -export async function loadModelCatalog() { +async function loadModelCatalog() { const url = getModelCatalogUrl(); if (!url) return null; const response = await fetch(url, { credentials: "include" }); @@ -68,6 +70,47 @@ export async function loadModelCatalog() { defaultModel, replacements: value.replacements ?? {}, }; - window.dispatchEvent(new Event(MODEL_CATALOG_EVENT)); return getAiModelOptions(); } + +interface CatalogLoad { + url: string; + status: ModelCatalogStatus; + pending: Promise; +} + +let load: CatalogLoad | undefined; + +function announce(url: string, status: ModelCatalogStatus) { + if (load?.url === url) load.status = status; + window.dispatchEvent(new Event(MODEL_CATALOG_EVENT)); +} + +export function getModelCatalogStatus(): ModelCatalogStatus { + const url = getModelCatalogUrl(); + if (!url) return "ready"; + return load?.url === url ? load.status : "loading"; +} + +/** + * Every AI consumer shares one catalog request per page, so a dozen mounting + * hooks cannot fan out into a dozen fetches or re-announce the same catalog. + */ +export function ensureModelCatalog(): Promise { + const url = getModelCatalogUrl(); + if (!url) return Promise.resolve(null); + if (load?.url !== url || load.status === "failed") { + const pending = loadModelCatalog().then( + (models) => { + announce(url, "ready"); + return models; + }, + (error: unknown) => { + announce(url, "failed"); + throw error; + }, + ); + load = { url, status: "loading", pending }; + } + return load.pending; +} diff --git a/src/hooks/useAiProviderSettings.ts b/src/hooks/useAiProviderSettings.ts index fa96da1499..490a883220 100644 --- a/src/hooks/useAiProviderSettings.ts +++ b/src/hooks/useAiProviderSettings.ts @@ -1,4 +1,4 @@ -import { useEffect, useState, useSyncExternalStore } from "react"; +import { useEffect, useSyncExternalStore } from "react"; import { AI_CONFIG, @@ -9,8 +9,9 @@ import { isAiReasoningEffort, } from "@/config/aiConfig"; import { + ensureModelCatalog, + getModelCatalogStatus, getModelCatalogUrl, - loadModelCatalog, MODEL_CATALOG_EVENT, } from "@/config/modelCatalog"; import { useBackend } from "@/providers/BackendProvider"; @@ -190,20 +191,13 @@ function getServerSnapshot(): AiProviderSettings { export function useAiProviderSettings() { const { backendUrl } = useBackend(); const catalogUrl = getModelCatalogUrl(); - const [catalogReady, setCatalogReady] = useState(!catalogUrl); + const catalogStatus = useSyncExternalStore( + subscribe, + getModelCatalogStatus, + () => "ready", + ); useEffect(() => { - if (!catalogUrl) return; - let active = true; - void loadModelCatalog() - .then(() => { - if (active) setCatalogReady(true); - }) - .catch(() => { - if (active) setCatalogReady(false); - }); - return () => { - active = false; - }; + if (catalogUrl) ensureModelCatalog().catch(() => undefined); }, [catalogUrl]); const backendBase = backendUrl.trim().replace(/\/+$/, ""); const { customConfig, proxyConfig } = useSyncExternalStore( @@ -230,7 +224,7 @@ export function useAiProviderSettings() { const config: AiProviderConfig = { apiBase: isOwnKeyEnabled ? customConfig.apiBase - : backendBase && catalogReady + : backendBase && catalogStatus === "ready" ? `${backendBase}/api/experimental/ai/v1` : "", apiKey: isOwnKeyEnabled ? customConfig.apiKey : "",