exa-js 2.13.0 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -32,6 +32,7 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  AGENT_BETA_HEADER: () => AGENT_BETA_HEADER,
34
34
  AgentBetaClient: () => AgentBetaClient,
35
+ AgentClient: () => AgentClient,
35
36
  AgentRunEventsClient: () => AgentRunEventsClient,
36
37
  AgentRunsClient: () => AgentRunsClient,
37
38
  BetaClient: () => BetaClient,
@@ -81,7 +82,7 @@ var import_cross_fetch = __toESM(require("cross-fetch"));
81
82
  // package.json
82
83
  var package_default = {
83
84
  name: "exa-js",
84
- version: "2.13.0",
85
+ version: "2.14.0",
85
86
  description: "Exa SDK for Node.js and the browser",
86
87
  publishConfig: {
87
88
  access: "public"
@@ -211,34 +212,22 @@ var AgentBaseClient = class {
211
212
  constructor(client) {
212
213
  this.client = client;
213
214
  }
214
- async request(endpoint, betas, method = "POST", data, params, headers) {
215
- if (!betas?.length) {
216
- throw new Error("betas must include the Agent API beta identifier");
217
- }
215
+ async request(endpoint, method = "POST", data, params, headers) {
218
216
  return this.client.request(
219
217
  `/agent/runs${endpoint}`,
220
218
  method,
221
219
  data,
222
220
  params,
223
- {
224
- "Exa-Beta": betas.join(","),
225
- ...headers
226
- }
221
+ headers
227
222
  );
228
223
  }
229
- async rawRequest(endpoint, betas, method = "POST", data, params, headers) {
230
- if (!betas?.length) {
231
- throw new Error("betas must include the Agent API beta identifier");
232
- }
224
+ async rawRequest(endpoint, method = "POST", data, params, headers) {
233
225
  return this.client.rawRequest(
234
226
  `/agent/runs${endpoint}`,
235
227
  method,
236
228
  data,
237
229
  params,
238
- {
239
- "Exa-Beta": betas.join(","),
240
- ...headers
241
- }
230
+ headers
242
231
  );
243
232
  }
244
233
  buildPaginationParams(pagination) {
@@ -251,6 +240,23 @@ var AgentBaseClient = class {
251
240
  };
252
241
 
253
242
  // src/agent/client.ts
243
+ function headersForBetas(betas) {
244
+ const betaValues = betas?.filter(Boolean);
245
+ if (!betaValues?.length) return void 0;
246
+ return { "Exa-Beta": betaValues.join(",") };
247
+ }
248
+ function buildAgentRunPayload(params) {
249
+ const { stream, outputSchema, ...rest } = params;
250
+ let schema = outputSchema;
251
+ if (schema && isZodSchema(schema)) {
252
+ schema = zodToJsonSchema(schema);
253
+ }
254
+ const payload = { ...rest };
255
+ if (schema) {
256
+ payload.outputSchema = schema;
257
+ }
258
+ return { stream, payload };
259
+ }
254
260
  async function* streamAgentEvents(response) {
255
261
  if (!response.body) {
256
262
  throw new Error("No response body for SSE stream");
@@ -312,7 +318,6 @@ var AgentRunEventsClient = class extends AgentBaseClient {
312
318
  const params = this.buildPaginationParams(options);
313
319
  return this.request(
314
320
  `/${runId}/events`,
315
- options.betas,
316
321
  "GET",
317
322
  void 0,
318
323
  params
@@ -325,26 +330,11 @@ var AgentRunsClient = class extends AgentBaseClient {
325
330
  this.events = new AgentRunEventsClient(client);
326
331
  }
327
332
  async create(params) {
328
- const { stream, outputSchema, betas, ...rest } = params;
329
- let schema = outputSchema;
330
- if (schema && isZodSchema(schema)) {
331
- schema = zodToJsonSchema(schema);
332
- }
333
- const payload = { ...rest };
334
- if (schema) {
335
- payload.outputSchema = schema;
336
- }
333
+ const { stream, payload } = buildAgentRunPayload(params);
337
334
  if (stream) {
338
- const response = await this.rawRequest(
339
- "",
340
- betas,
341
- "POST",
342
- payload,
343
- void 0,
344
- {
345
- Accept: "text/event-stream"
346
- }
347
- );
335
+ const response = await this.rawRequest("", "POST", payload, void 0, {
336
+ Accept: "text/event-stream"
337
+ });
348
338
  if (!response.ok) {
349
339
  const message = await response.text();
350
340
  throw new ExaError(
@@ -356,33 +346,27 @@ var AgentRunsClient = class extends AgentBaseClient {
356
346
  }
357
347
  return streamAgentEvents(response);
358
348
  }
359
- return this.request("", betas, "POST", payload);
349
+ return this.request("", "POST", payload);
360
350
  }
361
351
  /**
362
352
  * Get an Agent run by ID.
363
353
  */
364
- async get(runId, options) {
365
- return this.request(`/${runId}`, options.betas, "GET");
354
+ async get(runId) {
355
+ return this.request(`/${runId}`, "GET");
366
356
  }
367
357
  /**
368
358
  * List Agent runs.
369
359
  */
370
360
  async list(options) {
371
361
  const params = this.buildPaginationParams(options);
372
- return this.request(
373
- "",
374
- options.betas,
375
- "GET",
376
- void 0,
377
- params
378
- );
362
+ return this.request("", "GET", void 0, params);
379
363
  }
380
364
  /**
381
365
  * Iterate through all Agent runs, handling pagination automatically.
382
366
  */
383
367
  async *listAll(options) {
384
368
  let cursor = void 0;
385
- const pageOptions = { ...options };
369
+ const pageOptions = options ? { ...options } : {};
386
370
  while (true) {
387
371
  pageOptions.cursor = cursor;
388
372
  const response = await this.list(pageOptions);
@@ -408,14 +392,14 @@ var AgentRunsClient = class extends AgentBaseClient {
408
392
  /**
409
393
  * Cancel a queued or running Agent run.
410
394
  */
411
- async cancel(runId, options) {
412
- return this.request(`/${runId}/cancel`, options.betas, "POST");
395
+ async cancel(runId) {
396
+ return this.request(`/${runId}/cancel`, "POST");
413
397
  }
414
398
  /**
415
399
  * Delete a stored Agent run.
416
400
  */
417
- async delete(runId, options) {
418
- return this.request(`/${runId}`, options.betas, "DELETE");
401
+ async delete(runId) {
402
+ return this.request(`/${runId}`, "DELETE");
419
403
  }
420
404
  /**
421
405
  * Poll an Agent run until it reaches a terminal status.
@@ -425,7 +409,7 @@ var AgentRunsClient = class extends AgentBaseClient {
425
409
  const timeoutMs = options?.timeoutMs ?? 60 * 60 * 1e3;
426
410
  const startTime = Date.now();
427
411
  while (true) {
428
- const run = await this.get(runId, { betas: options.betas });
412
+ const run = await this.get(runId);
429
413
  if (run.status === "completed" || run.status === "failed" || run.status === "cancelled") {
430
414
  return run;
431
415
  }
@@ -438,14 +422,161 @@ var AgentRunsClient = class extends AgentBaseClient {
438
422
  }
439
423
  }
440
424
  };
441
- var AgentBetaClient = class {
425
+ var AgentClient = class {
442
426
  constructor(client) {
427
+ this.client = client;
443
428
  this.runs = new AgentRunsClient(client);
444
429
  }
445
430
  };
446
- var BetaClient = class {
431
+ var AgentBetaRunEventsClient = class extends AgentRunEventsClient {
432
+ /**
433
+ * List stored events for an Agent run.
434
+ *
435
+ * @deprecated Use exa.agent.runs.events instead.
436
+ */
437
+ async list(runId, options) {
438
+ const { betas, ...pagination } = options ?? {};
439
+ const params = this.buildPaginationParams(pagination);
440
+ return this.request(
441
+ `/${runId}/events`,
442
+ "GET",
443
+ void 0,
444
+ params,
445
+ headersForBetas(betas)
446
+ );
447
+ }
448
+ };
449
+ var AgentBetaRunsClient = class extends AgentRunsClient {
447
450
  constructor(client) {
448
- this.agent = new AgentBetaClient(client);
451
+ super(client);
452
+ this.events = new AgentBetaRunEventsClient(client);
453
+ }
454
+ async create(params) {
455
+ const { betas, ...createParams } = params;
456
+ const { stream, payload } = buildAgentRunPayload(
457
+ createParams
458
+ );
459
+ const headers = headersForBetas(betas);
460
+ if (stream) {
461
+ const response = await this.rawRequest("", "POST", payload, void 0, {
462
+ ...headers,
463
+ Accept: "text/event-stream"
464
+ });
465
+ if (!response.ok) {
466
+ const message = await response.text();
467
+ throw new ExaError(
468
+ message || `Request failed with status code ${response.status}`,
469
+ response.status,
470
+ (/* @__PURE__ */ new Date()).toISOString(),
471
+ "/agent/runs"
472
+ );
473
+ }
474
+ return streamAgentEvents(response);
475
+ }
476
+ return this.request(
477
+ "",
478
+ "POST",
479
+ payload,
480
+ void 0,
481
+ headers
482
+ );
483
+ }
484
+ async get(runId, options) {
485
+ return this.request(
486
+ `/${runId}`,
487
+ "GET",
488
+ void 0,
489
+ void 0,
490
+ headersForBetas(options?.betas)
491
+ );
492
+ }
493
+ async list(options) {
494
+ const { betas, ...pagination } = options ?? {};
495
+ const params = this.buildPaginationParams(pagination);
496
+ return this.request(
497
+ "",
498
+ "GET",
499
+ void 0,
500
+ params,
501
+ headersForBetas(betas)
502
+ );
503
+ }
504
+ async *listAll(options) {
505
+ let cursor = void 0;
506
+ const { betas, ...pagination } = options ?? {};
507
+ const pageOptions = { ...pagination };
508
+ while (true) {
509
+ pageOptions.cursor = cursor;
510
+ const response = await this.list({ ...pageOptions, betas });
511
+ for (const run of response.data) {
512
+ yield run;
513
+ }
514
+ if (!response.hasMore || !response.nextCursor) {
515
+ break;
516
+ }
517
+ cursor = response.nextCursor;
518
+ }
519
+ }
520
+ async getAll(options) {
521
+ const runs = [];
522
+ for await (const run of this.listAll(options)) {
523
+ runs.push(run);
524
+ }
525
+ return runs;
526
+ }
527
+ async cancel(runId, options) {
528
+ return this.request(
529
+ `/${runId}/cancel`,
530
+ "POST",
531
+ void 0,
532
+ void 0,
533
+ headersForBetas(options?.betas)
534
+ );
535
+ }
536
+ async delete(runId, options) {
537
+ return this.request(
538
+ `/${runId}`,
539
+ "DELETE",
540
+ void 0,
541
+ void 0,
542
+ headersForBetas(options?.betas)
543
+ );
544
+ }
545
+ async pollUntilFinished(runId, options) {
546
+ const pollInterval = options?.pollInterval ?? 1e3;
547
+ const timeoutMs = options?.timeoutMs ?? 60 * 60 * 1e3;
548
+ const startTime = Date.now();
549
+ const headers = headersForBetas(options?.betas);
550
+ while (true) {
551
+ const run = await this.request(
552
+ `/${runId}`,
553
+ "GET",
554
+ void 0,
555
+ void 0,
556
+ headers
557
+ );
558
+ if (run.status === "completed" || run.status === "failed" || run.status === "cancelled") {
559
+ return run;
560
+ }
561
+ if (Date.now() - startTime > timeoutMs) {
562
+ throw new Error(
563
+ `Polling timeout: Agent run ${runId} did not complete within ${timeoutMs}ms`
564
+ );
565
+ }
566
+ await new Promise((resolve) => setTimeout(resolve, pollInterval));
567
+ }
568
+ }
569
+ };
570
+ var AgentBetaClient = class extends AgentClient {
571
+ constructor(clientOrAgent) {
572
+ const client = clientOrAgent instanceof AgentClient ? clientOrAgent.client : clientOrAgent;
573
+ super(client);
574
+ this.runs = new AgentBetaRunsClient(client);
575
+ }
576
+ };
577
+ var BetaClient = class {
578
+ constructor(clientOrAgent) {
579
+ this.agent = new AgentBetaClient(clientOrAgent);
449
580
  }
450
581
  };
451
582
 
@@ -1874,6 +2005,22 @@ var AGENT_BETA_HEADER = "agent-2026-05-07";
1874
2005
  var fetchImpl = typeof global !== "undefined" && global.fetch ? global.fetch : import_cross_fetch.default;
1875
2006
  var HeadersImpl = typeof global !== "undefined" && global.Headers ? global.Headers : import_cross_fetch.Headers;
1876
2007
  var DEFAULT_MAX_CHARACTERS = 1e4;
2008
+ var NON_JSON_BODY_SNIPPET_LENGTH = 500;
2009
+ async function readResponseBody(response) {
2010
+ const text = await response.text();
2011
+ if (!text) {
2012
+ return { json: void 0, text };
2013
+ }
2014
+ try {
2015
+ return { json: JSON.parse(text), text };
2016
+ } catch {
2017
+ return { json: void 0, text };
2018
+ }
2019
+ }
2020
+ function truncateResponseBody(text) {
2021
+ const collapsed = text.replace(/\s+/g, " ").trim();
2022
+ return collapsed.length > NON_JSON_BODY_SNIPPET_LENGTH ? `${collapsed.slice(0, NON_JSON_BODY_SNIPPET_LENGTH)}\u2026` : collapsed;
2023
+ }
1877
2024
  var Exa2 = class {
1878
2025
  /**
1879
2026
  * Helper method to separate out the contents-specific options from the rest.
@@ -1963,7 +2110,8 @@ var Exa2 = class {
1963
2110
  this.websets = new WebsetsClient(this);
1964
2111
  this.research = new ResearchClient(this);
1965
2112
  this.monitors = new SearchMonitorsClient(this);
1966
- this.beta = new BetaClient(this);
2113
+ this.agent = new AgentClient(this);
2114
+ this.beta = new BetaClient(this.agent);
1967
2115
  }
1968
2116
  /**
1969
2117
  * Makes a request to the Exa API.
@@ -2006,7 +2154,18 @@ var Exa2 = class {
2006
2154
  body: body ? JSON.stringify(body) : void 0
2007
2155
  });
2008
2156
  if (!response.ok) {
2009
- const errorData = await response.json();
2157
+ const { json: json2, text: text2 } = await readResponseBody(response);
2158
+ if (!json2 || typeof json2 !== "object") {
2159
+ const snippet = truncateResponseBody(text2);
2160
+ const message2 = snippet ? `Request failed with status ${response.status}. Response body was not valid JSON: ${snippet}` : `Request failed with status ${response.status}.`;
2161
+ throw new ExaError(
2162
+ message2,
2163
+ response.status,
2164
+ (/* @__PURE__ */ new Date()).toISOString(),
2165
+ endpoint
2166
+ );
2167
+ }
2168
+ const errorData = json2;
2010
2169
  if (!errorData.statusCode) {
2011
2170
  errorData.statusCode = response.status;
2012
2171
  }
@@ -2031,7 +2190,16 @@ var Exa2 = class {
2031
2190
  if (contentType.includes("text/event-stream")) {
2032
2191
  return await this.parseSSEStream(response);
2033
2192
  }
2034
- return await response.json();
2193
+ const { json, text } = await readResponseBody(response);
2194
+ if (json === void 0 && text) {
2195
+ throw new ExaError(
2196
+ `Expected a JSON response but received a non-JSON body (status ${response.status}): ${truncateResponseBody(text)}`,
2197
+ response.status,
2198
+ (/* @__PURE__ */ new Date()).toISOString(),
2199
+ endpoint
2200
+ );
2201
+ }
2202
+ return json;
2035
2203
  }
2036
2204
  async rawRequest(endpoint, method = "POST", body, queryParams, headers) {
2037
2205
  let url = this.baseURL + endpoint;
@@ -2381,6 +2549,7 @@ var index_default = Exa2;
2381
2549
  0 && (module.exports = {
2382
2550
  AGENT_BETA_HEADER,
2383
2551
  AgentBetaClient,
2552
+ AgentClient,
2384
2553
  AgentRunEventsClient,
2385
2554
  AgentRunsClient,
2386
2555
  BetaClient,