exa-js 2.5.1-beta.0 → 2.5.1-beta.1
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.d.mts +430 -401
- package/dist/index.d.ts +430 -401
- package/dist/index.js +262 -177
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +262 -177
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import fetch2, { Headers } from "cross-fetch";
|
|
|
4
4
|
// package.json
|
|
5
5
|
var package_default = {
|
|
6
6
|
name: "exa-js",
|
|
7
|
-
version: "2.5.1-beta.
|
|
7
|
+
version: "2.5.1-beta.1",
|
|
8
8
|
description: "Exa SDK for Node.js and the browser",
|
|
9
9
|
publishConfig: {
|
|
10
10
|
access: "public"
|
|
@@ -1369,13 +1369,7 @@ var WebsetsBaseClient2 = class {
|
|
|
1369
1369
|
}
|
|
1370
1370
|
/** Make a request to the API, prepending `/websets` to the path. */
|
|
1371
1371
|
async request(endpoint, method = "POST", data, params, headers) {
|
|
1372
|
-
return this.client.request(
|
|
1373
|
-
`/websets${endpoint}`,
|
|
1374
|
-
method,
|
|
1375
|
-
data,
|
|
1376
|
-
params,
|
|
1377
|
-
headers
|
|
1378
|
-
);
|
|
1372
|
+
return this.client.request(`/websets${endpoint}`, method, data, params, headers);
|
|
1379
1373
|
}
|
|
1380
1374
|
buildPaginationParams(pagination) {
|
|
1381
1375
|
const params = {};
|
|
@@ -1393,50 +1387,50 @@ var WebsetItemsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1393
1387
|
return this.request(`/v0/websets/${websetId}/items/${id}`, "DELETE");
|
|
1394
1388
|
}
|
|
1395
1389
|
async list(websetId, options) {
|
|
1396
|
-
const queryParams = {
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1390
|
+
const queryParams = { ...this.buildPaginationParams(options), sourceId: options?.sourceId };
|
|
1391
|
+
return this.request(`/v0/websets/${websetId}/items`, "GET", void 0, queryParams);
|
|
1392
|
+
}
|
|
1393
|
+
/** Iterate through all items in a Webset, handling pagination automatically. */
|
|
1394
|
+
async *listAll(websetId, options) {
|
|
1395
|
+
let cursor = void 0;
|
|
1396
|
+
const pageOptions = options ? { ...options } : {};
|
|
1397
|
+
while (true) {
|
|
1398
|
+
const response = await this.list(websetId, { ...pageOptions, cursor });
|
|
1399
|
+
for (const item of response.data) {
|
|
1400
|
+
yield item;
|
|
1401
|
+
}
|
|
1402
|
+
if (!response.hasMore || !response.nextCursor) {
|
|
1403
|
+
break;
|
|
1404
|
+
}
|
|
1405
|
+
cursor = response.nextCursor;
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
/** Collect all items from a Webset into an array. */
|
|
1409
|
+
async getAll(websetId, options) {
|
|
1410
|
+
const items = [];
|
|
1411
|
+
for await (const item of this.listAll(websetId, options)) {
|
|
1412
|
+
items.push(item);
|
|
1413
|
+
}
|
|
1414
|
+
return items;
|
|
1406
1415
|
}
|
|
1407
1416
|
};
|
|
1408
1417
|
var WebsetSearchesClient2 = class extends WebsetsBaseClient2 {
|
|
1409
1418
|
async create(websetId, params, options) {
|
|
1410
|
-
return this.request(
|
|
1411
|
-
`/v0/websets/${websetId}/searches`,
|
|
1412
|
-
"POST",
|
|
1413
|
-
{ ...params },
|
|
1414
|
-
void 0,
|
|
1415
|
-
options?.headers
|
|
1416
|
-
);
|
|
1419
|
+
return this.request(`/v0/websets/${websetId}/searches`, "POST", { ...params }, void 0, options?.headers);
|
|
1417
1420
|
}
|
|
1418
1421
|
async get(websetId, id) {
|
|
1419
1422
|
return this.request(`/v0/websets/${websetId}/searches/${id}`, "GET");
|
|
1420
1423
|
}
|
|
1421
1424
|
async cancel(websetId, id) {
|
|
1422
|
-
return this.request(
|
|
1423
|
-
`/v0/websets/${websetId}/searches/${id}/cancel`,
|
|
1424
|
-
"POST"
|
|
1425
|
-
);
|
|
1425
|
+
return this.request(`/v0/websets/${websetId}/searches/${id}/cancel`, "POST");
|
|
1426
1426
|
}
|
|
1427
1427
|
};
|
|
1428
1428
|
var WebsetEnrichmentsClient2 = class extends WebsetsBaseClient2 {
|
|
1429
1429
|
async create(websetId, params) {
|
|
1430
|
-
return this.request(`/v0/websets/${websetId}/enrichments`, "POST", {
|
|
1431
|
-
...params
|
|
1432
|
-
});
|
|
1430
|
+
return this.request(`/v0/websets/${websetId}/enrichments`, "POST", { ...params });
|
|
1433
1431
|
}
|
|
1434
1432
|
async update(websetId, id, params) {
|
|
1435
|
-
return this.request(
|
|
1436
|
-
`/v0/websets/${websetId}/enrichments/${id}`,
|
|
1437
|
-
"PATCH",
|
|
1438
|
-
{ ...params }
|
|
1439
|
-
);
|
|
1433
|
+
return this.request(`/v0/websets/${websetId}/enrichments/${id}`, "PATCH", { ...params });
|
|
1440
1434
|
}
|
|
1441
1435
|
async get(websetId, id) {
|
|
1442
1436
|
return this.request(`/v0/websets/${websetId}/enrichments/${id}`, "GET");
|
|
@@ -1445,20 +1439,12 @@ var WebsetEnrichmentsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1445
1439
|
return this.request(`/v0/websets/${websetId}/enrichments/${id}`, "DELETE");
|
|
1446
1440
|
}
|
|
1447
1441
|
async cancel(websetId, id) {
|
|
1448
|
-
return this.request(
|
|
1449
|
-
`/v0/websets/${websetId}/enrichments/${id}/cancel`,
|
|
1450
|
-
"POST"
|
|
1451
|
-
);
|
|
1442
|
+
return this.request(`/v0/websets/${websetId}/enrichments/${id}/cancel`, "POST");
|
|
1452
1443
|
}
|
|
1453
1444
|
};
|
|
1454
1445
|
var EventsClient2 = class extends WebsetsBaseClient2 {
|
|
1455
1446
|
async list(options) {
|
|
1456
|
-
const queryParams = {
|
|
1457
|
-
...this.buildPaginationParams(options),
|
|
1458
|
-
types: options?.types,
|
|
1459
|
-
createdBefore: options?.createdBefore,
|
|
1460
|
-
createdAfter: options?.createdAfter
|
|
1461
|
-
};
|
|
1447
|
+
const queryParams = { ...this.buildPaginationParams(options), types: options?.types, createdBefore: options?.createdBefore, createdAfter: options?.createdAfter };
|
|
1462
1448
|
return this.request("/v0/events", "GET", void 0, queryParams);
|
|
1463
1449
|
}
|
|
1464
1450
|
async get(id) {
|
|
@@ -1482,10 +1468,7 @@ var WebsetMonitorsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1482
1468
|
return this.request("/v0/monitors", "POST", { ...params });
|
|
1483
1469
|
}
|
|
1484
1470
|
async list(options) {
|
|
1485
|
-
const queryParams = {
|
|
1486
|
-
...this.buildPaginationParams(options),
|
|
1487
|
-
websetId: options?.websetId
|
|
1488
|
-
};
|
|
1471
|
+
const queryParams = { ...this.buildPaginationParams(options), websetId: options?.websetId };
|
|
1489
1472
|
return this.request("/v0/monitors", "GET", void 0, queryParams);
|
|
1490
1473
|
}
|
|
1491
1474
|
async get(id) {
|
|
@@ -1497,6 +1480,29 @@ var WebsetMonitorsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1497
1480
|
async delete(id) {
|
|
1498
1481
|
return this.request(`/v0/monitors/${id}`, "DELETE");
|
|
1499
1482
|
}
|
|
1483
|
+
/** Iterate through all monitors, handling pagination automatically. */
|
|
1484
|
+
async *listAll(options) {
|
|
1485
|
+
let cursor = void 0;
|
|
1486
|
+
const pageOptions = options ? { ...options } : {};
|
|
1487
|
+
while (true) {
|
|
1488
|
+
const response = await this.list({ ...pageOptions, cursor });
|
|
1489
|
+
for (const monitor of response.data) {
|
|
1490
|
+
yield monitor;
|
|
1491
|
+
}
|
|
1492
|
+
if (!response.hasMore || !response.nextCursor) {
|
|
1493
|
+
break;
|
|
1494
|
+
}
|
|
1495
|
+
cursor = response.nextCursor;
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
/** Collect all monitors into an array. */
|
|
1499
|
+
async getAll(options) {
|
|
1500
|
+
const monitors = [];
|
|
1501
|
+
for await (const monitor of this.listAll(options)) {
|
|
1502
|
+
monitors.push(monitor);
|
|
1503
|
+
}
|
|
1504
|
+
return monitors;
|
|
1505
|
+
}
|
|
1500
1506
|
};
|
|
1501
1507
|
var WebsetWebhooksClient2 = class extends WebsetsBaseClient2 {
|
|
1502
1508
|
async create(params) {
|
|
@@ -1516,17 +1522,8 @@ var WebsetWebhooksClient2 = class extends WebsetsBaseClient2 {
|
|
|
1516
1522
|
return this.request(`/v0/webhooks/${id}`, "DELETE");
|
|
1517
1523
|
}
|
|
1518
1524
|
async listAttempts(id, options) {
|
|
1519
|
-
const queryParams = {
|
|
1520
|
-
|
|
1521
|
-
eventType: options?.eventType,
|
|
1522
|
-
successful: options?.successful
|
|
1523
|
-
};
|
|
1524
|
-
return this.request(
|
|
1525
|
-
`/v0/webhooks/${id}/attempts`,
|
|
1526
|
-
"GET",
|
|
1527
|
-
void 0,
|
|
1528
|
-
queryParams
|
|
1529
|
-
);
|
|
1525
|
+
const queryParams = { ...this.buildPaginationParams(options), eventType: options?.eventType, successful: options?.successful };
|
|
1526
|
+
return this.request(`/v0/webhooks/${id}/attempts`, "GET", void 0, queryParams);
|
|
1530
1527
|
}
|
|
1531
1528
|
};
|
|
1532
1529
|
var ImportsClient2 = class extends WebsetsBaseClient2 {
|
|
@@ -1559,19 +1556,10 @@ var WebsetsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1559
1556
|
this.imports = new ImportsClient2(client);
|
|
1560
1557
|
}
|
|
1561
1558
|
async create(params, options) {
|
|
1562
|
-
return this.request(
|
|
1563
|
-
"/v0/websets",
|
|
1564
|
-
"POST",
|
|
1565
|
-
{ ...params },
|
|
1566
|
-
void 0,
|
|
1567
|
-
options?.headers
|
|
1568
|
-
);
|
|
1559
|
+
return this.request("/v0/websets", "POST", { ...params }, void 0, options?.headers);
|
|
1569
1560
|
}
|
|
1570
1561
|
async list(options) {
|
|
1571
|
-
const queryParams = {
|
|
1572
|
-
...this.buildPaginationParams(options),
|
|
1573
|
-
search: options?.search
|
|
1574
|
-
};
|
|
1562
|
+
const queryParams = { ...this.buildPaginationParams(options), search: options?.search };
|
|
1575
1563
|
return this.request("/v0/websets", "GET", void 0, queryParams);
|
|
1576
1564
|
}
|
|
1577
1565
|
async get(id, options) {
|
|
@@ -1589,12 +1577,30 @@ var WebsetsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1589
1577
|
}
|
|
1590
1578
|
async preview(params, options) {
|
|
1591
1579
|
const queryParams = { search: options?.search };
|
|
1592
|
-
return this.request(
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1580
|
+
return this.request("/v0/websets/preview", "POST", { ...params }, queryParams);
|
|
1581
|
+
}
|
|
1582
|
+
/** Iterate through all Websets, handling pagination automatically. */
|
|
1583
|
+
async *listAll(options) {
|
|
1584
|
+
let cursor = void 0;
|
|
1585
|
+
const pageOptions = options ? { ...options } : {};
|
|
1586
|
+
while (true) {
|
|
1587
|
+
const response = await this.list({ ...pageOptions, cursor });
|
|
1588
|
+
for (const webset of response.data) {
|
|
1589
|
+
yield webset;
|
|
1590
|
+
}
|
|
1591
|
+
if (!response.hasMore || !response.nextCursor) {
|
|
1592
|
+
break;
|
|
1593
|
+
}
|
|
1594
|
+
cursor = response.nextCursor;
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
/** Collect all Websets into an array. */
|
|
1598
|
+
async getAll(options) {
|
|
1599
|
+
const websets = [];
|
|
1600
|
+
for await (const webset of this.listAll(options)) {
|
|
1601
|
+
websets.push(webset);
|
|
1602
|
+
}
|
|
1603
|
+
return websets;
|
|
1598
1604
|
}
|
|
1599
1605
|
/** Poll until a Webset reaches idle status. */
|
|
1600
1606
|
async waitUntilIdle(id, options) {
|
|
@@ -1614,9 +1620,7 @@ var WebsetsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1614
1620
|
if (onPoll) onPoll(webset.status);
|
|
1615
1621
|
if (webset.status === "idle") return webset;
|
|
1616
1622
|
if (timeout && Date.now() - startTime > timeout) {
|
|
1617
|
-
throw new Error(
|
|
1618
|
-
`Webset ${id} did not reach idle state within ${timeout}ms. Current status: ${webset.status}`
|
|
1619
|
-
);
|
|
1623
|
+
throw new Error(`Webset ${id} did not reach idle state within ${timeout}ms. Current status: ${webset.status}`);
|
|
1620
1624
|
}
|
|
1621
1625
|
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
1622
1626
|
}
|
|
@@ -1635,12 +1639,7 @@ var ResearchBaseClient2 = class {
|
|
|
1635
1639
|
}
|
|
1636
1640
|
/** Make a request to the API, prepending `/research/v1` to the path. */
|
|
1637
1641
|
async request(endpoint, method = "POST", data, params) {
|
|
1638
|
-
return this.client.request(
|
|
1639
|
-
`/research/v1${endpoint}`,
|
|
1640
|
-
method,
|
|
1641
|
-
data,
|
|
1642
|
-
params
|
|
1643
|
-
);
|
|
1642
|
+
return this.client.request(`/research/v1${endpoint}`, method, data, params);
|
|
1644
1643
|
}
|
|
1645
1644
|
buildPaginationParams(pagination) {
|
|
1646
1645
|
const params = {};
|
|
@@ -1674,14 +1673,8 @@ var ResearchClient2 = class extends ResearchBaseClient2 {
|
|
|
1674
1673
|
/** Get a research request by ID. */
|
|
1675
1674
|
async get(researchId, options) {
|
|
1676
1675
|
const params = {};
|
|
1677
|
-
if (options?.events !== void 0)
|
|
1678
|
-
|
|
1679
|
-
return this.client.request(
|
|
1680
|
-
`/research/v1/${researchId}`,
|
|
1681
|
-
"GET",
|
|
1682
|
-
void 0,
|
|
1683
|
-
params
|
|
1684
|
-
);
|
|
1676
|
+
if (options?.events !== void 0) params.events = options.events.toString();
|
|
1677
|
+
return this.client.request(`/research/v1/${researchId}`, "GET", void 0, params);
|
|
1685
1678
|
}
|
|
1686
1679
|
/** Poll a research request until it reaches a terminal state (completed, failed, or canceled). */
|
|
1687
1680
|
async pollUntilFinished(researchId, options) {
|
|
@@ -1692,9 +1685,7 @@ var ResearchClient2 = class extends ResearchBaseClient2 {
|
|
|
1692
1685
|
let consecutiveFailures = 0;
|
|
1693
1686
|
while (true) {
|
|
1694
1687
|
try {
|
|
1695
|
-
const research = await this.get(researchId, {
|
|
1696
|
-
events: options?.events
|
|
1697
|
-
});
|
|
1688
|
+
const research = await this.get(researchId, { events: options?.events });
|
|
1698
1689
|
consecutiveFailures = 0;
|
|
1699
1690
|
if (research.status === "completed" || research.status === "failed" || research.status === "canceled") {
|
|
1700
1691
|
return research;
|
|
@@ -1702,15 +1693,11 @@ var ResearchClient2 = class extends ResearchBaseClient2 {
|
|
|
1702
1693
|
} catch (err) {
|
|
1703
1694
|
consecutiveFailures += 1;
|
|
1704
1695
|
if (consecutiveFailures >= maxConsecutiveFailures) {
|
|
1705
|
-
throw new Error(
|
|
1706
|
-
`Polling failed ${maxConsecutiveFailures} times in a row for research ${researchId}: ${err}`
|
|
1707
|
-
);
|
|
1696
|
+
throw new Error(`Polling failed ${maxConsecutiveFailures} times in a row for research ${researchId}: ${err}`);
|
|
1708
1697
|
}
|
|
1709
1698
|
}
|
|
1710
1699
|
if (Date.now() - startTime > timeoutMs) {
|
|
1711
|
-
throw new Error(
|
|
1712
|
-
`Polling timeout: Research ${researchId} did not complete within ${timeoutMs}ms`
|
|
1713
|
-
);
|
|
1700
|
+
throw new Error(`Polling timeout: Research ${researchId} did not complete within ${timeoutMs}ms`);
|
|
1714
1701
|
}
|
|
1715
1702
|
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
1716
1703
|
}
|
|
@@ -1718,19 +1705,20 @@ var ResearchClient2 = class extends ResearchBaseClient2 {
|
|
|
1718
1705
|
};
|
|
1719
1706
|
|
|
1720
1707
|
// src/nova/client.ts
|
|
1721
|
-
|
|
1722
|
-
"
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1708
|
+
function isRecord(value) {
|
|
1709
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
1710
|
+
}
|
|
1711
|
+
var ExaError2 = class extends Error {
|
|
1712
|
+
constructor(message, statusCode, timestamp, path, details) {
|
|
1713
|
+
super(message);
|
|
1714
|
+
this.name = "ExaError";
|
|
1715
|
+
this.statusCode = statusCode;
|
|
1716
|
+
this.timestamp = timestamp;
|
|
1717
|
+
this.path = path;
|
|
1718
|
+
this.details = details;
|
|
1719
|
+
}
|
|
1720
|
+
};
|
|
1721
|
+
var CONTENTS_FIELDS = /* @__PURE__ */ new Set(["text", "highlights", "summary", "subpages", "subpageTarget", "extras", "livecrawl", "livecrawlTimeout", "maxAgeHours", "filterEmptyResults", "context"]);
|
|
1734
1722
|
var DEFAULT_MAX_CHARACTERS = 1e4;
|
|
1735
1723
|
var Exa2 = class {
|
|
1736
1724
|
constructor(apiKey, baseURL = "https://api.exa.ai") {
|
|
@@ -1738,8 +1726,9 @@ var Exa2 = class {
|
|
|
1738
1726
|
if (!apiKey) {
|
|
1739
1727
|
apiKey = typeof process !== "undefined" ? process.env?.EXA_API_KEY : void 0;
|
|
1740
1728
|
if (!apiKey) {
|
|
1741
|
-
throw new
|
|
1742
|
-
"API key must be provided as an argument or as an environment variable (EXA_API_KEY)"
|
|
1729
|
+
throw new ExaError2(
|
|
1730
|
+
"API key must be provided as an argument or as an environment variable (EXA_API_KEY)",
|
|
1731
|
+
401
|
|
1743
1732
|
);
|
|
1744
1733
|
}
|
|
1745
1734
|
}
|
|
@@ -1752,21 +1741,8 @@ var Exa2 = class {
|
|
|
1752
1741
|
}
|
|
1753
1742
|
/** Make a request to the Exa API. Used by sub-clients and available for direct use. */
|
|
1754
1743
|
async request(endpoint, method, body, params, headers) {
|
|
1755
|
-
|
|
1744
|
+
const url = this.buildRequestUrl(endpoint, params);
|
|
1756
1745
|
const normalizedBody = this.normalizeCategoryInBody(body);
|
|
1757
|
-
if (params && Object.keys(params).length > 0) {
|
|
1758
|
-
const searchParams = new URLSearchParams();
|
|
1759
|
-
for (const [key, value] of Object.entries(params)) {
|
|
1760
|
-
if (Array.isArray(value)) {
|
|
1761
|
-
for (const item of value) {
|
|
1762
|
-
searchParams.append(key, item);
|
|
1763
|
-
}
|
|
1764
|
-
} else if (value !== void 0) {
|
|
1765
|
-
searchParams.append(key, String(value));
|
|
1766
|
-
}
|
|
1767
|
-
}
|
|
1768
|
-
url += `?${searchParams.toString()}`;
|
|
1769
|
-
}
|
|
1770
1746
|
const combinedHeaders = headers ? { ...this.headers, ...headers } : this.headers;
|
|
1771
1747
|
const response = await fetch(url, {
|
|
1772
1748
|
method,
|
|
@@ -1774,28 +1750,14 @@ var Exa2 = class {
|
|
|
1774
1750
|
body: normalizedBody ? JSON.stringify(normalizedBody) : void 0
|
|
1775
1751
|
});
|
|
1776
1752
|
if (!response.ok) {
|
|
1777
|
-
|
|
1778
|
-
throw new Error(`Exa API error ${response.status}: ${text}`);
|
|
1753
|
+
throw await this.toExaError(response, endpoint);
|
|
1779
1754
|
}
|
|
1780
1755
|
return await response.json();
|
|
1781
1756
|
}
|
|
1782
1757
|
/** Make a raw request returning the Response object directly. */
|
|
1783
1758
|
async rawRequest(endpoint, method = "POST", body, params) {
|
|
1784
|
-
|
|
1759
|
+
const url = this.buildRequestUrl(endpoint, params);
|
|
1785
1760
|
const normalizedBody = this.normalizeCategoryInBody(body);
|
|
1786
|
-
if (params && Object.keys(params).length > 0) {
|
|
1787
|
-
const searchParams = new URLSearchParams();
|
|
1788
|
-
for (const [key, value] of Object.entries(params)) {
|
|
1789
|
-
if (Array.isArray(value)) {
|
|
1790
|
-
for (const item of value) {
|
|
1791
|
-
searchParams.append(key, item);
|
|
1792
|
-
}
|
|
1793
|
-
} else if (value !== void 0) {
|
|
1794
|
-
searchParams.append(key, String(value));
|
|
1795
|
-
}
|
|
1796
|
-
}
|
|
1797
|
-
url += `?${searchParams.toString()}`;
|
|
1798
|
-
}
|
|
1799
1761
|
return fetch(url, {
|
|
1800
1762
|
method,
|
|
1801
1763
|
headers: this.headers,
|
|
@@ -1809,6 +1771,116 @@ var Exa2 = class {
|
|
|
1809
1771
|
}
|
|
1810
1772
|
return { ...body, category: body.category.replace(/ /g, "_") };
|
|
1811
1773
|
}
|
|
1774
|
+
buildRequestUrl(endpoint, params) {
|
|
1775
|
+
let url = `${this.baseURL}${endpoint}`;
|
|
1776
|
+
if (params == null || Object.keys(params).length === 0) {
|
|
1777
|
+
return url;
|
|
1778
|
+
}
|
|
1779
|
+
const searchParams = new URLSearchParams();
|
|
1780
|
+
for (const [key, value] of Object.entries(params)) {
|
|
1781
|
+
if (Array.isArray(value)) {
|
|
1782
|
+
for (const item of value) {
|
|
1783
|
+
searchParams.append(key, item);
|
|
1784
|
+
}
|
|
1785
|
+
} else if (value !== void 0) {
|
|
1786
|
+
searchParams.append(key, String(value));
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
url += `?${searchParams.toString()}`;
|
|
1790
|
+
return url;
|
|
1791
|
+
}
|
|
1792
|
+
tryParseJsonRecord(text) {
|
|
1793
|
+
if (text.length === 0) {
|
|
1794
|
+
return null;
|
|
1795
|
+
}
|
|
1796
|
+
try {
|
|
1797
|
+
const parsed = JSON.parse(text);
|
|
1798
|
+
return isRecord(parsed) ? parsed : null;
|
|
1799
|
+
} catch {
|
|
1800
|
+
return null;
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
async toExaError(response, endpoint) {
|
|
1804
|
+
const text = await response.text();
|
|
1805
|
+
const parsed = this.tryParseJsonRecord(text);
|
|
1806
|
+
const statusCode = parsed != null && typeof parsed.statusCode === "number" ? parsed.statusCode : response.status;
|
|
1807
|
+
const timestamp = parsed != null && typeof parsed.timestamp === "string" ? parsed.timestamp : void 0;
|
|
1808
|
+
const path = parsed != null && typeof parsed.path === "string" ? parsed.path : endpoint;
|
|
1809
|
+
const errorMessage = parsed != null && typeof parsed.error === "string" ? parsed.error : void 0;
|
|
1810
|
+
const detailMessage = parsed != null && typeof parsed.message === "string" ? parsed.message : void 0;
|
|
1811
|
+
let message;
|
|
1812
|
+
if (errorMessage != null && detailMessage != null) {
|
|
1813
|
+
message = `${errorMessage}. ${detailMessage}`;
|
|
1814
|
+
} else if (errorMessage != null) {
|
|
1815
|
+
message = errorMessage;
|
|
1816
|
+
} else if (detailMessage != null) {
|
|
1817
|
+
message = detailMessage;
|
|
1818
|
+
} else if (text.length > 0) {
|
|
1819
|
+
message = text;
|
|
1820
|
+
} else {
|
|
1821
|
+
message = `Exa API error ${response.status}`;
|
|
1822
|
+
}
|
|
1823
|
+
return new ExaError2(message, statusCode, timestamp, path, parsed ?? text);
|
|
1824
|
+
}
|
|
1825
|
+
parseAnswerStreamLine(line) {
|
|
1826
|
+
if (!line.startsWith("data:")) {
|
|
1827
|
+
return null;
|
|
1828
|
+
}
|
|
1829
|
+
const jsonStr = line.replace(/^data:\s*/, "").trim();
|
|
1830
|
+
if (jsonStr.length === 0 || jsonStr === "[DONE]") {
|
|
1831
|
+
return null;
|
|
1832
|
+
}
|
|
1833
|
+
let parsedChunk;
|
|
1834
|
+
try {
|
|
1835
|
+
parsedChunk = JSON.parse(jsonStr);
|
|
1836
|
+
} catch {
|
|
1837
|
+
return null;
|
|
1838
|
+
}
|
|
1839
|
+
if (!isRecord(parsedChunk)) {
|
|
1840
|
+
return null;
|
|
1841
|
+
}
|
|
1842
|
+
let content;
|
|
1843
|
+
const choices = parsedChunk.choices;
|
|
1844
|
+
if (Array.isArray(choices) && choices.length > 0 && isRecord(choices[0])) {
|
|
1845
|
+
const delta = choices[0].delta;
|
|
1846
|
+
if (isRecord(delta) && typeof delta.content === "string") {
|
|
1847
|
+
content = delta.content;
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
let citations;
|
|
1851
|
+
if (Array.isArray(parsedChunk.citations)) {
|
|
1852
|
+
const normalized = [];
|
|
1853
|
+
for (const citation of parsedChunk.citations) {
|
|
1854
|
+
if (!isRecord(citation)) {
|
|
1855
|
+
continue;
|
|
1856
|
+
}
|
|
1857
|
+
const id = typeof citation.id === "string" ? citation.id : void 0;
|
|
1858
|
+
const url = typeof citation.url === "string" ? citation.url : void 0;
|
|
1859
|
+
const title = typeof citation.title === "string" ? citation.title : void 0;
|
|
1860
|
+
const publishedDate = typeof citation.publishedDate === "string" ? citation.publishedDate : void 0;
|
|
1861
|
+
const author = typeof citation.author === "string" ? citation.author : void 0;
|
|
1862
|
+
const text = typeof citation.text === "string" ? citation.text : void 0;
|
|
1863
|
+
normalized.push({
|
|
1864
|
+
...id != null ? { id } : {},
|
|
1865
|
+
...url != null ? { url } : {},
|
|
1866
|
+
...title != null ? { title } : {},
|
|
1867
|
+
...publishedDate != null ? { publishedDate } : {},
|
|
1868
|
+
...author != null ? { author } : {},
|
|
1869
|
+
...text != null ? { text } : {}
|
|
1870
|
+
});
|
|
1871
|
+
}
|
|
1872
|
+
if (normalized.length > 0) {
|
|
1873
|
+
citations = normalized;
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
if (content == null && (citations == null || citations.length === 0)) {
|
|
1877
|
+
return null;
|
|
1878
|
+
}
|
|
1879
|
+
return {
|
|
1880
|
+
...content != null ? { content } : {},
|
|
1881
|
+
...citations != null ? { citations } : {}
|
|
1882
|
+
};
|
|
1883
|
+
}
|
|
1812
1884
|
/** Separate content-specific options from search/filter options. */
|
|
1813
1885
|
extractContentsOptions(options) {
|
|
1814
1886
|
const contentsOptions = {};
|
|
@@ -1830,11 +1902,7 @@ var Exa2 = class {
|
|
|
1830
1902
|
search(query, options) {
|
|
1831
1903
|
let body;
|
|
1832
1904
|
if (options === void 0 || !("contents" in options)) {
|
|
1833
|
-
body = {
|
|
1834
|
-
query,
|
|
1835
|
-
...options,
|
|
1836
|
-
contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } }
|
|
1837
|
-
};
|
|
1905
|
+
body = { query, ...options, contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } } };
|
|
1838
1906
|
} else if (options.contents === false || options.contents === null || options.contents === void 0) {
|
|
1839
1907
|
const { contents, ...restOptions } = options;
|
|
1840
1908
|
body = { query, ...restOptions };
|
|
@@ -1846,11 +1914,7 @@ var Exa2 = class {
|
|
|
1846
1914
|
findSimilar(url, options) {
|
|
1847
1915
|
let body;
|
|
1848
1916
|
if (options === void 0 || !("contents" in options)) {
|
|
1849
|
-
body = {
|
|
1850
|
-
url,
|
|
1851
|
-
...options,
|
|
1852
|
-
contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } }
|
|
1853
|
-
};
|
|
1917
|
+
body = { url, ...options, contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } } };
|
|
1854
1918
|
} else if (options.contents === false || options.contents === null || options.contents === void 0) {
|
|
1855
1919
|
const { contents, ...restOptions } = options;
|
|
1856
1920
|
body = { url, ...restOptions };
|
|
@@ -1863,14 +1927,43 @@ var Exa2 = class {
|
|
|
1863
1927
|
return this.request("/contents", "POST", { urls, ...options });
|
|
1864
1928
|
}
|
|
1865
1929
|
answer(query, options) {
|
|
1866
|
-
|
|
1930
|
+
if (options?.stream) {
|
|
1931
|
+
throw new ExaError2("For streaming responses, please use streamAnswer() instead:\n\nfor await (const chunk of exa.streamAnswer(query)) {\n // Handle chunks\n}", 400);
|
|
1932
|
+
}
|
|
1933
|
+
return this.request("/answer", "POST", { query, model: "exa", stream: false, text: false, ...options });
|
|
1867
1934
|
}
|
|
1868
|
-
streamAnswer(query, options) {
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1935
|
+
async *streamAnswer(query, options) {
|
|
1936
|
+
const response = await this.rawRequest("/answer", "POST", { query, model: "exa", text: false, ...options, stream: true });
|
|
1937
|
+
if (!response.ok) {
|
|
1938
|
+
throw await this.toExaError(response, "/answer");
|
|
1939
|
+
}
|
|
1940
|
+
const reader = response.body?.getReader();
|
|
1941
|
+
if (!reader) {
|
|
1942
|
+
throw new ExaError2("No response body available for streaming.", 500, (/* @__PURE__ */ new Date()).toISOString(), "/answer");
|
|
1943
|
+
}
|
|
1944
|
+
const decoder = new TextDecoder();
|
|
1945
|
+
let buffer = "";
|
|
1946
|
+
try {
|
|
1947
|
+
while (true) {
|
|
1948
|
+
const { done, value } = await reader.read();
|
|
1949
|
+
if (done) break;
|
|
1950
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1951
|
+
const lines = buffer.split("\n");
|
|
1952
|
+
buffer = lines.pop() ?? "";
|
|
1953
|
+
for (const line of lines) {
|
|
1954
|
+
const chunk = this.parseAnswerStreamLine(line);
|
|
1955
|
+
if (chunk != null) {
|
|
1956
|
+
yield chunk;
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
const trailingChunk = this.parseAnswerStreamLine(buffer);
|
|
1961
|
+
if (trailingChunk != null) {
|
|
1962
|
+
yield trailingChunk;
|
|
1963
|
+
}
|
|
1964
|
+
} finally {
|
|
1965
|
+
reader.releaseLock();
|
|
1966
|
+
}
|
|
1874
1967
|
}
|
|
1875
1968
|
/**
|
|
1876
1969
|
* @deprecated Use `search()` instead. The search method now returns text contents by default.
|
|
@@ -1884,11 +1977,7 @@ var Exa2 = class {
|
|
|
1884
1977
|
});
|
|
1885
1978
|
}
|
|
1886
1979
|
const { contentsOptions, restOptions } = this.extractContentsOptions(options);
|
|
1887
|
-
return this.request("/search", "POST", {
|
|
1888
|
-
query,
|
|
1889
|
-
contents: contentsOptions,
|
|
1890
|
-
...restOptions
|
|
1891
|
-
});
|
|
1980
|
+
return this.request("/search", "POST", { query, contents: contentsOptions, ...restOptions });
|
|
1892
1981
|
}
|
|
1893
1982
|
/**
|
|
1894
1983
|
* @deprecated Use `findSimilar()` instead. The findSimilar method now returns text contents by default.
|
|
@@ -1902,11 +1991,7 @@ var Exa2 = class {
|
|
|
1902
1991
|
});
|
|
1903
1992
|
}
|
|
1904
1993
|
const { contentsOptions, restOptions } = this.extractContentsOptions(options);
|
|
1905
|
-
return this.request("/findSimilar", "POST", {
|
|
1906
|
-
url,
|
|
1907
|
-
contents: contentsOptions,
|
|
1908
|
-
...restOptions
|
|
1909
|
-
});
|
|
1994
|
+
return this.request("/findSimilar", "POST", { url, contents: contentsOptions, ...restOptions });
|
|
1910
1995
|
}
|
|
1911
1996
|
};
|
|
1912
1997
|
|