| 1 | // A deterministic Agent Client Protocol agent that advertises its own models, |
| 2 | // a mode, a config option and one control Codewhale cannot write, and reports |
| 3 | // token usage. Behavior is steered by `options-fixture.json` in the session |
| 4 | // cwd; everything it receives is appended to `options-received.jsonl` there. |
| 5 | import { appendFileSync, existsSync, readFileSync, writeFileSync } from 'node:fs'; |
| 6 | import { join } from 'node:path'; |
| 7 | |
| 8 | const plan = () => { |
| 9 | const path = join(process.cwd(), 'options-fixture.json'); |
| 10 | return existsSync(path) ? JSON.parse(readFileSync(path, 'utf8')) : {}; |
| 11 | }; |
| 12 | const record = (entry) => |
| 13 | appendFileSync(join(process.cwd(), 'options-received.jsonl'), `${JSON.stringify({ pid: process.pid, ...entry })}\n`); |
| 14 | writeFileSync(join(process.cwd(), 'options-env.json'), JSON.stringify(process.env)); |
| 15 | |
| 16 | const MODELS = [ |
| 17 | { value: 'model-a', name: 'Model A', description: 'Fast' }, |
| 18 | { value: 'model-b', name: 'Model B', description: 'Careful' }, |
| 19 | { value: 'model-c', name: 'Model C' }, |
| 20 | ]; |
| 21 | const EFFORTS = [ |
| 22 | { value: 'low', name: 'Low' }, |
| 23 | { value: 'high', name: 'High' }, |
| 24 | ]; |
| 25 | const MODES = [ |
| 26 | { id: 'default', name: 'Default' }, |
| 27 | { id: 'review', name: 'Review only', description: 'Reads, never edits' }, |
| 28 | ]; |
| 29 | |
| 30 | let buffer = ''; |
| 31 | let sessionId = null; |
| 32 | const state = { model: 'model-a', effort: 'low', mode: 'default' }; |
| 33 | |
| 34 | const send = (message) => process.stdout.write(`${JSON.stringify(message)}\n`); |
| 35 | const result = (id, value) => send({ jsonrpc: '2.0', id, result: value }); |
| 36 | const failure = (id, code, message) => send({ jsonrpc: '2.0', id, error: { code, message } }); |
| 37 | const notify = (method, params) => send({ jsonrpc: '2.0', method, params }); |
| 38 | |
| 39 | function offer() { |
| 40 | const currentPlan = plan(); |
| 41 | const withdrawn = new Set(currentPlan.withdraw ?? []); |
| 42 | const models = MODELS.filter((model) => !withdrawn.has(model.value)); |
| 43 | const modelEfforts = |
| 44 | currentPlan.efforts_by_model |
| 45 | && Object.prototype.hasOwnProperty.call(currentPlan.efforts_by_model, state.model) |
| 46 | ? currentPlan.efforts_by_model[state.model] |
| 47 | : EFFORTS; |
| 48 | const configOptions = [ |
| 49 | { id: 'model', name: 'Model', category: 'model', type: 'select', currentValue: state.model, options: models }, |
| 50 | ...(Array.isArray(modelEfforts) |
| 51 | ? [ |
| 52 | { |
| 53 | id: 'effort', |
| 54 | name: 'Reasoning effort', |
| 55 | category: 'thought_level', |
| 56 | type: 'select', |
| 57 | currentValue: modelEfforts.some((item) => item.value === state.effort) |
| 58 | ? state.effort |
| 59 | : (modelEfforts[0]?.value ?? state.effort), |
| 60 | options: modelEfforts, |
| 61 | }, |
| 62 | ] |
| 63 | : []), |
| 64 | { id: 'verbose', name: 'Verbose logging', type: 'boolean', currentValue: false }, |
| 65 | ]; |
| 66 | const reply = { |
| 67 | configOptions, |
| 68 | modes: { currentModeId: state.mode, availableModes: MODES }, |
| 69 | }; |
| 70 | if (currentPlan.malformed) { |
| 71 | reply.configOptions.push({ id: '', name: 'No id', type: 'select', currentValue: 'x', options: [] }); |
| 72 | reply.configOptions.push({ id: 'empty', name: 'Empty', type: 'select', currentValue: 'x', options: [] }); |
| 73 | } |
| 74 | if (currentPlan.huge) { |
| 75 | reply.configOptions.push({ |
| 76 | id: 'many', |
| 77 | name: 'Many', |
| 78 | type: 'select', |
| 79 | currentValue: 'v0', |
| 80 | options: Array.from({ length: 600 }, (_, index) => ({ value: `v${index}`, name: `Value ${index}` })), |
| 81 | }); |
| 82 | } |
| 83 | return reply; |
| 84 | } |
| 85 | |
| 86 | function offered(config, value) { |
| 87 | const found = offer().configOptions.find((option) => option.id === config); |
| 88 | return Boolean(found && found.type === 'select' && found.options.some((option) => option.value === value)); |
| 89 | } |
| 90 | |
| 91 | function prompt(request) { |
| 92 | const text = (request.params?.prompt ?? []).map((block) => block.text ?? '').join(''); |
| 93 | record({ method: 'session/prompt', text, state: { ...state } }); |
| 94 | notify('session/update', { |
| 95 | sessionId, |
| 96 | update: { |
| 97 | sessionUpdate: 'agent_message_chunk', |
| 98 | content: { type: 'text', text: `model=${state.model} effort=${state.effort} mode=${state.mode}` }, |
| 99 | }, |
| 100 | }); |
| 101 | if (!plan().no_usage) { |
| 102 | notify('session/update', { sessionId, update: { sessionUpdate: 'usage_update', used: 1200, size: 200000 } }); |
| 103 | } |
| 104 | result(request.id, { |
| 105 | stopReason: 'end_turn', |
| 106 | ...(plan().no_usage ? {} : { usage: { inputTokens: 100, outputTokens: 50, totalTokens: 150, thoughtTokens: 7 } }), |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | function handle(message) { |
| 111 | if (message.id !== undefined && message.method === undefined) return; |
| 112 | const params = message.params ?? {}; |
| 113 | switch (message.method) { |
| 114 | case 'initialize': |
| 115 | result(message.id, { |
| 116 | protocolVersion: 1, |
| 117 | agentInfo: { name: 'options-fixture', version: '1.0.0' }, |
| 118 | authMethods: [], |
| 119 | agentCapabilities: { |
| 120 | loadSession: true, |
| 121 | promptCapabilities: { image: true, embeddedContext: true }, |
| 122 | sessionCapabilities: { fork: {} }, |
| 123 | }, |
| 124 | }); |
| 125 | break; |
| 126 | case 'session/new': |
| 127 | if (plan().auth_required) { |
| 128 | failure(message.id, -32000, 'Authentication required'); |
| 129 | break; |
| 130 | } |
| 131 | sessionId = `options-session-${process.pid}`; |
| 132 | record({ method: 'session/new', cwd: params.cwd }); |
| 133 | if (plan().hang) break; |
| 134 | result(message.id, { sessionId, ...offer() }); |
| 135 | break; |
| 136 | case 'session/load': |
| 137 | case 'session/resume': |
| 138 | sessionId = params.sessionId ?? null; |
| 139 | record({ method: message.method, sessionId }); |
| 140 | result(message.id, offer()); |
| 141 | break; |
| 142 | case 'session/set_config_option': |
| 143 | record({ method: message.method, configId: params.configId, value: params.value }); |
| 144 | if (!offered(params.configId, params.value)) { |
| 145 | failure(message.id, -32602, 'value not offered'); |
| 146 | break; |
| 147 | } |
| 148 | state[params.configId] = params.value; |
| 149 | result(message.id, { configOptions: offer().configOptions }); |
| 150 | break; |
| 151 | case 'session/set_mode': |
| 152 | record({ method: message.method, modeId: params.modeId }); |
| 153 | if (!MODES.some((mode) => mode.id === params.modeId)) { |
| 154 | failure(message.id, -32602, 'mode not offered'); |
| 155 | break; |
| 156 | } |
| 157 | state.mode = params.modeId; |
| 158 | result(message.id, {}); |
| 159 | break; |
| 160 | case 'session/prompt': |
| 161 | prompt(message); |
| 162 | break; |
| 163 | case 'session/cancel': |
| 164 | break; |
| 165 | default: |
| 166 | if (message.id !== undefined) failure(message.id, -32601, `no method ${message.method}`); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | process.stdin.on('data', (chunk) => { |
| 171 | buffer += chunk; |
| 172 | let index; |
| 173 | while ((index = buffer.indexOf('\n')) >= 0) { |
| 174 | const line = buffer.slice(0, index); |
| 175 | buffer = buffer.slice(index + 1); |
| 176 | if (!line.trim()) continue; |
| 177 | try { |
| 178 | handle(JSON.parse(line)); |
| 179 | } catch (error) { |
| 180 | process.stderr.write(`options fixture: bad line: ${String(error)}\n`); |
| 181 | } |
| 182 | } |
| 183 | }); |
| 184 | process.stdin.on('end', () => process.exit(0)); |
| 185 |