Skip to content

Commit 9cf2dc6

Browse files
committed
Add streaming support and update command arguments for Claude Code and Codex agents in search mode
- it's been reported that whereas agy works flawlessly, 'codex' runs indefinitely [Website] - Update to pass --json to Codex, parse its JSONL stream events for live VS Code progress reporting, and extract the final agent message, mirroring the implementation used for agy. [Website] - handle Claude Code like agy and codex by adding --output-format stream-json, --verbose, and --permission-mode auto flags, and parsing its streaming JSON events to report tool progress and extract parsed.result [Pasted text] - add '--include-partial-messages' to Claude's CLI args [Pasted text] - does claude code implementation follow the docs?[Pasted text] └── apps └── editor └── src ├── constants │ └── instructions.ts └── features └── search-files └── search-modes └── perform-agent-search-mode.ts
1 parent 031241c commit 9cf2dc6

1 file changed

Lines changed: 88 additions & 36 deletions

File tree

‎apps/editor/src/features/search-files/search-modes/perform-agent-search-mode.ts‎

Lines changed: 88 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const perform_agent_search_mode = async (params: {
7575
label: 'Codex',
7676
cmd: 'codex',
7777
executable: 'codex',
78-
args: ['e']
78+
args: ['exec']
7979
})
8080
}
8181

@@ -84,7 +84,7 @@ export const perform_agent_search_mode = async (params: {
8484
label: 'Claude Code',
8585
cmd: 'claude',
8686
executable: 'claude',
87-
args: ['-p']
87+
args: ['--bare', '-p']
8888
})
8989
}
9090

