From 35cfc7c517ab9fa3de085bdb6a5f38282716919e Mon Sep 17 00:00:00 2001 From: suyao Date: Sat, 29 Nov 2025 19:12:56 +0800 Subject: [PATCH] feat: add sanitizeToolsForAnthropic function to clean tool definitions for Anthropic API --- packages/shared/anthropic/index.ts | 30 ++++++++++++++++++++++++- src/main/apiServer/services/messages.ts | 5 +++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/shared/anthropic/index.ts b/packages/shared/anthropic/index.ts index 2444ad6113..e2113eb749 100644 --- a/packages/shared/anthropic/index.ts +++ b/packages/shared/anthropic/index.ts @@ -9,7 +9,7 @@ */ import Anthropic from '@anthropic-ai/sdk' -import type { TextBlockParam } from '@anthropic-ai/sdk/resources' +import type { MessageCreateParams, TextBlockParam, Tool as AnthropicTool } from '@anthropic-ai/sdk/resources' import { loggerService } from '@logger' import type { Provider } from '@types' import type { ModelMessage } from 'ai' @@ -193,3 +193,31 @@ export function buildClaudeCodeSystemModelMessage(system?: string | Array { + if ('type' in tool && tool.type !== 'custom') return tool + + // oxlint-disable-next-line no-unused-vars + const { input_examples, ...sanitizedTool } = tool as AnthropicTool & { input_examples?: unknown } + + return sanitizedTool as typeof tool + }) +} diff --git a/src/main/apiServer/services/messages.ts b/src/main/apiServer/services/messages.ts index e2c9ad24e2..957c066520 100644 --- a/src/main/apiServer/services/messages.ts +++ b/src/main/apiServer/services/messages.ts @@ -2,7 +2,7 @@ import type Anthropic from '@anthropic-ai/sdk' import type { MessageCreateParams, MessageStreamEvent } from '@anthropic-ai/sdk/resources' import { loggerService } from '@logger' import anthropicService from '@main/services/AnthropicService' -import { buildClaudeCodeSystemMessage, getSdkClient } from '@shared/anthropic' +import { buildClaudeCodeSystemMessage, getSdkClient, sanitizeToolsForAnthropic } from '@shared/anthropic' import type { Provider } from '@types' import { APICallError, RetryError } from 'ai' import { net } from 'electron' @@ -148,7 +148,8 @@ export class MessagesService { createAnthropicRequest(request: MessageCreateParams, provider: Provider, modelId?: string): MessageCreateParams { const anthropicRequest: MessageCreateParams = { ...request, - stream: !!request.stream + stream: !!request.stream, + tools: sanitizeToolsForAnthropic(request.tools) } // Override model if provided