@agentionai/agents 1.8.0-beta-0 → 1.8.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.
|
@@ -331,10 +331,11 @@ class OpenRouterAgent extends BaseAgent_1.BaseAgent {
|
|
|
331
331
|
return error;
|
|
332
332
|
const err = error;
|
|
333
333
|
if (typeof err?.statusCode === "number") {
|
|
334
|
+
const message = unwrapOpenRouterMessage(parseOpenRouterErrorBody(err.body), err.message ?? "Unknown error");
|
|
334
335
|
if (err.statusCode === 429) {
|
|
335
|
-
return this.rateLimitError(err);
|
|
336
|
+
return this.rateLimitError(err, message);
|
|
336
337
|
}
|
|
337
|
-
return new AgentError_1.ApiError(`OpenRouter API error: ${
|
|
338
|
+
return new AgentError_1.ApiError(`OpenRouter API error: ${message}`, err.statusCode, error);
|
|
338
339
|
}
|
|
339
340
|
return new AgentError_1.ExecutionError(`OpenRouter error: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
340
341
|
}
|
|
@@ -344,7 +345,7 @@ class OpenRouterAgent extends BaseAgent_1.BaseAgent {
|
|
|
344
345
|
* a 429 passed through from an upstream provider carries neither, which is why
|
|
345
346
|
* every field is optional.
|
|
346
347
|
*/
|
|
347
|
-
rateLimitError(err) {
|
|
348
|
+
rateLimitError(err, message) {
|
|
348
349
|
const headers = err.headers;
|
|
349
350
|
const num = (name) => {
|
|
350
351
|
const raw = headers?.get(name);
|
|
@@ -358,7 +359,7 @@ class OpenRouterAgent extends BaseAgent_1.BaseAgent {
|
|
|
358
359
|
// legal and costs one branch to accept.
|
|
359
360
|
const retryAfterRaw = headers?.get("retry-after");
|
|
360
361
|
const retryAfterMs = parseRetryAfter(retryAfterRaw);
|
|
361
|
-
return new AgentError_1.RateLimitError(`OpenRouter rate limit: ${
|
|
362
|
+
return new AgentError_1.RateLimitError(`OpenRouter rate limit: ${message}`, retryAfterMs, num("x-ratelimit-limit"), num("x-ratelimit-remaining"), parseResetAt(num("x-ratelimit-reset")), err);
|
|
362
363
|
}
|
|
363
364
|
closeViz(name, message, throttled) {
|
|
364
365
|
if (!this.vizEventId)
|
|
@@ -548,7 +549,7 @@ class OpenRouterAgent extends BaseAgent_1.BaseAgent {
|
|
|
548
549
|
// turn — writing partial text to history and emitting DONE.
|
|
549
550
|
(0, cancellation_1.throwIfAborted)(options?.signal, `Execution of agent ${this.getName()}`);
|
|
550
551
|
if (streamError) {
|
|
551
|
-
throw new AgentError_1.ApiError(`OpenRouter stream error: ${streamError.message ?? "no message"}`, streamError.code, streamError);
|
|
552
|
+
throw new AgentError_1.ApiError(`OpenRouter stream error: ${unwrapOpenRouterMessage(streamError, streamError.message ?? "no message")}`, streamError.code, streamError);
|
|
552
553
|
}
|
|
553
554
|
if (finishReason === "length") {
|
|
554
555
|
const error = new AgentError_1.MaxTokensExceededError("Response exceeded maximum token limit", this.config.maxTokens);
|
|
@@ -708,4 +709,46 @@ function parseResetAt(value) {
|
|
|
708
709
|
return new Date(value * 1000);
|
|
709
710
|
return new Date(value);
|
|
710
711
|
}
|
|
712
|
+
/**
|
|
713
|
+
* Parse an OpenRouter HTTP error body (`OpenRouterError.body`) down to its
|
|
714
|
+
* `error` field. Returns `undefined` for a missing or non-JSON body rather
|
|
715
|
+
* than throwing, since a malformed body is itself just something to fall back
|
|
716
|
+
* from, not a reason to lose the original error.
|
|
717
|
+
*/
|
|
718
|
+
function parseOpenRouterErrorBody(body) {
|
|
719
|
+
if (!body)
|
|
720
|
+
return undefined;
|
|
721
|
+
try {
|
|
722
|
+
return JSON.parse(body)?.error;
|
|
723
|
+
}
|
|
724
|
+
catch {
|
|
725
|
+
return undefined;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Best-effort extraction of the most specific message an OpenRouter error
|
|
730
|
+
* payload carries, unwrapping `metadata.raw` when present. `raw` holds the
|
|
731
|
+
* upstream provider's exact error text (e.g. OpenAI's own `{error: {message}}`
|
|
732
|
+
* shape) — falls back to the payload's own `message`, then to `fallback`, so an
|
|
733
|
+
* unfamiliar or non-JSON `raw` still yields something rather than throwing.
|
|
734
|
+
*/
|
|
735
|
+
function unwrapOpenRouterMessage(payload, fallback) {
|
|
736
|
+
const topMessage = payload?.message ?? fallback;
|
|
737
|
+
const raw = payload?.metadata?.raw;
|
|
738
|
+
if (typeof raw !== "string" || !raw)
|
|
739
|
+
return topMessage;
|
|
740
|
+
try {
|
|
741
|
+
const parsedRaw = JSON.parse(raw);
|
|
742
|
+
const upstreamMessage = parsedRaw?.error?.message ?? parsedRaw?.message;
|
|
743
|
+
if (typeof upstreamMessage === "string" && upstreamMessage)
|
|
744
|
+
return upstreamMessage;
|
|
745
|
+
}
|
|
746
|
+
catch {
|
|
747
|
+
// Not JSON — some upstreams return plain text. Use it directly if short
|
|
748
|
+
// enough to be a message rather than a stack trace or HTML error page.
|
|
749
|
+
if (raw.length < 500)
|
|
750
|
+
return raw;
|
|
751
|
+
}
|
|
752
|
+
return topMessage;
|
|
753
|
+
}
|
|
711
754
|
//# sourceMappingURL=OpenRouterAgent.js.map
|