You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ai-net/server/util.ts

31 lines
839 B
TypeScript

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;
}) ?? []
);
}