UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

75 lines • 3.19 kB
import { classifySdkError, sdkAssistantFailure, sdkResultFailure } from "./errors.js"; import { refusalError } from "./refusal.js"; /** Diagnostic on the assistant message of a request that was retried at the 200k window (senpi#4065). */ export { LONG_CONTEXT_FALLBACK_DIAGNOSTIC } from "../../../provider-notices.js"; export function sdkFailure(message) { const refusal = refusalError(message); if (refusal) return refusal; if (message.type === "assistant") return sdkAssistantFailure(message); if (message.type === "result") return sdkResultFailure(message); return undefined; } export function visibleSdkMessage(message) { if (message.type !== "stream_event") return false; return /^(?:content_block_start|content_block_delta|content_block_stop)$/.test(message.event.type); } /** * Only a request that asked for the legacy `[1m]` window can have that window refused. The same words * on any other request (a native-1M model, which never sends the legacy beta) are an ordinary failure: * never a recorded refusal, a 200k retry, or a rate limit that blocks the account (senpi#3801). */ export function classifyForRequest(error, askedLegacyLongContext) { const classification = classifySdkError(error, askedLegacyLongContext); return classification.kind === "long_context_unavailable" && !askedLegacyLongContext ? { kind: "other", retryable: false } : classification; } /** * The ambient lane has no account failover, so a refused 1M window is retried here: once, and only * before anything visible streamed. Messages pass through as they arrive (system/init, api_retry and * refusals keep their order and timing); the refused attempt is closed before the retry starts, after * the refusal handler prepares the rebuilt options for the standard window (senpi#3408). */ export async function* retryAtStandardWindowOnce(attempt, askedLegacyLongContext, onFallback) { const first = (await attempt())[Symbol.asyncIterator](); let refused; let finished = false; try { let visible = false; for (;;) { const next = await first.next(); if (next.done) { finished = true; return; } const failure = visible ? undefined : sdkFailure(next.value); if (failure !== undefined && classifyForRequest(failure, askedLegacyLongContext()).kind === "long_context_unavailable") { refused = { error: failure }; break; } visible ||= visibleSdkMessage(next.value); yield next.value; } } finally { // Closes the first query on every exit (refusal, consumer return, error) before any retry spawns. if (!finished) { try { await first.return?.(); } catch { // Closing the first query must never replace the error (or the refusal) that ended it. } } } if (!refused) return; onFallback(refused.error); yield* await attempt(); } //# sourceMappingURL=long-context-retry.js.map