Group tool calls in the result

927c2418e0159cd9c10c041bd0ec534189bd7369

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

1 files changed, +16 -3Ignore whitespace
public/scripts/tool-calling.js+16 -3
@@ -158,7 +158,7 @@ class ToolDefinition {
158 description: this.#description,158 description: this.#description,
159 parameters: this.#parameters,159 parameters: this.#parameters,
160 },160 },
161 toString: function() {161 toString: function () {
162 return `<div><b>${this.function.name}</b></div><div><small>${this.function.description}</small></div><pre class="justifyLeft wordBreakAll"><code class="flex padding5">${JSON.stringify(this.function.parameters, null, 2)}</code></pre><hr>`;162 return `<div><b>${this.function.name}</b></div><div><small>${this.function.description}</small></div><pre class="justifyLeft wordBreakAll"><code class="flex padding5">${JSON.stringify(this.function.parameters, null, 2)}</code></pre><hr>`;
163 },163 },
164 };164 };
@@ -581,6 +581,19 @@ export class ToolManager {
581 }581 }
582582
583 /**583 /**
584 * Groups tool names by count.
585 * @param {string[]} toolNames Tool names
586 * @returns {string} Grouped tool names
587 */
588 static #groupToolNames(toolNames) {
589 const toolCounts = toolNames.reduce((acc, name) => {
590 acc[name] = (acc[name] || 0) + 1;
591 return acc;
592 }, {});
593 return Object.entries(toolCounts).map(([name, count]) => count > 1 ? `${name} (${count})` : name).join(', ');
594 }
595
596 /**
584 * Formats a message with tool invocations.597 * Formats a message with tool invocations.
585 * @param {ToolInvocation[]} invocations Tool invocations.598 * @param {ToolInvocation[]} invocations Tool invocations.
586 * @returns {string} Formatted message with tool invocations.599 * @returns {string} Formatted message with tool invocations.
@@ -597,8 +610,8 @@ export class ToolManager {
597 i.result = tryParse(i.result);610 i.result = tryParse(i.result);
598 });611 });
599 codeElement.textContent = JSON.stringify(data, null, 2);612 codeElement.textContent = JSON.stringify(data, null, 2);
600 const toolNames = data.map(i => i.displayName || i.name).join(', ');613 const toolNames = data.map(i => i.displayName || i.name);
601 summaryElement.textContent = `Tool calls: ${toolNames}`;614 summaryElement.textContent = `Tool calls: ${this.#groupToolNames(toolNames)}`;
602 preElement.append(codeElement);615 preElement.append(codeElement);
603 detailsElement.append(summaryElement, preElement);616 detailsElement.append(summaryElement, preElement);
604 return detailsElement.outerHTML;617 return detailsElement.outerHTML;