@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
48 lines • 2.92 kB
JavaScript
import { describeProviderFailureForUser, isRetryableErrorMessage, stripTurnRetrySuppressionPrefix, } from "@earendil-works/pi-ai";
import { usageLimitScope } from "./retry-fallback/usage-limit.js";
const LEAD = {
compaction: "Compaction failed",
"auto-compaction": "Auto-compaction failed",
"overflow-recovery": "Context overflow recovery failed",
};
const STEP_TEXT = {
resend: "The conversation is larger than the model's context, and compacting it hit a temporary provider error. Send the message again; if it keeps failing, switch to a larger-context model.",
"larger-context": "The conversation is larger than the model's context, and compacting it did not finish. Start a new session, or switch to a larger-context model.",
"nothing-to-compact": "There is nothing older to summarize yet, so the context was left as it is. No action is needed.",
"compact-again": "The summary of the earlier conversation could not be written. Compact again; if it keeps failing, start a new session or choose another model.",
};
/** The two refusals core raises before any summary request; both already say what happened. */
const NOTHING_TO_COMPACT = /^(?:Nothing to compact|Already compacted)\b/;
/** Rejection and cancellation texts that already name their own next step. */
const SELF_EXPLAINED = /^Compaction (?:rejected|cancelled)\b/;
function providerText(cause, model) {
return describeProviderFailureForUser(cause, { model, usageLimit: usageLimitScope(cause) });
}
/** Which step a failure names: a transient provider error during overflow recovery is worth resending (senpi#4329). */
export function compactionFailureStep(kind, cause, options = {}) {
if (SELF_EXPLAINED.test(cause))
return "self-explained";
if (providerText(cause, options.model) !== undefined)
return "provider";
if (kind === "overflow-recovery")
return isRetryableErrorMessage(cause) ? "resend" : "larger-context";
if (NOTHING_TO_COMPACT.test(cause))
return "nothing-to-compact";
return "compact-again";
}
/**
* The `compaction_end` failure text: the lead, the cause class with one next step that works on every
* surface, then the raw cause as a diagnostic (senpi#4329). The raw text stays in the message so
* consumers that read it (the TUI's transient-failure banner, overflow retry) see what they saw before.
*/
export function formatCompactionFailure(kind, error, options = {}) {
const cause = stripTurnRetrySuppressionPrefix(error).trim();
if (cause.length === 0)
return `${LEAD[kind]}.`;
const step = compactionFailureStep(kind, cause, options);
if (step === "self-explained")
return `${LEAD[kind]}: ${cause}`;
const explanation = step === "provider" ? providerText(cause, options.model) : STEP_TEXT[step];
return `${LEAD[kind]}: ${explanation} Cause: ${cause}`;
}
//# sourceMappingURL=compaction-failure-text.js.map