exa-js 2.5.1-beta.3 → 2.6.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/README.md +15 -1
- package/dist/index.d.mts +291 -8722
- package/dist/index.d.ts +291 -8722
- package/dist/index.js +69 -909
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +67 -906
- 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.
|
|
7
|
+
version: "2.6.1",
|
|
8
8
|
description: "Exa SDK for Node.js and the browser",
|
|
9
9
|
publishConfig: {
|
|
10
10
|
access: "public"
|
|
@@ -88,860 +88,6 @@ var package_default = {
|
|
|
88
88
|
homepage: "https://github.com/exa-labs/exa-js#readme"
|
|
89
89
|
};
|
|
90
90
|
|
|
91
|
-
// src/nova/websets-client.ts
|
|
92
|
-
var WebsetsBaseClient = class {
|
|
93
|
-
constructor(client) {
|
|
94
|
-
this.client = client;
|
|
95
|
-
}
|
|
96
|
-
/** Make a request to the API, prepending `/websets` to the path. */
|
|
97
|
-
async request(endpoint, method = "POST", data, params, headers) {
|
|
98
|
-
return this.client.request(`/websets${endpoint}`, method, data, params, headers);
|
|
99
|
-
}
|
|
100
|
-
buildPaginationParams(pagination) {
|
|
101
|
-
const params = {};
|
|
102
|
-
if (!pagination) return params;
|
|
103
|
-
if (pagination.cursor) params.cursor = pagination.cursor;
|
|
104
|
-
if (pagination.limit) params.limit = pagination.limit;
|
|
105
|
-
return params;
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
var WebsetItemsClient = class extends WebsetsBaseClient {
|
|
109
|
-
async get(websetId, id) {
|
|
110
|
-
return this.request(`/v0/websets/${websetId}/items/${id}`, "GET");
|
|
111
|
-
}
|
|
112
|
-
async delete(websetId, id) {
|
|
113
|
-
return this.request(`/v0/websets/${websetId}/items/${id}`, "DELETE");
|
|
114
|
-
}
|
|
115
|
-
async list(websetId, options) {
|
|
116
|
-
const queryParams = { ...this.buildPaginationParams(options), sourceId: options?.sourceId };
|
|
117
|
-
return this.request(`/v0/websets/${websetId}/items`, "GET", void 0, queryParams);
|
|
118
|
-
}
|
|
119
|
-
/** Iterate through all items in a Webset, handling pagination automatically. */
|
|
120
|
-
async *listAll(websetId, options) {
|
|
121
|
-
let cursor = void 0;
|
|
122
|
-
const pageOptions = options ? { ...options } : {};
|
|
123
|
-
while (true) {
|
|
124
|
-
const response = await this.list(websetId, { ...pageOptions, cursor });
|
|
125
|
-
for (const item of response.data) {
|
|
126
|
-
yield item;
|
|
127
|
-
}
|
|
128
|
-
if (!response.hasMore || !response.nextCursor) {
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
cursor = response.nextCursor;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
/** Collect all items from a Webset into an array. */
|
|
135
|
-
async getAll(websetId, options) {
|
|
136
|
-
const items = [];
|
|
137
|
-
for await (const item of this.listAll(websetId, options)) {
|
|
138
|
-
items.push(item);
|
|
139
|
-
}
|
|
140
|
-
return items;
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
var WebsetSearchesClient = class extends WebsetsBaseClient {
|
|
144
|
-
async create(websetId, params, options) {
|
|
145
|
-
return this.request(`/v0/websets/${websetId}/searches`, "POST", { ...params }, void 0, options?.headers);
|
|
146
|
-
}
|
|
147
|
-
async get(websetId, id) {
|
|
148
|
-
return this.request(`/v0/websets/${websetId}/searches/${id}`, "GET");
|
|
149
|
-
}
|
|
150
|
-
async cancel(websetId, id) {
|
|
151
|
-
return this.request(`/v0/websets/${websetId}/searches/${id}/cancel`, "POST");
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
var WebsetEnrichmentsClient = class extends WebsetsBaseClient {
|
|
155
|
-
async create(websetId, params) {
|
|
156
|
-
return this.request(`/v0/websets/${websetId}/enrichments`, "POST", { ...params });
|
|
157
|
-
}
|
|
158
|
-
async update(websetId, id, params) {
|
|
159
|
-
return this.request(`/v0/websets/${websetId}/enrichments/${id}`, "PATCH", { ...params });
|
|
160
|
-
}
|
|
161
|
-
async get(websetId, id) {
|
|
162
|
-
return this.request(`/v0/websets/${websetId}/enrichments/${id}`, "GET");
|
|
163
|
-
}
|
|
164
|
-
async delete(websetId, id) {
|
|
165
|
-
return this.request(`/v0/websets/${websetId}/enrichments/${id}`, "DELETE");
|
|
166
|
-
}
|
|
167
|
-
async cancel(websetId, id) {
|
|
168
|
-
return this.request(`/v0/websets/${websetId}/enrichments/${id}/cancel`, "POST");
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
var EventsClient = class extends WebsetsBaseClient {
|
|
172
|
-
async list(options) {
|
|
173
|
-
const queryParams = { ...this.buildPaginationParams(options), types: options?.types, createdBefore: options?.createdBefore, createdAfter: options?.createdAfter };
|
|
174
|
-
return this.request("/v0/events", "GET", void 0, queryParams);
|
|
175
|
-
}
|
|
176
|
-
async get(id) {
|
|
177
|
-
return this.request(`/v0/events/${id}`, "GET");
|
|
178
|
-
}
|
|
179
|
-
/** Iterate through all events, handling pagination automatically. */
|
|
180
|
-
async *listAll(options) {
|
|
181
|
-
let cursor = void 0;
|
|
182
|
-
const pageOptions = options ? { ...options } : {};
|
|
183
|
-
while (true) {
|
|
184
|
-
const response = await this.list({ ...pageOptions, cursor });
|
|
185
|
-
for (const event of response.data) {
|
|
186
|
-
yield event;
|
|
187
|
-
}
|
|
188
|
-
if (!response.hasMore || !response.nextCursor) {
|
|
189
|
-
break;
|
|
190
|
-
}
|
|
191
|
-
cursor = response.nextCursor;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
/** Collect all events into an array. */
|
|
195
|
-
async getAll(options) {
|
|
196
|
-
const events = [];
|
|
197
|
-
for await (const event of this.listAll(options)) {
|
|
198
|
-
events.push(event);
|
|
199
|
-
}
|
|
200
|
-
return events;
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
var WebsetMonitorRunsClient = class extends WebsetsBaseClient {
|
|
204
|
-
async list(monitorId) {
|
|
205
|
-
return this.request(`/v0/monitors/${monitorId}/runs`, "GET");
|
|
206
|
-
}
|
|
207
|
-
async get(monitorId, id) {
|
|
208
|
-
return this.request(`/v0/monitors/${monitorId}/runs/${id}`, "GET");
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
var WebsetMonitorsClient = class extends WebsetsBaseClient {
|
|
212
|
-
constructor(client) {
|
|
213
|
-
super(client);
|
|
214
|
-
this.runs = new WebsetMonitorRunsClient(client);
|
|
215
|
-
}
|
|
216
|
-
async create(params) {
|
|
217
|
-
return this.request("/v0/monitors", "POST", { ...params });
|
|
218
|
-
}
|
|
219
|
-
async list(options) {
|
|
220
|
-
const queryParams = { ...this.buildPaginationParams(options), websetId: options?.websetId };
|
|
221
|
-
return this.request("/v0/monitors", "GET", void 0, queryParams);
|
|
222
|
-
}
|
|
223
|
-
async get(id) {
|
|
224
|
-
return this.request(`/v0/monitors/${id}`, "GET");
|
|
225
|
-
}
|
|
226
|
-
async update(id, params) {
|
|
227
|
-
return this.request(`/v0/monitors/${id}`, "PATCH", { ...params });
|
|
228
|
-
}
|
|
229
|
-
async delete(id) {
|
|
230
|
-
return this.request(`/v0/monitors/${id}`, "DELETE");
|
|
231
|
-
}
|
|
232
|
-
/** Iterate through all monitors, handling pagination automatically. */
|
|
233
|
-
async *listAll(options) {
|
|
234
|
-
let cursor = void 0;
|
|
235
|
-
const pageOptions = options ? { ...options } : {};
|
|
236
|
-
while (true) {
|
|
237
|
-
const response = await this.list({ ...pageOptions, cursor });
|
|
238
|
-
for (const monitor of response.data) {
|
|
239
|
-
yield monitor;
|
|
240
|
-
}
|
|
241
|
-
if (!response.hasMore || !response.nextCursor) {
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
cursor = response.nextCursor;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
/** Collect all monitors into an array. */
|
|
248
|
-
async getAll(options) {
|
|
249
|
-
const monitors = [];
|
|
250
|
-
for await (const monitor of this.listAll(options)) {
|
|
251
|
-
monitors.push(monitor);
|
|
252
|
-
}
|
|
253
|
-
return monitors;
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
var WebsetWebhooksClient = class extends WebsetsBaseClient {
|
|
257
|
-
async create(params) {
|
|
258
|
-
return this.request("/v0/webhooks", "POST", { ...params });
|
|
259
|
-
}
|
|
260
|
-
async list(options) {
|
|
261
|
-
const queryParams = this.buildPaginationParams(options);
|
|
262
|
-
return this.request("/v0/webhooks", "GET", void 0, queryParams);
|
|
263
|
-
}
|
|
264
|
-
async get(id) {
|
|
265
|
-
return this.request(`/v0/webhooks/${id}`, "GET");
|
|
266
|
-
}
|
|
267
|
-
async update(id, params) {
|
|
268
|
-
return this.request(`/v0/webhooks/${id}`, "PATCH", { ...params });
|
|
269
|
-
}
|
|
270
|
-
async delete(id) {
|
|
271
|
-
return this.request(`/v0/webhooks/${id}`, "DELETE");
|
|
272
|
-
}
|
|
273
|
-
async listAttempts(id, options) {
|
|
274
|
-
const queryParams = { ...this.buildPaginationParams(options), eventType: options?.eventType, successful: options?.successful };
|
|
275
|
-
return this.request(`/v0/webhooks/${id}/attempts`, "GET", void 0, queryParams);
|
|
276
|
-
}
|
|
277
|
-
/** Iterate through all webhooks, handling pagination automatically. */
|
|
278
|
-
async *listAll(options) {
|
|
279
|
-
let cursor = void 0;
|
|
280
|
-
const pageOptions = options ? { ...options } : {};
|
|
281
|
-
while (true) {
|
|
282
|
-
const response = await this.list({ ...pageOptions, cursor });
|
|
283
|
-
for (const webhook of response.data) {
|
|
284
|
-
yield webhook;
|
|
285
|
-
}
|
|
286
|
-
if (!response.hasMore || !response.nextCursor) {
|
|
287
|
-
break;
|
|
288
|
-
}
|
|
289
|
-
cursor = response.nextCursor;
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
/** Collect all webhooks into an array. */
|
|
293
|
-
async getAll(options) {
|
|
294
|
-
const webhooks = [];
|
|
295
|
-
for await (const webhook of this.listAll(options)) {
|
|
296
|
-
webhooks.push(webhook);
|
|
297
|
-
}
|
|
298
|
-
return webhooks;
|
|
299
|
-
}
|
|
300
|
-
/** Iterate through all webhook attempts, handling pagination automatically. */
|
|
301
|
-
async *listAllAttempts(id, options) {
|
|
302
|
-
let cursor = void 0;
|
|
303
|
-
const pageOptions = options ? { ...options } : {};
|
|
304
|
-
while (true) {
|
|
305
|
-
const response = await this.listAttempts(id, { ...pageOptions, cursor });
|
|
306
|
-
for (const attempt of response.data) {
|
|
307
|
-
yield attempt;
|
|
308
|
-
}
|
|
309
|
-
if (!response.hasMore || !response.nextCursor) {
|
|
310
|
-
break;
|
|
311
|
-
}
|
|
312
|
-
cursor = response.nextCursor;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
/** Collect all webhook attempts into an array. */
|
|
316
|
-
async getAllAttempts(id, options) {
|
|
317
|
-
const attempts = [];
|
|
318
|
-
for await (const attempt of this.listAllAttempts(id, options)) {
|
|
319
|
-
attempts.push(attempt);
|
|
320
|
-
}
|
|
321
|
-
return attempts;
|
|
322
|
-
}
|
|
323
|
-
};
|
|
324
|
-
var ImportsClient = class extends WebsetsBaseClient {
|
|
325
|
-
async create(params) {
|
|
326
|
-
return this.request("/v0/imports", "POST", { ...params });
|
|
327
|
-
}
|
|
328
|
-
async list(options) {
|
|
329
|
-
const queryParams = this.buildPaginationParams(options);
|
|
330
|
-
return this.request("/v0/imports", "GET", void 0, queryParams);
|
|
331
|
-
}
|
|
332
|
-
async get(id) {
|
|
333
|
-
return this.request(`/v0/imports/${id}`, "GET");
|
|
334
|
-
}
|
|
335
|
-
async update(id, params) {
|
|
336
|
-
return this.request(`/v0/imports/${id}`, "PATCH", { ...params });
|
|
337
|
-
}
|
|
338
|
-
async delete(id) {
|
|
339
|
-
return this.request(`/v0/imports/${id}`, "DELETE");
|
|
340
|
-
}
|
|
341
|
-
/** Wait for an import to reach a terminal status. */
|
|
342
|
-
async waitUntilCompleted(id, options) {
|
|
343
|
-
const timeout = options?.timeout ?? 30 * 60 * 1e3;
|
|
344
|
-
const pollInterval = options?.pollInterval ?? 2e3;
|
|
345
|
-
const startTime = Date.now();
|
|
346
|
-
while (true) {
|
|
347
|
-
const importItem = await this.get(id);
|
|
348
|
-
if (options?.onPoll) {
|
|
349
|
-
options.onPoll(importItem.status);
|
|
350
|
-
}
|
|
351
|
-
if (importItem.status === "completed") {
|
|
352
|
-
return importItem;
|
|
353
|
-
}
|
|
354
|
-
if (importItem.status === "failed") {
|
|
355
|
-
throw new Error(`Import ${id} failed: ${importItem.failedMessage ?? "Unknown error"}`);
|
|
356
|
-
}
|
|
357
|
-
if (Date.now() - startTime > timeout) {
|
|
358
|
-
throw new Error(`Import ${id} did not complete within ${timeout}ms. Current status: ${importItem.status}`);
|
|
359
|
-
}
|
|
360
|
-
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
/** Iterate through all imports, handling pagination automatically. */
|
|
364
|
-
async *listAll(options) {
|
|
365
|
-
let cursor = void 0;
|
|
366
|
-
const pageOptions = options ? { ...options } : {};
|
|
367
|
-
while (true) {
|
|
368
|
-
const response = await this.list({ ...pageOptions, cursor });
|
|
369
|
-
for (const importItem of response.data) {
|
|
370
|
-
yield importItem;
|
|
371
|
-
}
|
|
372
|
-
if (!response.hasMore || !response.nextCursor) {
|
|
373
|
-
break;
|
|
374
|
-
}
|
|
375
|
-
cursor = response.nextCursor;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
/** Collect all imports into an array. */
|
|
379
|
-
async getAll(options) {
|
|
380
|
-
const imports = [];
|
|
381
|
-
for await (const importItem of this.listAll(options)) {
|
|
382
|
-
imports.push(importItem);
|
|
383
|
-
}
|
|
384
|
-
return imports;
|
|
385
|
-
}
|
|
386
|
-
};
|
|
387
|
-
var WebsetsClient = class extends WebsetsBaseClient {
|
|
388
|
-
constructor(client) {
|
|
389
|
-
super(client);
|
|
390
|
-
this.items = new WebsetItemsClient(client);
|
|
391
|
-
this.searches = new WebsetSearchesClient(client);
|
|
392
|
-
this.enrichments = new WebsetEnrichmentsClient(client);
|
|
393
|
-
this.events = new EventsClient(client);
|
|
394
|
-
this.monitors = new WebsetMonitorsClient(client);
|
|
395
|
-
this.webhooks = new WebsetWebhooksClient(client);
|
|
396
|
-
this.imports = new ImportsClient(client);
|
|
397
|
-
}
|
|
398
|
-
async create(params, options) {
|
|
399
|
-
return this.request("/v0/websets", "POST", { ...params }, void 0, options?.headers);
|
|
400
|
-
}
|
|
401
|
-
async list(options) {
|
|
402
|
-
const queryParams = { ...this.buildPaginationParams(options), search: options?.search };
|
|
403
|
-
return this.request("/v0/websets", "GET", void 0, queryParams);
|
|
404
|
-
}
|
|
405
|
-
async get(id, options) {
|
|
406
|
-
const queryParams = { expand: options?.expand };
|
|
407
|
-
return this.request(`/v0/websets/${id}`, "GET", void 0, queryParams);
|
|
408
|
-
}
|
|
409
|
-
async update(id, params) {
|
|
410
|
-
return this.request(`/v0/websets/${id}`, "POST", { ...params });
|
|
411
|
-
}
|
|
412
|
-
async delete(id) {
|
|
413
|
-
return this.request(`/v0/websets/${id}`, "DELETE");
|
|
414
|
-
}
|
|
415
|
-
async cancel(id) {
|
|
416
|
-
return this.request(`/v0/websets/${id}/cancel`, "POST");
|
|
417
|
-
}
|
|
418
|
-
async preview(params, options) {
|
|
419
|
-
const queryParams = { search: options?.search };
|
|
420
|
-
return this.request("/v0/websets/preview", "POST", { ...params }, queryParams);
|
|
421
|
-
}
|
|
422
|
-
/** Iterate through all Websets, handling pagination automatically. */
|
|
423
|
-
async *listAll(options) {
|
|
424
|
-
let cursor = void 0;
|
|
425
|
-
const pageOptions = options ? { ...options } : {};
|
|
426
|
-
while (true) {
|
|
427
|
-
const response = await this.list({ ...pageOptions, cursor });
|
|
428
|
-
for (const webset of response.data) {
|
|
429
|
-
yield webset;
|
|
430
|
-
}
|
|
431
|
-
if (!response.hasMore || !response.nextCursor) {
|
|
432
|
-
break;
|
|
433
|
-
}
|
|
434
|
-
cursor = response.nextCursor;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
/** Collect all Websets into an array. */
|
|
438
|
-
async getAll(options) {
|
|
439
|
-
const websets = [];
|
|
440
|
-
for await (const webset of this.listAll(options)) {
|
|
441
|
-
websets.push(webset);
|
|
442
|
-
}
|
|
443
|
-
return websets;
|
|
444
|
-
}
|
|
445
|
-
/** Poll until a Webset reaches idle status. */
|
|
446
|
-
async waitUntilIdle(id, options) {
|
|
447
|
-
let timeout;
|
|
448
|
-
let pollInterval = 1e3;
|
|
449
|
-
let onPoll;
|
|
450
|
-
if (typeof options === "number") {
|
|
451
|
-
timeout = options;
|
|
452
|
-
} else if (options) {
|
|
453
|
-
timeout = options.timeout;
|
|
454
|
-
pollInterval = options.pollInterval || 1e3;
|
|
455
|
-
onPoll = options.onPoll;
|
|
456
|
-
}
|
|
457
|
-
const startTime = Date.now();
|
|
458
|
-
while (true) {
|
|
459
|
-
const webset = await this.get(id);
|
|
460
|
-
if (onPoll) onPoll(webset.status);
|
|
461
|
-
if (webset.status === "idle") return webset;
|
|
462
|
-
if (timeout && Date.now() - startTime > timeout) {
|
|
463
|
-
throw new Error(`Webset ${id} did not reach idle state within ${timeout}ms. Current status: ${webset.status}`);
|
|
464
|
-
}
|
|
465
|
-
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
// src/nova/research-client.ts
|
|
471
|
-
import { ZodType } from "zod";
|
|
472
|
-
import { zodToJsonSchema as zodToJsonSchemaLib } from "zod-to-json-schema";
|
|
473
|
-
function zodToJsonSchema(schema) {
|
|
474
|
-
return zodToJsonSchemaLib(schema, { $refStrategy: "none" });
|
|
475
|
-
}
|
|
476
|
-
var ResearchBaseClient = class {
|
|
477
|
-
constructor(client) {
|
|
478
|
-
this.client = client;
|
|
479
|
-
}
|
|
480
|
-
/** Make a request to the API, prepending `/research/v1` to the path. */
|
|
481
|
-
async request(endpoint, method = "POST", data, params) {
|
|
482
|
-
return this.client.request(`/research/v1${endpoint}`, method, data, params);
|
|
483
|
-
}
|
|
484
|
-
buildPaginationParams(pagination) {
|
|
485
|
-
const params = {};
|
|
486
|
-
if (!pagination) return params;
|
|
487
|
-
if (pagination.cursor) params.cursor = pagination.cursor;
|
|
488
|
-
if (pagination.limit) params.limit = pagination.limit;
|
|
489
|
-
return params;
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
var ResearchClient = class extends ResearchBaseClient {
|
|
493
|
-
/** List research requests. */
|
|
494
|
-
async list(options) {
|
|
495
|
-
const params = this.buildPaginationParams(options);
|
|
496
|
-
return this.client.request("/research/v1", "GET", void 0, params);
|
|
497
|
-
}
|
|
498
|
-
/** Create a new research request. Defaults model to "exa-research-fast" if not provided. */
|
|
499
|
-
async create(params) {
|
|
500
|
-
let schema = params.outputSchema;
|
|
501
|
-
if (schema instanceof ZodType) {
|
|
502
|
-
schema = zodToJsonSchema(schema);
|
|
503
|
-
}
|
|
504
|
-
const payload = {
|
|
505
|
-
instructions: params.instructions,
|
|
506
|
-
model: params.model ?? "exa-research-fast"
|
|
507
|
-
};
|
|
508
|
-
if (schema) {
|
|
509
|
-
payload.outputSchema = schema;
|
|
510
|
-
}
|
|
511
|
-
return this.client.request("/research/v1", "POST", payload, void 0);
|
|
512
|
-
}
|
|
513
|
-
async get(researchId, options) {
|
|
514
|
-
const params = {};
|
|
515
|
-
if (options?.events !== void 0) params.events = options.events.toString();
|
|
516
|
-
if (options?.stream) {
|
|
517
|
-
params.stream = "true";
|
|
518
|
-
const response = await this.client.rawRequest(`/research/v1/${researchId}`, "GET", void 0, params);
|
|
519
|
-
if (!response.ok) {
|
|
520
|
-
const message = await response.text();
|
|
521
|
-
throw new Error(`Research stream request failed (${response.status}): ${message}`);
|
|
522
|
-
}
|
|
523
|
-
const reader = response.body?.getReader();
|
|
524
|
-
if (!reader) {
|
|
525
|
-
throw new Error("No response body for SSE stream");
|
|
526
|
-
}
|
|
527
|
-
const streamReader = reader;
|
|
528
|
-
const decoder = new TextDecoder();
|
|
529
|
-
let buffer = "";
|
|
530
|
-
const parseSseChunk = (chunk) => {
|
|
531
|
-
const lines = chunk.split("\n");
|
|
532
|
-
const dataLines = [];
|
|
533
|
-
for (const line of lines) {
|
|
534
|
-
if (line.startsWith("data:")) {
|
|
535
|
-
dataLines.push(line.replace(/^data:\s*/, ""));
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
const payload = dataLines.join("\n").trim();
|
|
539
|
-
if (payload.length === 0 || payload === "[DONE]") {
|
|
540
|
-
return null;
|
|
541
|
-
}
|
|
542
|
-
try {
|
|
543
|
-
const parsed = JSON.parse(payload);
|
|
544
|
-
if (parsed == null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
545
|
-
return null;
|
|
546
|
-
}
|
|
547
|
-
const event = {};
|
|
548
|
-
for (const [key, value] of Object.entries(parsed)) {
|
|
549
|
-
event[key] = value;
|
|
550
|
-
}
|
|
551
|
-
return event;
|
|
552
|
-
} catch {
|
|
553
|
-
return null;
|
|
554
|
-
}
|
|
555
|
-
};
|
|
556
|
-
async function* streamEvents() {
|
|
557
|
-
while (true) {
|
|
558
|
-
const { done, value } = await streamReader.read();
|
|
559
|
-
if (done) break;
|
|
560
|
-
buffer += decoder.decode(value, { stream: true });
|
|
561
|
-
const parts = buffer.split("\n\n");
|
|
562
|
-
buffer = parts.pop() ?? "";
|
|
563
|
-
for (const part of parts) {
|
|
564
|
-
const event = parseSseChunk(part);
|
|
565
|
-
if (event != null) {
|
|
566
|
-
yield event;
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
if (buffer.trim().length > 0) {
|
|
571
|
-
const event = parseSseChunk(buffer.trim());
|
|
572
|
-
if (event != null) {
|
|
573
|
-
yield event;
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
return streamEvents();
|
|
578
|
-
}
|
|
579
|
-
if (options?.stream === false) {
|
|
580
|
-
params.stream = "false";
|
|
581
|
-
}
|
|
582
|
-
return this.client.request(`/research/v1/${researchId}`, "GET", void 0, params);
|
|
583
|
-
}
|
|
584
|
-
/** Poll a research request until it reaches a terminal state (completed, failed, or canceled). */
|
|
585
|
-
async pollUntilFinished(researchId, options) {
|
|
586
|
-
const pollInterval = options?.pollInterval ?? 1e3;
|
|
587
|
-
const timeoutMs = options?.timeoutMs ?? 10 * 60 * 1e3;
|
|
588
|
-
const maxConsecutiveFailures = 5;
|
|
589
|
-
const startTime = Date.now();
|
|
590
|
-
let consecutiveFailures = 0;
|
|
591
|
-
while (true) {
|
|
592
|
-
try {
|
|
593
|
-
const research = await this.get(researchId, { events: options?.events });
|
|
594
|
-
consecutiveFailures = 0;
|
|
595
|
-
if (research.status === "completed" || research.status === "failed" || research.status === "canceled") {
|
|
596
|
-
return research;
|
|
597
|
-
}
|
|
598
|
-
} catch (err) {
|
|
599
|
-
consecutiveFailures += 1;
|
|
600
|
-
if (consecutiveFailures >= maxConsecutiveFailures) {
|
|
601
|
-
throw new Error(`Polling failed ${maxConsecutiveFailures} times in a row for research ${researchId}: ${err}`);
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
if (Date.now() - startTime > timeoutMs) {
|
|
605
|
-
throw new Error(`Polling timeout: Research ${researchId} did not complete within ${timeoutMs}ms`);
|
|
606
|
-
}
|
|
607
|
-
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
};
|
|
611
|
-
|
|
612
|
-
// src/nova/client.ts
|
|
613
|
-
import { ZodType as ZodType2 } from "zod";
|
|
614
|
-
import { zodToJsonSchema as zodToJsonSchemaLib2 } from "zod-to-json-schema";
|
|
615
|
-
function isRecord(value) {
|
|
616
|
-
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
617
|
-
}
|
|
618
|
-
function isZodSchema(value) {
|
|
619
|
-
return value instanceof ZodType2;
|
|
620
|
-
}
|
|
621
|
-
function zodToJsonSchema2(schema) {
|
|
622
|
-
const converted = zodToJsonSchemaLib2(schema, { $refStrategy: "none" });
|
|
623
|
-
if (!isRecord(converted)) {
|
|
624
|
-
throw new Error("Failed to convert Zod schema to JSON Schema.");
|
|
625
|
-
}
|
|
626
|
-
return converted;
|
|
627
|
-
}
|
|
628
|
-
var ExaError = class extends Error {
|
|
629
|
-
constructor(message, statusCode, timestamp, path, details) {
|
|
630
|
-
super(message);
|
|
631
|
-
this.name = "ExaError";
|
|
632
|
-
this.statusCode = statusCode;
|
|
633
|
-
this.timestamp = timestamp ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
634
|
-
this.path = path;
|
|
635
|
-
this.details = details;
|
|
636
|
-
}
|
|
637
|
-
};
|
|
638
|
-
var CONTENTS_FIELDS = /* @__PURE__ */ new Set(["text", "highlights", "summary", "subpages", "subpageTarget", "extras", "livecrawl", "livecrawlTimeout", "maxAgeHours", "filterEmptyResults", "context"]);
|
|
639
|
-
var DEFAULT_MAX_CHARACTERS = 1e4;
|
|
640
|
-
var Exa = class {
|
|
641
|
-
constructor(apiKey, baseURL = "https://api.exa.ai") {
|
|
642
|
-
this.baseURL = baseURL;
|
|
643
|
-
if (!apiKey) {
|
|
644
|
-
apiKey = typeof process !== "undefined" ? process.env?.EXA_API_KEY : void 0;
|
|
645
|
-
if (!apiKey) {
|
|
646
|
-
throw new ExaError(
|
|
647
|
-
"API key must be provided as an argument or as an environment variable (EXA_API_KEY)",
|
|
648
|
-
401
|
|
649
|
-
);
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
this.headers = {
|
|
653
|
-
"x-api-key": apiKey,
|
|
654
|
-
"Content-Type": "application/json"
|
|
655
|
-
};
|
|
656
|
-
this.websets = new WebsetsClient(this);
|
|
657
|
-
this.research = new ResearchClient(this);
|
|
658
|
-
}
|
|
659
|
-
/** Make a request to the Exa API. Used by sub-clients and available for direct use. */
|
|
660
|
-
async request(endpoint, method, body, params, headers) {
|
|
661
|
-
const url = this.buildRequestUrl(endpoint, params);
|
|
662
|
-
const normalizedBody = this.normalizeCategoryInBody(body);
|
|
663
|
-
const combinedHeaders = headers ? { ...this.headers, ...headers } : this.headers;
|
|
664
|
-
const response = await fetch(url, {
|
|
665
|
-
method,
|
|
666
|
-
headers: combinedHeaders,
|
|
667
|
-
body: normalizedBody ? JSON.stringify(normalizedBody) : void 0
|
|
668
|
-
});
|
|
669
|
-
if (!response.ok) {
|
|
670
|
-
throw await this.toExaError(response, endpoint);
|
|
671
|
-
}
|
|
672
|
-
return await response.json();
|
|
673
|
-
}
|
|
674
|
-
/** Make a raw request returning the Response object directly. */
|
|
675
|
-
async rawRequest(endpoint, method = "POST", body, params) {
|
|
676
|
-
const url = this.buildRequestUrl(endpoint, params);
|
|
677
|
-
const normalizedBody = this.normalizeCategoryInBody(body);
|
|
678
|
-
return fetch(url, {
|
|
679
|
-
method,
|
|
680
|
-
headers: this.headers,
|
|
681
|
-
body: normalizedBody ? JSON.stringify(normalizedBody) : void 0
|
|
682
|
-
});
|
|
683
|
-
}
|
|
684
|
-
/** Normalize category values to underscore form. */
|
|
685
|
-
normalizeCategoryInBody(body) {
|
|
686
|
-
if (body == null || typeof body.category !== "string") {
|
|
687
|
-
return body;
|
|
688
|
-
}
|
|
689
|
-
return { ...body, category: body.category.replace(/ /g, "_") };
|
|
690
|
-
}
|
|
691
|
-
buildRequestUrl(endpoint, params) {
|
|
692
|
-
let url = `${this.baseURL}${endpoint}`;
|
|
693
|
-
if (params == null || Object.keys(params).length === 0) {
|
|
694
|
-
return url;
|
|
695
|
-
}
|
|
696
|
-
const searchParams = new URLSearchParams();
|
|
697
|
-
for (const [key, value] of Object.entries(params)) {
|
|
698
|
-
if (Array.isArray(value)) {
|
|
699
|
-
for (const item of value) {
|
|
700
|
-
searchParams.append(key, item);
|
|
701
|
-
}
|
|
702
|
-
} else if (value !== void 0) {
|
|
703
|
-
searchParams.append(key, String(value));
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
url += `?${searchParams.toString()}`;
|
|
707
|
-
return url;
|
|
708
|
-
}
|
|
709
|
-
tryParseJsonRecord(text) {
|
|
710
|
-
if (text.length === 0) {
|
|
711
|
-
return null;
|
|
712
|
-
}
|
|
713
|
-
try {
|
|
714
|
-
const parsed = JSON.parse(text);
|
|
715
|
-
return isRecord(parsed) ? parsed : null;
|
|
716
|
-
} catch {
|
|
717
|
-
return null;
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
async toExaError(response, endpoint) {
|
|
721
|
-
const text = await response.text();
|
|
722
|
-
const parsed = this.tryParseJsonRecord(text);
|
|
723
|
-
const statusCode = parsed != null && typeof parsed.statusCode === "number" ? parsed.statusCode : response.status;
|
|
724
|
-
const timestamp = parsed != null && typeof parsed.timestamp === "string" ? parsed.timestamp : void 0;
|
|
725
|
-
const path = parsed != null && typeof parsed.path === "string" ? parsed.path : endpoint;
|
|
726
|
-
const errorMessage = parsed != null && typeof parsed.error === "string" ? parsed.error : void 0;
|
|
727
|
-
const detailMessage = parsed != null && typeof parsed.message === "string" ? parsed.message : void 0;
|
|
728
|
-
let message;
|
|
729
|
-
if (errorMessage != null && detailMessage != null) {
|
|
730
|
-
message = `${errorMessage}. ${detailMessage}`;
|
|
731
|
-
} else if (errorMessage != null) {
|
|
732
|
-
message = errorMessage;
|
|
733
|
-
} else if (detailMessage != null) {
|
|
734
|
-
message = detailMessage;
|
|
735
|
-
} else if (text.length > 0) {
|
|
736
|
-
message = text;
|
|
737
|
-
} else {
|
|
738
|
-
message = `Exa API error ${response.status}`;
|
|
739
|
-
}
|
|
740
|
-
return new ExaError(message, statusCode, timestamp, path, parsed ?? text);
|
|
741
|
-
}
|
|
742
|
-
parseAnswerStreamLine(line) {
|
|
743
|
-
if (!line.startsWith("data:")) {
|
|
744
|
-
return null;
|
|
745
|
-
}
|
|
746
|
-
const jsonStr = line.replace(/^data:\s*/, "").trim();
|
|
747
|
-
if (jsonStr.length === 0 || jsonStr === "[DONE]") {
|
|
748
|
-
return null;
|
|
749
|
-
}
|
|
750
|
-
let parsedChunk;
|
|
751
|
-
try {
|
|
752
|
-
parsedChunk = JSON.parse(jsonStr);
|
|
753
|
-
} catch {
|
|
754
|
-
return null;
|
|
755
|
-
}
|
|
756
|
-
if (!isRecord(parsedChunk)) {
|
|
757
|
-
return null;
|
|
758
|
-
}
|
|
759
|
-
let content;
|
|
760
|
-
const choices = parsedChunk.choices;
|
|
761
|
-
if (Array.isArray(choices) && choices.length > 0 && isRecord(choices[0])) {
|
|
762
|
-
const delta = choices[0].delta;
|
|
763
|
-
if (isRecord(delta) && typeof delta.content === "string") {
|
|
764
|
-
content = delta.content;
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
let citations;
|
|
768
|
-
if (Array.isArray(parsedChunk.citations)) {
|
|
769
|
-
const normalized = [];
|
|
770
|
-
for (const citation of parsedChunk.citations) {
|
|
771
|
-
if (!isRecord(citation)) {
|
|
772
|
-
continue;
|
|
773
|
-
}
|
|
774
|
-
const id = typeof citation.id === "string" ? citation.id : void 0;
|
|
775
|
-
const url = typeof citation.url === "string" ? citation.url : void 0;
|
|
776
|
-
const title = typeof citation.title === "string" ? citation.title : void 0;
|
|
777
|
-
const publishedDate = typeof citation.publishedDate === "string" ? citation.publishedDate : void 0;
|
|
778
|
-
const author = typeof citation.author === "string" ? citation.author : void 0;
|
|
779
|
-
const text = typeof citation.text === "string" ? citation.text : void 0;
|
|
780
|
-
normalized.push({
|
|
781
|
-
...id != null ? { id } : {},
|
|
782
|
-
...url != null ? { url } : {},
|
|
783
|
-
...title != null ? { title } : {},
|
|
784
|
-
...publishedDate != null ? { publishedDate } : {},
|
|
785
|
-
...author != null ? { author } : {},
|
|
786
|
-
...text != null ? { text } : {}
|
|
787
|
-
});
|
|
788
|
-
}
|
|
789
|
-
if (normalized.length > 0) {
|
|
790
|
-
citations = normalized;
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
if (content == null && (citations == null || citations.length === 0)) {
|
|
794
|
-
return null;
|
|
795
|
-
}
|
|
796
|
-
return { content, citations };
|
|
797
|
-
}
|
|
798
|
-
/** Separate content-specific options from search/filter options. */
|
|
799
|
-
extractContentsOptions(options) {
|
|
800
|
-
const contentsOptions = {};
|
|
801
|
-
const restOptions = {};
|
|
802
|
-
let hasContentField = false;
|
|
803
|
-
for (const [key, value] of Object.entries(options)) {
|
|
804
|
-
if (CONTENTS_FIELDS.has(key)) {
|
|
805
|
-
contentsOptions[key] = value;
|
|
806
|
-
hasContentField = true;
|
|
807
|
-
} else {
|
|
808
|
-
restOptions[key] = value;
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
if (!hasContentField) {
|
|
812
|
-
contentsOptions.text = { maxCharacters: DEFAULT_MAX_CHARACTERS };
|
|
813
|
-
}
|
|
814
|
-
return { contentsOptions, restOptions };
|
|
815
|
-
}
|
|
816
|
-
search(query, options) {
|
|
817
|
-
let body;
|
|
818
|
-
if (options === void 0 || !("contents" in options)) {
|
|
819
|
-
body = { query, ...options, contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } } };
|
|
820
|
-
} else if (options.contents === false || options.contents === null || options.contents === void 0) {
|
|
821
|
-
const { contents, ...restOptions } = options;
|
|
822
|
-
body = { query, ...restOptions };
|
|
823
|
-
} else {
|
|
824
|
-
body = { query, ...options };
|
|
825
|
-
}
|
|
826
|
-
return this.request("/search", "POST", body);
|
|
827
|
-
}
|
|
828
|
-
findSimilar(url, options) {
|
|
829
|
-
let body;
|
|
830
|
-
if (options === void 0 || !("contents" in options)) {
|
|
831
|
-
body = { url, ...options, contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } } };
|
|
832
|
-
} else if (options.contents === false || options.contents === null || options.contents === void 0) {
|
|
833
|
-
const { contents, ...restOptions } = options;
|
|
834
|
-
body = { url, ...restOptions };
|
|
835
|
-
} else {
|
|
836
|
-
body = { url, ...options };
|
|
837
|
-
}
|
|
838
|
-
return this.request("/findSimilar", "POST", body);
|
|
839
|
-
}
|
|
840
|
-
getContents(urls, options) {
|
|
841
|
-
let requestUrls;
|
|
842
|
-
if (typeof urls === "string") {
|
|
843
|
-
requestUrls = [urls];
|
|
844
|
-
} else if (urls.length === 0) {
|
|
845
|
-
throw new ExaError("Must provide at least one URL", 400);
|
|
846
|
-
} else if (typeof urls[0] === "string") {
|
|
847
|
-
requestUrls = [];
|
|
848
|
-
for (const url of urls) {
|
|
849
|
-
if (typeof url !== "string") {
|
|
850
|
-
throw new ExaError("Expected all URLs to be strings or SearchResult objects with a url field", 400);
|
|
851
|
-
}
|
|
852
|
-
requestUrls.push(url);
|
|
853
|
-
}
|
|
854
|
-
} else {
|
|
855
|
-
requestUrls = [];
|
|
856
|
-
for (const result of urls) {
|
|
857
|
-
if (!isRecord(result) || typeof result.url !== "string") {
|
|
858
|
-
throw new ExaError("Expected all entries to have a string url field", 400);
|
|
859
|
-
}
|
|
860
|
-
requestUrls.push(result.url);
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
if (requestUrls.length === 0) {
|
|
864
|
-
throw new ExaError("Must provide at least one URL", 400);
|
|
865
|
-
}
|
|
866
|
-
return this.request("/contents", "POST", { urls: requestUrls, ...options });
|
|
867
|
-
}
|
|
868
|
-
async answer(query, options) {
|
|
869
|
-
if (options?.stream) {
|
|
870
|
-
throw new ExaError("For streaming responses, please use streamAnswer() instead:\n\nfor await (const chunk of exa.streamAnswer(query)) {\n // Handle chunks\n}", 400);
|
|
871
|
-
}
|
|
872
|
-
let outputSchema = options?.outputSchema;
|
|
873
|
-
if (outputSchema != null && isZodSchema(outputSchema)) {
|
|
874
|
-
outputSchema = zodToJsonSchema2(outputSchema);
|
|
875
|
-
}
|
|
876
|
-
return this.request("/answer", "POST", { query, model: "exa", stream: false, text: false, ...options, outputSchema });
|
|
877
|
-
}
|
|
878
|
-
async *streamAnswer(query, options) {
|
|
879
|
-
let outputSchema = options?.outputSchema;
|
|
880
|
-
if (outputSchema != null && isZodSchema(outputSchema)) {
|
|
881
|
-
outputSchema = zodToJsonSchema2(outputSchema);
|
|
882
|
-
}
|
|
883
|
-
const response = await this.rawRequest("/answer", "POST", { query, model: "exa", text: false, ...options, outputSchema, stream: true });
|
|
884
|
-
if (!response.ok) {
|
|
885
|
-
throw await this.toExaError(response, "/answer");
|
|
886
|
-
}
|
|
887
|
-
const reader = response.body?.getReader();
|
|
888
|
-
if (!reader) {
|
|
889
|
-
throw new ExaError("No response body available for streaming.", 500, (/* @__PURE__ */ new Date()).toISOString(), "/answer");
|
|
890
|
-
}
|
|
891
|
-
const decoder = new TextDecoder();
|
|
892
|
-
let buffer = "";
|
|
893
|
-
try {
|
|
894
|
-
while (true) {
|
|
895
|
-
const { done, value } = await reader.read();
|
|
896
|
-
if (done) break;
|
|
897
|
-
buffer += decoder.decode(value, { stream: true });
|
|
898
|
-
const lines = buffer.split("\n");
|
|
899
|
-
buffer = lines.pop() ?? "";
|
|
900
|
-
for (const line of lines) {
|
|
901
|
-
const chunk = this.parseAnswerStreamLine(line);
|
|
902
|
-
if (chunk != null) {
|
|
903
|
-
yield chunk;
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
const trailingChunk = this.parseAnswerStreamLine(buffer);
|
|
908
|
-
if (trailingChunk != null) {
|
|
909
|
-
yield trailingChunk;
|
|
910
|
-
}
|
|
911
|
-
} finally {
|
|
912
|
-
reader.releaseLock();
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
/**
|
|
916
|
-
* @deprecated Use `search()` instead. The search method now returns text contents by default.
|
|
917
|
-
* Search with contents. Extracts content fields (text, highlights, summary, etc.) into a `contents` wrapper.
|
|
918
|
-
*/
|
|
919
|
-
async searchAndContents(query, options) {
|
|
920
|
-
if (options === void 0) {
|
|
921
|
-
return this.request("/search", "POST", {
|
|
922
|
-
query,
|
|
923
|
-
contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } }
|
|
924
|
-
});
|
|
925
|
-
}
|
|
926
|
-
const { contentsOptions, restOptions } = this.extractContentsOptions(options);
|
|
927
|
-
return this.request("/search", "POST", { query, contents: contentsOptions, ...restOptions });
|
|
928
|
-
}
|
|
929
|
-
/**
|
|
930
|
-
* @deprecated Use `findSimilar()` instead. The findSimilar method now returns text contents by default.
|
|
931
|
-
* Find similar with contents. Extracts content fields into a `contents` wrapper.
|
|
932
|
-
*/
|
|
933
|
-
async findSimilarAndContents(url, options) {
|
|
934
|
-
if (options === void 0) {
|
|
935
|
-
return this.request("/findSimilar", "POST", {
|
|
936
|
-
url,
|
|
937
|
-
contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } }
|
|
938
|
-
});
|
|
939
|
-
}
|
|
940
|
-
const { contentsOptions, restOptions } = this.extractContentsOptions(options);
|
|
941
|
-
return this.request("/findSimilar", "POST", { url, contents: contentsOptions, ...restOptions });
|
|
942
|
-
}
|
|
943
|
-
};
|
|
944
|
-
|
|
945
91
|
// src/errors.ts
|
|
946
92
|
var HttpStatusCode = /* @__PURE__ */ ((HttpStatusCode2) => {
|
|
947
93
|
HttpStatusCode2[HttpStatusCode2["BadRequest"] = 400] = "BadRequest";
|
|
@@ -954,21 +100,37 @@ var HttpStatusCode = /* @__PURE__ */ ((HttpStatusCode2) => {
|
|
|
954
100
|
HttpStatusCode2[HttpStatusCode2["ServiceUnavailable"] = 503] = "ServiceUnavailable";
|
|
955
101
|
return HttpStatusCode2;
|
|
956
102
|
})(HttpStatusCode || {});
|
|
103
|
+
var ExaError = class extends Error {
|
|
104
|
+
/**
|
|
105
|
+
* Create a new ExaError
|
|
106
|
+
* @param message Error message
|
|
107
|
+
* @param statusCode HTTP status code
|
|
108
|
+
* @param timestamp ISO timestamp from API
|
|
109
|
+
* @param path Path that caused the error
|
|
110
|
+
*/
|
|
111
|
+
constructor(message, statusCode, timestamp, path) {
|
|
112
|
+
super(message);
|
|
113
|
+
this.name = "ExaError";
|
|
114
|
+
this.statusCode = statusCode;
|
|
115
|
+
this.timestamp = timestamp ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
116
|
+
this.path = path;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
957
119
|
|
|
958
120
|
// src/zod-utils.ts
|
|
959
|
-
import { ZodType
|
|
121
|
+
import { ZodType } from "zod";
|
|
960
122
|
import { zodToJsonSchema as convertZodToJsonSchema } from "zod-to-json-schema";
|
|
961
|
-
function
|
|
962
|
-
return obj instanceof
|
|
123
|
+
function isZodSchema(obj) {
|
|
124
|
+
return obj instanceof ZodType;
|
|
963
125
|
}
|
|
964
|
-
function
|
|
126
|
+
function zodToJsonSchema(schema) {
|
|
965
127
|
return convertZodToJsonSchema(schema, {
|
|
966
128
|
$refStrategy: "none"
|
|
967
129
|
});
|
|
968
130
|
}
|
|
969
131
|
|
|
970
132
|
// src/research/base.ts
|
|
971
|
-
var
|
|
133
|
+
var ResearchBaseClient = class {
|
|
972
134
|
constructor(client) {
|
|
973
135
|
this.client = client;
|
|
974
136
|
}
|
|
@@ -998,15 +160,15 @@ var ResearchBaseClient2 = class {
|
|
|
998
160
|
};
|
|
999
161
|
|
|
1000
162
|
// src/research/client.ts
|
|
1001
|
-
var
|
|
163
|
+
var ResearchClient = class extends ResearchBaseClient {
|
|
1002
164
|
constructor(client) {
|
|
1003
165
|
super(client);
|
|
1004
166
|
}
|
|
1005
167
|
async create(params) {
|
|
1006
168
|
const { instructions, model, outputSchema } = params;
|
|
1007
169
|
let schema = outputSchema;
|
|
1008
|
-
if (schema &&
|
|
1009
|
-
schema =
|
|
170
|
+
if (schema && isZodSchema(schema)) {
|
|
171
|
+
schema = zodToJsonSchema(schema);
|
|
1010
172
|
}
|
|
1011
173
|
const payload = {
|
|
1012
174
|
instructions,
|
|
@@ -1123,7 +285,7 @@ var ResearchClient2 = class extends ResearchBaseClient2 {
|
|
|
1123
285
|
};
|
|
1124
286
|
|
|
1125
287
|
// src/websets/base.ts
|
|
1126
|
-
var
|
|
288
|
+
var WebsetsBaseClient = class {
|
|
1127
289
|
/**
|
|
1128
290
|
* Initialize a new Websets base client
|
|
1129
291
|
* @param client The Exa client instance
|
|
@@ -1164,7 +326,7 @@ var WebsetsBaseClient2 = class {
|
|
|
1164
326
|
};
|
|
1165
327
|
|
|
1166
328
|
// src/websets/enrichments.ts
|
|
1167
|
-
var
|
|
329
|
+
var WebsetEnrichmentsClient = class extends WebsetsBaseClient {
|
|
1168
330
|
/**
|
|
1169
331
|
* Create an Enrichment for a Webset
|
|
1170
332
|
* @param websetId The ID of the Webset
|
|
@@ -1231,7 +393,7 @@ var WebsetEnrichmentsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1231
393
|
};
|
|
1232
394
|
|
|
1233
395
|
// src/websets/events.ts
|
|
1234
|
-
var
|
|
396
|
+
var EventsClient = class extends WebsetsBaseClient {
|
|
1235
397
|
/**
|
|
1236
398
|
* Initialize a new Events client
|
|
1237
399
|
* @param client The Exa client instance
|
|
@@ -1463,7 +625,7 @@ var WebsetSearchCanceledReason = /* @__PURE__ */ ((WebsetSearchCanceledReason2)
|
|
|
1463
625
|
})(WebsetSearchCanceledReason || {});
|
|
1464
626
|
|
|
1465
627
|
// src/websets/imports.ts
|
|
1466
|
-
var
|
|
628
|
+
var ImportsClient = class extends WebsetsBaseClient {
|
|
1467
629
|
async create(params, csv) {
|
|
1468
630
|
if (csv === void 0) {
|
|
1469
631
|
return this.request(
|
|
@@ -1646,7 +808,7 @@ var ImportsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1646
808
|
};
|
|
1647
809
|
|
|
1648
810
|
// src/websets/items.ts
|
|
1649
|
-
var
|
|
811
|
+
var WebsetItemsClient = class extends WebsetsBaseClient {
|
|
1650
812
|
/**
|
|
1651
813
|
* List all Items for a Webset
|
|
1652
814
|
* @param websetId The ID of the Webset
|
|
@@ -1726,7 +888,7 @@ var WebsetItemsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1726
888
|
};
|
|
1727
889
|
|
|
1728
890
|
// src/websets/monitors.ts
|
|
1729
|
-
var
|
|
891
|
+
var WebsetMonitorRunsClient = class extends WebsetsBaseClient {
|
|
1730
892
|
/**
|
|
1731
893
|
* List all runs for a Monitor
|
|
1732
894
|
* @param monitorId The ID of the Monitor
|
|
@@ -1755,10 +917,10 @@ var WebsetMonitorRunsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1755
917
|
);
|
|
1756
918
|
}
|
|
1757
919
|
};
|
|
1758
|
-
var
|
|
920
|
+
var WebsetMonitorsClient = class extends WebsetsBaseClient {
|
|
1759
921
|
constructor(client) {
|
|
1760
922
|
super(client);
|
|
1761
|
-
this.runs = new
|
|
923
|
+
this.runs = new WebsetMonitorRunsClient(client);
|
|
1762
924
|
}
|
|
1763
925
|
/**
|
|
1764
926
|
* Create a Monitor
|
|
@@ -1846,7 +1008,7 @@ var WebsetMonitorsClient2 = class extends WebsetsBaseClient2 {
|
|
|
1846
1008
|
};
|
|
1847
1009
|
|
|
1848
1010
|
// src/websets/searches.ts
|
|
1849
|
-
var
|
|
1011
|
+
var WebsetSearchesClient = class extends WebsetsBaseClient {
|
|
1850
1012
|
/**
|
|
1851
1013
|
* Create a new Search for the Webset
|
|
1852
1014
|
* @param websetId The ID of the Webset
|
|
@@ -1889,7 +1051,7 @@ var WebsetSearchesClient2 = class extends WebsetsBaseClient2 {
|
|
|
1889
1051
|
};
|
|
1890
1052
|
|
|
1891
1053
|
// src/websets/webhooks.ts
|
|
1892
|
-
var
|
|
1054
|
+
var WebsetWebhooksClient = class extends WebsetsBaseClient {
|
|
1893
1055
|
/**
|
|
1894
1056
|
* Create a Webhook
|
|
1895
1057
|
* @param params The webhook parameters
|
|
@@ -2026,20 +1188,20 @@ var WebsetWebhooksClient2 = class extends WebsetsBaseClient2 {
|
|
|
2026
1188
|
};
|
|
2027
1189
|
|
|
2028
1190
|
// src/websets/client.ts
|
|
2029
|
-
var
|
|
1191
|
+
var WebsetsClient = class extends WebsetsBaseClient {
|
|
2030
1192
|
/**
|
|
2031
1193
|
* Initialize a new Websets client
|
|
2032
1194
|
* @param client The Exa client instance
|
|
2033
1195
|
*/
|
|
2034
1196
|
constructor(client) {
|
|
2035
1197
|
super(client);
|
|
2036
|
-
this.events = new
|
|
2037
|
-
this.imports = new
|
|
2038
|
-
this.items = new
|
|
2039
|
-
this.searches = new
|
|
2040
|
-
this.enrichments = new
|
|
2041
|
-
this.monitors = new
|
|
2042
|
-
this.webhooks = new
|
|
1198
|
+
this.events = new EventsClient(client);
|
|
1199
|
+
this.imports = new ImportsClient(client);
|
|
1200
|
+
this.items = new WebsetItemsClient(client);
|
|
1201
|
+
this.searches = new WebsetSearchesClient(client);
|
|
1202
|
+
this.enrichments = new WebsetEnrichmentsClient(client);
|
|
1203
|
+
this.monitors = new WebsetMonitorsClient(client);
|
|
1204
|
+
this.webhooks = new WebsetWebhooksClient(client);
|
|
2043
1205
|
}
|
|
2044
1206
|
/**
|
|
2045
1207
|
* Create a new Webset
|
|
@@ -2203,8 +1365,8 @@ var WebsetsClient2 = class extends WebsetsBaseClient2 {
|
|
|
2203
1365
|
// src/index.ts
|
|
2204
1366
|
var fetchImpl = typeof global !== "undefined" && global.fetch ? global.fetch : fetch2;
|
|
2205
1367
|
var HeadersImpl = typeof global !== "undefined" && global.Headers ? global.Headers : Headers;
|
|
2206
|
-
var
|
|
2207
|
-
var
|
|
1368
|
+
var DEFAULT_MAX_CHARACTERS = 1e4;
|
|
1369
|
+
var Exa2 = class {
|
|
2208
1370
|
/**
|
|
2209
1371
|
* Helper method to separate out the contents-specific options from the rest.
|
|
2210
1372
|
*/
|
|
@@ -2229,10 +1391,10 @@ var Exa3 = class {
|
|
|
2229
1391
|
if (text !== void 0) contentsOptions.text = text;
|
|
2230
1392
|
if (highlights !== void 0) contentsOptions.highlights = highlights;
|
|
2231
1393
|
if (summary !== void 0) {
|
|
2232
|
-
if (typeof summary === "object" && summary !== null && "schema" in summary && summary.schema &&
|
|
1394
|
+
if (typeof summary === "object" && summary !== null && "schema" in summary && summary.schema && isZodSchema(summary.schema)) {
|
|
2233
1395
|
contentsOptions.summary = {
|
|
2234
1396
|
...summary,
|
|
2235
|
-
schema:
|
|
1397
|
+
schema: zodToJsonSchema(summary.schema)
|
|
2236
1398
|
};
|
|
2237
1399
|
} else {
|
|
2238
1400
|
contentsOptions.summary = summary;
|
|
@@ -2273,8 +1435,8 @@ var Exa3 = class {
|
|
|
2273
1435
|
"Content-Type": "application/json",
|
|
2274
1436
|
"User-Agent": `exa-node ${package_default.version}`
|
|
2275
1437
|
});
|
|
2276
|
-
this.websets = new
|
|
2277
|
-
this.research = new
|
|
1438
|
+
this.websets = new WebsetsClient(this);
|
|
1439
|
+
this.research = new ResearchClient(this);
|
|
2278
1440
|
}
|
|
2279
1441
|
/**
|
|
2280
1442
|
* Makes a request to the Exa API.
|
|
@@ -2371,7 +1533,7 @@ var Exa3 = class {
|
|
|
2371
1533
|
return await this.request("/search", "POST", {
|
|
2372
1534
|
query,
|
|
2373
1535
|
...options,
|
|
2374
|
-
contents: { text: { maxCharacters:
|
|
1536
|
+
contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } }
|
|
2375
1537
|
});
|
|
2376
1538
|
}
|
|
2377
1539
|
if (options.contents === false || options.contents === null || options.contents === void 0) {
|
|
@@ -2397,7 +1559,7 @@ var Exa3 = class {
|
|
|
2397
1559
|
async searchAndContents(query, options) {
|
|
2398
1560
|
const { contentsOptions, restOptions } = options === void 0 ? {
|
|
2399
1561
|
contentsOptions: {
|
|
2400
|
-
text: { maxCharacters:
|
|
1562
|
+
text: { maxCharacters: DEFAULT_MAX_CHARACTERS }
|
|
2401
1563
|
},
|
|
2402
1564
|
restOptions: {}
|
|
2403
1565
|
} : this.extractContentsOptions(options);
|
|
@@ -2412,7 +1574,7 @@ var Exa3 = class {
|
|
|
2412
1574
|
return await this.request("/findSimilar", "POST", {
|
|
2413
1575
|
url,
|
|
2414
1576
|
...options,
|
|
2415
|
-
contents: { text: { maxCharacters:
|
|
1577
|
+
contents: { text: { maxCharacters: DEFAULT_MAX_CHARACTERS } }
|
|
2416
1578
|
});
|
|
2417
1579
|
}
|
|
2418
1580
|
if (options.contents === false || options.contents === null || options.contents === void 0) {
|
|
@@ -2440,7 +1602,7 @@ var Exa3 = class {
|
|
|
2440
1602
|
async findSimilarAndContents(url, options) {
|
|
2441
1603
|
const { contentsOptions, restOptions } = options === void 0 ? {
|
|
2442
1604
|
contentsOptions: {
|
|
2443
|
-
text: { maxCharacters:
|
|
1605
|
+
text: { maxCharacters: DEFAULT_MAX_CHARACTERS }
|
|
2444
1606
|
},
|
|
2445
1607
|
restOptions: {}
|
|
2446
1608
|
} : this.extractContentsOptions(options);
|
|
@@ -2485,8 +1647,8 @@ var Exa3 = class {
|
|
|
2485
1647
|
);
|
|
2486
1648
|
}
|
|
2487
1649
|
let outputSchema = options?.outputSchema;
|
|
2488
|
-
if (outputSchema &&
|
|
2489
|
-
outputSchema =
|
|
1650
|
+
if (outputSchema && isZodSchema(outputSchema)) {
|
|
1651
|
+
outputSchema = zodToJsonSchema(outputSchema);
|
|
2490
1652
|
}
|
|
2491
1653
|
const requestBody = {
|
|
2492
1654
|
query,
|
|
@@ -2501,8 +1663,8 @@ var Exa3 = class {
|
|
|
2501
1663
|
}
|
|
2502
1664
|
async *streamAnswer(query, options) {
|
|
2503
1665
|
let outputSchema = options?.outputSchema;
|
|
2504
|
-
if (outputSchema &&
|
|
2505
|
-
outputSchema =
|
|
1666
|
+
if (outputSchema && isZodSchema(outputSchema)) {
|
|
1667
|
+
outputSchema = zodToJsonSchema(outputSchema);
|
|
2506
1668
|
}
|
|
2507
1669
|
const body = {
|
|
2508
1670
|
query,
|
|
@@ -2664,21 +1826,20 @@ var Exa3 = class {
|
|
|
2664
1826
|
});
|
|
2665
1827
|
}
|
|
2666
1828
|
};
|
|
2667
|
-
var index_default =
|
|
1829
|
+
var index_default = Exa2;
|
|
2668
1830
|
export {
|
|
2669
1831
|
CreateEnrichmentParametersFormat,
|
|
2670
1832
|
CreateImportParametersFormat,
|
|
2671
1833
|
EventType,
|
|
2672
|
-
|
|
2673
|
-
|
|
1834
|
+
EventsClient,
|
|
1835
|
+
Exa2 as Exa,
|
|
2674
1836
|
ExaError,
|
|
2675
|
-
Exa as ExaNova,
|
|
2676
1837
|
HttpStatusCode,
|
|
2677
1838
|
ImportFailedReason,
|
|
2678
1839
|
ImportFormat,
|
|
2679
1840
|
ImportObject,
|
|
2680
1841
|
ImportStatus,
|
|
2681
|
-
|
|
1842
|
+
ImportsClient,
|
|
2682
1843
|
MonitorObject,
|
|
2683
1844
|
MonitorRunObject,
|
|
2684
1845
|
MonitorRunStatus,
|
|
@@ -2688,21 +1849,21 @@ export {
|
|
|
2688
1849
|
WebhookStatus,
|
|
2689
1850
|
WebsetEnrichmentFormat,
|
|
2690
1851
|
WebsetEnrichmentStatus,
|
|
2691
|
-
|
|
1852
|
+
WebsetEnrichmentsClient,
|
|
2692
1853
|
WebsetExcludeSource,
|
|
2693
1854
|
WebsetImportSource,
|
|
2694
1855
|
WebsetItemEvaluationSatisfied,
|
|
2695
1856
|
WebsetItemSource,
|
|
2696
|
-
|
|
2697
|
-
|
|
1857
|
+
WebsetItemsClient,
|
|
1858
|
+
WebsetMonitorsClient,
|
|
2698
1859
|
WebsetSearchBehavior,
|
|
2699
1860
|
WebsetSearchCanceledReason,
|
|
2700
1861
|
WebsetSearchScopeSource,
|
|
2701
1862
|
WebsetSearchStatus,
|
|
2702
|
-
|
|
1863
|
+
WebsetSearchesClient,
|
|
2703
1864
|
WebsetStatus,
|
|
2704
|
-
|
|
2705
|
-
|
|
1865
|
+
WebsetWebhooksClient,
|
|
1866
|
+
WebsetsClient,
|
|
2706
1867
|
index_default as default
|
|
2707
1868
|
};
|
|
2708
1869
|
//# sourceMappingURL=index.mjs.map
|