buroventures-harald-code-core
Version:
Harald Code Core - Core functionality for AI-powered coding assistant
105 lines • 4.26 kB
JavaScript
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { logToolCall, ToolErrorType, } from '../index.js';
import { convertToFunctionResponse } from './coreToolScheduler.js';
/**
* Executes a single tool call non-interactively.
* It does not handle confirmations, multiple calls, or live updates.
*/
export async function executeToolCall(config, toolCallRequest, toolRegistry, abortSignal) {
const tool = toolRegistry.getTool(toolCallRequest.name);
const startTime = Date.now();
if (!tool) {
const error = new Error(`Tool "${toolCallRequest.name}" not found in registry.`);
const durationMs = Date.now() - startTime;
logToolCall(config, {
'event.name': 'tool_call',
'event.timestamp': new Date().toISOString(),
function_name: toolCallRequest.name,
function_args: toolCallRequest.args,
duration_ms: durationMs,
success: false,
error: error.message,
prompt_id: toolCallRequest.prompt_id,
});
// Ensure the response structure matches what the API expects for an error
return {
callId: toolCallRequest.callId,
responseParts: [
{
functionResponse: {
id: toolCallRequest.callId,
name: toolCallRequest.name,
response: { error: error.message },
},
},
],
resultDisplay: error.message,
error,
errorType: ToolErrorType.TOOL_NOT_REGISTERED,
};
}
try {
// Directly execute without confirmation or live output handling
const effectiveAbortSignal = abortSignal ?? new AbortController().signal;
const toolResult = await tool.execute(toolCallRequest.args, effectiveAbortSignal);
const tool_output = toolResult.llmContent;
const tool_display = toolResult.returnDisplay;
const durationMs = Date.now() - startTime;
logToolCall(config, {
'event.name': 'tool_call',
'event.timestamp': new Date().toISOString(),
function_name: toolCallRequest.name,
function_args: toolCallRequest.args,
duration_ms: durationMs,
success: toolResult.error === undefined,
error: toolResult.error === undefined ? undefined : toolResult.error.message,
error_type: toolResult.error === undefined ? undefined : toolResult.error.type,
prompt_id: toolCallRequest.prompt_id,
});
const response = convertToFunctionResponse(toolCallRequest.name, toolCallRequest.callId, tool_output);
return {
callId: toolCallRequest.callId,
responseParts: response,
resultDisplay: tool_display,
error: toolResult.error === undefined
? undefined
: new Error(toolResult.error.message),
errorType: toolResult.error === undefined ? undefined : toolResult.error.type,
};
}
catch (e) {
const error = e instanceof Error ? e : new Error(String(e));
const durationMs = Date.now() - startTime;
logToolCall(config, {
'event.name': 'tool_call',
'event.timestamp': new Date().toISOString(),
function_name: toolCallRequest.name,
function_args: toolCallRequest.args,
duration_ms: durationMs,
success: false,
error: error.message,
error_type: ToolErrorType.UNHANDLED_EXCEPTION,
prompt_id: toolCallRequest.prompt_id,
});
return {
callId: toolCallRequest.callId,
responseParts: [
{
functionResponse: {
id: toolCallRequest.callId,
name: toolCallRequest.name,
response: { error: error.message },
},
},
],
resultDisplay: error.message,
error,
errorType: ToolErrorType.UNHANDLED_EXCEPTION,
};
}
}
//# sourceMappingURL=nonInteractiveToolExecutor.js.map