Tool Calling: Make formatMessage async

This commit is contained in:
Cohee 2024-10-09 03:56:24 +03:00
parent bc0f5bf4ce
commit c49138dfea

View File

@ -31,8 +31,8 @@ import { slashCommandReturnHelper } from './slash-commands/SlashCommandReturnHel
* @property {string} description - A description of the tool. * @property {string} description - A description of the tool.
* @property {object} parameters - The parameters for the tool. * @property {object} parameters - The parameters for the tool.
* @property {function} action - The action to perform when the tool is invoked. * @property {function} action - The action to perform when the tool is invoked.
* @property {function} formatMessage - A function to format the tool call message. * @property {function} [formatMessage] - A function to format the tool call message.
* @property {function} shouldRegister - A function to determine if the tool should be registered. * @property {function} [shouldRegister] - A function to determine if the tool should be registered.
*/ */
/** /**
@ -194,11 +194,11 @@ class ToolDefinition {
/** /**
* Formats a message with the tool invocation. * Formats a message with the tool invocation.
* @param {object} parameters The parameters to pass to the tool. * @param {object} parameters The parameters to pass to the tool.
* @returns {string} The formatted message. * @returns {Promise<string>} The formatted message.
*/ */
formatMessage(parameters) { async formatMessage(parameters) {
return typeof this.#formatMessage === 'function' return typeof this.#formatMessage === 'function'
? this.#formatMessage(parameters) ? await this.#formatMessage(parameters)
: `Invoking tool: ${this.#displayName || this.#name}`; : `Invoking tool: ${this.#displayName || this.#name}`;
} }
@ -303,9 +303,9 @@ export class ToolManager {
* Formats a message for a tool call by name. * Formats a message for a tool call by name.
* @param {string} name The name of the tool to format the message for. * @param {string} name The name of the tool to format the message for.
* @param {object} parameters Function tool call parameters. * @param {object} parameters Function tool call parameters.
* @returns {string} The formatted message for the tool call. * @returns {Promise<string>} The formatted message for the tool call.
*/ */
static formatToolCallMessage(name, parameters) { static async formatToolCallMessage(name, parameters) {
if (!this.#tools.has(name)) { if (!this.#tools.has(name)) {
return `Invoked unknown tool: ${name}`; return `Invoked unknown tool: ${name}`;
} }
@ -313,7 +313,7 @@ export class ToolManager {
try { try {
const tool = this.#tools.get(name); const tool = this.#tools.get(name);
const formatParameters = typeof parameters === 'string' ? JSON.parse(parameters) : parameters; const formatParameters = typeof parameters === 'string' ? JSON.parse(parameters) : parameters;
return tool.formatMessage(formatParameters); return await tool.formatMessage(formatParameters);
} catch (error) { } catch (error) {
console.error(`[ToolManager] An error occurred while formatting the tool call message for "${name}":`, error); console.error(`[ToolManager] An error occurred while formatting the tool call message for "${name}":`, error);
return `Invoking tool: ${name}`; return `Invoking tool: ${name}`;
@ -590,7 +590,7 @@ export class ToolManager {
const name = toolCall.function.name; const name = toolCall.function.name;
const displayName = ToolManager.getDisplayName(name); const displayName = ToolManager.getDisplayName(name);
const message = ToolManager.formatToolCallMessage(name, parameters); const message = await ToolManager.formatToolCallMessage(name, parameters);
const toast = message && toastr.info(message, 'Tool Calling', { timeOut: 0 }); const toast = message && toastr.info(message, 'Tool Calling', { timeOut: 0 });
const toolResult = await ToolManager.invokeFunctionTool(name, parameters); const toolResult = await ToolManager.invokeFunctionTool(name, parameters);
toastr.clear(toast); toastr.clear(toast);
@ -878,6 +878,7 @@ export class ToolManager {
parameters: JSON.parse(parameters ?? '{}'), parameters: JSON.parse(parameters ?? '{}'),
action: actionFunc, action: actionFunc,
formatMessage: formatMessageFunc, formatMessage: formatMessageFunc,
shouldRegister: async () => true, // TODO: Implement shouldRegister
}); });
return ''; return '';