exa-js 1.7.0 → 1.7.2

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
@@ -636,14 +636,76 @@ var WebsetsClient = class extends WebsetsBaseClient {
636
636
  }
637
637
  };
638
638
 
639
- // src/index.ts
640
- var fetchImpl = typeof global !== "undefined" && global.fetch ? global.fetch : fetch;
641
- var HeadersImpl = typeof global !== "undefined" && global.Headers ? global.Headers : Headers;
639
+ // src/research/base.ts
640
+ var ResearchBaseClient = class {
641
+ /**
642
+ * Initialize a new Research base client
643
+ * @param client The Exa client instance
644
+ */
645
+ constructor(client) {
646
+ this.client = client;
647
+ }
648
+ /**
649
+ * Make a request to the Research API (prefixes all paths with `/research`).
650
+ * @param endpoint The endpoint path, beginning with a slash (e.g. "/tasks").
651
+ * @param method The HTTP method. Defaults to "POST".
652
+ * @param data Optional request body
653
+ * @param params Optional query parameters
654
+ * @returns The parsed JSON response
655
+ */
656
+ async request(endpoint, method = "POST", data, params) {
657
+ return this.client.request(`/research${endpoint}`, method, data, params);
658
+ }
659
+ };
660
+
661
+ // src/research/client.ts
662
+ var ResearchClient = class extends ResearchBaseClient {
663
+ constructor(client) {
664
+ super(client);
665
+ }
666
+ /**
667
+ * Create and run a research task (blocking call).
668
+ *
669
+ * Both parameters are required and have fixed shapes:
670
+ * 1. `input`
671
+ * `{ instructions: string }`
672
+ * • `instructions` – High-level guidance that tells the research agent what to do.
673
+ * 2. `output`
674
+ * defines the exact structure you expect back, and guides the research conducted by the agent.
675
+ * `{ schema: JSONSchema }`.
676
+ * The agent's response will be validated against this schema.
677
+ *
678
+ * @param input Object containing high-level research instructions.
679
+ * @param output Object containing the expected output schema.
680
+ * @returns The ResearchTaskResponse returned by the API.
681
+ */
682
+ async createTask(input, output) {
683
+ return this.request("/tasks", "POST", {
684
+ input,
685
+ output
686
+ });
687
+ }
688
+ /**
689
+ * Retrieve a research task by ID.
690
+ *
691
+ * Not yet implemented server-side. Calling this will throw until the API is
692
+ * available.
693
+ */
694
+ async getTask() {
695
+ throw new Error("getTask is not implemented yet.");
696
+ }
697
+ };
698
+
699
+ // src/research/types.ts
642
700
  var ResearchStatus = /* @__PURE__ */ ((ResearchStatus2) => {
643
701
  ResearchStatus2["completed"] = "completed";
644
702
  ResearchStatus2["failed"] = "failed";
645
703
  return ResearchStatus2;
646
704
  })(ResearchStatus || {});
