exa-js 2.15.1 → 2.16.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.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.15.1",
7
+ version: "2.16.0",
8
8
  description: "Exa SDK for Node.js and the browser",
9
9
  publishConfig: {
10
10
  access: "public"
@@ -162,11 +162,51 @@ var AgentBaseClient = class {
162
162
  };
163
163
 
164
164
  // src/agent/client.ts
165
+ var DEFAULT_AGENT_POLL_INTERVAL_MS = 1e3;
166
+ var DEFAULT_AGENT_POLL_TIMEOUT_MS = 60 * 60 * 1e3;
167
+ var DEFAULT_AGENT_CREATE_AND_WAIT_TIMEOUT_MS = 2 * 60 * 1e3;
168
+ var TERMINAL_AGENT_RUN_STATUSES = /* @__PURE__ */ new Set([
169
+ "completed",
170
+ "failed",
171
+ "cancelled"
172
+ ]);
173
+ var AgentRunFailedError = class extends Error {
174
+ constructor(run) {
175
+ super(run.error?.message ?? `Agent run ${run.id} failed`);
176
+ this.name = "AgentRunFailedError";
177
+ this.run = run;
178
+ }
179
+ };
180
+ var AgentRunCancelledError = class extends Error {
181
+ constructor(run) {
182
+ super(`Agent run ${run.id} was cancelled`);
183
+ this.name = "AgentRunCancelledError";
184
+ this.run = run;
185
+ }
186
+ };
165
187
  function headersForBetas(betas) {
166
188
  const betaValues = betas?.filter(Boolean);
167
189
  if (!betaValues?.length) return void 0;
168
190
  return { "Exa-Beta": betaValues.join(",") };
169
191
  }
192
+ function isTerminalAgentRunStatus(status) {
193
+ return TERMINAL_AGENT_RUN_STATUSES.has(status);
194
+ }
195
+ function ensureCompletedRun(run) {
196
+ if (run.status === "failed") {
197
+ throw new AgentRunFailedError(run);
198
+ }
199
+ if (run.status === "cancelled") {
200
+ throw new AgentRunCancelledError(run);
201
+ }
202
+ return run;
203
+ }
204
+ function createAndWaitPollOptions(options) {
205
+ return {
206
+ pollInterval: options?.pollInterval ?? DEFAULT_AGENT_POLL_INTERVAL_MS,
207
+ timeoutMs: options?.timeoutMs ?? DEFAULT_AGENT_CREATE_AND_WAIT_TIMEOUT_MS
208
+ };
209
+ }
170
210
  function buildAgentRunPayload(params) {
171
211
  const { stream, outputSchema, ...rest } = params;
172
212
  let schema = outputSchema;
@@ -327,12 +367,12 @@ var AgentRunsClient = class extends AgentBaseClient {
327
367
  * Poll an Agent run until it reaches a terminal status.
328
368
  */
329
369
  async pollUntilFinished(runId, options) {
330
- const pollInterval = options?.pollInterval ?? 1e3;
331
- const timeoutMs = options?.timeoutMs ?? 60 * 60 * 1e3;
370
+ const pollInterval = options?.pollInterval ?? DEFAULT_AGENT_POLL_INTERVAL_MS;
371
+ const timeoutMs = options?.timeoutMs ?? DEFAULT_AGENT_POLL_TIMEOUT_MS;
332
372
  const startTime = Date.now();
333
373
  while (true) {
334
374
  const run = await this.get(runId);
335
- if (run.status === "completed" || run.status === "failed" || run.status === "cancelled") {
375
+ if (isTerminalAgentRunStatus(run.status)) {
336
376
  return run;
337
377
  }
338
378
  if (Date.now() - startTime > timeoutMs) {
@@ -343,6 +383,18 @@ var AgentRunsClient = class extends AgentBaseClient {
343
383
  await new Promise((resolve) => setTimeout(resolve, pollInterval));
344
384
  }
345
385
  }
386
+ async createAndWait(params, options) {
387
+ const { stream: _stream, ...createParams } = params;
388
+ const run = await this.create(
389
+ createParams
390
+ );
391
+ const runId = run.id;
392
+ const terminalRun = await this.pollUntilFinished(
393
+ runId,
394
+ createAndWaitPollOptions(options)
395
+ );
396
+ return ensureCompletedRun(terminalRun);
397
+ }
346
398
  };
347
399
  var AgentClient = class {
348
400
  constructor(client) {
@@ -465,8 +517,8 @@ var AgentBetaRunsClient = class extends AgentRunsClient {
465
517
  );
466
518
  }
467
519
  async pollUntilFinished(runId, options) {
468
- const pollInterval = options?.pollInterval ?? 1e3;
469
- const timeoutMs = options?.timeoutMs ?? 60 * 60 * 1e3;
520
+ const pollInterval = options?.pollInterval ?? DEFAULT_AGENT_POLL_INTERVAL_MS;
521
+ const timeoutMs = options?.timeoutMs ?? DEFAULT_AGENT_POLL_TIMEOUT_MS;
470
522
  const startTime = Date.now();
471
523
  const headers = headersForBetas(options?.betas);
472
524
  while (true) {
@@ -477,7 +529,7 @@ var AgentBetaRunsClient = class extends AgentRunsClient {
477
529
  void 0,
478
530
  headers
479
531
  );
480
- if (run.status === "completed" || run.status === "failed" || run.status === "cancelled") {
532
+ if (isTerminalAgentRunStatus(run.status)) {
481
533
  return run;
482
534
  }
483
535
  if (Date.now() - startTime > timeoutMs) {
@@ -488,6 +540,23 @@ var AgentBetaRunsClient = class extends AgentRunsClient {
488
540
  await new Promise((resolve) => setTimeout(resolve, pollInterval));
489
541
  }
490
542
  }
543
+ async createAndWait(params, options) {
544
+ const {
545
+ betas,
546
+ stream: _stream,
547
+ ...createParams
548
+ } = params;
549
+ const run = await this.create({
550
+ ...createParams,
551
+ betas
552
+ });
553
+ const runId = run.id;
554
+ const terminalRun = await this.pollUntilFinished(runId, {
555
+ ...createAndWaitPollOptions(options),
556
+ betas
557
+ });
558
+ return ensureCompletedRun(terminalRun);
559
+ }
491
560
  };
492
561
  var AgentBetaClient = class extends AgentClient {
493
562
  constructor(clientOrAgent) {
@@ -2471,7 +2540,9 @@ export {
2471
2540
  AGENT_BETA_HEADER,
2472
2541
  AgentBetaClient,
2473
2542
  AgentClient,
2543
+ AgentRunCancelledError,
2474
2544
  AgentRunEventsClient,
2545
+ AgentRunFailedError,
2475
2546
  AgentRunsClient,
2476
2547
  BetaClient,
2477
2548
  CreateEnrichmentParametersFormat,