/** * Wire contract for the settings card: public settings (no API key literal) and * the patch the settings page sends. * @module dsh-image-pathify/contract */ import type { TypertSchema } from "@deepseek-ai/dsh-typert-protocol"; import { DEFAULT_API_KEY_ENV, DEFAULT_DISABLE_THINKING, DEFAULT_MAX_TOKENS, DEFAULT_VISION_BASE_URL, DEFAULT_VISION_MODEL, MAX_VISION_MAX_TOKENS, MIN_VISION_MAX_TOKENS, } from "./defaults.ts"; /** One provider/model pair the admission shim may relax. */ export interface PathifyModelEntry { readonly provider: string; readonly model: string; } /** * Settings the browser is allowed to see. The key literal never rides this * object; the client reads configured/writable from the credentials domain. */ export interface ImagePathifyPublicSettings { readonly apiKeyEnv: string; readonly visionModel: string; readonly visionBaseUrl: string; readonly disableThinking: boolean; readonly maxTokens: number; readonly models: readonly PathifyModelEntry[]; readonly relaxAdmission: boolean; } /** * One field patch from the settings page. The API key is not a field here — * the card writes it through `credentials.set`. */ export interface ImagePathifySettingsUpdate { readonly apiKeyEnv?: string; readonly visionModel?: string; readonly visionBaseUrl?: string; readonly disableThinking?: boolean; readonly maxTokens?: number; readonly models?: readonly PathifyModelEntry[]; readonly relaxAdmission?: boolean; } /** Schema defaults as the public wire shape (no secret). */ export function defaultPublicSettings(): ImagePathifyPublicSettings { return { apiKeyEnv: DEFAULT_API_KEY_ENV, visionModel: DEFAULT_VISION_MODEL, visionBaseUrl: DEFAULT_VISION_BASE_URL, disableThinking: DEFAULT_DISABLE_THINKING, maxTokens: DEFAULT_MAX_TOKENS, models: [], relaxAdmission: true, }; } function fail(message: string): never { throw new TypeError(message); } function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } function readString(value: unknown, field: string): string { return typeof value === "string" ? value : fail(`${field} must be a string`); } function readBoolean(value: unknown, field: string): boolean { return typeof value === "boolean" ? value : fail(`${field} must be a boolean`); } function readMaxTokens(value: unknown, field: string): number { if (typeof value !== "number" || !Number.isInteger(value)) { fail(`${field} must be an integer`); } if (value < MIN_VISION_MAX_TOKENS || value > MAX_VISION_MAX_TOKENS) { fail( `${field} must be between ${String(MIN_VISION_MAX_TOKENS)} and ${String(MAX_VISION_MAX_TOKENS)}`, ); } return value; } function readModel(value: unknown): PathifyModelEntry { if (!isRecord(value)) fail("models[] must be an object"); const provider = readString(value.provider, "models[].provider").trim(); const model = readString(value.model, "models[].model").trim(); if (provider.length === 0 || model.length === 0) { fail("models[] requires non-empty provider and model"); } return { provider, model }; } /** Strict codec for the public settings object. */ export const publicSettingsSchema: TypertSchema = { parse(value: unknown): ImagePathifyPublicSettings { if (!isRecord(value)) fail("settings must be an object"); const modelsRaw = value.models; if (!Array.isArray(modelsRaw)) fail("models must be an array"); return { apiKeyEnv: readString(value.apiKeyEnv, "apiKeyEnv"), visionModel: readString(value.visionModel, "visionModel"), visionBaseUrl: readString(value.visionBaseUrl, "visionBaseUrl"), disableThinking: readBoolean(value.disableThinking, "disableThinking"), maxTokens: readMaxTokens(value.maxTokens, "maxTokens"), models: modelsRaw.map(readModel), relaxAdmission: readBoolean(value.relaxAdmission, "relaxAdmission"), }; }, };