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
refactor(llm): extract tool call card to separate file
Elian Doran343e3e67ed31
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsxindex 9ba708e8ff..1a57c3e542 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx@@ -2,13 +2,11 @@ import "./LlmChat.css";import { Marked } from "marked";import { useMemo } from "preact/hooks";--import { Trans } from "react-i18next";import { t } from "../../../services/i18n.js";import utils from "../../../services/utils.js";-import { NewNoteLink } from "../../react/NoteLink.js";import { SanitizedHtml } from "../../react/RawHtml.js";-import { type ContentBlock, getMessageText, type StoredMessage, type ToolCall } from "./llm_chat_types.js";+import { type ContentBlock, getMessageText, type StoredMessage } from "./llm_chat_types.js";+import ToolCallCard from "./ToolCallCard.js";function shortenNumber(n: number): string {if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;@@ -32,113 +30,6 @@ interface Props {isStreaming?: boolean;}-interface ToolCallContext {- /** The primary note the tool operates on or created. */- noteId: string | null;- /** The parent note, shown as "in <parent>" for creation tools. */- parentNoteId: string | null;- /** Plain-text detail (e.g. skill name, search query) when no note ref is available. */- detailText: string | null;-}--/** Try to extract a noteId from the tool call's result JSON. */-function parseResultNoteId(toolCall: ToolCall): string | null {- if (!toolCall.result) return null;- try {- const result = typeof toolCall.result === "string"- ? JSON.parse(toolCall.result)- : toolCall.result;- return result?.noteId || null;- } catch {- return null;- }-}--/** Extract contextual info from a tool call for display in the summary. */-function getToolCallContext(toolCall: ToolCall): ToolCallContext {- const input = toolCall.input;- const parentNoteId = (input?.parentNoteId as string) || null;-- // For creation tools, the created note ID is in the result.- if (parentNoteId) {- const createdNoteId = parseResultNoteId(toolCall);- if (createdNoteId) {- return { noteId: createdNoteId, parentNoteId, detailText: null };- }- }-- const noteId = (input?.noteId as string) || parentNoteId || parseResultNoteId(toolCall);- if (noteId) {- return { noteId, parentNoteId: null, detailText: null };- }-- const detailText = (input?.name ?? input?.query) as string | undefined;- return { noteId: null, parentNoteId: null, detailText: detailText || null };-}--function toolCallIcon(toolCall: ToolCall): string {- if (toolCall.isError) return "bx bx-error-circle";- if (toolCall.result) return "bx bx-check";- return "bx bx-loader-alt bx-spin";-}--function ToolCallCard({ toolCall }: { toolCall: ToolCall }) {- const classes = [- "llm-chat-tool-call-inline",- toolCall.isError && "llm-chat-tool-call-error"- ].filter(Boolean).join(" ");- const { noteId: refNoteId, parentNoteId: refParentId, detailText } = getToolCallContext(toolCall);-- return (- <details className={classes}>- <summary className="llm-chat-tool-call-inline-summary">- <span className={toolCallIcon(toolCall)} />- {t(`llm.tools.${toolCall.toolName}`, { defaultValue: toolCall.toolName })}- {detailText && (- <span className="llm-chat-tool-call-detail">{detailText}</span>- )}- {refNoteId && (- <span className="llm-chat-tool-call-note-ref">- {refParentId ? (- <Trans- i18nKey="llm.tools.note_in_parent"- components={{- Note: <NewNoteLink notePath={refNoteId} showNoteIcon noPreview />,- Parent: <NewNoteLink notePath={refParentId} showNoteIcon noPreview />- } as any}- />- ) : (- <NewNoteLink notePath={refNoteId} showNoteIcon noPreview />- )}- </span>- )}- {toolCall.isError && <span className="llm-chat-tool-call-error-badge">{t("llm_chat.tool_error")}</span>}- </summary>- <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>- </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>- </div>- )}- </div>- </details>- );-}-function renderContentBlocks(blocks: ContentBlock[], isStreaming?: boolean) {return blocks.map((block, idx) => {if (block.type === "text") {diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsxnew file mode 100644index 0000000000..a74e398baa--- /dev/null+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx@@ -0,0 +1,112 @@+import { Trans } from "react-i18next";++import { t } from "../../../services/i18n.js";+import { NewNoteLink } from "../../react/NoteLink.js";+import type { ToolCall } from "./llm_chat_types.js";++interface ToolCallContext {+ /** The primary note the tool operates on or created. */+ noteId: string | null;+ /** The parent note, shown as "in <parent>" for creation tools. */+ parentNoteId: string | null;+ /** Plain-text detail (e.g. skill name, search query) when no note ref is available. */+ detailText: string | null;+}++/** Try to extract a noteId from the tool call's result JSON. */+function parseResultNoteId(toolCall: ToolCall): string | null {+ if (!toolCall.result) return null;+ try {+ const result = typeof toolCall.result === "string"+ ? JSON.parse(toolCall.result)+ : toolCall.result;+ return result?.noteId || null;+ } catch {+ return null;+ }+}++/** Extract contextual info from a tool call for display in the summary. */+function getToolCallContext(toolCall: ToolCall): ToolCallContext {+ const input = toolCall.input;+ const parentNoteId = (input?.parentNoteId as string) || null;++ // For creation tools, the created note ID is in the result.+ if (parentNoteId) {+ const createdNoteId = parseResultNoteId(toolCall);+ if (createdNoteId) {+ return { noteId: createdNoteId, parentNoteId, detailText: null };+ }+ }++ const noteId = (input?.noteId as string) || parentNoteId || parseResultNoteId(toolCall);+ if (noteId) {+ return { noteId, parentNoteId: null, detailText: null };+ }++ const detailText = (input?.name ?? input?.query) as string | undefined;+ return { noteId: null, parentNoteId: null, detailText: detailText || null };+}++function toolCallIcon(toolCall: ToolCall): string {+ if (toolCall.isError) return "bx bx-error-circle";+ if (toolCall.result) return "bx bx-check";+ return "bx bx-loader-alt bx-spin";+}++export default function ToolCallCard({ toolCall }: { toolCall: ToolCall }) {+ const classes = [+ "llm-chat-tool-call-inline",+ toolCall.isError && "llm-chat-tool-call-error"+ ].filter(Boolean).join(" ");+ const { noteId: refNoteId, parentNoteId: refParentId, detailText } = getToolCallContext(toolCall);++ return (+ <details className={classes}>+ <summary className="llm-chat-tool-call-inline-summary">+ <span className={toolCallIcon(toolCall)} />+ {t(`llm.tools.${toolCall.toolName}`, { defaultValue: toolCall.toolName })}+ {detailText && (+ <span className="llm-chat-tool-call-detail">{detailText}</span>+ )}+ {refNoteId && (+ <span className="llm-chat-tool-call-note-ref">+ {refParentId ? (+ <Trans+ i18nKey="llm.tools.note_in_parent"+ components={{+ Note: <NewNoteLink notePath={refNoteId} showNoteIcon noPreview />,+ Parent: <NewNoteLink notePath={refParentId} showNoteIcon noPreview />+ } as any}+ />+ ) : (+ <NewNoteLink notePath={refNoteId} showNoteIcon noPreview />+ )}+ </span>+ )}+ {toolCall.isError && <span className="llm-chat-tool-call-error-badge">{t("llm_chat.tool_error")}</span>}+ </summary>+ <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>+ </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>+ </div>+ )}+ </div>+ </details>+ );+}