From d4001e45286f0a50b6df4a8a2955f18822273978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20M=C3=B3ricz?= Date: Thu, 29 Aug 2024 20:01:00 +0200 Subject: [PATCH 1/9] fix(api/websocket): fix auth and termination --- apps/api/src/controllers/auth.ts | 3 ++- apps/api/src/controllers/v1/crawl-status-ws.ts | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts index 0aee6db0..5df6a857 100644 --- a/apps/api/src/controllers/auth.ts +++ b/apps/api/src/controllers/auth.ts @@ -104,7 +104,8 @@ export async function supaAuthenticateUser( status?: number; plan?: PlanType; }> { - const authHeader = req.headers.authorization; + console.log(req.headers); + const authHeader = req.headers.authorization ?? (req.headers["sec-websocket-protocol"] ? `Bearer ${req.headers["sec-websocket-protocol"]}` : null); if (!authHeader) { return { success: false, error: "Unauthorized", status: 401 }; } diff --git a/apps/api/src/controllers/v1/crawl-status-ws.ts b/apps/api/src/controllers/v1/crawl-status-ws.ts index 551948de..8d823096 100644 --- a/apps/api/src/controllers/v1/crawl-status-ws.ts +++ b/apps/api/src/controllers/v1/crawl-status-ws.ts @@ -85,6 +85,8 @@ async function crawlStatusWS(ws: WebSocket, req: RequestWithAuth Date: Thu, 29 Aug 2024 20:01:16 +0200 Subject: [PATCH 2/9] feat(js-sdk): add crawlUrlAndWatch --- apps/js-sdk/example.js | 16 +++ apps/js-sdk/example.ts | 16 +++ apps/js-sdk/firecrawl/package-lock.json | 46 ++++++++- apps/js-sdk/firecrawl/package.json | 4 +- apps/js-sdk/firecrawl/src/index.ts | 129 +++++++++++++++++++++++- 5 files changed, 206 insertions(+), 5 deletions(-) diff --git a/apps/js-sdk/example.js b/apps/js-sdk/example.js index b4ee7747..5698a017 100644 --- a/apps/js-sdk/example.js +++ b/apps/js-sdk/example.js @@ -29,5 +29,21 @@ if (job.data) { console.log(job.data[0].markdown); } +// Map a website: const mapResult = await app.map('https://firecrawl.dev'); console.log(mapResult) + +// Crawl a website with WebSockets: +const watch = await app.crawlUrlAndWatch('mendable.ai', { excludePaths: ['blog/*'], limit: 5}); + +watch.addEventListener("document", doc => { + console.log("DOC", doc.detail); +}); + +watch.addEventListener("error", err => { + console.error("ERR", err.detail.error); +}); + +watch.addEventListener("done", state => { + console.log("DONE", state.detail.status); +}); diff --git a/apps/js-sdk/example.ts b/apps/js-sdk/example.ts index f8d7d5d9..80589f5a 100644 --- a/apps/js-sdk/example.ts +++ b/apps/js-sdk/example.ts @@ -32,8 +32,24 @@ const main = async () => { console.log(checkStatus.data[0].markdown); } + // Map a website: const mapResult = await app.mapUrl('https://firecrawl.dev'); console.log(mapResult) + + // Crawl a website with WebSockets: + const watch = await app.crawlUrlAndWatch('mendable.ai', { excludePaths: ['blog/*'], limit: 5}); + + watch.addEventListener("document", doc => { + console.log("DOC", doc.detail); + }); + + watch.addEventListener("error", err => { + console.error("ERR", err.detail.error); + }); + + watch.addEventListener("done", state => { + console.log("DONE", state.detail.status); + }); } main() \ No newline at end of file diff --git a/apps/js-sdk/firecrawl/package-lock.json b/apps/js-sdk/firecrawl/package-lock.json index 4d9254ac..7f25babc 100644 --- a/apps/js-sdk/firecrawl/package-lock.json +++ b/apps/js-sdk/firecrawl/package-lock.json @@ -1,16 +1,18 @@ { "name": "@mendable/firecrawl-js", - "version": "0.0.36", + "version": "1.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@mendable/firecrawl-js", - "version": "0.0.36", + "version": "1.0.3", "license": "MIT", "dependencies": { "axios": "^1.6.8", "dotenv": "^16.4.5", + "isows": "^1.0.4", + "typescript-event-target": "^1.1.1", "uuid": "^9.0.1", "zod": "^3.23.8", "zod-to-json-schema": "^3.23.0" @@ -2137,6 +2139,20 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "node_modules/isows": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/isows/-/isows-1.0.4.tgz", + "integrity": "sha512-hEzjY+x9u9hPmBom9IIAqdJCwNLax+xrPb51vEPpERoFlIxgmZcHzsT5jKG06nvInKOBGvReAVz80Umed5CczQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/wagmi-dev" + } + ], + "peerDependencies": { + "ws": "*" + } + }, "node_modules/istanbul-lib-coverage": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz", @@ -3733,6 +3749,11 @@ "node": ">=14.17" } }, + "node_modules/typescript-event-target": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/typescript-event-target/-/typescript-event-target-1.1.1.tgz", + "integrity": "sha512-dFSOFBKV6uwaloBCCUhxlD3Pr/P1a/tJdcmPrTXCHlEFD3faj0mztjcGn6VBAhQ0/Bdy8K3VWrrqwbt/ffsYsg==" + }, "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", @@ -3855,6 +3876,27 @@ "node": "^12.13.0 || ^14.15.0 || >=16.0.0" } }, + "node_modules/ws": { + "version": "8.18.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", + "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==", + "peer": true, + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/y18n": { "version": "5.0.8", "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", diff --git a/apps/js-sdk/firecrawl/package.json b/apps/js-sdk/firecrawl/package.json index 6eb37a22..cadd1eaf 100644 --- a/apps/js-sdk/firecrawl/package.json +++ b/apps/js-sdk/firecrawl/package.json @@ -1,6 +1,6 @@ { "name": "@mendable/firecrawl-js", - "version": "1.0.3", + "version": "1.0.4", "description": "JavaScript SDK for Firecrawl API", "main": "build/cjs/index.js", "types": "types/index.d.ts", @@ -30,6 +30,8 @@ "dependencies": { "axios": "^1.6.8", "dotenv": "^16.4.5", + "isows": "^1.0.4", + "typescript-event-target": "^1.1.1", "uuid": "^9.0.1", "zod": "^3.23.8", "zod-to-json-schema": "^3.23.0" diff --git a/apps/js-sdk/firecrawl/src/index.ts b/apps/js-sdk/firecrawl/src/index.ts index cb2a0e4f..7f4eb1e4 100644 --- a/apps/js-sdk/firecrawl/src/index.ts +++ b/apps/js-sdk/firecrawl/src/index.ts @@ -1,6 +1,8 @@ import axios, { AxiosResponse, AxiosRequestHeaders } from "axios"; import { z } from "zod"; import { zodToJsonSchema } from "zod-to-json-schema"; +import { WebSocket } from "isows"; +import { TypedEventTarget } from "typescript-event-target"; /** * Configuration interface for FirecrawlApp. @@ -315,8 +317,8 @@ export interface SearchResponseV0 { * Provides methods for scraping, searching, crawling, and mapping web content. */ export default class FirecrawlApp { - private apiKey: string; - private apiUrl: string; + public apiKey: string; + public apiUrl: string; public version: T; /** @@ -561,6 +563,21 @@ export default class FirecrawlApp { } as this['version'] extends 'v0' ? CrawlStatusResponseV0 : CrawlStatusResponse); } + async crawlUrlAndWatch( + url: string, + params?: this['version'] extends 'v0' ? CrawlParamsV0 : CrawlParams, + idempotencyKey?: string, + ) { + if (this.version === 'v0') { + throw new Error("crawlUrlAndWatch is only available on v1"); + } + + const crawl = await this.crawlUrl(url, params, false, 0, idempotencyKey); + const id = this.version === 'v0' ? (crawl as CrawlResponseV0).jobId : (crawl as CrawlResponse).id; + + return new CrawlWatcher(id as string, this as FirecrawlApp<"v1">); + } + async mapUrl(url: string, params?: MapParams): Promise { if (this.version == 'v0') { throw new Error("Map is not supported in v0"); @@ -696,3 +713,111 @@ export default class FirecrawlApp { } } } + +interface CrawlWatcherEvents { + document: CustomEvent, + done: CustomEvent<{ + status: CrawlStatusResponse["status"]; + data: FirecrawlDocument[]; + }>, + error: CustomEvent<{ + status: CrawlStatusResponse["status"], + data: FirecrawlDocument[], + error: string, + }>, +} + +export class CrawlWatcher extends TypedEventTarget { + private ws: WebSocket; + public data: FirecrawlDocument[]; + public status: CrawlStatusResponse["status"]; + + constructor(id: string, app: FirecrawlApp<"v1">) { + super(); + this.ws = new WebSocket(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey); + this.status = "scraping"; + this.data = []; + + type ErrorMessage = { + type: "error", + error: string, + } + + type CatchupMessage = { + type: "catchup", + data: CrawlStatusResponse, + } + + type DocumentMessage = { + type: "document", + data: FirecrawlDocument, + } + + type DoneMessage = { type: "done" } + + type Message = ErrorMessage | CatchupMessage | DoneMessage | DocumentMessage; + + const messageHandler = (msg: Message) => { + if (msg.type === "done") { + this.status = "completed"; + this.dispatchTypedEvent("done", new CustomEvent("done", { + detail: { + status: this.status, + data: this.data, + }, + })); + } else if (msg.type === "error") { + this.status = "failed"; + this.dispatchTypedEvent("error", new CustomEvent("error", { + detail: { + status: this.status, + data: this.data, + error: msg.error, + }, + })); + } else if (msg.type === "catchup") { + this.status = msg.data.status; + this.data.push(...(msg.data.data ?? [])); + for (const doc of this.data) { + this.dispatchTypedEvent("document", new CustomEvent("document", { + detail: doc, + })); + } + } else if (msg.type === "document") { + this.dispatchTypedEvent("document", new CustomEvent("document", { + detail: msg.data, + })); + } + } + + this.ws.onmessage = ((ev: MessageEvent) => { + if (typeof ev.data !== "string") { + this.ws.close(); + return; + } + + const msg = JSON.parse(ev.data) as Message; + messageHandler(msg); + }).bind(this); + + this.ws.onclose = ((ev: CloseEvent) => { + const msg = JSON.parse(ev.reason) as Message; + messageHandler(msg); + }).bind(this); + + this.ws.onerror = ((_: Event) => { + this.status = "failed" + this.dispatchTypedEvent("error", new CustomEvent("error", { + detail: { + status: this.status, + data: this.data, + error: "WebSocket error", + }, + })); + }).bind(this); + } + + close() { + this.ws.close(); + } +} From 377e8ded34d0dd94558043beea65993310c01d85 Mon Sep 17 00:00:00 2001 From: rafaelsideguide <150964962+rafaelsideguide@users.noreply.github.com> Date: Thu, 29 Aug 2024 17:40:43 -0300 Subject: [PATCH 3/9] removed v0 support --- apps/js-sdk/firecrawl/package.json | 2 +- apps/js-sdk/firecrawl/src/index.ts | 394 +++++++---------------------- 2 files changed, 90 insertions(+), 306 deletions(-) diff --git a/apps/js-sdk/firecrawl/package.json b/apps/js-sdk/firecrawl/package.json index cadd1eaf..48c6c128 100644 --- a/apps/js-sdk/firecrawl/package.json +++ b/apps/js-sdk/firecrawl/package.json @@ -1,6 +1,6 @@ { "name": "@mendable/firecrawl-js", - "version": "1.0.4", + "version": "1.1.4", "description": "JavaScript SDK for Firecrawl API", "main": "build/cjs/index.js", "types": "types/index.d.ts", diff --git a/apps/js-sdk/firecrawl/src/index.ts b/apps/js-sdk/firecrawl/src/index.ts index 033ffd88..586e9240 100644 --- a/apps/js-sdk/firecrawl/src/index.ts +++ b/apps/js-sdk/firecrawl/src/index.ts @@ -8,12 +8,10 @@ import { TypedEventTarget } from "typescript-event-target"; * Configuration interface for FirecrawlApp. * @param apiKey - Optional API key for authentication. * @param apiUrl - Optional base URL of the API; defaults to 'https://api.firecrawl.dev'. - * @param version - API version, either 'v0' or 'v1'. */ export interface FirecrawlAppConfig { apiKey?: string | null; apiUrl?: string | null; - version?: "v0" | "v1"; } /** @@ -56,17 +54,6 @@ export interface FirecrawlDocumentMetadata { [key: string]: any; // Allows for additional metadata properties not explicitly defined. } -/** - * Metadata for a Firecrawl document on v0. - * Similar to FirecrawlDocumentMetadata but includes properties specific to API version v0. - */ -export interface FirecrawlDocumentMetadataV0 { - // Similar properties as FirecrawlDocumentMetadata with additional v0 specific adjustments - pageStatusCode?: number; - pageError?: string; - [key: string]: any; -} - /** * Document interface for Firecrawl. * Represents a document retrieved or processed by Firecrawl. @@ -78,28 +65,7 @@ export interface FirecrawlDocument { rawHtml?: string; links?: string[]; screenshot?: string; - metadata: FirecrawlDocumentMetadata; -} - -/** - * Document interface for Firecrawl on v0. - * Represents a document specifically for API version v0 with additional properties. - */ -export interface FirecrawlDocumentV0 { - id?: string; - url?: string; - content: string; - markdown?: string; - html?: string; - llm_extraction?: Record; - createdAt?: Date; - updatedAt?: Date; - type?: string; - metadata: FirecrawlDocumentMetadataV0; - childrenLinks?: string[]; - provider?: string; - warning?: string; - index?: number; + metadata?: FirecrawlDocumentMetadata; } /** @@ -107,38 +73,12 @@ export interface FirecrawlDocumentV0 { * Defines the options and configurations available for scraping web content. */ export interface ScrapeParams { - formats: ("markdown" | "html" | "rawHtml" | "content" | "links" | "screenshot")[]; + formats: ("markdown" | "html" | "rawHtml" | "content" | "links" | "screenshot" | "full@scrennshot")[]; headers?: Record; includeTags?: string[]; excludeTags?: string[]; onlyMainContent?: boolean; - screenshotMode?: "desktop" | "full-desktop" | "mobile" | "full-mobile"; - waitFor?: number; - timeout?: number; -} - -/** - * Parameters for scraping operations on v0. - * Includes page and extractor options specific to API version v0. - */ -export interface ScrapeParamsV0 { - pageOptions?: { - headers?: Record; - includeHtml?: boolean; - includeRawHtml?: boolean; - onlyIncludeTags?: string[]; - onlyMainContent?: boolean; - removeTags?: string[]; - replaceAllPathsWithAbsolutePaths?: boolean; - screenshot?: boolean; - fullPageScreenshot?: boolean; waitFor?: number; - }; - extractorOptions?: { - mode?: "markdown" | "llm-extraction" | "llm-extraction-from-raw-html" | "llm-extraction-from-markdown"; - extractionPrompt?: string; - extractionSchema?: Record | z.ZodSchema | any; - }; timeout?: number; } @@ -147,21 +87,11 @@ export interface ScrapeParamsV0 { * Defines the structure of the response received after a scraping operation. */ export interface ScrapeResponse extends FirecrawlDocument { - success: boolean; + success: true; warning?: string; error?: string; } -/** - * Response interface for scraping operations on v0. - * Similar to ScrapeResponse but tailored for responses from API version v0. - */ -export interface ScrapeResponseV0 { - success: boolean; - data?: FirecrawlDocumentV0; - error?: string; -} - /** * Parameters for crawling operations. * Includes options for both scraping and mapping during a crawl. @@ -177,37 +107,6 @@ export interface CrawlParams { scrapeOptions?: ScrapeParams; } -/** - * Parameters for crawling operations on v0. - * Tailored for API version v0, includes specific options for crawling. - */ -export interface CrawlParamsV0 { - crawlerOptions?: { - includes?: string[]; - excludes?: string[]; - generateImgAltText?: boolean; - returnOnlyUrls?: boolean; - maxDepth?: number; - mode?: "default" | "fast"; - ignoreSitemap?: boolean; - limit?: number; - allowBackwardCrawling?: boolean; - allowExternalContentLinks?: boolean; - }; - pageOptions?: { - headers?: Record; - includeHtml?: boolean; - includeRawHtml?: boolean; - onlyIncludeTags?: string[]; - onlyMainContent?: boolean; - removeTags?: string[]; - replaceAllPathsWithAbsolutePaths?: boolean; - screenshot?: boolean; - fullPageScreenshot?: boolean; - waitFor?: number; - }; -} - /** * Response interface for crawling operations. * Defines the structure of the response received after initiating a crawl. @@ -215,17 +114,7 @@ export interface CrawlParamsV0 { export interface CrawlResponse { id?: string; url?: string; - success: boolean; - error?: string; -} - -/** - * Response interface for crawling operations on v0. - * Similar to CrawlResponse but tailored for responses from API version v0. - */ -export interface CrawlResponseV0 { - jobId?: string; - success: boolean; + success: true; error?: string; } @@ -234,7 +123,7 @@ export interface CrawlResponseV0 { * Provides detailed status of a crawl job including progress and results. */ export interface CrawlStatusResponse { - success: boolean; + success: true; total: number; completed: number; creditsUsed: number; @@ -245,23 +134,6 @@ export interface CrawlStatusResponse { error?: string; } -/** - * Response interface for job status checks on v0. - * Tailored for API version v0, provides status and partial data of a crawl job. - */ -export interface CrawlStatusResponseV0 { - success: boolean; - status: string; - current?: number; - current_url?: string; - current_step?: string; - total?: number; - data?: FirecrawlDocumentV0[]; - partial_data?: FirecrawlDocumentV0[]; - error?: string; -} - - /** * Parameters for mapping operations. * Defines options for mapping URLs during a crawl. @@ -278,57 +150,35 @@ export interface MapParams { * Defines the structure of the response received after a mapping operation. */ export interface MapResponse { - success: boolean; + success: true; links?: string[]; error?: string; } /** - * Parameters for searching operations on v0. - * Tailored for API version v0, includes specific options for searching content. + * Error response interface. + * Defines the structure of the response received when an error occurs. */ -export interface SearchParamsV0 { - pageOptions?: { - onlyMainContent?: boolean; - fetchPageContent?: boolean; - includeHtml?: boolean; - includeRawHtml?: boolean; - }; - searchOptions?: { - limit?: number; - }; -} - -/** - * Response interface for searching operations on v0. - * Defines the structure of the response received after a search operation on v0. - */ -export interface SearchResponseV0 { - success: boolean; - data?: FirecrawlDocumentV0[]; - error?: string; +export interface ErrorResponse { + success: false; + error: string; } /** * Main class for interacting with the Firecrawl API. * Provides methods for scraping, searching, crawling, and mapping web content. */ -export default class FirecrawlApp { +export default class FirecrawlApp { public apiKey: string; public apiUrl: string; - public version: T; /** * Initializes a new instance of the FirecrawlApp class. * @param config - Configuration options for the FirecrawlApp instance. */ - constructor({ apiKey = null, apiUrl = null, version = "v1" }: FirecrawlAppConfig) { + constructor({ apiKey = null, apiUrl = null }: FirecrawlAppConfig) { this.apiKey = apiKey || ""; this.apiUrl = apiUrl || "https://api.firecrawl.dev"; - this.version = version as T; - if (!this.apiKey) { - throw new Error("No API key provided"); - } } /** @@ -339,8 +189,8 @@ export default class FirecrawlApp { */ async scrapeUrl( url: string, - params?: ScrapeParams | ScrapeParamsV0 - ): Promise { + params?: ScrapeParams + ): Promise { const headers: AxiosRequestHeaders = { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}`, @@ -363,19 +213,19 @@ export default class FirecrawlApp { } try { const response: AxiosResponse = await axios.post( - this.apiUrl + `/${this.version}/scrape`, + this.apiUrl + `/v1/scrape`, jsonData, { headers } ); if (response.status === 200) { const responseData = response.data; if (responseData.success) { - return (this.version === 'v0' ? responseData as ScrapeResponseV0 : { + return { success: true, warning: responseData.warning, error: responseData.error, ...responseData.data - }) as ScrapeResponse; + }; } else { throw new Error(`Failed to scrape URL. Error: ${responseData.error}`); } @@ -385,100 +235,47 @@ export default class FirecrawlApp { } catch (error: any) { throw new Error(error.message); } - return { success: false, error: "Internal server error." } as this['version'] extends 'v0' ? ScrapeResponseV0 : ScrapeResponse; + return { success: false, error: "Internal server error." }; } /** - * Searches for a query using the Firecrawl API. - * @param query - The query to search for. - * @param params - Additional parameters for the search request. - * @returns The response from the search operation. + * This method is intended to search for a query using the Firecrawl API. However, it is not supported in version 1 of the API. + * @param query - The search query string. + * @param params - Additional parameters for the search. + * @returns Throws an error advising to use version 0 of the API. */ async search( query: string, - params?: SearchParamsV0 - ): Promise { - if (this.version === "v1") { - throw new Error("Search is not supported in v1, please update FirecrawlApp() initialization to use v0."); - } - - const headers: AxiosRequestHeaders = { - "Content-Type": "application/json", - Authorization: `Bearer ${this.apiKey}`, - } as AxiosRequestHeaders; - let jsonData: any = { query }; - if (params) { - jsonData = { ...jsonData, ...params }; - } - try { - const response: AxiosResponse = await axios.post( - this.apiUrl + "/v0/search", - jsonData, - { headers } - ); - if (response.status === 200) { - const responseData = response.data; - if (responseData.success) { - return responseData; - } else { - throw new Error(`Failed to search. Error: ${responseData.error}`); - } - } else { - this.handleError(response, "search"); - } - } catch (error: any) { - throw new Error(error.message); - } - return { success: false, error: "Internal server error." }; + params?: any + ): Promise { + throw new Error("Search is not supported in v1, please update FirecrawlApp() initialization to use v0."); } /** * Initiates a crawl job for a URL using the Firecrawl API. * @param url - The URL to crawl. * @param params - Additional parameters for the crawl request. - * @param waitUntilDone - Whether to wait for the crawl job to complete. * @param pollInterval - Time in seconds for job status checks. * @param idempotencyKey - Optional idempotency key for the request. * @returns The response from the crawl operation. */ async crawlUrl( url: string, - params?: this['version'] extends 'v0' ? CrawlParamsV0 : CrawlParams, - waitUntilDone: boolean = true, + params?: CrawlParams, pollInterval: number = 2, idempotencyKey?: string - ): Promise< - this['version'] extends 'v0' - ? CrawlResponseV0 | CrawlStatusResponseV0 | FirecrawlDocumentV0[] - : CrawlResponse | CrawlStatusResponse - > { + ): Promise { const headers = this.prepareHeaders(idempotencyKey); let jsonData: any = { url, ...params }; try { const response: AxiosResponse = await this.postRequest( - this.apiUrl + `/${this.version}/crawl`, + this.apiUrl + `/v1/crawl`, jsonData, headers ); if (response.status === 200) { - const id: string = this.version === 'v0' ? response.data.jobId : response.data.id; - let checkUrl: string | undefined = undefined; - if (waitUntilDone) { - if (this.version === 'v1') { checkUrl = response.data.url } - return this.monitorJobStatus(id, headers, pollInterval, checkUrl); - } else { - if (this.version === 'v0') { - return { - success: true, - jobId: id - } as CrawlResponseV0; - } else { - return { - success: true, - id: id - } as CrawlResponse; - } - } + const id: string = response.data.id; + return this.monitorJobStatus(id, headers, pollInterval); } else { this.handleError(response, "start crawl job"); } @@ -489,7 +286,35 @@ export default class FirecrawlApp { throw new Error(error.message); } } - return { success: false, error: "Internal server error." } as this['version'] extends 'v0' ? CrawlResponseV0 : CrawlResponse; + return { success: false, error: "Internal server error." }; + } + + async asyncCrawlUrl( + url: string, + params?: CrawlParams, + idempotencyKey?: string + ): Promise { + const headers = this.prepareHeaders(idempotencyKey); + let jsonData: any = { url, ...params }; + try { + const response: AxiosResponse = await this.postRequest( + this.apiUrl + `/v1/crawl`, + jsonData, + headers + ); + if (response.status === 200) { + return response.data; + } else { + this.handleError(response, "start crawl job"); + } + } catch (error: any) { + if (error.response?.data?.error) { + throw new Error(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`); + } else { + throw new Error(error.message); + } + } + return { success: false, error: "Internal server error." }; } /** @@ -497,7 +322,7 @@ export default class FirecrawlApp { * @param id - The ID of the crawl operation. * @returns The response containing the job status. */ - async checkCrawlStatus(id?: string): Promise { + async checkCrawlStatus(id?: string): Promise { if (!id) { throw new Error("No crawl ID provided"); } @@ -505,86 +330,52 @@ export default class FirecrawlApp { const headers: AxiosRequestHeaders = this.prepareHeaders(); try { const response: AxiosResponse = await this.getRequest( - this.version === 'v1' ? - `${this.apiUrl}/${this.version}/crawl/${id}` : - `${this.apiUrl}/${this.version}/crawl/status/${id}`, + `${this.apiUrl}/v1/crawl/${id}`, headers ); if (response.status === 200) { - if (this.version === 'v0') { - return ({ - success: true, - status: response.data.status, - current: response.data.current, - current_url: response.data.current_url, - current_step: response.data.current_step, - total: response.data.total, - data: response.data.data, - partial_data: !response.data.data - ? response.data.partial_data - : undefined, - } as CrawlStatusResponseV0) as this['version'] extends 'v0' ? CrawlStatusResponseV0 : CrawlStatusResponse; - } else { - return ({ - success: true, - status: response.data.status, - total: response.data.total, - completed: response.data.completed, - creditsUsed: response.data.creditsUsed, - expiresAt: new Date(response.data.expiresAt), - next: response.data.next, - data: response.data.data, - error: response.data.error - } as CrawlStatusResponse) as this['version'] extends 'v0' ? CrawlStatusResponseV0 : CrawlStatusResponse; - } + return ({ + success: true, + status: response.data.status, + total: response.data.total, + completed: response.data.completed, + creditsUsed: response.data.creditsUsed, + expiresAt: new Date(response.data.expiresAt), + next: response.data.next, + data: response.data.data, + error: response.data.error + }) } else { this.handleError(response, "check crawl status"); } } catch (error: any) { throw new Error(error.message); } - - return this.version === 'v0' ? - ({ - success: false, - status: "unknown", - current: 0, - current_url: "", - current_step: "", - total: 0, - error: "Internal server error.", - } as this['version'] extends 'v0' ? CrawlStatusResponseV0 : CrawlStatusResponse) : - ({ - success: false, - error: "Internal server error.", - } as this['version'] extends 'v0' ? CrawlStatusResponseV0 : CrawlStatusResponse); + return { success: false, error: "Internal server error." }; } async crawlUrlAndWatch( url: string, - params?: this['version'] extends 'v0' ? CrawlParamsV0 : CrawlParams, + params?: CrawlParams, idempotencyKey?: string, ) { - if (this.version === 'v0') { - throw new Error("crawlUrlAndWatch is only available on v1"); + const crawl = await this.asyncCrawlUrl(url, params, idempotencyKey); + + if (crawl.success && crawl.id) { + const id = crawl.id; + return new CrawlWatcher(id, this); } - const crawl = await this.crawlUrl(url, params, false, 0, idempotencyKey); - const id = this.version === 'v0' ? (crawl as CrawlResponseV0).jobId : (crawl as CrawlResponse).id; - - return new CrawlWatcher(id as string, this as FirecrawlApp<"v1">); + throw new Error("Crawl job failed to start"); } - async mapUrl(url: string, params?: MapParams): Promise { - if (this.version == 'v0') { - throw new Error("Map is not supported in v0"); - } + async mapUrl(url: string, params?: MapParams): Promise { const headers = this.prepareHeaders(); let jsonData: { url: string } & MapParams = { url, ...params }; try { const response: AxiosResponse = await this.postRequest( - this.apiUrl + `/${this.version}/map`, + this.apiUrl + `/v1/map`, jsonData, headers ); @@ -596,7 +387,7 @@ export default class FirecrawlApp { } catch (error: any) { throw new Error(error.message); } - return { success: false, error: "Internal server error." } as MapResponse; + return { success: false, error: "Internal server error." }; } /** @@ -651,25 +442,18 @@ export default class FirecrawlApp { async monitorJobStatus( id: string, headers: AxiosRequestHeaders, - checkInterval: number, - checkUrl?: string - ): Promise { - let apiUrl: string = ''; + checkInterval: number + ): Promise { while (true) { - if (this.version === 'v1') { - apiUrl = checkUrl ?? `${this.apiUrl}/v1/crawl/${id}`; - } else if (this.version === 'v0') { - apiUrl = `${this.apiUrl}/v0/crawl/status/${id}`; - } const statusResponse: AxiosResponse = await this.getRequest( - apiUrl, + `${this.apiUrl}/v1/crawl/${id}`, headers ); if (statusResponse.status === 200) { const statusData = statusResponse.data; if (statusData.status === "completed") { if ("data" in statusData) { - return this.version === 'v0' ? statusData.data : statusData; + return statusData; } else { throw new Error("Crawl job completed but no data was returned"); } @@ -729,7 +513,7 @@ export class CrawlWatcher extends TypedEventTarget { public data: FirecrawlDocument[]; public status: CrawlStatusResponse["status"]; - constructor(id: string, app: FirecrawlApp<"v1">) { + constructor(id: string, app: FirecrawlApp) { super(); this.ws = new WebSocket(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey); this.status = "scraping"; From ae38c26fa8834b3cf93bcc0211a195ad6cd24bd1 Mon Sep 17 00:00:00 2001 From: rafaelsideguide <150964962+rafaelsideguide@users.noreply.github.com> Date: Fri, 30 Aug 2024 10:09:39 -0300 Subject: [PATCH 4/9] feat(v1-sdks): async crawl node, python websocket + async crawl + example --- apps/js-sdk/README.md | 136 --------------- apps/js-sdk/example.js | 95 ++++++----- apps/js-sdk/example.ts | 40 +++-- apps/js-sdk/exampleV0.js | 85 ---------- apps/js-sdk/exampleV0.ts | 93 ---------- apps/js-sdk/firecrawl/README.md | 42 ++++- apps/js-sdk/test.ts | 28 --- apps/python-sdk/README.md | 59 +++++-- apps/python-sdk/example.py | 160 ++++++++++++------ apps/python-sdk/examplev0.py | 75 --------- apps/python-sdk/firecrawl/firecrawl.py | 225 ++++++++++++++----------- apps/python-sdk/requirements.txt | 5 +- 12 files changed, 394 insertions(+), 649 deletions(-) delete mode 100644 apps/js-sdk/README.md delete mode 100644 apps/js-sdk/exampleV0.js delete mode 100644 apps/js-sdk/exampleV0.ts delete mode 100644 apps/js-sdk/test.ts delete mode 100644 apps/python-sdk/examplev0.py diff --git a/apps/js-sdk/README.md b/apps/js-sdk/README.md deleted file mode 100644 index 0368677a..00000000 --- a/apps/js-sdk/README.md +++ /dev/null @@ -1,136 +0,0 @@ -# Firecrawl Node SDK - -The Firecrawl Node SDK is a library that allows you to easily scrape and crawl websites, and output the data in a format ready for use with language models (LLMs). It provides a simple and intuitive interface for interacting with the Firecrawl API. - -## Installation - -To install the Firecrawl Node SDK, you can use npm: - -```bash -npm install @mendable/firecrawl-js -``` - -## Usage - -1. Get an API key from [firecrawl.dev](https://firecrawl.dev) -2. Set the API key as an environment variable named `FIRECRAWL_API_KEY` or pass it as a parameter to the `FirecrawlApp` class. - -Here's an example of how to use the SDK with error handling: - -```js -import FirecrawlApp, { CrawlParams, CrawlStatusResponse } from '@mendable/firecrawl-js'; - -const app = new FirecrawlApp({apiKey: "fc-YOUR_API_KEY"}); - -// Scrape a website -const scrapeResponse = await app.scrapeUrl('https://firecrawl.dev', { - formats: ['markdown', 'html'], -}); - -if (scrapeResponse) { - console.log(scrapeResponse) -} - -// Crawl a website -const crawlResponse = await app.crawlUrl('https://firecrawl.dev', { - limit: 100, - scrapeOptions: { - formats: ['markdown', 'html'], - } -} as CrawlParams, true, 30) as CrawlStatusResponse; - -if (crawlResponse) { - console.log(crawlResponse) -} -``` - -### Scraping a URL - -To scrape a single URL with error handling, use the `scrapeUrl` method. It takes the URL as a parameter and returns the scraped data as a dictionary. - -```js -const url = "https://example.com"; -const scrapedData = await app.scrapeUrl(url); -``` - -### Crawling a Website - -To crawl a website with error handling, use the `crawlUrl` method. It takes the starting URL and optional parameters as arguments. The `params` argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format. - -```js -const crawlResponse = await app.crawlUrl('https://firecrawl.dev', { - limit: 100, - scrapeOptions: { - formats: ['markdown', 'html'], - } -} as CrawlParams, true, 30) as CrawlStatusResponse; - -if (crawlResponse) { - console.log(crawlResponse) -} -``` - -### Checking Crawl Status - -To check the status of a crawl job with error handling, use the `checkCrawlStatus` method. It takes the job ID as a parameter and returns the current status of the crawl job. - -```js -const status = await app.checkCrawlStatus(id); -``` - -### Extracting structured data from a URL - -With LLM extraction, you can easily extract structured data from any URL. We support zod schema to make it easier for you too. Here is how you to use it: - -```js -import FirecrawlApp from "@mendable/firecrawl-js"; -import { z } from "zod"; - -const app = new FirecrawlApp({ - apiKey: "fc-YOUR_API_KEY", -}); - -// Define schema to extract contents into -const schema = z.object({ - top: z - .array( - z.object({ - title: z.string(), - points: z.number(), - by: z.string(), - commentsURL: z.string(), - }) - ) - .length(5) - .describe("Top 5 stories on Hacker News"), -}); - -const scrapeResult = await app.scrapeUrl("https://firecrawl.dev", { - extractorOptions: { extractionSchema: schema }, -}); - -console.log(scrapeResult.data["llm_extraction"]); -``` - -### Map a Website - -Use `map_url` to generate a list of URLs from a website. The `params` argument let you customize the mapping process, including options to exclude subdomains or to utilize the sitemap. - -```js -const mapResult = await app.mapUrl('https://example.com') as MapResponse; -console.log(mapResult) -``` - -## Error Handling - -The SDK handles errors returned by the Firecrawl API and raises appropriate exceptions. If an error occurs during a request, an exception will be raised with a descriptive error message. The examples above demonstrate how to handle these errors using `try/catch` blocks. - -## License - -The Firecrawl Node SDK is licensed under the MIT License. This means you are free to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the SDK, subject to the following conditions: - -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -Please note that while this SDK is MIT licensed, it is part of a larger project which may be under different licensing terms. Always refer to the license information in the root directory of the main project for overall licensing details. diff --git a/apps/js-sdk/example.js b/apps/js-sdk/example.js index 5698a017..eb4bc489 100644 --- a/apps/js-sdk/example.js +++ b/apps/js-sdk/example.js @@ -1,49 +1,62 @@ -import FirecrawlApp from './firecrawl/src/index'; //'@mendable/firecrawl-js'; +import FirecrawlApp from '@mendable/firecrawl-js'; const app = new FirecrawlApp({apiKey: "fc-YOUR_API_KEY"}); -// Scrape a website: -const scrapeResult = await app.scrapeUrl('firecrawl.dev'); +const main = async () => { -if (scrapeResult.data) { - console.log(scrapeResult.data.markdown) -} + // Scrape a website: + const scrapeResult = await app.scrapeUrl('firecrawl.dev'); -// Crawl a website: -const crawlResult = await app.crawlUrl('mendable.ai', {crawlerOptions: {excludes: ['blog/*'], limit: 5}}, false); -console.log(crawlResult) - -const jobId = await crawlResult['jobId']; -console.log(jobId); - -let job; -while (true) { - job = await app.checkCrawlStatus(jobId); - if (job.status === 'completed') { - break; + if (scrapeResult.success) { + console.log(scrapeResult.markdown) } - await new Promise(resolve => setTimeout(resolve, 1000)); // wait 1 second + + // Crawl a website: + const crawlResult = await app.crawlUrl('mendable.ai', { excludePaths: ['blog/*'], limit: 5}); + console.log(crawlResult); + + // Asynchronously crawl a website: + const asyncCrawlResult = await app.asyncCrawlUrl('mendable.ai', { excludePaths: ['blog/*'], limit: 5}); + + if (asyncCrawlResult.success) { + const id = asyncCrawlResult.id; + console.log(id); + + let checkStatus; + if (asyncCrawlResult.success) { + while (true) { + checkStatus = await app.checkCrawlStatus(id); + if (checkStatus.success && checkStatus.status === 'completed') { + break; + } + await new Promise(resolve => setTimeout(resolve, 1000)); // wait 1 second + } + + if (checkStatus.success && checkStatus.data) { + console.log(checkStatus.data[0].markdown); + } + } + } + + // Map a website: + const mapResult = await app.mapUrl('https://firecrawl.dev'); + console.log(mapResult) + + + // Crawl a website with WebSockets: + const watch = await app.crawlUrlAndWatch('mendable.ai', { excludePaths: ['blog/*'], limit: 5}); + + watch.addEventListener("document", doc => { + console.log("DOC", doc.detail); + }); + + watch.addEventListener("error", err => { + console.error("ERR", err.detail.error); + }); + + watch.addEventListener("done", state => { + console.log("DONE", state.detail.status); + }); } -if (job.data) { - console.log(job.data[0].markdown); -} - -// Map a website: -const mapResult = await app.map('https://firecrawl.dev'); -console.log(mapResult) - -// Crawl a website with WebSockets: -const watch = await app.crawlUrlAndWatch('mendable.ai', { excludePaths: ['blog/*'], limit: 5}); - -watch.addEventListener("document", doc => { - console.log("DOC", doc.detail); -}); - -watch.addEventListener("error", err => { - console.error("ERR", err.detail.error); -}); - -watch.addEventListener("done", state => { - console.log("DONE", state.detail.status); -}); +main() diff --git a/apps/js-sdk/example.ts b/apps/js-sdk/example.ts index 80589f5a..4142416f 100644 --- a/apps/js-sdk/example.ts +++ b/apps/js-sdk/example.ts @@ -1,4 +1,4 @@ -import FirecrawlApp, { CrawlStatusResponse, CrawlResponse } from '@mendable/firecrawl-js'; +import FirecrawlApp, { CrawlStatusResponse, ErrorResponse } from '@mendable/firecrawl-js'; const app = new FirecrawlApp({apiKey: "fc-YOUR_API_KEY"}); @@ -7,29 +7,35 @@ const main = async () => { // Scrape a website: const scrapeResult = await app.scrapeUrl('firecrawl.dev'); - if (scrapeResult) { + if (scrapeResult.success) { console.log(scrapeResult.markdown) } // Crawl a website: - // @ts-ignore - const crawlResult = await app.crawlUrl('mendable.ai', { excludePaths: ['blog/*'], limit: 5}, false) as CrawlResponse; - console.log(crawlResult) + const crawlResult = await app.crawlUrl('mendable.ai', { excludePaths: ['blog/*'], limit: 5}); + console.log(crawlResult); - const id = crawlResult.id; - console.log(id); + // Asynchronously crawl a website: + const asyncCrawlResult = await app.asyncCrawlUrl('mendable.ai', { excludePaths: ['blog/*'], limit: 5}); + + if (asyncCrawlResult.success) { + const id = asyncCrawlResult.id; + console.log(id); - let checkStatus: CrawlStatusResponse; - while (true) { - checkStatus = await app.checkCrawlStatus(id); - if (checkStatus.status === 'completed') { - break; + let checkStatus: CrawlStatusResponse | ErrorResponse; + if (asyncCrawlResult.success) { + while (true) { + checkStatus = await app.checkCrawlStatus(id); + if (checkStatus.success && checkStatus.status === 'completed') { + break; + } + await new Promise(resolve => setTimeout(resolve, 1000)); // wait 1 second + } + + if (checkStatus.success && checkStatus.data) { + console.log(checkStatus.data[0].markdown); + } } - await new Promise(resolve => setTimeout(resolve, 1000)); // wait 1 second - } - - if (checkStatus.data) { - console.log(checkStatus.data[0].markdown); } // Map a website: diff --git a/apps/js-sdk/exampleV0.js b/apps/js-sdk/exampleV0.js deleted file mode 100644 index 7f198598..00000000 --- a/apps/js-sdk/exampleV0.js +++ /dev/null @@ -1,85 +0,0 @@ -import { v4 as uuidv4 } from 'uuid'; -import FirecrawlApp from '@mendable/firecrawl-js'; -import { z } from "zod"; - -const app = new FirecrawlApp({apiKey: "fc-YOUR_API_KEY"}); - -// Scrape a website: -const scrapeResult = await app.scrapeUrl('firecrawl.dev'); -console.log(scrapeResult.data.content) - -// Crawl a website: -const idempotencyKey = uuidv4(); // optional -const crawlResult = await app.crawlUrl('mendable.ai', {crawlerOptions: {excludes: ['blog/*'], limit: 5}}, false, 2, idempotencyKey); -console.log(crawlResult) - -const jobId = await crawlResult['jobId']; -console.log(jobId); - -let job; -while (true) { - job = await app.checkCrawlStatus(jobId); - if (job.status == 'completed') { - break; - } - await new Promise(resolve => setTimeout(resolve, 1000)); // wait 1 second -} - -console.log(job.data[0].content); - -// Search for a query: -const query = 'what is mendable?' -const searchResult = await app.search(query) -console.log(searchResult) - -// LLM Extraction: -// Define schema to extract contents into using zod schema -const zodSchema = z.object({ - top: z - .array( - z.object({ - title: z.string(), - points: z.number(), - by: z.string(), - commentsURL: z.string(), - }) - ) - .length(5) - .describe("Top 5 stories on Hacker News"), -}); - -let llmExtractionResult = await app.scrapeUrl("https://news.ycombinator.com", { - extractorOptions: { extractionSchema: zodSchema }, -}); - -console.log(llmExtractionResult.data.llm_extraction); - -// Define schema to extract contents into using json schema -const jsonSchema = { - "type": "object", - "properties": { - "top": { - "type": "array", - "items": { - "type": "object", - "properties": { - "title": {"type": "string"}, - "points": {"type": "number"}, - "by": {"type": "string"}, - "commentsURL": {"type": "string"} - }, - "required": ["title", "points", "by", "commentsURL"] - }, - "minItems": 5, - "maxItems": 5, - "description": "Top 5 stories on Hacker News" - } - }, - "required": ["top"] -} - -llmExtractionResult = await app.scrapeUrl("https://news.ycombinator.com", { - extractorOptions: { extractionSchema: jsonSchema }, -}); - -console.log(llmExtractionResult.data.llm_extraction); \ No newline at end of file diff --git a/apps/js-sdk/exampleV0.ts b/apps/js-sdk/exampleV0.ts deleted file mode 100644 index cecaaf24..00000000 --- a/apps/js-sdk/exampleV0.ts +++ /dev/null @@ -1,93 +0,0 @@ -import FirecrawlApp, { ScrapeResponseV0, CrawlStatusResponseV0, SearchResponseV0 } from './firecrawl/src/index' //'@mendable/firecrawl-js'; -import { z } from "zod"; - -const app = new FirecrawlApp<"v0">({apiKey: "fc-YOUR_API_KEY", version: "v0"}) - -// Scrape a website: -const scrapeResult = await app.scrapeUrl('firecrawl.dev'); - -if (scrapeResult.data) { - console.log(scrapeResult.data.content) -} - -// Crawl a website: -const crawlResult = await app.crawlUrl('mendable.ai', {crawlerOptions: {excludes: ['blog/*'], limit: 5}}, false); -console.log(crawlResult) - -const jobId: string = await crawlResult['jobId']; -console.log(jobId); - -let job: CrawlStatusResponseV0; -while (true) { - job = await app.checkCrawlStatus(jobId) as CrawlStatusResponseV0; - if (job.status === 'completed') { - break; - } - await new Promise(resolve => setTimeout(resolve, 1000)); // wait 1 second -} - -if (job.data) { - console.log(job.data[0].content); -} - -// Search for a query: -const query = 'what is mendable?' -const searchResult = await app.search(query) as SearchResponseV0; -if (searchResult.data) { - console.log(searchResult.data[0].content) -} - -// LLM Extraction: -// Define schema to extract contents into using zod schema -const zodSchema = z.object({ - top: z - .array( - z.object({ - title: z.string(), - points: z.number(), - by: z.string(), - commentsURL: z.string(), - }) - ) - .length(5) - .describe("Top 5 stories on Hacker News"), -}); - -let llmExtractionResult = await app.scrapeUrl("https://news.ycombinator.com"); - -if (llmExtractionResult.data) { - console.log(llmExtractionResult.data[0].llm_extraction); -} - -// Define schema to extract contents into using json schema -const jsonSchema = { - "type": "object", - "properties": { - "top": { - "type": "array", - "items": { - "type": "object", - "properties": { - "title": {"type": "string"}, - "points": {"type": "number"}, - "by": {"type": "string"}, - "commentsURL": {"type": "string"} - }, - "required": ["title", "points", "by", "commentsURL"] - }, - "minItems": 5, - "maxItems": 5, - "description": "Top 5 stories on Hacker News" - } - }, - "required": ["top"] -} - -llmExtractionResult = await app.scrapeUrl("https://news.ycombinator.com", { - extractorOptions: { extractionSchema: jsonSchema }, -}); - -if (llmExtractionResult.data) { - console.log(llmExtractionResult.data[0].llm_extraction); -} - diff --git a/apps/js-sdk/firecrawl/README.md b/apps/js-sdk/firecrawl/README.md index 0368677a..0f3a6824 100644 --- a/apps/js-sdk/firecrawl/README.md +++ b/apps/js-sdk/firecrawl/README.md @@ -37,11 +37,9 @@ const crawlResponse = await app.crawlUrl('https://firecrawl.dev', { scrapeOptions: { formats: ['markdown', 'html'], } -} as CrawlParams, true, 30) as CrawlStatusResponse; +}) -if (crawlResponse) { - console.log(crawlResponse) -} +console.log(crawlResponse) ``` ### Scraping a URL @@ -63,16 +61,21 @@ const crawlResponse = await app.crawlUrl('https://firecrawl.dev', { scrapeOptions: { formats: ['markdown', 'html'], } -} as CrawlParams, true, 30) as CrawlStatusResponse; +}) +``` -if (crawlResponse) { - console.log(crawlResponse) -} + +### Asynchronous Crawl + +To initiate an asynchronous crawl of a website, utilize the AsyncCrawlURL method. This method requires the starting URL and optional parameters as inputs. The params argument enables you to define various settings for the asynchronous crawl, such as the maximum number of pages to crawl, permitted domains, and the output format. Upon successful initiation, this method returns an ID, which is essential for subsequently checking the status of the crawl. + +```js +const asyncCrawlResult = await app.asyncCrawlUrl('mendable.ai', { excludePaths: ['blog/*'], limit: 5}); ``` ### Checking Crawl Status -To check the status of a crawl job with error handling, use the `checkCrawlStatus` method. It takes the job ID as a parameter and returns the current status of the crawl job. +To check the status of a crawl job with error handling, use the `checkCrawlStatus` method. It takes the job ID as a parameter and returns the current status of the crawl job` ```js const status = await app.checkCrawlStatus(id); @@ -121,6 +124,27 @@ const mapResult = await app.mapUrl('https://example.com') as MapResponse; console.log(mapResult) ``` +### Crawl a website with WebSockets + +To crawl a website with WebSockets, use the `crawlUrlAndWatch` method. It takes the starting URL and optional parameters as arguments. The `params` argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format. + +```js +// Crawl a website with WebSockets: +const watch = await app.crawlUrlAndWatch('mendable.ai', { excludePaths: ['blog/*'], limit: 5}); + +watch.addEventListener("document", doc => { + console.log("DOC", doc.detail); +}); + +watch.addEventListener("error", err => { + console.error("ERR", err.detail.error); +}); + +watch.addEventListener("done", state => { + console.log("DONE", state.detail.status); +}); +``` + ## Error Handling The SDK handles errors returned by the Firecrawl API and raises appropriate exceptions. If an error occurs during a request, an exception will be raised with a descriptive error message. The examples above demonstrate how to handle these errors using `try/catch` blocks. diff --git a/apps/js-sdk/test.ts b/apps/js-sdk/test.ts deleted file mode 100644 index 5419c2d5..00000000 --- a/apps/js-sdk/test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import FirecrawlApp from "@mendable/firecrawl-js"; -import { z } from "zod"; - -async function a() { - const app = new FirecrawlApp({ - apiKey: "fc-YOUR_API_KEY", - }); - - // Define schema to extract contents into - const schema = z.object({ - top: z - .array( - z.object({ - title: z.string(), - points: z.number(), - by: z.string(), - commentsURL: z.string(), - }) - ) - .length(5) - .describe("Top 5 stories on Hacker News"), - }); - const scrapeResult = await app.scrapeUrl("https://firecrawl.dev", { - extractorOptions: { extractionSchema: schema }, - }); - console.log(scrapeResult.data["llm_extraction"]); -} -a(); diff --git a/apps/python-sdk/README.md b/apps/python-sdk/README.md index 0cf36e9c..dcf44b25 100644 --- a/apps/python-sdk/README.md +++ b/apps/python-sdk/README.md @@ -81,22 +81,20 @@ print(data["llm_extraction"]) To crawl a website, use the `crawl_url` method. It takes the starting URL and optional parameters as arguments. The `params` argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format. -The `wait_until_done` parameter determines whether the method should wait for the crawl job to complete before returning the result. If set to `True`, the method will periodically check the status of the crawl job until it is completed or the specified `timeout` (in seconds) is reached. If set to `False`, the method will return immediately with the job ID, and you can manually check the status of the crawl job using the `check_crawl_status` method. - ```python -crawl_status = app.crawl_url( - 'https://firecrawl.dev', - params={ - 'limit': 100, - 'scrapeOptions': {'formats': ['markdown', 'html']} - }, - wait_until_done=True, - poll_interval=30 -) -print(crawl_status) +idempotency_key = str(uuid.uuid4()) # optional idempotency key +crawl_result = app.crawl_url('firecrawl.dev', {'excludePaths': ['blog/*']}, 2, idempotency_key) +print(crawl_result) ``` -If `wait_until_done` is set to `True`, the `crawl_url` method will return the crawl result once the job is completed. If the job fails or is stopped, an exception will be raised. +### Asynchronous Crawl a Website + +To crawl a website asynchronously, use the `async_crawl_url` method. It takes the starting URL and optional parameters as arguments. The `params` argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format. + +```python +crawl_result = app.async_crawl_url('firecrawl.dev', {'excludePaths': ['blog/*']}, "") +print(crawl_result) +``` ### Checking Crawl Status @@ -117,6 +115,41 @@ map_result = app.map_url('https://example.com') print(map_result) ``` +### Crawl a website with WebSockets + +To crawl a website with WebSockets, use the `crawl_url_and_watch` method. It takes the starting URL and optional parameters as arguments. The `params` argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format. + +```python +# inside an async function... +nest_asyncio.apply() + +# Define event handlers +def on_document(detail): + print("DOC", detail) + +def on_error(detail): + print("ERR", detail['error']) + +def on_done(detail): + print("DONE", detail['status']) + + # Function to start the crawl and watch process +async def start_crawl_and_watch(): + # Initiate the crawl job and get the watcher + watcher = app.crawl_url_and_watch('firecrawl.dev', { 'excludePaths': ['blog/*'], 'limit': 5 }) + + # Add event listeners + watcher.add_event_listener("document", on_document) + watcher.add_event_listener("error", on_error) + watcher.add_event_listener("done", on_done) + + # Start the watcher + await watcher.connect() + +# Run the event loop +await start_crawl_and_watch() +``` + ## Error Handling The SDK handles errors returned by the Firecrawl API and raises appropriate exceptions. If an error occurs during a request, an exception will be raised with a descriptive error message. diff --git a/apps/python-sdk/example.py b/apps/python-sdk/example.py index d80fa795..efb13939 100644 --- a/apps/python-sdk/example.py +++ b/apps/python-sdk/example.py @@ -1,3 +1,5 @@ +import time +import nest_asyncio import uuid from firecrawl.firecrawl import FirecrawlApp @@ -9,67 +11,119 @@ print(scrape_result['markdown']) # Crawl a website: idempotency_key = str(uuid.uuid4()) # optional idempotency key -crawl_result = app.crawl_url('mendable.ai', {'crawlerOptions': {'excludes': ['blog/*']}}, True, 2, idempotency_key) +crawl_result = app.crawl_url('firecrawl.dev', {'excludePaths': ['blog/*']}, 2, idempotency_key) print(crawl_result) +# Asynchronous Crawl a website: +async_result = app.async_crawl_url('firecrawl.dev', {'excludePaths': ['blog/*']}, "") +print(async_result) + +crawl_status = app.check_crawl_status(async_result['id']) +print(crawl_status) + +attempts = 15 +while attempts > 0 and crawl_status['status'] != 'completed': + print(crawl_status) + crawl_status = app.check_crawl_status(async_result['id']) + attempts -= 1 + time.sleep(1) + +crawl_status = app.get_crawl_status(async_result['id']) +print(crawl_status) + # LLM Extraction: # Define schema to extract contents into using pydantic -from pydantic import BaseModel, Field -from typing import List +# from pydantic import BaseModel, Field +# from typing import List -class ArticleSchema(BaseModel): - title: str - points: int - by: str - commentsURL: str +# class ArticleSchema(BaseModel): +# title: str +# points: int +# by: str +# commentsURL: str -class TopArticlesSchema(BaseModel): - top: List[ArticleSchema] = Field(..., max_items=5, description="Top 5 stories") +# class TopArticlesSchema(BaseModel): +# top: List[ArticleSchema] = Field(..., max_items=5, description="Top 5 stories") -llm_extraction_result = app.scrape_url('https://news.ycombinator.com', { - 'extractorOptions': { - 'extractionSchema': TopArticlesSchema.model_json_schema(), - 'mode': 'llm-extraction' - }, - 'pageOptions':{ - 'onlyMainContent': True - } -}) +# llm_extraction_result = app.scrape_url('https://news.ycombinator.com', { +# 'extractorOptions': { +# 'extractionSchema': TopArticlesSchema.model_json_schema(), +# 'mode': 'llm-extraction' +# }, +# 'pageOptions':{ +# 'onlyMainContent': True +# } +# }) -print(llm_extraction_result['llm_extraction']) +# print(llm_extraction_result['llm_extraction']) -# Define schema to extract contents into using json schema -json_schema = { - "type": "object", - "properties": { - "top": { - "type": "array", - "items": { - "type": "object", - "properties": { - "title": {"type": "string"}, - "points": {"type": "number"}, - "by": {"type": "string"}, - "commentsURL": {"type": "string"} - }, - "required": ["title", "points", "by", "commentsURL"] - }, - "minItems": 5, - "maxItems": 5, - "description": "Top 5 stories on Hacker News" - } - }, - "required": ["top"] -} +# # Define schema to extract contents into using json schema +# json_schema = { +# "type": "object", +# "properties": { +# "top": { +# "type": "array", +# "items": { +# "type": "object", +# "properties": { +# "title": {"type": "string"}, +# "points": {"type": "number"}, +# "by": {"type": "string"}, +# "commentsURL": {"type": "string"} +# }, +# "required": ["title", "points", "by", "commentsURL"] +# }, +# "minItems": 5, +# "maxItems": 5, +# "description": "Top 5 stories on Hacker News" +# } +# }, +# "required": ["top"] +# } -llm_extraction_result = app.scrape_url('https://news.ycombinator.com', { - 'extractorOptions': { - 'extractionSchema': json_schema, - 'mode': 'llm-extraction' - }, - 'pageOptions':{ - 'onlyMainContent': True - } -}) +# llm_extraction_result = app.scrape_url('https://news.ycombinator.com', { +# 'extractorOptions': { +# 'extractionSchema': json_schema, +# 'mode': 'llm-extraction' +# }, +# 'pageOptions':{ +# 'onlyMainContent': True +# } +# }) -print(llm_extraction_result['llm_extraction']) \ No newline at end of file +# print(llm_extraction_result['llm_extraction']) + + +# Map a website: +map_result = app.map_url('https://firecrawl.dev', { 'search': 'blog' }) +print(map_result) + +# Crawl a website with WebSockets: +# inside an async function... +nest_asyncio.apply() + +# Define event handlers +def on_document(detail): + print("DOC", detail) + +def on_error(detail): + print("ERR", detail['error']) + +def on_done(detail): + print("DONE", detail['status']) + + # Function to start the crawl and watch process +async def start_crawl_and_watch(): + # Initiate the crawl job and get the watcher + watcher = app.crawl_url_and_watch('firecrawl.dev', { 'excludePaths': ['blog/*'], 'limit': 5 }) + + # Add event listeners + watcher.add_event_listener("document", on_document) + watcher.add_event_listener("error", on_error) + watcher.add_event_listener("done", on_done) + + # Start the watcher + await watcher.connect() + +# Run the event loop +await start_crawl_and_watch() \ No newline at end of file diff --git a/apps/python-sdk/examplev0.py b/apps/python-sdk/examplev0.py deleted file mode 100644 index d80fa795..00000000 --- a/apps/python-sdk/examplev0.py +++ /dev/null @@ -1,75 +0,0 @@ -import uuid -from firecrawl.firecrawl import FirecrawlApp - -app = FirecrawlApp(api_key="fc-YOUR_API_KEY") - -# Scrape a website: -scrape_result = app.scrape_url('firecrawl.dev') -print(scrape_result['markdown']) - -# Crawl a website: -idempotency_key = str(uuid.uuid4()) # optional idempotency key -crawl_result = app.crawl_url('mendable.ai', {'crawlerOptions': {'excludes': ['blog/*']}}, True, 2, idempotency_key) -print(crawl_result) - -# LLM Extraction: -# Define schema to extract contents into using pydantic -from pydantic import BaseModel, Field -from typing import List - -class ArticleSchema(BaseModel): - title: str - points: int - by: str - commentsURL: str - -class TopArticlesSchema(BaseModel): - top: List[ArticleSchema] = Field(..., max_items=5, description="Top 5 stories") - -llm_extraction_result = app.scrape_url('https://news.ycombinator.com', { - 'extractorOptions': { - 'extractionSchema': TopArticlesSchema.model_json_schema(), - 'mode': 'llm-extraction' - }, - 'pageOptions':{ - 'onlyMainContent': True - } -}) - -print(llm_extraction_result['llm_extraction']) - -# Define schema to extract contents into using json schema -json_schema = { - "type": "object", - "properties": { - "top": { - "type": "array", - "items": { - "type": "object", - "properties": { - "title": {"type": "string"}, - "points": {"type": "number"}, - "by": {"type": "string"}, - "commentsURL": {"type": "string"} - }, - "required": ["title", "points", "by", "commentsURL"] - }, - "minItems": 5, - "maxItems": 5, - "description": "Top 5 stories on Hacker News" - } - }, - "required": ["top"] -} - -llm_extraction_result = app.scrape_url('https://news.ycombinator.com', { - 'extractorOptions': { - 'extractionSchema': json_schema, - 'mode': 'llm-extraction' - }, - 'pageOptions':{ - 'onlyMainContent': True - } -}) - -print(llm_extraction_result['llm_extraction']) \ No newline at end of file diff --git a/apps/python-sdk/firecrawl/firecrawl.py b/apps/python-sdk/firecrawl/firecrawl.py index 89c51803..b7a0bff6 100644 --- a/apps/python-sdk/firecrawl/firecrawl.py +++ b/apps/python-sdk/firecrawl/firecrawl.py @@ -12,29 +12,30 @@ Classes: import logging import os import time -from typing import Any, Dict, Optional +from typing import Any, Dict, Optional, List +import asyncio +import json import requests +import websockets logger : logging.Logger = logging.getLogger("firecrawl") class FirecrawlApp: - def __init__(self, api_key: Optional[str] = None, api_url: Optional[str] = None, version: str = 'v1') -> None: + def __init__(self, api_key: Optional[str] = None, api_url: Optional[str] = None) -> None: """ - Initialize the FirecrawlApp instance with API key, API URL, and version. + Initialize the FirecrawlApp instance with API key, API URL. Args: api_key (Optional[str]): API key for authenticating with the Firecrawl API. api_url (Optional[str]): Base URL for the Firecrawl API. - version (str): API version, either 'v0' or 'v1'. """ self.api_key = api_key or os.getenv('FIRECRAWL_API_KEY') self.api_url = api_url or os.getenv('FIRECRAWL_API_URL', 'https://api.firecrawl.dev') - self.version = version if self.api_key is None: logger.warning("No API key provided") raise ValueError('No API key provided') - logger.debug(f"Initialized FirecrawlApp with API key: {self.api_key} and version: {self.version}") + logger.debug(f"Initialized FirecrawlApp with API key: {self.api_key}") def scrape_url(self, url: str, params: Optional[Dict[str, Any]] = None) -> Any: """ @@ -74,7 +75,7 @@ class FirecrawlApp: if key != 'extractorOptions': scrape_params[key] = value - endpoint = f'/{self.version}/scrape' + endpoint = f'/v1/scrape' # Make the POST request with the prepared headers and JSON data response = requests.post( f'{self.api_url}{endpoint}', @@ -102,35 +103,14 @@ class FirecrawlApp: Any: The search results if the request is successful. Raises: + NotImplementedError: If the search request is attempted on API version v1. Exception: If the search request fails. """ - if self.version == 'v1': - raise NotImplementedError("Search is not supported in v1") - - headers = self._prepare_headers() - json_data = {'query': query} - if params: - json_data.update(params) - response = requests.post( - f'{self.api_url}/v0/search', - headers=headers, - json=json_data - ) - if response.status_code == 200: - response = response.json() - - if response['success'] and 'data' in response: - return response['data'] - else: - raise Exception(f'Failed to search. Error: {response["error"]}') - - else: - self._handle_error(response, 'search') + raise NotImplementedError("Search is not supported in v1.") def crawl_url(self, url: str, params: Optional[Dict[str, Any]] = None, - wait_until_done: bool = True, - poll_interval: int = 2, + poll_interval: Optional[int] = 2, idempotency_key: Optional[str] = None) -> Any: """ Initiate a crawl job for the specified URL using the Firecrawl API. @@ -138,8 +118,7 @@ class FirecrawlApp: Args: url (str): The URL to crawl. params (Optional[Dict[str, Any]]): Additional parameters for the crawl request. - wait_until_done (bool): Whether to wait until the crawl job is completed. - poll_interval (int): Time in seconds between status checks when waiting for job completion. + poll_interval (Optional[int]): Time in seconds between status checks when waiting for job completion. Defaults to 2 seconds. idempotency_key (Optional[str]): A unique uuid key to ensure idempotency of requests. Returns: @@ -148,28 +127,40 @@ class FirecrawlApp: Raises: Exception: If the crawl job initiation or monitoring fails. """ - endpoint = f'/{self.version}/crawl' + endpoint = f'/v1/crawl' headers = self._prepare_headers(idempotency_key) json_data = {'url': url} if params: json_data.update(params) response = self._post_request(f'{self.api_url}{endpoint}', json_data, headers) if response.status_code == 200: - if self.version == 'v0': - id = response.json().get('jobId') - else: - id = response.json().get('id') + id = response.json().get('id') + return self._monitor_job_status(id, headers, poll_interval) - if wait_until_done: - check_url = None - if self.version == 'v1': - check_url = response.json().get('url') - return self._monitor_job_status(id, headers, poll_interval, check_url) - else: - if self.version == 'v0': - return {'jobId': id} - else: - return {'id': id} + else: + self._handle_error(response, 'start crawl job') + + + def async_crawl_url(self, url: str, params: Optional[Dict[str, Any]] = None, idempotency_key: Optional[str] = None) -> Dict[str, Any]: + """ + Initiate a crawl job asynchronously. + + Args: + url (str): The URL to crawl. + params (Optional[Dict[str, Any]]): Additional parameters for the crawl request. + idempotency_key (Optional[str]): A unique uuid key to ensure idempotency of requests. + + Returns: + Dict[str, Any]: The response from the crawl initiation request. + """ + endpoint = f'/v1/crawl' + headers = self._prepare_headers(idempotency_key) + json_data = {'url': url} + if params: + json_data.update(params) + response = self._post_request(f'{self.api_url}{endpoint}', json_data, headers) + if response.status_code == 200: + return response.json() else: self._handle_error(response, 'start crawl job') @@ -186,50 +177,56 @@ class FirecrawlApp: Raises: Exception: If the status check request fails. """ - - if self.version == 'v0': - endpoint = f'/{self.version}/crawl/status/{id}' - else: - endpoint = f'/{self.version}/crawl/{id}' + endpoint = f'/v1/crawl/{id}' headers = self._prepare_headers() response = self._get_request(f'{self.api_url}{endpoint}', headers) if response.status_code == 200: data = response.json() - if self.version == 'v0': - return { - 'success': True, - 'status': data.get('status'), - 'current': data.get('current'), - 'current_url': data.get('current_url'), - 'current_step': data.get('current_step'), - 'total': data.get('total'), - 'data': data.get('data'), - 'partial_data': data.get('partial_data') if not data.get('data') else None, - } - elif self.version == 'v1': - return { - 'success': True, - 'status': data.get('status'), - 'total': data.get('total'), - 'completed': data.get('completed'), - 'creditsUsed': data.get('creditsUsed'), - 'expiresAt': data.get('expiresAt'), - 'next': data.get('next'), - 'data': data.get('data'), - 'error': data.get('error') - } + return { + 'success': True, + 'status': data.get('status'), + 'total': data.get('total'), + 'completed': data.get('completed'), + 'creditsUsed': data.get('creditsUsed'), + 'expiresAt': data.get('expiresAt'), + 'next': data.get('next'), + 'data': data.get('data'), + 'error': data.get('error') + } else: self._handle_error(response, 'check crawl status') + def crawl_url_and_watch(self, url: str, params: Optional[Dict[str, Any]] = None, idempotency_key: Optional[str] = None) -> 'CrawlWatcher': + """ + Initiate a crawl job and return a CrawlWatcher to monitor the job via WebSocket. + + Args: + url (str): The URL to crawl. + params (Optional[Dict[str, Any]]): Additional parameters for the crawl request. + idempotency_key (Optional[str]): A unique uuid key to ensure idempotency of requests. + + Returns: + CrawlWatcher: An instance of CrawlWatcher to monitor the crawl job. + """ + crawl_response = self.async_crawl_url(url, params, idempotency_key) + if crawl_response['success'] and 'id' in crawl_response: + return CrawlWatcher(crawl_response['id'], self) + else: + raise Exception("Crawl job failed to start") + def map_url(self, url: str, params: Optional[Dict[str, Any]] = None) -> Any: """ Perform a map search using the Firecrawl API. + + Args: + url (str): The URL to perform the map search on. + params (Optional[Dict[str, Any]]): Additional parameters for the map search. + + Returns: + Any: The result of the map search, typically a dictionary containing mapping data. """ - if self.version == 'v0': - raise NotImplementedError("Map is not supported in v0") - - endpoint = f'/{self.version}/map' + endpoint = f'/v1/map' headers = self._prepare_headers() # Prepare the base scrape parameters with the URL @@ -331,7 +328,7 @@ class FirecrawlApp: return response return response - def _monitor_job_status(self, id: str, headers: Dict[str, str], poll_interval: int, check_url: Optional[str] = None) -> Any: + def _monitor_job_status(self, id: str, headers: Dict[str, str], poll_interval: int) -> Any: """ Monitor the status of a crawl job until completion. @@ -339,7 +336,6 @@ class FirecrawlApp: id (str): The ID of the crawl job. headers (Dict[str, str]): The headers to include in the status check requests. poll_interval (int): Secounds between status checks. - check_url (Optional[str]): The URL to check for the crawl job. Returns: Any: The crawl results if the job is completed successfully. @@ -347,27 +343,14 @@ class FirecrawlApp: Exception: If the job fails or an error occurs during status checks. """ while True: - api_url = '' - if (self.version == 'v0'): - if check_url: - api_url = check_url - else: - api_url = f'{self.api_url}/v0/crawl/status/{id}' - else: - if check_url: - api_url = check_url - else: - api_url = f'{self.api_url}/v1/crawl/{id}' + api_url = f'{self.api_url}/v1/crawl/{id}' status_response = self._get_request(api_url, headers) if status_response.status_code == 200: status_data = status_response.json() if status_data['status'] == 'completed': if 'data' in status_data: - if self.version == 'v0': - return status_data['data'] - else: - return status_data + return status_data else: raise Exception('Crawl job completed but no data was returned') elif status_data['status'] in ['active', 'paused', 'pending', 'queued', 'waiting', 'scraping']: @@ -405,4 +388,50 @@ class FirecrawlApp: # Raise an HTTPError with the custom message and attach the response raise requests.exceptions.HTTPError(message, response=response) - \ No newline at end of file + +class CrawlWatcher: + def __init__(self, id: str, app: FirecrawlApp): + self.id = id + self.app = app + self.data: List[Dict[str, Any]] = [] + self.status = "scraping" + self.ws_url = f"{app.api_url.replace('http', 'ws')}/v1/crawl/{id}" + self.event_handlers = { + 'done': [], + 'error': [], + 'document': [] + } + + async def connect(self): + async with websockets.connect(self.ws_url, extra_headers={"Authorization": f"Bearer {self.app.api_key}"}) as websocket: + await self._listen(websocket) + + async def _listen(self, websocket): + async for message in websocket: + msg = json.loads(message) + await self._handle_message(msg) + + def add_event_listener(self, event_type: str, handler): + if event_type in self.event_handlers: + self.event_handlers[event_type].append(handler) + + def dispatch_event(self, event_type: str, detail: Dict[str, Any]): + if event_type in self.event_handlers: + for handler in self.event_handlers[event_type]: + handler(detail) + + async def _handle_message(self, msg: Dict[str, Any]): + if msg['type'] == 'done': + self.status = 'completed' + self.dispatch_event('done', {'status': self.status, 'data': self.data}) + elif msg['type'] == 'error': + self.status = 'failed' + self.dispatch_event('error', {'status': self.status, 'data': self.data, 'error': msg['error']}) + elif msg['type'] == 'catchup': + self.status = msg['data']['status'] + self.data.extend(msg['data'].get('data', [])) + for doc in self.data: + self.dispatch_event('document', doc) + elif msg['type'] == 'document': + self.data.append(msg['data']) + self.dispatch_event('document', msg['data']) \ No newline at end of file diff --git a/apps/python-sdk/requirements.txt b/apps/python-sdk/requirements.txt index 1bed5881..94971fde 100644 --- a/apps/python-sdk/requirements.txt +++ b/apps/python-sdk/requirements.txt @@ -1,3 +1,6 @@ requests pytest -python-dotenv \ No newline at end of file +python-dotenv +websockets +asyncio +nest-asyncio \ No newline at end of file From 52ac1323280fb849103301101a4fc4cb8a8e4c23 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 30 Aug 2024 11:10:48 -0300 Subject: [PATCH 5/9] Update auth.ts --- apps/api/src/controllers/auth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts index 5df6a857..d634b9ed 100644 --- a/apps/api/src/controllers/auth.ts +++ b/apps/api/src/controllers/auth.ts @@ -104,7 +104,7 @@ export async function supaAuthenticateUser( status?: number; plan?: PlanType; }> { - console.log(req.headers); + const authHeader = req.headers.authorization ?? (req.headers["sec-websocket-protocol"] ? `Bearer ${req.headers["sec-websocket-protocol"]}` : null); if (!authHeader) { return { success: false, error: "Unauthorized", status: 401 }; From 234c6daee864b4f53b2da33e4cbebb87cecd60eb Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 30 Aug 2024 14:52:59 -0300 Subject: [PATCH 6/9] Update supabase-jobs.ts --- apps/api/src/lib/supabase-jobs.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/apps/api/src/lib/supabase-jobs.ts b/apps/api/src/lib/supabase-jobs.ts index b4247883..8ff46a23 100644 --- a/apps/api/src/lib/supabase-jobs.ts +++ b/apps/api/src/lib/supabase-jobs.ts @@ -1,10 +1,12 @@ import { supabase_service } from "../services/supabase"; +import { Logger } from "./logger"; +import * as Sentry from "@sentry/node"; export const supabaseGetJobById = async (jobId: string) => { const { data, error } = await supabase_service - .from('firecrawl_jobs') - .select('*') - .eq('job_id', jobId) + .from("firecrawl_jobs") + .select("*") + .eq("job_id", jobId) .single(); if (error) { @@ -16,15 +18,16 @@ export const supabaseGetJobById = async (jobId: string) => { } return data; -} +}; export const supabaseGetJobsById = async (jobIds: string[]) => { - const { data, error } = await supabase_service - .from('firecrawl_jobs') - .select('*') - .in('job_id', jobIds); + const { data, error } = await supabase_service.rpc("get_jobs_by_ids", { + job_ids: jobIds, + }); if (error) { + Logger.error(`Error in get_jobs_by_ids: ${error}`); + Sentry.captureException(error); return []; } @@ -33,5 +36,4 @@ export const supabaseGetJobsById = async (jobIds: string[]) => { } return data; -} - +}; From d7dbc2536dbf49ce7496d17e3a9812dcf607cf36 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 30 Aug 2024 15:21:22 -0300 Subject: [PATCH 7/9] Update crawl.ts --- apps/api/src/controllers/v1/crawl.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apps/api/src/controllers/v1/crawl.ts b/apps/api/src/controllers/v1/crawl.ts index f4c4586f..fd72c8cf 100644 --- a/apps/api/src/controllers/v1/crawl.ts +++ b/apps/api/src/controllers/v1/crawl.ts @@ -110,6 +110,7 @@ export async function crawlController( origin: "api", crawl_id: id, sitemapped: true, + webhook: req.body.webhook, v1: true, }, opts: { @@ -155,3 +156,5 @@ export async function crawlController( url: `${req.protocol}://${req.get("host")}/v1/crawl/${id}`, }); } + + From 6a6b487474009c9aeff5e891422c8d9d8ef1c54f Mon Sep 17 00:00:00 2001 From: Gergo Moricz Date: Fri, 30 Aug 2024 20:28:18 +0200 Subject: [PATCH 8/9] fix(v1): don't fail on doc = null --- apps/api/src/controllers/v1/types.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/api/src/controllers/v1/types.ts b/apps/api/src/controllers/v1/types.ts index 20f1b775..12d1c501 100644 --- a/apps/api/src/controllers/v1/types.ts +++ b/apps/api/src/controllers/v1/types.ts @@ -292,6 +292,8 @@ export function legacyScrapeOptions(x: ScrapeOptions): PageOptions { } export function legacyDocumentConverter(doc: any): Document { + if (doc === null || doc === undefined) return doc; + if (doc.metadata) { if (doc.metadata.screenshot) { doc.screenshot = doc.metadata.screenshot; From 282962e36f15fb8787fe6db163808c0adc164bb0 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 30 Aug 2024 15:29:41 -0300 Subject: [PATCH 9/9] Nick: --- apps/js-sdk/firecrawl/build/cjs/index.js | 257 ++++++++++++----------- apps/js-sdk/firecrawl/build/esm/index.js | 255 +++++++++++----------- apps/js-sdk/firecrawl/package-lock.json | 4 +- apps/js-sdk/firecrawl/types/index.d.ts | 211 +++++-------------- apps/python-sdk/firecrawl/__init__.py | 2 +- apps/python-sdk/pyproject.toml | 4 + apps/python-sdk/setup.py | 3 + 7 files changed, 320 insertions(+), 416 deletions(-) diff --git a/apps/js-sdk/firecrawl/build/cjs/index.js b/apps/js-sdk/firecrawl/build/cjs/index.js index c6e93e00..7b0730f5 100644 --- a/apps/js-sdk/firecrawl/build/cjs/index.js +++ b/apps/js-sdk/firecrawl/build/cjs/index.js @@ -3,9 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); +exports.CrawlWatcher = void 0; const axios_1 = __importDefault(require("axios")); const zod_1 = require("zod"); const zod_to_json_schema_1 = require("zod-to-json-schema"); +const isows_1 = require("isows"); +const typescript_event_target_1 = require("typescript-event-target"); /** * Main class for interacting with the Firecrawl API. * Provides methods for scraping, searching, crawling, and mapping web content. @@ -15,13 +18,9 @@ class FirecrawlApp { * Initializes a new instance of the FirecrawlApp class. * @param config - Configuration options for the FirecrawlApp instance. */ - constructor({ apiKey = null, apiUrl = null, version = "v1" }) { + constructor({ apiKey = null, apiUrl = null }) { this.apiKey = apiKey || ""; this.apiUrl = apiUrl || "https://api.firecrawl.dev"; - this.version = version; - if (!this.apiKey) { - throw new Error("No API key provided"); - } } /** * Scrapes a URL using the Firecrawl API. @@ -51,16 +50,16 @@ class FirecrawlApp { }; } try { - const response = await axios_1.default.post(this.apiUrl + `/${this.version}/scrape`, jsonData, { headers }); + const response = await axios_1.default.post(this.apiUrl + `/v1/scrape`, jsonData, { headers }); if (response.status === 200) { const responseData = response.data; if (responseData.success) { - return (this.version === 'v0' ? responseData : { + return { success: true, warning: responseData.warning, error: responseData.error, ...responseData.data - }); + }; } else { throw new Error(`Failed to scrape URL. Error: ${responseData.error}`); @@ -76,80 +75,52 @@ class FirecrawlApp { return { success: false, error: "Internal server error." }; } /** - * Searches for a query using the Firecrawl API. - * @param query - The query to search for. - * @param params - Additional parameters for the search request. - * @returns The response from the search operation. + * This method is intended to search for a query using the Firecrawl API. However, it is not supported in version 1 of the API. + * @param query - The search query string. + * @param params - Additional parameters for the search. + * @returns Throws an error advising to use version 0 of the API. */ async search(query, params) { - if (this.version === "v1") { - throw new Error("Search is not supported in v1, please update FirecrawlApp() initialization to use v0."); - } - const headers = { - "Content-Type": "application/json", - Authorization: `Bearer ${this.apiKey}`, - }; - let jsonData = { query }; - if (params) { - jsonData = { ...jsonData, ...params }; - } - try { - const response = await axios_1.default.post(this.apiUrl + "/v0/search", jsonData, { headers }); - if (response.status === 200) { - const responseData = response.data; - if (responseData.success) { - return responseData; - } - else { - throw new Error(`Failed to search. Error: ${responseData.error}`); - } - } - else { - this.handleError(response, "search"); - } - } - catch (error) { - throw new Error(error.message); - } - return { success: false, error: "Internal server error." }; + throw new Error("Search is not supported in v1, please update FirecrawlApp() initialization to use v0."); } /** * Initiates a crawl job for a URL using the Firecrawl API. * @param url - The URL to crawl. * @param params - Additional parameters for the crawl request. - * @param waitUntilDone - Whether to wait for the crawl job to complete. * @param pollInterval - Time in seconds for job status checks. * @param idempotencyKey - Optional idempotency key for the request. * @returns The response from the crawl operation. */ - async crawlUrl(url, params, waitUntilDone = true, pollInterval = 2, idempotencyKey) { + async crawlUrl(url, params, pollInterval = 2, idempotencyKey) { const headers = this.prepareHeaders(idempotencyKey); let jsonData = { url, ...params }; try { - const response = await this.postRequest(this.apiUrl + `/${this.version}/crawl`, jsonData, headers); + const response = await this.postRequest(this.apiUrl + `/v1/crawl`, jsonData, headers); if (response.status === 200) { - const id = this.version === 'v0' ? response.data.jobId : response.data.id; - let checkUrl = undefined; - if (waitUntilDone) { - if (this.version === 'v1') { - checkUrl = response.data.url; - } - return this.monitorJobStatus(id, headers, pollInterval, checkUrl); - } - else { - if (this.version === 'v0') { - return { - success: true, - jobId: id - }; - } - else { - return { - success: true, - id: id - }; - } - } + const id = response.data.id; + return this.monitorJobStatus(id, headers, pollInterval); + } + else { + this.handleError(response, "start crawl job"); + } + } + catch (error) { + if (error.response?.data?.error) { + throw new Error(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`); + } + else { + throw new Error(error.message); + } + } + return { success: false, error: "Internal server error." }; + } + async asyncCrawlUrl(url, params, idempotencyKey) { + const headers = this.prepareHeaders(idempotencyKey); + let jsonData = { url, ...params }; + try { + const response = await this.postRequest(this.apiUrl + `/v1/crawl`, jsonData, headers); + if (response.status === 200) { + return response.data; } else { this.handleError(response, "start crawl job"); @@ -176,37 +147,19 @@ class FirecrawlApp { } const headers = this.prepareHeaders(); try { - const response = await this.getRequest(this.version === 'v1' ? - `${this.apiUrl}/${this.version}/crawl/${id}` : - `${this.apiUrl}/${this.version}/crawl/status/${id}`, headers); + const response = await this.getRequest(`${this.apiUrl}/v1/crawl/${id}`, headers); if (response.status === 200) { - if (this.version === 'v0') { - return { - success: true, - status: response.data.status, - current: response.data.current, - current_url: response.data.current_url, - current_step: response.data.current_step, - total: response.data.total, - data: response.data.data, - partial_data: !response.data.data - ? response.data.partial_data - : undefined, - }; - } - else { - return { - success: true, - status: response.data.status, - total: response.data.total, - completed: response.data.completed, - creditsUsed: response.data.creditsUsed, - expiresAt: new Date(response.data.expiresAt), - next: response.data.next, - data: response.data.data, - error: response.data.error - }; - } + return ({ + success: true, + status: response.data.status, + total: response.data.total, + completed: response.data.completed, + creditsUsed: response.data.creditsUsed, + expiresAt: new Date(response.data.expiresAt), + next: response.data.next, + data: response.data.data, + error: response.data.error + }); } else { this.handleError(response, "check crawl status"); @@ -215,29 +168,21 @@ class FirecrawlApp { catch (error) { throw new Error(error.message); } - return this.version === 'v0' ? - { - success: false, - status: "unknown", - current: 0, - current_url: "", - current_step: "", - total: 0, - error: "Internal server error.", - } : - { - success: false, - error: "Internal server error.", - }; + return { success: false, error: "Internal server error." }; + } + async crawlUrlAndWatch(url, params, idempotencyKey) { + const crawl = await this.asyncCrawlUrl(url, params, idempotencyKey); + if (crawl.success && crawl.id) { + const id = crawl.id; + return new CrawlWatcher(id, this); + } + throw new Error("Crawl job failed to start"); } async mapUrl(url, params) { - if (this.version == 'v0') { - throw new Error("Map is not supported in v0"); - } const headers = this.prepareHeaders(); let jsonData = { url, ...params }; try { - const response = await this.postRequest(this.apiUrl + `/${this.version}/map`, jsonData, headers); + const response = await this.postRequest(this.apiUrl + `/v1/map`, jsonData, headers); if (response.status === 200) { return response.data; } @@ -289,21 +234,14 @@ class FirecrawlApp { * @param checkUrl - Optional URL to check the status (used for v1 API) * @returns The final job status or data. */ - async monitorJobStatus(id, headers, checkInterval, checkUrl) { - let apiUrl = ''; + async monitorJobStatus(id, headers, checkInterval) { while (true) { - if (this.version === 'v1') { - apiUrl = checkUrl ?? `${this.apiUrl}/v1/crawl/${id}`; - } - else if (this.version === 'v0') { - apiUrl = `${this.apiUrl}/v0/crawl/status/${id}`; - } - const statusResponse = await this.getRequest(apiUrl, headers); + const statusResponse = await this.getRequest(`${this.apiUrl}/v1/crawl/${id}`, headers); if (statusResponse.status === 200) { const statusData = statusResponse.data; if (statusData.status === "completed") { if ("data" in statusData) { - return this.version === 'v0' ? statusData.data : statusData; + return statusData; } else { throw new Error("Crawl job completed but no data was returned"); @@ -338,3 +276,72 @@ class FirecrawlApp { } } exports.default = FirecrawlApp; +class CrawlWatcher extends typescript_event_target_1.TypedEventTarget { + constructor(id, app) { + super(); + this.ws = new isows_1.WebSocket(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey); + this.status = "scraping"; + this.data = []; + const messageHandler = (msg) => { + if (msg.type === "done") { + this.status = "completed"; + this.dispatchTypedEvent("done", new CustomEvent("done", { + detail: { + status: this.status, + data: this.data, + }, + })); + } + else if (msg.type === "error") { + this.status = "failed"; + this.dispatchTypedEvent("error", new CustomEvent("error", { + detail: { + status: this.status, + data: this.data, + error: msg.error, + }, + })); + } + else if (msg.type === "catchup") { + this.status = msg.data.status; + this.data.push(...(msg.data.data ?? [])); + for (const doc of this.data) { + this.dispatchTypedEvent("document", new CustomEvent("document", { + detail: doc, + })); + } + } + else if (msg.type === "document") { + this.dispatchTypedEvent("document", new CustomEvent("document", { + detail: msg.data, + })); + } + }; + this.ws.onmessage = ((ev) => { + if (typeof ev.data !== "string") { + this.ws.close(); + return; + } + const msg = JSON.parse(ev.data); + messageHandler(msg); + }).bind(this); + this.ws.onclose = ((ev) => { + const msg = JSON.parse(ev.reason); + messageHandler(msg); + }).bind(this); + this.ws.onerror = ((_) => { + this.status = "failed"; + this.dispatchTypedEvent("error", new CustomEvent("error", { + detail: { + status: this.status, + data: this.data, + error: "WebSocket error", + }, + })); + }).bind(this); + } + close() { + this.ws.close(); + } +} +exports.CrawlWatcher = CrawlWatcher; diff --git a/apps/js-sdk/firecrawl/build/esm/index.js b/apps/js-sdk/firecrawl/build/esm/index.js index 3491a673..cccd1770 100644 --- a/apps/js-sdk/firecrawl/build/esm/index.js +++ b/apps/js-sdk/firecrawl/build/esm/index.js @@ -1,6 +1,8 @@ import axios from "axios"; import { z } from "zod"; import { zodToJsonSchema } from "zod-to-json-schema"; +import { WebSocket } from "isows"; +import { TypedEventTarget } from "typescript-event-target"; /** * Main class for interacting with the Firecrawl API. * Provides methods for scraping, searching, crawling, and mapping web content. @@ -10,13 +12,9 @@ export default class FirecrawlApp { * Initializes a new instance of the FirecrawlApp class. * @param config - Configuration options for the FirecrawlApp instance. */ - constructor({ apiKey = null, apiUrl = null, version = "v1" }) { + constructor({ apiKey = null, apiUrl = null }) { this.apiKey = apiKey || ""; this.apiUrl = apiUrl || "https://api.firecrawl.dev"; - this.version = version; - if (!this.apiKey) { - throw new Error("No API key provided"); - } } /** * Scrapes a URL using the Firecrawl API. @@ -46,16 +44,16 @@ export default class FirecrawlApp { }; } try { - const response = await axios.post(this.apiUrl + `/${this.version}/scrape`, jsonData, { headers }); + const response = await axios.post(this.apiUrl + `/v1/scrape`, jsonData, { headers }); if (response.status === 200) { const responseData = response.data; if (responseData.success) { - return (this.version === 'v0' ? responseData : { + return { success: true, warning: responseData.warning, error: responseData.error, ...responseData.data - }); + }; } else { throw new Error(`Failed to scrape URL. Error: ${responseData.error}`); @@ -71,80 +69,52 @@ export default class FirecrawlApp { return { success: false, error: "Internal server error." }; } /** - * Searches for a query using the Firecrawl API. - * @param query - The query to search for. - * @param params - Additional parameters for the search request. - * @returns The response from the search operation. + * This method is intended to search for a query using the Firecrawl API. However, it is not supported in version 1 of the API. + * @param query - The search query string. + * @param params - Additional parameters for the search. + * @returns Throws an error advising to use version 0 of the API. */ async search(query, params) { - if (this.version === "v1") { - throw new Error("Search is not supported in v1, please update FirecrawlApp() initialization to use v0."); - } - const headers = { - "Content-Type": "application/json", - Authorization: `Bearer ${this.apiKey}`, - }; - let jsonData = { query }; - if (params) { - jsonData = { ...jsonData, ...params }; - } - try { - const response = await axios.post(this.apiUrl + "/v0/search", jsonData, { headers }); - if (response.status === 200) { - const responseData = response.data; - if (responseData.success) { - return responseData; - } - else { - throw new Error(`Failed to search. Error: ${responseData.error}`); - } - } - else { - this.handleError(response, "search"); - } - } - catch (error) { - throw new Error(error.message); - } - return { success: false, error: "Internal server error." }; + throw new Error("Search is not supported in v1, please update FirecrawlApp() initialization to use v0."); } /** * Initiates a crawl job for a URL using the Firecrawl API. * @param url - The URL to crawl. * @param params - Additional parameters for the crawl request. - * @param waitUntilDone - Whether to wait for the crawl job to complete. * @param pollInterval - Time in seconds for job status checks. * @param idempotencyKey - Optional idempotency key for the request. * @returns The response from the crawl operation. */ - async crawlUrl(url, params, waitUntilDone = true, pollInterval = 2, idempotencyKey) { + async crawlUrl(url, params, pollInterval = 2, idempotencyKey) { const headers = this.prepareHeaders(idempotencyKey); let jsonData = { url, ...params }; try { - const response = await this.postRequest(this.apiUrl + `/${this.version}/crawl`, jsonData, headers); + const response = await this.postRequest(this.apiUrl + `/v1/crawl`, jsonData, headers); if (response.status === 200) { - const id = this.version === 'v0' ? response.data.jobId : response.data.id; - let checkUrl = undefined; - if (waitUntilDone) { - if (this.version === 'v1') { - checkUrl = response.data.url; - } - return this.monitorJobStatus(id, headers, pollInterval, checkUrl); - } - else { - if (this.version === 'v0') { - return { - success: true, - jobId: id - }; - } - else { - return { - success: true, - id: id - }; - } - } + const id = response.data.id; + return this.monitorJobStatus(id, headers, pollInterval); + } + else { + this.handleError(response, "start crawl job"); + } + } + catch (error) { + if (error.response?.data?.error) { + throw new Error(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`); + } + else { + throw new Error(error.message); + } + } + return { success: false, error: "Internal server error." }; + } + async asyncCrawlUrl(url, params, idempotencyKey) { + const headers = this.prepareHeaders(idempotencyKey); + let jsonData = { url, ...params }; + try { + const response = await this.postRequest(this.apiUrl + `/v1/crawl`, jsonData, headers); + if (response.status === 200) { + return response.data; } else { this.handleError(response, "start crawl job"); @@ -171,37 +141,19 @@ export default class FirecrawlApp { } const headers = this.prepareHeaders(); try { - const response = await this.getRequest(this.version === 'v1' ? - `${this.apiUrl}/${this.version}/crawl/${id}` : - `${this.apiUrl}/${this.version}/crawl/status/${id}`, headers); + const response = await this.getRequest(`${this.apiUrl}/v1/crawl/${id}`, headers); if (response.status === 200) { - if (this.version === 'v0') { - return { - success: true, - status: response.data.status, - current: response.data.current, - current_url: response.data.current_url, - current_step: response.data.current_step, - total: response.data.total, - data: response.data.data, - partial_data: !response.data.data - ? response.data.partial_data - : undefined, - }; - } - else { - return { - success: true, - status: response.data.status, - total: response.data.total, - completed: response.data.completed, - creditsUsed: response.data.creditsUsed, - expiresAt: new Date(response.data.expiresAt), - next: response.data.next, - data: response.data.data, - error: response.data.error - }; - } + return ({ + success: true, + status: response.data.status, + total: response.data.total, + completed: response.data.completed, + creditsUsed: response.data.creditsUsed, + expiresAt: new Date(response.data.expiresAt), + next: response.data.next, + data: response.data.data, + error: response.data.error + }); } else { this.handleError(response, "check crawl status"); @@ -210,29 +162,21 @@ export default class FirecrawlApp { catch (error) { throw new Error(error.message); } - return this.version === 'v0' ? - { - success: false, - status: "unknown", - current: 0, - current_url: "", - current_step: "", - total: 0, - error: "Internal server error.", - } : - { - success: false, - error: "Internal server error.", - }; + return { success: false, error: "Internal server error." }; + } + async crawlUrlAndWatch(url, params, idempotencyKey) { + const crawl = await this.asyncCrawlUrl(url, params, idempotencyKey); + if (crawl.success && crawl.id) { + const id = crawl.id; + return new CrawlWatcher(id, this); + } + throw new Error("Crawl job failed to start"); } async mapUrl(url, params) { - if (this.version == 'v0') { - throw new Error("Map is not supported in v0"); - } const headers = this.prepareHeaders(); let jsonData = { url, ...params }; try { - const response = await this.postRequest(this.apiUrl + `/${this.version}/map`, jsonData, headers); + const response = await this.postRequest(this.apiUrl + `/v1/map`, jsonData, headers); if (response.status === 200) { return response.data; } @@ -284,21 +228,14 @@ export default class FirecrawlApp { * @param checkUrl - Optional URL to check the status (used for v1 API) * @returns The final job status or data. */ - async monitorJobStatus(id, headers, checkInterval, checkUrl) { - let apiUrl = ''; + async monitorJobStatus(id, headers, checkInterval) { while (true) { - if (this.version === 'v1') { - apiUrl = checkUrl ?? `${this.apiUrl}/v1/crawl/${id}`; - } - else if (this.version === 'v0') { - apiUrl = `${this.apiUrl}/v0/crawl/status/${id}`; - } - const statusResponse = await this.getRequest(apiUrl, headers); + const statusResponse = await this.getRequest(`${this.apiUrl}/v1/crawl/${id}`, headers); if (statusResponse.status === 200) { const statusData = statusResponse.data; if (statusData.status === "completed") { if ("data" in statusData) { - return this.version === 'v0' ? statusData.data : statusData; + return statusData; } else { throw new Error("Crawl job completed but no data was returned"); @@ -332,3 +269,71 @@ export default class FirecrawlApp { } } } +export class CrawlWatcher extends TypedEventTarget { + constructor(id, app) { + super(); + this.ws = new WebSocket(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey); + this.status = "scraping"; + this.data = []; + const messageHandler = (msg) => { + if (msg.type === "done") { + this.status = "completed"; + this.dispatchTypedEvent("done", new CustomEvent("done", { + detail: { + status: this.status, + data: this.data, + }, + })); + } + else if (msg.type === "error") { + this.status = "failed"; + this.dispatchTypedEvent("error", new CustomEvent("error", { + detail: { + status: this.status, + data: this.data, + error: msg.error, + }, + })); + } + else if (msg.type === "catchup") { + this.status = msg.data.status; + this.data.push(...(msg.data.data ?? [])); + for (const doc of this.data) { + this.dispatchTypedEvent("document", new CustomEvent("document", { + detail: doc, + })); + } + } + else if (msg.type === "document") { + this.dispatchTypedEvent("document", new CustomEvent("document", { + detail: msg.data, + })); + } + }; + this.ws.onmessage = ((ev) => { + if (typeof ev.data !== "string") { + this.ws.close(); + return; + } + const msg = JSON.parse(ev.data); + messageHandler(msg); + }).bind(this); + this.ws.onclose = ((ev) => { + const msg = JSON.parse(ev.reason); + messageHandler(msg); + }).bind(this); + this.ws.onerror = ((_) => { + this.status = "failed"; + this.dispatchTypedEvent("error", new CustomEvent("error", { + detail: { + status: this.status, + data: this.data, + error: "WebSocket error", + }, + })); + }).bind(this); + } + close() { + this.ws.close(); + } +} diff --git a/apps/js-sdk/firecrawl/package-lock.json b/apps/js-sdk/firecrawl/package-lock.json index 7f25babc..ce6a1a4a 100644 --- a/apps/js-sdk/firecrawl/package-lock.json +++ b/apps/js-sdk/firecrawl/package-lock.json @@ -1,12 +1,12 @@ { "name": "@mendable/firecrawl-js", - "version": "1.0.3", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@mendable/firecrawl-js", - "version": "1.0.3", + "version": "1.1.0", "license": "MIT", "dependencies": { "axios": "^1.6.8", diff --git a/apps/js-sdk/firecrawl/types/index.d.ts b/apps/js-sdk/firecrawl/types/index.d.ts index 3ca10744..6b5166b3 100644 --- a/apps/js-sdk/firecrawl/types/index.d.ts +++ b/apps/js-sdk/firecrawl/types/index.d.ts @@ -1,15 +1,13 @@ import { AxiosResponse, AxiosRequestHeaders } from "axios"; -import { z } from "zod"; +import { TypedEventTarget } from "typescript-event-target"; /** * Configuration interface for FirecrawlApp. * @param apiKey - Optional API key for authentication. * @param apiUrl - Optional base URL of the API; defaults to 'https://api.firecrawl.dev'. - * @param version - API version, either 'v0' or 'v1'. */ export interface FirecrawlAppConfig { apiKey?: string | null; apiUrl?: string | null; - version?: "v0" | "v1"; } /** * Metadata for a Firecrawl document. @@ -50,15 +48,6 @@ export interface FirecrawlDocumentMetadata { error?: string; [key: string]: any; } -/** - * Metadata for a Firecrawl document on v0. - * Similar to FirecrawlDocumentMetadata but includes properties specific to API version v0. - */ -export interface FirecrawlDocumentMetadataV0 { - pageStatusCode?: number; - pageError?: string; - [key: string]: any; -} /** * Document interface for Firecrawl. * Represents a document retrieved or processed by Firecrawl. @@ -70,84 +59,30 @@ export interface FirecrawlDocument { rawHtml?: string; links?: string[]; screenshot?: string; - metadata: FirecrawlDocumentMetadata; -} -/** - * Document interface for Firecrawl on v0. - * Represents a document specifically for API version v0 with additional properties. - */ -export interface FirecrawlDocumentV0 { - id?: string; - url?: string; - content: string; - markdown?: string; - html?: string; - llm_extraction?: Record; - createdAt?: Date; - updatedAt?: Date; - type?: string; - metadata: FirecrawlDocumentMetadataV0; - childrenLinks?: string[]; - provider?: string; - warning?: string; - index?: number; + metadata?: FirecrawlDocumentMetadata; } /** * Parameters for scraping operations. * Defines the options and configurations available for scraping web content. */ export interface ScrapeParams { - formats: ("markdown" | "html" | "rawHtml" | "content" | "links" | "screenshot")[]; + formats: ("markdown" | "html" | "rawHtml" | "content" | "links" | "screenshot" | "full@scrennshot")[]; headers?: Record; includeTags?: string[]; excludeTags?: string[]; onlyMainContent?: boolean; - screenshotMode?: "desktop" | "full-desktop" | "mobile" | "full-mobile"; waitFor?: number; timeout?: number; } -/** - * Parameters for scraping operations on v0. - * Includes page and extractor options specific to API version v0. - */ -export interface ScrapeParamsV0 { - pageOptions?: { - headers?: Record; - includeHtml?: boolean; - includeRawHtml?: boolean; - onlyIncludeTags?: string[]; - onlyMainContent?: boolean; - removeTags?: string[]; - replaceAllPathsWithAbsolutePaths?: boolean; - screenshot?: boolean; - fullPageScreenshot?: boolean; - waitFor?: number; - }; - extractorOptions?: { - mode?: "markdown" | "llm-extraction" | "llm-extraction-from-raw-html" | "llm-extraction-from-markdown"; - extractionPrompt?: string; - extractionSchema?: Record | z.ZodSchema | any; - }; - timeout?: number; -} /** * Response interface for scraping operations. * Defines the structure of the response received after a scraping operation. */ export interface ScrapeResponse extends FirecrawlDocument { - success: boolean; + success: true; warning?: string; error?: string; } -/** - * Response interface for scraping operations on v0. - * Similar to ScrapeResponse but tailored for responses from API version v0. - */ -export interface ScrapeResponseV0 { - success: boolean; - data?: FirecrawlDocumentV0; - error?: string; -} /** * Parameters for crawling operations. * Includes options for both scraping and mapping during a crawl. @@ -162,36 +97,6 @@ export interface CrawlParams { ignoreSitemap?: boolean; scrapeOptions?: ScrapeParams; } -/** - * Parameters for crawling operations on v0. - * Tailored for API version v0, includes specific options for crawling. - */ -export interface CrawlParamsV0 { - crawlerOptions?: { - includes?: string[]; - excludes?: string[]; - generateImgAltText?: boolean; - returnOnlyUrls?: boolean; - maxDepth?: number; - mode?: "default" | "fast"; - ignoreSitemap?: boolean; - limit?: number; - allowBackwardCrawling?: boolean; - allowExternalContentLinks?: boolean; - }; - pageOptions?: { - headers?: Record; - includeHtml?: boolean; - includeRawHtml?: boolean; - onlyIncludeTags?: string[]; - onlyMainContent?: boolean; - removeTags?: string[]; - replaceAllPathsWithAbsolutePaths?: boolean; - screenshot?: boolean; - fullPageScreenshot?: boolean; - waitFor?: number; - }; -} /** * Response interface for crawling operations. * Defines the structure of the response received after initiating a crawl. @@ -199,16 +104,7 @@ export interface CrawlParamsV0 { export interface CrawlResponse { id?: string; url?: string; - success: boolean; - error?: string; -} -/** - * Response interface for crawling operations on v0. - * Similar to CrawlResponse but tailored for responses from API version v0. - */ -export interface CrawlResponseV0 { - jobId?: string; - success: boolean; + success: true; error?: string; } /** @@ -216,7 +112,7 @@ export interface CrawlResponseV0 { * Provides detailed status of a crawl job including progress and results. */ export interface CrawlStatusResponse { - success: boolean; + success: true; total: number; completed: number; creditsUsed: number; @@ -226,21 +122,6 @@ export interface CrawlStatusResponse { data?: FirecrawlDocument[]; error?: string; } -/** - * Response interface for job status checks on v0. - * Tailored for API version v0, provides status and partial data of a crawl job. - */ -export interface CrawlStatusResponseV0 { - success: boolean; - status: string; - current?: number; - current_url?: string; - current_step?: string; - total?: number; - data?: FirecrawlDocumentV0[]; - partial_data?: FirecrawlDocumentV0[]; - error?: string; -} /** * Parameters for mapping operations. * Defines options for mapping URLs during a crawl. @@ -256,78 +137,62 @@ export interface MapParams { * Defines the structure of the response received after a mapping operation. */ export interface MapResponse { - success: boolean; + success: true; links?: string[]; error?: string; } /** - * Parameters for searching operations on v0. - * Tailored for API version v0, includes specific options for searching content. + * Error response interface. + * Defines the structure of the response received when an error occurs. */ -export interface SearchParamsV0 { - pageOptions?: { - onlyMainContent?: boolean; - fetchPageContent?: boolean; - includeHtml?: boolean; - includeRawHtml?: boolean; - }; - searchOptions?: { - limit?: number; - }; -} -/** - * Response interface for searching operations on v0. - * Defines the structure of the response received after a search operation on v0. - */ -export interface SearchResponseV0 { - success: boolean; - data?: FirecrawlDocumentV0[]; - error?: string; +export interface ErrorResponse { + success: false; + error: string; } /** * Main class for interacting with the Firecrawl API. * Provides methods for scraping, searching, crawling, and mapping web content. */ -export default class FirecrawlApp { - private apiKey; - private apiUrl; - version: T; +export default class FirecrawlApp { + apiKey: string; + apiUrl: string; /** * Initializes a new instance of the FirecrawlApp class. * @param config - Configuration options for the FirecrawlApp instance. */ - constructor({ apiKey, apiUrl, version }: FirecrawlAppConfig); + constructor({ apiKey, apiUrl }: FirecrawlAppConfig); /** * Scrapes a URL using the Firecrawl API. * @param url - The URL to scrape. * @param params - Additional parameters for the scrape request. * @returns The response from the scrape operation. */ - scrapeUrl(url: string, params?: ScrapeParams | ScrapeParamsV0): Promise; + scrapeUrl(url: string, params?: ScrapeParams): Promise; /** - * Searches for a query using the Firecrawl API. - * @param query - The query to search for. - * @param params - Additional parameters for the search request. - * @returns The response from the search operation. + * This method is intended to search for a query using the Firecrawl API. However, it is not supported in version 1 of the API. + * @param query - The search query string. + * @param params - Additional parameters for the search. + * @returns Throws an error advising to use version 0 of the API. */ - search(query: string, params?: SearchParamsV0): Promise; + search(query: string, params?: any): Promise; /** * Initiates a crawl job for a URL using the Firecrawl API. * @param url - The URL to crawl. * @param params - Additional parameters for the crawl request. - * @param waitUntilDone - Whether to wait for the crawl job to complete. * @param pollInterval - Time in seconds for job status checks. * @param idempotencyKey - Optional idempotency key for the request. * @returns The response from the crawl operation. */ - crawlUrl(url: string, params?: this['version'] extends 'v0' ? CrawlParamsV0 : CrawlParams, waitUntilDone?: boolean, pollInterval?: number, idempotencyKey?: string): Promise; + crawlUrl(url: string, params?: CrawlParams, pollInterval?: number, idempotencyKey?: string): Promise; + asyncCrawlUrl(url: string, params?: CrawlParams, idempotencyKey?: string): Promise; /** * Checks the status of a crawl job using the Firecrawl API. * @param id - The ID of the crawl operation. * @returns The response containing the job status. */ - checkCrawlStatus(id?: string): Promise; - mapUrl(url: string, params?: MapParams): Promise; + checkCrawlStatus(id?: string): Promise; + crawlUrlAndWatch(url: string, params?: CrawlParams, idempotencyKey?: string): Promise; + mapUrl(url: string, params?: MapParams): Promise; /** * Prepares the headers for an API request. * @param idempotencyKey - Optional key to ensure idempotency. @@ -357,7 +222,7 @@ export default class FirecrawlApp { * @param checkUrl - Optional URL to check the status (used for v1 API) * @returns The final job status or data. */ - monitorJobStatus(id: string, headers: AxiosRequestHeaders, checkInterval: number, checkUrl?: string): Promise; + monitorJobStatus(id: string, headers: AxiosRequestHeaders, checkInterval: number): Promise; /** * Handles errors from API responses. * @param {AxiosResponse} response - The response from the API. @@ -365,3 +230,23 @@ export default class FirecrawlApp { */ handleError(response: AxiosResponse, action: string): void; } +interface CrawlWatcherEvents { + document: CustomEvent; + done: CustomEvent<{ + status: CrawlStatusResponse["status"]; + data: FirecrawlDocument[]; + }>; + error: CustomEvent<{ + status: CrawlStatusResponse["status"]; + data: FirecrawlDocument[]; + error: string; + }>; +} +export declare class CrawlWatcher extends TypedEventTarget { + private ws; + data: FirecrawlDocument[]; + status: CrawlStatusResponse["status"]; + constructor(id: string, app: FirecrawlApp); + close(): void; +} +export {}; diff --git a/apps/python-sdk/firecrawl/__init__.py b/apps/python-sdk/firecrawl/__init__.py index 229f9ccd..13df20d9 100644 --- a/apps/python-sdk/firecrawl/__init__.py +++ b/apps/python-sdk/firecrawl/__init__.py @@ -13,7 +13,7 @@ import os from .firecrawl import FirecrawlApp -__version__ = "1.0.1" +__version__ = "1.1.1" # Define the logger for the Firecrawl project logger: logging.Logger = logging.getLogger("firecrawl") diff --git a/apps/python-sdk/pyproject.toml b/apps/python-sdk/pyproject.toml index 0a732c43..969fb051 100644 --- a/apps/python-sdk/pyproject.toml +++ b/apps/python-sdk/pyproject.toml @@ -10,6 +10,10 @@ readme = {file="README.md", content-type = "text/markdown"} requires-python = ">=3.8" dependencies = [ "requests", + "python-dotenv", + "websockets", + "asyncio", +"nest-asyncio" ] authors = [{name = "Mendable.ai",email = "nick@mendable.ai"}] maintainers = [{name = "Mendable.ai",email = "nick@mendable.ai"}] diff --git a/apps/python-sdk/setup.py b/apps/python-sdk/setup.py index 4978559b..8a67d1fd 100644 --- a/apps/python-sdk/setup.py +++ b/apps/python-sdk/setup.py @@ -30,6 +30,9 @@ setup( 'requests', 'pytest', 'python-dotenv', + 'websockets', + 'asyncio', + 'nest-asyncio' ], python_requires=">=3.8", classifiers=[