Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): group similar tool calls together

Elian Doranb0c91bd889b4
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
index 1de4319b8d..e139116994 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
@@ -5,6 +5,17 @@
color: var(--muted-text-color);
}
+.llm-chat-tool-call-count {
+ font-weight: 400;
+ color: var(--muted-text-color);
+ margin-left: 0.35rem;
+}
+
+/* Folded group — separate the first nested row from the group header. */
+.llm-chat-tool-call-group[open] > .expandable-section-body > .expandable-section:first-child {
+ border-top: 1px solid var(--main-border-color);
+}
+
.llm-chat-tool-call-note-ref {
font-weight: 400;
margin-left: 0.25rem;
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
index aa766fd1b4..d81b45ab99 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
@@ -51,18 +51,20 @@ function getToolCallContext(toolCall: ToolCall): ToolCallContext {
return { noteId: null, parentNoteId: null, detailText: detailText || null };
}
+function toolNameIcon(toolName: string): string {
+ if (toolName.includes("search")) return "bx bx-search";
+ if (toolName.includes("note")) return "bx bx-note";
+ if (toolName.includes("attribute")) return "bx bx-purchase-tag";
+ if (toolName.includes("attachment")) return "bx bx-paperclip";
+ if (toolName.includes("skill")) return "bx bx-book-open";
+ if (toolName.includes("web")) return "bx bx-globe";
+ return "bx bx-wrench";
+}
+
function toolCallIcon(toolCall: ToolCall): string {
if (toolCall.isError) return "bx bx-error-circle";
if (!toolCall.result) return "bx bx-loader-alt bx-spin";
-
- const name = toolCall.toolName;
- if (name.includes("search")) return "bx bx-search";
- if (name.includes("note")) return "bx bx-note";
- if (name.includes("attribute")) return "bx bx-purchase-tag";
- if (name.includes("attachment")) return "bx bx-paperclip";
- if (name.includes("skill")) return "bx bx-book-open";
- if (name.includes("web")) return "bx bx-globe";
- return "bx bx-wrench";
+ return toolNameIcon(toolCall.toolName);
}
/** Try to parse a JSON string into a structured value. */
@@ -201,12 +203,56 @@ function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {
);
}
+/** Fold a section showing multiple invocations of the same tool under a single header. */
+function ToolCallGroupSection({ toolCalls }: { toolCalls: ToolCall[] }) {
+ const first = toolCalls[0];
+ const anyPending = toolCalls.some(tc => !tc.result);
+ const anyError = toolCalls.some(tc => tc.isError);
+
+ const icon = anyPending ? "bx bx-loader-alt bx-spin" : toolNameIcon(first.toolName);
+ const friendlyName = t(`llm.tools.${first.toolName}`, { defaultValue: first.toolName });
+ const label = (
+ <>
+ {friendlyName}
+ <span className="llm-chat-tool-call-count">×{toolCalls.length}</span>
+ {anyError && <span className="llm-chat-tool-call-error-badge">{t("llm_chat.tool_error")}</span>}
+ </>
+ );
+
+ return (
+ <ExpandableSection icon={icon} label={label} className="llm-chat-tool-call-group">
+ {toolCalls.map((tc, idx) => (
+ <ToolCallSection key={tc.id ?? idx} toolCall={tc} />
+ ))}
+ </ExpandableSection>
+ );
+}
+
+/** Group consecutive tool calls that share the same tool name. Singletons pass through unchanged. */
+function groupByToolName(toolCalls: ToolCall[]): Array<ToolCall | ToolCall[]> {
+ const groups: Array<ToolCall | ToolCall[]> = [];
+ for (const tc of toolCalls) {
+ const last = groups[groups.length - 1];
+ if (Array.isArray(last) && last[0].toolName === tc.toolName) {
+ last.push(tc);
+ } else if (last && !Array.isArray(last) && last.toolName === tc.toolName) {
+ groups[groups.length - 1] = [last, tc];
+ } else {
+ groups.push(tc);
+ }
+ }
+ return groups;
+}
+
/** A card that groups one or more sequential tool calls together. */
export default function ToolCallCard({ toolCalls }: { toolCalls: ToolCall[] }) {
+ const groups = groupByToolName(toolCalls);
return (
<ExpandableCard>
- {toolCalls.map((tc, idx) => (
- <ToolCallSection key={tc.id ?? idx} toolCall={tc} />
+ {groups.map((group, idx) => (
+ Array.isArray(group)
+ ? <ToolCallGroupSection key={idx} toolCalls={group} />
+ : <ToolCallSection key={group.id ?? idx} toolCall={group} />
))}
</ExpandableCard>
);