705
+
706
+ // src/index.ts
707
+ var fetchImpl = typeof global !== "undefined" && global.fetch ? global.fetch : fetch;
708
+ var HeadersImpl = typeof global !== "undefined" && global.Headers ? global.Headers : Headers;
647
709
  var Exa2 = class {
648
710
  /**
649
711
  * Helper method to separate out the contents-specific options from the rest.
@@ -701,6 +763,7 @@ var Exa2 = class {
701
763
  "User-Agent": "exa-node 1.4.0"
702
764
  });
703
765
  this.websets = new WebsetsClient(this);
766
+ this.research = new ResearchClient(this);
704
767
  }
705
768
  /**
706
769
  * Makes a request to the Exa API.
@@ -750,6 +813,10 @@ var Exa2 = class {
750
813
  errorData.path
751
814
  );
752
815
  }
816
+ const contentType = response.headers.get("content-type") || "";
817
+ if (contentType.includes("text/event-stream")) {
818
+ return await this.parseSSEStream(response);
819
+ }
753
820
  return await response.json();
754
821
  }
755
822
  /**
@@ -861,7 +928,8 @@ var Exa2 = class {
861
928
  stream: false,
862
929
  text: options?.text ?? false,
863
930
  model: options?.model ?? "exa",
864
- systemPrompt: options?.systemPrompt
931
+ systemPrompt: options?.systemPrompt,
932
+ outputSchema: options?.outputSchema
865
933
  };
866
934
  return await this.request("/answer", "POST", requestBody);
867
935
  }
@@ -890,7 +958,8 @@ var Exa2 = class {
890
958
  text: options?.text ?? false,
891
959
  stream: true,
892
960
  model: options?.model ?? "exa",
893
- systemPrompt: options?.systemPrompt
961
+ systemPrompt: options?.systemPrompt,
962
+ outputSchema: options?.outputSchema
894
963
  };
895
964
  const response = await fetchImpl(this.baseURL + "/answer", {
896
965
  method: "POST",
@@ -971,47 +1040,76 @@ var Exa2 = class {
971
1040
  }
972
1041
  return { content, citations };
973
1042
  }
974
- /**
975
- * Creates and runs a research task in a blocking manner.
976
- *
977
- * Both parameters are required and have fixed shapes:
978
- * 1. `input`
979
- * `{ instructions: string }`
980
- * • `instructions` High-level guidance that tells the research agent what to do.
981
- * 2. `output`
982
- * defines the exact structure you expect back, and guides the research conducted by the agent.
983
- * `{ schema: JSONSchema }`.
984
- * The agent’s response will be validated against this schema.
985
- *
986
- * @param {{ instructions: string }} input The research prompt.
987
- * @param {{ schema: JSONSchema }} output The desired output schema.
988
- * @returns {Promise<ResearchTaskResponse>} The research response.
989
- *
990
- * @example
991
- * const response = await exa.researchTask(
992
- * { instructions: "I need a few key facts about honey pot ants." },
993
- * {
994
- * schema: {
995
- * type: "object",
996
- * required: ["scientificName", "primaryRegions"],
997
- * properties: {
998
- * scientificName: { type: "string" },
999
- * primaryRegions: { type: "string" },
1000
- * },
1001
- * },
1002
- * },
1003
- * );
1004
- */
1005
- async researchTask(input, output) {
1006
- const body = {
1007
- input,
1008
- output
1009
- };
1010
- return await this.request(
1011
- "/research/tasks",
1012
- "POST",
1013
- body
1014
- );
1043
+ async parseSSEStream(response) {
1044
+ const reader = response.body?.getReader();
1045
+ if (!reader) {
1046
+ throw new ExaError(
1047
+ "No response body available for streaming.",
1048
+ 500,
1049
+ (/* @__PURE__ */ new Date()).toISOString()
1050
+ );
1051
+ }
1052
+ const decoder = new TextDecoder();
1053
+ let buffer = "";
1054
+ return new Promise(async (resolve, reject) => {
1055
+ try {
1056
+ while (true) {
1057
+ const { done, value } = await reader.read();
1058
+ if (done) break;
1059
+ buffer += decoder.decode(value, { stream: true });
1060
+ const lines = buffer.split("\n");
1061
+ buffer = lines.pop() || "";
1062
+ for (const line of lines) {
1063
+ if (!line.startsWith("data: ")) continue;
1064
+ const jsonStr = line.replace(/^data:\s*/, "").trim();
1065
+ if (!jsonStr || jsonStr === "[DONE]") {
1066
+ continue;
1067
+ }
1068
+ let chunk;
1069
+ try {
1070
+ chunk = JSON.parse(jsonStr);
1071
+ } catch {
1072
+ continue;
1073
+ }
1074
+ switch (chunk.tag) {
1075
+ case "complete":
1076
+ reader.releaseLock();
1077
+ resolve(chunk.data);
1078
+ return;
1079
+ case "error": {
1080
+ const message = chunk.error?.message || "Unknown error";
1081
+ reader.releaseLock();
1082
+ reject(
1083
+ new ExaError(
1084
+ message,
1085
+ 500 /* InternalServerError */,
1086
+ (/* @__PURE__ */ new Date()).toISOString()
1087
+ )
1088
+ );
1089
+ return;
1090
+ }
1091
+ // 'progress' and any other tags are ignored for the blocking variant
1092
+ default:
1093
+ break;
1094
+ }
1095
+ }
1096
+ }
1097
+ reject(
1098
+ new ExaError(
1099
+ "Stream ended without a completion event.",
1100
+ 500 /* InternalServerError */,
1101
+ (/* @__PURE__ */ new Date()).toISOString()
1102
+ )
1103
+ );
1104
+ } catch (err) {
1105
+ reject(err);
1106
+ } finally {
1107
+ try {
1108
+ reader.releaseLock();
1109
+ } catch {
1110
+ }
1111
+ }
1112
+ });
1015
1113
  }
1016
1114
  };
1017
1115
  var index_default = Exa2;
@@ -1022,6 +1120,7 @@ export {
1022
1120
  Exa2 as Exa,
1023
1121
  ExaError,
1024
1122
  HttpStatusCode,
1123
+ ResearchClient,
1025
1124
  ResearchStatus,
1026
1125
  WebhookStatus,
1027
1126
  WebsetEnrichmentFormat,