valyu-js 2.8.0 → 2.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.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +53 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -38,7 +38,8 @@ var import_crypto = require("crypto");
|
|
|
38
38
|
var import_http = __toESM(require("http"));
|
|
39
39
|
var import_https = __toESM(require("https"));
|
|
40
40
|
var import_axios = __toESM(require("axios"));
|
|
41
|
-
var SDK_VERSION = "2.
|
|
41
|
+
var SDK_VERSION = "2.9.0";
|
|
42
|
+
var TRANSIENT_STATUS_CODES = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
|
|
42
43
|
function normalizeContentsJobResponse(api) {
|
|
43
44
|
return {
|
|
44
45
|
success: api.success ?? true,
|
|
@@ -823,19 +824,49 @@ var Valyu = class {
|
|
|
823
824
|
/**
|
|
824
825
|
* DeepResearch: Get task status
|
|
825
826
|
*/
|
|
826
|
-
async _deepresearchStatus(taskId) {
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
827
|
+
async _deepresearchStatus(taskId, maxAttempts = 5) {
|
|
828
|
+
const url = `${this.baseUrl}/deepresearch/tasks/${taskId}/status`;
|
|
829
|
+
let lastError = "status endpoint unreachable";
|
|
830
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
831
|
+
try {
|
|
832
|
+
const response = await this.client.get(url, {
|
|
833
|
+
headers: this.headers,
|
|
834
|
+
// Classify status codes ourselves rather than letting axios throw.
|
|
835
|
+
validateStatus: () => true
|
|
836
|
+
});
|
|
837
|
+
if (TRANSIENT_STATUS_CODES.has(response.status)) {
|
|
838
|
+
lastError = `HTTP ${response.status}`;
|
|
839
|
+
} else {
|
|
840
|
+
const contentType = String(
|
|
841
|
+
response.headers?.["content-type"] || ""
|
|
842
|
+
);
|
|
843
|
+
const body = response.data;
|
|
844
|
+
const isJsonObject = contentType.toLowerCase().includes("application/json") && typeof body === "object" && body !== null && !Array.isArray(body);
|
|
845
|
+
if (!isJsonObject) {
|
|
846
|
+
lastError = `non-JSON/empty status response (HTTP ${response.status}, content-type: ${contentType || "none"})`;
|
|
847
|
+
} else if (response.status >= 400) {
|
|
848
|
+
return {
|
|
849
|
+
success: false,
|
|
850
|
+
error: body.error || `HTTP Error: ${response.status}`
|
|
851
|
+
};
|
|
852
|
+
} else {
|
|
853
|
+
const { success: _ignored, ...rest } = body;
|
|
854
|
+
return { success: true, ...rest };
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
} catch (e) {
|
|
858
|
+
lastError = e?.message || String(e);
|
|
859
|
+
}
|
|
860
|
+
if (attempt < maxAttempts - 1) {
|
|
861
|
+
const delayMs = Math.min(2 ** attempt, 30) * 1e3 + Math.random() * 1e3;
|
|
862
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
863
|
+
}
|
|
838
864
|
}
|
|
865
|
+
return {
|
|
866
|
+
success: false,
|
|
867
|
+
unreachable: true,
|
|
868
|
+
error: `Status endpoint unreachable after ${maxAttempts} attempts: ${lastError}`
|
|
869
|
+
};
|
|
839
870
|
}
|
|
840
871
|
/**
|
|
841
872
|
* DeepResearch: Wait for task completion with polling
|
|
@@ -847,6 +878,15 @@ var Valyu = class {
|
|
|
847
878
|
while (true) {
|
|
848
879
|
const status = await this._deepresearchStatus(taskId);
|
|
849
880
|
if (!status.success) {
|
|
881
|
+
if (status.unreachable) {
|
|
882
|
+
if (Date.now() - startTime > maxWaitTime) {
|
|
883
|
+
throw new Error(
|
|
884
|
+
`Status endpoint unreachable for ${maxWaitTime}ms: ${status.error}`
|
|
885
|
+
);
|
|
886
|
+
}
|
|
887
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
850
890
|
throw new Error(status.error);
|
|
851
891
|
}
|
|
852
892
|
if (options.onProgress) {
|