Q69: provider failures classify by status and code; cooldowns follow Retry-After or back off
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4325dfa874
commit
c6a0460035
15 files changed
+198
-41
No files matched your search
@@ -3,7 +3,7 @@
|
||||
* (Codex) and github-copilot turns. Matches Pi's `convertResponsesMessages` /
|
||||
* `convertResponsesTools` and the Responses streaming event shapes.
|
||||
*/
|
||||
import { isRecord, type JsonRecord, type ProviderEventStreamMessage } from "../provider-http";
|
||||
import { isRecord, ProviderHttpError, type JsonRecord, type ProviderEventStreamMessage } from "../provider-http";
|
||||
import { parseArguments, type AgentTurnMessage, type AgentTurnResult, type ToolCall, type ToolDefinition } from "../provider-tool-turn";
|
||||
|
||||
export function responsesToolDefs(tools: ToolDefinition[], strict: boolean | null = false): unknown[] {
|
||||
@@ -66,14 +66,28 @@ function eventType(message: ProviderEventStreamMessage): string | null {
|
||||
return typeof type === "string" ? type : message.eventName;
|
||||
}
|
||||
|
||||
/** Responses error codes that name passing provider trouble, as HTTP statuses for failure classification. */
|
||||
const RESPONSES_ERROR_STATUS: Record<string, number> = { server_error: 500, rate_limit_exceeded: 429 };
|
||||
|
||||
/** The error of an `error` event, or of a `response.failed` event's response. */
|
||||
function streamError(data: JsonRecord): unknown {
|
||||
return data.error ?? (isRecord(data.response) ? data.response.error : undefined);
|
||||
}
|
||||
|
||||
function errorText(data: JsonRecord): string {
|
||||
const error = data.error;
|
||||
const error = streamError(data);
|
||||
if (typeof error === "string") return error;
|
||||
if (isRecord(error) && typeof error.message === "string") return error.message;
|
||||
if (typeof data.message === "string") return data.message;
|
||||
return "Provider stream failed.";
|
||||
}
|
||||
|
||||
function errorStatus(data: JsonRecord): number | null {
|
||||
const error = streamError(data);
|
||||
const code = isRecord(error) ? error.code : data.code;
|
||||
return typeof code === "string" ? RESPONSES_ERROR_STATUS[code] ?? null : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulate a Responses SSE stream (Codex/Copilot) into the final result.
|
||||
*
|
||||
@@ -91,7 +105,7 @@ export async function accumulateResponsesStream(messages: AsyncIterable<Provider
|
||||
raw.push(message.rawData);
|
||||
const type = eventType(message);
|
||||
if (!type) continue;
|
||||
if (type === "error" || type === "response.failed") throw new Error(errorText(message.data));
|
||||
if (type === "error" || type === "response.failed") throw new ProviderHttpError(errorText(message.data), null, errorStatus(message.data));
|
||||
if (type === "response.output_item.done" && isRecord(message.data.item)) {
|
||||
streamed.push(message.data.item);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user