浏览代码

feat: fetch model list from api and cache them with a TTL

Signed-off-by: greatbridf <[email protected]>
greatbridf 4 月之前
父节点
当前提交
62fa75959d
共有 1 个文件被更改,包括 131 次插入 和 75 次删除
  1. 131 75
      index.ts

+ 131 - 75
index.ts

@@ -8,90 +8,146 @@
  *   Set the DUCKCODING_API_KEY environment variable with your API key.
  *   Then use /model to select duckcoding/<model-id>.
  *
- * Models:
- *   The model list below uses common model IDs. Update it to match
- *   the models actually available at DuckCoding AI.
+ * Models are fetched from DuckCoding's pricing endpoint and cached locally.
+ * The cache is refreshed after seven days by default. Set
+ * DUCKCODING_MODEL_CACHE_TTL_MS to override the cache lifetime.
  */
 
+import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
+import { homedir } from "node:os";
+import { join } from "node:path";
 import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
 
-export default function (pi: ExtensionAPI) {
+type DuckCodingPricingResponse = {
+	success?: boolean;
+	data?: Array<{
+		model_name?: string;
+		vendor_id?: number;
+		supported_endpoint_types?: string[];
+		enable_groups?: string[];
+		model_price?: number;
+		model_ratio?: number;
+		completion_ratio?: number;
+		cache_ratio?: number;
+		create_cache_ratio?: number;
+	}>;
+};
+
+const PRICING_URL = "https://www.duckcoding.ai/api/pricing";
+const DEFAULT_MODEL_CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
+const MODEL_CACHE_PATH = join(homedir(), ".cache", "pi-agent-duckcoding", "models.json");
+
+type CachedModels = {
+	fetchedAt: number;
+	models: ReturnType<typeof createModels>;
+};
+
+function titleCaseModelName(modelName: string) {
+	return modelName
+		.split(/[-_/]+/)
+		.map((part) => (part ? part[0].toUpperCase() + part.slice(1) : part))
+		.join(" ");
+}
+
+function inferReasoning(modelName: string) {
+	return /reasoner|thinking|o[34]|claude-sonnet|codex/i.test(modelName);
+}
+
+function inferInputModes(endpoints: string[] = []) {
+	const input = ["text"];
+	if (endpoints.some((endpoint) => ["openai", "anthropic"].includes(endpoint))) {
+		input.push("image");
+	}
+	return input;
+}
+
+function inferContextWindow(modelName: string) {
+	if (/gpt-5\./i.test(modelName) || /claude/i.test(modelName)) return 200000;
+	if (/gpt-4o|o4|codex|deepseek/i.test(modelName)) return 128000;
+	return 64000;
+}
+
+function inferMaxTokens(modelName: string) {
+	if (/gpt-5\./i.test(modelName) || /claude/i.test(modelName)) return 8192;
+	return 16384;
+}
+
+function inferCost(model: { model_price?: number; model_ratio?: number; completion_ratio?: number; cache_ratio?: number; create_cache_ratio?: number }) {
+	const input = model.model_price ?? model.model_ratio ?? 0;
+	const output = model.completion_ratio ?? 0;
+	const cacheRead = model.cache_ratio ?? 0;
+	const cacheWrite = model.create_cache_ratio ?? 0;
+	return { input, output, cacheRead, cacheWrite };
+}
+
+function createModels(pricing: DuckCodingPricingResponse) {
+	return (pricing.data ?? [])
+		.filter((model) => model.model_name)
+		.filter((model) => (model.supported_endpoint_types ?? []).some((endpoint) => endpoint === "openai" || endpoint === "anthropic"))
+		.map((model) => ({
+			id: model.model_name!,
+			name: titleCaseModelName(model.model_name!),
+			reasoning: inferReasoning(model.model_name!),
+			input: inferInputModes(model.supported_endpoint_types),
+			cost: inferCost(model),
+			contextWindow: inferContextWindow(model.model_name!),
+			maxTokens: inferMaxTokens(model.model_name!),
+		}))
+		.sort((a, b) => a.id.localeCompare(b.id));
+}
+
+async function readCachedModels() {
+	try {
+		const cached = JSON.parse(await readFile(MODEL_CACHE_PATH, "utf8")) as CachedModels;
+		if (Array.isArray(cached.models) && typeof cached.fetchedAt === "number") return cached;
+	} catch {
+		// A missing or invalid cache is treated as a cache miss.
+	}
+	return undefined;
+}
+
+async function writeCachedModels(models: CachedModels["models"]) {
+	const directory = join(homedir(), ".cache", "pi-agent-duckcoding");
+	const temporaryPath = `${MODEL_CACHE_PATH}.tmp`;
+	await mkdir(directory, { recursive: true });
+	await writeFile(temporaryPath, JSON.stringify({ fetchedAt: Date.now(), models }, null, 2));
+	await rename(temporaryPath, MODEL_CACHE_PATH);
+}
+
+async function getModels() {
+	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;
+	if (cached && Date.now() - cached.fetchedAt < ttl) return cached.models;
+
+	try {
+		const response = await fetch(PRICING_URL);
+		if (!response.ok) {
+			throw new Error(`${response.status} ${response.statusText}`);
+		}
+		const models = createModels((await response.json()) as DuckCodingPricingResponse);
+		if (models.length === 0) throw new Error("pricing response contained no compatible models");
+		try {
+			await writeCachedModels(models);
+		} catch {
+			// The freshly fetched list is still usable if the cache cannot be written.
+		}
+		return models;
+	} catch (error) {
+		if (cached) return cached.models;
+		throw new Error(`Failed to fetch DuckCoding pricing: ${error instanceof Error ? error.message : String(error)}`);
+	}
+}
+
+export default async function (pi: ExtensionAPI) {
+	const models = await getModels();
+
 	pi.registerProvider("duckcoding", {
 		name: "DuckCoding AI",
 		baseUrl: "https://api.duckcoding.ai/v1",
 		apiKey: "$DUCKCODING_API_KEY",
 		api: "openai-completions",
-
-		models: [
-			{
-				id: "claude-sonnet-4-20250514",
-				name: "Claude Sonnet 4",
-				reasoning: true,
-				input: ["text", "image"],
-				cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
-				contextWindow: 200000,
-				maxTokens: 8192,
-			},
-			{
-				id: "claude-3-5-sonnet-20241022",
-				name: "Claude 3.5 Sonnet",
-				reasoning: false,
-				input: ["text", "image"],
-				cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
-				contextWindow: 200000,
-				maxTokens: 8192,
-			},
-			{
-				id: "claude-3-5-haiku-20241022",
-				name: "Claude 3.5 Haiku",
-				reasoning: false,
-				input: ["text", "image"],
-				cost: { input: 0.8, output: 4, cacheRead: 0.08, cacheWrite: 1 },
-				contextWindow: 200000,
-				maxTokens: 8192,
-			},
-			{
-				id: "gpt-5.4-mini",
-				name: "GPT-5.4-Mini",
-				reasoning: false,
-				input: ["text", "image"],
-				cost: { input: 2.5, output: 10, cacheRead: 1.25, cacheWrite: 0 },
-				contextWindow: 128000,
-				maxTokens: 16384,
-			},
-			{
-				id: "gpt-4o-mini",
-				name: "GPT-4o Mini",
-				reasoning: false,
-				input: ["text", "image"],
-				cost: { input: 0.15, output: 0.6, cacheRead: 0.075, cacheWrite: 0 },
-				contextWindow: 128000,
-				maxTokens: 16384,
-			},
-			{
-				id: "deepseek-chat",
-				name: "DeepSeek V3",
-				reasoning: false,
-				input: ["text"],
-				cost: { input: 0.27, output: 1.1, cacheRead: 0.07, cacheWrite: 0.27 },
-				contextWindow: 64000,
-				maxTokens: 8192,
-			},
-			{
-				id: "deepseek-reasoner",
-				name: "DeepSeek R1",
-				reasoning: true,
-				input: ["text"],
-				compat: {
-					thinkingFormat: "deepseek",
-				},
-				cost: { input: 0.55, output: 2.19, cacheRead: 0.14, cacheWrite: 0.55 },
-				contextWindow: 64000,
-				maxTokens: 8192,
-			},
-		],
-
-		// Custom headers if needed by DuckCoding
+		models,
 		headers: {},
 	});
 }