|
| 1 | +import { spawn } from 'node:child_process'; |
| 2 | +import type { CursorModelsResponse, CursorModelSummary } from '@hapi/protocol/apiTypes'; |
| 3 | +import { getErrorMessage } from './rpcResponses'; |
| 4 | + |
| 5 | +export type ListCursorModelsResponse = CursorModelsResponse; |
| 6 | + |
| 7 | +interface CacheEntry { |
| 8 | + expiresAt: number; |
| 9 | + response: ListCursorModelsResponse; |
| 10 | +} |
| 11 | + |
| 12 | +const CACHE_TTL_MS = 60_000; |
| 13 | +const PROBE_TIMEOUT_MS = 30_000; |
| 14 | +const cache: CacheEntry = { |
| 15 | + expiresAt: 0, |
| 16 | + response: { success: true, availableModels: [], currentModelId: null } |
| 17 | +}; |
| 18 | +let inflight: Promise<ListCursorModelsResponse> | null = null; |
| 19 | + |
| 20 | +export function parseCursorModelsOutput(output: string): { |
| 21 | + availableModels: CursorModelSummary[]; |
| 22 | + currentModelId: string | null; |
| 23 | +} { |
| 24 | + const availableModels: CursorModelSummary[] = []; |
| 25 | + let currentModelId: string | null = null; |
| 26 | + |
| 27 | + for (const rawLine of output.split(/\r?\n/)) { |
| 28 | + const line = rawLine.trim(); |
| 29 | + if (!line || line === 'Available models' || line.startsWith('Tip:')) { |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + const separatorIndex = line.indexOf(' - '); |
| 34 | + if (separatorIndex <= 0) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + const modelId = line.slice(0, separatorIndex).trim(); |
| 39 | + const rawName = line.slice(separatorIndex + 3).trim(); |
| 40 | + if (!modelId || !rawName) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + const isCurrent = /\s*\(current\)\s*$/.test(rawName); |
| 45 | + const isDefault = /\s*\(default\)\s*$/.test(rawName); |
| 46 | + const name = rawName.replace(/\s*\((?:current|default)\)\s*$/, '').trim(); |
| 47 | + availableModels.push(name && name !== modelId ? { modelId, name } : { modelId }); |
| 48 | + |
| 49 | + if (isCurrent) { |
| 50 | + currentModelId = modelId; |
| 51 | + } else if (isDefault && currentModelId === null) { |
| 52 | + currentModelId = modelId; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return { availableModels, currentModelId }; |
| 57 | +} |
| 58 | + |
| 59 | +async function runCursorModelProbe(): Promise<ListCursorModelsResponse> { |
| 60 | + return await new Promise((resolve, reject) => { |
| 61 | + const child = spawn('agent', ['--list-models'], { |
| 62 | + env: process.env, |
| 63 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 64 | + shell: process.platform === 'win32' |
| 65 | + }); |
| 66 | + let stdout = ''; |
| 67 | + let stderr = ''; |
| 68 | + let settled = false; |
| 69 | + |
| 70 | + const timeout = setTimeout(() => { |
| 71 | + if (settled) return; |
| 72 | + settled = true; |
| 73 | + child.kill('SIGTERM'); |
| 74 | + reject(new Error('Cursor model discovery timed out')); |
| 75 | + }, PROBE_TIMEOUT_MS); |
| 76 | + |
| 77 | + child.stdout?.on('data', (chunk) => { |
| 78 | + stdout += chunk.toString(); |
| 79 | + }); |
| 80 | + child.stderr?.on('data', (chunk) => { |
| 81 | + stderr += chunk.toString(); |
| 82 | + }); |
| 83 | + child.on('error', (error) => { |
| 84 | + if (settled) return; |
| 85 | + settled = true; |
| 86 | + clearTimeout(timeout); |
| 87 | + reject(error); |
| 88 | + }); |
| 89 | + child.on('exit', (code) => { |
| 90 | + if (settled) return; |
| 91 | + settled = true; |
| 92 | + clearTimeout(timeout); |
| 93 | + if (code !== 0) { |
| 94 | + reject(new Error(stderr.trim() || `agent --list-models exited with code ${code}`)); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + resolve({ |
| 99 | + success: true, |
| 100 | + ...parseCursorModelsOutput(stdout) |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +export async function listCursorModels(): Promise<ListCursorModelsResponse> { |
| 107 | + if (cache.expiresAt > Date.now()) { |
| 108 | + return cache.response; |
| 109 | + } |
| 110 | + |
| 111 | + if (inflight) { |
| 112 | + return inflight; |
| 113 | + } |
| 114 | + |
| 115 | + inflight = (async () => { |
| 116 | + try { |
| 117 | + const response = await runCursorModelProbe(); |
| 118 | + cache.expiresAt = Date.now() + CACHE_TTL_MS; |
| 119 | + cache.response = response; |
| 120 | + return response; |
| 121 | + } catch (error) { |
| 122 | + return { |
| 123 | + success: false, |
| 124 | + error: getErrorMessage(error, 'Failed to discover Cursor models') |
| 125 | + }; |
| 126 | + } finally { |
| 127 | + inflight = null; |
| 128 | + } |
| 129 | + })(); |
| 130 | + |
| 131 | + return inflight; |
| 132 | +} |
| 133 | + |
| 134 | +export function _resetCursorModelsCacheForTests(): void { |
| 135 | + cache.expiresAt = 0; |
| 136 | + cache.response = { success: true, availableModels: [], currentModelId: null }; |
| 137 | + inflight = null; |
| 138 | +} |
0 commit comments