Add brave as alternative WebSearchTool

This commit is contained in:
Eric-Guo
2026-04-12 10:34:08 +08:00
parent 8399d9ed20
commit 711440474c
9 changed files with 777 additions and 53 deletions

View File

@@ -6,36 +6,42 @@
import { isFirstPartyAnthropicBaseUrl } from '../../../utils/model/providers.js'
import { ApiSearchAdapter } from './apiAdapter.js'
import { BingSearchAdapter } from './bingAdapter.js'
import { BraveSearchAdapter } from './braveAdapter.js'
import type { WebSearchAdapter } from './types.js'
export type { SearchResult, SearchOptions, SearchProgress, WebSearchAdapter } from './types.js'
export type {
SearchResult,
SearchOptions,
SearchProgress,
WebSearchAdapter,
} from './types.js'
let cachedAdapter: WebSearchAdapter | null = null
let cachedAdapterKey: 'api' | 'bing' | 'brave' | null = null
export function createAdapter(): WebSearchAdapter {
// 直接用 bing 适配器,跳过 API 适配器的选择逻辑
return new BingSearchAdapter()
// // Adapter is stateless — safe to reuse across calls within a session
// if (cachedAdapter) return cachedAdapter
const envAdapter = process.env.WEB_SEARCH_ADAPTER
const adapterKey =
envAdapter === 'api' || envAdapter === 'bing' || envAdapter === 'brave'
? envAdapter
: isFirstPartyAnthropicBaseUrl()
? 'api'
: 'bing'
// // Env override: WEB_SEARCH_ADAPTER=api|bing forces specific backend
// const envAdapter = process.env.WEB_SEARCH_ADAPTER
// if (envAdapter === 'api') {
// cachedAdapter = new ApiSearchAdapter()
// return cachedAdapter
// }
// if (envAdapter === 'bing') {
// cachedAdapter = new BingSearchAdapter()
// return cachedAdapter
// }
if (cachedAdapter && cachedAdapterKey === adapterKey) return cachedAdapter
// // Anthropic official URL → API server-side search
// if (isFirstPartyAnthropicBaseUrl()) {
// cachedAdapter = new ApiSearchAdapter()
// return cachedAdapter
// }
if (adapterKey === 'api') {
cachedAdapter = new ApiSearchAdapter()
cachedAdapterKey = 'api'
return cachedAdapter
}
if (adapterKey === 'bing') {
cachedAdapter = new BingSearchAdapter()
cachedAdapterKey = 'bing'
return cachedAdapter
}
// // Third-party proxies / non-Anthropic endpoints → Bing fallback
// cachedAdapter = new BingSearchAdapter()
// return cachedAdapter
cachedAdapter = new BraveSearchAdapter()
cachedAdapterKey = 'brave'
return cachedAdapter
}