Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions src/config/aiConfig.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type {
AiModelApi,
AiModelCapabilities,
AiModelOption,
AiReasoningEffort,
} from "@/types/aiProvider";
import { isRecord } from "@/utils/typeGuards";

export interface AiModelOption extends AiModelCapabilities {
label?: string;
description?: string;
}
import { isRecord, isStringRecord } from "@/utils/typeGuards";

interface AiModelOptionsConfig {
models?: AiModelOption[];
catalogUrl?: string;
defaultModel?: string;
replacements?: Record<string, string>;
}

export const AI_CONFIG = {
Expand Down Expand Up @@ -98,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<string, AiReasoningEffort | null> {
return (
isRecord(value) &&
Object.values(value).every(
(effort) => effort === null || isAiReasoningEffort(effort),
)
);
}

declare global {
interface Window {
__TANGLE_AI_MODELS__?: AiModelOptionsConfig;
Expand All @@ -112,6 +132,13 @@ function readModelOption(value: unknown): AiModelOption | null {

return {
id,
...(typeof value.tangentModelId === "string"
? { tangentModelId: value.tangentModelId }
: {}),
...(isAiModelApi(value.api) ? { api: value.api } : {}),
...(isThinkingLevelMap(value.thinkingLevelMap)
? { thinkingLevelMap: value.thinkingLevelMap }
: {}),
...(typeof value.label === "string" && value.label.trim()
? { label: value.label.trim() }
: {}),
Expand All @@ -130,6 +157,12 @@ function readInjectedModelOptions(): AiModelOptionsConfig | null {
if (!isRecord(config)) return null;

return {
...(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) }
: {}),
Expand All @@ -143,8 +176,14 @@ export function getAiModelOptions(): AiModelOption[] {
: AI_CONFIG.models;
}

export function getAiModelReplacement(modelId: string): string | undefined {
return readInjectedModelOptions()?.replacements?.[modelId];
}

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 {
Expand All @@ -156,7 +195,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;
}
Expand Down
169 changes: 169 additions & 0 deletions src/config/modelCatalog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
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 { ensureModelCatalog, getModelCatalogStatus } 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 ensureModelCatalog()).toBeNull();
expect(fetch).not.toHaveBeenCalled();
});
it("loads authenticated choices and explicit model/thinking handoff", async () => {
window.__TANGLE_AI_MODELS__ = { catalogUrl: "/api/example-models/handoff" };
const fetch = vi
.fn()
.mockResolvedValue(new Response(JSON.stringify(fixture)));
vi.stubGlobal("fetch", fetch);
await ensureModelCatalog();
expect(fetch).toHaveBeenCalledWith(
"/api/example-models/handoff",
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("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/agent-only",
};
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 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/gappy" };
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 ensureModelCatalog();
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/invalid" };
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue(new Response("{}", { status: 401 })),
);
await expect(ensureModelCatalog()).rejects.toThrow();
expect(window.__TANGLE_AI_MODELS__.models).toBeUndefined();
vi.stubGlobal(
"fetch",
vi
.fn()
.mockResolvedValue(
new Response(JSON.stringify({ ...fixture, schemaVersion: 2 })),
),
);
await expect(ensureModelCatalog()).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(ensureModelCatalog()).rejects.toThrow();
expect(window.__TANGLE_AI_MODELS__.models).toBeUndefined();
});
});
116 changes: 116 additions & 0 deletions src/config/modelCatalog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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";

export type ModelCatalogStatus = "loading" | "ready" | "failed";

type CatalogModel = AiModelOption &
Required<Pick<AiModelOption, "label" | "tangentModelId" | "api">>;

/** 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;
}

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)
);
}

async function loadModelCatalog() {
const url = getModelCatalogUrl();
if (!url) return null;
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 (
!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 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) ||
new Set(models.map((m) => m.id)).size !== models.length
)
throw new Error("Invalid catalog default");
if (value.replacements !== undefined && !isStringRecord(value.replacements))
throw new Error("Invalid catalog replacements");
window.__TANGLE_AI_MODELS__ = {
catalogUrl: url,
models,
defaultModel,
replacements: value.replacements ?? {},
};
return getAiModelOptions();
}

interface CatalogLoad {
url: string;
status: ModelCatalogStatus;
pending: Promise<AiModelOption[] | null>;
}

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<AiModelOption[] | null> {
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;
}
Loading
Loading