| 1 | // A deterministic Agent Client Protocol agent for the fleet transport test. |
| 2 | // Speaks newline-framed JSON-RPC on stdio. No dependencies, no network. |
| 3 | import { appendFileSync, existsSync, readFileSync, writeFileSync } from 'node:fs'; |
| 4 | import { join } from 'node:path'; |
| 5 | |
| 6 | const HANG = process.env.HANG === '1'; |
| 7 | const envPath = join(process.cwd(), 'fixture-env.json'); |
| 8 | writeFileSync(envPath, JSON.stringify(process.env)); |
| 9 | |
| 10 | let buffer = ''; |
| 11 | let sessionId = null; |
| 12 | let cwd = process.cwd(); |
| 13 | let permissionWaiter = null; |
| 14 | let pendingPrompt = null; |
| 15 | let calls = 0; |
| 16 | let configModel = null; |
| 17 | |
| 18 | const plan = () => { |
| 19 | const path = join(process.cwd(), 'fixture-plan.json'); |
| 20 | return existsSync(path) ? JSON.parse(readFileSync(path, 'utf8')) : {}; |
| 21 | }; |
| 22 | const record = (name, value) => appendFileSync(join(process.cwd(), name), `${JSON.stringify(value)}\n`); |
| 23 | |
| 24 | const send = (message) => process.stdout.write(`${JSON.stringify(message)}\n`); |
| 25 | const result = (id, value) => send({ jsonrpc: '2.0', id, result: value }); |
| 26 | const notify = (method, params) => send({ jsonrpc: '2.0', method, params }); |
| 27 | |
| 28 | function askPermission(toolCall) { |
| 29 | return new Promise((resolve) => { |
| 30 | permissionWaiter = resolve; |
| 31 | send({ |
| 32 | jsonrpc: '2.0', |
| 33 | id: 9001, |
| 34 | method: 'session/request_permission', |
| 35 | params: { |
| 36 | sessionId, |
| 37 | toolCall, |
| 38 | options: [ |
| 39 | { optionId: 'allow', name: 'Allow once', kind: 'allow_once' }, |
| 40 | { optionId: 'reject', name: 'Reject once', kind: 'reject_once' }, |
| 41 | ], |
| 42 | }, |
| 43 | }); |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | async function prompt(request) { |
| 48 | const text = (request.params?.prompt ?? []).map((block) => block.text ?? '').join(''); |
| 49 | notify('session/update', { sessionId, update: { sessionUpdate: 'agent_thought_chunk', content: { type: 'text', text: 'Thinking about it.' } } }); |
| 50 | if (plan().extraUpdates) { |
| 51 | notify('session/update', { sessionId, update: { sessionUpdate: 'plan_update', entries: [{ content: 'Write the fixture', status: 'in_progress' }] } }); |
| 52 | notify('session/update', { sessionId, update: { sessionUpdate: 'current_mode_update', currentModeId: 'review' } }); |
| 53 | notify('session/update', { sessionId, update: { sessionUpdate: 'available_commands_update', availableCommands: [{ name: 'compact', description: 'Shrink the context' }] } }); |
| 54 | } |
| 55 | for (const chunk of [`You said: `, text, `.`]) { |
| 56 | notify('session/update', { sessionId, update: { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: chunk } } }); |
| 57 | } |
| 58 | const toolCall = { toolCallId: `call-${++calls}`, title: 'Write fixture.txt', kind: 'edit', status: 'pending', locations: [{ path: join(cwd, 'fixture.txt') }], rawInput: { path: 'fixture.txt' } }; |
| 59 | notify('session/update', { sessionId, update: { sessionUpdate: 'tool_call', ...toolCall } }); |
| 60 | const answer = await askPermission(toolCall); |
| 61 | const outcome = answer?.outcome ?? {}; |
| 62 | if (outcome.outcome === 'selected' && outcome.optionId === 'allow') { |
| 63 | writeFileSync(join(cwd, 'fixture.txt'), 'written by the fixture agent\n'); |
| 64 | notify('session/update', { sessionId, update: { sessionUpdate: 'tool_call_update', toolCallId: toolCall.toolCallId, status: 'completed', content: [{ type: 'content', content: { type: 'text', text: 'wrote fixture.txt' } }] } }); |
| 65 | } else { |
| 66 | notify('session/update', { sessionId, update: { sessionUpdate: 'tool_call_update', toolCallId: toolCall.toolCallId, status: 'failed' } }); |
| 67 | } |
| 68 | if (outcome.outcome === 'cancelled') { |
| 69 | result(request.id, { stopReason: plan().endTurnOnCancel ? 'end_turn' : 'cancelled' }); |
| 70 | return; |
| 71 | } |
| 72 | notify('session/update', { sessionId, update: { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: ' Done.' } } }); |
| 73 | result(request.id, { stopReason: 'end_turn' }); |
| 74 | } |
| 75 | |
| 76 | function handle(message) { |
| 77 | if (message.id !== undefined && message.method === undefined) { |
| 78 | if (message.id === 9001 && permissionWaiter) { |
| 79 | const resolve = permissionWaiter; |
| 80 | permissionWaiter = null; |
| 81 | record('permission-outcomes.jsonl', message.result?.outcome ?? null); |
| 82 | resolve(message.result); |
| 83 | } |
| 84 | return; |
| 85 | } |
| 86 | switch (message.method) { |
| 87 | case 'initialize': |
| 88 | result(message.id, { |
| 89 | protocolVersion: 1, |
| 90 | agentInfo: { name: 'fixture-agent', version: '1.0.0' }, |
| 91 | authMethods: [], |
| 92 | agentCapabilities: { loadSession: true, sessionCapabilities: plan().additionalDirectories ? { additionalDirectories: {} } : {} }, |
| 93 | }); |
| 94 | break; |
| 95 | case 'session/new': |
| 96 | record('session-params.jsonl', { method: message.method, params: message.params }); |
| 97 | sessionId = `fixture-session-${process.pid}`; |
| 98 | cwd = message.params?.cwd ?? cwd; |
| 99 | result(message.id, { |
| 100 | sessionId, |
| 101 | ...(plan().legacyModels |
| 102 | ? { models: { currentModelId: 'legacy-a', availableModels: [{ modelId: 'legacy-a', name: 'Legacy A' }, { modelId: 'legacy-b', name: 'Legacy B' }] } } |
| 103 | : {}), |
| 104 | }); |
| 105 | break; |
| 106 | case 'session/load': |
| 107 | record('session-params.jsonl', { method: message.method, params: message.params }); |
| 108 | sessionId = message.params?.sessionId ?? null; |
| 109 | cwd = message.params?.cwd ?? cwd; |
| 110 | notify('session/update', { sessionId, update: { sessionUpdate: 'user_message_chunk', content: { type: 'text', text: 'replayed history' } } }); |
| 111 | notify('session/update', { sessionId, update: { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: 'replayed answer' } } }); |
| 112 | result(message.id, {}); |
| 113 | break; |
| 114 | case 'session/set_model': |
| 115 | result(message.id, {}); |
| 116 | break; |
| 117 | case 'session/set_config_option': |
| 118 | configModel = message.params?.value ?? null; |
| 119 | result(message.id, { configOptions: [] }); |
| 120 | break; |
| 121 | case 'session/prompt': |
| 122 | if (plan().ignoreCancel) { |
| 123 | record('held-prompts.jsonl', { id: message.id }); |
| 124 | break; |
| 125 | } |
| 126 | if (HANG) { |
| 127 | pendingPrompt = message; |
| 128 | break; |
| 129 | } |
| 130 | void prompt(message); |
| 131 | break; |
| 132 | case 'session/cancel': |
| 133 | if (pendingPrompt) { |
| 134 | result(pendingPrompt.id, { stopReason: 'cancelled' }); |
| 135 | pendingPrompt = null; |
| 136 | } |
| 137 | break; |
| 138 | default: |
| 139 | if (message.id !== undefined) { |
| 140 | send({ jsonrpc: '2.0', id: message.id, error: { code: -32601, message: `no method ${message.method}` } }); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | process.stdin.on('data', (chunk) => { |
| 146 | buffer += chunk; |
| 147 | let index; |
| 148 | while ((index = buffer.indexOf('\n')) >= 0) { |
| 149 | const line = buffer.slice(0, index); |
| 150 | buffer = buffer.slice(index + 1); |
| 151 | if (!line.trim()) continue; |
| 152 | try { |
| 153 | handle(JSON.parse(line)); |
| 154 | } catch (error) { |
| 155 | process.stderr.write(`fixture: bad line: ${String(error)}\n`); |
| 156 | } |
| 157 | } |
| 158 | }); |
| 159 | process.stdin.on('end', () => process.exit(0)); |
| 160 |