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
fix(llm): show Claude Code's web tools as web search and page reads
Elian Doranf6293e53c5c4
diff --git a/apps/server/src/services/llm/providers/claude_agent.spec.ts b/apps/server/src/services/llm/providers/claude_agent.spec.tsindex e08f475894..b1477e3c03 100644--- a/apps/server/src/services/llm/providers/claude_agent.spec.ts+++ b/apps/server/src/services/llm/providers/claude_agent.spec.ts@@ -158,6 +158,51 @@ describe("ClaudeAgentProvider.chatChunks", () => {closeMock.mockClear();});+ it("names Claude Code's web tools the way the other providers name theirs", async () => {+ const webCall = (id: string, name: string, input: Record<string, unknown>) => [+ {+ type: "stream_event",+ parent_tool_use_id: null,+ session_id: "sess-1",+ event: { type: "content_block_start", index: 0, content_block: { type: "tool_use", id, name } }+ },+ {+ type: "assistant",+ parent_tool_use_id: null,+ session_id: "sess-1",+ message: { content: [{ type: "tool_use", id, name, input }] }+ },+ {+ type: "user",+ parent_tool_use_id: null,+ session_id: "sess-1",+ message: { content: [{ type: "tool_result", tool_use_id: id, content: "found" }] }+ }+ ];+ scriptAgent([+ { type: "system", subtype: "init", session_id: "sess-1" },+ ...webCall("toolu_s", "WebSearch", { query: "trilium" }),+ ...webCall("toolu_f", "WebFetch", { url: "https://triliumnotes.org", prompt: "Summarize" }),+ successResult()+ ]);++ const provider = new ClaudeAgentProvider();+ const chunks = await collect(provider.chatChunks([{ role: "user", content: "hi" }], { enableWebSearch: true }));++ expect(chunks.filter(c => c.type.startsWith("tool_")).map(c => [c.type, "toolName" in c ? c.toolName : undefined])).toEqual([+ ["tool_input_start", "web_search"],+ ["tool_use", "web_search"],+ ["tool_result", "web_search"],+ ["tool_input_start", "read_web_page"],+ ["tool_use", "read_web_page"],+ ["tool_result", "read_web_page"]+ ]);+ expect(chunks).toContainEqual({+ type: "tool_use", toolCallId: "toolu_f", toolName: "read_web_page",+ toolInput: { url: "https://triliumnotes.org", prompt: "Summarize" }+ });+ });+it("maps stream events, tool calls, results, and usage to chunks in order", async () => {scriptAgent([{ type: "system", subtype: "init", session_id: "sess-1" },diff --git a/apps/server/src/services/llm/providers/claude_agent.ts b/apps/server/src/services/llm/providers/claude_agent.tsindex 2ce0681b2b..4028b14a10 100644--- a/apps/server/src/services/llm/providers/claude_agent.ts+++ b/apps/server/src/services/llm/providers/claude_agent.ts@@ -1009,11 +1009,20 @@ async function pushUserTurn(input: Pushable<SDKUserMessage>, prompt: string | As});}-/** Strip the MCP prefix so the client shows "search_notes", not "mcp__trilium__search_notes". */+/**+ * The name the client knows a tool by: Trilium's tools without their MCP prefix ("search_notes", not+ * "mcp__trilium__search_notes"), and Claude Code's web tools under the names the other providers give+ * theirs. Their inputs already carry the `query` and `url` the chat shows.+ */function friendlyToolName(name: string): string {- return name.replace(/^mcp__trilium__/, "");+ return BUILTIN_TOOL_NAMES[name] ?? name.replace(/^mcp__trilium__/, "");}+const BUILTIN_TOOL_NAMES: Record<string, string> = {+ WebSearch: "web_search",+ WebFetch: "read_web_page"+};+function flattenToolResult(content: unknown): string {if (typeof content === "string") {return content;