Files
simstudioai--sim/apps/sim/lib/copilot/tools/server/other/search-online.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

124 lines
3.4 KiB
TypeScript

import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { SearchOnline } from '@/lib/copilot/generated/tool-catalog-v1'
import type { BaseServerTool } from '@/lib/copilot/tools/server/base-tool'
import { env } from '@/lib/core/config/env'
import { executeTool } from '@/tools'
interface OnlineSearchParams {
query: string
num?: number
type?: string
gl?: string
hl?: string
}
interface SearchResult {
title: string
link: string
snippet: string
date?: string
position?: number
}
interface SearchResponse {
results: SearchResult[]
query: string
type: string
totalResults: number
source: 'exa' | 'serper'
}
export const searchOnlineServerTool: BaseServerTool<OnlineSearchParams, SearchResponse> = {
name: SearchOnline.id,
async execute(params: OnlineSearchParams): Promise<SearchResponse> {
const logger = createLogger('SearchOnlineServerTool')
const { query, num = 10, type = 'search', gl, hl } = params
if (!query || typeof query !== 'string') throw new Error('query is required')
const hasExaApiKey = Boolean(env.EXA_API_KEY && String(env.EXA_API_KEY).length > 0)
const hasSerperApiKey = Boolean(env.SERPER_API_KEY && String(env.SERPER_API_KEY).length > 0)
logger.debug('Performing online search', { queryLength: query.length, num, type })
// Try Exa first if available
if (hasExaApiKey) {
try {
const exaResult = await executeTool('exa_search', {
query,
numResults: num,
type: 'auto',
apiKey: env.EXA_API_KEY ?? '',
})
const output = exaResult.output as
| {
results?: Array<{
title?: string
url?: string
text?: string
summary?: string
publishedDate?: string
}>
}
| undefined
const exaResults = output?.results ?? []
if (exaResult.success && exaResults.length > 0) {
const transformedResults: SearchResult[] = exaResults.map((result, index) => ({
title: result.title ?? '',
link: result.url ?? '',
snippet: result.text ?? result.summary ?? '',
date: result.publishedDate,
position: index + 1,
}))
return {
results: transformedResults,
query,
type,
totalResults: transformedResults.length,
source: 'exa',
}
}
logger.debug('exa_search returned no results, falling back to Serper')
} catch (exaError) {
logger.warn('exa_search failed, falling back to Serper', {
error: toError(exaError).message,
})
}
}
if (!hasSerperApiKey) {
throw new Error('No search API keys available (EXA_API_KEY or SERPER_API_KEY required)')
}
const toolParams = {
query,
num,
type,
gl,
hl,
apiKey: env.SERPER_API_KEY ?? '',
}
const result = await executeTool('serper_search', toolParams)
const output = result.output as { searchResults?: SearchResult[] } | undefined
const results = output?.searchResults ?? []
if (!result.success) {
const errorMsg = (result as { error?: string }).error ?? 'Search failed'
throw new Error(errorMsg)
}
return {
results,
query,
type,
totalResults: results.length,
source: 'serper',
}
},
}