From 927c2418e0159cd9c10c041bd0ec534189bd7369 Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Sun, 6 Oct 2024 20:19:58 +0300
Subject: [PATCH] Group tool calls in the result
---
public/scripts/tool-calling.js | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/public/scripts/tool-calling.js b/public/scripts/tool-calling.js
index 2e5c0a65c..26ae26091 100644
--- a/public/scripts/tool-calling.js
+++ b/public/scripts/tool-calling.js
@@ -158,7 +158,7 @@ class ToolDefinition {
description: this.#description,
parameters: this.#parameters,
},
- toString: function() {
+ toString: function () {
return `
${this.function.name}
${this.function.description}
${JSON.stringify(this.function.parameters, null, 2)}
`;
},
};
@@ -580,6 +580,19 @@ export class ToolManager {
return result;
}
+ /**
+ * Groups tool names by count.
+ * @param {string[]} toolNames Tool names
+ * @returns {string} Grouped tool names
+ */
+ static #groupToolNames(toolNames) {
+ const toolCounts = toolNames.reduce((acc, name) => {
+ acc[name] = (acc[name] || 0) + 1;
+ return acc;
+ }, {});
+ return Object.entries(toolCounts).map(([name, count]) => count > 1 ? `${name} (${count})` : name).join(', ');
+ }
+
/**
* Formats a message with tool invocations.
* @param {ToolInvocation[]} invocations Tool invocations.
@@ -597,8 +610,8 @@ export class ToolManager {
i.result = tryParse(i.result);
});
codeElement.textContent = JSON.stringify(data, null, 2);
- const toolNames = data.map(i => i.displayName || i.name).join(', ');
- summaryElement.textContent = `Tool calls: ${toolNames}`;
+ const toolNames = data.map(i => i.displayName || i.name);
+ summaryElement.textContent = `Tool calls: ${this.#groupToolNames(toolNames)}`;
preElement.append(codeElement);
detailsElement.append(summaryElement, preElement);
return detailsElement.outerHTML;