diff --git a/src/execution.ts b/src/execution.ts index feb4bba..99acd35 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -260,12 +260,10 @@ async function executeFromQueue(channel: string) { // handle function calls if (generatedMessage.function_call) { OpenAImessages.push(generatedMessage); - OpenAImessages.push({ - role: "function", - name: generatedMessage.function_call.name, - // FIXME: don't use new instance of FunctionManager - content: new FunctionManager().handleFunction(generatedMessage.function_call), - }); + // FIXME: don't use new instance of FunctionManager + OpenAImessages.push( + new FunctionManager().handleFunction(generatedMessage.function_call) + ); } } while (generatedMessage.function_call); diff --git a/src/funcitonManager.ts b/src/funcitonManager.ts index d351c15..4a0910e 100644 --- a/src/funcitonManager.ts +++ b/src/funcitonManager.ts @@ -1,4 +1,5 @@ -import { ChatCompletionFunctions, ChatCompletionRequestMessageFunctionCall } from "openai"; +import { ChatCompletionFunctions, ChatCompletionRequestMessage, ChatCompletionRequestMessageFunctionCall } from "openai"; + import config from "./config"; type parameterMap = { @@ -60,9 +61,20 @@ export default class FunctionManager { return rvalue; } - public handleFunction(request: ChatCompletionRequestMessageFunctionCall) { + public handleFunction(request: ChatCompletionRequestMessageFunctionCall): ChatCompletionRequestMessage { // eslint-disable-next-line @typescript-eslint/no-explicit-any let parsedArguments: any; + + const functionToRun = this.store.get(request.name ?? ""); + + // check if the function is registered + if (!functionToRun) { + return { + role: "system", + content: "Only use functions that were provided to you", + }; + } + try { parsedArguments = JSON.parse(request.arguments ?? ""); } @@ -70,7 +82,11 @@ export default class FunctionManager { console.error("Function arguments raw: " + request.arguments); throw new Error(`Failed to parse the function JSON arguments when running function [${request.name}]`, {cause: e}); } - return this.store.get(request.name ?? "")?.execute(parsedArguments); + return { + role: "function", + name: request.name, + content: functionToRun.execute(parsedArguments), + }; } }