@@ -336,6 +336,10 @@ export const perform_agent_search_mode = async (params: {
336336

337337
if (executable == 'agy') {
338338
args.push('--output-format', 'stream-json', '--dangerously-skip-permissions')
339+
} else if (executable == 'codex') {
340+
args.push('--json')
341+
} else if (executable == 'claude') {
342+
args.push('--output-format', 'stream-json', '--verbose', '--permission-mode', 'auto', '--include-partial-messages')
339343
}
340344

341345
let agent_output = ''
@@ -354,12 +358,13 @@ export const perform_agent_search_mode = async (params: {
354358
return new Promise<void>((resolve, reject) => {
355359
const child = spawn(executable, args, {
356360
cwd: selected_root,
357-
shell: false // Prevents syntax breakage and command injection from newlines/quotes
361+
shell: false, // Prevents syntax breakage and command injection from newlines/quotes
362+
stdio: ['ignore', 'pipe', 'pipe']
358363
})
359364

360365
child.stdout.on('data', (data) => {
361366
const chunk = data.toString()
362-
if (executable == 'agy') {
367+
if (executable == 'agy' || executable == 'codex' || executable == 'claude') {
363368
raw_stream_output += chunk
364369
const lines = raw_stream_output.split('\n')
365370
raw_stream_output = lines.pop() || ''
@@ -368,41 +373,76 @@ export const perform_agent_search_mode = async (params: {
368373
if (!trimmed) continue
369374
try {
370375
const parsed = JSON.parse(trimmed)
371-
if (parsed.event == 'step_update' && parsed.step_update) {
372-
const step = parsed.step_update
373-
if (step.step_type == 'tool') {
374-
const tool_name = step.tool_name || step.tool_info?.name
375-
if (tool_name) {
376-
let msg = `Using ${tool_name.replace(/_/g, ' ')}...`
377-
const params = step.tool_info?.parameters
378-
379-
if (params) {
380-
if (tool_name == 'run_command' && (params.CommandLine || params.command)) {
381-
msg = `Running: ${params.CommandLine || params.command}`
382-
} else if (tool_name == 'read_file' && params.path) {
383-
msg = `Reading: ${params.path}`
384-
} else if ((tool_name == 'write_file' || tool_name == 'write_to_file') && params.path) {
385-
msg = `Writing: ${params.path}`
386-
} else if (tool_name == 'list_directory' && params.path) {
387-
msg = `Listing: ${params.path}`
388-
} else if (tool_name == 'search_files' && (params.query || params.pattern)) {
389-
msg = `Searching: ${params.query || params.pattern}`
376+
if (executable == 'agy') {
377+
if (parsed.event == 'step_update' && parsed.step_update) {
378+
const step = parsed.step_update
379+
if (step.step_type == 'tool') {
380+
const tool_name = step.tool_name || step.tool_info?.name
381+
if (tool_name) {
382+
let msg = `Using ${tool_name.replace(/_/g, ' ')}...`
383+
const params = step.tool_info?.parameters
384+
385+
if (params) {
386+
if (tool_name == 'run_command' && (params.CommandLine || params.command)) {
387+
msg = `Running: ${params.CommandLine || params.command}`
388+
} else if (tool_name == 'read_file' && params.path) {
389+
msg = `Reading: ${params.path}`
390+
} else if ((tool_name == 'write_file' || tool_name == 'write_to_file') && params.path) {
391+
msg = `Writing: ${params.path}`
392+
} else if (tool_name == 'list_directory' && params.path) {
393+
msg = `Listing: ${params.path}`
394+
} else if (tool_name == 'search_files' && (params.query || params.pattern)) {
395+
msg = `Searching: ${params.query || params.pattern}`
396+
}
397+
}
398+
399+
if (msg.length > 60) {
400+
msg = msg.substring(0, 57) + '...'
390401
}
402+
progress.report({ message: msg })
391403
}
392-
404+
} else if (step.subagent_info?.subagents?.length > 0) {
405+
const subagent = step.subagent_info.subagents[0]
406+
progress.report({ message: `Delegating to ${subagent.role || subagent.type_name}...` })
407+
} else if (step.step_type == 'agent_response') {
408+
progress.report({ message: 'Synthesizing results...' })
409+
}
410+
} else if (parsed.event == 'result' && parsed.result) {
411+
agent_output = parsed.result.response || ''
412+
}
413+
} else if (executable == 'codex') {
414+
if (parsed.type == 'item.started' && parsed.item) {
415+
const item = parsed.item
416+
let msg = ''
417+
if (item.type == 'command_execution' && item.command) {
418+
msg = `Running: ${item.command}`
419+
} else if (item.type) {
420+
msg = `Running ${item.type.replace(/_/g, ' ')}...`
421+
}
422+
if (msg) {
393423
if (msg.length > 60) {
394424
msg = msg.substring(0, 57) + '...'
395425
}
396426
progress.report({ message: msg })
397427
}
398-
} else if (step.subagent_info?.subagents?.length > 0) {
399-
const subagent = step.subagent_info.subagents[0]
400-
progress.report({ message: `Delegating to ${subagent.role || subagent.type_name}...` })
401-
} else if (step.step_type == 'agent_response') {
402-
progress.report({ message: 'Synthesizing results...' })
428+
} else if (parsed.type == 'item.completed' && parsed.item) {
429+
if (parsed.item.type == 'agent_message' && parsed.item.text) {
430+
agent_output = parsed.item.text
431+
}
432+
}
433+
} else if (executable == 'claude') {
434+
if (parsed.type == 'stream_event' && parsed.event) {
435+
if (parsed.event.type == 'content_block_start' && parsed.event.content_block?.type == 'tool_use') {
436+
const tool_name = parsed.event.content_block.name
437+
if (tool_name) {
438+
progress.report({ message: `Using ${tool_name.replace(/_/g, ' ')}...` })
439+
}
440+
}
441+
} else if (parsed.type == 'result' && parsed.result) {
442+
agent_output = typeof parsed.result === 'string' ? parsed.result : (parsed.result.text || '')
443+
} else if (parsed.result) {
444+
agent_output = typeof parsed.result === 'string' ? parsed.result : (parsed.result.text || '')
403445
}
404-
} else if (parsed.event == 'result' && parsed.result) {
405-
agent_output = parsed.result.response || ''
406446
}
407447
} catch (e) {
408448
// Ignore parse errors for partial chunks
@@ -424,13 +464,25 @@ export const perform_agent_search_mode = async (params: {
424464
})
425465

426466
child.on('close', () => {
427-
if (executable == 'agy' && raw_stream_output.trim()) {
467+
if ((executable == 'agy' || executable == 'codex' || executable == 'claude') && raw_stream_output.trim()) {
428468
try {
429469
const parsed = JSON.parse(raw_stream_output.trim())
430-
if (parsed.event == 'result' && parsed.result) {
431-
agent_output = parsed.result.response || ''
432-
} else if (parsed.response) {
433-
agent_output = parsed.response || ''
470+
if (executable == 'agy') {
471+
if (parsed.event == 'result' && parsed.result) {
472+
agent_output = parsed.result.response || ''
473+
} else if (parsed.response) {
474+
agent_output = parsed.response || ''
475+
}
476+
} else if (executable == 'codex') {
477+
if (parsed.type == 'item.completed' && parsed.item?.type == 'agent_message' && parsed.item.text) {
478+
agent_output = parsed.item.text
479+
}
480+
} else if (executable == 'claude') {
481+
if (parsed.type == 'result' && parsed.result) {
482+
agent_output = typeof parsed.result === 'string' ? parsed.result : (parsed.result.text || '')
483+
} else if (parsed.result) {
484+
agent_output = typeof parsed.result === 'string' ? parsed.result : (parsed.result.text || '')
485+
}
434486
}
435487
} catch (e) {
436488
// Ignore

0 commit comments

Comments
 (0)