mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-12 00:49:14 +08:00
feat: add sanitizeToolsForAnthropic function to clean tool definitions for Anthropic API
This commit is contained in:
parent
e255a992cc
commit
35cfc7c517
@ -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<TextBl
|
||||
content: block.text
|
||||
}))
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize tool definitions for Anthropic API.
|
||||
*
|
||||
* Removes non-standard fields like `input_examples` from tool definitions
|
||||
* that Anthropic's API doesn't support. This prevents validation errors when
|
||||
* tools with extended fields are passed to the Anthropic SDK.
|
||||
*
|
||||
* @param tools - Array of tool definitions from MessageCreateParams
|
||||
* @returns Sanitized tools array with non-standard fields removed
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const sanitizedTools = sanitizeToolsForAnthropic(request.tools)
|
||||
* ```
|
||||
*/
|
||||
export function sanitizeToolsForAnthropic(tools?: MessageCreateParams['tools']): MessageCreateParams['tools'] {
|
||||
if (!tools || tools.length === 0) return tools
|
||||
|
||||
return tools.map((tool) => {
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user