exa-js 2.26.0 → 2.28.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
@@ -103,7 +103,7 @@ var import_cross_fetch = __toESM(require("cross-fetch"));
103
103
  // package.json
104
104
  var package_default = {
105
105
  name: "exa-js",
106
- version: "2.26.0",
106
+ version: "2.28.0",
107
107
  description: "Exa SDK for Node.js and the browser",
108
108
  publishConfig: {
109
109
  access: "public"
@@ -115,10 +115,18 @@ var package_default = {
115
115
  module: "./dist/index.mjs",
116
116
  exports: {
117
117
  ".": {
118
- types: "./dist/index.d.ts",
119
- require: "./dist/index.js",
120
- module: "./dist/index.mjs",
121
- import: "./dist/index.mjs"
118
+ require: {
119
+ types: "./dist/index.d.ts",
120
+ default: "./dist/index.js"
121
+ },
122
+ module: {
123
+ types: "./dist/index.d.mts",
124
+ default: "./dist/index.mjs"
125
+ },
126
+ import: {
127
+ types: "./dist/index.d.mts",
128
+ default: "./dist/index.mjs"
129
+ }
122
130
  },
123
131
  "./package.json": "./package.json"
124
132
  },
@@ -130,9 +138,8 @@ var package_default = {
130
138
  "test:unit": "vitest run --config vitest.unit.config.ts",
131
139
  "test:integration": "vitest run --config vitest.integration.config.ts",
132
140
  typecheck: "tsc --noEmit",
133
- "typecheck:examples": "tsc -p examples/tsconfig.json --noEmit",
134
141
  "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",
135
- format: 'prettier --write "src/**/*.ts" "examples/**/*.ts"',
142
+ format: 'prettier --write "src/**/*.ts"',
136
143
  "format:websets": "prettier --write src/websets/openapi.ts",
137
144
  "generate-docs": "npx tsx scripts/generate-docs.ts",
138
145
  "build:beta": "cross-env NPM_CONFIG_TAG=beta npm run build",
@@ -145,23 +152,22 @@ var package_default = {
145
152
  license: "MIT",
146
153
  devDependencies: {
147
154
  "@anthropic-ai/sdk": "^0.115.0",
148
- "@types/node": "~22.14.0",
155
+ "@types/node": "~22.14.1",
149
156
  "cross-env": "~7.0.3",
157
+ openai: "^6.49.0",
150
158
  "openapi-typescript": "~7.6.1",
151
159
  prettier: "~3.5.3",
152
160
  "ts-morph": "^27.0.2",
153
161
  "ts-node": "~10.9.2",
154
162
  tsup: "~8.4.0",
155
- tsx: "^4.21.0",
163
+ tsx: "^4.23.15",
156
164
  typescript: "~5.8.3",
157
- vitest: "~3.1.1"
165
+ vitest: "~4.1.12"
158
166
  },
159
167
  dependencies: {
160
168
  "cross-fetch": "~4.1.0",
161
- dotenv: "~16.4.7",
162
- openai: "^5.0.1",
163
- zod: "^3.22.0",
164
- "zod-to-json-schema": "^3.20.0"
169
+ zod: "^3.25.28 || ^4.6.6",
170
+ "zod-to-json-schema": "^3.25.2"
165
171
  },
166
172
  directories: {
167
173
  test: "test"
@@ -267,6 +273,22 @@ async function readResponseBody(response) {
267
273
  return { json: void 0, text };
268
274
  }
269
275
  }
276
+ async function readJsonBody(response, path) {
277
+ const { json, text } = await readResponseBody(response);
278
+ if (json === void 0 && text) {
279
+ throw new ExaError(
280
+ `Expected a JSON response but received a non-JSON body (status ${response.status}): ${truncateResponseBody(text)}`,
281
+ response.status,
282
+ (/* @__PURE__ */ new Date()).toISOString(),
283
+ path
284
+ );
285
+ }
286
+ return json;
287
+ }
288
+ function isJsonContentType(contentType) {
289
+ const mediaType = (contentType ?? "").split(";")[0].trim().toLowerCase();
290
+ return mediaType === "application/json" || mediaType.endsWith("+json");
291
+ }
270
292
  function truncateResponseBody(text) {
271
293
  const collapsed = text.replace(/\s+/g, " ").trim();
272
294
  return collapsed.length > NON_JSON_BODY_SNIPPET_LENGTH ? `${collapsed.slice(0, NON_JSON_BODY_SNIPPET_LENGTH)}\u2026` : collapsed;
@@ -318,10 +340,10 @@ async function exaErrorFromResponse(response, path) {
318
340
  }
319
341
 
320
342
  // src/zod-utils.ts
321
- var import_zod = require("zod");
343
+ var import_v3 = require("zod/v3");
322
344
  var import_zod_to_json_schema = require("zod-to-json-schema");
323
345
  function isZodSchema(obj) {
324
- return obj instanceof import_zod.ZodType;
346
+ return obj instanceof import_v3.ZodType;
325
347
  }
326
348
  function zodToJsonSchema(schema) {
327
349
  return (0, import_zod_to_json_schema.zodToJsonSchema)(schema, {
@@ -3133,7 +3155,7 @@ var WebsetsClient = class extends WebsetsBaseClient {
3133
3155
  };
3134
3156
 
3135
3157
  // src/tools/core.ts
3136
- var import_zod2 = require("zod");
3158
+ var import_v32 = require("zod/v3");
3137
3159
  var DEFAULT_WEB_SEARCH_TOOL_DESCRIPTION = "Search the web for up-to-date, relevant information. Describe the ideal page rather than listing keywords.";
3138
3160
  var DEFAULT_GET_CONTENTS_TOOL_DESCRIPTION = "Read the full contents of web pages you already have URLs for, such as pages returned by a search or mentioned by the user.";
3139
3161
  var SEARCH_OBJECTIVE_TOOL_DESCRIPTION = "Goal for this search turn; say which documents should rank first, which should be excluded, and what specific facts or figures to pull from them.";
@@ -3198,7 +3220,7 @@ function createTool(registry, params) {
3198
3220
  };
3199
3221
  return registerTool(registry, tool);
3200
3222
  }
3201
- var AdvertisedRequiredOptional = class extends import_zod2.z.ZodOptional {
3223
+ var AdvertisedRequiredOptional = class extends import_v32.z.ZodOptional {
3202
3224
  isOptional() {
3203
3225
  return false;
3204
3226
  }
@@ -3206,7 +3228,7 @@ var AdvertisedRequiredOptional = class extends import_zod2.z.ZodOptional {
3206
3228
  function advertisedRequired(inner) {
3207
3229
  return new AdvertisedRequiredOptional({
3208
3230
  innerType: inner,
3209
- typeName: import_zod2.z.ZodFirstPartyTypeKind.ZodOptional
3231
+ typeName: import_v32.z.ZodFirstPartyTypeKind.ZodOptional
3210
3232
  });
3211
3233
  }
3212
3234
  function createWebSearchTool(exa, registry, config = {}) {
@@ -3215,14 +3237,14 @@ function createWebSearchTool(exa, registry, config = {}) {
3215
3237
  description = DEFAULT_WEB_SEARCH_TOOL_DESCRIPTION,
3216
3238
  ...searchOptions
3217
3239
  } = config;
3218
- const inputSchema = import_zod2.z.object({
3219
- query: import_zod2.z.string().describe(
3240
+ const inputSchema = import_v32.z.object({
3241
+ query: import_v32.z.string().describe(
3220
3242
  "Natural language search query. Should be a semantically rich description of the ideal page, not just keywords."
3221
3243
  ),
3222
3244
  // Length limits are validated but not advertised: strict tool use on some
3223
3245
  // providers rejects `minLength`/`maxLength` in the schema.
3224
3246
  objective: advertisedRequired(
3225
- import_zod2.z.string().trim().refine((value) => value.length > 0, "objective must not be empty").refine(
3247
+ import_v32.z.string().trim().refine((value) => value.length > 0, "objective must not be empty").refine(
3226
3248
  (value) => value.length <= MAX_OBJECTIVE_LENGTH,
3227
3249
  `objective must be at most ${MAX_OBJECTIVE_LENGTH} characters`
3228
3250
  )
@@ -3259,8 +3281,8 @@ function createGetContentsTool(exa, registry, config = {}) {
3259
3281
  description = DEFAULT_GET_CONTENTS_TOOL_DESCRIPTION,
3260
3282
  ...contentsOptions
3261
3283
  } = config;
3262
- const inputSchema = import_zod2.z.object({
3263
- urls: import_zod2.z.array(import_zod2.z.string()).describe(
3284
+ const inputSchema = import_v32.z.object({
3285
+ urls: import_v32.z.array(import_v32.z.string()).describe(
3264
3286
  "Absolute URLs of the pages to read, including the scheme. Pass several URLs to read them in a single call."
3265
3287
  )
3266
3288
  });
@@ -3608,6 +3630,30 @@ function bodyWarnings(endpoint, body) {
3608
3630
  const { warnings } = body;
3609
3631
  return Array.isArray(warnings) ? warnings.filter(isApiWarning) : [];
3610
3632
  }
3633
+ function costDollarsOf(payload) {
3634
+ if (typeof payload !== "object" || payload === null) {
3635
+ return void 0;
3636
+ }
3637
+ const costDollars = payload.costDollars;
3638
+ if (typeof costDollars !== "object" || costDollars === null || typeof costDollars.total !== "number") {
3639
+ return void 0;
3640
+ }
3641
+ return costDollars;
3642
+ }
3643
+ function citationsFromResults(results) {
3644
+ if (!Array.isArray(results)) {
3645
+ return void 0;
3646
+ }
3647
+ const citations = results.filter((result) => typeof result === "object" && result !== null).map((result) => ({
3648
+ id: result.id,
3649
+ url: result.url,
3650
+ title: result.title ?? void 0,
3651
+ publishedDate: result.publishedDate ?? void 0,
3652
+ author: result.author ?? void 0,
3653
+ text: result.text ?? void 0
3654
+ }));
3655
+ return citations.length > 0 ? citations : void 0;
3656
+ }
3611
3657
  function withJsonSummarySchema(contents) {
3612
3658
  if (typeof contents !== "object" || contents === null) return contents;
3613
3659
  const summary = contents.summary;
@@ -3806,15 +3852,7 @@ var Exa2 = class {
3806
3852
  if (contentType.includes("text/event-stream")) {
3807
3853
  return await this.parseSSEStream(response);
3808
3854
  }
3809
- const { json, text } = await readResponseBody(response);
3810
- if (json === void 0 && text) {
3811
- throw new ExaError(
3812
- `Expected a JSON response but received a non-JSON body (status ${response.status}): ${truncateResponseBody(text)}`,
3813
- response.status,
3814
- (/* @__PURE__ */ new Date()).toISOString(),
3815
- endpoint
3816
- );
3817
- }
3855
+ const json = await readJsonBody(response, endpoint);
3818
3856
  this.reportWarnings(
3819
3857
  endpoint,
3820
3858
  method,
@@ -3959,9 +3997,14 @@ var Exa2 = class {
3959
3997
  *
3960
3998
  * Each iteration yields a chunk with partial text (`content`) or new citations.
3961
3999
  * When the API returns `warnings` about the request, one chunk after the text
3962
- * carries them.
4000
+ * carries them. The last chunk carries the request's cost breakdown
4001
+ * (`costDollars`).
3963
4002
  * Use this if you'd like to read synthesized search output incrementally.
3964
4003
  * The `lite` search type does not support streaming.
4004
+ *
4005
+ * A search without `outputSchema` has no text to stream. Its stream yields
4006
+ * one chunk with the results as `citations`, then the warnings chunk when
4007
+ * the API returns warnings. Call `search()` for every field of the results.
3965
4008
  * @param {RequestOptions} [requestOptions] - Per-call request options (`signal`, `timeout`, `body`, `headers`, `query`).
3966
4009
  */
3967
4010
  streamSearch(query, options, requestOptions) {
@@ -4179,6 +4222,12 @@ var Exa2 = class {
4179
4222
  throw await exaErrorFromResponse(response, endpoint);
4180
4223
  }
4181
4224
  const requestIdHeader = response.headers.get("x-request-id");
4225
+ if (isJsonContentType(response.headers.get("content-type"))) {
4226
+ const payload = await readJsonBody(response, endpoint);
4227
+ this.reportWarnings(endpoint, "POST", payload, requestIdHeader);
4228
+ yield* this.jsonResponseChunks(payload);
4229
+ return;
4230
+ }
4182
4231
  const reader = response.body?.getReader();
4183
4232
  if (!reader) {
4184
4233
  throw new ExaError(
@@ -4208,11 +4257,8 @@ var Exa2 = class {
4208
4257
  } catch (err) {
4209
4258
  continue;
4210
4259
  }
4211
- const chunk = this.processChunk(chunkData);
4212
4260
  this.reportWarnings(endpoint, "POST", chunkData, requestIdHeader);
4213
- if (chunk.content || chunk.citations || chunk.warnings) {
4214
- yield chunk;
4215
- }
4261
+ yield* this.payloadChunks(chunkData);
4216
4262
  }
4217
4263
  }
4218
4264
  if (buffer.startsWith("data: ")) {
@@ -4220,11 +4266,8 @@ var Exa2 = class {
4220
4266
  if (leftover && leftover !== "[DONE]") {
4221
4267
  try {
4222
4268
  const chunkData = JSON.parse(leftover);
4223
- const chunk = this.processChunk(chunkData);
4224
4269
  this.reportWarnings(endpoint, "POST", chunkData, requestIdHeader);
4225
- if (chunk.content || chunk.citations || chunk.warnings) {
4226
- yield chunk;
4227
- }
4270
+ yield* this.payloadChunks(chunkData);
4228
4271
  } catch (e) {
4229
4272
  }
4230
4273
  }
@@ -4236,6 +4279,39 @@ var Exa2 = class {
4236
4279
  }
4237
4280
  }
4238
4281
  }
4282
+ /**
4283
+ * The chunks for a response the API sent whole as JSON instead of streaming
4284
+ * it. The first carries its `results` as `citations`. The rest are the
4285
+ * chunks `payloadChunks` makes of the whole payload: its `warnings`, then
4286
+ * its `costDollars`. A chunk with nothing in it is left out, as in a stream.
4287
+ */
4288
+ *jsonResponseChunks(payload) {
4289
+ if (typeof payload !== "object" || payload === null) {
4290
+ return;
4291
+ }
4292
+ const citations = citationsFromResults(
4293
+ payload.results
4294
+ );
4295
+ if (citations) {
4296
+ yield { citations };
4297
+ }
4298
+ yield* this.payloadChunks(payload);
4299
+ }
4300
+ /**
4301
+ * The chunks one stream payload yields: the chunk `processChunk` makes of
4302
+ * it, then its `costDollars` on a chunk of their own. A chunk with nothing
4303
+ * in it is left out.
4304
+ */
4305
+ *payloadChunks(chunkData) {
4306
+ const chunk = this.processChunk(chunkData);
4307
+ if (chunk.content || chunk.citations || chunk.warnings) {
4308
+ yield chunk;
4309
+ }
4310
+ const costDollars = costDollarsOf(chunkData);
4311
+ if (costDollars) {
4312
+ yield { costDollars };
4313
+ }
4314
+ }
4239
4315
  /**
4240
4316
  * Parses one stream payload into a chunk. `warnings` is set only when the
4241
4317
  * payload carries a non-empty `warnings` array, which `/search` sends on its
@@ -4384,4 +4460,3 @@ var index_default = Exa2;
4384
4460
  createGetContentsTool,
4385
4461
  createWebSearchTool
4386
4462
  });
4387
- //# sourceMappingURL=index.js.map
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.26.0",
7
+ version: "2.28.0",
8
8
  description: "Exa SDK for Node.js and the browser",
9
9
  publishConfig: {
10
10
  access: "public"
@@ -16,10 +16,18 @@ var package_default = {
16
16
  module: "./dist/index.mjs",
17
17
  exports: {
18
18
  ".": {
19
- types: "./dist/index.d.ts",
20
- require: "./dist/index.js",
21
- module: "./dist/index.mjs",
22
- import: "./dist/index.mjs"
19
+ require: {
20
+ types: "./dist/index.d.ts",
21
+ default: "./dist/index.js"
22
+ },
23
+ module: {
24
+ types: "./dist/index.d.mts",
25
+ default: "./dist/index.mjs"
26
+ },
27
+ import: {
28
+ types: "./dist/index.d.mts",
29
+ default: "./dist/index.mjs"
30
+ }
23
31
  },
24
32
  "./package.json": "./package.json"
25
33
  },
@@ -31,9 +39,8 @@ var package_default = {
31
39
  "test:unit": "vitest run --config vitest.unit.config.ts",
32
40
  "test:integration": "vitest run --config vitest.integration.config.ts",
33
41
  typecheck: "tsc --noEmit",
34
- "typecheck:examples": "tsc -p examples/tsconfig.json --noEmit",
35
42
  "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",
36
- format: 'prettier --write "src/**/*.ts" "examples/**/*.ts"',
43
+ format: 'prettier --write "src/**/*.ts"',
37
44
  "format:websets": "prettier --write src/websets/openapi.ts",
38
45
  "generate-docs": "npx tsx scripts/generate-docs.ts",
39
46
  "build:beta": "cross-env NPM_CONFIG_TAG=beta npm run build",
@@ -46,23 +53,22 @@ var package_default = {
46
53
  license: "MIT",
47
54
  devDependencies: {
48
55
  "@anthropic-ai/sdk": "^0.115.0",
49
- "@types/node": "~22.14.0",
56
+ "@types/node": "~22.14.1",
50
57
  "cross-env": "~7.0.3",
58
+ openai: "^6.49.0",
51
59
  "openapi-typescript": "~7.6.1",
52
60
  prettier: "~3.5.3",
53
61
  "ts-morph": "^27.0.2",
54
62
  "ts-node": "~10.9.2",
55
63
  tsup: "~8.4.0",
56
- tsx: "^4.21.0",
64
+ tsx: "^4.23.15",
57
65
  typescript: "~5.8.3",
58
- vitest: "~3.1.1"
66
+ vitest: "~4.1.12"
59
67
  },
60
68
  dependencies: {
61
69
  "cross-fetch": "~4.1.0",
62
- dotenv: "~16.4.7",
63
- openai: "^5.0.1",
64
- zod: "^3.22.0",
65
- "zod-to-json-schema": "^3.20.0"
70
+ zod: "^3.25.28 || ^4.6.6",
71
+ "zod-to-json-schema": "^3.25.2"
66
72
  },
67
73
  directories: {
68
74
  test: "test"
@@ -168,6 +174,22 @@ async function readResponseBody(response) {
168
174
  return { json: void 0, text };
169
175
  }
170
176
  }
177
+ async function readJsonBody(response, path) {
178
+ const { json, text } = await readResponseBody(response);
179
+ if (json === void 0 && text) {
180
+ throw new ExaError(
181
+ `Expected a JSON response but received a non-JSON body (status ${response.status}): ${truncateResponseBody(text)}`,
182
+ response.status,
183
+ (/* @__PURE__ */ new Date()).toISOString(),
184
+ path
185
+ );
186
+ }
187
+ return json;
188
+ }
189
+ function isJsonContentType(contentType) {
190
+ const mediaType = (contentType ?? "").split(";")[0].trim().toLowerCase();
191
+ return mediaType === "application/json" || mediaType.endsWith("+json");
192
+ }
171
193
  function truncateResponseBody(text) {
172
194
  const collapsed = text.replace(/\s+/g, " ").trim();
173
195
  return collapsed.length > NON_JSON_BODY_SNIPPET_LENGTH ? `${collapsed.slice(0, NON_JSON_BODY_SNIPPET_LENGTH)}\u2026` : collapsed;
@@ -219,7 +241,7 @@ async function exaErrorFromResponse(response, path) {
219
241
  }
220
242
 
221
243
  // src/zod-utils.ts
222
- import { ZodType } from "zod";
244
+ import { ZodType } from "zod/v3";
223
245
  import { zodToJsonSchema as convertZodToJsonSchema } from "zod-to-json-schema";
224
246
  function isZodSchema(obj) {
225
247
  return obj instanceof ZodType;
@@ -3034,7 +3056,7 @@ var WebsetsClient = class extends WebsetsBaseClient {
3034
3056
  };
3035
3057
 
3036
3058
  // src/tools/core.ts
3037
- import { z } from "zod";
3059
+ import { z } from "zod/v3";
3038
3060
  var DEFAULT_WEB_SEARCH_TOOL_DESCRIPTION = "Search the web for up-to-date, relevant information. Describe the ideal page rather than listing keywords.";
3039
3061
  var DEFAULT_GET_CONTENTS_TOOL_DESCRIPTION = "Read the full contents of web pages you already have URLs for, such as pages returned by a search or mentioned by the user.";
3040
3062
  var SEARCH_OBJECTIVE_TOOL_DESCRIPTION = "Goal for this search turn; say which documents should rank first, which should be excluded, and what specific facts or figures to pull from them.";
@@ -3509,6 +3531,30 @@ function bodyWarnings(endpoint, body) {
3509
3531
  const { warnings } = body;
3510
3532
  return Array.isArray(warnings) ? warnings.filter(isApiWarning) : [];
3511
3533
  }
3534
+ function costDollarsOf(payload) {
3535
+ if (typeof payload !== "object" || payload === null) {
3536
+ return void 0;
3537
+ }
3538
+ const costDollars = payload.costDollars;
3539
+ if (typeof costDollars !== "object" || costDollars === null || typeof costDollars.total !== "number") {
3540
+ return void 0;
3541
+ }
3542
+ return costDollars;
3543
+ }
3544
+ function citationsFromResults(results) {
3545
+ if (!Array.isArray(results)) {
3546
+ return void 0;
3547
+ }
3548
+ const citations = results.filter((result) => typeof result === "object" && result !== null).map((result) => ({
3549
+ id: result.id,
3550
+ url: result.url,
3551
+ title: result.title ?? void 0,
3552
+ publishedDate: result.publishedDate ?? void 0,
3553
+ author: result.author ?? void 0,
3554
+ text: result.text ?? void 0
3555
+ }));
3556
+ return citations.length > 0 ? citations : void 0;
3557
+ }
3512
3558
  function withJsonSummarySchema(contents) {
3513
3559
  if (typeof contents !== "object" || contents === null) return contents;
3514
3560
  const summary = contents.summary;
@@ -3707,15 +3753,7 @@ var Exa2 = class {
3707
3753
  if (contentType.includes("text/event-stream")) {
3708
3754
  return await this.parseSSEStream(response);
3709
3755
  }
3710
- const { json, text } = await readResponseBody(response);
3711
- if (json === void 0 && text) {
3712
- throw new ExaError(
3713
- `Expected a JSON response but received a non-JSON body (status ${response.status}): ${truncateResponseBody(text)}`,
3714
- response.status,
3715
- (/* @__PURE__ */ new Date()).toISOString(),
3716
- endpoint
3717
- );
3718
- }
3756
+ const json = await readJsonBody(response, endpoint);
3719
3757
  this.reportWarnings(
3720
3758
  endpoint,
3721
3759
  method,
@@ -3860,9 +3898,14 @@ var Exa2 = class {
3860
3898
  *
3861
3899
  * Each iteration yields a chunk with partial text (`content`) or new citations.
3862
3900
  * When the API returns `warnings` about the request, one chunk after the text
3863
- * carries them.
3901
+ * carries them. The last chunk carries the request's cost breakdown
3902
+ * (`costDollars`).
3864
3903
  * Use this if you'd like to read synthesized search output incrementally.
3865
3904
  * The `lite` search type does not support streaming.
3905
+ *
3906
+ * A search without `outputSchema` has no text to stream. Its stream yields
3907
+ * one chunk with the results as `citations`, then the warnings chunk when
3908
+ * the API returns warnings. Call `search()` for every field of the results.
3866
3909
  * @param {RequestOptions} [requestOptions] - Per-call request options (`signal`, `timeout`, `body`, `headers`, `query`).
3867
3910
  */
3868
3911
  streamSearch(query, options, requestOptions) {
@@ -4080,6 +4123,12 @@ var Exa2 = class {
4080
4123
  throw await exaErrorFromResponse(response, endpoint);
4081
4124
  }
4082
4125
  const requestIdHeader = response.headers.get("x-request-id");
4126
+ if (isJsonContentType(response.headers.get("content-type"))) {
4127
+ const payload = await readJsonBody(response, endpoint);
4128
+ this.reportWarnings(endpoint, "POST", payload, requestIdHeader);
4129
+ yield* this.jsonResponseChunks(payload);
4130
+ return;
4131
+ }
4083
4132
  const reader = response.body?.getReader();
4084
4133
  if (!reader) {
4085
4134
  throw new ExaError(
@@ -4109,11 +4158,8 @@ var Exa2 = class {
4109
4158
  } catch (err) {
4110
4159
  continue;
4111
4160
  }
4112
- const chunk = this.processChunk(chunkData);
4113
4161
  this.reportWarnings(endpoint, "POST", chunkData, requestIdHeader);
4114
- if (chunk.content || chunk.citations || chunk.warnings) {
4115
- yield chunk;
4116
- }
4162
+ yield* this.payloadChunks(chunkData);
4117
4163
  }
4118
4164
  }
4119
4165
  if (buffer.startsWith("data: ")) {
@@ -4121,11 +4167,8 @@ var Exa2 = class {
4121
4167
  if (leftover && leftover !== "[DONE]") {
4122
4168
  try {
4123
4169
  const chunkData = JSON.parse(leftover);
4124
- const chunk = this.processChunk(chunkData);
4125
4170
  this.reportWarnings(endpoint, "POST", chunkData, requestIdHeader);
4126
- if (chunk.content || chunk.citations || chunk.warnings) {
4127
- yield chunk;
4128
- }
4171
+ yield* this.payloadChunks(chunkData);
4129
4172
  } catch (e) {
4130
4173
  }
4131
4174
  }
@@ -4137,6 +4180,39 @@ var Exa2 = class {
4137
4180
  }
4138
4181
  }
4139
4182
  }
4183
+ /**
4184
+ * The chunks for a response the API sent whole as JSON instead of streaming
4185
+ * it. The first carries its `results` as `citations`. The rest are the
4186
+ * chunks `payloadChunks` makes of the whole payload: its `warnings`, then
4187
+ * its `costDollars`. A chunk with nothing in it is left out, as in a stream.
4188
+ */
4189
+ *jsonResponseChunks(payload) {
4190
+ if (typeof payload !== "object" || payload === null) {
4191
+ return;
4192
+ }
4193
+ const citations = citationsFromResults(
4194
+ payload.results
4195
+ );
4196
+ if (citations) {
4197
+ yield { citations };
4198
+ }
4199
+ yield* this.payloadChunks(payload);
4200
+ }
4201
+ /**
4202
+ * The chunks one stream payload yields: the chunk `processChunk` makes of
4203
+ * it, then its `costDollars` on a chunk of their own. A chunk with nothing
4204
+ * in it is left out.
4205
+ */
4206
+ *payloadChunks(chunkData) {
4207
+ const chunk = this.processChunk(chunkData);
4208
+ if (chunk.content || chunk.citations || chunk.warnings) {
4209
+ yield chunk;
4210
+ }
4211
+ const costDollars = costDollarsOf(chunkData);
4212
+ if (costDollars) {
4213
+ yield { costDollars };
4214
+ }
4215
+ }
4140
4216
  /**
4141
4217
  * Parses one stream payload into a chunk. `warnings` is set only when the
4142
4218
  * payload carries a non-empty `warnings` array, which `/search` sends on its
@@ -4285,4 +4361,3 @@ export {
4285
4361
  createWebSearchTool,
4286
4362
  index_default as default
4287
4363
  };
4288
- //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exa-js",
3
- "version": "2.26.0",
3
+ "version": "2.28.0",
4
4
  "description": "Exa SDK for Node.js and the browser",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,10 +12,18 @@
12
12
  "module": "./dist/index.mjs",
13
13
  "exports": {
14
14
  ".": {
15
- "types": "./dist/index.d.ts",
16
- "require": "./dist/index.js",
17
- "module": "./dist/index.mjs",
18
- "import": "./dist/index.mjs"
15
+ "require": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ },
19
+ "module": {
20
+ "types": "./dist/index.d.mts",
21
+ "default": "./dist/index.mjs"
22
+ },
23
+ "import": {
24
+ "types": "./dist/index.d.mts",
25
+ "default": "./dist/index.mjs"
26
+ }
19
27
  },
20
28
  "./package.json": "./package.json"
21
29
  },
@@ -27,9 +35,8 @@
27
35
  "test:unit": "vitest run --config vitest.unit.config.ts",
28
36
  "test:integration": "vitest run --config vitest.integration.config.ts",
29
37
  "typecheck": "tsc --noEmit",
30
- "typecheck:examples": "tsc -p examples/tsconfig.json --noEmit",
31
38
  "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",
32
- "format": "prettier --write \"src/**/*.ts\" \"examples/**/*.ts\"",
39
+ "format": "prettier --write \"src/**/*.ts\"",
33
40
  "format:websets": "prettier --write src/websets/openapi.ts",
34
41
  "generate-docs": "npx tsx scripts/generate-docs.ts",
35
42
  "build:beta": "cross-env NPM_CONFIG_TAG=beta npm run build",
@@ -42,23 +49,22 @@
42
49
  "license": "MIT",
43
50
  "devDependencies": {
44
51
  "@anthropic-ai/sdk": "^0.115.0",
45
- "@types/node": "~22.14.0",
52
+ "@types/node": "~22.14.1",
46
53
  "cross-env": "~7.0.3",
54
+ "openai": "^6.49.0",
47
55
  "openapi-typescript": "~7.6.1",
48
56
  "prettier": "~3.5.3",
49
57
  "ts-morph": "^27.0.2",
50
58
  "ts-node": "~10.9.2",
51
59
  "tsup": "~8.4.0",
52
- "tsx": "^4.21.0",
60
+ "tsx": "^4.23.15",
53
61
  "typescript": "~5.8.3",
54
- "vitest": "~3.1.1"
62
+ "vitest": "~4.1.12"
55
63
  },
56
64
  "dependencies": {
57
65
  "cross-fetch": "~4.1.0",
58
- "dotenv": "~16.4.7",
59
- "openai": "^5.0.1",
60
- "zod": "^3.22.0",
61
- "zod-to-json-schema": "^3.20.0"
66
+ "zod": "^3.25.28 || ^4.6.6",
67
+ "zod-to-json-schema": "^3.25.2"
62
68
  },
63
69
  "directories": {
64
70
  "test": "test"