/** * Benchmark Extension - Production-grade model performance testing * * Tests AI models across 6 categories: * 1. Code Generation — Write a function from a spec * 2. Debugging — Find and fix a bug * 3. Planning — Create an implementation plan * 4. Tool Calling — Generate structured JSON output * 5. Speed — Response latency measurement * 6. Orchestration — Multi-step reasoning task * * Usage: * - /benchmark — Run benchmark on current model * - /benchmark all — Run on all available models * - /benchmark results — Show saved results * - /benchmark compare — Side-by-side model comparison * - /benchmark clear — Clear all results */ import { access, mkdir, readFile, writeFile } from "node:fs/promises"; import { homedir } from "node:os"; import { join } from "node:path"; import { type ExtensionAPI, type ExtensionContext, getApiKeyStore } from "phi-code"; import type { Api, AssistantMessage, Model } from "phi-code-ai"; import { completeSimple } from "phi-code-ai/compat"; import { catalogEnvKey, getProviderCatalog } from "./providers/catalog.ts"; // ─── Types ─────────────────────────────────────────────────────────────── interface TestCase { category: "code-gen" | "debug" | "planning" | "tool-calling" | "speed" | "orchestration"; name: string; prompt: string; validate: (response: string) => TestResult; weight: number; // Score weight (1-3) } interface TestResult { passed: boolean; score: number; // 0-100 details: string; } interface ModelBenchmark { modelId: string; modelName: string; provider: string; timestamp: string; categories: { [key: string]: { score: number; timeMs: number; details: string; }; }; totalScore: number; totalTimeMs: number; avgTimeMs: number; } interface BenchmarkStore { version: 2; results: ModelBenchmark[]; lastUpdated: string; } // ─── Test Suite ────────────────────────────────────────────────────────── function createTestSuite(): TestCase[] { return [ // 1. CODE GENERATION { category: "code-gen", name: "Fibonacci Function", weight: 2, prompt: `Write a TypeScript function called 'fibonacci' that: - Takes a number n as parameter - Returns the nth Fibonacci number - Handles edge cases (n <= 0 returns 0, n = 1 returns 1) - Uses iterative approach (not recursive) - Is properly typed Respond with ONLY the function code, no explanations.`, validate: (response: string) => { const code = extractCode(response); const checks = [ { test: /function\s+fibonacci/.test(code), detail: "Function named 'fibonacci'" }, { test: /:\s*number/.test(code), detail: "TypeScript type annotation" }, { test: /return/.test(code), detail: "Has return statement" }, { test: /for|while/.test(code), detail: "Uses iteration (not recursion)" }, { test: /(<=\s*0|===?\s*0|<\s*1)/.test(code), detail: "Handles edge case n=0" }, { test: /(===?\s*1|<=\s*1)/.test(code), detail: "Handles edge case n=1" }, ]; const passed = checks.filter((c) => c.test).length; const total = checks.length; return { passed: passed >= 5, score: Math.round((passed / total) * 100), details: checks.map((c) => `${c.test ? "✅" : "❌"} ${c.detail}`).join("\n"), }; }, }, // 2. DEBUGGING { category: "debug", name: "Find the Bug", weight: 2, prompt: `Find and fix the bug in this TypeScript code: \`\`\`typescript function mergeArrays(arr1: T[], arr2: T[]): T[] { const result = arr1; for (let i = 0; i < arr2.length; i++) { result.push(arr2[i]); } return result; } // Bug: calling mergeArrays modifies the original arr1 const a = [1, 2, 3]; const b = [4, 5, 6]; const merged = mergeArrays(a, b); console.log(a); // Expected [1,2,3] but got [1,2,3,4,5,6] \`\`\` Explain the bug and provide the fixed code.`, validate: (response: string) => { const lower = response.toLowerCase(); const checks = [ { test: /reference|shallow|copy|spread|\[\.\.\./.test(lower), detail: "Identifies reference/copy issue", }, { test: /\[\.\.\.arr1\]|\[\.\.\.arr1,|Array\.from|\.slice\(\)|structuredClone|concat/.test(response), detail: "Uses spread/copy/concat fix", }, { test: /mutate|modify|original|side.?effect/.test(lower), detail: "Explains the mutation problem" }, { test: /const result\s*=\s*\[/.test(response) || /\.slice\(/.test(response) || /\.concat\(/.test(response) || /Array\.from/.test(response), detail: "Creates new array in fix", }, ]; const passed = checks.filter((c) => c.test).length; return { passed: passed >= 3, score: Math.round((passed / checks.length) * 100), details: checks.map((c) => `${c.test ? "✅" : "❌"} ${c.detail}`).join("\n"), }; }, }, // 3. PLANNING { category: "planning", name: "Implementation Plan", weight: 2, prompt: `Create a detailed implementation plan for adding JWT authentication to an existing Express.js REST API. The API currently has: - User model with email/password - CRUD endpoints for /users and /posts - PostgreSQL database with Prisma ORM Requirements: - Login endpoint returns access + refresh tokens - Protected routes require valid access token - Refresh token rotation - Token blacklisting on logout Provide a structured plan with specific files to create/modify, dependencies to add, and implementation steps.`, validate: (response: string) => { const lower = response.toLowerCase(); const checks = [ { test: /jsonwebtoken|jwt|jose/.test(lower), detail: "Mentions JWT library" }, { test: /access.?token|refresh.?token/.test(lower), detail: "Covers both token types" }, { test: /middleware/.test(lower), detail: "Mentions auth middleware" }, { test: /bcrypt|argon|hash/.test(lower), detail: "Addresses password hashing" }, { test: /blacklist|revoke|invalidat/.test(lower), detail: "Addresses token revocation" }, { test: /prisma|schema|model|migration/.test(lower), detail: "Covers database changes" }, { test: /env|secret|config/.test(lower), detail: "Addresses secret management" }, { test: /step|phase|\d\.|create|modify|add/.test(lower), detail: "Provides structured steps" }, ]; const passed = checks.filter((c) => c.test).length; return { passed: passed >= 6, score: Math.round((passed / checks.length) * 100), details: checks.map((c) => `${c.test ? "✅" : "❌"} ${c.detail}`).join("\n"), }; }, }, // 4. TOOL CALLING (structured output) { category: "tool-calling", name: "Structured JSON Output", weight: 1, prompt: `Parse this natural language description and output ONLY a valid JSON object (no markdown, no explanation): "Create a new user named Alice Smith, email alice@example.com, she's a software engineer at TechCorp, based in San Francisco, age 28, prefers dark mode and email notifications" Required JSON schema: { "name": { "first": string, "last": string }, "email": string, "profile": { "occupation": string, "company": string, "location": string, "age": number }, "preferences": { "theme": "light" | "dark", "notifications": { "email": boolean, "push": boolean } } } Output ONLY the JSON.`, validate: (response: string) => { const checks: Array<{ test: boolean; detail: string }> = []; // Try to extract JSON from response let jsonStr = response.trim(); const jsonMatch = response.match(/```(?:json)?\s*([\s\S]*?)```/) || response.match(/(\{[\s\S]*\})/); if (jsonMatch) jsonStr = jsonMatch[1].trim(); try { const obj = JSON.parse(jsonStr); checks.push({ test: true, detail: "Valid JSON" }); checks.push({ test: obj?.name?.first === "Alice", detail: 'name.first = "Alice"' }); checks.push({ test: obj?.name?.last === "Smith", detail: 'name.last = "Smith"' }); checks.push({ test: obj?.email === "alice@example.com", detail: "Correct email" }); checks.push({ test: typeof obj?.profile?.age === "number" && obj.profile.age === 28, detail: "Age is number 28", }); checks.push({ test: obj?.preferences?.theme === "dark", detail: 'theme = "dark"' }); checks.push({ test: obj?.preferences?.notifications?.email === true, detail: "email notifications = true", }); } catch { checks.push({ test: false, detail: "Valid JSON (parse failed)" }); checks.push({ test: false, detail: "name.first" }); checks.push({ test: false, detail: "name.last" }); checks.push({ test: false, detail: "email" }); checks.push({ test: false, detail: "age" }); checks.push({ test: false, detail: "theme" }); checks.push({ test: false, detail: "notifications" }); } const passed = checks.filter((c) => c.test).length; return { passed: passed >= 5, score: Math.round((passed / checks.length) * 100), details: checks.map((c) => `${c.test ? "✅" : "❌"} ${c.detail}`).join("\n"), }; }, }, // 5. SPEED (simple task, measures latency) { category: "speed", name: "Quick Response", weight: 1, prompt: `Reply with exactly this text and nothing else: "Hello, World!"`, validate: (response: string) => { const trimmed = response .trim() .replace(/^["']|["']$/g, "") .replace(/```\w*\n?/g, "") .trim(); const exact = trimmed === "Hello, World!"; const close = trimmed.toLowerCase().includes("hello, world"); return { passed: close, score: exact ? 100 : close ? 75 : 0, details: exact ? "✅ Exact match" : close ? "⚠️ Close match" : `❌ Got: "${trimmed.substring(0, 50)}"`, }; }, }, // 6. ORCHESTRATION (multi-step reasoning) { category: "orchestration", name: "Multi-Step Analysis", weight: 2, prompt: `Analyze this scenario step by step: A Node.js microservice has these symptoms: 1. Response times gradually increase from 50ms to 3000ms over 24 hours 2. Memory usage grows steadily from 200MB to 1.5GB 3. The service handles file uploads (multipart/form-data) 4. After restart, everything returns to normal 5. No errors in logs 6. Database queries remain fast (<10ms) Tasks: A) Identify the most likely root cause B) List 3 specific things to check in the code C) Propose a fix with code example D) Suggest monitoring to prevent recurrence Be specific and technical.`, validate: (response: string) => { const lower = response.toLowerCase(); const checks = [ { test: /memory.?leak|leak/.test(lower), detail: "Identifies memory leak" }, { test: /stream|buffer|file|upload|temp|cleanup/.test(lower), detail: "Links to file upload handling" }, { test: /close|destroy|cleanup|dispose|gc|garbage/.test(lower), detail: "Suggests resource cleanup" }, { test: /event.?listener|handler|remove|off/.test(lower) || /stream|pipe/.test(lower), detail: "Checks for handler/stream leaks", }, { test: /heapdump|heap.?snapshot|inspect|profile|--max-old-space/.test(lower) || /process\.memoryUsage/.test(lower), detail: "Suggests debugging tools", }, { test: /monitor|alert|metric|prometheus|grafana|threshold/.test(lower), detail: "Suggests monitoring" }, ]; const passed = checks.filter((c) => c.test).length; return { passed: passed >= 4, score: Math.round((passed / checks.length) * 100), details: checks.map((c) => `${c.test ? "✅" : "❌"} ${c.detail}`).join("\n"), }; }, }, ]; } // ─── Helpers ───────────────────────────────────────────────────────────── function extractCode(response: string): string { const match = response.match(/```(?:typescript|ts|javascript|js)?\s*([\s\S]*?)```/); return match ? match[1].trim() : response.trim(); } // Provider list comes from the shared catalog (providers/catalog.ts) — the // same source /setup and /phi-init use. Cloud entries expose `benchModels` // (the subset /benchmark exercises); local servers are discovered separately. /** A benchmarkable model: where it lives and which wire API it speaks. */ export interface BenchTarget { id: string; provider: string; baseUrl: string; apiKey: string; /** Wire API (e.g. "openai-completions", "anthropic-messages"). */ api: string; } /** Minimal registry surface /benchmark needs (ExtensionContext.modelRegistry). */ export type BenchRegistry = Pick; const OPENAI_COMPLETIONS_API = "openai-completions"; /** * Whether /benchmark can actually call this target: through the model * registry (any API: Anthropic, Google, OAuth, compat...) when the model is * registered, otherwise only with the raw OpenAI chat/completions fallback. */ export function isBenchmarkable(target: BenchTarget, registry: BenchRegistry | undefined): boolean { return Boolean(registry?.find(target.provider, target.id)) || target.api === OPENAI_COMPLETIONS_API; } async function getAvailableModels(registry?: BenchRegistry): Promise { const models: BenchTarget[] = []; // 1. Cloud providers via env vars, falling back to keys stored in // models.json via /setup or /keys (the store resolves env-var names and // "!cmd" values the same way the model registry does at request time). const store = getApiKeyStore(); for (const provider of getProviderCatalog()) { if (provider.local) continue; const benchModels = provider.benchModels ?? []; let apiKey = catalogEnvKey(provider)?.value; if (!apiKey) { try { apiKey = store.getKey(provider.id); } catch { /* unreadable models.json — env-only behavior */ } } if (!apiKey) continue; for (const modelId of benchModels) { models.push({ id: modelId, provider: provider.id, baseUrl: provider.baseUrl, apiKey, api: provider.api, }); } } // 2. Local providers (LM Studio, Ollama) — auto-detect via models.json, // read through ApiKeyStore so // comments and trailing commas are // tolerated and PHI_AGENT_DIR overrides are honored. try { for (const id of store.listProviders()) { const providerConfig = store.getProvider(id); if (!providerConfig) continue; const baseUrl = providerConfig.baseUrl || ""; const apiKey = store.getKey(id) || "local"; if (!providerConfig.models || providerConfig.models.length === 0) continue; for (const m of providerConfig.models) { const modelId = typeof m === "string" ? m : (m as { id?: string }).id; if (!modelId) continue; // Per-model api (e.g. OpenCode Go qwen/minimax) wins over the provider's. const api = (typeof m === "object" && m !== null ? (m as { api?: string }).api : undefined) ?? providerConfig.api ?? OPENAI_COMPLETIONS_API; // Skip if already added from env vars if (!models.some((existing) => existing.id === modelId && existing.baseUrl === baseUrl)) { models.push({ id: modelId, provider: id, baseUrl, apiKey, api }); } } } } catch { /* ignore unreadable models.json */ } // 3. Try to detect LM Studio (port 1234) and Ollama (port 11434) directly for (const local of [ { name: "lm-studio", port: 1234, baseUrl: "http://localhost:1234/v1" }, { name: "ollama", port: 11434, baseUrl: "http://localhost:11434/v1" }, ]) { // Skip if already discovered via models.json if (models.some((m) => m.baseUrl === local.baseUrl)) continue; try { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 2000); const resp = await fetch(`${local.baseUrl}/models`, { signal: controller.signal }); clearTimeout(timeout); if (resp.ok) { const data = (await resp.json()) as any; const modelList = data?.data || []; for (const m of modelList) { const modelId = m.id || m.name; if (modelId && !models.some((existing) => existing.id === modelId)) { models.push({ id: modelId, provider: local.name, baseUrl: local.baseUrl, apiKey: "local", api: OPENAI_COMPLETIONS_API, }); } } } } catch { /* not running */ } } // Drop what cannot be called correctly (e.g. an Anthropic-API model that is // not in the registry would only get a guaranteed-failing chat/completions). return models.filter((m) => isBenchmarkable(m, registry)); } /** Text of an assistant reply (text parts only; thinking is not graded). */ function assistantText(message: AssistantMessage): string { return message.content .filter((part): part is Extract => part.type === "text") .map((part) => part.text) .join(""); } /** * Call a REGISTERED model through its own API (Anthropic, Google, OpenAI * responses, OAuth providers, compat quirks...) via the registry's auth, the * same path /btw uses. */ async function callRegisteredModel( registry: BenchRegistry, model: Model, prompt: string, timeoutMs: number, ): Promise<{ response: string; timeMs: number }> { const auth = await registry.getApiKeyAndHeaders(model); if (!auth.ok) throw new Error(auth.error); const startTime = Date.now(); const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), timeoutMs); try { const reply = await completeSimple( model, { messages: [{ role: "user", content: [{ type: "text", text: prompt }], timestamp: Date.now() }] }, { apiKey: auth.apiKey, headers: auth.headers, signal: controller.signal, maxTokens: 4096, temperature: 0.1 }, ); if (reply.stopReason === "error" || reply.stopReason === "aborted") { throw new Error(reply.errorMessage || `request ${reply.stopReason}`); } return { response: assistantText(reply), timeMs: Date.now() - startTime }; } finally { clearTimeout(timeout); } } /** * Route a benchmark prompt: through the model's own API when the registry * knows it, else the raw OpenAI chat/completions call, which is only correct * for "openai-completions" endpoints (getAvailableModels filters the rest). */ async function callBenchTarget( target: BenchTarget, prompt: string, registry: BenchRegistry | undefined, timeoutMs: number, ): Promise<{ response: string; timeMs: number }> { const registered = registry?.find(target.provider, target.id); if (registry && registered) return callRegisteredModel(registry, registered, prompt, timeoutMs); if (target.api !== OPENAI_COMPLETIONS_API) { throw new Error( `${target.provider}/${target.id} speaks ${target.api} and is not in the model registry: cannot benchmark it with a chat/completions call`, ); } return callModel(target.baseUrl, target.apiKey, target.id, prompt, timeoutMs); } async function callModel( baseUrl: string, apiKey: string, model: string, prompt: string, timeoutMs: number = 60000, ): Promise<{ response: string; timeMs: number }> { const startTime = Date.now(); const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), timeoutMs); try { const res = await fetch(`${baseUrl}/chat/completions`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiKey}`, }, body: JSON.stringify({ model, messages: [{ role: "user", content: prompt }], max_tokens: 4096, temperature: 0.1, }), signal: controller.signal, }); if (!res.ok) { const errorBody = await res.text().catch(() => ""); throw new Error(`API error ${res.status}: ${errorBody.substring(0, 200)}`); } const data = (await res.json()) as any; const response = data?.choices?.[0]?.message?.content || ""; const timeMs = Date.now() - startTime; return { response, timeMs }; } finally { clearTimeout(timeout); } } // ─── Extension ─────────────────────────────────────────────────────────── export default function benchmarkExtension(pi: ExtensionAPI) { const benchmarkDir = join(homedir(), ".phi", "benchmark"); const resultsPath = join(benchmarkDir, "results.json"); async function ensureDir() { await mkdir(benchmarkDir, { recursive: true }); } async function loadStore(): Promise { try { await access(resultsPath); const content = await readFile(resultsPath, "utf-8"); const store = JSON.parse(content); if (store.version === 2) return store; return { version: 2, results: [], lastUpdated: new Date().toISOString() }; } catch { return { version: 2, results: [], lastUpdated: new Date().toISOString() }; } } async function saveStore(store: BenchmarkStore) { await ensureDir(); store.lastUpdated = new Date().toISOString(); await writeFile(resultsPath, JSON.stringify(store, null, 2), "utf-8"); } /** * Run full benchmark on a single model */ async function benchmarkModel(target: BenchTarget, ctx: ExtensionContext): Promise { const { id: modelId, provider } = target; const tests = createTestSuite(); const categories: ModelBenchmark["categories"] = {}; let totalTime = 0; for (let testIdx = 0; testIdx < tests.length; testIdx++) { const test = tests[testIdx]; // Rate limiting: 1.5s between API calls to avoid throttling if (testIdx > 0) await new Promise((r) => setTimeout(r, 1500)); ctx.ui.notify(` ⏳ ${test.category}: ${test.name}...`, "info"); try { const { response, timeMs } = await callBenchTarget(target, test.prompt, ctx.modelRegistry, 90000); totalTime += timeMs; const result = test.validate(response); categories[test.category] = { score: result.score, timeMs, details: result.details, }; const emoji = result.score >= 80 ? "✅" : result.score >= 50 ? "⚠️" : "❌"; ctx.ui.notify(` ${emoji} ${test.category}: ${result.score}/100 (${timeMs}ms)`, "info"); } catch (error) { totalTime += 60000; categories[test.category] = { score: 0, timeMs: 60000, details: `Error: ${error}`, }; ctx.ui.notify(` ❌ ${test.category}: Error — ${String(error).substring(0, 100)}`, "error"); } } // Calculate weighted total const weights: Record = {}; for (const test of tests) { weights[test.category] = test.weight; } let weightedSum = 0; let totalWeight = 0; for (const [cat, data] of Object.entries(categories)) { const w = weights[cat] || 1; weightedSum += data.score * w; totalWeight += w; } const totalScore = Math.round(weightedSum / totalWeight); return { modelId, modelName: modelId, provider, timestamp: new Date().toISOString(), categories, totalScore, totalTimeMs: totalTime, avgTimeMs: Math.round(totalTime / tests.length), }; } /** * Generate formatted comparison report */ function generateReport(results: ModelBenchmark[]): string { if (results.length === 0) return "No benchmark results yet. Run `/benchmark` to start."; // Sort by totalScore desc const sorted = [...results].sort((a, b) => b.totalScore - a.totalScore); const categories = ["code-gen", "debug", "planning", "tool-calling", "speed", "orchestration"]; let report = "🏆 **Phi Code Benchmark Results**\n\n"; // Leaderboard report += "**Leaderboard:**\n"; sorted.forEach((r, i) => { const medal = i === 0 ? "🥇" : i === 1 ? "🥈" : i === 2 ? "🥉" : `${i + 1}.`; const tier = r.totalScore >= 80 ? "S" : r.totalScore >= 65 ? "A" : r.totalScore >= 50 ? "B" : r.totalScore >= 35 ? "C" : "D"; report += `${medal} **${r.modelId}** — ${r.totalScore}/100 [${tier}] (avg ${r.avgTimeMs}ms)\n`; }); // Category breakdown report += "\n**Category Breakdown:**\n```\n"; const header = `${"Model".padEnd(25) + categories.map((c) => c.substring(0, 8).padEnd(10)).join("")}TOTAL\n`; report += header; report += `${"-".repeat(header.length)}\n`; for (const r of sorted) { let line = r.modelId.substring(0, 24).padEnd(25); for (const cat of categories) { const score = r.categories[cat]?.score ?? "-"; line += String(score).padEnd(10); } line += String(r.totalScore); report += `${line}\n`; } report += "```\n"; // Best model per category report += "\n**Best per Category:**\n"; for (const cat of categories) { let best = { model: "none", score: -1 }; for (const r of sorted) { const s = r.categories[cat]?.score ?? 0; if (s > best.score) { best = { model: r.modelId, score: s }; } } report += `- ${cat}: **${best.model}** (${best.score}/100)\n`; } report += `\n_Last updated: ${sorted[0]?.timestamp ?? "N/A"}_`; return report; } // ─── Command ───────────────────────────────────────────────────────── pi.registerCommand("benchmark", { description: "Run AI model benchmarks (6 categories: code-gen, debug, planning, tool-calling, speed, orchestration)", handler: async (args, ctx) => { const arg = args.trim().toLowerCase(); // Show results if (arg === "results" || arg === "report") { const store = await loadStore(); ctx.ui.notify(generateReport(store.results), "info"); return; } // Compare (same as results but emphasized) if (arg === "compare") { const store = await loadStore(); if (store.results.length < 2) { ctx.ui.notify("Need at least 2 model results to compare. Run `/benchmark all` first.", "info"); return; } ctx.ui.notify(generateReport(store.results), "info"); return; } // Clear if (arg === "clear") { await saveStore({ version: 2, results: [], lastUpdated: new Date().toISOString() }); ctx.ui.notify("🗑️ All benchmark results cleared.", "info"); return; } // Help if (arg === "help" || arg === "?") { ctx.ui.notify( `**Phi Code Benchmark** — 6 categories, real API calls Commands: /benchmark Run on current model /benchmark all Run on ALL available models /benchmark Run on a specific model /benchmark results Show saved results /benchmark compare Side-by-side comparison /benchmark clear Clear all results Categories tested (weighted): ⚡ code-gen (×2) — Generate a TypeScript function 🐛 debug (×2) — Find and fix a bug 📋 planning (×2) — Create implementation plan 🔧 tool-calling (×1) — Structured JSON output ⏱️ speed (×1) — Response latency 🧩 orchestration (×2) — Multi-step analysis Scoring: S (80+), A (65+), B (50+), C (35+), D (<35)`, "info", ); return; } // Get available models (validates API keys are non-empty and reasonable length) const available = await getAvailableModels(ctx.modelRegistry); if (available.length === 0) { const hint = getProviderCatalog() .filter((p) => !p.local) .map((p) => ` ${p.envVar}: ${catalogEnvKey(p) ? "set but no models configured" : "not set"}`) .join("\n"); ctx.ui.notify( `❌ No benchmarkable models found.\n\nProvider status (env):\n${hint}\n\nSet at least one API key with known models (env var, /setup or /keys).`, "warning", ); return; } const store = await loadStore(); if (arg === "all") { // Benchmark ALL available models ctx.ui.notify(`🚀 Starting benchmark on ${available.length} models (6 tests each)...\n`, "info"); for (const model of available) { ctx.ui.notify(`\n🧪 **${model.id}** (${model.provider})`, "info"); const result = await benchmarkModel(model, ctx); // Replace existing result for this model store.results = store.results.filter((r) => r.modelId !== model.id); store.results.push(result); await saveStore(store); } ctx.ui.notify(`\n✅ Benchmark complete! ${available.length} models tested.\n`, "info"); ctx.ui.notify(generateReport(store.results), "info"); return; } if (arg) { // Benchmark specific model: prefer exact id match, then require an // unambiguous substring match to avoid benchmarking the wrong model. const exact = available.find((m) => m.id.toLowerCase() === arg); const matches = available.filter((m) => m.id.toLowerCase().includes(arg)); const model = exact ?? (matches.length === 1 ? matches[0] : undefined); if (!model) { if (!exact && matches.length > 1) { ctx.ui.notify( `Model "${arg}" is ambiguous. Did you mean:\n${matches.map((m) => ` - ${m.id} (${m.provider})`).join("\n")}`, "warning", ); return; } ctx.ui.notify( `Model "${arg}" not found or no API key. Available:\n${available.map((m) => ` - ${m.id} (${m.provider})`).join("\n")}`, "warning", ); return; } ctx.ui.notify(`🧪 Benchmarking **${model.id}** (6 categories)...\n`, "info"); const result = await benchmarkModel(model, ctx); store.results = store.results.filter((r) => r.modelId !== model.id); store.results.push(result); await saveStore(store); ctx.ui.notify(`\n✅ **${model.id}** — Total: ${result.totalScore}/100 (avg ${result.avgTimeMs}ms)`, "info"); return; } // Default: benchmark current model // Try to find current model in available list const currentModel = ctx.model; if (currentModel) { const modelConfig = available.find((m) => m.id === currentModel.id && m.provider === currentModel.provider) ?? available.find((m) => m.id === currentModel.id); if (modelConfig) { ctx.ui.notify(`🧪 Benchmarking current model **${currentModel.id}** (6 categories)...\n`, "info"); const result = await benchmarkModel(modelConfig, ctx); store.results = store.results.filter((r) => r.modelId !== modelConfig.id); store.results.push(result); await saveStore(store); ctx.ui.notify(`\n✅ **${currentModel.id}** — Total: ${result.totalScore}/100`, "info"); return; } } // Fallback: show available models ctx.ui.notify( `Available models for benchmark:\n${available.map((m) => ` - ${m.id} (${m.provider})`).join("\n")}\n\nUsage: /benchmark or /benchmark all`, "info", ); }, }); // Session start notification pi.on("session_start", async (_event, ctx) => { try { const store = await loadStore(); if (store.results.length > 0) { ctx.ui.notify( `🧪 ${store.results.length} benchmark results available. /benchmark results to view.`, "info", ); } } catch { // ignore } }); }