exa-js 1.8.26 → 1.9.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.mjs CHANGED
@@ -48,8 +48,14 @@ var WebsetsBaseClient = class {
48
48
  * @returns The response JSON
49
49
  * @throws ExaError with API error details if the request fails
50
50
  */
51
- async request(endpoint, method = "POST", data, params) {
52
- return this.client.request(`/websets${endpoint}`, method, data, params);
51
+ async request(endpoint, method = "POST", data, params, headers) {
52
+ return this.client.request(
53
+ `/websets${endpoint}`,
54
+ method,
55
+ data,
56
+ params,
57
+ headers
58
+ );
53
59
  }
54
60
  /**
55
61
  * Helper to build pagination parameters
@@ -658,11 +664,13 @@ var WebsetSearchesClient = class extends WebsetsBaseClient {
658
664
  * @param params The search parameters
659
665
  * @returns The created Webset Search
660
666
  */
661
- async create(websetId, params) {
667
+ async create(websetId, params, options) {
662
668
  return this.request(
663
669
  `/v0/websets/${websetId}/searches`,
664
670
  "POST",
665
- params
671
+ params,
672
+ void 0,
673
+ options?.headers
666
674
  );
667
675
  }
668
676
  /**
@@ -849,8 +857,14 @@ var WebsetsClient = class extends WebsetsBaseClient {
849
857
  * @param params The Webset creation parameters
850
858
  * @returns The created Webset
851
859
  */
852
- async create(params) {
853
- return this.request("/v0/websets", "POST", params);
860
+ async create(params, options) {
861
+ return this.request(
862
+ "/v0/websets",
863
+ "POST",
864
+ params,
865
+ void 0,
866
+ options?.headers
867
+ );
854
868
  }
855
869
  /**
856
870
  * Preview a webset
@@ -858,7 +872,11 @@ var WebsetsClient = class extends WebsetsBaseClient {
858
872
  * @returns The preview response showing how the query will be decomposed
859
873
  */
860
874
  async preview(params) {
861
- return this.request("/v0/websets/preview", "POST", params);
875
+ return this.request(
876
+ "/v0/websets/preview",
877
+ "POST",
878
+ params
879
+ );
862
880
  }
863
881
  /**
864
882
  * Get a Webset by ID
@@ -1001,50 +1019,25 @@ function zodToJsonSchema(schema) {
1001
1019
 
1002
1020
  // src/research/base.ts
1003
1021
  var ResearchBaseClient = class {
1004
- /**
1005
- * Initialize a new Research base client
1006
- * @param client The Exa client instance
1007
- */
1008
1022
  constructor(client) {
1009
1023
  this.client = client;
1010
1024
  }
1011
- /**
1012
- * Make a request to the Research API (prefixes all paths with `/research`).
1013
- * @param endpoint The endpoint path, beginning with a slash (e.g. "/tasks").
1014
- * @param method The HTTP method. Defaults to "POST".
1015
- * @param data Optional request body
1016
- * @param params Optional query parameters
1017
- * @returns The parsed JSON response
1018
- */
1019
1025
  async request(endpoint, method = "POST", data, params) {
1020
1026
  return this.client.request(
1021
- `/research/v0${endpoint}`,
1027
+ `/research/v1${endpoint}`,
1022
1028
  method,
1023
1029
  data,
1024
1030
  params
1025
1031
  );
1026
1032
  }
1027
- /**
1028
- * Make a request to the Research API (prefixes all paths with `/research`).
1029
- * @param endpoint The endpoint path, beginning with a slash (e.g. "/tasks").
1030
- * @param method The HTTP method. Defaults to "POST".
1031
- * @param data Optional request body
1032
- * @param params Optional query parameters
1033
- * @returns The parsed JSON response
1034
- */
1035
1033
  async rawRequest(endpoint, method = "POST", data, params) {
1036
1034
  return this.client.rawRequest(
1037
- `/research/v0${endpoint}`,
1035
+ `/research/v1${endpoint}`,
1038
1036
  method,
1039
1037
  data,
1040
1038
  params
1041
1039
  );
1042
1040
  }
1043
- /**
1044
- * Helper to build pagination parameters.
1045
- * @param pagination The pagination parameters
1046
- * @returns QueryParams object with pagination parameters
1047
- */
1048
1041
  buildPaginationParams(pagination) {
1049
1042
  const params = {};
1050
1043
  if (!pagination) return params;
@@ -1059,26 +1052,34 @@ var ResearchClient = class extends ResearchBaseClient {
1059
1052
  constructor(client) {
1060
1053
  super(client);
1061
1054
  }
1062
- async createTask(params) {
1063
- const { instructions, model, output } = params;
1064
- let schema = output?.schema;
1055
+ async create(params) {
1056
+ const { instructions, model, outputSchema } = params;
1057
+ let schema = outputSchema;
1065
1058
  if (schema && isZodSchema(schema)) {
1066
1059
  schema = zodToJsonSchema(schema);
1067
1060
  }
1068
1061
  const payload = {
1069
1062
  instructions,
1070
- model: model ?? "exa-research",
1071
- output: output ? {
1072
- schema,
1073
- inferSchema: output.inferSchema ?? true
1074
- } : { inferSchema: true }
1063
+ model: model ?? "exa-research"
1075
1064
  };
1076
- return this.request("/tasks", "POST", payload);
1065
+ if (schema) {
1066
+ payload.outputSchema = schema;
1067
+ }
1068
+ return this.request("", "POST", payload);
1077
1069
  }
1078
- getTask(id, options) {
1070
+ get(researchId, options) {
1079
1071
  if (options?.stream) {
1080
1072
  const promise = async () => {
1081
- const resp = await this.rawRequest(`/tasks/${id}?stream=true`, "GET");
1073
+ const params = { stream: "true" };
1074
+ if (options.events !== void 0) {
1075
+ params.events = options.events.toString();
1076
+ }
1077
+ const resp = await this.rawRequest(
1078
+ `/${researchId}`,
1079
+ "GET",
1080
+ void 0,
1081
+ params
1082
+ );
1082
1083
  if (!resp.body) {
1083
1084
  throw new Error("No response body for SSE stream");
1084
1085
  }
@@ -1122,59 +1123,53 @@ var ResearchClient = class extends ResearchBaseClient {
1122
1123
  };
1123
1124
  return promise();
1124
1125
  } else {
1125
- return this.request(`/tasks/${id}`, "GET");
1126
+ const params = { stream: "false" };
1127
+ if (options?.events !== void 0) {
1128
+ params.events = options.events.toString();
1129
+ }
1130
+ return this.request(
1131
+ `/${researchId}`,
1132
+ "GET",
1133
+ void 0,
1134
+ params
1135
+ );
1126
1136
  }
1127
1137
  }
1128
- /**
1129
- * @deprecated This method is deprecated and may be removed in a future release.
1130
- * @see getTask(id, {stream: true})
1131
- * Poll a research task until completion or failure.
1132
- * Polls every 1 second with a maximum timeout of 10 minutes.
1133
- * Resilient to up to 10 consecutive polling failures.
1134
- */
1135
- async pollTask(id) {
1136
- const pollingInterval = 1e3;
1137
- const maxPollingTime = 10 * 60 * 1e3;
1138
- const maxConsecutiveFailures = 10;
1138
+ async list(options) {
1139
+ const params = this.buildPaginationParams(options);
1140
+ return this.request("", "GET", void 0, params);
1141
+ }
1142
+ async pollUntilFinished(researchId, options) {
1143
+ const pollInterval = options?.pollInterval ?? 1e3;
1144
+ const timeoutMs = options?.timeoutMs ?? 10 * 60 * 1e3;
1145
+ const maxConsecutiveFailures = 5;
1139
1146
  const startTime = Date.now();
1140
1147
  let consecutiveFailures = 0;
1141
1148
  while (true) {
1142
1149
  try {
1143
- const task = await this.request(`/tasks/${id}`, "GET");
1150
+ const research = await this.get(researchId, {
1151
+ events: options?.events
1152
+ });
1144
1153
  consecutiveFailures = 0;
1145
- if (task.status === "completed" || task.status === "failed") {
1146
- return task;
1154
+ if (research.status === "completed" || research.status === "failed" || research.status === "canceled") {
1155
+ return research;
1147
1156
  }
1148
1157
  } catch (err) {
1149
1158
  consecutiveFailures += 1;
1150
1159
  if (consecutiveFailures >= maxConsecutiveFailures) {
1151
1160
  throw new Error(
1152
- `Polling failed ${maxConsecutiveFailures} times in a row for task ${id}: ${err}`
1161
+ `Polling failed ${maxConsecutiveFailures} times in a row for research ${researchId}: ${err}`
1153
1162
  );
1154
1163
  }
1155
1164
  }
1156
- if (Date.now() - startTime > maxPollingTime) {
1165
+ if (Date.now() - startTime > timeoutMs) {
1157
1166
  throw new Error(
1158
- `Polling timeout: Task ${id} did not complete within 10 minutes`
1167
+ `Polling timeout: Research ${researchId} did not complete within ${timeoutMs}ms`
1159
1168
  );
1160
1169
  }
1161
- await new Promise((resolve) => setTimeout(resolve, pollingInterval));
1170
+ await new Promise((resolve) => setTimeout(resolve, pollInterval));
1162
1171
  }
1163
1172
  }
1164
- /**
1165
- * List research tasks
1166
- * @param options Pagination options
1167
- * @returns The paginated list of research tasks
1168
- */
1169
- async listTasks(options) {
1170
- const params = this.buildPaginationParams(options);
1171
- return this.request(
1172
- "/tasks",
1173
- "GET",
1174
- void 0,
1175
- params
1176
- );
1177
- }
1178
1173
  };
1179
1174
 
1180
1175
  // src/index.ts
@@ -1259,7 +1254,7 @@ var Exa2 = class {
1259
1254
  * @returns {Promise<any>} The response from the API.
1260
1255
  * @throws {ExaError} When any API request fails with structured error information
1261
1256
  */
1262
- async request(endpoint, method, body, params) {
1257
+ async request(endpoint, method, body, params, headers) {
1263
1258
  let url = this.baseURL + endpoint;
1264
1259
  if (params && Object.keys(params).length > 0) {
1265
1260
  const searchParams = new URLSearchParams();
@@ -1274,9 +1269,20 @@ var Exa2 = class {
1274
1269
  }
1275
1270
  url += `?${searchParams.toString()}`;
1276
1271
  }
1272
+ let combinedHeaders = {};
1273
+ if (this.headers instanceof HeadersImpl) {
1274
+ this.headers.forEach((value, key) => {
1275
+ combinedHeaders[key] = value;
1276
+ });
1277
+ } else {
1278
+ combinedHeaders = { ...this.headers };
1279
+ }
1280
+ if (headers) {
1281
+ combinedHeaders = { ...combinedHeaders, ...headers };
1282
+ }
1277
1283
  const response = await fetchImpl(url, {
1278
1284
  method,
1279
- headers: this.headers,
1285
+ headers: combinedHeaders,
1280
1286
  body: body ? JSON.stringify(body) : void 0
1281
1287
  });
1282
1288
  if (!response.ok) {
@@ -1613,7 +1619,6 @@ export {
1613
1619
  MonitorRunType,
1614
1620
  MonitorStatus,
1615
1621
  PreviewWebsetResponseEnrichmentsFormat,
1616
- ResearchClient,
1617
1622
  ScopeSourceType,
1618
1623
  UpdateMonitorStatus,
1619
1624
  WebhookStatus,