exa-js 1.9.0 → 1.9.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.d.mts +2444 -3579
- package/dist/index.d.ts +2444 -3579
- package/dist/index.js +293 -215
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +290 -209
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,89 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import fetch2, { Headers } from "cross-fetch";
|
|
3
3
|
|
|
4
|
+
// package.json
|
|
5
|
+
var package_default = {
|
|
6
|
+
name: "exa-js",
|
|
7
|
+
version: "1.9.2",
|
|
8
|
+
description: "Exa SDK for Node.js and the browser",
|
|
9
|
+
publishConfig: {
|
|
10
|
+
access: "public"
|
|
11
|
+
},
|
|
12
|
+
files: [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
main: "./dist/index.js",
|
|
16
|
+
module: "./dist/index.mjs",
|
|
17
|
+
exports: {
|
|
18
|
+
".": {
|
|
19
|
+
types: "./dist/index.d.ts",
|
|
20
|
+
require: "./dist/index.js",
|
|
21
|
+
module: "./dist/index.mjs",
|
|
22
|
+
import: "./dist/index.mjs"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
types: "./dist/index.d.ts",
|
|
27
|
+
scripts: {
|
|
28
|
+
"build-fast": "tsup src/index.ts --format cjs,esm",
|
|
29
|
+
build: "tsup",
|
|
30
|
+
test: "vitest run",
|
|
31
|
+
typecheck: "tsc --noEmit",
|
|
32
|
+
"typecheck:src": "tsc --noEmit src/**/*.ts",
|
|
33
|
+
"typecheck:examples": "tsc --noEmit examples/**/*.ts",
|
|
34
|
+
"generate:types:websets": "openapi-typescript https://raw.githubusercontent.com/exa-labs/openapi-spec/refs/heads/master/exa-websets-spec.yaml --enum --root-types --alphabetize --root-types-no-schema-prefix --output ./src/websets/openapi.ts && npm run format:websets",
|
|
35
|
+
format: 'prettier --write "src/**/*.ts" "examples/**/*.ts"',
|
|
36
|
+
"format:websets": "prettier --write src/websets/openapi.ts",
|
|
37
|
+
"build:beta": "cross-env NPM_CONFIG_TAG=beta npm run build",
|
|
38
|
+
"version:beta": "npm version prerelease --preid=beta",
|
|
39
|
+
"version:stable": "npm version patch",
|
|
40
|
+
"publish:beta": "npm run version:beta && npm run build:beta && npm publish --tag beta",
|
|
41
|
+
"publish:stable": "npm run version:stable && npm run build && npm publish",
|
|
42
|
+
prepublishOnly: "npm run build"
|
|
43
|
+
},
|
|
44
|
+
license: "MIT",
|
|
45
|
+
devDependencies: {
|
|
46
|
+
"@types/node": "~22.14.0",
|
|
47
|
+
"cross-env": "~7.0.3",
|
|
48
|
+
"openapi-typescript": "~7.6.1",
|
|
49
|
+
prettier: "~3.5.3",
|
|
50
|
+
"ts-node": "~10.9.2",
|
|
51
|
+
tsup: "~8.4.0",
|
|
52
|
+
typescript: "~5.8.3",
|
|
53
|
+
vitest: "~3.1.1"
|
|
54
|
+
},
|
|
55
|
+
dependencies: {
|
|
56
|
+
"cross-fetch": "~4.1.0",
|
|
57
|
+
dotenv: "~16.4.7",
|
|
58
|
+
openai: "^5.0.1",
|
|
59
|
+
zod: "^3.22.0",
|
|
60
|
+
"zod-to-json-schema": "^3.20.0"
|
|
61
|
+
},
|
|
62
|
+
directories: {
|
|
63
|
+
test: "test"
|
|
64
|
+
},
|
|
65
|
+
repository: {
|
|
66
|
+
type: "git",
|
|
67
|
+
url: "git+https://github.com/exa-labs/exa-js.git"
|
|
68
|
+
},
|
|
69
|
+
keywords: [
|
|
70
|
+
"exa",
|
|
71
|
+
"metaphor",
|
|
72
|
+
"search",
|
|
73
|
+
"AI",
|
|
74
|
+
"LLMs",
|
|
75
|
+
"RAG",
|
|
76
|
+
"retrieval",
|
|
77
|
+
"augmented",
|
|
78
|
+
"generation"
|
|
79
|
+
],
|
|
80
|
+
author: "jeffzwang",
|
|
81
|
+
bugs: {
|
|
82
|
+
url: "https://github.com/exa-labs/exa-js/issues"
|
|
83
|
+
},
|
|
84
|
+
homepage: "https://github.com/exa-labs/exa-js#readme"
|
|
85
|
+
};
|
|
86
|
+
|
|
4
87
|
// src/errors.ts
|
|
5
88
|
var HttpStatusCode = /* @__PURE__ */ ((HttpStatusCode2) => {
|
|
6
89
|
HttpStatusCode2[HttpStatusCode2["BadRequest"] = 400] = "BadRequest";
|
|
@@ -30,6 +113,173 @@ var ExaError = class extends Error {
|
|
|
30
113
|
}
|
|
31
114
|
};
|
|
32
115
|
|
|
116
|
+
// src/zod-utils.ts
|
|
117
|
+
import { ZodType } from "zod";
|
|
118
|
+
import { zodToJsonSchema as convertZodToJsonSchema } from "zod-to-json-schema";
|
|
119
|
+
function isZodSchema(obj) {
|
|
120
|
+
return obj instanceof ZodType;
|
|
121
|
+
}
|
|
122
|
+
function zodToJsonSchema(schema) {
|
|
123
|
+
return convertZodToJsonSchema(schema, {
|
|
124
|
+
$refStrategy: "none"
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/research/base.ts
|
|
129
|
+
var ResearchBaseClient = class {
|
|
130
|
+
constructor(client) {
|
|
131
|
+
this.client = client;
|
|
132
|
+
}
|
|
133
|
+
async request(endpoint, method = "POST", data, params) {
|
|
134
|
+
return this.client.request(
|
|
135
|
+
`/research/v1${endpoint}`,
|
|
136
|
+
method,
|
|
137
|
+
data,
|
|
138
|
+
params
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
async rawRequest(endpoint, method = "POST", data, params) {
|
|
142
|
+
return this.client.rawRequest(
|
|
143
|
+
`/research/v1${endpoint}`,
|
|
144
|
+
method,
|
|
145
|
+
data,
|
|
146
|
+
params
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
buildPaginationParams(pagination) {
|
|
150
|
+
const params = {};
|
|
151
|
+
if (!pagination) return params;
|
|
152
|
+
if (pagination.cursor) params.cursor = pagination.cursor;
|
|
153
|
+
if (pagination.limit) params.limit = pagination.limit;
|
|
154
|
+
return params;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// src/research/client.ts
|
|
159
|
+
var ResearchClient = class extends ResearchBaseClient {
|
|
160
|
+
constructor(client) {
|
|
161
|
+
super(client);
|
|
162
|
+
}
|
|
163
|
+
async create(params) {
|
|
164
|
+
const { instructions, model, outputSchema } = params;
|
|
165
|
+
let schema = outputSchema;
|
|
166
|
+
if (schema && isZodSchema(schema)) {
|
|
167
|
+
schema = zodToJsonSchema(schema);
|
|
168
|
+
}
|
|
169
|
+
const payload = {
|
|
170
|
+
instructions,
|
|
171
|
+
model: model ?? "exa-research"
|
|
172
|
+
};
|
|
173
|
+
if (schema) {
|
|
174
|
+
payload.outputSchema = schema;
|
|
175
|
+
}
|
|
176
|
+
return this.request("", "POST", payload);
|
|
177
|
+
}
|
|
178
|
+
get(researchId, options) {
|
|
179
|
+
if (options?.stream) {
|
|
180
|
+
const promise = async () => {
|
|
181
|
+
const params = { stream: "true" };
|
|
182
|
+
if (options.events !== void 0) {
|
|
183
|
+
params.events = options.events.toString();
|
|
184
|
+
}
|
|
185
|
+
const resp = await this.rawRequest(
|
|
186
|
+
`/${researchId}`,
|
|
187
|
+
"GET",
|
|
188
|
+
void 0,
|
|
189
|
+
params
|
|
190
|
+
);
|
|
191
|
+
if (!resp.body) {
|
|
192
|
+
throw new Error("No response body for SSE stream");
|
|
193
|
+
}
|
|
194
|
+
const reader = resp.body.getReader();
|
|
195
|
+
const decoder = new TextDecoder();
|
|
196
|
+
let buffer = "";
|
|
197
|
+
function processPart(part) {
|
|
198
|
+
const lines = part.split("\n");
|
|
199
|
+
let data = lines.slice(1).join("\n");
|
|
200
|
+
if (data.startsWith("data:")) {
|
|
201
|
+
data = data.slice(5).trimStart();
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
return JSON.parse(data);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async function* streamEvents() {
|
|
210
|
+
while (true) {
|
|
211
|
+
const { done, value } = await reader.read();
|
|
212
|
+
if (done) break;
|
|
213
|
+
buffer += decoder.decode(value, { stream: true });
|
|
214
|
+
let parts = buffer.split("\n\n");
|
|
215
|
+
buffer = parts.pop() ?? "";
|
|
216
|
+
for (const part of parts) {
|
|
217
|
+
const processed = processPart(part);
|
|
218
|
+
if (processed) {
|
|
219
|
+
yield processed;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (buffer.trim()) {
|
|
224
|
+
const processed = processPart(buffer.trim());
|
|
225
|
+
if (processed) {
|
|
226
|
+
yield processed;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return streamEvents();
|
|
231
|
+
};
|
|
232
|
+
return promise();
|
|
233
|
+
} else {
|
|
234
|
+
const params = { stream: "false" };
|
|
235
|
+
if (options?.events !== void 0) {
|
|
236
|
+
params.events = options.events.toString();
|
|
237
|
+
}
|
|
238
|
+
return this.request(
|
|
239
|
+
`/${researchId}`,
|
|
240
|
+
"GET",
|
|
241
|
+
void 0,
|
|
242
|
+
params
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
async list(options) {
|
|
247
|
+
const params = this.buildPaginationParams(options);
|
|
248
|
+
return this.request("", "GET", void 0, params);
|
|
249
|
+
}
|
|
250
|
+
async pollUntilFinished(researchId, options) {
|
|
251
|
+
const pollInterval = options?.pollInterval ?? 1e3;
|
|
252
|
+
const timeoutMs = options?.timeoutMs ?? 10 * 60 * 1e3;
|
|
253
|
+
const maxConsecutiveFailures = 5;
|
|
254
|
+
const startTime = Date.now();
|
|
255
|
+
let consecutiveFailures = 0;
|
|
256
|
+
while (true) {
|
|
257
|
+
try {
|
|
258
|
+
const research = await this.get(researchId, {
|
|
259
|
+
events: options?.events
|
|
260
|
+
});
|
|
261
|
+
consecutiveFailures = 0;
|
|
262
|
+
if (research.status === "completed" || research.status === "failed" || research.status === "canceled") {
|
|
263
|
+
return research;
|
|
264
|
+
}
|
|
265
|
+
} catch (err) {
|
|
266
|
+
consecutiveFailures += 1;
|
|
267
|
+
if (consecutiveFailures >= maxConsecutiveFailures) {
|
|
268
|
+
throw new Error(
|
|
269
|
+
`Polling failed ${maxConsecutiveFailures} times in a row for research ${researchId}: ${err}`
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (Date.now() - startTime > timeoutMs) {
|
|
274
|
+
throw new Error(
|
|
275
|
+
`Polling timeout: Research ${researchId} did not complete within ${timeoutMs}ms`
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
33
283
|
// src/websets/base.ts
|
|
34
284
|
var WebsetsBaseClient = class {
|
|
35
285
|
/**
|
|
@@ -110,6 +360,20 @@ var WebsetEnrichmentsClient = class extends WebsetsBaseClient {
|
|
|
110
360
|
"DELETE"
|
|
111
361
|
);
|
|
112
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* Update an Enrichment
|
|
365
|
+
* @param websetId The ID of the Webset
|
|
366
|
+
* @param id The ID of the Enrichment
|
|
367
|
+
* @param params The enrichment update parameters
|
|
368
|
+
* @returns Promise that resolves when the update is complete
|
|
369
|
+
*/
|
|
370
|
+
async update(websetId, id, params) {
|
|
371
|
+
return this.request(
|
|
372
|
+
`/v0/websets/${websetId}/enrichments/${id}`,
|
|
373
|
+
"PATCH",
|
|
374
|
+
params
|
|
375
|
+
);
|
|
376
|
+
}
|
|
113
377
|
/**
|
|
114
378
|
* Cancel a running Enrichment
|
|
115
379
|
* @param websetId The ID of the Webset
|
|
@@ -169,37 +433,18 @@ var CreateEnrichmentParametersFormat = /* @__PURE__ */ ((CreateEnrichmentParamet
|
|
|
169
433
|
CreateEnrichmentParametersFormat2["options"] = "options";
|
|
170
434
|
CreateEnrichmentParametersFormat2["email"] = "email";
|
|
171
435
|
CreateEnrichmentParametersFormat2["phone"] = "phone";
|
|
436
|
+
CreateEnrichmentParametersFormat2["url"] = "url";
|
|
172
437
|
return CreateEnrichmentParametersFormat2;
|
|
173
438
|
})(CreateEnrichmentParametersFormat || {});
|
|
174
439
|
var CreateImportParametersFormat = /* @__PURE__ */ ((CreateImportParametersFormat2) => {
|
|
175
440
|
CreateImportParametersFormat2["csv"] = "csv";
|
|
176
441
|
return CreateImportParametersFormat2;
|
|
177
442
|
})(CreateImportParametersFormat || {});
|
|
178
|
-
var
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
return
|
|
182
|
-
})(
|
|
183
|
-
var CreateWebsetParametersSearchExcludeSource = /* @__PURE__ */ ((CreateWebsetParametersSearchExcludeSource2) => {
|
|
184
|
-
CreateWebsetParametersSearchExcludeSource2["import"] = "import";
|
|
185
|
-
CreateWebsetParametersSearchExcludeSource2["webset"] = "webset";
|
|
186
|
-
return CreateWebsetParametersSearchExcludeSource2;
|
|
187
|
-
})(CreateWebsetParametersSearchExcludeSource || {});
|
|
188
|
-
var ScopeSourceType = /* @__PURE__ */ ((ScopeSourceType2) => {
|
|
189
|
-
ScopeSourceType2["import"] = "import";
|
|
190
|
-
ScopeSourceType2["webset"] = "webset";
|
|
191
|
-
return ScopeSourceType2;
|
|
192
|
-
})(ScopeSourceType || {});
|
|
193
|
-
var CreateWebsetSearchParametersExcludeSource = /* @__PURE__ */ ((CreateWebsetSearchParametersExcludeSource2) => {
|
|
194
|
-
CreateWebsetSearchParametersExcludeSource2["import"] = "import";
|
|
195
|
-
CreateWebsetSearchParametersExcludeSource2["webset"] = "webset";
|
|
196
|
-
return CreateWebsetSearchParametersExcludeSource2;
|
|
197
|
-
})(CreateWebsetSearchParametersExcludeSource || {});
|
|
198
|
-
var CreateWebsetSearchParametersScopeSource = /* @__PURE__ */ ((CreateWebsetSearchParametersScopeSource2) => {
|
|
199
|
-
CreateWebsetSearchParametersScopeSource2["import"] = "import";
|
|
200
|
-
CreateWebsetSearchParametersScopeSource2["webset"] = "webset";
|
|
201
|
-
return CreateWebsetSearchParametersScopeSource2;
|
|
202
|
-
})(CreateWebsetSearchParametersScopeSource || {});
|
|
443
|
+
var WebsetImportSource = /* @__PURE__ */ ((WebsetImportSource2) => {
|
|
444
|
+
WebsetImportSource2["import"] = "import";
|
|
445
|
+
WebsetImportSource2["webset"] = "webset";
|
|
446
|
+
return WebsetImportSource2;
|
|
447
|
+
})(WebsetImportSource || {});
|
|
203
448
|
var EventType = /* @__PURE__ */ ((EventType2) => {
|
|
204
449
|
EventType2["webset_created"] = "webset.created";
|
|
205
450
|
EventType2["webset_deleted"] = "webset.deleted";
|
|
@@ -211,9 +456,13 @@ var EventType = /* @__PURE__ */ ((EventType2) => {
|
|
|
211
456
|
EventType2["webset_search_updated"] = "webset.search.updated";
|
|
212
457
|
EventType2["import_created"] = "import.created";
|
|
213
458
|
EventType2["import_completed"] = "import.completed";
|
|
214
|
-
EventType2["import_processing"] = "import.processing";
|
|
215
459
|
EventType2["webset_item_created"] = "webset.item.created";
|
|
216
460
|
EventType2["webset_item_enriched"] = "webset.item.enriched";
|
|
461
|
+
EventType2["monitor_created"] = "monitor.created";
|
|
462
|
+
EventType2["monitor_updated"] = "monitor.updated";
|
|
463
|
+
EventType2["monitor_deleted"] = "monitor.deleted";
|
|
464
|
+
EventType2["monitor_run_created"] = "monitor.run.created";
|
|
465
|
+
EventType2["monitor_run_completed"] = "monitor.run.completed";
|
|
217
466
|
EventType2["webset_export_created"] = "webset.export.created";
|
|
218
467
|
EventType2["webset_export_completed"] = "webset.export.completed";
|
|
219
468
|
return EventType2;
|
|
@@ -266,15 +515,6 @@ var MonitorRunType = /* @__PURE__ */ ((MonitorRunType2) => {
|
|
|
266
515
|
MonitorRunType2["refresh"] = "refresh";
|
|
267
516
|
return MonitorRunType2;
|
|
268
517
|
})(MonitorRunType || {});
|
|
269
|
-
var PreviewWebsetResponseEnrichmentsFormat = /* @__PURE__ */ ((PreviewWebsetResponseEnrichmentsFormat2) => {
|
|
270
|
-
PreviewWebsetResponseEnrichmentsFormat2["text"] = "text";
|
|
271
|
-
PreviewWebsetResponseEnrichmentsFormat2["date"] = "date";
|
|
272
|
-
PreviewWebsetResponseEnrichmentsFormat2["number"] = "number";
|
|
273
|
-
PreviewWebsetResponseEnrichmentsFormat2["options"] = "options";
|
|
274
|
-
PreviewWebsetResponseEnrichmentsFormat2["email"] = "email";
|
|
275
|
-
PreviewWebsetResponseEnrichmentsFormat2["phone"] = "phone";
|
|
276
|
-
return PreviewWebsetResponseEnrichmentsFormat2;
|
|
277
|
-
})(PreviewWebsetResponseEnrichmentsFormat || {});
|
|
278
518
|
var UpdateMonitorStatus = /* @__PURE__ */ ((UpdateMonitorStatus2) => {
|
|
279
519
|
UpdateMonitorStatus2["enabled"] = "enabled";
|
|
280
520
|
UpdateMonitorStatus2["disabled"] = "disabled";
|
|
@@ -305,6 +545,7 @@ var WebsetEnrichmentFormat = /* @__PURE__ */ ((WebsetEnrichmentFormat2) => {
|
|
|
305
545
|
WebsetEnrichmentFormat2["options"] = "options";
|
|
306
546
|
WebsetEnrichmentFormat2["email"] = "email";
|
|
307
547
|
WebsetEnrichmentFormat2["phone"] = "phone";
|
|
548
|
+
WebsetEnrichmentFormat2["url"] = "url";
|
|
308
549
|
return WebsetEnrichmentFormat2;
|
|
309
550
|
})(WebsetEnrichmentFormat || {});
|
|
310
551
|
var WebsetItemSource = /* @__PURE__ */ ((WebsetItemSource2) => {
|
|
@@ -318,6 +559,16 @@ var WebsetItemEvaluationSatisfied = /* @__PURE__ */ ((WebsetItemEvaluationSatisf
|
|
|
318
559
|
WebsetItemEvaluationSatisfied2["unclear"] = "unclear";
|
|
319
560
|
return WebsetItemEvaluationSatisfied2;
|
|
320
561
|
})(WebsetItemEvaluationSatisfied || {});
|
|
562
|
+
var WebsetSearchExcludeSource = /* @__PURE__ */ ((WebsetSearchExcludeSource2) => {
|
|
563
|
+
WebsetSearchExcludeSource2["import"] = "import";
|
|
564
|
+
WebsetSearchExcludeSource2["webset"] = "webset";
|
|
565
|
+
return WebsetSearchExcludeSource2;
|
|
566
|
+
})(WebsetSearchExcludeSource || {});
|
|
567
|
+
var WebsetSearchScopeSource = /* @__PURE__ */ ((WebsetSearchScopeSource2) => {
|
|
568
|
+
WebsetSearchScopeSource2["import"] = "import";
|
|
569
|
+
WebsetSearchScopeSource2["webset"] = "webset";
|
|
570
|
+
return WebsetSearchScopeSource2;
|
|
571
|
+
})(WebsetSearchScopeSource || {});
|
|
321
572
|
var WebsetSearchStatus = /* @__PURE__ */ ((WebsetSearchStatus2) => {
|
|
322
573
|
WebsetSearchStatus2["created"] = "created";
|
|
323
574
|
WebsetSearchStatus2["pending"] = "pending";
|
|
@@ -1005,173 +1256,6 @@ var WebsetsClient = class extends WebsetsBaseClient {
|
|
|
1005
1256
|
}
|
|
1006
1257
|
};
|
|
1007
1258
|
|
|
1008
|
-
// src/zod-utils.ts
|
|
1009
|
-
import { ZodType } from "zod";
|
|
1010
|
-
import { zodToJsonSchema as convertZodToJsonSchema } from "zod-to-json-schema";
|
|
1011
|
-
function isZodSchema(obj) {
|
|
1012
|
-
return obj instanceof ZodType;
|
|
1013
|
-
}
|
|
1014
|
-
function zodToJsonSchema(schema) {
|
|
1015
|
-
return convertZodToJsonSchema(schema, {
|
|
1016
|
-
$refStrategy: "none"
|
|
1017
|
-
});
|
|
1018
|
-
}
|
|
1019
|
-
|
|
1020
|
-
// src/research/base.ts
|
|
1021
|
-
var ResearchBaseClient = class {
|
|
1022
|
-
constructor(client) {
|
|
1023
|
-
this.client = client;
|
|
1024
|
-
}
|
|
1025
|
-
async request(endpoint, method = "POST", data, params) {
|
|
1026
|
-
return this.client.request(
|
|
1027
|
-
`/research/v1${endpoint}`,
|
|
1028
|
-
method,
|
|
1029
|
-
data,
|
|
1030
|
-
params
|
|
1031
|
-
);
|
|
1032
|
-
}
|
|
1033
|
-
async rawRequest(endpoint, method = "POST", data, params) {
|
|
1034
|
-
return this.client.rawRequest(
|
|
1035
|
-
`/research/v1${endpoint}`,
|
|
1036
|
-
method,
|
|
1037
|
-
data,
|
|
1038
|
-
params
|
|
1039
|
-
);
|
|
1040
|
-
}
|
|
1041
|
-
buildPaginationParams(pagination) {
|
|
1042
|
-
const params = {};
|
|
1043
|
-
if (!pagination) return params;
|
|
1044
|
-
if (pagination.cursor) params.cursor = pagination.cursor;
|
|
1045
|
-
if (pagination.limit) params.limit = pagination.limit;
|
|
1046
|
-
return params;
|
|
1047
|
-
}
|
|
1048
|
-
};
|
|
1049
|
-
|
|
1050
|
-
// src/research/client.ts
|
|
1051
|
-
var ResearchClient = class extends ResearchBaseClient {
|
|
1052
|
-
constructor(client) {
|
|
1053
|
-
super(client);
|
|
1054
|
-
}
|
|
1055
|
-
async create(params) {
|
|
1056
|
-
const { instructions, model, outputSchema } = params;
|
|
1057
|
-
let schema = outputSchema;
|
|
1058
|
-
if (schema && isZodSchema(schema)) {
|
|
1059
|
-
schema = zodToJsonSchema(schema);
|
|
1060
|
-
}
|
|
1061
|
-
const payload = {
|
|
1062
|
-
instructions,
|
|
1063
|
-
model: model ?? "exa-research"
|
|
1064
|
-
};
|
|
1065
|
-
if (schema) {
|
|
1066
|
-
payload.outputSchema = schema;
|
|
1067
|
-
}
|
|
1068
|
-
return this.request("", "POST", payload);
|
|
1069
|
-
}
|
|
1070
|
-
get(researchId, options) {
|
|
1071
|
-
if (options?.stream) {
|
|
1072
|
-
const promise = async () => {
|
|
1073
|
-
const params = { stream: "true" };
|
|
1074
|
-
if (options.events !== void 0) {
|
|
1075
|
-
params.events = options.events.toString();
|
|
1076
|
-
}
|
|
1077
|
-
const resp = await this.rawRequest(
|
|
1078
|
-
`/${researchId}`,
|
|
1079
|
-
"GET",
|
|
1080
|
-
void 0,
|
|
1081
|
-
params
|
|
1082
|
-
);
|
|
1083
|
-
if (!resp.body) {
|
|
1084
|
-
throw new Error("No response body for SSE stream");
|
|
1085
|
-
}
|
|
1086
|
-
const reader = resp.body.getReader();
|
|
1087
|
-
const decoder = new TextDecoder();
|
|
1088
|
-
let buffer = "";
|
|
1089
|
-
function processPart(part) {
|
|
1090
|
-
const lines = part.split("\n");
|
|
1091
|
-
let data = lines.slice(1).join("\n");
|
|
1092
|
-
if (data.startsWith("data:")) {
|
|
1093
|
-
data = data.slice(5).trimStart();
|
|
1094
|
-
}
|
|
1095
|
-
try {
|
|
1096
|
-
return JSON.parse(data);
|
|
1097
|
-
} catch (e) {
|
|
1098
|
-
return null;
|
|
1099
|
-
}
|
|
1100
|
-
}
|
|
1101
|
-
async function* streamEvents() {
|
|
1102
|
-
while (true) {
|
|
1103
|
-
const { done, value } = await reader.read();
|
|
1104
|
-
if (done) break;
|
|
1105
|
-
buffer += decoder.decode(value, { stream: true });
|
|
1106
|
-
let parts = buffer.split("\n\n");
|
|
1107
|
-
buffer = parts.pop() ?? "";
|
|
1108
|
-
for (const part of parts) {
|
|
1109
|
-
const processed = processPart(part);
|
|
1110
|
-
if (processed) {
|
|
1111
|
-
yield processed;
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1114
|
-
}
|
|
1115
|
-
if (buffer.trim()) {
|
|
1116
|
-
const processed = processPart(buffer.trim());
|
|
1117
|
-
if (processed) {
|
|
1118
|
-
yield processed;
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
return streamEvents();
|
|
1123
|
-
};
|
|
1124
|
-
return promise();
|
|
1125
|
-
} else {
|
|
1126
|
-
const params = { stream: "false" };
|
|
1127
|
-
if (options?.events !== void 0) {
|
|
1128
|
-
params.events = options.events.toString();
|
|
1129
|
-
}
|
|
1130
|
-
return this.request(
|
|
1131
|
-
`/${researchId}`,
|
|
1132
|
-
"GET",
|
|
1133
|
-
void 0,
|
|
1134
|
-
params
|
|
1135
|
-
);
|
|
1136
|
-
}
|
|
1137
|
-
}
|
|
1138
|
-
async list(options) {
|
|
1139
|
-
const params = this.buildPaginationParams(options);
|
|
1140
|
-
return this.request("", "GET", void 0, params);
|
|
1141
|
-
}
|
|
1142
|
-
async pollUntilFinished(researchId, options) {
|
|
1143
|
-
const pollInterval = options?.pollInterval ?? 1e3;
|
|
1144
|
-
const timeoutMs = options?.timeoutMs ?? 10 * 60 * 1e3;
|
|
1145
|
-
const maxConsecutiveFailures = 5;
|
|
1146
|
-
const startTime = Date.now();
|
|
1147
|
-
let consecutiveFailures = 0;
|
|
1148
|
-
while (true) {
|
|
1149
|
-
try {
|
|
1150
|
-
const research = await this.get(researchId, {
|
|
1151
|
-
events: options?.events
|
|
1152
|
-
});
|
|
1153
|
-
consecutiveFailures = 0;
|
|
1154
|
-
if (research.status === "completed" || research.status === "failed" || research.status === "canceled") {
|
|
1155
|
-
return research;
|
|
1156
|
-
}
|
|
1157
|
-
} catch (err) {
|
|
1158
|
-
consecutiveFailures += 1;
|
|
1159
|
-
if (consecutiveFailures >= maxConsecutiveFailures) {
|
|
1160
|
-
throw new Error(
|
|
1161
|
-
`Polling failed ${maxConsecutiveFailures} times in a row for research ${researchId}: ${err}`
|
|
1162
|
-
);
|
|
1163
|
-
}
|
|
1164
|
-
}
|
|
1165
|
-
if (Date.now() - startTime > timeoutMs) {
|
|
1166
|
-
throw new Error(
|
|
1167
|
-
`Polling timeout: Research ${researchId} did not complete within ${timeoutMs}ms`
|
|
1168
|
-
);
|
|
1169
|
-
}
|
|
1170
|
-
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
};
|
|
1174
|
-
|
|
1175
1259
|
// src/index.ts
|
|
1176
1260
|
var fetchImpl = typeof global !== "undefined" && global.fetch ? global.fetch : fetch2;
|
|
1177
1261
|
var HeadersImpl = typeof global !== "undefined" && global.Headers ? global.Headers : Headers;
|
|
@@ -1240,7 +1324,7 @@ var Exa2 = class {
|
|
|
1240
1324
|
this.headers = new HeadersImpl({
|
|
1241
1325
|
"x-api-key": apiKey,
|
|
1242
1326
|
"Content-Type": "application/json",
|
|
1243
|
-
"User-Agent":
|
|
1327
|
+
"User-Agent": `exa-node ${package_default.version}`
|
|
1244
1328
|
});
|
|
1245
1329
|
this.websets = new WebsetsClient(this);
|
|
1246
1330
|
this.research = new ResearchClient(this);
|
|
@@ -1599,10 +1683,6 @@ var index_default = Exa2;
|
|
|
1599
1683
|
export {
|
|
1600
1684
|
CreateEnrichmentParametersFormat,
|
|
1601
1685
|
CreateImportParametersFormat,
|
|
1602
|
-
CreateWebsetParametersImportSource,
|
|
1603
|
-
CreateWebsetParametersSearchExcludeSource,
|
|
1604
|
-
CreateWebsetSearchParametersExcludeSource,
|
|
1605
|
-
CreateWebsetSearchParametersScopeSource,
|
|
1606
1686
|
EventType,
|
|
1607
1687
|
EventsClient,
|
|
1608
1688
|
Exa2 as Exa,
|
|
@@ -1618,19 +1698,20 @@ export {
|
|
|
1618
1698
|
MonitorRunStatus,
|
|
1619
1699
|
MonitorRunType,
|
|
1620
1700
|
MonitorStatus,
|
|
1621
|
-
PreviewWebsetResponseEnrichmentsFormat,
|
|
1622
|
-
ScopeSourceType,
|
|
1623
1701
|
UpdateMonitorStatus,
|
|
1624
1702
|
WebhookStatus,
|
|
1625
1703
|
WebsetEnrichmentFormat,
|
|
1626
1704
|
WebsetEnrichmentStatus,
|
|
1627
1705
|
WebsetEnrichmentsClient,
|
|
1706
|
+
WebsetImportSource,
|
|
1628
1707
|
WebsetItemEvaluationSatisfied,
|
|
1629
1708
|
WebsetItemSource,
|
|
1630
1709
|
WebsetItemsClient,
|
|
1631
1710
|
WebsetMonitorsClient,
|
|
1632
1711
|
WebsetSearchBehavior,
|
|
1633
1712
|
WebsetSearchCanceledReason,
|
|
1713
|
+
WebsetSearchExcludeSource,
|
|
1714
|
+
WebsetSearchScopeSource,
|
|
1634
1715
|
WebsetSearchStatus,
|
|
1635
1716
|
WebsetSearchesClient,
|
|
1636
1717
|
WebsetStatus,
|