UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

216 lines • 10.1 kB
/** * One-shot model discovery for an OpenAI-compatible provider in models.json (senpi#2196). * * `GET <baseUrl>/models` is fetched once and every listed id is upserted into the provider's * `models` array. When the provider opts in with `compat.supportsReasoningEffort: true`, the * `reasoning_efforts` an entry advertises become that model's `thinkingLevelMap` (endpoint * spelling kept, unadvertised levels vetoed) and its `default` becomes `defaultThinkingLevel`. * Discovery owns `reasoning`, `thinkingLevelMap`, and `defaultThinkingLevel` of a model that advertises * efforts; every other field is left exactly as the user wrote them. */ import { chmodSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs"; import { isDeepStrictEqual } from "node:util"; import { normalizeProviderId, parseEndpointReasoningEfforts, } from "@earendil-works/pi-ai"; import { stripJsonComments } from "../utils/json.js"; import { stripBom } from "../utils/text.js"; import { ModelConfig } from "./model-config.js"; import { listingUrl, redactUrlInMessage } from "./model-discovery-url.js"; import { uniqueBackupPath } from "./models-json-migration.js"; export class ModelsDiscoveryError extends Error { } const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"]; const OPENAI_COMPATIBLE_APIS = new Set(["openai-completions", "openai-responses"]); const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value); function readDocument(modelsPath) { let content; try { content = readFileSync(modelsPath, "utf-8"); } catch (error) { throw new ModelsDiscoveryError(`Cannot read ${modelsPath}: ${error instanceof Error ? error.message : error}`); } let document; try { document = JSON.parse(stripJsonComments(stripBom(content))); } catch (error) { throw new ModelsDiscoveryError(`Cannot parse ${modelsPath}: ${error instanceof Error ? error.message : error}`); } if (!isRecord(document)) throw new ModelsDiscoveryError(`${modelsPath} is not a JSON object`); return { content, document }; } function findProvider(document, providerId, modelsPath) { const wanted = normalizeProviderId(providerId); const providers = isRecord(document.providers) ? document.providers : {}; const key = Object.keys(providers).find((candidate) => normalizeProviderId(candidate) === wanted); const provider = key === undefined ? undefined : providers[key]; if (key === undefined || !isRecord(provider)) { throw new ModelsDiscoveryError(`Provider "${providerId}" is not defined in ${modelsPath}`); } return { key, providers, provider }; } function providerListingUrl(provider, providerId) { if (typeof provider.api === "string" && !OPENAI_COMPATIBLE_APIS.has(provider.api)) { throw new ModelsDiscoveryError(`Provider "${providerId}" uses api "${provider.api}", not an OpenAI-compatible api`); } if (typeof provider.baseUrl !== "string" || provider.baseUrl.trim() === "") { throw new ModelsDiscoveryError(`Provider "${providerId}" needs a "baseUrl" to discover models`); } const url = listingUrl(provider.baseUrl); if (!url) throw new ModelsDiscoveryError(`Provider "${providerId}" has a "baseUrl" that is not a valid URL`); return url; } async function fetchListing(url, auth, fetchImpl, signal) { const headers = { accept: "application/json" }; for (const [name, value] of Object.entries(auth.headers ?? {})) { if (value === null) delete headers[name]; else headers[name] = value; } if (auth.apiKey && !Object.keys(headers).some((name) => name.toLowerCase() === "authorization")) { headers.authorization = `Bearer ${auth.apiKey}`; } let response; try { response = await fetchImpl(url.request, { headers, signal }); } catch (error) { const reason = redactUrlInMessage(error instanceof Error ? error.message : String(error), url); throw new ModelsDiscoveryError(`GET ${url.display} failed: ${reason}`); } if (!response.ok) throw new ModelsDiscoveryError(`GET ${url.display} failed: HTTP ${response.status}`); let payload; try { payload = await response.json(); } catch { throw new ModelsDiscoveryError(`GET ${url.display} did not return JSON`); } const data = Array.isArray(payload) ? payload : isRecord(payload) ? payload.data : undefined; if (!Array.isArray(data)) throw new ModelsDiscoveryError(`GET ${url.display} did not return a model list`); const listed = new Map(); for (const entry of data) { if (!isRecord(entry) || typeof entry.id !== "string" || entry.id.trim() === "") continue; if (!listed.has(entry.id)) listed.set(entry.id, { id: entry.id, reasoningEfforts: entry.reasoning_efforts }); } return [...listed.values()]; } function describeEfforts(parsed) { const map = parsed.thinkingLevelMap ?? {}; const levels = THINKING_LEVELS.filter((level) => typeof map[level] === "string"); return parsed.defaultThinkingLevel === undefined ? { levels, unmapped: parsed.unmapped } : { levels, defaultThinkingLevel: parsed.defaultThinkingLevel, unmapped: parsed.unmapped }; } /** * Discovery owns `reasoning`, `thinkingLevelMap`, and `defaultThinkingLevel` of a model whose * listing advertises efforts; absent metadata leaves the entry alone. An advertised ladder with no * value senpi can represent turns the controls off rather than keeping a stale map. */ function withEfforts(entry, parsed) { if (!parsed) return entry; const { defaultThinkingLevel: _previousDefault, thinkingLevelMap: _previousMap, ...rest } = entry; if (!parsed.thinkingLevelMap) return { ...rest, reasoning: false }; return parsed.defaultThinkingLevel === undefined ? { ...rest, reasoning: true, thinkingLevelMap: parsed.thinkingLevelMap } : { ...rest, reasoning: true, thinkingLevelMap: parsed.thinkingLevelMap, defaultThinkingLevel: parsed.defaultThinkingLevel, }; } function writeWithBackup(modelsPath, original, next) { const backupPath = uniqueBackupPath(modelsPath); const temporary = `${modelsPath}.${process.pid}.tmp`; const mode = statSync(modelsPath).mode & 0o777; writeFileSync(backupPath, original, { encoding: "utf-8", mode, flag: "wx" }); try { writeFileSync(temporary, next, { encoding: "utf-8", mode, flag: "wx" }); chmodSync(temporary, mode); if (readFileSync(modelsPath, "utf-8") !== original) { throw new ModelsDiscoveryError(`${modelsPath} changed during discovery; nothing was written`); } renameSync(temporary, modelsPath); return backupPath; } catch (error) { rmSync(temporary, { force: true }); rmSync(backupPath, { force: true }); throw error; } } /** Fetch the provider's model listing once and upsert it into models.json. */ export async function discoverProviderModels(options) { const { content, document } = readDocument(options.modelsPath); const { key: providerKey, providers, provider } = findProvider(document, options.providerId, options.modelsPath); const url = providerListingUrl(provider, options.providerId); const listing = await fetchListing(url, options.auth, options.fetch ?? fetch, options.signal); const honorEfforts = isRecord(provider.compat) && provider.compat.supportsReasoningEffort === true; const configured = Array.isArray(provider.models) ? [...provider.models] : []; const report = { providerId: providerKey, url: url.display, modelsPath: options.modelsPath, added: [], updated: [], unchanged: [], unlisted: [], efforts: {}, effortsIgnored: false, written: false, commentsDropped: false, }; for (const listed of listing) { const parsed = honorEfforts ? parseEndpointReasoningEfforts(listed.reasoningEfforts) : undefined; if (!honorEfforts && Array.isArray(listed.reasoningEfforts)) report.effortsIgnored = true; if (parsed) report.efforts[listed.id] = describeEfforts(parsed); const index = configured.findIndex((entry) => isRecord(entry) && entry.id === listed.id); const existing = index >= 0 ? configured[index] : undefined; const current = isRecord(existing) ? existing : undefined; const next = withEfforts(current ?? { id: listed.id }, parsed); if (current === undefined) { configured.push(next); report.added.push(listed.id); } else if (isDeepStrictEqual(current, next)) { report.unchanged.push(listed.id); } else { configured[index] = next; report.updated.push(listed.id); } } const listedIds = new Set(listing.map((listed) => listed.id)); for (const entry of configured) { if (isRecord(entry) && typeof entry.id === "string" && !listedIds.has(entry.id)) report.unlisted.push(entry.id); } if (report.added.length === 0 && report.updated.length === 0) return report; const nextDocument = { ...document, providers: { ...providers, [providerKey]: { ...provider, models: configured } }, }; const nextContent = `${JSON.stringify(nextDocument, null, 2)}\n`; const invalid = ModelConfig.validationError(nextContent, options.modelsPath); if (invalid !== undefined) { throw new ModelsDiscoveryError(`Discovery would leave an invalid models.json, so nothing was written.\n${invalid}`); } report.backupPath = writeWithBackup(options.modelsPath, content, nextContent); report.written = true; report.commentsDropped = stripJsonComments(stripBom(content)) !== stripBom(content); return report; } //# sourceMappingURL=model-discovery.js.map