UNPKG

@code-yeongyu/senpi

Version:

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

181 lines • 8.91 kB
import { getModelType, isModelType, upstreamProviderIdFor, } from "@earendil-works/pi-ai"; import { VERSION } from "../config.js"; import { fetchWithRetry } from "../utils/management-http.js"; import { getPiUserAgent } from "../utils/pi-user-agent.js"; import { mergeRemoteCatalogModels, parseRemoteCatalog } from "./remote-catalog-merge.js"; const DEFAULT_CATALOG_BASE_URL = "https://pi.dev"; const REMOTE_CATALOG_ATTEMPT_TIMEOUT_MS = 4_000; export const REMOTE_CATALOG_REFRESH_INTERVAL_MS = 4 * 60 * 60 * 1000; /** * Model types this client can consume. Sent as `?types=` so the catalog server * returns the full-type shard instead of the chat-only one served to clients * that predate model types. A server that ignores the parameter still returns * the chat-only shard, which this client handles unchanged. */ export const REMOTE_CATALOG_MODEL_TYPES = ["chat", "image", "classifier"]; const NON_CHAT_MODEL_TYPES = REMOTE_CATALOG_MODEL_TYPES.filter((type) => type !== "chat"); /** * Builtin providers that exist only in this fork. Upstream's pi.dev catalog does * not serve them and answers with a non-404 failure, which becomes a chronic * per-refresh "Could not refresh <id>" warning (transient failures never persist * lastModified, so the freshness throttle below never engages for them). */ export const FORK_ONLY_BUILTIN_PROVIDERS = new Set(["alibaba-token-plan", "opengateway"]); /** * Whether the remote catalog overlay can serve a provider. Fork-only providers * are skipped under the default upstream catalog base URL; a custom base URL (a * fork-owned catalog) may serve them, so the wrap is preserved there. */ export function remoteCatalogServesProvider(providerId, catalogBaseUrl) { return catalogBaseUrl !== undefined || !FORK_ONLY_BUILTIN_PROVIDERS.has(providerId); } function isSupportedModelType(model) { return (model.type === undefined || (typeof model.type === "string" && REMOTE_CATALOG_MODEL_TYPES.includes(model.type))); } function mergeModels(baseline, dynamic) { const merged = new Map(); for (const model of [...baseline, ...dynamic]) merged.set(`${getModelType(model)}\0${model.id}`, model); return [...merged.values()]; } const remoteCatalogConflicts = new WeakMap(); /** Return capability conflicts rejected from a provider's remote overlay. */ export function getRemoteCatalogConflicts(provider) { return remoteCatalogConflicts.get(provider) ?? []; } function chatModels(models) { return models.filter((model) => isModelType(model, "chat")); } function nonChatModels(models) { return models.filter((model) => !isModelType(model, "chat")); } /** * Parse a remote catalog body. Chat entries keep the fork's strict shape validation * (`parseRemoteCatalog`: one malformed chat row rejects the body); image and * classifier entries of a supported type pass through, unsupported types are dropped. */ function parseCatalog(providerId, value) { const entries = Array.isArray(value) ? value : typeof value === "object" && value !== null && "models" in value && Array.isArray(value.models) ? value.models : typeof value === "object" && value !== null ? Object.values(value) : undefined; if (!entries) throw new Error(`Invalid model catalog for provider "${providerId}"`); const supported = entries .filter((entry) => typeof entry === "object" && entry !== null) .filter(isSupportedModelType); const isNonChat = (entry) => NON_CHAT_MODEL_TYPES.includes(entry.type); const chat = supported.filter((entry) => !isNonChat(entry)); const other = supported .filter((entry) => isNonChat(entry) && "id" in entry) .map((model) => ({ ...model, provider: providerId })); return [...parseRemoteCatalog(providerId, chat), ...other]; } function remoteModels(entry, localGeneratedAt) { if (!entry) return []; if (localGeneratedAt !== undefined && (entry.lastModified === undefined || entry.lastModified <= localGeneratedAt)) { return []; } return entry.models; } /** Add a persisted pi.dev catalog overlay to a static built-in provider. */ export function withRemoteCatalog(provider, catalogBaseUrl = DEFAULT_CATALOG_BASE_URL, localGeneratedAt) { let dynamicModels = []; const currentChatModels = () => mergeRemoteCatalogModels(provider.id, provider.getModels(), chatModels(dynamicModels)).models; const conflictsFor = (models) => mergeRemoteCatalogModels(provider.id, provider.getModels(), chatModels(models)).conflicts; const wrappedProvider = { ...provider, getModels: currentChatModels, getAllModels: () => [ ...currentChatModels(), ...mergeModels(nonChatModels(provider.getAllModels?.() ?? []), nonChatModels(dynamicModels)), ], refreshModels: async (context) => { const stored = context.stored; const restored = remoteModels(stored, localGeneratedAt).filter((model) => model.provider === provider.id); if (!(await context.publish({ update: () => { dynamicModels = restored; remoteCatalogConflicts.set(wrappedProvider, conflictsFor(restored)); }, }))) { return; } if (!context.allowNetwork || context.signal.aborted) return; if (!context.force && stored?.checkedAt !== undefined && stored.lastModified !== undefined && Date.now() - stored.checkedAt < REMOTE_CATALOG_REFRESH_INTERVAL_MS) { return; } // Only revalidate when a cached body backs the validator, so a 304 can never // leave the overlay empty. const validator = stored && stored.models.length > 0 ? stored.etag : undefined; // The catalog knows renamed providers by their upstream id; parseCatalog files the answer under provider.id. const catalogProviderId = upstreamProviderIdFor(provider.id); const url = new URL(`/api/models/providers/${encodeURIComponent(catalogProviderId)}`, catalogBaseUrl); url.searchParams.set("types", REMOTE_CATALOG_MODEL_TYPES.join(",")); const response = await fetchWithRetry(url, { headers: { accept: "application/json", "User-Agent": getPiUserAgent(VERSION), ...(validator ? { "if-none-match": validator } : {}), }, signal: context.signal, }, { attemptTimeoutMs: REMOTE_CATALOG_ATTEMPT_TIMEOUT_MS }); if (context.signal.aborted) return; const checkedAt = Date.now(); // Unchanged: dynamicModels already holds the stored overlay, so only the // freshness window moves. if (response.status === 304 && stored) { await context.publish({ persist: { ...stored, checkedAt } }); return; } if (response.status === 404 || response.status === 501) { await context.publish({ persist: { ...(stored ?? { models: [] }), checkedAt, lastModified: 0, etag: undefined, }, }); return; } if (!response.ok) { // Transient failure: the cached body and its validator stay valid, so keep the // etag and let the next refresh revalidate instead of downloading the catalog. await context.publish({ persist: { ...(stored ?? { models: [] }), checkedAt } }); throw new Error(`Model catalog request failed for ${provider.id}: ${response.status}`); } const refreshed = parseCatalog(provider.id, await response.json()); const lastModified = Date.parse(response.headers.get("last-modified") ?? ""); if (context.signal.aborted) return; const entry = { models: refreshed, checkedAt, lastModified: Number.isNaN(lastModified) ? 0 : lastModified, etag: response.headers.get("etag") ?? undefined, }; const published = remoteModels(entry, localGeneratedAt); await context.publish({ persist: entry, update: () => { dynamicModels = published; remoteCatalogConflicts.set(wrappedProvider, conflictsFor(published)); }, }); }, }; remoteCatalogConflicts.set(wrappedProvider, []); return wrappedProvider; } //# sourceMappingURL=remote-catalog-provider.js.map