UNPKG

buroventures-harald-code

Version:

Harald Code - AI-powered coding assistant CLI

99 lines 4.38 kB
/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { executeToolCall, shutdownTelemetry, isTelemetrySdkInitialized, GeminiEventType, ToolErrorType, } from 'buroventures-harald-code-core'; import { parseAndFormatApiError } from './ui/utils/errorParsing.js'; export async function runNonInteractive(config, input, prompt_id) { await config.initialize(); // Handle EPIPE errors when the output is piped to a command that closes early. process.stdout.on('error', (err) => { if (err.code === 'EPIPE') { // Exit gracefully if the pipe is closed. process.exit(0); } }); const geminiClient = config.getGeminiClient(); const toolRegistry = await config.getToolRegistry(); const abortController = new AbortController(); let currentMessages = [{ role: 'user', parts: [{ text: input }] }]; let turnCount = 0; try { while (true) { turnCount++; if (config.getMaxSessionTurns() >= 0 && turnCount > config.getMaxSessionTurns()) { console.error('\n Reached max session turns for this session. Increase the number of turns by specifying maxSessionTurns in settings.json.'); return; } const functionCalls = []; const responseStream = geminiClient.sendMessageStream(currentMessages[0]?.parts || [], abortController.signal, prompt_id); for await (const event of responseStream) { if (abortController.signal.aborted) { console.error('Operation cancelled.'); return; } if (event.type === GeminiEventType.Content) { process.stdout.write(event.value); } else if (event.type === GeminiEventType.ToolCallRequest) { const toolCallRequest = event.value; const fc = { name: toolCallRequest.name, args: toolCallRequest.args, id: toolCallRequest.callId, }; functionCalls.push(fc); } } if (functionCalls.length > 0) { const toolResponseParts = []; for (const fc of functionCalls) { const callId = fc.id ?? `${fc.name}-${Date.now()}`; const requestInfo = { callId, name: fc.name, args: (fc.args ?? {}), isClientInitiated: false, prompt_id, }; const toolResponse = await executeToolCall(config, requestInfo, toolRegistry, abortController.signal); if (toolResponse.error) { console.error(`Error executing tool ${fc.name}: ${toolResponse.resultDisplay || toolResponse.error.message}`); if (toolResponse.errorType === ToolErrorType.UNHANDLED_EXCEPTION) process.exit(1); } if (toolResponse.responseParts) { const parts = Array.isArray(toolResponse.responseParts) ? toolResponse.responseParts : [toolResponse.responseParts]; for (const part of parts) { if (typeof part === 'string') { toolResponseParts.push({ text: part }); } else if (part) { toolResponseParts.push(part); } } } } currentMessages = [{ role: 'user', parts: toolResponseParts }]; } else { process.stdout.write('\n'); // Ensure a final newline return; } } } catch (error) { console.error(parseAndFormatApiError(error, config.getContentGeneratorConfig()?.authType)); process.exit(1); } finally { if (isTelemetrySdkInitialized()) { await shutdownTelemetry(); } } } //# sourceMappingURL=nonInteractiveCli.js.map