add: human-in-the-loop tool-calling
parent
e292cebb1d
commit
8b8c92f1d6
@ -0,0 +1,5 @@
|
||||
export default {
|
||||
delegate: async function () {
|
||||
return "Here's a vegetarian lasagna recipe for 4 people:";
|
||||
},
|
||||
};
|
@ -1,3 +1,30 @@
|
||||
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;
|
||||
}) ?? []
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue