( config: Config, input: string, prompt_id: string, )
| 20 | import { ConsolePatcher } from './ui/utils/ConsolePatcher.js'; |
| 21 | |
| 22 | export async function runNonInteractive( |
| 23 | config: Config, |
| 24 | input: string, |
| 25 | prompt_id: string, |
| 26 | ): Promise<void> { |
| 27 | const consolePatcher = new ConsolePatcher({ |
| 28 | stderr: true, |
| 29 | debugMode: config.getDebugMode(), |
| 30 | }); |
| 31 | |
| 32 | try { |
| 33 | consolePatcher.patch(); |
| 34 | // Handle EPIPE errors when the output is piped to a command that closes early. |
| 35 | process.stdout.on('error', (err: NodeJS.ErrnoException) => { |
| 36 | if (err.code === 'EPIPE') { |
| 37 | // Exit gracefully if the pipe is closed. |
| 38 | process.exit(0); |
| 39 | } |
| 40 | }); |
| 41 | |
| 42 | const anusClient = config.getAnusClient(); |
| 43 | const toolRegistry: ToolRegistry = await config.getToolRegistry(); |
| 44 | |
| 45 | const abortController = new AbortController(); |
| 46 | let currentMessages: Content[] = [ |
| 47 | { role: 'user', parts: [{ text: input }] }, |
| 48 | ]; |
| 49 | let turnCount = 0; |
| 50 | while (true) { |
| 51 | turnCount++; |
| 52 | if ( |
| 53 | config.getMaxSessionTurns() >= 0 && |
| 54 | turnCount > config.getMaxSessionTurns() |
| 55 | ) { |
| 56 | console.error( |
| 57 | '\n Reached max session turns for this session. Increase the number of turns by specifying maxSessionTurns in settings.json.', |
| 58 | ); |
| 59 | return; |
| 60 | } |
| 61 | const functionCalls: FunctionCall[] = []; |
| 62 | |
| 63 | const responseStream = anusClient.sendMessageStream( |
| 64 | currentMessages[0]?.parts || [], |
| 65 | abortController.signal, |
| 66 | prompt_id, |
| 67 | ); |
| 68 | |
| 69 | for await (const event of responseStream) { |
| 70 | if (abortController.signal.aborted) { |
| 71 | console.error('Operation cancelled.'); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (event.type === AnusEventType.Content) { |
| 76 | process.stdout.write(event.value); |
| 77 | } else if (event.type === AnusEventType.ToolCallRequest) { |
| 78 | const toolCallRequest = event.value; |
| 79 | const fc: FunctionCall = { |
no test coverage detected