Update tool registration

5cf64a2613252cc9ab8557fe145fab7546ea56ec

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

1 files changed, +89 -11Showing whitespace changes
public/scripts/tool-calling.js+89 -11
@@ -5,6 +5,7 @@ import { Popup } from './popup.js';
55/**
66 * @typedef {object} ToolInvocation
77 * @property {string} id - A unique identifier for the tool invocation.
8+ * @property {string} displayName - The display name of the tool.
89 * @property {string} name - The name of the tool.
910 * @property {string} parameters - The parameters for the tool invocation.
1011 * @property {string} result - The result of the tool invocation.
@@ -28,6 +29,12 @@ class ToolDefinition {
2829 #name;
2930
3031 /**
32+ * A user-friendly display name for the tool.
33+ * @type {string}
34+ */
35+ #displayName;
36+
37+ /**
3138 * A description of what the tool does.
3239 * @type {string}
3340 */
@@ -46,17 +53,27 @@ class ToolDefinition {
4653 #action;
4754
4855 /**
56+ * A function that will be called to format the tool call toast.
57+ * @type {function}
58+ */
59+ #formatMessage;
60+
61+ /**
4962 * Creates a new ToolDefinition.
5063 * @param {string} name A unique name for the tool.
64+ * @param {string} displayName A user-friendly display name for the tool.
5165 * @param {string} description A description of what the tool does.
5266 * @param {object} parameters A JSON schema for the parameters that the tool accepts.
5367 * @param {function} action A function that will be called when the tool is executed.
68+ * @param {function} formatMessage A function that will be called to format the tool call toast.
5469 */
5570 constructor(name, displayName, description, parameters, action, formatMessage) {
5671 this.#name = name;
72+ this.#displayName = displayName;
5773 this.#description = description;
5874 this.#parameters = parameters;
5975 this.#action = action;
76+ this.#formatMessage = formatMessage;
6077 }
6178
6279 /**
@@ -82,6 +99,21 @@ class ToolDefinition {
8299 async invoke(parameters) {
83100 return await this.#action(parameters);
84101 }
102+
103+ /**
104+ * Formats a message with the tool invocation.
105+ * @param {object} parameters The parameters to pass to the tool.
106+ * @returns {string} The formatted message.
107+ */
108+ formatMessage(parameters) {
109+ return typeof this.#formatMessage === 'function'
110+ ? this.#formatMessage(parameters)
111+ : `Invoking tool: ${this.#displayName || this.#name}`;
112+ }
113+
114+ get displayName() {
115+ return this.#displayName;
116+ }
85117}
86118
87119/**
@@ -104,17 +136,25 @@ export class ToolManager {
104136
105137 /**
106138 * Registers a new tool with the tool registry.
107139 * @param {stringobject} nametool The name oftool theto toolregister.
108140 * @param {string} descriptiontool.name AThe descriptionname of what the tool does.
109141 * @param {objectstring} parameterstool.displayName A JSON schema foruser-friendly thedisplay parametersname thatfor the tool accepts.
110142 * @param {functionstring} actiontool.description A function that will bedescription calledof whenwhat the tool is executeddoes.
143+ * @param {object} tool.parameters A JSON schema for the parameters that the tool accepts.
144+ * @param {function} tool.action A function that will be called when the tool is executed.
145+ * @param {function} tool.formatMessage A function that will be called to format the tool call toast.
111146 */
112147 static registerFunctionTool({ name, displayName, description, parameters, action, formatMessage }) {
148+ // Convert WIP arguments
149+ if (typeof arguments[0] !== 'object') {
150+ [name, description, parameters, action] = arguments;
151+ }
152+
113153 if (this.#tools.has(name)) {
114154 console.warn(`A tool with the name "${name}" has already been registered. The definition will be overwritten.`);
115155 }
116156
117157 const definition = new ToolDefinition(name, displayName, description, parameters, action, formatMessage);
118158 this.#tools.set(name, definition);
119159 console.log('[ToolManager] Registered function tool:', definition);
120160 }
@@ -161,6 +201,35 @@ export class ToolManager {
161201 }
162202 }
163203
204+ static formatToolCallMessage(name, parameters) {
205+ if (!this.#tools.has(name)) {
206+ return `Invoked unknown tool: ${name}`;
207+ }
208+
209+ try {
210+ const tool = this.#tools.get(name);
211+ const formatParameters = typeof parameters === 'string' ? JSON.parse(parameters) : parameters;
212+ return tool.formatMessage(formatParameters);
213+ } catch (error) {
214+ console.error(`An error occurred while formatting the tool call message for "${name}":`, error);
215+ return `Invoking tool: ${name}`;
216+ }
217+ }
218+
219+ /**
220+ * Gets the display name of a tool by name.
221+ * @param {string} name
222+ * @returns {string} The display name of the tool.
223+ */
224+ static getDisplayName(name) {
225+ if (!this.#tools.has(name)) {
226+ return name;
227+ }
228+
229+ const tool = this.#tools.get(name);
230+ return tool.displayName || name;
231+ }
232+
164233 /**
165234 * Register function tools for the next chat completion request.
166235 * @param {object} data Generation data
@@ -352,9 +421,11 @@ export class ToolManager {
352421 const id = toolCall.id;
353422 const parameters = toolCall.function.arguments;
354423 const name = toolCall.function.name;
424+ const displayName = ToolManager.getDisplayName(name);
355425 result.hadToolCalls = true;
356426
357427 const toastmessage = toastrToolManager.infoformatToolCallMessage(`Invoking function tool: ${name}`, parameters);
428+ const toast = message && toastr.info(message, 'Tool Calling', { timeOut: 0 });
358429 const toolResult = await ToolManager.invokeFunctionTool(name, parameters);
359430 toastr.clear(toast);
360431 console.log('Function tool result:', result);
@@ -365,7 +436,14 @@ export class ToolManager {
365436 continue;
366437 }
367438
368- result.invocations.push({ id, name, parameters, result: toolResult });
439+ const invocation = {
440+ id,
441+ displayName,
442+ name,
443+ parameters,
444+ result: toolResult,
445+ };
446+ result.invocations.push(invocation);
369447 }
370448 }
371449
@@ -414,8 +492,8 @@ export class ToolManager {
414492 codeElement.classList.add('language-json');
415493 data.forEach(i => i.parameters = tryParse(i.parameters));
416494 codeElement.textContent = JSON.stringify(data, null, 2);
417495 const toolNames = data.map(i => i.displayName || i.name).join(', ');
418496 summaryElement.textContent = `Performed toolTool calls: ${toolNames}`;
419497 preElement.append(codeElement);
420498 detailsElement.append(summaryElement, preElement);
421499 return detailsElement.outerHTML;