Nicely format message for tool calls

2b7c03f3b0c9662471880b25b1ee83ea90f51860

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

2 files changed, +32 -3Ignore whitespace
public/scripts/tool-calling.js+23 -3
@@ -108,6 +108,7 @@ export class ToolManager {
108108
109109 const definition = new ToolDefinition(name, description, parameters, action);
110110 this.#tools.set(name, definition);
111+ console.log('[ToolManager] Registered function tool:', definition);
111112 }
112113
113114 /**
@@ -121,6 +122,7 @@ export class ToolManager {
121122 }
122123
123124 this.#tools.delete(name);
125+ console.log(`[ToolManager] Unregistered function tool: ${name}`);
124126 }
125127
126128 /**
@@ -333,8 +335,9 @@ export class ToolManager {
333335 const parameters = toolCall.function.arguments;
334336 const name = toolCall.function.name;
335337
336338 const toast = toastr.info('`Invoking function tool: ' + ${name}`);
337339 const result = await ToolManager.invokeFunctionTool(name, parameters);
340+ toastr.clear(toast);
338341 console.log('Function tool result:', result);
339342
340343 // Save a successful invocation
@@ -375,17 +378,34 @@ export class ToolManager {
375378 }
376379
377380 /**
381+ * Formats a message with tool invocations.
382+ * @param {ToolInvocation[]} invocations Tool invocations.
383+ * @returns {string} Formatted message with tool invocations.
384+ */
385+ static #formatMessage(invocations) {
386+ const toolNames = invocations.map(i => i.name).join(', ');
387+ const detailsElement = document.createElement('details');
388+ const summaryElement = document.createElement('summary');
389+ const preElement = document.createElement('pre');
390+ const codeElement = document.createElement('code');
391+ codeElement.textContent = JSON.stringify(invocations, null, 2);
392+ summaryElement.textContent = `Performed tool calls: ${toolNames}`;
393+ preElement.append(codeElement);
394+ detailsElement.append(summaryElement, preElement);
395+ return detailsElement.outerHTML;
396+ }
397+
398+ /**
378399 * Saves function tool invocations to the last user chat message extra metadata.
379400 * @param {ToolInvocation[]} invocations Successful tool invocations
380401 */
381402 static saveFunctionToolInvocations(invocations) {
382- const toolNames = invocations.map(i => i.name).join(', ');
383403 const message = {
384404 name: systemUserName,
385405 force_avatar: system_avatar,
386406 is_system: true,
387407 is_user: false,
388- mes: `Performed tool calls: ${toolNames}`,
408+ mes: ToolManager.#formatMessage(invocations),
389409 extra: {
390410 isSmallSys: true,
391411 tool_invocations: invocations,
public/style.css+9 -0
@@ -418,6 +418,15 @@ small {
418418 text-align: center;
419419}
420420
421+.mes.smallSysMes pre {
422+ text-align: initial;
423+ word-break: break-all;
424+}
425+
426+.mes.smallSysMes summary {
427+ cursor: pointer;
428+}
429+
421430.mes.smallSysMes .mes_text p:last-child {
422431 margin: 0;
423432}