feat: extract models from command-code dist, load via models.json

- Add scripts/extract-models.ts to parse command-code npm dist file
- Generate models.json (21 models, 15 pricing entries) with contextWindow
  and maxOutputTokens pre-filled; no nulls or hardcoded fallbacks in index.ts
- Rewrite index.ts to load model list and costs from models.json
- Cap gateway model maxOutputTokens at 65536 (API limit for Baseten/Vercel)
- Add 'Update models' section to README documenting the generation flow
- Add npm run extract-models script
This commit is contained in:
Tao Yang
2026-05-25 16:37:08 +08:00
parent b5b109c63c
commit 960d0d1f23
5 changed files with 907 additions and 143 deletions
+84 -142
View File
@@ -9,158 +9,100 @@
* 3. Place API key in `~/.commandcode/auth.json` or `~/.pi/agent/auth.json`
* as {"apiKey": "user_..."} or {"commandcode": "user_..."}
*
* Models: deepseek-v4-pro, deepseek-v4-flash, claude-sonnet-4-6, claude-opus-4-7, etc.
* 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.
*/
import { calculateCost, createAssistantMessageEventStream } from "@mariozechner/pi-ai"
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent"
import { readFileSync } from "node:fs";
import { createStreamCommandCode, DEFAULT_API_BASE } from "./src/core.ts"
import { getApiKey, login, refreshToken } from "./src/oauth.ts"
import { calculateCost, createAssistantMessageEventStream } from "@mariozechner/pi-ai";
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
const API_BASE = process.env.COMMANDCODE_API_BASE ?? DEFAULT_API_BASE
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;
// ---------------------------------------------------------------------------
// Model definitions
// Load model definitions from models.json
// ---------------------------------------------------------------------------
const MODELS = [
// Premium (Anthropic)
{
id: "claude-opus-4-7",
name: "Claude Opus 4.7 (CC)",
reasoning: true,
contextWindow: 200_000,
maxTokens: 32_000,
},
{
id: "claude-opus-4-6",
name: "Claude Opus 4.6 (CC)",
reasoning: true,
contextWindow: 200_000,
maxTokens: 32_000,
},
{
id: "claude-sonnet-4-6",
name: "Claude Sonnet 4.6 (CC)",
reasoning: true,
contextWindow: 200_000,
maxTokens: 16_384,
},
{
id: "claude-haiku-4-5-20251001",
name: "Claude Haiku 4.5 (CC)",
reasoning: true,
contextWindow: 200_000,
maxTokens: 8_192,
},
// Premium (OpenAI)
{
id: "gpt-5.5",
name: "GPT-5.5 (CC)",
reasoning: true,
contextWindow: 256_000,
maxTokens: 128_000,
},
{
id: "gpt-5.4",
name: "GPT-5.4 (CC)",
reasoning: true,
contextWindow: 256_000,
maxTokens: 128_000,
},
{
id: "gpt-5.3-codex",
name: "GPT-5.3 Codex (CC)",
reasoning: true,
contextWindow: 256_000,
maxTokens: 128_000,
},
{
id: "gpt-5.4-mini",
name: "GPT-5.4 Mini (CC)",
reasoning: false,
contextWindow: 256_000,
maxTokens: 128_000,
},
// Open-source
{
id: "deepseek/deepseek-v4-pro",
name: "DeepSeek V4 Pro (CC)",
reasoning: true,
contextWindow: 1_000_000,
maxTokens: 384_000,
},
{
id: "deepseek/deepseek-v4-flash",
name: "DeepSeek V4 Flash (CC)",
reasoning: true,
contextWindow: 1_000_000,
maxTokens: 384_000,
},
{
id: "moonshotai/Kimi-K2.6",
name: "Kimi K2.6 (CC)",
reasoning: true,
contextWindow: 262_144,
maxTokens: 131_072,
},
{
id: "moonshotai/Kimi-K2.5",
name: "Kimi K2.5 (CC)",
reasoning: true,
contextWindow: 262_144,
maxTokens: 131_072,
},
{
id: "zai-org/GLM-5.1",
name: "GLM-5.1 (CC)",
reasoning: true,
contextWindow: 200_000,
maxTokens: 131_072,
},
{
id: "zai-org/GLM-5",
name: "GLM-5 (CC)",
reasoning: true,
contextWindow: 200_000,
maxTokens: 131_072,
},
{
id: "MiniMaxAI/MiniMax-M2.7",
name: "MiniMax M2.7 (CC)",
reasoning: true,
contextWindow: 1_048_576,
maxTokens: 131_072,
},
{
id: "MiniMaxAI/MiniMax-M2.5",
name: "MiniMax M2.5 (CC)",
reasoning: true,
contextWindow: 1_048_576,
maxTokens: 131_072,
},
{
id: "Qwen/Qwen3.6-Max-Preview",
name: "Qwen 3.6 Max (CC)",
reasoning: true,
contextWindow: 1_000_000,
maxTokens: 131_072,
},
{
id: "Qwen/Qwen3.6-Plus",
name: "Qwen 3.6 Plus (CC)",
reasoning: true,
contextWindow: 1_000_000,
maxTokens: 131_072,
},
]
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 streamCommandCode = createStreamCommandCode({
createStream: createAssistantMessageEventStream,
calculateCost,
apiBase: API_BASE,
})
});
// ---------------------------------------------------------------------------
// Extension entry point
@@ -188,10 +130,10 @@ export default function (pi: ExtensionAPI) {
id: model.id,
name: model.name,
reasoning: model.reasoning,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
input: ["text"] as const,
cost: model.cost,
contextWindow: model.contextWindow,
maxTokens: model.maxTokens,
})),
})
});
}