UNPKG

@code-yeongyu/senpi

Version:

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

57 lines • 2.83 kB
import { isContextOverflow } from "@earendil-works/pi-ai/compat"; import { isBillingErrorMessage } from "./billing.js"; export const REQUEST_SHAPE_RECOVERY_ENTRY_TYPE = "request-shape-recovery"; export const REQUEST_SHAPE_RECOVERY_NOTE = "Retried without web search replay data"; export const REQUEST_SHAPE_FAILURE_TEXT = "Anthropic rejected this conversation's request format, even after retrying without web search replay data. " + "Run /compact or start a new session to continue, and report it if it happens again."; const INVALID_REQUEST_PATTERN = /\binvalid_request_error\b/; /** Gateways speak the same wire but answer their own 400s; only Anthropic's own rejection is a replay-shape problem. */ const ANTHROPIC_PROVIDER = "anthropic"; /** A request-shape 400 is ours to fix, not the model's: it never walks the fallback chain (senpi#3727). */ export function isAnthropicRequestShapeRejection(message, contextWindow) { return (message.stopReason === "error" && message.api === "anthropic-messages" && message.provider === ANTHROPIC_PROVIDER && INVALID_REQUEST_PATTERN.test(message.errorMessage ?? "") && !isBillingErrorMessage(message.errorMessage) && !isContextOverflow(message, contextWindow)); } /** Only assistant `providerNative` blocks at or before `cutoff` go; user text and client tool pairs never do. */ export function stripProviderNativeReplay(messages, cutoff) { let strippedBlocks = 0; const stripped = messages.map((message) => { if (message.role !== "assistant" || message.timestamp > cutoff) return message; const content = message.content.filter((block) => block.type !== "providerNative"); if (content.length === message.content.length) return message; strippedBlocks += message.content.length - content.length; return { ...message, content }; }); return { messages: stripped, strippedBlocks }; } /** One same-model recovery per failing streak; older history stays stripped because the bad block is still in it. */ export class RequestShapeRecovery { constructor() { this.attempted = false; } decide(message, contextWindow) { if (!isAnthropicRequestShapeRejection(message, contextWindow)) return "not-request-shape"; return this.attempted ? "give-up" : "recover"; } begin(failedAt) { this.attempted = true; this.cutoff = failedAt; } /** Any non-request-shape outcome (success, another error, a new prompt) ends the streak. */ endStreak() { this.attempted = false; } apply(messages) { if (this.cutoff === undefined) return messages; return stripProviderNativeReplay(messages, this.cutoff).messages; } } //# sourceMappingURL=request-shape.js.map