@betterlu/agentlu 1.7.0 → 1.7.1
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.ts +2 -0
- package/dist/index.js +30 -7
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -490,6 +490,8 @@ export declare class AgentLu {
|
|
|
490
490
|
finalText: () => Promise<string>;
|
|
491
491
|
};
|
|
492
492
|
private runForApiKey;
|
|
493
|
+
/** Attempts for a run: the caller's `maxAttempts` or the shared default. */
|
|
494
|
+
private runAttempts;
|
|
493
495
|
private runJsonForApiKey;
|
|
494
496
|
private runJsonOnce;
|
|
495
497
|
private runTextForApiKey;
|
package/dist/index.js
CHANGED
|
@@ -289,14 +289,37 @@ export class AgentLu {
|
|
|
289
289
|
return typedStream(this.stream(opts));
|
|
290
290
|
}
|
|
291
291
|
async runForApiKey(apiKey, opts) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
292
|
+
// A failed run is an HTTP 200 with `error.retryable` in the body, which the
|
|
293
|
+
// transport retry never sees. Retry those here (idempotency keys make it
|
|
294
|
+
// safe), honoring `maxAttempts` like the transport does. The key is minted
|
|
295
|
+
// once per logical run so every attempt replays the same server-side run.
|
|
296
|
+
const maxAttempts = this.runAttempts(opts);
|
|
297
|
+
const idempotencyKey = opts.idempotencyKey ?? randomIdempotencyKey();
|
|
298
|
+
let last;
|
|
299
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
300
|
+
const response = await this.createRunResponse({ ...opts, idempotencyKey }, apiKey);
|
|
301
|
+
if (response.status === "failed" && response.error) {
|
|
302
|
+
const err = new AgentLuError(200, response.error.code, response.error.message, response.error.retryable);
|
|
303
|
+
// A retryable failed run is a transient provider fault; retry it.
|
|
304
|
+
if (response.error.retryable && attempt < maxAttempts - 1) {
|
|
305
|
+
last = err;
|
|
306
|
+
await sleep(retryDelayMs(err, attempt));
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
throw err;
|
|
310
|
+
}
|
|
311
|
+
if (opts.throwOnIncomplete && response.status === "incomplete") {
|
|
312
|
+
throw new AgentLuResponseError(response);
|
|
313
|
+
}
|
|
314
|
+
return { data: response.output_parsed, response };
|
|
298
315
|
}
|
|
299
|
-
|
|
316
|
+
throw last ?? new AgentLuError(200, "upstream_error", "run failed after retries", false);
|
|
317
|
+
}
|
|
318
|
+
/** Attempts for a run: the caller's `maxAttempts` or the shared default. */
|
|
319
|
+
runAttempts(opts) {
|
|
320
|
+
return opts.maxAttempts !== undefined && Number.isFinite(opts.maxAttempts) && opts.maxAttempts >= 1
|
|
321
|
+
? Math.floor(opts.maxAttempts)
|
|
322
|
+
: 5;
|
|
300
323
|
}
|
|
301
324
|
async runJsonForApiKey(apiKey, opts) {
|
|
302
325
|
const attempts = jsonAttempts(opts);
|
package/package.json
CHANGED