exa-js 1.8.8 → 1.8.9

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
@@ -42,7 +42,7 @@ __export(index_exports, {
42
42
  MonitorRunType: () => MonitorRunType,
43
43
  MonitorStatus: () => MonitorStatus,
44
44
  ResearchClient: () => ResearchClient,
45
- ResearchModel: () => ResearchCreateTaskRequestDtoModel,
45
+ ResearchModel: () => ResearchCreateOpenAIResponseDtoModel,
46
46
  ResearchStatus: () => ResearchTaskDtoStatus,
47
47
  UpdateMonitorStatus: () => UpdateMonitorStatus,
48
48
  WebhookStatus: () => WebhookStatus,
@@ -846,6 +846,22 @@ var ResearchBaseClient = class {
846
846
  params
847
847
  );
848
848
  }
849
+ /**
850
+ * Make a request to the Research API (prefixes all paths with `/research`).
851
+ * @param endpoint The endpoint path, beginning with a slash (e.g. "/tasks").
852
+ * @param method The HTTP method. Defaults to "POST".
853
+ * @param data Optional request body
854
+ * @param params Optional query parameters
855
+ * @returns The parsed JSON response
856
+ */
857
+ async rawRequest(endpoint, method = "POST", data, params) {
858
+ return this.client.rawRequest(
859
+ `/research/v0${endpoint}`,
860
+ method,
861
+ data,
862
+ params
863
+ );
864
+ }
849
865
  /**
850
866
  * Helper to build pagination parameters.
851
867
  * @param pagination The pagination parameters
@@ -860,19 +876,6 @@ var ResearchBaseClient = class {
860
876
  }
861
877
  };
862
878
 
863
- // src/research/openapi.ts
864
- var ResearchCreateTaskRequestDtoModel = /* @__PURE__ */ ((ResearchCreateTaskRequestDtoModel2) => {
865
- ResearchCreateTaskRequestDtoModel2["exa_research"] = "exa-research";
866
- ResearchCreateTaskRequestDtoModel2["exa_research_pro"] = "exa-research-pro";
867
- return ResearchCreateTaskRequestDtoModel2;
868
- })(ResearchCreateTaskRequestDtoModel || {});
869
- var ResearchTaskDtoStatus = /* @__PURE__ */ ((ResearchTaskDtoStatus2) => {
870
- ResearchTaskDtoStatus2["running"] = "running";
871
- ResearchTaskDtoStatus2["completed"] = "completed";
872
- ResearchTaskDtoStatus2["failed"] = "failed";
873
- return ResearchTaskDtoStatus2;
874
- })(ResearchTaskDtoStatus || {});
875
-
876
879
  // src/research/client.ts
877
880
  var ResearchClient = class extends ResearchBaseClient {
878
881
  constructor(client) {
@@ -898,19 +901,61 @@ var ResearchClient = class extends ResearchBaseClient {
898
901
  inferSchema: output.inferSchema ?? true
899
902
  } : { inferSchema: true }
900
903
  };
901
- return this.request(
902
- "/tasks",
903
- "POST",
904
- payload
905
- );
904
+ return this.request("/tasks", "POST", payload);
906
905
  }
907
- /**
908
- * Retrieve a research task by ID.
909
- */
910
- async getTask(id) {
911
- return this.request(`/tasks/${id}`, "GET");
906
+ getTask(id, options) {
907
+ if (options?.stream) {
908
+ const promise = async () => {
909
+ const resp = await this.rawRequest(`/tasks/${id}?stream=true`, "GET");
910
+ if (!resp.body) {
911
+ throw new Error("No response body for SSE stream");
912
+ }
913
+ const reader = resp.body.getReader();
914
+ const decoder = new TextDecoder();
915
+ let buffer = "";
916
+ function processPart(part) {
917
+ const lines = part.split("\n");
918
+ let data = lines.slice(1).join("\n");
919
+ if (data.startsWith("data:")) {
920
+ data = data.slice(5).trimStart();
921
+ }
922
+ try {
923
+ return JSON.parse(data);
924
+ } catch (e) {
925
+ return null;
926
+ }
927
+ }
928
+ async function* streamEvents() {
929
+ while (true) {
930
+ const { done, value } = await reader.read();
931
+ if (done) break;
932
+ buffer += decoder.decode(value, { stream: true });
933
+ let parts = buffer.split("\n\n");
934
+ buffer = parts.pop() ?? "";
935
+ for (const part of parts) {
936
+ const processed = processPart(part);
937
+ if (processed) {
938
+ yield processed;
939
+ }
940
+ }
941
+ }
942
+ if (buffer.trim()) {
943
+ const processed = processPart(buffer.trim());
944
+ if (processed) {
945
+ yield processed;
946
+ }
947
+ }
948
+ }
949
+ return streamEvents();
950
+ };
951
+ return promise();
952
+ } else {
953
+ return this.request(`/tasks/${id}`, "GET");
954
+ }
912
955
  }
913
956
  /**
957
+ * @deprecated This method is deprecated and may be removed in a future release.
958
+ * @see getTask(id, {stream: true})
914
959
  * Poll a research task until completion or failure.
915
960
  * Polls every 1 second with a maximum timeout of 10 minutes.
916
961
  * Resilient to up to 10 consecutive polling failures.
@@ -960,6 +1005,19 @@ var ResearchClient = class extends ResearchBaseClient {
960
1005
  }
961
1006
  };
962
1007
 
1008
+ // src/research/openapi.ts
1009
+ var ResearchCreateOpenAIResponseDtoModel = /* @__PURE__ */ ((ResearchCreateOpenAIResponseDtoModel2) => {
1010
+ ResearchCreateOpenAIResponseDtoModel2["exa_research"] = "exa-research";
1011
+ ResearchCreateOpenAIResponseDtoModel2["exa_research_pro"] = "exa-research-pro";
1012
+ return ResearchCreateOpenAIResponseDtoModel2;
1013
+ })(ResearchCreateOpenAIResponseDtoModel || {});
1014
+ var ResearchTaskDtoStatus = /* @__PURE__ */ ((ResearchTaskDtoStatus2) => {
1015
+ ResearchTaskDtoStatus2["running"] = "running";
1016
+ ResearchTaskDtoStatus2["completed"] = "completed";
1017
+ ResearchTaskDtoStatus2["failed"] = "failed";
1018
+ return ResearchTaskDtoStatus2;
1019
+ })(ResearchTaskDtoStatus || {});
1020
+
963
1021
  // src/index.ts
964
1022
  var fetchImpl = typeof global !== "undefined" && global.fetch ? global.fetch : import_cross_fetch.default;
965
1023
  var HeadersImpl = typeof global !== "undefined" && global.Headers ? global.Headers : import_cross_fetch.Headers;
@@ -1081,6 +1139,28 @@ var Exa2 = class {
1081
1139
  }
1082
1140
  return await response.json();
1083
1141
  }
1142
+ async rawRequest(endpoint, method = "POST", body, queryParams) {
1143
+ let url = this.baseURL + endpoint;
1144
+ if (queryParams) {
1145
+ const searchParams = new URLSearchParams();
1146
+ for (const [key, value] of Object.entries(queryParams)) {
1147
+ if (Array.isArray(value)) {
1148
+ for (const item of value) {
1149
+ searchParams.append(key, String(item));
1150
+ }
1151
+ } else if (value !== void 0) {
1152
+ searchParams.append(key, String(value));
1153
+ }
1154
+ }
1155
+ url += `?${searchParams.toString()}`;
1156
+ }
1157
+ const response = await fetchImpl(url, {
1158
+ method,
1159
+ headers: this.headers,
1160
+ body: body ? JSON.stringify(body) : void 0
1161
+ });
1162
+ return response;
1163
+ }
1084
1164
  /**
1085
1165
  * Performs a search with an Exa prompt-engineered query.
1086
1166
  *