Tool Calling: Make formatMessage async

c49138dfeae3f58b0775777d5a361116a9d9dc8b

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

1 files changed, +10 -9Showing whitespace changes
public/scripts/tool-calling.js+10 -9
@@ -31,8 +31,8 @@ import { slashCommandReturnHelper } from './slash-commands/SlashCommandReturnHel
31 * @property {string} description - A description of the tool.31 * @property {string} description - A description of the tool.
32 * @property {object} parameters - The parameters for the tool.32 * @property {object} parameters - The parameters for the tool.
33 * @property {function} action - The action to perform when the tool is invoked.33 * @property {function} action - The action to perform when the tool is invoked.
34 * @property {function} formatMessage - A function to format the tool call message.34 * @property {function} [formatMessage] - A function to format the tool call message.
35 * @property {function} shouldRegister - A function to determine if the tool should be registered.35 * @property {function} [shouldRegister] - A function to determine if the tool should be registered.
36 */36 */
3737
38/**38/**
@@ -194,11 +194,11 @@ class ToolDefinition {
194 /**194 /**
195 * Formats a message with the tool invocation.195 * Formats a message with the tool invocation.
196 * @param {object} parameters The parameters to pass to the tool.196 * @param {object} parameters The parameters to pass to the tool.
197 * @returns {string} The formatted message.197 * @returns {Promise<string>} The formatted message.
198 */198 */
199 formatMessage(parameters) {199 async formatMessage(parameters) {
200 return typeof this.#formatMessage === 'function'200 return typeof this.#formatMessage === 'function'
201 ? this.#formatMessage(parameters)201 ? await this.#formatMessage(parameters)
202 : `Invoking tool: ${this.#displayName || this.#name}`;202 : `Invoking tool: ${this.#displayName || this.#name}`;
203 }203 }
204204
@@ -303,9 +303,9 @@ export class ToolManager {
303 * Formats a message for a tool call by name.303 * Formats a message for a tool call by name.
304 * @param {string} name The name of the tool to format the message for.304 * @param {string} name The name of the tool to format the message for.
305 * @param {object} parameters Function tool call parameters.305 * @param {object} parameters Function tool call parameters.
306 * @returns {string} The formatted message for the tool call.306 * @returns {Promise<string>} The formatted message for the tool call.
307 */307 */
308 static formatToolCallMessage(name, parameters) {308 static async formatToolCallMessage(name, parameters) {
309 if (!this.#tools.has(name)) {309 if (!this.#tools.has(name)) {
310 return `Invoked unknown tool: ${name}`;310 return `Invoked unknown tool: ${name}`;
311 }311 }
@@ -313,7 +313,7 @@ export class ToolManager {
313 try {313 try {
314 const tool = this.#tools.get(name);314 const tool = this.#tools.get(name);
315 const formatParameters = typeof parameters === 'string' ? JSON.parse(parameters) : parameters;315 const formatParameters = typeof parameters === 'string' ? JSON.parse(parameters) : parameters;
316 return tool.formatMessage(formatParameters);316 return await tool.formatMessage(formatParameters);
317 } catch (error) {317 } catch (error) {
318 console.error(`[ToolManager] An error occurred while formatting the tool call message for "${name}":`, error);318 console.error(`[ToolManager] An error occurred while formatting the tool call message for "${name}":`, error);
319 return `Invoking tool: ${name}`;319 return `Invoking tool: ${name}`;
@@ -590,7 +590,7 @@ export class ToolManager {
590 const name = toolCall.function.name;590 const name = toolCall.function.name;
591 const displayName = ToolManager.getDisplayName(name);591 const displayName = ToolManager.getDisplayName(name);
592592
593 const message = ToolManager.formatToolCallMessage(name, parameters);593 const message = await ToolManager.formatToolCallMessage(name, parameters);
594 const toast = message && toastr.info(message, 'Tool Calling', { timeOut: 0 });594 const toast = message && toastr.info(message, 'Tool Calling', { timeOut: 0 });
595 const toolResult = await ToolManager.invokeFunctionTool(name, parameters);595 const toolResult = await ToolManager.invokeFunctionTool(name, parameters);
596 toastr.clear(toast);596 toastr.clear(toast);
@@ -878,6 +878,7 @@ export class ToolManager {
878 parameters: JSON.parse(parameters ?? '{}'),878 parameters: JSON.parse(parameters ?? '{}'),
879 action: actionFunc,879 action: actionFunc,
880 formatMessage: formatMessageFunc,880 formatMessage: formatMessageFunc,
881 shouldRegister: async () => true, // TODO: Implement shouldRegister
881 });882 });
882883
883 return '';884 return '';