index.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /**
  2. * vim: set noet ts=8:
  3. * DuckCoding AI Custom Provider
  4. *
  5. * Provides access to DuckCoding AI models via an OpenAI-compatible API.
  6. *
  7. * Usage:
  8. * Set the DUCKCODING_API_KEY environment variable with your API key.
  9. * Then use /model to select duckcoding/<model-id>.
  10. *
  11. * Model availability and rates come from DuckCoding's pricing endpoint.
  12. * The authenticated `/v1/models` list narrows that catalog to the models the
  13. * API key's billing group can actually use; the result is cached for an hour
  14. * and revalidated on refresh. Capabilities and limits come from the Pi model
  15. * catalog: the public catalog is fetched when the cache is refreshed, and Pi's
  16. * local registry re-applies catalog metadata at session start so an offline or
  17. * partial refresh still resolves correctly. The cache is refreshed after seven
  18. * days by default. Set DUCKCODING_MODEL_CACHE_TTL_MS to override the cache
  19. * lifetime.
  20. */
  21. import { createHash } from "node:crypto";
  22. import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
  23. import { homedir } from "node:os";
  24. import { join } from "node:path";
  25. import type { ExtensionAPI, ProviderModelConfig } from "@earendil-works/pi-coding-agent";
  26. type ChatModelConfig = Extract<ProviderModelConfig, { reasoning: boolean }>;
  27. type DuckCodingPricingModel = {
  28. model_name?: string;
  29. vendor_id?: number;
  30. quota_type?: number;
  31. supported_endpoint_types?: string[];
  32. enable_groups?: string[];
  33. model_price?: number;
  34. model_ratio?: number;
  35. completion_ratio?: number;
  36. cache_ratio?: number;
  37. create_cache_ratio?: number;
  38. billing_expr?: string;
  39. };
  40. type DuckCodingPricingResponse = {
  41. success?: boolean;
  42. vendors?: Array<{ id?: number; name?: string }>;
  43. data?: DuckCodingPricingModel[];
  44. };
  45. type DuckCodingModelsResponse = {
  46. data?: Array<{ id?: string }>;
  47. };
  48. type CatalogModel = {
  49. id?: string;
  50. provider?: string;
  51. type?: string;
  52. reasoning?: boolean;
  53. input?: string[];
  54. contextWindow?: number;
  55. maxTokens?: number;
  56. thinkingLevelMap?: ChatModelConfig["thinkingLevelMap"];
  57. };
  58. type CatalogIndex = Map<string, Map<string, CatalogModel>>;
  59. type ModelSet = {
  60. models: ChatModelConfig[];
  61. /** DuckCoding model ID → Pi catalog provider ID (google, anthropic, …). */
  62. vendorProviders: Map<string, string>;
  63. };
  64. type CachedModels = {
  65. version: number;
  66. fetchedAt: number;
  67. degraded?: boolean;
  68. models: ChatModelConfig[];
  69. vendorProviders: Record<string, string>;
  70. };
  71. type CachedAvailability = {
  72. version: number;
  73. keyHash: string;
  74. checkedAt: number;
  75. ids: string[];
  76. };
  77. const PRICING_URL = "https://www.duckcoding.ai/api/pricing";
  78. const MODELS_URL = "https://api.duckcoding.ai/v1/models";
  79. const CATALOG_BASE_URL = "https://pi.dev";
  80. const CATALOG_TIMEOUT_MS = 5_000;
  81. const CATALOG_TOTAL_BUDGET_MS = 12_000;
  82. const PRICING_TIMEOUT_MS = 15_000;
  83. const AVAILABILITY_TIMEOUT_MS = 6_000;
  84. const AVAILABILITY_TTL_MS = 60 * 60 * 1000;
  85. const AVAILABILITY_CACHE_VERSION = 1;
  86. const DEFAULT_MODEL_CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
  87. // Retry sooner when the Pi catalog could not be fetched and some limits fell
  88. // back to family defaults.
  89. const DEGRADED_CACHE_TTL_MS = 6 * 60 * 60 * 1000;
  90. const CACHE_VERSION = 2;
  91. const MODEL_CACHE_DIR = join(homedir(), ".cache", "pi-agent-duckcoding");
  92. const MODEL_CACHE_PATH = join(MODEL_CACHE_DIR, "models.json");
  93. const AVAILABILITY_CACHE_PATH = join(MODEL_CACHE_DIR, "availability.json");
  94. /** DuckCoding vendor names mapped to Pi catalog provider IDs. */
  95. const VENDOR_CATALOG_PROVIDERS: Record<string, string> = {
  96. gemini: "google",
  97. anthropic: "anthropic",
  98. openai: "openai",
  99. grok: "xai",
  100. deepseek: "deepseek",
  101. zhipu: "zai",
  102. "智谱": "zai",
  103. };
  104. type FamilyDefaults = {
  105. reasoning: boolean;
  106. contextWindow: number;
  107. maxTokens: number;
  108. };
  109. function titleCaseModelName(modelName: string) {
  110. return modelName
  111. .split(/[-_/]+/)
  112. .map((part) => (part ? part[0].toUpperCase() + part.slice(1) : part))
  113. .join(" ");
  114. }
  115. /** Fallback limits for models missing from the Pi catalog. */
  116. function inferFamilyDefaults(modelName: string): FamilyDefaults {
  117. if (/gemini-3|gemini-2\.5/i.test(modelName)) {
  118. return { reasoning: true, contextWindow: 1048576, maxTokens: 65536 };
  119. }
  120. if (/^claude/i.test(modelName)) {
  121. return { reasoning: true, contextWindow: 1000000, maxTokens: 128000 };
  122. }
  123. if (/^gpt-6|^gpt-5\.6/i.test(modelName)) {
  124. return { reasoning: true, contextWindow: 272000, maxTokens: 128000 };
  125. }
  126. if (/^gpt-5|^o[34]/i.test(modelName)) {
  127. return { reasoning: true, contextWindow: 400000, maxTokens: 128000 };
  128. }
  129. if (/^grok-[5-9]|^grok-4\.[5-9]/i.test(modelName)) {
  130. return { reasoning: true, contextWindow: 500000, maxTokens: 500000 };
  131. }
  132. if (/^grok-4/i.test(modelName)) {
  133. return { reasoning: true, contextWindow: 200000, maxTokens: 32000 };
  134. }
  135. if (/^deepseek/i.test(modelName)) {
  136. return { reasoning: true, contextWindow: 1000000, maxTokens: 384000 };
  137. }
  138. if (/^glm-5/i.test(modelName)) {
  139. return { reasoning: true, contextWindow: 1000000, maxTokens: 131072 };
  140. }
  141. if (/reasoner|thinking|codex/i.test(modelName)) {
  142. return { reasoning: true, contextWindow: 400000, maxTokens: 128000 };
  143. }
  144. return { reasoning: false, contextWindow: 128000, maxTokens: 16384 };
  145. }
  146. function inferInputModes(endpoints: string[] = []) {
  147. const input: ChatModelConfig["input"] = ["text"];
  148. if (endpoints.some((endpoint) => ["openai", "anthropic", "gemini"].includes(endpoint))) {
  149. input.push("image");
  150. }
  151. return input;
  152. }
  153. function normalizeInput(input: string[] | undefined): ChatModelConfig["input"] {
  154. const modes: ChatModelConfig["input"] = [];
  155. for (const mode of input ?? []) {
  156. if ((mode === "text" || mode === "image") && !modes.includes(mode)) modes.push(mode);
  157. }
  158. if (modes.length === 0) modes.push("text");
  159. return modes;
  160. }
  161. function roundRate(value: number) {
  162. return Number.isFinite(value) ? Number(value.toFixed(6)) : 0;
  163. }
  164. /**
  165. * DuckCoding bills through a small expression language. The standard tier is
  166. * listed first; `p`, `c`, `cr`, and `cc` are USD per million input, output,
  167. * cache-read, and cache-write tokens.
  168. */
  169. function parseBillingExpr(expr: string | undefined) {
  170. if (!expr) return undefined;
  171. const rate = (variable: string) => {
  172. const match = expr.match(new RegExp(`(?:^|[^a-zA-Z0-9_])${variable}\\s*\\*\\s*([0-9]+(?:\\.[0-9]+)?)`));
  173. return match ? roundRate(Number(match[1])) : 0;
  174. };
  175. return {
  176. input: rate("p"),
  177. output: rate("c"),
  178. cacheRead: rate("cr"),
  179. cacheWrite: rate("cc"),
  180. };
  181. }
  182. function inferCost(model: DuckCodingPricingModel): ChatModelConfig["cost"] {
  183. const parsed = parseBillingExpr(model.billing_expr);
  184. if (parsed && (parsed.input || parsed.output || parsed.cacheRead || parsed.cacheWrite)) return parsed;
  185. // Legacy fallback: `model_ratio` is a quota ratio, not USD per token.
  186. const input = roundRate((model.model_ratio ?? 0) * 2);
  187. return {
  188. input,
  189. output: roundRate(input * (model.completion_ratio ?? 0)),
  190. cacheRead: roundRate(input * (model.cache_ratio ?? 0)),
  191. cacheWrite: roundRate(input * (model.create_cache_ratio ?? 0)),
  192. };
  193. }
  194. /** Exclude image, video, embedding, and classifier entries billed per request. */
  195. function isChatModel(model: DuckCodingPricingModel) {
  196. if (!model.model_name) return false;
  197. if (model.quota_type !== undefined && model.quota_type !== 0) return false;
  198. if (!(model.supported_endpoint_types ?? []).some((endpoint) => endpoint === "openai" || endpoint === "anthropic")) {
  199. return false;
  200. }
  201. return !/^jev-/i.test(model.model_name);
  202. }
  203. function vendorCatalogProvider(vendorId: number | undefined, vendors: Map<number, string>) {
  204. if (vendorId === undefined) return undefined;
  205. const name = vendors.get(vendorId)?.trim().toLowerCase();
  206. return name ? VENDOR_CATALOG_PROVIDERS[name] : undefined;
  207. }
  208. function catalogIdCandidates(id: string) {
  209. const candidates = [id];
  210. const compactStripped = id.replace(/-openai-compact$/, "");
  211. if (compactStripped !== id) candidates.push(compactStripped);
  212. return candidates;
  213. }
  214. function findCatalogModel(catalogs: CatalogIndex, provider: string | undefined, modelName: string) {
  215. if (!provider) return undefined;
  216. const catalog = catalogs.get(provider);
  217. if (!catalog) return undefined;
  218. for (const candidate of catalogIdCandidates(modelName)) {
  219. const entry = catalog.get(candidate);
  220. if (entry) return entry;
  221. }
  222. return undefined;
  223. }
  224. function withCatalogMetadata(model: ChatModelConfig, catalog: CatalogModel | undefined) {
  225. if (!catalog) return model;
  226. return {
  227. ...model,
  228. reasoning: catalog.reasoning ?? model.reasoning,
  229. input: catalog.input ? normalizeInput(catalog.input) : model.input,
  230. contextWindow: catalog.contextWindow ?? model.contextWindow,
  231. maxTokens: catalog.maxTokens ?? model.maxTokens,
  232. thinkingLevelMap: catalog.thinkingLevelMap ?? model.thinkingLevelMap,
  233. };
  234. }
  235. function sameModelMetadata(a: ChatModelConfig, b: ChatModelConfig) {
  236. return (
  237. a.reasoning === b.reasoning &&
  238. a.contextWindow === b.contextWindow &&
  239. a.maxTokens === b.maxTokens &&
  240. JSON.stringify(a.input) === JSON.stringify(b.input) &&
  241. JSON.stringify(a.thinkingLevelMap ?? null) === JSON.stringify(b.thinkingLevelMap ?? null)
  242. );
  243. }
  244. function applyCatalogMetadata(models: ChatModelConfig[], vendorProviders: Map<string, string>, catalogs: CatalogIndex) {
  245. return models.map((model) =>
  246. withCatalogMetadata(model, findCatalogModel(catalogs, vendorProviders.get(model.id), model.id)),
  247. );
  248. }
  249. async function fetchCatalogProvider(provider: string, timeoutMs: number) {
  250. const url = `${CATALOG_BASE_URL}/api/models/providers/${encodeURIComponent(provider)}?types=chat`;
  251. try {
  252. const response = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) });
  253. if (!response.ok) return undefined;
  254. const payload = (await response.json()) as unknown;
  255. const entries = Array.isArray(payload)
  256. ? payload
  257. : typeof payload === "object" && payload !== null && Array.isArray((payload as { models?: unknown }).models)
  258. ? ((payload as { models: unknown[] }).models as unknown[])
  259. : [];
  260. const models = new Map<string, CatalogModel>();
  261. for (const entry of entries) {
  262. if (typeof entry !== "object" || entry === null) continue;
  263. const model = entry as CatalogModel;
  264. if (typeof model.id !== "string") continue;
  265. if (model.type !== undefined && model.type !== "chat") continue;
  266. models.set(model.id, model);
  267. }
  268. return models.size > 0 ? models : undefined;
  269. } catch {
  270. return undefined;
  271. }
  272. }
  273. function collectCatalogProviders(pricing: DuckCodingPricingResponse) {
  274. const vendors = new Map<number, string>();
  275. for (const vendor of pricing.vendors ?? []) {
  276. if (vendor.id !== undefined && vendor.name) vendors.set(vendor.id, vendor.name);
  277. }
  278. const providers = new Set<string>();
  279. for (const model of pricing.data ?? []) {
  280. if (!isChatModel(model)) continue;
  281. const provider = vendorCatalogProvider(model.vendor_id, vendors);
  282. if (provider) providers.add(provider);
  283. }
  284. return [...providers];
  285. }
  286. async function fetchCatalogs(pricing: DuckCodingPricingResponse) {
  287. const providers = collectCatalogProviders(pricing);
  288. const deadline = Date.now() + CATALOG_TOTAL_BUDGET_MS;
  289. const catalogs: CatalogIndex = new Map();
  290. const pending = new Set(providers);
  291. // Keep catalog work bounded. A few quick parallel requests cover the normal
  292. // case; failures are retried one at a time while the budget lasts, which is
  293. // more reliable than hammering a proxy with a second parallel wave.
  294. await Promise.all(
  295. providers.map(async (provider) => {
  296. const remaining = deadline - Date.now();
  297. if (remaining <= 0) return;
  298. const models = await fetchCatalogProvider(provider, Math.min(CATALOG_TIMEOUT_MS, remaining));
  299. if (models) {
  300. catalogs.set(provider, models);
  301. pending.delete(provider);
  302. }
  303. }),
  304. );
  305. for (const provider of [...pending]) {
  306. const remaining = deadline - Date.now();
  307. if (remaining <= 0) break;
  308. const models = await fetchCatalogProvider(provider, Math.min(CATALOG_TIMEOUT_MS, remaining));
  309. if (models) {
  310. catalogs.set(provider, models);
  311. pending.delete(provider);
  312. }
  313. }
  314. return { catalogs, degraded: pending.size > 0 };
  315. }
  316. /** Build a catalog index from the models Pi already has registered locally. */
  317. function localCatalogIndex(models: readonly CatalogModel[]): CatalogIndex {
  318. const catalogs: CatalogIndex = new Map();
  319. for (const model of models) {
  320. if (typeof model.id !== "string" || typeof model.provider !== "string") continue;
  321. if (!Object.values(VENDOR_CATALOG_PROVIDERS).includes(model.provider)) continue;
  322. const provider = model.provider;
  323. let catalog = catalogs.get(provider);
  324. if (!catalog) catalogs.set(provider, (catalog = new Map()));
  325. catalog.set(model.id, model);
  326. }
  327. return catalogs;
  328. }
  329. function createModels(pricing: DuckCodingPricingResponse, catalogs: CatalogIndex): ModelSet {
  330. const vendors = new Map<number, string>();
  331. for (const vendor of pricing.vendors ?? []) {
  332. if (vendor.id !== undefined && vendor.name) vendors.set(vendor.id, vendor.name);
  333. }
  334. const vendorProviders = new Map<string, string>();
  335. const models = (pricing.data ?? [])
  336. .filter(isChatModel)
  337. .map((model): ChatModelConfig => {
  338. const id = model.model_name!;
  339. const provider = vendorCatalogProvider(model.vendor_id, vendors);
  340. if (provider) vendorProviders.set(id, provider);
  341. const fallback = inferFamilyDefaults(id);
  342. return withCatalogMetadata(
  343. {
  344. id,
  345. name: titleCaseModelName(id),
  346. reasoning: fallback.reasoning,
  347. input: inferInputModes(model.supported_endpoint_types),
  348. cost: inferCost(model),
  349. contextWindow: fallback.contextWindow,
  350. maxTokens: fallback.maxTokens,
  351. },
  352. findCatalogModel(catalogs, provider, id),
  353. );
  354. })
  355. .sort((a, b) => a.id.localeCompare(b.id));
  356. return { models, vendorProviders };
  357. }
  358. async function readCachedModels() {
  359. try {
  360. const cached = JSON.parse(await readFile(MODEL_CACHE_PATH, "utf8")) as CachedModels;
  361. if (
  362. cached.version === CACHE_VERSION &&
  363. Array.isArray(cached.models) &&
  364. typeof cached.fetchedAt === "number" &&
  365. typeof cached.vendorProviders === "object" &&
  366. cached.vendorProviders !== null
  367. ) {
  368. return cached;
  369. }
  370. } catch {
  371. // A missing, outdated, or invalid cache is treated as a cache miss.
  372. }
  373. return undefined;
  374. }
  375. async function writeCachedModels(set: ModelSet, degraded: boolean) {
  376. const temporaryPath = `${MODEL_CACHE_PATH}.tmp`;
  377. await mkdir(MODEL_CACHE_DIR, { recursive: true });
  378. await writeFile(
  379. temporaryPath,
  380. JSON.stringify(
  381. {
  382. version: CACHE_VERSION,
  383. fetchedAt: Date.now(),
  384. degraded,
  385. models: set.models,
  386. vendorProviders: Object.fromEntries(set.vendorProviders),
  387. },
  388. null,
  389. 2,
  390. ),
  391. );
  392. await rename(temporaryPath, MODEL_CACHE_PATH);
  393. }
  394. async function fetchPricing() {
  395. const response = await fetch(PRICING_URL, { signal: AbortSignal.timeout(PRICING_TIMEOUT_MS) });
  396. if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
  397. return (await response.json()) as DuckCodingPricingResponse;
  398. }
  399. /**
  400. * The authenticated model list only contains models enabled for the API key's
  401. * billing group. The pricing endpoint lists every group, so it over-advertises
  402. * models that would fail with `model_not_found`.
  403. */
  404. async function fetchAvailableModelIds(apiKey: string, signal: AbortSignal) {
  405. try {
  406. const response = await fetch(MODELS_URL, {
  407. headers: { Authorization: `Bearer ${apiKey}`, Accept: "application/json" },
  408. signal: AbortSignal.any([signal, AbortSignal.timeout(AVAILABILITY_TIMEOUT_MS)]),
  409. });
  410. if (!response.ok) return undefined;
  411. const payload = (await response.json()) as DuckCodingModelsResponse;
  412. const ids = new Set<string>();
  413. for (const model of payload.data ?? []) {
  414. if (typeof model.id === "string") ids.add(model.id);
  415. }
  416. return ids.size > 0 ? ids : undefined;
  417. } catch {
  418. return undefined;
  419. }
  420. }
  421. function hashApiKey(apiKey: string) {
  422. return createHash("sha256").update(apiKey).digest("hex").slice(0, 16);
  423. }
  424. async function readCachedAvailability() {
  425. try {
  426. const cached = JSON.parse(await readFile(AVAILABILITY_CACHE_PATH, "utf8")) as CachedAvailability;
  427. if (
  428. cached.version === AVAILABILITY_CACHE_VERSION &&
  429. typeof cached.keyHash === "string" &&
  430. typeof cached.checkedAt === "number" &&
  431. Array.isArray(cached.ids)
  432. ) {
  433. return cached;
  434. }
  435. } catch {
  436. // A missing or invalid availability cache is treated as a cache miss.
  437. }
  438. return undefined;
  439. }
  440. async function writeCachedAvailability(keyHash: string, ids: Set<string>) {
  441. const temporaryPath = `${AVAILABILITY_CACHE_PATH}.tmp`;
  442. await mkdir(MODEL_CACHE_DIR, { recursive: true });
  443. await writeFile(
  444. temporaryPath,
  445. JSON.stringify({
  446. version: AVAILABILITY_CACHE_VERSION,
  447. keyHash,
  448. checkedAt: Date.now(),
  449. ids: [...ids],
  450. }),
  451. );
  452. await rename(temporaryPath, AVAILABILITY_CACHE_PATH);
  453. }
  454. async function getModels(): Promise<ModelSet> {
  455. const cached = await readCachedModels();
  456. const cacheTtl = Number(process.env.DUCKCODING_MODEL_CACHE_TTL_MS ?? DEFAULT_MODEL_CACHE_TTL_MS);
  457. const ttl = Number.isFinite(cacheTtl) && cacheTtl >= 0 ? cacheTtl : DEFAULT_MODEL_CACHE_TTL_MS;
  458. const effectiveTtl = cached?.degraded ? Math.min(ttl, DEGRADED_CACHE_TTL_MS) : ttl;
  459. if (cached && Date.now() - cached.fetchedAt < effectiveTtl) {
  460. return { models: cached.models, vendorProviders: new Map(Object.entries(cached.vendorProviders)) };
  461. }
  462. try {
  463. const pricing = await fetchPricing();
  464. const { catalogs, degraded } = await fetchCatalogs(pricing);
  465. const set = createModels(pricing, catalogs);
  466. if (set.models.length === 0) throw new Error("pricing response contained no compatible models");
  467. try {
  468. await writeCachedModels(set, degraded);
  469. } catch {
  470. // The freshly fetched list is still usable if the cache cannot be written.
  471. }
  472. return set;
  473. } catch (error) {
  474. if (cached) return { models: cached.models, vendorProviders: new Map(Object.entries(cached.vendorProviders)) };
  475. throw new Error(`Failed to fetch DuckCoding pricing: ${error instanceof Error ? error.message : String(error)}`);
  476. }
  477. }
  478. type AvailabilityState = {
  479. ids: Set<string> | undefined;
  480. keyHash: string | undefined;
  481. checkedAt: number | undefined;
  482. load: Promise<void> | undefined;
  483. inFlight: Promise<void> | undefined;
  484. };
  485. function registerDuckCoding(pi: ExtensionAPI, models: ChatModelConfig[], availability: AvailabilityState) {
  486. const availableModels = () => {
  487. if (!availability.ids) return models;
  488. const filtered = models.filter((model) => availability.ids?.has(model.id));
  489. // If the pricing catalog and the key's model list share nothing, keep the
  490. // full list rather than presenting an empty provider.
  491. return filtered.length > 0 ? filtered : models;
  492. };
  493. pi.registerProvider("duckcoding", {
  494. name: "DuckCoding AI",
  495. baseUrl: "https://api.duckcoding.ai/v1",
  496. apiKey: "$DUCKCODING_API_KEY",
  497. api: "openai-completions",
  498. models: availableModels(),
  499. headers: {},
  500. refreshModels: async (context) => {
  501. const apiKey = context.credential?.type === "api_key" ? context.credential.key : undefined;
  502. const keyHash = apiKey ? hashApiKey(apiKey) : undefined;
  503. if (!availability.load) {
  504. availability.load = (async () => {
  505. const cached = await readCachedAvailability();
  506. if (cached) {
  507. availability.ids = new Set(cached.ids);
  508. availability.keyHash = cached.keyHash;
  509. availability.checkedAt = cached.checkedAt;
  510. }
  511. })();
  512. }
  513. await availability.load;
  514. const cacheMatches = keyHash !== undefined && availability.keyHash === keyHash;
  515. if (!cacheMatches) {
  516. // Never apply a previous key's availability to a different credential.
  517. availability.ids = undefined;
  518. availability.checkedAt = undefined;
  519. }
  520. const stale = !availability.checkedAt || Date.now() - availability.checkedAt > AVAILABILITY_TTL_MS;
  521. // Pi passes allowNetwork=false during normal startup, so honoring that
  522. // flag here would leave dead models in the picker. This is a small,
  523. // credential-scoped check with its own TTL cache; explicit offline mode
  524. // still short-circuits it.
  525. const canRefresh = apiKey !== undefined && keyHash !== undefined && !context.signal.aborted && !process.env.PI_OFFLINE;
  526. if (stale && canRefresh) {
  527. if (!availability.inFlight) {
  528. availability.inFlight = (async () => {
  529. try {
  530. const ids = await fetchAvailableModelIds(apiKey!, context.signal);
  531. if (ids) {
  532. availability.ids = ids;
  533. availability.keyHash = keyHash!;
  534. availability.checkedAt = Date.now();
  535. try {
  536. await writeCachedAvailability(keyHash!, ids);
  537. } catch {
  538. // Availability still applies for this process.
  539. }
  540. }
  541. } finally {
  542. availability.inFlight = undefined;
  543. }
  544. })();
  545. }
  546. await availability.inFlight;
  547. }
  548. return availableModels();
  549. },
  550. });
  551. }
  552. export default async function (pi: ExtensionAPI) {
  553. let { models, vendorProviders } = await getModels();
  554. const availability: AvailabilityState = {
  555. ids: undefined,
  556. keyHash: undefined,
  557. checkedAt: undefined,
  558. load: undefined,
  559. inFlight: undefined,
  560. };
  561. registerDuckCoding(pi, models, availability);
  562. // Pi's local registry includes the bundled catalog for built-in providers.
  563. // Prefer it at session start: it is offline-safe and repairs any model that
  564. // used family defaults because the public catalog was unreachable.
  565. pi.on("session_start", (_event, ctx) => {
  566. const catalogs = localCatalogIndex(ctx.modelRegistry.getAll());
  567. if (catalogs.size === 0) return;
  568. const enriched = applyCatalogMetadata(models, vendorProviders, catalogs);
  569. if (enriched.some((model, index) => !sameModelMetadata(model, models[index]))) {
  570. models = enriched;
  571. registerDuckCoding(pi, models, availability);
  572. }
  573. });
  574. }