exa-js 1.7.1 → 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.js CHANGED
@@ -36,6 +36,7 @@ __export(index_exports, {
36
36
  Exa: () => Exa2,
37
37
  ExaError: () => ExaError,
38
38
  HttpStatusCode: () => HttpStatusCode,
39
+ ResearchClient: () => ResearchClient,
39
40
  ResearchStatus: () => ResearchStatus,
40
41
  WebhookStatus: () => WebhookStatus,
41
42
  WebsetEnrichmentFormat: () => WebsetEnrichmentFormat,
@@ -690,14 +691,76 @@ var WebsetsClient = class extends WebsetsBaseClient {
690
691
  }
691
692
  };
692
693
 
693
- // src/index.ts
694
- var fetchImpl = typeof global !== "undefined" && global.fetch ? global.fetch : import_cross_fetch.default;
695
- var HeadersImpl = typeof global !== "undefined" && global.Headers ? global.Headers : import_cross_fetch.Headers;
694
+ // src/research/base.ts
695
+ var ResearchBaseClient = class {
696
+ /**
697
+ * Initialize a new Research base client
698
+ * @param client The Exa client instance
699
+ */
700
+ constructor(client) {
701
+ this.client = client;
702
+ }
703
+ /**
704
+ * Make a request to the Research API (prefixes all paths with `/research`).
705
+ * @param endpoint The endpoint path, beginning with a slash (e.g. "/tasks").
706
+ * @param method The HTTP method. Defaults to "POST".
707
+ * @param data Optional request body
708
+ * @param params Optional query parameters
709
+ * @returns The parsed JSON response
710
+ */
711
+ async request(endpoint, method = "POST", data, params) {
712
+ return this.client.request(`/research${endpoint}`, method, data, params);
713
+ }
714
+ };
715
+
716
+ // src/research/client.ts
717
+ var ResearchClient = class extends ResearchBaseClient {
718
+ constructor(client) {
719
+ super(client);
720
+ }
721
+ /**
722
+ * Create and run a research task (blocking call).
723
+ *
724
+ * Both parameters are required and have fixed shapes:
725
+ * 1. `input`
726
+ * `{ instructions: string }`
727
+ * • `instructions` – High-level guidance that tells the research agent what to do.
728
+ * 2. `output`
729
+ * defines the exact structure you expect back, and guides the research conducted by the agent.
730
+ * `{ schema: JSONSchema }`.
731
+ * The agent's response will be validated against this schema.
732
+ *
733
+ * @param input Object containing high-level research instructions.
734
+ * @param output Object containing the expected output schema.
735
+ * @returns The ResearchTaskResponse returned by the API.
736
+ */
737
+ async createTask(input, output) {
738
+ return this.request("/tasks", "POST", {
739
+ input,
740
+ output
741
+ });
742
+ }
743
+ /**
744
+ * Retrieve a research task by ID.
745
+ *
746
+ * Not yet implemented server-side. Calling this will throw until the API is
747
+ * available.
748
+ */
749
+ async getTask() {
750
+ throw new Error("getTask is not implemented yet.");
751
+ }
752
+ };
753
+
754
+ // src/research/types.ts
696
755
  var ResearchStatus = /* @__PURE__ */ ((ResearchStatus2) => {
697
756
  ResearchStatus2["completed"] = "completed";
698
757
  ResearchStatus2["failed"] = "failed";
699
758
  return ResearchStatus2;
700
759
  })(ResearchStatus || {});
760
+
761
+ // src/index.ts
762
+ var fetchImpl = typeof global !== "undefined" && global.fetch ? global.fetch : import_cross_fetch.default;
763
+ var HeadersImpl = typeof global !== "undefined" && global.Headers ? global.Headers : import_cross_fetch.Headers;
701
764
  var Exa2 = class {
702
765
  /**
703
766
  * Helper method to separate out the contents-specific options from the rest.
@@ -755,6 +818,7 @@ var Exa2 = class {
755
818
  "User-Agent": "exa-node 1.4.0"
756
819
  });
757
820
  this.websets = new WebsetsClient(this);
821
+ this.research = new ResearchClient(this);
758
822
  }
759
823
  /**
760
824
  * Makes a request to the Exa API.
@@ -804,6 +868,10 @@ var Exa2 = class {
804
868
  errorData.path
805
869
  );
806
870
  }
871
+ const contentType = response.headers.get("content-type") || "";
872
+ if (contentType.includes("text/event-stream")) {
873
+ return await this.parseSSEStream(response);
874
+ }
807
875
  return await response.json();
808
876
  }
809
877
  /**
@@ -1027,47 +1095,76 @@ var Exa2 = class {
1027
1095
  }
1028
1096
  return { content, citations };
1029
1097
  }
1030
- /**
1031
- * Creates and runs a research task in a blocking manner.
1032
- *
1033
- * Both parameters are required and have fixed shapes:
1034
- * 1. `input`
1035
- * `{ instructions: string }`
1036
- * • `instructions` High-level guidance that tells the research agent what to do.
1037
- * 2. `output`
1038
- * defines the exact structure you expect back, and guides the research conducted by the agent.
1039
- * `{ schema: JSONSchema }`.
1040
- * The agent’s response will be validated against this schema.
1041
- *
1042
- * @param {{ instructions: string }} input The research prompt.
1043
- * @param {{ schema: JSONSchema }} output The desired output schema.
1044
- * @returns {Promise<ResearchTaskResponse>} The research response.
1045
- *
1046
- * @example
1047
- * const response = await exa.researchTask(
1048
- * { instructions: "I need a few key facts about honey pot ants." },
1049
- * {
1050
- * schema: {
1051
- * type: "object",
1052
- * required: ["scientificName", "primaryRegions"],
1053
- * properties: {
1054
- * scientificName: { type: "string" },
1055
- * primaryRegions: { type: "string" },
1056
- * },
1057
- * },
1058
- * },
1059
- * );
1060
- */
1061
- async researchTask(input, output) {
1062
- const body = {
1063
- input,
1064
- output
1065
- };
1066
- return await this.request(
1067
- "/research/tasks",
1068
- "POST",
1069
- body
1070
- );
1098
+ async parseSSEStream(response) {
1099
+ const reader = response.body?.getReader();
1100
+ if (!reader) {
1101
+ throw new ExaError(
1102
+ "No response body available for streaming.",
1103
+ 500,
1104
+ (/* @__PURE__ */ new Date()).toISOString()
1105
+ );
1106
+ }
1107
+ const decoder = new TextDecoder();
1108
+ let buffer = "";
1109
+ return new Promise(async (resolve, reject) => {
1110
+ try {
1111
+ while (true) {
1112
+ const { done, value } = await reader.read();
1113
+ if (done) break;
1114
+ buffer += decoder.decode(value, { stream: true });
1115
+ const lines = buffer.split("\n");
1116
+ buffer = lines.pop() || "";
1117
+ for (const line of lines) {
1118
+ if (!line.startsWith("data: ")) continue;
1119
+ const jsonStr = line.replace(/^data:\s*/, "").trim();
1120
+ if (!jsonStr || jsonStr === "[DONE]") {
1121
+ continue;
1122
+ }
1123
+ let chunk;
1124
+ try {
1125
+ chunk = JSON.parse(jsonStr);
1126
+ } catch {
1127
+ continue;
1128
+ }
1129
+ switch (chunk.tag) {
1130
+ case "complete":
1131
+ reader.releaseLock();
1132
+ resolve(chunk.data);
1133
+ return;
1134
+ case "error": {
1135
+ const message = chunk.error?.message || "Unknown error";
1136
+ reader.releaseLock();
1137
+ reject(
1138
+ new ExaError(
1139
+ message,
1140
+ 500 /* InternalServerError */,
1141
+ (/* @__PURE__ */ new Date()).toISOString()
1142
+ )
1143
+ );
1144
+ return;
1145
+ }
1146
+ // 'progress' and any other tags are ignored for the blocking variant
1147
+ default:
1148
+ break;
1149
+ }
1150
+ }
1151
+ }
1152
+ reject(
1153
+ new ExaError(
1154
+ "Stream ended without a completion event.",
1155
+ 500 /* InternalServerError */,
1156
+ (/* @__PURE__ */ new Date()).toISOString()
1157
+ )
1158
+ );
1159
+ } catch (err) {
1160
+ reject(err);
1161
+ } finally {
1162
+ try {
1163
+ reader.releaseLock();
1164
+ } catch {
1165
+ }
1166
+ }
1167
+ });
1071
1168
  }
1072
1169
  };
1073
1170
  var index_default = Exa2;
@@ -1079,6 +1176,7 @@ var index_default = Exa2;
1079
1176
  Exa,
1080
1177
  ExaError,
1081
1178
  HttpStatusCode,
1179
+ ResearchClient,
1082
1180
  ResearchStatus,
1083
1181
  WebhookStatus,
1084
1182
  WebsetEnrichmentFormat,