Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): show a tool call that failed through an agent as failed

Elian Dorana308f524848a
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
index eb676cf62d..0ed190d34c 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
@@ -427,4 +427,18 @@ describe("ToolCallCard", () => {
expect(codeRemoved?.querySelector("pre")?.textContent).toBe("y = 2");
expect(codeRemoved?.querySelector(".markdown-stub")).toBeNull();
});
+
+ it("shows a call as failed from its { error } result even when the provider did not flag it", () => {
+ const target = renderCard([ {
+ id: "1",
+ toolName: "edit_note_content",
+ input: { noteId: "t", edits: [ { oldText: "a", newText: "b" } ] },
+ result: JSON.stringify({ error: "edit_note_content does not support rich-text notes." })
+ } ]);
+ const line = target.querySelector("details.llm-chat-tool-call");
+ expect(line?.classList.contains("llm-chat-tool-call-error")).toBe(true);
+ expect(line?.querySelector(".llm-chat-tool-call-error-badge")).not.toBeNull();
+ expect(line?.querySelector(".llm-chat-tool-call-error-message")?.textContent)
+ .toBe("edit_note_content does not support rich-text notes.");
+ });
});
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 a02f965967..37cd5cf72b 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
@@ -8,7 +8,7 @@ import ActionButton from "../../react/ActionButton.js";
import { NewNoteLink } from "../../react/NoteLink.js";
import { EditNoteContentDiff, isSmallEdit, parseNoteContentEdits } from "./EditNoteContentDiff.js";
import { ExpandableSection } from "./ExpandableCard.js";
-import type { ToolCall } from "./llm_chat_types.js";
+import { isFailedToolCall, type ToolCall } from "./llm_chat_types.js";
import { getToolCallView } from "./ToolCallViews.js";
interface ToolCallContext {
@@ -69,7 +69,7 @@ function toolNameIcon(toolName: string): string {
}
function toolCallIcon(toolCall: ToolCall): string {
- if (toolCall.isError) return "bx bx-error-circle";
+ if (isFailedToolCall(toolCall)) return "bx bx-error-circle";
if (!toolCall.result) return "bx bx-loader-alt bx-spin";
return toolNameIcon(toolCall.toolName);
}
@@ -90,7 +90,7 @@ function getErrorMessage(result: string): string {
/** Build the label content for a tool call section. */
function ToolCallLabel({ toolCall, summary }: { toolCall: ToolCall; summary?: string }) {
const { noteId: refNoteId, parentNoteId: refParentId, detailText } = getToolCallContext(toolCall);
- const hasError = toolCall.isError;
+ const hasError = isFailedToolCall(toolCall);
return (
<>
@@ -126,7 +126,7 @@ function ToolCallLabel({ toolCall, summary }: { toolCall: ToolCall; summary?: st
* result are in the dialog the debug button opens.
*/
function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {
- const hasError = toolCall.isError;
+ const hasError = isFailedToolCall(toolCall);
const isStreamingInput = toolCall.inputStreaming !== undefined;
// The partial JSON of a streaming input does not parse, so the diff waits for the whole input.
@@ -195,7 +195,7 @@ function ToolCallDebugButton({ toolCall }: { toolCall: ToolCall }) {
function ToolCallGroupSection({ toolCalls }: { toolCalls: ToolCall[] }) {
const first = toolCalls[0];
const anyPending = toolCalls.some(tc => !tc.result);
- const anyError = toolCalls.some(tc => tc.isError);
+ const anyError = toolCalls.some(isFailedToolCall);
const icon = anyPending ? "bx bx-loader-alt bx-spin" : toolNameIcon(first.toolName);
const friendlyName = t(`llm.tools.${first.toolName}`, { defaultValue: first.toolName });
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
index bff9a68608..37a4a90341 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -13,7 +13,7 @@ import { useNote } from "../../react/hooks.js";
import { NewNoteLink } from "../../react/NoteLink.js";
import { ReadOnlyTextContent } from "../text/ReadOnlyText.js";
import { renderMarkdown } from "./chat_markdown.js";
-import type { ToolCall } from "./llm_chat_types.js";
+import { isFailedToolCall, type ToolCall } from "./llm_chat_types.js";
const HELP_NOTE_PREFIX = "_help_";
const CONTENT_PREVIEW_SOURCE_LENGTH = 1000;
@@ -26,7 +26,7 @@ export interface ToolCallView {
/** The view of a successful call to a tool that has one, or `null` to show the bare line. */
export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
- if (toolCall.isError) return null;
+ if (isFailedToolCall(toolCall)) return null;
// What a writing tool writes is in its input, so it shows before the result arrives.
if (toolCall.toolName === "create_note") {
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts b/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts
index c540e95141..0ad746d87b 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts
@@ -1,4 +1,10 @@
-import type { LlmCitation, LlmErrorDetails, LlmReasoningEffort, LlmUsage } from "@triliumnext/commons";
+import {
+ isToolErrorResult,
+ type LlmCitation,
+ type LlmErrorDetails,
+ type LlmReasoningEffort,
+ type LlmUsage
+} from "@triliumnext/commons";
export type MessageType = "message" | "error" | "thinking";
@@ -108,6 +114,14 @@ export function trimToFirstUserMessage<T extends { role: string }>(messages: T[]
/**
* Extract tool calls from message content blocks.
*/
+/**
+ * Whether a tool call failed: flagged so by the provider, or answered with the `{ error }` a note tool
+ * returns. The result covers chats saved while a provider did not pass the flag on.
+ */
+export function isFailedToolCall(toolCall: ToolCall): boolean {
+ return !!toolCall.isError || isToolErrorResult(toolCall.result);
+}
+
export function getMessageToolCalls(message: StoredMessage): ToolCall[] {
if (Array.isArray(message.content)) {
return message.content
diff --git a/apps/server/src/services/mcp/mcp_server.spec.ts b/apps/server/src/services/mcp/mcp_server.spec.ts
index 4271a8c4a7..90d82402a4 100644
--- a/apps/server/src/services/mcp/mcp_server.spec.ts
+++ b/apps/server/src/services/mcp/mcp_server.spec.ts
@@ -45,8 +45,11 @@ vi.mock("@triliumnext/core", async (importOriginal) => {
const readExecute = vi.fn(() => ({ ok: "read" }));
const mutateExecute = vi.fn(() => ({ ok: "mutate" }));
+const failExecute = vi.fn(() => ({ error: "Note not found" }));
+
const readRegistry: any = [
- ["read_tool", { description: "r", inputSchema: {}, execute: readExecute }]
+ ["read_tool", { description: "r", inputSchema: {}, execute: readExecute }],
+ ["fail_tool", { description: "f", inputSchema: {}, execute: failExecute }]
];
const mutateRegistry: any = [
["mutate_tool", { description: "m", inputSchema: {}, mutates: true, execute: mutateExecute }]
@@ -76,7 +79,7 @@ describe("createMcpServer", () => {
expect(server).toBeInstanceOf(FakeMcpServer);
expect((server as any).info).toMatchObject({ name: "trilium-notes", version: "9.9.9-test" });
- expect(registered.map((r) => r.name)).toEqual(["read_tool", "mutate_tool"]);
+ expect(registered.map((r) => r.name)).toEqual(["read_tool", "fail_tool", "mutate_tool"]);
expect(resolveToolRegistriesMock).toHaveBeenCalled();
});
@@ -102,4 +105,15 @@ describe("createMcpServer", () => {
expect(mockSql.transactional).toHaveBeenCalledTimes(1);
expect(result).toEqual({ content: [{ type: "text", text: JSON.stringify({ ok: "mutate" }) }] });
});
+
+ it("flags the { error } a tool returns as a failed call", async () => {
+ await createMcpServer();
+ const fail = registered.find((r) => r.name === "fail_tool");
+ expect(fail).toBeDefined();
+
+ expect(fail?.callback({})).toEqual({
+ content: [{ type: "text", text: JSON.stringify({ error: "Note not found" }) }],
+ isError: true
+ });
+ });
});
diff --git a/apps/server/src/services/mcp/mcp_server.ts b/apps/server/src/services/mcp/mcp_server.ts
index 0b40f20ed0..2ab3b60f2d 100644
--- a/apps/server/src/services/mcp/mcp_server.ts
+++ b/apps/server/src/services/mcp/mcp_server.ts
@@ -7,6 +7,7 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
+import { isToolErrorResult } from "@triliumnext/commons";
import { app_info as appInfo } from "@triliumnext/core";
import { cls } from "@triliumnext/core";
@@ -34,7 +35,9 @@ function registerTool(server: McpServer, name: string, def: ToolDefinition) {
: def.execute(args);
});
- return { content: [{ type: "text", text: JSON.stringify(result) }] };
+ const content: CallToolResult["content"] = [{ type: "text", text: JSON.stringify(result) }];
+ // Without `isError`, an agent (and the chat behind it) takes a failure for a success.
+ return isToolErrorResult(result) ? { content, isError: true } : { content };
});
}
diff --git a/packages/commons/src/lib/llm_api.spec.ts b/packages/commons/src/lib/llm_api.spec.ts
index 1028830daa..c2cf2e8525 100644
--- a/packages/commons/src/lib/llm_api.spec.ts
+++ b/packages/commons/src/lib/llm_api.spec.ts
@@ -1,9 +1,21 @@
import { describe, expect, it } from "vitest";
-import { LLM_REASONING_EFFORTS } from "./llm_api.js";
+import { isToolErrorResult, LLM_REASONING_EFFORTS } from "./llm_api.js";
describe("LLM_REASONING_EFFORTS", () => {
it("lists the levels weakest first, since providers pick the nearest level by position", () => {
expect(LLM_REASONING_EFFORTS).toEqual([ "none", "minimal", "low", "medium", "high", "xhigh", "max" ]);
});
});
+
+describe("isToolErrorResult", () => {
+ it("recognizes the { error } a tool returns on failure, as an object or as its JSON", () => {
+ expect(isToolErrorResult({ error: "Note not found" })).toBe(true);
+ expect(isToolErrorResult(JSON.stringify({ error: "Note not found" }))).toBe(true);
+ expect(isToolErrorResult({ noteId: "a" })).toBe(false);
+ expect(isToolErrorResult(JSON.stringify([ { error: "x" } ]))).toBe(false);
+ expect(isToolErrorResult("Sunny")).toBe(false);
+ expect(isToolErrorResult(null)).toBe(false);
+ expect(isToolErrorResult(undefined)).toBe(false);
+ });
+});
diff --git a/packages/commons/src/lib/llm_api.ts b/packages/commons/src/lib/llm_api.ts
index 4cb19f1238..0661882772 100644
--- a/packages/commons/src/lib/llm_api.ts
+++ b/packages/commons/src/lib/llm_api.ts
@@ -207,6 +207,22 @@ export interface LlmErrorDetails {
*/
export type LlmStreamStatus = "starting_agent";
+/**
+ * Whether a tool result is the `{ error }` object the note tools return on failure. Takes the object
+ * itself or its JSON, as a `tool_result` chunk carries it.
+ */
+export function isToolErrorResult(result: unknown): boolean {
+ let value = result;
+ if (typeof value === "string") {
+ try {
+ value = JSON.parse(value);
+ } catch {
+ return false;
+ }
+ }
+ return typeof value === "object" && value !== null && !Array.isArray(value) && "error" in value;
+}
+
/**
* Stream chunk types for real-time SSE updates.
* Defines the protocol between server and client.
diff --git a/packages/trilium-core/src/services/llm/stream.ts b/packages/trilium-core/src/services/llm/stream.ts
index 331c4931fc..2f1dd12629 100644
--- a/packages/trilium-core/src/services/llm/stream.ts
+++ b/packages/trilium-core/src/services/llm/stream.ts
@@ -2,7 +2,7 @@
* Shared streaming utilities for converting AI SDK streams to SSE chunks.
*/
-import type { LlmErrorDetails, LlmStreamChunk } from "@triliumnext/commons";
+import { isToolErrorResult, type LlmErrorDetails, type LlmStreamChunk } from "@triliumnext/commons";
import { APICallError, type LanguageModelUsage } from "ai";
import type { ModelPricing, StreamResult } from "./types.js";
@@ -163,7 +163,7 @@ export async function* streamToChunks(result: StreamResult, options: StreamOptio
case "tool-result": {
const output = part.output;
- const isError = typeof output === "object" && output !== null && "error" in output;
+ const isError = isToolErrorResult(output);
yield {
type: "tool_result",
toolCallId: part.toolCallId,