* Send a query to Codex and stream responses * @param prompt - User message or prompt * @param cwd - Working directory for Codex * @param resumeSessionId - Optional thread ID to resume
(
prompt: string,
cwd: string,
resumeSessionId?: string
)
| 46 | * @param resumeSessionId - Optional thread ID to resume |
| 47 | */ |
| 48 | async *sendQuery( |
| 49 | prompt: string, |
| 50 | cwd: string, |
| 51 | resumeSessionId?: string |
| 52 | ): AsyncGenerator<MessageChunk> { |
| 53 | const codex = await getCodex(); |
| 54 | |
| 55 | // Get or create thread (synchronous operations!) |
| 56 | let thread; |
| 57 | if (resumeSessionId) { |
| 58 | console.log(`[Codex] Resuming thread: ${resumeSessionId}`); |
| 59 | try { |
| 60 | // NOTE: resumeThread is synchronous, not async |
| 61 | // IMPORTANT: Must pass options when resuming! |
| 62 | thread = codex.resumeThread(resumeSessionId, { |
| 63 | workingDirectory: cwd, |
| 64 | skipGitRepoCheck: true, |
| 65 | }); |
| 66 | } catch (error) { |
| 67 | console.error(`[Codex] Failed to resume thread ${resumeSessionId}, creating new one:`, error); |
| 68 | // Fall back to creating new thread |
| 69 | thread = codex.startThread({ |
| 70 | workingDirectory: cwd, |
| 71 | skipGitRepoCheck: true, |
| 72 | }); |
| 73 | } |
| 74 | } else { |
| 75 | console.log(`[Codex] Starting new thread in ${cwd}`); |
| 76 | // NOTE: startThread is synchronous, not async |
| 77 | thread = codex.startThread({ |
| 78 | workingDirectory: cwd, |
| 79 | skipGitRepoCheck: true, |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | try { |
| 84 | // Run streamed query (this IS async) |
| 85 | const result = await thread.runStreamed(prompt); |
| 86 | |
| 87 | // Process streaming events |
| 88 | for await (const event of result.events) { |
| 89 | // Handle error events |
| 90 | if (event.type === 'error') { |
| 91 | console.error('[Codex] Stream error:', event.message); |
| 92 | // Don't send MCP timeout errors (they're optional) |
| 93 | if (!event.message.includes('MCP client')) { |
| 94 | yield { type: 'system', content: `⚠️ ${event.message}` }; |
| 95 | } |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | // Handle turn failed events |
| 100 | if (event.type === 'turn.failed') { |
| 101 | console.error('[Codex] Turn failed:', event.error?.message); |
| 102 | yield { |
| 103 | type: 'system', |
| 104 | content: `❌ Turn failed: ${event.error?.message || 'Unknown error'}`, |
| 105 | }; |
nothing calls this directly
no test coverage detected