@mendable/firecrawl-js 1.17.0 → 1.18.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +87 -0
- package/dist/index.d.cts +84 -1
- package/dist/index.d.ts +84 -1
- package/dist/index.js +87 -0
- package/package.json +1 -1
- package/src/index.ts +164 -0
package/dist/index.cjs
CHANGED
|
@@ -851,6 +851,93 @@ var FirecrawlApp = class {
|
|
|
851
851
|
);
|
|
852
852
|
}
|
|
853
853
|
}
|
|
854
|
+
/**
|
|
855
|
+
* Initiates a deep research operation on a given topic and polls until completion.
|
|
856
|
+
* @param params - Parameters for the deep research operation.
|
|
857
|
+
* @returns The final research results.
|
|
858
|
+
*/
|
|
859
|
+
async __deepResearch(params) {
|
|
860
|
+
try {
|
|
861
|
+
const response = await this.__asyncDeepResearch(params);
|
|
862
|
+
if (!response.success || "error" in response) {
|
|
863
|
+
return { success: false, error: "error" in response ? response.error : "Unknown error" };
|
|
864
|
+
}
|
|
865
|
+
if (response.id) {
|
|
866
|
+
const jobId = response.id;
|
|
867
|
+
let researchStatus;
|
|
868
|
+
do {
|
|
869
|
+
researchStatus = await this.__checkDeepResearchStatus(jobId);
|
|
870
|
+
if ("error" in researchStatus && !researchStatus.success) {
|
|
871
|
+
return researchStatus;
|
|
872
|
+
}
|
|
873
|
+
if (researchStatus.status === "completed") {
|
|
874
|
+
return researchStatus;
|
|
875
|
+
} else if (researchStatus.status === "failed") {
|
|
876
|
+
throw new FirecrawlError(`Research job failed. Error: ${researchStatus.error}`, 500);
|
|
877
|
+
}
|
|
878
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
879
|
+
} while (researchStatus.status === "processing");
|
|
880
|
+
}
|
|
881
|
+
throw new FirecrawlError(`Failed to start research. No job ID returned.`, 500);
|
|
882
|
+
} catch (error) {
|
|
883
|
+
throw new FirecrawlError(error.message, 500, error.response?.data?.details);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
/**
|
|
887
|
+
* Initiates a deep research operation on a given topic without polling.
|
|
888
|
+
* @param params - Parameters for the deep research operation.
|
|
889
|
+
* @returns The response containing the research job ID.
|
|
890
|
+
*/
|
|
891
|
+
async __asyncDeepResearch(params) {
|
|
892
|
+
const headers = this.prepareHeaders();
|
|
893
|
+
try {
|
|
894
|
+
const response = await this.postRequest(
|
|
895
|
+
`${this.apiUrl}/v1/deep-research`,
|
|
896
|
+
params,
|
|
897
|
+
headers
|
|
898
|
+
);
|
|
899
|
+
if (response.status === 200) {
|
|
900
|
+
return response.data;
|
|
901
|
+
} else {
|
|
902
|
+
this.handleError(response, "start deep research");
|
|
903
|
+
}
|
|
904
|
+
} catch (error) {
|
|
905
|
+
if (error.response?.data?.error) {
|
|
906
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
907
|
+
} else {
|
|
908
|
+
throw new FirecrawlError(error.message, 500);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
return { success: false, error: "Internal server error." };
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Checks the status of a deep research operation.
|
|
915
|
+
* @param id - The ID of the deep research operation.
|
|
916
|
+
* @returns The current status and results of the research operation.
|
|
917
|
+
*/
|
|
918
|
+
async __checkDeepResearchStatus(id) {
|
|
919
|
+
const headers = this.prepareHeaders();
|
|
920
|
+
try {
|
|
921
|
+
const response = await this.getRequest(
|
|
922
|
+
`${this.apiUrl}/v1/deep-research/${id}`,
|
|
923
|
+
headers
|
|
924
|
+
);
|
|
925
|
+
if (response.status === 200) {
|
|
926
|
+
return response.data;
|
|
927
|
+
} else if (response.status === 404) {
|
|
928
|
+
throw new FirecrawlError("Deep research job not found", 404);
|
|
929
|
+
} else {
|
|
930
|
+
this.handleError(response, "check deep research status");
|
|
931
|
+
}
|
|
932
|
+
} catch (error) {
|
|
933
|
+
if (error.response?.data?.error) {
|
|
934
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
935
|
+
} else {
|
|
936
|
+
throw new FirecrawlError(error.message, 500);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
return { success: false, error: "Internal server error." };
|
|
940
|
+
}
|
|
854
941
|
};
|
|
855
942
|
var CrawlWatcher = class extends import_typescript_event_target.TypedEventTarget {
|
|
856
943
|
ws;
|
package/dist/index.d.cts
CHANGED
|
@@ -317,6 +317,71 @@ interface CrawlErrorsResponse {
|
|
|
317
317
|
*/
|
|
318
318
|
robotsBlocked: string[];
|
|
319
319
|
}
|
|
320
|
+
/**
|
|
321
|
+
* Parameters for deep research operations.
|
|
322
|
+
* Defines options for conducting deep research on a topic.
|
|
323
|
+
*/
|
|
324
|
+
interface DeepResearchParams {
|
|
325
|
+
/**
|
|
326
|
+
* The topic or question to research
|
|
327
|
+
*/
|
|
328
|
+
topic: string;
|
|
329
|
+
/**
|
|
330
|
+
* Maximum depth of research iterations (1-10)
|
|
331
|
+
* @default 7
|
|
332
|
+
*/
|
|
333
|
+
maxDepth?: number;
|
|
334
|
+
/**
|
|
335
|
+
* Time limit in seconds (30-300)
|
|
336
|
+
* @default 270
|
|
337
|
+
*/
|
|
338
|
+
timeLimit?: number;
|
|
339
|
+
/**
|
|
340
|
+
* Experimental flag for streaming steps
|
|
341
|
+
*/
|
|
342
|
+
__experimental_streamSteps?: boolean;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Response interface for deep research operations.
|
|
346
|
+
*/
|
|
347
|
+
interface DeepResearchResponse {
|
|
348
|
+
success: boolean;
|
|
349
|
+
id: string;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Status response interface for deep research operations.
|
|
353
|
+
*/
|
|
354
|
+
interface DeepResearchStatusResponse {
|
|
355
|
+
success: boolean;
|
|
356
|
+
data: {
|
|
357
|
+
findings: Array<{
|
|
358
|
+
text: string;
|
|
359
|
+
source: string;
|
|
360
|
+
}>;
|
|
361
|
+
finalAnalysis: string;
|
|
362
|
+
analysis: string;
|
|
363
|
+
completedSteps: number;
|
|
364
|
+
totalSteps: number;
|
|
365
|
+
};
|
|
366
|
+
status: "processing" | "completed" | "failed";
|
|
367
|
+
error?: string;
|
|
368
|
+
expiresAt: string;
|
|
369
|
+
currentDepth: number;
|
|
370
|
+
maxDepth: number;
|
|
371
|
+
activities: Array<{
|
|
372
|
+
type: string;
|
|
373
|
+
status: string;
|
|
374
|
+
message: string;
|
|
375
|
+
timestamp: string;
|
|
376
|
+
depth: number;
|
|
377
|
+
}>;
|
|
378
|
+
sources: Array<{
|
|
379
|
+
url: string;
|
|
380
|
+
title: string;
|
|
381
|
+
description: string;
|
|
382
|
+
}>;
|
|
383
|
+
summaries: string[];
|
|
384
|
+
}
|
|
320
385
|
/**
|
|
321
386
|
* Main class for interacting with the Firecrawl API.
|
|
322
387
|
* Provides methods for scraping, searching, crawling, and mapping web content.
|
|
@@ -491,6 +556,24 @@ declare class FirecrawlApp {
|
|
|
491
556
|
* @param {string} action - The action being performed when the error occurred.
|
|
492
557
|
*/
|
|
493
558
|
handleError(response: AxiosResponse, action: string): void;
|
|
559
|
+
/**
|
|
560
|
+
* Initiates a deep research operation on a given topic and polls until completion.
|
|
561
|
+
* @param params - Parameters for the deep research operation.
|
|
562
|
+
* @returns The final research results.
|
|
563
|
+
*/
|
|
564
|
+
__deepResearch(params: DeepResearchParams): Promise<DeepResearchStatusResponse | ErrorResponse>;
|
|
565
|
+
/**
|
|
566
|
+
* Initiates a deep research operation on a given topic without polling.
|
|
567
|
+
* @param params - Parameters for the deep research operation.
|
|
568
|
+
* @returns The response containing the research job ID.
|
|
569
|
+
*/
|
|
570
|
+
__asyncDeepResearch(params: DeepResearchParams): Promise<DeepResearchResponse | ErrorResponse>;
|
|
571
|
+
/**
|
|
572
|
+
* Checks the status of a deep research operation.
|
|
573
|
+
* @param id - The ID of the deep research operation.
|
|
574
|
+
* @returns The current status and results of the research operation.
|
|
575
|
+
*/
|
|
576
|
+
__checkDeepResearchStatus(id: string): Promise<DeepResearchStatusResponse | ErrorResponse>;
|
|
494
577
|
}
|
|
495
578
|
interface CrawlWatcherEvents {
|
|
496
579
|
document: CustomEvent<FirecrawlDocument<undefined>>;
|
|
@@ -513,4 +596,4 @@ declare class CrawlWatcher extends TypedEventTarget<CrawlWatcherEvents> {
|
|
|
513
596
|
close(): void;
|
|
514
597
|
}
|
|
515
598
|
|
|
516
|
-
export { type Action, type ActionsResult, type BatchScrapeResponse, type BatchScrapeStatusResponse, type CrawlErrorsResponse, type CrawlParams, type CrawlResponse, type CrawlScrapeOptions, type CrawlStatusResponse, CrawlWatcher, type ErrorResponse, type ExtractParams, type ExtractResponse, type FirecrawlAppConfig, type FirecrawlDocument, type FirecrawlDocumentMetadata, FirecrawlError, type MapParams, type MapResponse, type ScrapeParams, type ScrapeResponse, type SearchParams, type SearchResponse, FirecrawlApp as default };
|
|
599
|
+
export { type Action, type ActionsResult, type BatchScrapeResponse, type BatchScrapeStatusResponse, type CrawlErrorsResponse, type CrawlParams, type CrawlResponse, type CrawlScrapeOptions, type CrawlStatusResponse, CrawlWatcher, type DeepResearchParams, type DeepResearchResponse, type DeepResearchStatusResponse, type ErrorResponse, type ExtractParams, type ExtractResponse, type FirecrawlAppConfig, type FirecrawlDocument, type FirecrawlDocumentMetadata, FirecrawlError, type MapParams, type MapResponse, type ScrapeParams, type ScrapeResponse, type SearchParams, type SearchResponse, FirecrawlApp as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -317,6 +317,71 @@ interface CrawlErrorsResponse {
|
|
|
317
317
|
*/
|
|
318
318
|
robotsBlocked: string[];
|
|
319
319
|
}
|
|
320
|
+
/**
|
|
321
|
+
* Parameters for deep research operations.
|
|
322
|
+
* Defines options for conducting deep research on a topic.
|
|
323
|
+
*/
|
|
324
|
+
interface DeepResearchParams {
|
|
325
|
+
/**
|
|
326
|
+
* The topic or question to research
|
|
327
|
+
*/
|
|
328
|
+
topic: string;
|
|
329
|
+
/**
|
|
330
|
+
* Maximum depth of research iterations (1-10)
|
|
331
|
+
* @default 7
|
|
332
|
+
*/
|
|
333
|
+
maxDepth?: number;
|
|
334
|
+
/**
|
|
335
|
+
* Time limit in seconds (30-300)
|
|
336
|
+
* @default 270
|
|
337
|
+
*/
|
|
338
|
+
timeLimit?: number;
|
|
339
|
+
/**
|
|
340
|
+
* Experimental flag for streaming steps
|
|
341
|
+
*/
|
|
342
|
+
__experimental_streamSteps?: boolean;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Response interface for deep research operations.
|
|
346
|
+
*/
|
|
347
|
+
interface DeepResearchResponse {
|
|
348
|
+
success: boolean;
|
|
349
|
+
id: string;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Status response interface for deep research operations.
|
|
353
|
+
*/
|
|
354
|
+
interface DeepResearchStatusResponse {
|
|
355
|
+
success: boolean;
|
|
356
|
+
data: {
|
|
357
|
+
findings: Array<{
|
|
358
|
+
text: string;
|
|
359
|
+
source: string;
|
|
360
|
+
}>;
|
|
361
|
+
finalAnalysis: string;
|
|
362
|
+
analysis: string;
|
|
363
|
+
completedSteps: number;
|
|
364
|
+
totalSteps: number;
|
|
365
|
+
};
|
|
366
|
+
status: "processing" | "completed" | "failed";
|
|
367
|
+
error?: string;
|
|
368
|
+
expiresAt: string;
|
|
369
|
+
currentDepth: number;
|
|
370
|
+
maxDepth: number;
|
|
371
|
+
activities: Array<{
|
|
372
|
+
type: string;
|
|
373
|
+
status: string;
|
|
374
|
+
message: string;
|
|
375
|
+
timestamp: string;
|
|
376
|
+
depth: number;
|
|
377
|
+
}>;
|
|
378
|
+
sources: Array<{
|
|
379
|
+
url: string;
|
|
380
|
+
title: string;
|
|
381
|
+
description: string;
|
|
382
|
+
}>;
|
|
383
|
+
summaries: string[];
|
|
384
|
+
}
|
|
320
385
|
/**
|
|
321
386
|
* Main class for interacting with the Firecrawl API.
|
|
322
387
|
* Provides methods for scraping, searching, crawling, and mapping web content.
|
|
@@ -491,6 +556,24 @@ declare class FirecrawlApp {
|
|
|
491
556
|
* @param {string} action - The action being performed when the error occurred.
|
|
492
557
|
*/
|
|
493
558
|
handleError(response: AxiosResponse, action: string): void;
|
|
559
|
+
/**
|
|
560
|
+
* Initiates a deep research operation on a given topic and polls until completion.
|
|
561
|
+
* @param params - Parameters for the deep research operation.
|
|
562
|
+
* @returns The final research results.
|
|
563
|
+
*/
|
|
564
|
+
__deepResearch(params: DeepResearchParams): Promise<DeepResearchStatusResponse | ErrorResponse>;
|
|
565
|
+
/**
|
|
566
|
+
* Initiates a deep research operation on a given topic without polling.
|
|
567
|
+
* @param params - Parameters for the deep research operation.
|
|
568
|
+
* @returns The response containing the research job ID.
|
|
569
|
+
*/
|
|
570
|
+
__asyncDeepResearch(params: DeepResearchParams): Promise<DeepResearchResponse | ErrorResponse>;
|
|
571
|
+
/**
|
|
572
|
+
* Checks the status of a deep research operation.
|
|
573
|
+
* @param id - The ID of the deep research operation.
|
|
574
|
+
* @returns The current status and results of the research operation.
|
|
575
|
+
*/
|
|
576
|
+
__checkDeepResearchStatus(id: string): Promise<DeepResearchStatusResponse | ErrorResponse>;
|
|
494
577
|
}
|
|
495
578
|
interface CrawlWatcherEvents {
|
|
496
579
|
document: CustomEvent<FirecrawlDocument<undefined>>;
|
|
@@ -513,4 +596,4 @@ declare class CrawlWatcher extends TypedEventTarget<CrawlWatcherEvents> {
|
|
|
513
596
|
close(): void;
|
|
514
597
|
}
|
|
515
598
|
|
|
516
|
-
export { type Action, type ActionsResult, type BatchScrapeResponse, type BatchScrapeStatusResponse, type CrawlErrorsResponse, type CrawlParams, type CrawlResponse, type CrawlScrapeOptions, type CrawlStatusResponse, CrawlWatcher, type ErrorResponse, type ExtractParams, type ExtractResponse, type FirecrawlAppConfig, type FirecrawlDocument, type FirecrawlDocumentMetadata, FirecrawlError, type MapParams, type MapResponse, type ScrapeParams, type ScrapeResponse, type SearchParams, type SearchResponse, FirecrawlApp as default };
|
|
599
|
+
export { type Action, type ActionsResult, type BatchScrapeResponse, type BatchScrapeStatusResponse, type CrawlErrorsResponse, type CrawlParams, type CrawlResponse, type CrawlScrapeOptions, type CrawlStatusResponse, CrawlWatcher, type DeepResearchParams, type DeepResearchResponse, type DeepResearchStatusResponse, type ErrorResponse, type ExtractParams, type ExtractResponse, type FirecrawlAppConfig, type FirecrawlDocument, type FirecrawlDocumentMetadata, FirecrawlError, type MapParams, type MapResponse, type ScrapeParams, type ScrapeResponse, type SearchParams, type SearchResponse, FirecrawlApp as default };
|
package/dist/index.js
CHANGED
|
@@ -815,6 +815,93 @@ var FirecrawlApp = class {
|
|
|
815
815
|
);
|
|
816
816
|
}
|
|
817
817
|
}
|
|
818
|
+
/**
|
|
819
|
+
* Initiates a deep research operation on a given topic and polls until completion.
|
|
820
|
+
* @param params - Parameters for the deep research operation.
|
|
821
|
+
* @returns The final research results.
|
|
822
|
+
*/
|
|
823
|
+
async __deepResearch(params) {
|
|
824
|
+
try {
|
|
825
|
+
const response = await this.__asyncDeepResearch(params);
|
|
826
|
+
if (!response.success || "error" in response) {
|
|
827
|
+
return { success: false, error: "error" in response ? response.error : "Unknown error" };
|
|
828
|
+
}
|
|
829
|
+
if (response.id) {
|
|
830
|
+
const jobId = response.id;
|
|
831
|
+
let researchStatus;
|
|
832
|
+
do {
|
|
833
|
+
researchStatus = await this.__checkDeepResearchStatus(jobId);
|
|
834
|
+
if ("error" in researchStatus && !researchStatus.success) {
|
|
835
|
+
return researchStatus;
|
|
836
|
+
}
|
|
837
|
+
if (researchStatus.status === "completed") {
|
|
838
|
+
return researchStatus;
|
|
839
|
+
} else if (researchStatus.status === "failed") {
|
|
840
|
+
throw new FirecrawlError(`Research job failed. Error: ${researchStatus.error}`, 500);
|
|
841
|
+
}
|
|
842
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
843
|
+
} while (researchStatus.status === "processing");
|
|
844
|
+
}
|
|
845
|
+
throw new FirecrawlError(`Failed to start research. No job ID returned.`, 500);
|
|
846
|
+
} catch (error) {
|
|
847
|
+
throw new FirecrawlError(error.message, 500, error.response?.data?.details);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* Initiates a deep research operation on a given topic without polling.
|
|
852
|
+
* @param params - Parameters for the deep research operation.
|
|
853
|
+
* @returns The response containing the research job ID.
|
|
854
|
+
*/
|
|
855
|
+
async __asyncDeepResearch(params) {
|
|
856
|
+
const headers = this.prepareHeaders();
|
|
857
|
+
try {
|
|
858
|
+
const response = await this.postRequest(
|
|
859
|
+
`${this.apiUrl}/v1/deep-research`,
|
|
860
|
+
params,
|
|
861
|
+
headers
|
|
862
|
+
);
|
|
863
|
+
if (response.status === 200) {
|
|
864
|
+
return response.data;
|
|
865
|
+
} else {
|
|
866
|
+
this.handleError(response, "start deep research");
|
|
867
|
+
}
|
|
868
|
+
} catch (error) {
|
|
869
|
+
if (error.response?.data?.error) {
|
|
870
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
871
|
+
} else {
|
|
872
|
+
throw new FirecrawlError(error.message, 500);
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
return { success: false, error: "Internal server error." };
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Checks the status of a deep research operation.
|
|
879
|
+
* @param id - The ID of the deep research operation.
|
|
880
|
+
* @returns The current status and results of the research operation.
|
|
881
|
+
*/
|
|
882
|
+
async __checkDeepResearchStatus(id) {
|
|
883
|
+
const headers = this.prepareHeaders();
|
|
884
|
+
try {
|
|
885
|
+
const response = await this.getRequest(
|
|
886
|
+
`${this.apiUrl}/v1/deep-research/${id}`,
|
|
887
|
+
headers
|
|
888
|
+
);
|
|
889
|
+
if (response.status === 200) {
|
|
890
|
+
return response.data;
|
|
891
|
+
} else if (response.status === 404) {
|
|
892
|
+
throw new FirecrawlError("Deep research job not found", 404);
|
|
893
|
+
} else {
|
|
894
|
+
this.handleError(response, "check deep research status");
|
|
895
|
+
}
|
|
896
|
+
} catch (error) {
|
|
897
|
+
if (error.response?.data?.error) {
|
|
898
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
899
|
+
} else {
|
|
900
|
+
throw new FirecrawlError(error.message, 500);
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
return { success: false, error: "Internal server error." };
|
|
904
|
+
}
|
|
818
905
|
};
|
|
819
906
|
var CrawlWatcher = class extends TypedEventTarget {
|
|
820
907
|
ws;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -347,6 +347,74 @@ export interface CrawlErrorsResponse {
|
|
|
347
347
|
robotsBlocked: string[];
|
|
348
348
|
};
|
|
349
349
|
|
|
350
|
+
/**
|
|
351
|
+
* Parameters for deep research operations.
|
|
352
|
+
* Defines options for conducting deep research on a topic.
|
|
353
|
+
*/
|
|
354
|
+
export interface DeepResearchParams {
|
|
355
|
+
/**
|
|
356
|
+
* The topic or question to research
|
|
357
|
+
*/
|
|
358
|
+
topic: string;
|
|
359
|
+
/**
|
|
360
|
+
* Maximum depth of research iterations (1-10)
|
|
361
|
+
* @default 7
|
|
362
|
+
*/
|
|
363
|
+
maxDepth?: number;
|
|
364
|
+
/**
|
|
365
|
+
* Time limit in seconds (30-300)
|
|
366
|
+
* @default 270
|
|
367
|
+
*/
|
|
368
|
+
timeLimit?: number;
|
|
369
|
+
/**
|
|
370
|
+
* Experimental flag for streaming steps
|
|
371
|
+
*/
|
|
372
|
+
__experimental_streamSteps?: boolean;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Response interface for deep research operations.
|
|
377
|
+
*/
|
|
378
|
+
export interface DeepResearchResponse {
|
|
379
|
+
success: boolean;
|
|
380
|
+
id: string;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Status response interface for deep research operations.
|
|
385
|
+
*/
|
|
386
|
+
export interface DeepResearchStatusResponse {
|
|
387
|
+
success: boolean;
|
|
388
|
+
data: {
|
|
389
|
+
findings: Array<{
|
|
390
|
+
text: string;
|
|
391
|
+
source: string;
|
|
392
|
+
}>;
|
|
393
|
+
finalAnalysis: string;
|
|
394
|
+
analysis: string;
|
|
395
|
+
completedSteps: number;
|
|
396
|
+
totalSteps: number;
|
|
397
|
+
};
|
|
398
|
+
status: "processing" | "completed" | "failed";
|
|
399
|
+
error?: string;
|
|
400
|
+
expiresAt: string;
|
|
401
|
+
currentDepth: number;
|
|
402
|
+
maxDepth: number;
|
|
403
|
+
activities: Array<{
|
|
404
|
+
type: string;
|
|
405
|
+
status: string;
|
|
406
|
+
message: string;
|
|
407
|
+
timestamp: string;
|
|
408
|
+
depth: number;
|
|
409
|
+
}>;
|
|
410
|
+
sources: Array<{
|
|
411
|
+
url: string;
|
|
412
|
+
title: string;
|
|
413
|
+
description: string;
|
|
414
|
+
}>;
|
|
415
|
+
summaries: string[];
|
|
416
|
+
}
|
|
417
|
+
|
|
350
418
|
/**
|
|
351
419
|
* Main class for interacting with the Firecrawl API.
|
|
352
420
|
* Provides methods for scraping, searching, crawling, and mapping web content.
|
|
@@ -1280,6 +1348,102 @@ export default class FirecrawlApp {
|
|
|
1280
1348
|
);
|
|
1281
1349
|
}
|
|
1282
1350
|
}
|
|
1351
|
+
|
|
1352
|
+
/**
|
|
1353
|
+
* Initiates a deep research operation on a given topic and polls until completion.
|
|
1354
|
+
* @param params - Parameters for the deep research operation.
|
|
1355
|
+
* @returns The final research results.
|
|
1356
|
+
*/
|
|
1357
|
+
async __deepResearch(params: DeepResearchParams): Promise<DeepResearchStatusResponse | ErrorResponse> {
|
|
1358
|
+
try {
|
|
1359
|
+
const response = await this.__asyncDeepResearch(params);
|
|
1360
|
+
|
|
1361
|
+
if (!response.success || 'error' in response) {
|
|
1362
|
+
return { success: false, error: 'error' in response ? response.error : 'Unknown error' };
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
if (response.id) {
|
|
1366
|
+
const jobId = response.id;
|
|
1367
|
+
let researchStatus;
|
|
1368
|
+
do {
|
|
1369
|
+
researchStatus = await this.__checkDeepResearchStatus(jobId);
|
|
1370
|
+
|
|
1371
|
+
if ('error' in researchStatus && !researchStatus.success) {
|
|
1372
|
+
return researchStatus;
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
if (researchStatus.status === "completed") {
|
|
1376
|
+
return researchStatus;
|
|
1377
|
+
} else if (researchStatus.status === "failed") {
|
|
1378
|
+
throw new FirecrawlError(`Research job failed. Error: ${researchStatus.error}`, 500);
|
|
1379
|
+
}
|
|
1380
|
+
await new Promise(resolve => setTimeout(resolve, 1000)); // Polling interval
|
|
1381
|
+
} while (researchStatus.status === "processing");
|
|
1382
|
+
}
|
|
1383
|
+
throw new FirecrawlError(`Failed to start research. No job ID returned.`, 500);
|
|
1384
|
+
} catch (error: any) {
|
|
1385
|
+
throw new FirecrawlError(error.message, 500, error.response?.data?.details);
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
/**
|
|
1390
|
+
* Initiates a deep research operation on a given topic without polling.
|
|
1391
|
+
* @param params - Parameters for the deep research operation.
|
|
1392
|
+
* @returns The response containing the research job ID.
|
|
1393
|
+
*/
|
|
1394
|
+
async __asyncDeepResearch(params: DeepResearchParams): Promise<DeepResearchResponse | ErrorResponse> {
|
|
1395
|
+
const headers = this.prepareHeaders();
|
|
1396
|
+
try {
|
|
1397
|
+
const response: AxiosResponse = await this.postRequest(
|
|
1398
|
+
`${this.apiUrl}/v1/deep-research`,
|
|
1399
|
+
params,
|
|
1400
|
+
headers
|
|
1401
|
+
);
|
|
1402
|
+
|
|
1403
|
+
if (response.status === 200) {
|
|
1404
|
+
return response.data;
|
|
1405
|
+
} else {
|
|
1406
|
+
this.handleError(response, "start deep research");
|
|
1407
|
+
}
|
|
1408
|
+
} catch (error: any) {
|
|
1409
|
+
if (error.response?.data?.error) {
|
|
1410
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
|
|
1411
|
+
} else {
|
|
1412
|
+
throw new FirecrawlError(error.message, 500);
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
return { success: false, error: "Internal server error." };
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
/**
|
|
1419
|
+
* Checks the status of a deep research operation.
|
|
1420
|
+
* @param id - The ID of the deep research operation.
|
|
1421
|
+
* @returns The current status and results of the research operation.
|
|
1422
|
+
*/
|
|
1423
|
+
async __checkDeepResearchStatus(id: string): Promise<DeepResearchStatusResponse | ErrorResponse> {
|
|
1424
|
+
const headers = this.prepareHeaders();
|
|
1425
|
+
try {
|
|
1426
|
+
const response: AxiosResponse = await this.getRequest(
|
|
1427
|
+
`${this.apiUrl}/v1/deep-research/${id}`,
|
|
1428
|
+
headers
|
|
1429
|
+
);
|
|
1430
|
+
|
|
1431
|
+
if (response.status === 200) {
|
|
1432
|
+
return response.data;
|
|
1433
|
+
} else if (response.status === 404) {
|
|
1434
|
+
throw new FirecrawlError("Deep research job not found", 404);
|
|
1435
|
+
} else {
|
|
1436
|
+
this.handleError(response, "check deep research status");
|
|
1437
|
+
}
|
|
1438
|
+
} catch (error: any) {
|
|
1439
|
+
if (error.response?.data?.error) {
|
|
1440
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
|
|
1441
|
+
} else {
|
|
1442
|
+
throw new FirecrawlError(error.message, 500);
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
return { success: false, error: "Internal server error." };
|
|
1446
|
+
}
|
|
1283
1447
|
}
|
|
1284
1448
|
|
|
1285
1449
|
interface CrawlWatcherEvents {
|