/** * vim: set noet ts=8: * DuckCoding AI Custom Provider * * Provides access to DuckCoding AI models via an OpenAI-compatible API. * * Usage: * Set the DUCKCODING_API_KEY environment variable with your API key. * Then use /model to select duckcoding/. * * Model availability and rates come from DuckCoding's pricing endpoint. * The authenticated `/v1/models` list narrows that catalog to the models the * API key's billing group can actually use; the result is cached for an hour * and revalidated on refresh. Capabilities and limits come from the Pi model * catalog: the public catalog is fetched when the cache is refreshed, and Pi's * local registry re-applies catalog metadata at session start so an offline or * partial refresh still resolves correctly. The cache is refreshed after seven * days by default. Set DUCKCODING_MODEL_CACHE_TTL_MS to override the cache * lifetime. */ import { createHash } from "node:crypto"; import { mkdir, readFile, rename, writeFile } from "node:fs/promises"; import { homedir } from "node:os"; import { join } from "node:path"; import type { ExtensionAPI, ProviderModelConfig } from "@earendil-works/pi-coding-agent"; type ChatModelConfig = Extract; type DuckCodingPricingModel = { model_name?: string; vendor_id?: number; quota_type?: number; supported_endpoint_types?: string[]; enable_groups?: string[]; model_price?: number; model_ratio?: number; completion_ratio?: number; cache_ratio?: number; create_cache_ratio?: number; billing_expr?: string; }; type DuckCodingPricingResponse = { success?: boolean; vendors?: Array<{ id?: number; name?: string }>; data?: DuckCodingPricingModel[]; }; type DuckCodingModelsResponse = { data?: Array<{ id?: string }>; }; type CatalogModel = { id?: string; provider?: string; type?: string; reasoning?: boolean; input?: string[]; contextWindow?: number; maxTokens?: number; thinkingLevelMap?: ChatModelConfig["thinkingLevelMap"]; }; type CatalogIndex = Map>; type ModelSet = { models: ChatModelConfig[]; /** DuckCoding model ID → Pi catalog provider ID (google, anthropic, …). */ vendorProviders: Map; }; type CachedModels = { version: number; fetchedAt: number; degraded?: boolean; models: ChatModelConfig[]; vendorProviders: Record; }; type CachedAvailability = { version: number; keyHash: string; checkedAt: number; ids: string[]; }; const PRICING_URL = "https://www.duckcoding.ai/api/pricing"; const MODELS_URL = "https://api.duckcoding.ai/v1/models"; const CATALOG_BASE_URL = "https://pi.dev"; const CATALOG_TIMEOUT_MS = 5_000; const CATALOG_TOTAL_BUDGET_MS = 12_000; const PRICING_TIMEOUT_MS = 15_000; const AVAILABILITY_TIMEOUT_MS = 6_000; const AVAILABILITY_TTL_MS = 60 * 60 * 1000; const AVAILABILITY_CACHE_VERSION = 1; const DEFAULT_MODEL_CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000; // Retry sooner when the Pi catalog could not be fetched and some limits fell // back to family defaults. const DEGRADED_CACHE_TTL_MS = 6 * 60 * 60 * 1000; const CACHE_VERSION = 2; const MODEL_CACHE_DIR = join(homedir(), ".cache", "pi-agent-duckcoding"); const MODEL_CACHE_PATH = join(MODEL_CACHE_DIR, "models.json"); const AVAILABILITY_CACHE_PATH = join(MODEL_CACHE_DIR, "availability.json"); /** DuckCoding vendor names mapped to Pi catalog provider IDs. */ const VENDOR_CATALOG_PROVIDERS: Record = { gemini: "google", anthropic: "anthropic", openai: "openai", grok: "xai", deepseek: "deepseek", zhipu: "zai", "智谱": "zai", }; type FamilyDefaults = { reasoning: boolean; contextWindow: number; maxTokens: number; }; function titleCaseModelName(modelName: string) { return modelName .split(/[-_/]+/) .map((part) => (part ? part[0].toUpperCase() + part.slice(1) : part)) .join(" "); } /** Fallback limits for models missing from the Pi catalog. */ function inferFamilyDefaults(modelName: string): FamilyDefaults { if (/gemini-3|gemini-2\.5/i.test(modelName)) { return { reasoning: true, contextWindow: 1048576, maxTokens: 65536 }; } if (/^claude/i.test(modelName)) { return { reasoning: true, contextWindow: 1000000, maxTokens: 128000 }; } if (/^gpt-6|^gpt-5\.6/i.test(modelName)) { return { reasoning: true, contextWindow: 272000, maxTokens: 128000 }; } if (/^gpt-5|^o[34]/i.test(modelName)) { return { reasoning: true, contextWindow: 400000, maxTokens: 128000 }; } if (/^grok-[5-9]|^grok-4\.[5-9]/i.test(modelName)) { return { reasoning: true, contextWindow: 500000, maxTokens: 500000 }; } if (/^grok-4/i.test(modelName)) { return { reasoning: true, contextWindow: 200000, maxTokens: 32000 }; } if (/^deepseek/i.test(modelName)) { return { reasoning: true, contextWindow: 1000000, maxTokens: 384000 }; } if (/^glm-5/i.test(modelName)) { return { reasoning: true, contextWindow: 1000000, maxTokens: 131072 }; } if (/reasoner|thinking|codex/i.test(modelName)) { return { reasoning: true, contextWindow: 400000, maxTokens: 128000 }; } return { reasoning: false, contextWindow: 128000, maxTokens: 16384 }; } function inferInputModes(endpoints: string[] = []) { const input: ChatModelConfig["input"] = ["text"]; if (endpoints.some((endpoint) => ["openai", "anthropic", "gemini"].includes(endpoint))) { input.push("image"); } return input; } function normalizeInput(input: string[] | undefined): ChatModelConfig["input"] { const modes: ChatModelConfig["input"] = []; for (const mode of input ?? []) { if ((mode === "text" || mode === "image") && !modes.includes(mode)) modes.push(mode); } if (modes.length === 0) modes.push("text"); return modes; } function roundRate(value: number) { return Number.isFinite(value) ? Number(value.toFixed(6)) : 0; } /** * DuckCoding bills through a small expression language. The standard tier is * listed first; `p`, `c`, `cr`, and `cc` are USD per million input, output, * cache-read, and cache-write tokens. */ function parseBillingExpr(expr: string | undefined) { if (!expr) return undefined; const rate = (variable: string) => { const match = expr.match(new RegExp(`(?:^|[^a-zA-Z0-9_])${variable}\\s*\\*\\s*([0-9]+(?:\\.[0-9]+)?)`)); return match ? roundRate(Number(match[1])) : 0; }; return { input: rate("p"), output: rate("c"), cacheRead: rate("cr"), cacheWrite: rate("cc"), }; } function inferCost(model: DuckCodingPricingModel): ChatModelConfig["cost"] { const parsed = parseBillingExpr(model.billing_expr); if (parsed && (parsed.input || parsed.output || parsed.cacheRead || parsed.cacheWrite)) return parsed; // Legacy fallback: `model_ratio` is a quota ratio, not USD per token. const input = roundRate((model.model_ratio ?? 0) * 2); return { input, output: roundRate(input * (model.completion_ratio ?? 0)), cacheRead: roundRate(input * (model.cache_ratio ?? 0)), cacheWrite: roundRate(input * (model.create_cache_ratio ?? 0)), }; } /** Exclude image, video, embedding, and classifier entries billed per request. */ function isChatModel(model: DuckCodingPricingModel) { if (!model.model_name) return false; if (model.quota_type !== undefined && model.quota_type !== 0) return false; if (!(model.supported_endpoint_types ?? []).some((endpoint) => endpoint === "openai" || endpoint === "anthropic")) { return false; } return !/^jev-/i.test(model.model_name); } function vendorCatalogProvider(vendorId: number | undefined, vendors: Map) { if (vendorId === undefined) return undefined; const name = vendors.get(vendorId)?.trim().toLowerCase(); return name ? VENDOR_CATALOG_PROVIDERS[name] : undefined; } function catalogIdCandidates(id: string) { const candidates = [id]; const compactStripped = id.replace(/-openai-compact$/, ""); if (compactStripped !== id) candidates.push(compactStripped); return candidates; } function findCatalogModel(catalogs: CatalogIndex, provider: string | undefined, modelName: string) { if (!provider) return undefined; const catalog = catalogs.get(provider); if (!catalog) return undefined; for (const candidate of catalogIdCandidates(modelName)) { const entry = catalog.get(candidate); if (entry) return entry; } return undefined; } function withCatalogMetadata(model: ChatModelConfig, catalog: CatalogModel | undefined) { if (!catalog) return model; return { ...model, reasoning: catalog.reasoning ?? model.reasoning, input: catalog.input ? normalizeInput(catalog.input) : model.input, contextWindow: catalog.contextWindow ?? model.contextWindow, maxTokens: catalog.maxTokens ?? model.maxTokens, thinkingLevelMap: catalog.thinkingLevelMap ?? model.thinkingLevelMap, }; } function sameModelMetadata(a: ChatModelConfig, b: ChatModelConfig) { return ( a.reasoning === b.reasoning && a.contextWindow === b.contextWindow && a.maxTokens === b.maxTokens && JSON.stringify(a.input) === JSON.stringify(b.input) && JSON.stringify(a.thinkingLevelMap ?? null) === JSON.stringify(b.thinkingLevelMap ?? null) ); } function applyCatalogMetadata(models: ChatModelConfig[], vendorProviders: Map, catalogs: CatalogIndex) { return models.map((model) => withCatalogMetadata(model, findCatalogModel(catalogs, vendorProviders.get(model.id), model.id)), ); } async function fetchCatalogProvider(provider: string, timeoutMs: number) { const url = `${CATALOG_BASE_URL}/api/models/providers/${encodeURIComponent(provider)}?types=chat`; try { const response = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) }); if (!response.ok) return undefined; const payload = (await response.json()) as unknown; const entries = Array.isArray(payload) ? payload : typeof payload === "object" && payload !== null && Array.isArray((payload as { models?: unknown }).models) ? ((payload as { models: unknown[] }).models as unknown[]) : []; const models = new Map(); for (const entry of entries) { if (typeof entry !== "object" || entry === null) continue; const model = entry as CatalogModel; if (typeof model.id !== "string") continue; if (model.type !== undefined && model.type !== "chat") continue; models.set(model.id, model); } return models.size > 0 ? models : undefined; } catch { return undefined; } } function collectCatalogProviders(pricing: DuckCodingPricingResponse) { const vendors = new Map(); for (const vendor of pricing.vendors ?? []) { if (vendor.id !== undefined && vendor.name) vendors.set(vendor.id, vendor.name); } const providers = new Set(); for (const model of pricing.data ?? []) { if (!isChatModel(model)) continue; const provider = vendorCatalogProvider(model.vendor_id, vendors); if (provider) providers.add(provider); } return [...providers]; } async function fetchCatalogs(pricing: DuckCodingPricingResponse) { const providers = collectCatalogProviders(pricing); const deadline = Date.now() + CATALOG_TOTAL_BUDGET_MS; const catalogs: CatalogIndex = new Map(); const pending = new Set(providers); // Keep catalog work bounded. A few quick parallel requests cover the normal // case; failures are retried one at a time while the budget lasts, which is // more reliable than hammering a proxy with a second parallel wave. await Promise.all( providers.map(async (provider) => { const remaining = deadline - Date.now(); if (remaining <= 0) return; const models = await fetchCatalogProvider(provider, Math.min(CATALOG_TIMEOUT_MS, remaining)); if (models) { catalogs.set(provider, models); pending.delete(provider); } }), ); for (const provider of [...pending]) { const remaining = deadline - Date.now(); if (remaining <= 0) break; const models = await fetchCatalogProvider(provider, Math.min(CATALOG_TIMEOUT_MS, remaining)); if (models) { catalogs.set(provider, models); pending.delete(provider); } } return { catalogs, degraded: pending.size > 0 }; } /** Build a catalog index from the models Pi already has registered locally. */ function localCatalogIndex(models: readonly CatalogModel[]): CatalogIndex { const catalogs: CatalogIndex = new Map(); for (const model of models) { if (typeof model.id !== "string" || typeof model.provider !== "string") continue; if (!Object.values(VENDOR_CATALOG_PROVIDERS).includes(model.provider)) continue; const provider = model.provider; let catalog = catalogs.get(provider); if (!catalog) catalogs.set(provider, (catalog = new Map())); catalog.set(model.id, model); } return catalogs; } function createModels(pricing: DuckCodingPricingResponse, catalogs: CatalogIndex): ModelSet { const vendors = new Map(); for (const vendor of pricing.vendors ?? []) { if (vendor.id !== undefined && vendor.name) vendors.set(vendor.id, vendor.name); } const vendorProviders = new Map(); const models = (pricing.data ?? []) .filter(isChatModel) .map((model): ChatModelConfig => { const id = model.model_name!; const provider = vendorCatalogProvider(model.vendor_id, vendors); if (provider) vendorProviders.set(id, provider); const fallback = inferFamilyDefaults(id); return withCatalogMetadata( { id, name: titleCaseModelName(id), reasoning: fallback.reasoning, input: inferInputModes(model.supported_endpoint_types), cost: inferCost(model), contextWindow: fallback.contextWindow, maxTokens: fallback.maxTokens, }, findCatalogModel(catalogs, provider, id), ); }) .sort((a, b) => a.id.localeCompare(b.id)); return { models, vendorProviders }; } async function readCachedModels() { try { const cached = JSON.parse(await readFile(MODEL_CACHE_PATH, "utf8")) as CachedModels; if ( cached.version === CACHE_VERSION && Array.isArray(cached.models) && typeof cached.fetchedAt === "number" && typeof cached.vendorProviders === "object" && cached.vendorProviders !== null ) { return cached; } } catch { // A missing, outdated, or invalid cache is treated as a cache miss. } return undefined; } async function writeCachedModels(set: ModelSet, degraded: boolean) { const temporaryPath = `${MODEL_CACHE_PATH}.tmp`; await mkdir(MODEL_CACHE_DIR, { recursive: true }); await writeFile( temporaryPath, JSON.stringify( { version: CACHE_VERSION, fetchedAt: Date.now(), degraded, models: set.models, vendorProviders: Object.fromEntries(set.vendorProviders), }, null, 2, ), ); await rename(temporaryPath, MODEL_CACHE_PATH); } async function fetchPricing() { const response = await fetch(PRICING_URL, { signal: AbortSignal.timeout(PRICING_TIMEOUT_MS) }); if (!response.ok) throw new Error(`${response.status} ${response.statusText}`); return (await response.json()) as DuckCodingPricingResponse; } /** * The authenticated model list only contains models enabled for the API key's * billing group. The pricing endpoint lists every group, so it over-advertises * models that would fail with `model_not_found`. */ async function fetchAvailableModelIds(apiKey: string, signal: AbortSignal) { try { const response = await fetch(MODELS_URL, { headers: { Authorization: `Bearer ${apiKey}`, Accept: "application/json" }, signal: AbortSignal.any([signal, AbortSignal.timeout(AVAILABILITY_TIMEOUT_MS)]), }); if (!response.ok) return undefined; const payload = (await response.json()) as DuckCodingModelsResponse; const ids = new Set(); for (const model of payload.data ?? []) { if (typeof model.id === "string") ids.add(model.id); } return ids.size > 0 ? ids : undefined; } catch { return undefined; } } function hashApiKey(apiKey: string) { return createHash("sha256").update(apiKey).digest("hex").slice(0, 16); } async function readCachedAvailability() { try { const cached = JSON.parse(await readFile(AVAILABILITY_CACHE_PATH, "utf8")) as CachedAvailability; if ( cached.version === AVAILABILITY_CACHE_VERSION && typeof cached.keyHash === "string" && typeof cached.checkedAt === "number" && Array.isArray(cached.ids) ) { return cached; } } catch { // A missing or invalid availability cache is treated as a cache miss. } return undefined; } async function writeCachedAvailability(keyHash: string, ids: Set) { const temporaryPath = `${AVAILABILITY_CACHE_PATH}.tmp`; await mkdir(MODEL_CACHE_DIR, { recursive: true }); await writeFile( temporaryPath, JSON.stringify({ version: AVAILABILITY_CACHE_VERSION, keyHash, checkedAt: Date.now(), ids: [...ids], }), ); await rename(temporaryPath, AVAILABILITY_CACHE_PATH); } async function getModels(): Promise { const cached = await readCachedModels(); const cacheTtl = Number(process.env.DUCKCODING_MODEL_CACHE_TTL_MS ?? DEFAULT_MODEL_CACHE_TTL_MS); const ttl = Number.isFinite(cacheTtl) && cacheTtl >= 0 ? cacheTtl : DEFAULT_MODEL_CACHE_TTL_MS; const effectiveTtl = cached?.degraded ? Math.min(ttl, DEGRADED_CACHE_TTL_MS) : ttl; if (cached && Date.now() - cached.fetchedAt < effectiveTtl) { return { models: cached.models, vendorProviders: new Map(Object.entries(cached.vendorProviders)) }; } try { const pricing = await fetchPricing(); const { catalogs, degraded } = await fetchCatalogs(pricing); const set = createModels(pricing, catalogs); if (set.models.length === 0) throw new Error("pricing response contained no compatible models"); try { await writeCachedModels(set, degraded); } catch { // The freshly fetched list is still usable if the cache cannot be written. } return set; } catch (error) { if (cached) return { models: cached.models, vendorProviders: new Map(Object.entries(cached.vendorProviders)) }; throw new Error(`Failed to fetch DuckCoding pricing: ${error instanceof Error ? error.message : String(error)}`); } } type AvailabilityState = { ids: Set | undefined; keyHash: string | undefined; checkedAt: number | undefined; load: Promise | undefined; inFlight: Promise | undefined; }; function registerDuckCoding(pi: ExtensionAPI, models: ChatModelConfig[], availability: AvailabilityState) { const availableModels = () => { if (!availability.ids) return models; const filtered = models.filter((model) => availability.ids?.has(model.id)); // If the pricing catalog and the key's model list share nothing, keep the // full list rather than presenting an empty provider. return filtered.length > 0 ? filtered : models; }; pi.registerProvider("duckcoding", { name: "DuckCoding AI", baseUrl: "https://api.duckcoding.ai/v1", apiKey: "$DUCKCODING_API_KEY", api: "openai-completions", models: availableModels(), headers: {}, refreshModels: async (context) => { const apiKey = context.credential?.type === "api_key" ? context.credential.key : undefined; const keyHash = apiKey ? hashApiKey(apiKey) : undefined; if (!availability.load) { availability.load = (async () => { const cached = await readCachedAvailability(); if (cached) { availability.ids = new Set(cached.ids); availability.keyHash = cached.keyHash; availability.checkedAt = cached.checkedAt; } })(); } await availability.load; const cacheMatches = keyHash !== undefined && availability.keyHash === keyHash; if (!cacheMatches) { // Never apply a previous key's availability to a different credential. availability.ids = undefined; availability.checkedAt = undefined; } const stale = !availability.checkedAt || Date.now() - availability.checkedAt > AVAILABILITY_TTL_MS; // Pi passes allowNetwork=false during normal startup, so honoring that // flag here would leave dead models in the picker. This is a small, // credential-scoped check with its own TTL cache; explicit offline mode // still short-circuits it. const canRefresh = apiKey !== undefined && keyHash !== undefined && !context.signal.aborted && !process.env.PI_OFFLINE; if (stale && canRefresh) { if (!availability.inFlight) { availability.inFlight = (async () => { try { const ids = await fetchAvailableModelIds(apiKey!, context.signal); if (ids) { availability.ids = ids; availability.keyHash = keyHash!; availability.checkedAt = Date.now(); try { await writeCachedAvailability(keyHash!, ids); } catch { // Availability still applies for this process. } } } finally { availability.inFlight = undefined; } })(); } await availability.inFlight; } return availableModels(); }, }); } export default async function (pi: ExtensionAPI) { let { models, vendorProviders } = await getModels(); const availability: AvailabilityState = { ids: undefined, keyHash: undefined, checkedAt: undefined, load: undefined, inFlight: undefined, }; registerDuckCoding(pi, models, availability); // Pi's local registry includes the bundled catalog for built-in providers. // Prefer it at session start: it is offline-safe and repairs any model that // used family defaults because the public catalog was unreachable. pi.on("session_start", (_event, ctx) => { const catalogs = localCatalogIndex(ctx.modelRegistry.getAll()); if (catalogs.size === 0) return; const enriched = applyCatalogMetadata(models, vendorProviders, catalogs); if (enriched.some((model, index) => !sameModelMetadata(model, models[index]))) { models = enriched; registerDuckCoding(pi, models, availability); } }); }