@mendable/firecrawl 1.18.4 → 1.19.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mendable/firecrawl",
3
- "version": "1.18.4",
3
+ "version": "1.19.0",
4
4
  "description": "JavaScript SDK for Firecrawl API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -351,7 +351,7 @@ export interface CrawlErrorsResponse {
351
351
 
352
352
  /**
353
353
  * Parameters for deep research operations.
354
- * Defines options for conducting deep research on a topic.
354
+ * Defines options for conducting deep research on a query.
355
355
  */
356
356
  export interface DeepResearchParams {
357
357
  /**
@@ -389,14 +389,19 @@ export interface DeepResearchResponse {
389
389
  export interface DeepResearchStatusResponse {
390
390
  success: boolean;
391
391
  data: {
392
- findings: Array<{
393
- text: string;
394
- source: string;
395
- }>;
396
392
  finalAnalysis: string;
397
- analysis: string;
398
- completedSteps: number;
399
- totalSteps: number;
393
+ activities: Array<{
394
+ type: string;
395
+ status: string;
396
+ message: string;
397
+ timestamp: string;
398
+ depth: number;
399
+ }>;
400
+ sources: Array<{
401
+ url: string;
402
+ title: string;
403
+ description: string;
404
+ }>;
400
405
  };
401
406
  status: "processing" | "completed" | "failed";
402
407
  error?: string;
@@ -1395,6 +1400,156 @@ export default class FirecrawlApp {
1395
1400
  }
1396
1401
 
1397
1402
  /**
1403
+ * Initiates a deep research operation on a given query and polls until completion.
1404
+ * @param query - The query to research.
1405
+ * @param params - Parameters for the deep research operation.
1406
+ * @param onActivity - Optional callback to receive activity updates in real-time.
1407
+ * @param onSource - Optional callback to receive source updates in real-time.
1408
+ * @returns The final research results.
1409
+ */
1410
+ async deepResearch(
1411
+ query: string,
1412
+ params: DeepResearchParams,
1413
+ onActivity?: (activity: {
1414
+ type: string;
1415
+ status: string;
1416
+ message: string;
1417
+ timestamp: string;
1418
+ depth: number;
1419
+ }) => void,
1420
+ onSource?: (source: {
1421
+ url: string;
1422
+ title?: string;
1423
+ description?: string;
1424
+ icon?: string;
1425
+ }) => void
1426
+ ): Promise<DeepResearchStatusResponse | ErrorResponse> {
1427
+ try {
1428
+ const response = await this.asyncDeepResearch(query, params);
1429
+
1430
+ if (!response.success || 'error' in response) {
1431
+ return { success: false, error: 'error' in response ? response.error : 'Unknown error' };
1432
+ }
1433
+
1434
+ if (!response.id) {
1435
+ throw new FirecrawlError(`Failed to start research. No job ID returned.`, 500);
1436
+ }
1437
+
1438
+ const jobId = response.id;
1439
+ let researchStatus;
1440
+ let lastActivityCount = 0;
1441
+ let lastSourceCount = 0;
1442
+
1443
+ while (true) {
1444
+ researchStatus = await this.checkDeepResearchStatus(jobId);
1445
+
1446
+ if ('error' in researchStatus && !researchStatus.success) {
1447
+ return researchStatus;
1448
+ }
1449
+
1450
+ // Stream new activities through the callback if provided
1451
+ if (onActivity && researchStatus.activities) {
1452
+ const newActivities = researchStatus.activities.slice(lastActivityCount);
1453
+ for (const activity of newActivities) {
1454
+ onActivity(activity);
1455
+ }
1456
+ lastActivityCount = researchStatus.activities.length;
1457
+ }
1458
+
1459
+ // Stream new sources through the callback if provided
1460
+ if (onSource && researchStatus.sources) {
1461
+ const newSources = researchStatus.sources.slice(lastSourceCount);
1462
+ for (const source of newSources) {
1463
+ onSource(source);
1464
+ }
1465
+ lastSourceCount = researchStatus.sources.length;
1466
+ }
1467
+
1468
+ if (researchStatus.status === "completed") {
1469
+ return researchStatus;
1470
+ }
1471
+
1472
+ if (researchStatus.status === "failed") {
1473
+ throw new FirecrawlError(
1474
+ `Research job ${researchStatus.status}. Error: ${researchStatus.error}`,
1475
+ 500
1476
+ );
1477
+ }
1478
+
1479
+ if (researchStatus.status !== "processing") {
1480
+ break;
1481
+ }
1482
+
1483
+ await new Promise(resolve => setTimeout(resolve, 2000));
1484
+ }
1485
+
1486
+ return { success: false, error: "Research job terminated unexpectedly" };
1487
+ } catch (error: any) {
1488
+ throw new FirecrawlError(error.message, 500, error.response?.data?.details);
1489
+ }
1490
+ }
1491
+
1492
+ /**
1493
+ * Initiates a deep research operation on a given query without polling.
1494
+ * @param params - Parameters for the deep research operation.
1495
+ * @returns The response containing the research job ID.
1496
+ */
1497
+ async asyncDeepResearch(query: string, params: DeepResearchParams): Promise<DeepResearchResponse | ErrorResponse> {
1498
+ const headers = this.prepareHeaders();
1499
+ try {
1500
+ const response: AxiosResponse = await this.postRequest(
1501
+ `${this.apiUrl}/v1/deep-research`,
1502
+ { query, ...params },
1503
+ headers
1504
+ );
1505
+
1506
+ if (response.status === 200) {
1507
+ return response.data;
1508
+ } else {
1509
+ this.handleError(response, "start deep research");
1510
+ }
1511
+ } catch (error: any) {
1512
+ if (error.response?.data?.error) {
1513
+ 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);
1514
+ } else {
1515
+ throw new FirecrawlError(error.message, 500);
1516
+ }
1517
+ }
1518
+ return { success: false, error: "Internal server error." };
1519
+ }
1520
+
1521
+ /**
1522
+ * Checks the status of a deep research operation.
1523
+ * @param id - The ID of the deep research operation.
1524
+ * @returns The current status and results of the research operation.
1525
+ */
1526
+ async checkDeepResearchStatus(id: string): Promise<DeepResearchStatusResponse | ErrorResponse> {
1527
+ const headers = this.prepareHeaders();
1528
+ try {
1529
+ const response: AxiosResponse = await this.getRequest(
1530
+ `${this.apiUrl}/v1/deep-research/${id}`,
1531
+ headers
1532
+ );
1533
+
1534
+ if (response.status === 200) {
1535
+ return response.data;
1536
+ } else if (response.status === 404) {
1537
+ throw new FirecrawlError("Deep research job not found", 404);
1538
+ } else {
1539
+ this.handleError(response, "check deep research status");
1540
+ }
1541
+ } catch (error: any) {
1542
+ if (error.response?.data?.error) {
1543
+ 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);
1544
+ } else {
1545
+ throw new FirecrawlError(error.message, 500);
1546
+ }
1547
+ }
1548
+ return { success: false, error: "Internal server error." };
1549
+ }
1550
+
1551
+ /**
1552
+ * @deprecated Use deepResearch() instead
1398
1553
  * Initiates a deep research operation on a given topic and polls until completion.
1399
1554
  * @param topic - The topic to research.
1400
1555
  * @param params - Parameters for the deep research operation.
@@ -1468,6 +1623,7 @@ export default class FirecrawlApp {
1468
1623
  }
1469
1624
 
1470
1625
  /**
1626
+ * @deprecated Use asyncDeepResearch() instead
1471
1627
  * Initiates a deep research operation on a given topic without polling.
1472
1628
  * @param params - Parameters for the deep research operation.
1473
1629
  * @returns The response containing the research job ID.
@@ -1497,6 +1653,7 @@ export default class FirecrawlApp {
1497
1653
  }
1498
1654
 
1499
1655
  /**
1656
+ * @deprecated Use checkDeepResearchStatus() instead
1500
1657
  * Checks the status of a deep research operation.
1501
1658
  * @param id - The ID of the deep research operation.
1502
1659
  * @returns The current status and results of the research operation.