Files
simstudioai--sim/apps/sim/tools/exa/research.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

172 lines
4.8 KiB
TypeScript

import { createLogger } from '@sim/logger'
import { sleep } from '@sim/utils/helpers'
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/core/execution-limits'
import type { ExaResearchParams, ExaResearchResponse } from '@/tools/exa/types'
import type { ToolConfig } from '@/tools/types'
const logger = createLogger('ExaResearchTool')
const POLL_INTERVAL_MS = 5000
const MAX_POLL_TIME_MS = DEFAULT_EXECUTION_TIMEOUT_MS
export const researchTool: ToolConfig<ExaResearchParams, ExaResearchResponse> = {
id: 'exa_research',
name: 'Exa Research',
description:
'Perform comprehensive research using AI to generate detailed reports with citations',
version: '1.0.0',
params: {
query: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Research query or topic',
},
model: {
type: 'string',
required: false,
visibility: 'user-only',
description: 'Research model: exa-research-fast, exa-research (default), or exa-research-pro',
},
apiKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'Exa AI API Key',
},
},
request: {
url: 'https://api.exa.ai/research/v1',
method: 'POST',
headers: (params) => ({
'Content-Type': 'application/json',
'x-api-key': params.apiKey,
}),
body: (params) => {
const body: any = {
instructions: params.query,
}
// Add model if specified, otherwise use default
if (params.model) {
body.model = params.model
}
return body
},
},
transformResponse: async (response: Response) => {
const data = await response.json()
return {
success: true,
output: {
taskId: data.researchId,
research: [],
},
}
},
postProcess: async (result, params) => {
if (!result.success) {
return result
}
const taskId = result.output.taskId
logger.info(`Exa research task ${taskId} created, polling for completion...`)
let elapsedTime = 0
while (elapsedTime < MAX_POLL_TIME_MS) {
try {
const statusResponse = await fetch(`https://api.exa.ai/research/v1/${taskId}`, {
method: 'GET',
headers: {
'x-api-key': params.apiKey,
'Content-Type': 'application/json',
},
})
if (!statusResponse.ok) {
throw new Error(`Failed to get task status: ${statusResponse.statusText}`)
}
const taskData = await statusResponse.json()
logger.info(`Exa research task ${taskId} status: ${taskData.status}`)
if (taskData.status === 'completed') {
// The completed response contains output.content (text) and output.parsed (structured data)
const content =
taskData.output?.content || taskData.output?.parsed || 'Research completed successfully'
result.output = {
research: [
{
title: 'Research Complete',
url: '',
summary: typeof content === 'string' ? content : JSON.stringify(content, null, 2),
text: typeof content === 'string' ? content : JSON.stringify(content, null, 2),
publishedDate: undefined,
author: undefined,
score: 1.0,
},
],
}
return result
}
if (taskData.status === 'failed' || taskData.status === 'canceled') {
return {
...result,
success: false,
error: `Research task ${taskData.status}: ${taskData.error || 'Unknown error'}`,
}
}
await sleep(POLL_INTERVAL_MS)
elapsedTime += POLL_INTERVAL_MS
} catch (error: any) {
logger.error('Error polling for research task status:', {
message: error.message || 'Unknown error',
taskId,
})
return {
...result,
success: false,
error: `Error polling for research task status: ${error.message || 'Unknown error'}`,
}
}
}
logger.warn(
`Research task ${taskId} did not complete within the maximum polling time (${MAX_POLL_TIME_MS / 1000}s)`
)
return {
...result,
success: false,
error: `Research task did not complete within the maximum polling time (${MAX_POLL_TIME_MS / 1000}s)`,
}
},
outputs: {
research: {
type: 'array',
description: 'Comprehensive research findings with citations and summaries',
items: {
type: 'object',
properties: {
title: { type: 'string' },
url: { type: 'string' },
summary: { type: 'string' },
text: { type: 'string' },
publishedDate: { type: 'string' },
author: { type: 'string' },
score: { type: 'number' },
},
},
},
},
}