6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (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 / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
207 lines
5.8 KiB
TypeScript
207 lines
5.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 { AgentParams, AgentResponse } from '@/tools/firecrawl/types'
|
|
import type { ToolConfig } from '@/tools/types'
|
|
|
|
const logger = createLogger('FirecrawlAgentTool')
|
|
|
|
const POLL_INTERVAL_MS = 5000
|
|
const MAX_POLL_TIME_MS = DEFAULT_EXECUTION_TIMEOUT_MS
|
|
|
|
export const agentTool: ToolConfig<AgentParams, AgentResponse> = {
|
|
id: 'firecrawl_agent',
|
|
name: 'Firecrawl Agent',
|
|
description:
|
|
'Autonomous web data extraction agent. Searches and gathers information based on natural language prompts without requiring specific URLs.',
|
|
version: '1.0.0',
|
|
|
|
params: {
|
|
prompt: {
|
|
type: 'string',
|
|
required: true,
|
|
visibility: 'user-or-llm',
|
|
description: 'Natural language description of the data to extract (max 10,000 characters)',
|
|
},
|
|
urls: {
|
|
type: 'json',
|
|
required: false,
|
|
visibility: 'user-or-llm',
|
|
description:
|
|
'Optional array of URLs to focus the agent on (e.g., ["https://example.com", "https://docs.example.com"])',
|
|
},
|
|
schema: {
|
|
type: 'json',
|
|
required: false,
|
|
visibility: 'user-or-llm',
|
|
description: 'JSON Schema defining the structure of data to extract',
|
|
},
|
|
maxCredits: {
|
|
type: 'number',
|
|
required: false,
|
|
visibility: 'user-only',
|
|
description: 'Maximum credits to spend on this agent task',
|
|
},
|
|
strictConstrainToURLs: {
|
|
type: 'boolean',
|
|
required: false,
|
|
visibility: 'user-only',
|
|
description: 'If true, agent will only visit URLs provided in the urls array',
|
|
},
|
|
apiKey: {
|
|
type: 'string',
|
|
required: true,
|
|
visibility: 'user-only',
|
|
description: 'Firecrawl API key',
|
|
},
|
|
},
|
|
|
|
request: {
|
|
modelInput: {
|
|
mode: 'project',
|
|
select: (params) => ({ prompt: params.prompt, schema: params.schema }),
|
|
},
|
|
method: 'POST',
|
|
url: 'https://api.firecrawl.dev/v2/agent',
|
|
headers: (params) => ({
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${params.apiKey}`,
|
|
}),
|
|
body: (params) => {
|
|
const body: Record<string, any> = {
|
|
prompt: params.prompt,
|
|
}
|
|
|
|
if (params.urls) {
|
|
if (Array.isArray(params.urls)) {
|
|
body.urls = params.urls
|
|
} else if (typeof params.urls === 'string') {
|
|
try {
|
|
const parsed = JSON.parse(params.urls)
|
|
body.urls = Array.isArray(parsed) ? parsed : [parsed]
|
|
} catch {
|
|
body.urls = [params.urls]
|
|
}
|
|
}
|
|
}
|
|
if (params.schema) body.schema = params.schema
|
|
if (params.maxCredits) body.maxCredits = Number(params.maxCredits)
|
|
if (typeof params.strictConstrainToURLs === 'boolean')
|
|
body.strictConstrainToURLs = params.strictConstrainToURLs
|
|
|
|
return body
|
|
},
|
|
},
|
|
|
|
transformResponse: async (response: Response) => {
|
|
const data = await response.json()
|
|
|
|
return {
|
|
success: true,
|
|
output: {
|
|
jobId: data.id,
|
|
success: false,
|
|
status: 'processing',
|
|
data: {},
|
|
},
|
|
}
|
|
},
|
|
|
|
postProcess: async (result, params) => {
|
|
if (!result.success) {
|
|
return result
|
|
}
|
|
|
|
const jobId = result.output.jobId
|
|
logger.info(`Firecrawl agent job ${jobId} created, polling for completion...`)
|
|
|
|
let elapsedTime = 0
|
|
|
|
while (elapsedTime < MAX_POLL_TIME_MS) {
|
|
try {
|
|
const statusResponse = await fetch(`https://api.firecrawl.dev/v2/agent/${jobId}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
Authorization: `Bearer ${params.apiKey}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
})
|
|
|
|
if (!statusResponse.ok) {
|
|
throw new Error(`Failed to get agent status: ${statusResponse.statusText}`)
|
|
}
|
|
|
|
const agentData = await statusResponse.json()
|
|
logger.info(`Firecrawl agent job ${jobId} status: ${agentData.status}`)
|
|
|
|
if (agentData.status === 'completed') {
|
|
result.output = {
|
|
jobId,
|
|
success: true,
|
|
status: 'completed',
|
|
data: agentData.data || {},
|
|
creditsUsed: agentData.creditsUsed,
|
|
expiresAt: agentData.expiresAt,
|
|
sources: agentData.sources,
|
|
}
|
|
return result
|
|
}
|
|
|
|
if (agentData.status === 'failed') {
|
|
return {
|
|
...result,
|
|
success: false,
|
|
error: `Agent job failed: ${agentData.error || 'Unknown error'}`,
|
|
}
|
|
}
|
|
|
|
await sleep(POLL_INTERVAL_MS)
|
|
elapsedTime += POLL_INTERVAL_MS
|
|
} catch (error: any) {
|
|
logger.error('Error polling for agent job status:', {
|
|
message: error.message || 'Unknown error',
|
|
jobId,
|
|
})
|
|
|
|
return {
|
|
...result,
|
|
success: false,
|
|
error: `Error polling for agent job status: ${error.message || 'Unknown error'}`,
|
|
}
|
|
}
|
|
}
|
|
|
|
logger.warn(
|
|
`Agent job ${jobId} did not complete within the maximum polling time (${MAX_POLL_TIME_MS / 1000}s)`
|
|
)
|
|
return {
|
|
...result,
|
|
success: false,
|
|
error: `Agent job did not complete within the maximum polling time (${MAX_POLL_TIME_MS / 1000}s)`,
|
|
}
|
|
},
|
|
|
|
outputs: {
|
|
success: {
|
|
type: 'boolean',
|
|
description: 'Whether the agent operation was successful',
|
|
},
|
|
status: {
|
|
type: 'string',
|
|
description: 'Current status of the agent job (processing, completed, failed)',
|
|
},
|
|
data: {
|
|
type: 'object',
|
|
description: 'Extracted data from the agent',
|
|
},
|
|
expiresAt: {
|
|
type: 'string',
|
|
description: 'Timestamp when the results expire (24 hours)',
|
|
},
|
|
sources: {
|
|
type: 'object',
|
|
description: 'Array of source URLs used by the agent',
|
|
},
|
|
},
|
|
}
|