exa-js 2.5.1-beta.1 → 2.6.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/README.md +7 -1
- package/dist/index.d.mts +272 -8679
- package/dist/index.d.ts +272 -8679
- package/dist/index.js +9 -644
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9 -643
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -34,9 +34,8 @@ __export(index_exports, {
|
|
|
34
34
|
CreateImportParametersFormat: () => CreateImportParametersFormat,
|
|
35
35
|
EventType: () => EventType,
|
|
36
36
|
EventsClient: () => EventsClient,
|
|
37
|
-
Exa: () =>
|
|
37
|
+
Exa: () => Exa2,
|
|
38
38
|
ExaError: () => ExaError,
|
|
39
|
-
ExaNova: () => Exa2,
|
|
40
39
|
HttpStatusCode: () => HttpStatusCode,
|
|
41
40
|
ImportFailedReason: () => ImportFailedReason,
|
|
42
41
|
ImportFormat: () => ImportFormat,
|
|
@@ -75,7 +74,7 @@ var import_cross_fetch = __toESM(require("cross-fetch"));
|
|
|
75
74
|
// package.json
|
|
76
75
|
var package_default = {
|
|
77
76
|
name: "exa-js",
|
|
78
|
-
version: "2.
|
|
77
|
+
version: "2.6.0",
|
|
79
78
|
description: "Exa SDK for Node.js and the browser",
|
|
80
79
|
publishConfig: {
|
|
81
80
|
access: "public"
|
|
@@ -1433,644 +1432,11 @@ var WebsetsClient = class extends WebsetsBaseClient {
|
|
|
1433
1432
|
}
|
|
1434
1433
|
};
|
|
1435
1434
|
|
|
1436
|
-
// src/nova/websets-client.ts
|
|
1437
|
-
var WebsetsBaseClient2 = class {
|
|
1438
|
-
constructor(client) {
|
|
1439
|
-
this.client = client;
|
|
1440
|
-
}
|
|
1441
|
-
/** Make a request to the API, prepending `/websets` to the path. */
|
|
1442
|
-
async request(endpoint, method = "POST", data, params, headers) {
|
|
1443
|
-
return this.client.request(`/websets${endpoint}`, method, data, params, headers);
|
|
1444
|
-
}
|
|
1445
|
-
buildPaginationParams(pagination) {
|
|
1446
|
-
const params = {};
|
|
1447
|
-
if (!pagination) return params;
|
|
1448
|
-
if (pagination.cursor) params.cursor = pagination.cursor;
|
|
1449
|
-
if (pagination.limit) params.limit = pagination.limit;
|
|
1450
|
-
return params;
|
|
1451
|
-
}
|
|
1452
|
-
};
|
|
1453
|
-
var WebsetItemsClient2 = class extends WebsetsBaseClient2 {
|
|
1454
|
-
async get(websetId, id) {
|
|
1455
|
-
return this.request(`/v0/websets/${websetId}/items/${id}`, "GET");
|
|
1456
|
-
}
|
|
1457
|
-
async delete(websetId, id) {
|
|
1458
|
-
return this.request(`/v0/websets/${websetId}/items/${id}`, "DELETE");
|
|
1459
|
-
}
|
|
1460
|
-
async list(websetId, options) {
|
|
1461
|
-
const queryParams = { ...this.buildPaginationParams(options), sourceId: options?.sourceId };
|
|
1462
|
-
return this.request(`/v0/websets/${websetId}/items`, "GET", void 0, queryParams);
|
|
1463
|
-
}
|
|
1464
|
-
/** Iterate through all items in a Webset, handling pagination automatically. */
|
|
1465
|
-
async *listAll(websetId, options) {
|
|
1466
|
-
let cursor = void 0;
|
|
1467
|
-
const pageOptions = options ? { ...options } : {};
|
|
1468
|
-
while (true) {
|
|
1469
|
-
const response = await this.list(websetId, { ...pageOptions, cursor });
|
|
1470
|
-
for (const item of response.data) {
|
|
1471
|
-
yield item;
|
|
1472
|
-
}
|
|
1473
|
-
if (!response.hasMore || !response.nextCursor) {
|
|
1474
|
-
break;
|
|
1475
|
-
}
|
|
1476
|
-
cursor = response.nextCursor;
|
|
1477
|
-
}
|
|
1478
|
-
}
|
|
1479
|
-
/** Collect all items from a Webset into an array. */
|
|
1480
|
-
async getAll(websetId, options) {
|
|
1481
|
-
const items = [];
|
|
1482
|
-
for await (const item of this.listAll(websetId, options)) {
|
|
1483
|
-
items.push(item);
|
|
1484
|
-
}
|
|
1485
|
-
return items;
|
|
1486
|
-
}
|
|
1487
|
-
};
|
|
1488
|
-
var WebsetSearchesClient2 = class extends WebsetsBaseClient2 {
|
|
1489
|
-
async create(websetId, params, options) {
|
|
1490
|
-
return this.request(`/v0/websets/${websetId}/searches`, "POST", { ...params }, void 0, options?.headers);
|
|
1491
|
-
}
|
|
1492
|
-
async get(websetId, id) {
|
|
1493
|
-
return this.request(`/v0/websets/${websetId}/searches/${id}`, "GET");
|
|
1494
|
-
}
|
|
1495
|
-
async cancel(websetId, id) {
|
|
1496
|
-
return this.request(`/v0/websets/${websetId}/searches/${id}/cancel`, "POST");
|
|
1497
|
-
}
|
|
1498
|
-
};
|
|
1499
|
-
var WebsetEnrichmentsClient2 = class extends WebsetsBaseClient2 {
|
|
1500
|
-
async create(websetId, params) {
|
|
1501
|
-
return this.request(`/v0/websets/${websetId}/enrichments`, "POST", { ...params });
|
|
1502
|
-
}
|
|
1503
|
-
async update(websetId, id, params) {
|
|
1504
|
-
return this.request(`/v0/websets/${websetId}/enrichments/${id}`, "PATCH", { ...params });
|
|
1505
|
-
}
|
|
1506
|
-
async get(websetId, id) {
|
|
1507
|
-
return this.request(`/v0/websets/${websetId}/enrichments/${id}`, "GET");
|
|
1508
|
-
}
|
|
1509
|
-
async delete(websetId, id) {
|
|
1510
|
-
return this.request(`/v0/websets/${websetId}/enrichments/${id}`, "DELETE");
|
|
1511
|
-
}
|
|
1512
|
-
async cancel(websetId, id) {
|
|
1513
|
-
return this.request(`/v0/websets/${websetId}/enrichments/${id}/cancel`, "POST");
|
|
1514
|
-
}
|
|
1515
|
-
};
|
|
1516
|
-
var EventsClient2 = class extends WebsetsBaseClient2 {
|
|
1517
|
-
async list(options) {
|
|
1518
|
-
const queryParams = { ...this.buildPaginationParams(options), types: options?.types, createdBefore: options?.createdBefore, createdAfter: options?.createdAfter };
|
|
1519
|
-
return this.request("/v0/events", "GET", void 0, queryParams);
|
|
1520
|
-
}
|
|
1521
|
-
async get(id) {
|
|
1522
|
-
return this.request(`/v0/events/${id}`, "GET");
|
|
1523
|
-
}
|
|
1524
|
-
};
|
|
1525
|
-
var WebsetMonitorRunsClient2 = class extends WebsetsBaseClient2 {
|
|
1526
|
-
async list(monitorId) {
|
|
1527
|
-
return this.request(`/v0/monitors/${monitorId}/runs`, "GET");
|
|
1528
|
-
}
|
|
1529
|
-
async get(monitorId, id) {
|
|
1530
|
-
return this.request(`/v0/monitors/${monitorId}/runs/${id}`, "GET");
|
|
1531
|
-
}
|
|
1532
|
-
};
|
|
1533
|
-
var WebsetMonitorsClient2 = class extends WebsetsBaseClient2 {
|
|
1534
|
-
constructor(client) {
|
|
1535
|
-
super(client);
|
|
1536
|
-
this.runs = new WebsetMonitorRunsClient2(client);
|
|
1537
|
-
}
|
|
1538
|
-
async create(params) {
|
|
1539
|
-
return this.request("/v0/monitors", "POST", { ...params });
|
|
1540
|
-
}
|
|
1541
|
-
async list(options) {
|
|
1542
|
-
const queryParams = { ...this.buildPaginationParams(options), websetId: options?.websetId };
|
|
1543
|
-
return this.request("/v0/monitors", "GET", void 0, queryParams);
|
|
1544
|
-
}
|
|
1545
|
-
async get(id) {
|
|
1546
|
-
return this.request(`/v0/monitors/${id}`, "GET");
|
|
1547
|
-
}
|
|
1548
|
-
async update(id, params) {
|
|
1549
|
-
return this.request(`/v0/monitors/${id}`, "PATCH", { ...params });
|
|
1550
|
-
}
|
|
1551
|
-
async delete(id) {
|
|
1552
|
-
return this.request(`/v0/monitors/${id}`, "DELETE");
|
|
1553
|
-
}
|
|
1554
|
-
/** Iterate through all monitors, handling pagination automatically. */
|
|
1555
|
-
async *listAll(options) {
|
|
1556
|
-
let cursor = void 0;
|
|
1557
|
-
const pageOptions = options ? { ...options } : {};
|
|
1558
|
-
while (true) {
|
|
1559
|
-
const response = await this.list({ ...pageOptions, cursor });
|
|
1560
|
-
for (const monitor of response.data) {
|
|
1561
|
-
yield monitor;
|
|
1562
|
-
}
|
|
1563
|
-
if (!response.hasMore || !response.nextCursor) {
|
|
1564
|
-
break;
|
|
1565
|
-
}
|
|
1566
|
-
cursor = response.nextCursor;
|
|
1567
|
-
}
|
|
1568
|
-
}
|
|
1569
|
-
/** Collect all monitors into an array. */
|
|
1570
|
-
async getAll(options) {
|
|
1571
|
-
const monitors = [];
|
|
1572
|
-
for await (const monitor of this.listAll(options)) {
|
|
1573
|
-
monitors.push(monitor);
|
|
1574
|
-
}
|
|
1575
|
-
return monitors;
|
|
1576
|
-
}
|
|
1577
|
-
};
|
|
1578
|
-
var WebsetWebhooksClient2 = class extends WebsetsBaseClient2 {
|
|
1579
|
-
async create(params) {
|
|
1580
|
-
return this.request("/v0/webhooks", "POST", { ...params });
|
|
1581
|
-
}
|
|
1582
|
-
async list(options) {
|
|
1583
|
-
const queryParams = this.buildPaginationParams(options);
|
|
1584
|
-
return this.request("/v0/webhooks", "GET", void 0, queryParams);
|
|
1585
|
-
}
|
|
1586
|
-
async get(id) {
|
|
1587
|
-
return this.request(`/v0/webhooks/${id}`, "GET");
|
|
1588
|
-
}
|
|
1589
|
-
async update(id, params) {
|
|
1590
|
-
return this.request(`/v0/webhooks/${id}`, "PATCH", { ...params });
|
|
1591
|
-
}
|
|
1592
|
-
async delete(id) {
|
|
1593
|
-
return this.request(`/v0/webhooks/${id}`, "DELETE");
|
|
1594
|
-
}
|
|
1595
|
-
async listAttempts(id, options) {
|
|
1596
|
-
const queryParams = { ...this.buildPaginationParams(options), eventType: options?.eventType, successful: options?.successful };
|
|
1597
|
-
return this.request(`/v0/webhooks/${id}/attempts`, "GET", void 0, queryParams);
|
|
1598
|
-
}
|
|
1599
|
-
};
|
|
1600
|
-
var ImportsClient2 = class extends WebsetsBaseClient2 {
|
|
1601
|
-
async create(params) {
|
|
1602
|
-
return this.request("/v0/imports", "POST", { ...params });
|
|
1603
|
-
}
|
|
1604
|
-
async list(options) {
|
|
1605
|
-
const queryParams = this.buildPaginationParams(options);
|
|
1606
|
-
return this.request("/v0/imports", "GET", void 0, queryParams);
|
|
1607
|
-
}
|
|
1608
|
-
async get(id) {
|
|
1609
|
-
return this.request(`/v0/imports/${id}`, "GET");
|
|
1610
|
-
}
|
|
1611
|
-
async update(id, params) {
|
|
1612
|
-
return this.request(`/v0/imports/${id}`, "PATCH", { ...params });
|
|
1613
|
-
}
|
|
1614
|
-
async delete(id) {
|
|
1615
|
-
return this.request(`/v0/imports/${id}`, "DELETE");
|
|
1616
|
-
}
|
|
1617
|
-
};
|
|
1618
|
-
var WebsetsClient2 = class extends WebsetsBaseClient2 {
|
|
1619
|
-
constructor(client) {
|
|
1620
|
-
super(client);
|
|
1621
|
-
this.items = new WebsetItemsClient2(client);
|
|
1622
|
-
this.searches = new WebsetSearchesClient2(client);
|
|
1623
|
-
this.enrichments = new WebsetEnrichmentsClient2(client);
|
|
1624
|
-
this.events = new EventsClient2(client);
|
|
1625
|
-
this.monitors = new WebsetMonitorsClient2(client);
|
|
1626
|
-
this.webhooks = new WebsetWebhooksClient2(client);
|
|
1627
|
-
this.imports = new ImportsClient2(client);
|
|
1628
|
-
}
|
|
1629
|
-
async create(params, options) {
|
|
1630
|
-
return this.request("/v0/websets", "POST", { ...params }, void 0, options?.headers);
|
|
1631
|
-
}
|
|
1632
|
-
async list(options) {
|
|
1633
|
-
const queryParams = { ...this.buildPaginationParams(options), search: options?.search };
|
|
1634
|
-
return this.request("/v0/websets", "GET", void 0, queryParams);
|
|
1635
|
-
}
|
|
1636
|
-
async get(id, options) {
|
|
1637
|
-
const queryParams = { expand: options?.expand };
|
|
1638
|
-
return this.request(`/v0/websets/${id}`, "GET", void 0, queryParams);
|
|
1639
|
-
}
|
|
1640
|
-
async update(id, params) {
|
|
1641
|
-
return this.request(`/v0/websets/${id}`, "POST", { ...params });
|
|
1642
|
-
}
|
|
1643
|
-
async delete(id) {
|
|
1644
|
-
return this.request(`/v0/websets/${id}`, "DELETE");
|
|
1645
|
-
}
|
|
1646
|
-
async cancel(id) {
|
|
1647
|
-
return this.request(`/v0/websets/${id}/cancel`, "POST");
|
|
1648
|
-
}
|
|
1649
|
-
async preview(params, options) {
|
|
1650
|
-
const queryParams = { search: options?.search };
|
|
1651
|
-
return this.request("/v0/websets/preview", "POST", { ...params }, queryParams);
|
|
1652
|
-
}
|
|
1653
|
-
/** Iterate through all Websets, handling pagination automatically. */
|
|
1654
|
-
async *listAll(options) {
|
|
1655
|
-
let cursor = void 0;
|
|
1656
|
-
const pageOptions = options ? { ...options } : {};
|
|
1657
|
-
while (true) {
|
|
1658
|
-
const response = await this.list({ ...pageOptions, cursor });
|
|
1659
|
-
for (const webset of response.data) {
|
|
1660
|
-
yield webset;
|
|
1661
|
-
}
|
|
1662
|
-
if (!response.hasMore || !response.nextCursor) {
|
|
1663
|
-
break;
|
|
1664
|
-
}
|
|
1665
|
-
cursor = response.nextCursor;
|
|
1666
|
-
}
|
|
1667
|
-
}
|
|
1668
|
-
/** Collect all Websets into an array. */
|
|
1669
|
-
async getAll(options) {
|
|
1670
|
-
const websets = [];
|
|
1671
|
-
for await (const webset of this.listAll(options)) {
|
|
1672
|
-
websets.push(webset);
|
|
1673
|
-
}
|
|
1674
|
-
return websets;
|
|
1675
|
-
}
|
|
1676
|
-
/** Poll until a Webset reaches idle status. */
|
|
1677
|
-
async waitUntilIdle(id, options) {
|
|
1678
|
-
let timeout;
|
|
1679
|
-
let pollInterval = 1e3;
|
|
1680
|
-
let onPoll;
|
|
1681
|
-
if (typeof options === "number") {
|
|
1682
|
-
timeout = options;
|
|
1683
|
-
} else if (options) {
|
|
1684
|
-
timeout = options.timeout;
|
|
1685
|
-
pollInterval = options.pollInterval || 1e3;
|
|
1686
|
-
onPoll = options.onPoll;
|
|
1687
|
-
}
|
|
1688
|
-
const startTime = Date.now();
|
|
1689
|
-
while (true) {
|
|
1690
|
-
const webset = await this.get(id);
|
|
1691
|
-
if (onPoll) onPoll(webset.status);
|
|
1692
|
-
if (webset.status === "idle") return webset;
|
|
1693
|
-
if (timeout && Date.now() - startTime > timeout) {
|
|
1694
|
-
throw new Error(`Webset ${id} did not reach idle state within ${timeout}ms. Current status: ${webset.status}`);
|
|
1695
|
-
}
|
|
1696
|
-
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
1697
|
-
}
|
|
1698
|
-
}
|
|
1699
|
-
};
|
|
1700
|
-
|
|
1701
|
-
// src/nova/research-client.ts
|
|
1702
|
-
var import_zod2 = require("zod");
|
|
1703
|
-
var import_zod_to_json_schema2 = require("zod-to-json-schema");
|
|
1704
|
-
function zodToJsonSchema2(schema) {
|
|
1705
|
-
return (0, import_zod_to_json_schema2.zodToJsonSchema)(schema, { $refStrategy: "none" });
|
|
1706
|
-
}
|
|
1707
|
-
var ResearchBaseClient2 = class {
|
|
1708
|
-
constructor(client) {
|
|
1709
|
-
this.client = client;
|
|
1710
|
-
}
|
|
1711
|
-
/** Make a request to the API, prepending `/research/v1` to the path. */
|
|
1712
|
-
async request(endpoint, method = "POST", data, params) {
|
|
1713
|
-
return this.client.request(`/research/v1${endpoint}`, method, data, params);
|
|
1714
|
-
}
|
|
1715
|
-
buildPaginationParams(pagination) {
|
|
1716
|
-
const params = {};
|
|
1717
|
-
if (!pagination) return params;
|
|
1718
|
-
if (pagination.cursor) params.cursor = pagination.cursor;
|
|
1719
|
-
if (pagination.limit) params.limit = pagination.limit;
|
|
1720
|
-
return params;
|
|
1721
|
-
}
|
|
1722
|
-
};
|
|
1723
|
-
var ResearchClient2 = class extends ResearchBaseClient2 {
|
|
1724
|
-
/** List research requests. */
|
|
1725
|
-
async list(options) {
|
|
1726
|
-
const params = this.buildPaginationParams(options);
|
|
1727
|
-
return this.client.request("/research/v1", "GET", void 0, params);
|
|
1728
|
-
}
|
|
1729
|
-
/** Create a new research request. Defaults model to "exa-research-fast" if not provided. */
|
|
1730
|
-
async create(params) {
|
|
1731
|
-
let schema = params.outputSchema;
|
|
1732
|
-
if (schema instanceof import_zod2.ZodType) {
|
|
1733
|
-
schema = zodToJsonSchema2(schema);
|
|
1734
|
-
}
|
|
1735
|
-
const payload = {
|
|
1736
|
-
instructions: params.instructions,
|
|
1737
|
-
model: params.model ?? "exa-research-fast"
|
|
1738
|
-
};
|
|
1739
|
-
if (schema) {
|
|
1740
|
-
payload.outputSchema = schema;
|
|
1741
|
-
}
|
|
1742
|
-
return this.client.request("/research/v1", "POST", payload, void 0);
|
|
1743
|
-
}
|
|
1744
|
-
/** Get a research request by ID. */
|
|
1745
|
-
async get(researchId, options) {
|
|
1746
|
-
const params = {};
|
|
1747
|
-
if (options?.events !== void 0) params.events = options.events.toString();
|
|
1748
|
-
return this.client.request(`/research/v1/${researchId}`, "GET", void 0, params);
|
|
1749
|
-
}
|
|
1750
|
-
/** Poll a research request until it reaches a terminal state (completed, failed, or canceled). */
|
|
1751
|
-
async pollUntilFinished(researchId, options) {
|
|
1752
|
-
const pollInterval = options?.pollInterval ?? 1e3;
|
|
1753
|
-
const timeoutMs = options?.timeoutMs ?? 10 * 60 * 1e3;
|
|
1754
|
-
const maxConsecutiveFailures = 5;
|
|
1755
|
-
const startTime = Date.now();
|
|
1756
|
-
let consecutiveFailures = 0;
|
|
1757
|
-
while (true) {
|
|
1758
|
-
try {
|
|
1759
|
-
const research = await this.get(researchId, { events: options?.events });
|
|
1760
|
-
consecutiveFailures = 0;
|
|
1761
|
-
if (research.status === "completed" || research.status === "failed" || research.status === "canceled") {
|
|
1762
|
-
return research;
|
|
1763
|
-
}
|
|
1764
|
-
} catch (err) {
|
|
1765
|
-
consecutiveFailures += 1;
|
|
1766
|
-
if (consecutiveFailures >= maxConsecutiveFailures) {
|
|
1767
|
-
throw new Error(`Polling failed ${maxConsecutiveFailures} times in a row for research ${researchId}: ${err}`);
|
|
1768
|
-
}
|
|
1769
|
-
}
|
|
1770
|
-
if (Date.now() - startTime > timeoutMs) {
|
|
1771
|
-
throw new Error(`Polling timeout: Research ${researchId} did not complete within ${timeoutMs}ms`);
|
|
1772
|
-
}
|
|
1773
|
-
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
1774
|
-
}
|
|
1775
|
-
}
|
|
1776
|
-
};
|
|
1777
|
-
|
|
1778
|
-
// src/nova/client.ts
|
|
1779
|
-
function isRecord(value) {
|
|
1780
|
-
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
1781
|
-
}
|
|
1782
|
-
var ExaError2 = class extends Error {
|
|
1783
|
-
constructor(message, statusCode, timestamp, path, details) {
|
|
1784
|
-
super(message);
|
|
1785
|
-
this.name = "ExaError";
|
|
1786
|
-
this.statusCode = statusCode;
|
|
1787
|
-
this.timestamp = timestamp;
|
|
1788
|
-
this.path = path;
|
|
1789
|
-
this.details = details;
|
|
1790
|
-
}
|
|
1791
|
-
};
|
|
1792
|
-
var CONTENTS_FIELDS = /* @__PURE__ */ new Set(["text", "highlights", "summary", "subpages", "subpageTarget", "extras", "livecrawl", "livecrawlTimeout", "maxAgeHours", "filterEmptyResults", "context"]);
|
|
1793
|
-
var DEFAULT_MAX_CHARACTERS = 1e4;
|
|
1794
|
-
var Exa2 = class {
|
|
1795
|
-
constructor(apiKey, baseURL = "https://api.exa.ai") {
|
|
1796
|
-
this.baseURL = baseURL;
|
|
1797
|
-
if (!apiKey) {
|
|
1798
|
-
apiKey = typeof process !== "undefined" ? process.env?.EXA_API_KEY : void 0;
|
|
1799
|
-
if (!apiKey) {
|
|
1800
|
-
throw new ExaError2(
|
|
1801
|
-
"API key must be provided as an argument or as an environment variable (EXA_API_KEY)",
|
|
1802
|
-
401
|
|
1803
|
-
);
|
|
1804
|
-
}
|
|
1805
|
-
}
|
|
1806
|
-
this.headers = {
|
|
1807
|
-
"x-api-key": apiKey,
|
|
1808
|
-
"Content-Type": "application/json"
|
|
1809
|
-
};
|
|
1810
|
-
this.websets = new WebsetsClient2(this);
|
|
1811
|
-
this.research = new ResearchClient2(this);
|
|
1812
|
-
}
|
|
1813
|
-
/** Make a request to the Exa API. Used by sub-clients and available for direct use. */
|
|
1814
|
-
async request(endpoint, method, body, params, headers) {
|
|
1815
|
-
const url = this.buildRequestUrl(endpoint, params);
|
|
1816
|
-
const normalizedBody = this.normalizeCategoryInBody(body);
|
|
1817
|
-
const combinedHeaders = headers ? { ...this.headers, ...headers } : this.headers;
|
|
1818
|
-
const response = await fetch(url, {
|
|
1819
|
-
method,
|
|
1820
|
-
headers: combinedHeaders,
|
|
1821
|
-
body: normalizedBody ? JSON.stringify(normalizedBody) : void 0
|
|
1822
|
-
});
|
|
1823
|
-
if (!response.ok) {
|
|
1824
|
-
throw await this.toExaError(response, endpoint);
|
|
1825
|
-
}
|
|
1826
|
-
return await response.json();
|
|
1827
|
-
}
|
|
1828
|
-
/** Make a raw request returning the Response object directly. */
|
|
1829
|
-
async rawRequest(endpoint, method = "POST", body, params) {
|
|
1830
|
-
const url = this.buildRequestUrl(endpoint, params);
|
|
1831
|
-
const normalizedBody = this.normalizeCategoryInBody(body);
|
|
1832
|
-
return fetch(url, {
|
|
1833
|
-
method,
|
|
1834
|
-
headers: this.headers,
|
|
1835
|
-
body: normalizedBody ? JSON.stringify(normalizedBody) : void 0
|
|
1836
|
-
});
|
|
1837
|
-
}
|
|
1838
|
-
/** Normalize category values to underscore form. */
|
|
1839
|
-
normalizeCategoryInBody(body) {
|
|
1840
|
-
if (body == null || typeof body.category !== "string") {
|
|
1841
|
-
return body;
|
|
1842
|
-
}
|
|
1843
|
-
return { ...body, category: body.category.replace(/ /g, "_") };
|
|
1844
|
-
}
|
|
1845
|
-
buildRequestUrl(endpoint, params) {
|
|
1846
|
-
let url = `${this.baseURL}${endpoint}`;
|
|
1847
|
-
if (params == null || Object.keys(params).length === 0) {
|
|
1848
|
-
return url;
|
|
1849
|
-
}
|
|
1850
|
-
const searchParams = new URLSearchParams();
|
|
1851
|
-
for (const [key, value] of Object.entries(params)) {
|
|
1852
|
-
if (Array.isArray(value)) {
|
|
1853
|
-
for (const item of value) {
|
|
1854
|
-
searchParams.append(key, item);
|
|
1855
|
-
}
|
|
1856
|
-
} else if (value !== void 0) {
|
|
1857
|
-
searchParams.append(key, String(value));
|
|
1858
|
-
}
|
|
1859
|
-
}
|
|
1860
|
-
url += `?${searchParams.toString()}`;
|
|
1861
|
-
return url;
|
|
1862
|
-
}
|
|
1863
|
-
tryParseJsonRecord(text) {
|
|
1864
|
-
if (text.length === 0) {
|
|
1865
|
-
return null;
|
|
1866
|
-
}
|
|
1867
|
-
try {
|
|
1868
|
-
const parsed = JSON.parse(text);
|
|
1869
|
-
return isRecord(parsed) ? parsed : null;
|
|
1870
|
-
} catch {
|
|
1871
|
-
return null;
|
|
1872
|
-
}
|
|
1873
|
-
}
|
|
1874
|
-
async toExaError(response, endpoint) {
|
|
1875
|
-
const text = await response.text();
|
|
1876
|
-
const parsed = this.tryParseJsonRecord(text);
|
|
1877
|
-
const statusCode = parsed != null && typeof parsed.statusCode === "number" ? parsed.statusCode : response.status;
|
|
1878
|
-
const timestamp = parsed != null && typeof parsed.timestamp === "string" ? parsed.timestamp : void 0;
|
|
1879
|
-
const path = parsed != null && typeof parsed.path === "string" ? parsed.path : endpoint;
|
|
1880
|
-
const errorMessage = parsed != null && typeof parsed.error === "string" ? parsed.error : void 0;
|
|
1881
|
-
const detailMessage = parsed != null && typeof parsed.message === "string" ? parsed.message : void 0;
|
|
1882
|
-
let message;
|
|
1883
|
-
if (errorMessage != null && detailMessage != null) {
|
|
1884
|
-
message = `${errorMessage}. ${detailMessage}`;
|
|
1885
|
-
} else if (errorMessage != null) {
|
|
1886
|
-
message = errorMessage;
|
|
1887
|
-
} else if (detailMessage != null) {
|
|
1888
|
-
message = detailMessage;
|
|
1889
|
-
} else if (text.length > 0) {
|
|
1890
|
-
message = text;
|
|
1891
|
-
} else {
|
|
1892
|
-
message = `Exa API error ${response.status}`;
|
|
1893
|
-
}
|
|
1894
|
-
return new ExaError2(message, statusCode, timestamp, path, parsed ?? text);
|
|
1895
|
-
}
|
|
1896
|
-
parseAnswerStreamLine(line) {
|
|
1897
|
-
if (!line.startsWith("data:")) {
|
|
1898
|
-
return null;
|
|
1899
|
-
}
|
|
1900
|
-
const jsonStr = line.replace(/^data:\s*/, "").trim();
|
|
1901
|
-
if (jsonStr.length === 0 || jsonStr === "[DONE]") {
|
|
1902
|
-
return null;
|
|
1903
|
-
}
|
|
1904
|
-
let parsedChunk;
|
|
1905
|
-
try {
|
|
1906
|
-
parsedChunk = JSON.parse(jsonStr);
|
|
1907
|
-
} catch {
|
|
1908
|
-
return null;
|
|
1909
|
-
}
|
|
1910
|
-
if (!isRecord(parsedChunk)) {
|
|
1911
|
-
return null;
|
|
1912
|
-
}
|
|
1913
|
-
let content;
|
|
1914
|
-
const choices = parsedChunk.choices;
|
|
1915
|
-
if (Array.isArray(choices) && choices.length > 0 && isRecord(choices[0])) {
|
|
1916
|
-
const delta = choices[0].delta;
|
|
1917
|
-
if (isRecord(delta) && typeof delta.content === "string") {
|
|
1918
|
-
content = delta.content;
|
|
1919
|
-
}
|
|
1920
|
-
}
|
|
1921
|
-
let citations;
|
|
1922
|
-
if (Array.isArray(parsedChunk.citations)) {
|
|
1923
|
-
const normalized = [];
|
|
1924
|
-
for (const citation of parsedChunk.citations) {
|
|
1925
|
-
if (!isRecord(citation)) {
|
|
1926
|
-
continue;
|
|
1927
|
-
}
|
|
1928
|
-
const id = typeof citation.id === "string" ? citation.id : void 0;
|
|
1929
|
-
const url = typeof citation.url === "string" ? citation.url : void 0;
|
|
1930
|
-
const title = typeof citation.title === "string" ? citation.title : void 0;
|
|
1931
|
-
const publishedDate = typeof citation.publishedDate === "string" ? citation.publishedDate : void 0;
|
|
1932
|
-
const author = typeof citation.author === "string" ? citation.author : void 0;
|
|
1933
|
-
const text = typeof citation.text === "string" ? citation.text : void 0;
|
|
1934
|
-
normalized.push({
|
|
1935
|
-
...id != null ? { id } : {},
|
|
1936
|
-
...url != null ? { url } : {},
|
|
1937
|
-
...title != null ? { title } : {},
|
|
1938
|
-
...publishedDate != null ? { publishedDate } : {},
|
|
1939
|
-
...author != null ? { author } : {},
|
|
1940
|
-
...text != null ? { text } : {}
|
|
1941
|
-
});
|
|
1942
|
-
}
|
|
1943
|
-
if (normalized.length > 0) {
|
|
1944
|
-
citations = normalized;
|
|
1945
|
-
}
|
|
1946
|
-
}
|
|
1947
|
-
if (content == null && (citations == null || citations.length === 0)) {
|
|
1948
|
-
return null;
|
|
1949
|
-
}
|
|
1950
|
-
return {
|
|
1951
|
-
...content != null ? { content } : {},
|
|
1952
|
-
...citations != null ? { citations } : {}
|
|
1953
|
-
};
|
|
1954
|
-
}
|
|
1955
|
-
/** Separate content-specific options from search/filter options. */
|
|
1956
|
-
extractContentsOptions(options) {
|
|
1957
|
-
const contentsOptions = {};
|
|
1958
|
-
const restOptions = {};
|
|
1959
|
-
let hasContentField = false;
|
|
1960
|
-
for (const [key, value] of Object.entries(options)) {
|
|
1961
|
-
if (CONTENTS_FIELDS.has(key)) {
|
|
1962
|
-
contentsOptions[key] = value;
|
|
1963
|
-
hasContentField = true;
|
|
1964
|
-
} else {
|
|
1965
|
-
restOptions[key] = value;
|
|
1966
|
-
}
|
|
1967
|
-
}
|
|
1968
|
-
if (!hasContentField) {
|
|
1969
|
-
contentsOptions.text = { maxCharacters: DEFAULT_MAX_CHARACTERS };
|
|
1970
|
-
}
|
|
1971
|
-
return { contentsOptions, restOptions };
|
|
1972
|
-
}
|
|
1973
|
-
search(query, options) {
|
|
1974
|
-
let body;
|
|
1975
|
-
if (options === void 0 || !("contents" in options)) {
|
|
1976
|
-
body = { query, ...options, contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } } };
|
|
1977
|
-
} else if (options.contents === false || options.contents === null || options.contents === void 0) {
|
|
1978
|
-
const { contents, ...restOptions } = options;
|
|
1979
|
-
body = { query, ...restOptions };
|
|
1980
|
-
} else {
|
|
1981
|
-
body = { query, ...options };
|
|
1982
|
-
}
|
|
1983
|
-
return this.request("/search", "POST", body);
|
|
1984
|
-
}
|
|
1985
|
-
findSimilar(url, options) {
|
|
1986
|
-
let body;
|
|
1987
|
-
if (options === void 0 || !("contents" in options)) {
|
|
1988
|
-
body = { url, ...options, contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } } };
|
|
1989
|
-
} else if (options.contents === false || options.contents === null || options.contents === void 0) {
|
|
1990
|
-
const { contents, ...restOptions } = options;
|
|
1991
|
-
body = { url, ...restOptions };
|
|
1992
|
-
} else {
|
|
1993
|
-
body = { url, ...options };
|
|
1994
|
-
}
|
|
1995
|
-
return this.request("/findSimilar", "POST", body);
|
|
1996
|
-
}
|
|
1997
|
-
getContents(urls, options) {
|
|
1998
|
-
return this.request("/contents", "POST", { urls, ...options });
|
|
1999
|
-
}
|
|
2000
|
-
answer(query, options) {
|
|
2001
|
-
if (options?.stream) {
|
|
2002
|
-
throw new ExaError2("For streaming responses, please use streamAnswer() instead:\n\nfor await (const chunk of exa.streamAnswer(query)) {\n // Handle chunks\n}", 400);
|
|
2003
|
-
}
|
|
2004
|
-
return this.request("/answer", "POST", { query, model: "exa", stream: false, text: false, ...options });
|
|
2005
|
-
}
|
|
2006
|
-
async *streamAnswer(query, options) {
|
|
2007
|
-
const response = await this.rawRequest("/answer", "POST", { query, model: "exa", text: false, ...options, stream: true });
|
|
2008
|
-
if (!response.ok) {
|
|
2009
|
-
throw await this.toExaError(response, "/answer");
|
|
2010
|
-
}
|
|
2011
|
-
const reader = response.body?.getReader();
|
|
2012
|
-
if (!reader) {
|
|
2013
|
-
throw new ExaError2("No response body available for streaming.", 500, (/* @__PURE__ */ new Date()).toISOString(), "/answer");
|
|
2014
|
-
}
|
|
2015
|
-
const decoder = new TextDecoder();
|
|
2016
|
-
let buffer = "";
|
|
2017
|
-
try {
|
|
2018
|
-
while (true) {
|
|
2019
|
-
const { done, value } = await reader.read();
|
|
2020
|
-
if (done) break;
|
|
2021
|
-
buffer += decoder.decode(value, { stream: true });
|
|
2022
|
-
const lines = buffer.split("\n");
|
|
2023
|
-
buffer = lines.pop() ?? "";
|
|
2024
|
-
for (const line of lines) {
|
|
2025
|
-
const chunk = this.parseAnswerStreamLine(line);
|
|
2026
|
-
if (chunk != null) {
|
|
2027
|
-
yield chunk;
|
|
2028
|
-
}
|
|
2029
|
-
}
|
|
2030
|
-
}
|
|
2031
|
-
const trailingChunk = this.parseAnswerStreamLine(buffer);
|
|
2032
|
-
if (trailingChunk != null) {
|
|
2033
|
-
yield trailingChunk;
|
|
2034
|
-
}
|
|
2035
|
-
} finally {
|
|
2036
|
-
reader.releaseLock();
|
|
2037
|
-
}
|
|
2038
|
-
}
|
|
2039
|
-
/**
|
|
2040
|
-
* @deprecated Use `search()` instead. The search method now returns text contents by default.
|
|
2041
|
-
* Search with contents. Extracts content fields (text, highlights, summary, etc.) into a `contents` wrapper.
|
|
2042
|
-
*/
|
|
2043
|
-
async searchAndContents(query, options) {
|
|
2044
|
-
if (options === void 0) {
|
|
2045
|
-
return this.request("/search", "POST", {
|
|
2046
|
-
query,
|
|
2047
|
-
contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } }
|
|
2048
|
-
});
|
|
2049
|
-
}
|
|
2050
|
-
const { contentsOptions, restOptions } = this.extractContentsOptions(options);
|
|
2051
|
-
return this.request("/search", "POST", { query, contents: contentsOptions, ...restOptions });
|
|
2052
|
-
}
|
|
2053
|
-
/**
|
|
2054
|
-
* @deprecated Use `findSimilar()` instead. The findSimilar method now returns text contents by default.
|
|
2055
|
-
* Find similar with contents. Extracts content fields into a `contents` wrapper.
|
|
2056
|
-
*/
|
|
2057
|
-
async findSimilarAndContents(url, options) {
|
|
2058
|
-
if (options === void 0) {
|
|
2059
|
-
return this.request("/findSimilar", "POST", {
|
|
2060
|
-
url,
|
|
2061
|
-
contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } }
|
|
2062
|
-
});
|
|
2063
|
-
}
|
|
2064
|
-
const { contentsOptions, restOptions } = this.extractContentsOptions(options);
|
|
2065
|
-
return this.request("/findSimilar", "POST", { url, contents: contentsOptions, ...restOptions });
|
|
2066
|
-
}
|
|
2067
|
-
};
|
|
2068
|
-
|
|
2069
1435
|
// src/index.ts
|
|
2070
1436
|
var fetchImpl = typeof global !== "undefined" && global.fetch ? global.fetch : import_cross_fetch.default;
|
|
2071
1437
|
var HeadersImpl = typeof global !== "undefined" && global.Headers ? global.Headers : import_cross_fetch.Headers;
|
|
2072
|
-
var
|
|
2073
|
-
var
|
|
1438
|
+
var DEFAULT_MAX_CHARACTERS = 1e4;
|
|
1439
|
+
var Exa2 = class {
|
|
2074
1440
|
/**
|
|
2075
1441
|
* Helper method to separate out the contents-specific options from the rest.
|
|
2076
1442
|
*/
|
|
@@ -2237,7 +1603,7 @@ var Exa3 = class {
|
|
|
2237
1603
|
return await this.request("/search", "POST", {
|
|
2238
1604
|
query,
|
|
2239
1605
|
...options,
|
|
2240
|
-
contents: { text: { maxCharacters:
|
|
1606
|
+
contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } }
|
|
2241
1607
|
});
|
|
2242
1608
|
}
|
|
2243
1609
|
if (options.contents === false || options.contents === null || options.contents === void 0) {
|
|
@@ -2263,7 +1629,7 @@ var Exa3 = class {
|
|
|
2263
1629
|
async searchAndContents(query, options) {
|
|
2264
1630
|
const { contentsOptions, restOptions } = options === void 0 ? {
|
|
2265
1631
|
contentsOptions: {
|
|
2266
|
-
text: { maxCharacters:
|
|
1632
|
+
text: { maxCharacters: DEFAULT_MAX_CHARACTERS }
|
|
2267
1633
|
},
|
|
2268
1634
|
restOptions: {}
|
|
2269
1635
|
} : this.extractContentsOptions(options);
|
|
@@ -2278,7 +1644,7 @@ var Exa3 = class {
|
|
|
2278
1644
|
return await this.request("/findSimilar", "POST", {
|
|
2279
1645
|
url,
|
|
2280
1646
|
...options,
|
|
2281
|
-
contents: { text: { maxCharacters:
|
|
1647
|
+
contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } }
|
|
2282
1648
|
});
|
|
2283
1649
|
}
|
|
2284
1650
|
if (options.contents === false || options.contents === null || options.contents === void 0) {
|
|
@@ -2306,7 +1672,7 @@ var Exa3 = class {
|
|
|
2306
1672
|
async findSimilarAndContents(url, options) {
|
|
2307
1673
|
const { contentsOptions, restOptions } = options === void 0 ? {
|
|
2308
1674
|
contentsOptions: {
|
|
2309
|
-
text: { maxCharacters:
|
|
1675
|
+
text: { maxCharacters: DEFAULT_MAX_CHARACTERS }
|
|
2310
1676
|
},
|
|
2311
1677
|
restOptions: {}
|
|
2312
1678
|
} : this.extractContentsOptions(options);
|
|
@@ -2530,7 +1896,7 @@ var Exa3 = class {
|
|
|
2530
1896
|
});
|
|
2531
1897
|
}
|
|
2532
1898
|
};
|
|
2533
|
-
var index_default =
|
|
1899
|
+
var index_default = Exa2;
|
|
2534
1900
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2535
1901
|
0 && (module.exports = {
|
|
2536
1902
|
CreateEnrichmentParametersFormat,
|
|
@@ -2539,7 +1905,6 @@ var index_default = Exa3;
|
|
|
2539
1905
|
EventsClient,
|
|
2540
1906
|
Exa,
|
|
2541
1907
|
ExaError,
|
|
2542
|
-
ExaNova,
|
|
2543
1908
|
HttpStatusCode,
|
|
2544
1909
|
ImportFailedReason,
|
|
2545
1910
|
ImportFormat,
|