import { Message } from "ai"; import tools from "./tools.js"; export function singleSpace(str: string) { return str.replace(/\s+/g, " "); } export async function processPendingToolCalls(messages: Message[]) { const lastMessage = messages[messages.length - 1]; if (!lastMessage) { return; } if (!lastMessage.parts) { return; } /** Execute all the pending tool calls: */ lastMessage.parts = await Promise.all( lastMessage.parts?.map(async (part) => { const toolInvocation = part.toolInvocation; if (toolInvocation?.state === "call") { toolInvocation.state = "result"; toolInvocation.result = toolInvocation.result === "yes" ? await tools[toolInvocation.toolName]?.() : "Error: User denied tool call."; } return part; }) ?? [] ); }