Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(llm): display tool calls as table
Elian Doran208330d73a8b
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.cssindex 8c4b78f2f4..20d1290756 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css@@ -595,7 +595,16 @@}.llm-chat-tool-call-inline-body {- padding: 0 0.75rem 0.75rem;+ padding: 0;+}++.llm-chat-tool-call-inline-body .llm-chat-tool-call-input,+.llm-chat-tool-call-inline-body .llm-chat-tool-call-result {+ padding: 0.5rem 0.75rem;+}++.llm-chat-tool-call-inline-body .llm-chat-tool-call-result {+ border-top: 1px solid var(--main-border-color);}.llm-chat-tool-call-inline-body pre {@@ -617,8 +626,41 @@margin-bottom: 0.25rem;}-.llm-chat-tool-call-inline-body .llm-chat-tool-call-result {- margin-top: 0.5rem;+/* Tool call key-value table */+.llm-chat-tool-call-table {+ width: 100%;+ border-collapse: collapse;+ font-size: 0.8rem;+ background: var(--main-background-color);+ border-radius: 4px;+ overflow: hidden;+}++.llm-chat-tool-call-table td {+ padding: 0.25rem 0.5rem;+ vertical-align: top;+ border-bottom: 1px solid var(--main-border-color);+}++.llm-chat-tool-call-table tr:last-child td {+ border-bottom: none;+}++.llm-chat-tool-call-table-key {+ font-weight: 600;+ white-space: nowrap;+ color: var(--muted-text-color);+ width: 1%;+}++.llm-chat-tool-call-table-value pre {+ margin: 0;+ padding: 0;+ background: none;+ white-space: pre-wrap;+ word-break: break-word;+ max-height: 150px;+ overflow-y: auto;}/* Tool call error styling */diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsxindex a74e398baa..e7eff4d59e 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx@@ -54,6 +54,60 @@ function toolCallIcon(toolCall: ToolCall): string {return "bx bx-loader-alt bx-spin";}+/** Format a value for display in the key-value table. */+function formatValue(value: unknown): string {+ if (value === null || value === undefined) return "";+ if (typeof value === "string") return value;+ if (typeof value === "number" || typeof value === "boolean") return String(value);+ return JSON.stringify(value, null, 2);+}++/** Parse a JSON value (string or object) into a flat key-value record. */+function parseData(data: unknown): Record<string, unknown> | null {+ if (!data) return null;++ let obj = data;+ if (typeof obj === "string") {+ try {+ obj = JSON.parse(obj);+ } catch {+ return null;+ }+ }++ if (typeof obj === "object" && obj !== null && !Array.isArray(obj)) {+ return obj as Record<string, unknown>;+ }++ return null;+}++/** Renders a key-value data object as a compact two-column table. */+function KeyValueTable({ data, className }: { data: unknown; className?: string }) {+ const record = parseData(data);++ // Fall back to raw display for non-object data (arrays, plain strings).+ if (!record) {+ const raw = typeof data === "string" ? data : JSON.stringify(data, null, 2);+ return <pre className={className}>{raw}</pre>;+ }++ return (+ <table className={`llm-chat-tool-call-table ${className ?? ""}`}>+ <tbody>+ {Object.entries(record).map(([key, value]) => (+ <tr key={key}>+ <td className="llm-chat-tool-call-table-key">{key}</td>+ <td className="llm-chat-tool-call-table-value">+ <pre>{formatValue(value)}</pre>+ </td>+ </tr>+ ))}+ </tbody>+ </table>+ );+}+export default function ToolCallCard({ toolCall }: { toolCall: ToolCall }) {const classes = ["llm-chat-tool-call-inline",@@ -89,21 +143,12 @@ export default function ToolCallCard({ toolCall }: { toolCall: ToolCall }) {<div className="llm-chat-tool-call-inline-body"><div className="llm-chat-tool-call-input"><strong>{t("llm_chat.input")}:</strong>- <pre>{JSON.stringify(toolCall.input, null, 2)}</pre>+ <KeyValueTable data={toolCall.input} /></div>{toolCall.result && (<div className={`llm-chat-tool-call-result ${toolCall.isError ? "llm-chat-tool-call-result-error" : ""}`}><strong>{toolCall.isError ? t("llm_chat.error") : t("llm_chat.result")}:</strong>- <pre>{(() => {- if (typeof toolCall.result === "string" && (toolCall.result.startsWith("{") || toolCall.result.startsWith("["))) {- try {- return JSON.stringify(JSON.parse(toolCall.result), null, 2);- } catch {- return toolCall.result;- }- }- return toolCall.result;- })()}</pre>+ <KeyValueTable data={toolCall.result} /></div>)}</div>