diff --git a/package.json b/package.json index 29c4f4d155..a7436a1f18 100644 --- a/package.json +++ b/package.json @@ -110,7 +110,7 @@ "@agentic/searxng": "^7.3.3", "@agentic/tavily": "^7.3.3", "@ant-design/v5-patch-for-react-19": "^1.0.3", - "@anthropic-ai/sdk": "^0.38.0", + "@anthropic-ai/sdk": "^0.41.0", "@electron-toolkit/eslint-config-prettier": "^3.0.0", "@electron-toolkit/eslint-config-ts": "^3.0.0", "@electron-toolkit/preload": "^3.0.0", diff --git a/src/renderer/src/config/models.ts b/src/renderer/src/config/models.ts index 42073b43e3..65ed25a04f 100644 --- a/src/renderer/src/config/models.ts +++ b/src/renderer/src/config/models.ts @@ -230,6 +230,12 @@ export const FUNCTION_CALLING_REGEX = new RegExp( `\\b(?!(?:${FUNCTION_CALLING_EXCLUDED_MODELS.join('|')})\\b)(?:${FUNCTION_CALLING_MODELS.join('|')})\\b`, 'i' ) + +export const CLAUDE_SUPPORTED_WEBSEARCH_REGEX = new RegExp( + `\\b(?:claude-3(-|\\.)(7|5)-sonnet(?:-[\\w-]+)|claude-3(-|\\.)5-haiku(?:-[\\w-]+))\\b`, + 'i' +) + export function isFunctionCallingModel(model: Model): boolean { if (model.type?.includes('function_calling')) { return true @@ -2399,6 +2405,10 @@ export function isWebSearchModel(model: Model): boolean { return false } + if (model.id.includes('claude')) { + return CLAUDE_SUPPORTED_WEBSEARCH_REGEX.test(model.id) + } + if (provider.type === 'openai') { if ( isOpenAILLMModel(model) && diff --git a/src/renderer/src/providers/AiProvider/AnthropicProvider.ts b/src/renderer/src/providers/AiProvider/AnthropicProvider.ts index 4402186050..d113d7f532 100644 --- a/src/renderer/src/providers/AiProvider/AnthropicProvider.ts +++ b/src/renderer/src/providers/AiProvider/AnthropicProvider.ts @@ -1,7 +1,15 @@ import Anthropic from '@anthropic-ai/sdk' -import { MessageCreateParamsNonStreaming, MessageParam, TextBlockParam } from '@anthropic-ai/sdk/resources' +import { + MessageCreateParamsNonStreaming, + MessageParam, + TextBlockParam, + ToolUnion, + WebSearchResultBlock, + WebSearchTool20250305, + WebSearchToolResultError +} from '@anthropic-ai/sdk/resources' import { DEFAULT_MAX_TOKENS } from '@renderer/config/constant' -import { isReasoningModel, isVisionModel } from '@renderer/config/models' +import { isReasoningModel, isVisionModel, isWebSearchModel } from '@renderer/config/models' import { getStoreSetting } from '@renderer/hooks/useSettings' import i18n from '@renderer/i18n' import { getAssistantSettings, getDefaultModel, getTopNamingModel } from '@renderer/services/AssistantService' @@ -11,7 +19,16 @@ import { filterEmptyMessages, filterUserRoleStartMessages } from '@renderer/services/MessagesService' -import { Assistant, EFFORT_RATIO, FileTypes, MCPToolResponse, Model, Provider, Suggestion } from '@renderer/types' +import { + Assistant, + EFFORT_RATIO, + FileTypes, + MCPToolResponse, + Model, + Provider, + Suggestion, + WebSearchSource +} from '@renderer/types' import { ChunkType } from '@renderer/types/chunk' import type { Message } from '@renderer/types/newMessage' import { removeSpecialCharactersForTopicName } from '@renderer/utils' @@ -109,6 +126,18 @@ export default class AnthropicProvider extends BaseProvider { } } + private async getWebSearchParams(model: Model): Promise { + if (!isWebSearchModel(model)) { + return undefined + } + + return { + type: 'web_search_20250305', + name: 'web_search', + max_uses: 5 + } as WebSearchTool20250305 + } + /** * Get the temperature * @param assistant - The assistant @@ -201,6 +230,17 @@ export default class AnthropicProvider extends BaseProvider { } } + const isEnabledBuiltinWebSearch = assistant.enableWebSearch + + const tools: ToolUnion[] = [] + + if (isEnabledBuiltinWebSearch) { + const webSearchTool = await this.getWebSearchParams(model) + if (webSearchTool) { + tools.push(webSearchTool) + } + } + const body: MessageCreateParamsNonStreaming = { model: model.id, messages: userMessages, @@ -211,6 +251,7 @@ export default class AnthropicProvider extends BaseProvider { system: systemMessage ? [systemMessage] : undefined, // @ts-ignore thinking thinking: this.getBudgetToken(assistant, model), + tools: tools, ...this.getCustomParameters(assistant) } @@ -289,6 +330,34 @@ export default class AnthropicProvider extends BaseProvider { onChunk({ type: ChunkType.TEXT_DELTA, text }) }) + .on('contentBlock', (block) => { + if (block.type === 'server_tool_use' && block.name === 'web_search') { + onChunk({ + type: ChunkType.LLM_WEB_SEARCH_IN_PROGRESS + }) + } else if (block.type === 'web_search_tool_result') { + if ( + block.content && + (block.content as WebSearchToolResultError).type === 'web_search_tool_result_error' + ) { + onChunk({ + type: ChunkType.ERROR, + error: { + code: (block.content as WebSearchToolResultError).error_code, + message: (block.content as WebSearchToolResultError).error_code + } + }) + } else { + onChunk({ + type: ChunkType.LLM_WEB_SEARCH_COMPLETE, + llm_web_search: { + results: block.content as Array, + source: WebSearchSource.ANTHROPIC + } + }) + } + } + }) .on('thinking', (thinking) => { hasThinkingContent = true const currentTime = new Date().getTime() // Get current time for each chunk diff --git a/src/renderer/src/store/messageBlock.ts b/src/renderer/src/store/messageBlock.ts index db5ec338f8..4814da8306 100644 --- a/src/renderer/src/store/messageBlock.ts +++ b/src/renderer/src/store/messageBlock.ts @@ -1,3 +1,4 @@ +import { WebSearchResultBlock } from '@anthropic-ai/sdk/resources' import type { GroundingMetadata } from '@google/genai' import { createEntityAdapter, createSelector, createSlice, type PayloadAction } from '@reduxjs/toolkit' import type { Citation } from '@renderer/pages/home/Messages/CitationsList' @@ -136,6 +137,26 @@ const formatCitationsFromBlock = (block: CitationMessageBlock | undefined): Cita } }) || [] break + case WebSearchSource.ANTHROPIC: + formattedCitations = + (block.response.results as Array)?.map((result, index) => { + const {url} = result + let hostname: string | undefined + try { + hostname = new URL(url).hostname + } catch { + hostname = url + } + return { + number: index + 1, + url: url, + title: result.title, + hostname: hostname, + showFavicon: true, + type: 'websearch' + } + }) || [] + break case WebSearchSource.OPENROUTER: case WebSearchSource.PERPLEXITY: formattedCitations = diff --git a/src/renderer/src/store/thunk/messageThunk.ts b/src/renderer/src/store/thunk/messageThunk.ts index 8094a5ff29..ac285298b7 100644 --- a/src/renderer/src/store/thunk/messageThunk.ts +++ b/src/renderer/src/store/thunk/messageThunk.ts @@ -483,6 +483,12 @@ const fetchAndProcessAssistantResponseImpl = async ( console.error('[onExternalToolComplete] citationBlockId is null. Cannot update.') } }, + onLLMWebSearchInProgress: () => { + const citationBlock = createCitationBlock(assistantMsgId, {}, { status: MessageBlockStatus.PROCESSING }) + citationBlockId = citationBlock.id + handleBlockTransition(citationBlock, MessageBlockType.CITATION) + saveUpdatedBlockToDB(citationBlock.id, assistantMsgId, topicId, getState) + }, onLLMWebSearchComplete(llmWebSearchResult) { if (citationBlockId) { const changes: Partial = { diff --git a/src/renderer/src/types/index.ts b/src/renderer/src/types/index.ts index fdc02d35f7..0bece1576f 100644 --- a/src/renderer/src/types/index.ts +++ b/src/renderer/src/types/index.ts @@ -1,3 +1,4 @@ +import type { WebSearchResultBlock } from '@anthropic-ai/sdk/resources' import type { GroundingMetadata } from '@google/genai' import type OpenAI from 'openai' import React from 'react' @@ -450,6 +451,7 @@ export type WebSearchResults = | GroundingMetadata | OpenAI.Chat.Completions.ChatCompletionMessage.Annotation.URLCitation[] | OpenAI.Responses.ResponseOutputText.URLCitation[] + | WebSearchResultBlock[] | any[] export enum WebSearchSource { @@ -457,6 +459,7 @@ export enum WebSearchSource { OPENAI = 'openai', OPENAI_COMPATIBLE = 'openai-compatible', OPENROUTER = 'openrouter', + ANTHROPIC = 'anthropic', GEMINI = 'gemini', PERPLEXITY = 'perplexity', QWEN = 'qwen', diff --git a/yarn.lock b/yarn.lock index 58b49f553e..0cf53dba53 100644 --- a/yarn.lock +++ b/yarn.lock @@ -176,9 +176,9 @@ __metadata: languageName: node linkType: hard -"@anthropic-ai/sdk@npm:^0.38.0": - version: 0.38.0 - resolution: "@anthropic-ai/sdk@npm:0.38.0" +"@anthropic-ai/sdk@npm:^0.41.0": + version: 0.41.0 + resolution: "@anthropic-ai/sdk@npm:0.41.0" dependencies: "@types/node": "npm:^18.11.18" "@types/node-fetch": "npm:^2.6.4" @@ -187,7 +187,7 @@ __metadata: form-data-encoder: "npm:1.7.2" formdata-node: "npm:^4.3.2" node-fetch: "npm:^2.6.7" - checksum: 10c0/accd003cbe314d32d4d36f5fd7fd743c32e2a896c9ea57190966eda20b8c46e00f542bf03ec3603d1274a7ac18e902bed4158ff5980e4e248a6d5c75e3fd891a + checksum: 10c0/b57df42df33f29f3baa682da663d7411624f511384f84c790a936b505c5074d9676749ce05d45b1caa9af7077a20013889efb65f3e25f979e7ab2fa245e2a444 languageName: node linkType: hard @@ -1488,13 +1488,6 @@ __metadata: languageName: node linkType: hard -"@google/generative-ai@npm:^0.24.1": - version: 0.24.1 - resolution: "@google/generative-ai@npm:0.24.1" - checksum: 10c0/8da77fc648b04fc2ecef53e75230e2ee67a8fd29a34b6b8874e77a7332b2a1e4b51d44dd9eb604fb063ed8ea46d293aac5b1e2a955ae2435e2582a265f2cb80d - languageName: node - linkType: hard - "@hello-pangea/dnd@npm:^16.6.0": version: 16.6.0 resolution: "@hello-pangea/dnd@npm:16.6.0" @@ -4378,7 +4371,7 @@ __metadata: "@agentic/searxng": "npm:^7.3.3" "@agentic/tavily": "npm:^7.3.3" "@ant-design/v5-patch-for-react-19": "npm:^1.0.3" - "@anthropic-ai/sdk": "npm:^0.38.0" + "@anthropic-ai/sdk": "npm:^0.41.0" "@cherrystudio/embedjs": "npm:^0.1.28" "@cherrystudio/embedjs-libsql": "npm:^0.1.28" "@cherrystudio/embedjs-loader-csv": "npm:^0.1.28"