-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(firecrawl): add parse operation and revert short-input selection style #4340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a9cbb3a
feat(firecrawl): add parse operation and revert short-input selection…
waleedlatif1 5bb89f4
chore(firecrawl): regenerate docs and integrations data for parse
waleedlatif1 29580b3
fix(firecrawl): forward firecrawl error body in parse route response
waleedlatif1 16961cf
fix(firecrawl): add pricing config to parse tool hosting
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { toError } from '@sim/utils/errors' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
| import { checkInternalAuth } from '@/lib/auth/hybrid' | ||
| import { generateRequestId } from '@/lib/core/utils/request' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { RawFileInputSchema } from '@/lib/uploads/utils/file-schemas' | ||
| import { processFilesToUserFiles } from '@/lib/uploads/utils/file-utils' | ||
| import { downloadFileFromStorage } from '@/lib/uploads/utils/file-utils.server' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| const logger = createLogger('FirecrawlParseAPI') | ||
|
|
||
| const FirecrawlParseSchema = z.object({ | ||
| apiKey: z.string().min(1, 'API key is required'), | ||
| file: RawFileInputSchema, | ||
| options: z.record(z.unknown()).optional(), | ||
| }) | ||
|
|
||
| export const POST = withRouteHandler(async (request: NextRequest) => { | ||
| const requestId = generateRequestId() | ||
|
|
||
| try { | ||
| const authResult = await checkInternalAuth(request, { requireWorkflowId: false }) | ||
|
|
||
| if (!authResult.success || !authResult.userId) { | ||
| logger.warn(`[${requestId}] Unauthorized Firecrawl parse attempt`, { | ||
| error: authResult.error || 'Missing userId', | ||
| }) | ||
| return NextResponse.json( | ||
| { success: false, error: authResult.error || 'Unauthorized' }, | ||
| { status: 401 } | ||
| ) | ||
| } | ||
|
|
||
| const body = await request.json() | ||
| const validatedData = FirecrawlParseSchema.parse(body) | ||
|
|
||
| const [userFile] = processFilesToUserFiles([validatedData.file], requestId, logger) | ||
| if (!userFile) { | ||
| return NextResponse.json({ success: false, error: 'File input is required' }, { status: 400 }) | ||
| } | ||
|
|
||
| logger.info(`[${requestId}] Firecrawl parse request`, { | ||
| fileName: userFile.name, | ||
| size: userFile.size, | ||
| }) | ||
|
|
||
| const buffer = await downloadFileFromStorage(userFile, requestId, logger) | ||
|
|
||
| const formData = new FormData() | ||
| const blob = new Blob([new Uint8Array(buffer)], { | ||
| type: userFile.type || 'application/octet-stream', | ||
| }) | ||
| formData.append('file', blob, userFile.name) | ||
|
|
||
| if (validatedData.options && Object.keys(validatedData.options).length > 0) { | ||
| formData.append('options', JSON.stringify(validatedData.options)) | ||
| } | ||
|
|
||
| const firecrawlResponse = await fetch('https://api.firecrawl.dev/v2/parse', { | ||
| method: 'POST', | ||
| headers: { | ||
| Authorization: `Bearer ${validatedData.apiKey}`, | ||
| }, | ||
| body: formData, | ||
| }) | ||
|
|
||
| if (!firecrawlResponse.ok) { | ||
| const errorText = await firecrawlResponse.text() | ||
| logger.error(`[${requestId}] Firecrawl API error:`, errorText) | ||
| return NextResponse.json( | ||
| { | ||
| success: false, | ||
| error: `Firecrawl API error: ${errorText || firecrawlResponse.statusText}`, | ||
| }, | ||
| { status: firecrawlResponse.status } | ||
| ) | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const firecrawlData = await firecrawlResponse.json() | ||
|
|
||
| logger.info(`[${requestId}] Firecrawl parse successful`) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| output: firecrawlData.data ?? firecrawlData, | ||
| }) | ||
| } catch (error) { | ||
| if (error instanceof z.ZodError) { | ||
| logger.warn(`[${requestId}] Invalid request data`, { errors: error.errors }) | ||
| return NextResponse.json( | ||
| { success: false, error: 'Invalid request data', details: error.errors }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| logger.error(`[${requestId}] Error in Firecrawl parse:`, error) | ||
|
|
||
| return NextResponse.json({ success: false, error: toError(error).message }, { status: 500 }) | ||
| } | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.