Tool Calling: Make formatMessage async

c49138dfeae3f58b0775777d5a361116a9d9dc8b

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +10 -9Ignore whitespace
public/scripts/tool-calling.js+10 -9
@@ -31,8 +31,8 @@ import { slashCommandReturnHelper } from './slash-commands/SlashCommandReturnHel
3131 * @property {string} description - A description of the tool.
3232 * @property {object} parameters - The parameters for the tool.
3333 * @property {function} action - The action to perform when the tool is invoked.
3434 * @property {function} [formatMessage] - A function to format the tool call message.
3535 * @property {function} [shouldRegister] - A function to determine if the tool should be registered.
3636 */
3737
3838/**
@@ -194,11 +194,11 @@ class ToolDefinition {
194194 /**
195195 * Formats a message with the tool invocation.
196196 * @param {object} parameters The parameters to pass to the tool.
197197 * @returns {Promise<string>} The formatted message.
198198 */
199199 async formatMessage(parameters) {
200200 return typeof this.#formatMessage === 'function'
201201 ? await this.#formatMessage(parameters)
202202 : `Invoking tool: ${this.#displayName || this.#name}`;
203203 }
204204
@@ -303,9 +303,9 @@ export class ToolManager {
303303 * Formats a message for a tool call by name.
304304 * @param {string} name The name of the tool to format the message for.
305305 * @param {object} parameters Function tool call parameters.
306306 * @returns {Promise<string>} The formatted message for the tool call.
307307 */
308308 static async formatToolCallMessage(name, parameters) {
309309 if (!this.#tools.has(name)) {
310310 return `Invoked unknown tool: ${name}`;
311311 }
@@ -313,7 +313,7 @@ export class ToolManager {
313313 try {
314314 const tool = this.#tools.get(name);
315315 const formatParameters = typeof parameters === 'string' ? JSON.parse(parameters) : parameters;
316316 return await tool.formatMessage(formatParameters);
317317 } catch (error) {
318318 console.error(`[ToolManager] An error occurred while formatting the tool call message for "${name}":`, error);
319319 return `Invoking tool: ${name}`;
@@ -590,7 +590,7 @@ export class ToolManager {
590590 const name = toolCall.function.name;
591591 const displayName = ToolManager.getDisplayName(name);
592592
593593 const message = await ToolManager.formatToolCallMessage(name, parameters);
594594 const toast = message && toastr.info(message, 'Tool Calling', { timeOut: 0 });
595595 const toolResult = await ToolManager.invokeFunctionTool(name, parameters);
596596 toastr.clear(toast);
@@ -878,6 +878,7 @@ export class ToolManager {
878878 parameters: JSON.parse(parameters ?? '{}'),
879879 action: actionFunc,
880880 formatMessage: formatMessageFunc,
881+ shouldRegister: async () => true, // TODO: Implement shouldRegister
881882 });
882883
883884 return '';