exa-js 1.8.17 → 1.8.19

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
@@ -953,6 +953,18 @@ var WebsetsClient = class extends WebsetsBaseClient {
953
953
  }
954
954
  };
955
955
 
956
+ // src/zod-utils.ts
957
+ import { ZodType } from "zod";
958
+ import { zodToJsonSchema as convertZodToJsonSchema } from "zod-to-json-schema";
959
+ function isZodSchema(obj) {
960
+ return obj instanceof ZodType;
961
+ }
962
+ function zodToJsonSchema(schema) {
963
+ return convertZodToJsonSchema(schema, {
964
+ $refStrategy: "none"
965
+ });
966
+ }
967
+
956
968
  // src/research/base.ts
957
969
  var ResearchBaseClient = class {
958
970
  /**
@@ -1013,23 +1025,17 @@ var ResearchClient = class extends ResearchBaseClient {
1013
1025
  constructor(client) {
1014
1026
  super(client);
1015
1027
  }
1016
- /**
1017
- * Create a new research task.
1018
- *
1019
- * @param params Object containing:
1020
- * - model: The research model to use (e.g., ResearchModel.ExaResearch).
1021
- * - instructions: High-level guidance for the research agent.
1022
- * - output: An object with a `schema` property (JSONSchema) that defines the expected output structure.
1023
- *
1024
- * @returns An object containing the unique ID of the created research task.
1025
- */
1026
1028
  async createTask(params) {
1027
1029
  const { instructions, model, output } = params;
1030
+ let schema = output?.schema;
1031
+ if (schema && isZodSchema(schema)) {
1032
+ schema = zodToJsonSchema(schema);
1033
+ }
1028
1034
  const payload = {
1029
1035
  instructions,
1030
1036
  model: model ?? "exa-research",
1031
1037
  output: output ? {
1032
- schema: output.schema,
1038
+ schema,
1033
1039
  inferSchema: output.inferSchema ?? true
1034
1040
  } : { inferSchema: true }
1035
1041
  };
@@ -1162,7 +1168,16 @@ var Exa2 = class {
1162
1168
  contentsOptions.text = true;
1163
1169
  }
1164
1170
  if (text !== void 0) contentsOptions.text = text;
1165
- if (summary !== void 0) contentsOptions.summary = summary;
1171
+ if (summary !== void 0) {
1172
+ if (typeof summary === "object" && summary !== null && "schema" in summary && summary.schema && isZodSchema(summary.schema)) {
1173
+ contentsOptions.summary = {
1174
+ ...summary,
1175
+ schema: zodToJsonSchema(summary.schema)
1176
+ };
1177
+ } else {
1178
+ contentsOptions.summary = summary;
1179
+ }
1180
+ }
1166
1181
  if (highlights !== void 0) contentsOptions.highlights = highlights;
1167
1182
  if (subpages !== void 0) contentsOptions.subpages = subpages;
1168
1183
  if (subpageTarget !== void 0)
@@ -1355,28 +1370,6 @@ var Exa2 = class {
1355
1370
  };
1356
1371
  return await this.request("/contents", "POST", payload);
1357
1372
  }
1358
- /**
1359
- * Generate an answer to a query.
1360
- * @param {string} query - The question or query to answer.
1361
- * @param {AnswerOptions} [options] - Additional options for answer generation.
1362
- * @returns {Promise<AnswerResponse>} The generated answer and source references.
1363
- *
1364
- * Example with systemPrompt:
1365
- * ```ts
1366
- * const answer = await exa.answer("What is quantum computing?", {
1367
- * text: true,
1368
- * model: "exa",
1369
- * systemPrompt: "Answer in a technical manner suitable for experts."
1370
- * });
1371
- * ```
1372
- *
1373
- * Note: For streaming responses, use the `streamAnswer` method:
1374
- * ```ts
1375
- * for await (const chunk of exa.streamAnswer(query)) {
1376
- * // Handle chunks
1377
- * }
1378
- * ```
1379
- */
1380
1373
  async answer(query, options) {
1381
1374
  if (options?.stream) {
1382
1375
  throw new ExaError(
@@ -1384,43 +1377,32 @@ var Exa2 = class {
1384
1377
  400 /* BadRequest */
1385
1378
  );
1386
1379
  }
1380
+ let outputSchema = options?.outputSchema;
1381
+ if (outputSchema && isZodSchema(outputSchema)) {
1382
+ outputSchema = zodToJsonSchema(outputSchema);
1383
+ }
1387
1384
  const requestBody = {
1388
1385
  query,
1389
1386
  stream: false,
1390
1387
  text: options?.text ?? false,
1391
1388
  model: options?.model ?? "exa",
1392
1389
  systemPrompt: options?.systemPrompt,
1393
- outputSchema: options?.outputSchema
1390
+ outputSchema
1394
1391
  };
1395
1392
  return await this.request("/answer", "POST", requestBody);
1396
1393
  }
1397
- /**
1398
- * Stream an answer as an async generator
1399
- *
1400
- * Each iteration yields a chunk with partial text (`content`) or new citations.
1401
- * Use this if you'd like to read the answer incrementally, e.g. in a chat UI.
1402
- *
1403
- * Example usage:
1404
- * ```ts
1405
- * for await (const chunk of exa.streamAnswer("What is quantum computing?", {
1406
- * text: false,
1407
- * systemPrompt: "Answer in a concise manner suitable for beginners."
1408
- * })) {
1409
- * if (chunk.content) process.stdout.write(chunk.content);
1410
- * if (chunk.citations) {
1411
- * console.log("\nCitations: ", chunk.citations);
1412
- * }
1413
- * }
1414
- * ```
1415
- */
1416
1394
  async *streamAnswer(query, options) {
1395
+ let outputSchema = options?.outputSchema;
1396
+ if (outputSchema && isZodSchema(outputSchema)) {
1397
+ outputSchema = zodToJsonSchema(outputSchema);
1398
+ }
1417
1399
  const body = {
1418
1400
  query,
1419
1401
  text: options?.text ?? false,
1420
1402
  stream: true,
1421
1403
  model: options?.model ?? "exa",
1422
1404
  systemPrompt: options?.systemPrompt,
1423
- outputSchema: options?.outputSchema
1405
+ outputSchema
1424
1406
  };
1425
1407
  const response = await fetchImpl(this.baseURL + "/answer", {
1426
1408
  method: "POST",