feat(ai): claude CLI subprocess driver (subscription auth, stream-json)
Implements runClaudeTurn() — spawns the claude CLI for a single companion turn using subscription/OAuth auth (strips ANTHROPIC_API_KEY + ANTHROPIC_AUTH_TOKEN from child env), streaming normalised events (delta, tool, tool_result, result, error) via onEvent callback. Includes hermetic test + fake-claude.js fixture that mimics real 2.1.159 stream-json output; zero network/CLI calls in the test suite. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
66
tests/fixtures/fake-claude.js
vendored
Executable file
66
tests/fixtures/fake-claude.js
vendored
Executable file
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Fake claude CLI for hermetic tests.
|
||||
*
|
||||
* Mimics the stream-json output format of claude CLI 2.1.159:
|
||||
* top-level events are either bare objects (system, assistant, result) or
|
||||
* wrapped in {type:"stream_event", event:{...}}.
|
||||
*
|
||||
* Writes to stdout and exits 0.
|
||||
*/
|
||||
|
||||
const lines = [
|
||||
// system init (ignored by driver)
|
||||
{ type: 'system', subtype: 'init', session_id: 'fake-session-001', tools: [], cwd: '/tmp' },
|
||||
|
||||
// content_block_start: text block
|
||||
{ type: 'stream_event', event: { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } } },
|
||||
|
||||
// text deltas — together they spell "Hello"
|
||||
{ type: 'stream_event', event: { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'Hel' } } },
|
||||
{ type: 'stream_event', event: { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'lo' } } },
|
||||
|
||||
// assistant snapshot (should be ignored by driver to avoid duplication)
|
||||
{ type: 'assistant', message: { role: 'assistant', content: [{ type: 'text', text: 'Hello' }] } },
|
||||
|
||||
// content_block_stop for text
|
||||
{ type: 'stream_event', event: { type: 'content_block_stop', index: 0 } },
|
||||
|
||||
// content_block_start: tool_use
|
||||
{ type: 'stream_event', event: { type: 'content_block_start', index: 1, content_block: { type: 'tool_use', id: 'toolu_fake01', name: 'propose_change', input: {} } } },
|
||||
|
||||
// tool input delta
|
||||
{ type: 'stream_event', event: { type: 'content_block_delta', index: 1, delta: { type: 'input_json_delta', partial_json: '{"file":"/tmp/x.js","content":"console.log(1)"}' } } },
|
||||
|
||||
// assistant snapshot for tool_use (ignored)
|
||||
{ type: 'assistant', message: { role: 'assistant', content: [{ type: 'tool_use', id: 'toolu_fake01', name: 'propose_change', input: { file: '/tmp/x.js', content: 'console.log(1)' } }] } },
|
||||
|
||||
// content_block_stop for tool
|
||||
{ type: 'stream_event', event: { type: 'content_block_stop', index: 1 } },
|
||||
|
||||
// tool_result event
|
||||
{ type: 'tool_result', tool_use_id: 'toolu_fake01', content: [{ type: 'text', text: 'change staged' }] },
|
||||
|
||||
// final result
|
||||
{
|
||||
type: 'result',
|
||||
subtype: 'success',
|
||||
is_error: false,
|
||||
result: 'Hello',
|
||||
stop_reason: 'end_turn',
|
||||
session_id: 'fake-session-001',
|
||||
total_cost_usd: 0.001234,
|
||||
usage: {
|
||||
input_tokens: 100,
|
||||
output_tokens: 10,
|
||||
cache_read_input_tokens: 0,
|
||||
cache_creation_input_tokens: 0,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
for (const line of lines) {
|
||||
process.stdout.write(JSON.stringify(line) + '\n');
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
Reference in New Issue
Block a user