Nicely format message for tool calls

2b7c03f3b0c9662471880b25b1ee83ea90f51860

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

2 files changed, +32 -3Showing whitespace changes
public/scripts/tool-calling.js+23 -3
@@ -108,6 +108,7 @@ export class ToolManager {
108108
109 const definition = new ToolDefinition(name, description, parameters, action);109 const definition = new ToolDefinition(name, description, parameters, action);
110 this.#tools.set(name, definition);110 this.#tools.set(name, definition);
111 console.log('[ToolManager] Registered function tool:', definition);
111 }112 }
112113
113 /**114 /**
@@ -121,6 +122,7 @@ export class ToolManager {
121 }122 }
122123
123 this.#tools.delete(name);124 this.#tools.delete(name);
125 console.log(`[ToolManager] Unregistered function tool: ${name}`);
124 }126 }
125127
126 /**128 /**
@@ -333,8 +335,9 @@ export class ToolManager {
333 const parameters = toolCall.function.arguments;335 const parameters = toolCall.function.arguments;
334 const name = toolCall.function.name;336 const name = toolCall.function.name;
335337
336 toastr.info('Invoking function tool: ' + name);338 const toast = toastr.info(`Invoking function tool: ${name}`);
337 const result = await ToolManager.invokeFunctionTool(name, parameters);339 const result = await ToolManager.invokeFunctionTool(name, parameters);
340 toastr.clear(toast);
338 console.log('Function tool result:', result);341 console.log('Function tool result:', result);
339342
340 // Save a successful invocation343 // Save a successful invocation
@@ -375,17 +378,34 @@ export class ToolManager {
375 }378 }
376379
377 /**380 /**
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 /**
378 * Saves function tool invocations to the last user chat message extra metadata.399 * Saves function tool invocations to the last user chat message extra metadata.
379 * @param {ToolInvocation[]} invocations Successful tool invocations400 * @param {ToolInvocation[]} invocations Successful tool invocations
380 */401 */
381 static saveFunctionToolInvocations(invocations) {402 static saveFunctionToolInvocations(invocations) {
382 const toolNames = invocations.map(i => i.name).join(', ');
383 const message = {403 const message = {
384 name: systemUserName,404 name: systemUserName,
385 force_avatar: system_avatar,405 force_avatar: system_avatar,
386 is_system: true,406 is_system: true,
387 is_user: false,407 is_user: false,
388 mes: `Performed tool calls: ${toolNames}`,408 mes: ToolManager.#formatMessage(invocations),
389 extra: {409 extra: {
390 isSmallSys: true,410 isSmallSys: true,
391 tool_invocations: invocations,411 tool_invocations: invocations,
public/style.css+9 -0
@@ -418,6 +418,15 @@ small {
418 text-align: center;418 text-align: center;
419}419}
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
421.mes.smallSysMes .mes_text p:last-child {430.mes.smallSysMes .mes_text p:last-child {
422 margin: 0;431 margin: 0;
423}432}