Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): basic nesting support

Elian Dorane54cb9c6268f
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
index 20d1290756..5e040c8a01 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
@@ -601,6 +601,8 @@
.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;
+ max-height: 300px;
+ overflow: auto;
}
.llm-chat-tool-call-inline-body .llm-chat-tool-call-result {
@@ -612,11 +614,8 @@
padding: 0.5rem;
background: var(--main-background-color);
border-radius: 4px;
- overflow-x: auto;
font-size: 0.8rem;
font-family: var(--monospace-font-family, monospace);
- max-height: 200px;
- overflow-y: auto;
}
.llm-chat-tool-call-inline-body strong {
@@ -629,6 +628,7 @@
/* Tool call key-value table */
.llm-chat-tool-call-table {
width: 100%;
+ table-layout: auto;
border-collapse: collapse;
font-size: 0.8rem;
background: var(--main-background-color);
@@ -650,7 +650,6 @@
font-weight: 600;
white-space: nowrap;
color: var(--muted-text-color);
- width: 1%;
}
.llm-chat-tool-call-table-value pre {
@@ -659,8 +658,26 @@
background: none;
white-space: pre-wrap;
word-break: break-word;
- max-height: 150px;
- overflow-y: auto;
+}
+
+/* Nested tables */
+.llm-chat-tool-call-table-value .llm-chat-tool-call-table {
+ background: none;
+ width: auto;
+ min-width: 100%;
+}
+
+.llm-chat-tool-call-table-array {
+ display: flex;
+ flex-direction: column;
+}
+
+.llm-chat-tool-call-table-array > .llm-chat-tool-call-table {
+ background: none;
+}
+
+.llm-chat-tool-call-table-array > .llm-chat-tool-call-table + .llm-chat-tool-call-table {
+ border-top: 1px solid var(--main-border-color);
}
/* 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.tsx
index e7eff4d59e..6cc983a2b2 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
@@ -54,40 +54,67 @@ 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);
+/** Try to parse a JSON string into a structured value. */
+function tryParseJson(data: unknown): unknown {
+ if (typeof data === "string") {
+ try {
+ return JSON.parse(data);
+ } catch {
+ return data;
+ }
+ }
+ return data;
}
-/** 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;
+/** Check if a value is a plain object (not null, not array). */
+function isPlainObject(value: unknown): value is Record<string, unknown> {
+ return typeof value === "object" && value !== null && !Array.isArray(value);
+}
- let obj = data;
- if (typeof obj === "string") {
- try {
- obj = JSON.parse(obj);
- } catch {
- return null;
+const MAX_TABLE_DEPTH = 2;
+
+/** Render a single value — recurse for objects/arrays up to max depth. */
+function ValueCell({ value, depth }: { value: unknown; depth: number }) {
+ if (value === null || value === undefined) return <pre />;
+
+ // Beyond max depth, fall back to JSON.
+ if (depth >= MAX_TABLE_DEPTH) {
+ if (isPlainObject(value) || Array.isArray(value)) {
+ return <pre>{JSON.stringify(value, null, 2)}</pre>;
}
+ return <pre>{String(value)}</pre>;
+ }
+
+ if (isPlainObject(value)) {
+ return <KeyValueTable data={value} depth={depth} />;
}
- if (typeof obj === "object" && obj !== null && !Array.isArray(obj)) {
- return obj as Record<string, unknown>;
+ if (Array.isArray(value)) {
+ if (value.length === 0) return <pre>{"[]"}</pre>;
+
+ // Array of objects: render each as a nested table.
+ if (value.every(isPlainObject)) {
+ return (
+ <div className="llm-chat-tool-call-table-array">
+ {value.map((item, idx) => (
+ <KeyValueTable key={idx} data={item} depth={depth} />
+ ))}
+ </div>
+ );
+ }
+
+ // Array of primitives: comma-separated.
+ return <pre>{value.map(String).join(", ")}</pre>;
}
- return null;
+ return <pre>{String(value)}</pre>;
}
-/** Renders a key-value data object as a compact two-column table. */
-function KeyValueTable({ data, className }: { data: unknown; className?: string }) {
- const record = parseData(data);
+/** Renders a data object as a recursive two-column key-value table. */
+function KeyValueTable({ data, className, depth = 0 }: { data: unknown; className?: string; depth?: number }) {
+ const obj = tryParseJson(data);
- // Fall back to raw display for non-object data (arrays, plain strings).
- if (!record) {
+ if (!isPlainObject(obj)) {
const raw = typeof data === "string" ? data : JSON.stringify(data, null, 2);
return <pre className={className}>{raw}</pre>;
}
@@ -95,11 +122,11 @@ function KeyValueTable({ data, className }: { data: unknown; className?: string
return (
<table className={`llm-chat-tool-call-table ${className ?? ""}`}>
<tbody>
- {Object.entries(record).map(([key, value]) => (
+ {Object.entries(obj).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>
+ <ValueCell value={value} depth={depth + 1} />
</td>
</tr>
))}