11import { anthropic } from '@ai-sdk/anthropic'
22import { google } from '@ai-sdk/google'
33import { openai } from '@ai-sdk/openai'
4+ import { xai } from '@ai-sdk/xai'
45import type { InferToolInput , InferToolOutput } from 'ai'
56import { type Tool } from 'ai'
67
7- import { createOpenRouterOptions , createXaiOptions , mergeProviderOptions } from '../../../options'
8- import type { ProviderOptionsMap } from '../../../options/types'
8+ import { createOpenRouterOptions , mergeProviderOptions } from '../../../options'
99import type { AiRequestContext } from '../../'
1010import type { OpenRouterSearchConfig } from './openrouter'
1111
@@ -16,14 +16,17 @@ export type OpenAISearchConfig = NonNullable<Parameters<typeof openai.tools.webS
1616export type OpenAISearchPreviewConfig = NonNullable < Parameters < typeof openai . tools . webSearchPreview > [ 0 ] >
1717export type AnthropicSearchConfig = NonNullable < Parameters < typeof anthropic . tools . webSearch_20250305 > [ 0 ] >
1818export type GoogleSearchConfig = NonNullable < Parameters < typeof google . tools . googleSearch > [ 0 ] >
19- export type XAISearchConfig = NonNullable < ProviderOptionsMap [ 'xai' ] [ 'searchParameters' ] >
19+ export type XAIWebSearchConfig = NonNullable < Parameters < typeof xai . tools . webSearch > [ 0 ] >
20+ export type XAIXSearchConfig = NonNullable < Parameters < typeof xai . tools . xSearch > [ 0 ] >
2021
2122type NormalizeTool < T > = T extends Tool < infer INPUT , infer OUTPUT > ? Tool < INPUT , OUTPUT > : Tool < any , any >
2223
2324type AnthropicWebSearchTool = NormalizeTool < ReturnType < typeof anthropic . tools . webSearch_20250305 > >
2425type OpenAIWebSearchTool = NormalizeTool < ReturnType < typeof openai . tools . webSearch > >
2526type OpenAIChatWebSearchTool = NormalizeTool < ReturnType < typeof openai . tools . webSearchPreview > >
2627type GoogleWebSearchTool = NormalizeTool < ReturnType < typeof google . tools . googleSearch > >
28+ type XAIWebSearchTool = NormalizeTool < ReturnType < typeof xai . tools . webSearch > >
29+ type XAIXSearchTool = NormalizeTool < ReturnType < typeof xai . tools . xSearch > >
2730
2831/**
2932 * 插件初始化时接收的完整配置对象
@@ -34,7 +37,8 @@ export interface WebSearchPluginConfig {
3437 openai ?: OpenAISearchConfig
3538 'openai-chat' ?: OpenAISearchPreviewConfig
3639 anthropic ?: AnthropicSearchConfig
37- xai ?: ProviderOptionsMap [ 'xai' ] [ 'searchParameters' ]
40+ xai ?: XAIWebSearchConfig
41+ 'xai-xsearch' ?: XAIXSearchConfig
3842 google ?: GoogleSearchConfig
3943 openrouter ?: OpenRouterSearchConfig
4044}
@@ -47,10 +51,10 @@ export const DEFAULT_WEB_SEARCH_CONFIG: WebSearchPluginConfig = {
4751 openai : { } ,
4852 'openai-chat' : { } ,
4953 xai : {
50- mode : 'on' ,
51- returnCitations : true ,
52- maxSearchResults : 5 ,
53- sources : [ { type : 'web' } , { type : 'x' } , { type : 'news' } ]
54+ enableImageUnderstanding : true
55+ } ,
56+ 'xai-xsearch' : {
57+ enableImageUnderstanding : true
5458 } ,
5559 anthropic : {
5660 maxUses : 5
@@ -87,13 +91,18 @@ export type WebSearchToolOutputSchema = {
8791 web ?: { uri : string ; title : string }
8892 } >
8993 }
94+ // xAI 工具
95+ xai : InferToolOutput < XAIWebSearchTool >
96+ 'xai-xsearch' : InferToolOutput < XAIXSearchTool >
9097}
9198
9299export type WebSearchToolInputSchema = {
93100 anthropic : InferToolInput < AnthropicWebSearchTool >
94101 openai : InferToolInput < OpenAIWebSearchTool >
95102 google : InferToolInput < GoogleWebSearchTool >
96103 'openai-chat' : InferToolInput < OpenAIChatWebSearchTool >
104+ xai : InferToolInput < XAIWebSearchTool >
105+ 'xai-xsearch' : InferToolInput < XAIXSearchTool >
97106}
98107
99108/**
@@ -141,8 +150,9 @@ export const switchWebSearchTool = (config: WebSearchPluginConfig, params: any,
141150 } ,
142151 xai : ( ) => {
143152 const cfg = config . xai ?? DEFAULT_WEB_SEARCH_CONFIG . xai
144- const searchOptions = createXaiOptions ( { searchParameters : { ...cfg , mode : 'on' } } )
145- applyProviderOptionsSearch ( params , searchOptions )
153+ applyToolBasedSearch ( params , 'web_search' , xai . tools . webSearch ( cfg ) )
154+ const xSearchCfg = config [ 'xai-xsearch' ] ?? DEFAULT_WEB_SEARCH_CONFIG [ 'xai-xsearch' ]
155+ applyToolBasedSearch ( params , 'x_search' , xai . tools . xSearch ( xSearchCfg ) )
146156 } ,
147157 openrouter : ( ) => {
148158 const cfg = ( config . openrouter ?? DEFAULT_WEB_SEARCH_CONFIG . openrouter ) as OpenRouterSearchConfig
0 commit comments