Fetch Command Code models at startup

This commit is contained in:
Patrick Wozniak
2026-05-26 21:51:41 +02:00
parent 960d0d1f23
commit 418f4c925b
6 changed files with 114 additions and 921 deletions
+15 -89
View File
@@ -9,106 +9,32 @@
* 3. Place API key in `~/.commandcode/auth.json` or `~/.pi/agent/auth.json`
* as {"apiKey": "user_..."} or {"commandcode": "user_..."}
*
* Models are sourced from models.json, which is extracted from the command-code
* npm package dist file. Run `npx tsx scripts/extract-models.ts` to refresh.
* Models are fetched from Command Code's Provider API at startup.
*/
import { readFileSync } from "node:fs";
import { calculateCost, createAssistantMessageEventStream } from "@mariozechner/pi-ai"
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent"
import { calculateCost, createAssistantMessageEventStream } from "@mariozechner/pi-ai";
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { createStreamCommandCode, DEFAULT_API_BASE } from "./src/core.ts"
import { DEFAULT_MODELS_URL, fetchCommandCodeModels } from "./src/models.ts"
import { getApiKey, login, refreshToken } from "./src/oauth.ts"
import { createStreamCommandCode, DEFAULT_API_BASE } from "./src/core.ts";
import { getApiKey, login, refreshToken } from "./src/oauth.ts";
const API_BASE = process.env.COMMANDCODE_API_BASE ?? DEFAULT_API_BASE;
// ---------------------------------------------------------------------------
// Load model definitions from models.json
// ---------------------------------------------------------------------------
interface ModelsJson {
providers: Record<string, string>;
models: Array<{
key: string;
id: string;
provider: string;
spec: string;
label: string;
name: string;
description: string;
reasoning: boolean;
reasoningEfforts: string[] | null;
contextWindow: number;
maxOutputTokens: number;
vendorLabel: string | null;
}>;
pricing: Array<{
provider: string;
id: string;
category: string;
promptCost: number;
completionCost: number;
cacheWrite5mCost: number;
cacheWrite1hCost: number;
cacheHitCost: number;
}>;
}
const modelsJson: ModelsJson = JSON.parse(
readFileSync(new URL("./models.json", import.meta.url), "utf8"),
);
// ---------------------------------------------------------------------------
// Build cost lookup (model id -> pricing)
// ---------------------------------------------------------------------------
const costByModelId = new Map<string, ModelsJson["pricing"][number]>();
for (const p of modelsJson.pricing) {
// Pricing id is like "anthropic:claude-sonnet-4-6"
const colonIdx = p.id.indexOf(":");
if (colonIdx > 0) {
costByModelId.set(p.id.substring(colonIdx + 1), p);
}
costByModelId.set(p.id, p);
}
// ---------------------------------------------------------------------------
// Build pi model list (all defaults come from models.json)
// ---------------------------------------------------------------------------
const MODELS = modelsJson.models.map((m) => {
const cost = costByModelId.get(m.id);
return {
id: m.id,
name: `${m.name} (CC)`,
reasoning: m.reasoning,
contextWindow: m.contextWindow,
maxTokens: m.maxOutputTokens,
cost: {
input: cost?.promptCost ?? 0,
output: cost?.completionCost ?? 0,
cacheRead: cost?.cacheHitCost ?? 0,
cacheWrite: Math.max(cost?.cacheWrite5mCost ?? 0, cost?.cacheWrite1hCost ?? 0),
},
};
});
// ---------------------------------------------------------------------------
// Stream factory
// ---------------------------------------------------------------------------
const API_BASE = process.env.COMMANDCODE_API_BASE ?? DEFAULT_API_BASE
const MODELS_URL = process.env.COMMANDCODE_MODELS_URL ?? DEFAULT_MODELS_URL
const streamCommandCode = createStreamCommandCode({
createStream: createAssistantMessageEventStream,
calculateCost,
apiBase: API_BASE,
});
})
// ---------------------------------------------------------------------------
// Extension entry point
// ---------------------------------------------------------------------------
export default function (pi: ExtensionAPI) {
export default async function (pi: ExtensionAPI) {
const models = await fetchCommandCodeModels({ url: MODELS_URL })
pi.registerProvider("commandcode", {
name: "Command Code",
baseUrl: API_BASE,
@@ -126,14 +52,14 @@ export default function (pi: ExtensionAPI) {
refreshToken,
getApiKey,
},
models: MODELS.map((model) => ({
models: models.map((model) => ({
id: model.id,
name: model.name,
reasoning: model.reasoning,
input: ["text"] as const,
cost: model.cost,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: model.contextWindow,
maxTokens: model.maxTokens,
})),
});
})
}