import type { ContextCompactionItem } from "./types"; import { type OpenGeniText } from "../messages/runtime"; /** Headline for a compaction that could not produce a checkpoint. */ export const COMPACTION_FAILURE_TITLE = (t: OpenGeniText) => t("timeline.compactionCopy.couldnTCompactConversationHistory"); /** * Headline for a compaction cut short by its own attempt's cancellation (for * example a worker restarting during a deploy). Not a failure: the recovered * attempt checks again and compacts if the history still needs it. */ export const COMPACTION_INTERRUPTED_TITLE = (t: OpenGeniText) => t("timeline.compactionCopy.compactionInterrupted"); /** * Headline for an automatic idle checkpoint dropped because no subscription * had capacity. Not a failure: the next turn compacts if still needed. */ export const COMPACTION_POSTPONED_TITLE = (t: OpenGeniText) => t("timeline.compactionCopy.compactionPostponed"); /** Headline for a settled `skipped` compaction landmark. */ export function compactionSkipTitle(t: OpenGeniText, reason: string | null): string { if (reason === "interrupted") return COMPACTION_INTERRUPTED_TITLE(t); if (reason === "capacity_unavailable") return COMPACTION_POSTPONED_TITLE(t); return COMPACTION_FAILURE_TITLE(t); } /** Why a definitive provider rejection repeats; shown behind Details. */ export const COMPACTION_REJECTION_NOTE = (t: OpenGeniText) => t("timeline.compactionCopy.theProviderRejectsThisRequest"); /** * Subtitle for a settled compaction landmark. Kept short: a definitive provider * rejection withholds the generic retry promise, names the one recovery step, * and moves its identifiers behind Details ({@link compactionFailureDetails}). */ export function compactionSkipSubtitle( t: OpenGeniText, reason: string | null, providerRejection: ContextCompactionItem["providerRejection"] = null, ): string { switch (reason) { case "no_history": return t("timeline.compactionCopy.noActiveHistoryToCompact"); case "replacement_not_smaller": return t("timeline.compactionCopy.checkpointWouldNotReduceMemory"); case "replacement_exceeds_model_budget": return t("timeline.compactionCopy.checkpointExceedsThisModelS"); case "replacement_unchanged": return t("timeline.compactionCopy.checkpointMadeNoProgress"); case "interrupted": return t("timeline.compactionCopy.itWillRetryIfStill"); case "capacity_unavailable": return t("timeline.compactionCopy.noSubscriptionCapacityRightNow"); case "summarization_failed": return providerRejection ? t("timeline.compactionCopy.chatHistoryIsUnchangedIf") : t("timeline.compactionCopy.requestItAgainToRetry"); default: return t("timeline.compactionCopy.compactionWasNotNeededChat"); } } export type CompactionFailureDetails = { /** One plain sentence on why it failed, when known. */ note: string | null; /** Provider identifiers or the recorded error text, in display order. */ facts: Array<{ label: string; value: string }>; }; /** * Technical diagnostics for a failed compaction landmark, or null when it has * none: a definitive provider rejection lists its content-free identifiers; * any other failure shows the turn's recorded error text. Presentation only. */ export function compactionFailureDetails( t: OpenGeniText, item: Pick, ): CompactionFailureDetails | null { if (item.phase !== "skipped" || item.skipReason !== "summarization_failed") return null; const rejection = item.providerRejection; if (!rejection) { const recorded = item.failureDetail?.trim(); return recorded ? { note: null, facts: [{ label: t("timeline.compactionCopy.error"), value: recorded }] } : null; } const facts = [ { label: t("timeline.compactionCopy.response"), value: [`HTTP ${rejection.httpStatus}`, rejection.type, rejection.code] .filter((part): part is string => Boolean(part)) .join(" ยท "), }, ]; if (rejection.param) facts.push({ label: t("timeline.compactionCopy.parameter"), value: rejection.param }); if (rejection.requestId) facts.push({ label: t("timeline.compactionCopy.requestId"), value: rejection.requestId }); return { note: COMPACTION_REJECTION_NOTE(t), facts }; } const COMPACTION_TURN_FAILURE_PREFIX = "compaction summarization failed:"; /** How a turn's failure reads on its chip and notice; see {@link presentTurnFailureText}. */ export type TurnFailurePresentation = { summary: string; /** The recorded text the summary replaced, offered behind Details. */ detail?: string; /** The summary names a compaction failure, whose own landmark owns the detail. */ compaction?: true; }; /** * Short copy for a turn's failure reason. A compaction failure records the raw * provider text after a fixed prefix, and a diagnostic failure (an unclassified * error or operator text; see `presentFailure`) records text not written for * people. Both name the outcome instead (`diagnosticOutcome` for a diagnostic) * and keep the recorded text as `detail`, behind Details. Copy the worker * wrote for people passes through unchanged. Presentation only: the stored * (and model-facing) event keeps its text. */ export function presentTurnFailureText( t: OpenGeniText, failureText: string | null | undefined, diagnosticOutcome: string | null = null, ): TurnFailurePresentation | null { const text = failureText?.trim(); if (!text) return null; if (text.toLowerCase().startsWith(COMPACTION_TURN_FAILURE_PREFIX)) { return { summary: COMPACTION_FAILURE_TITLE(t), detail: text, compaction: true }; } if (diagnosticOutcome) return { summary: diagnosticOutcome, detail: text }; return { summary: text }; } /** `HTTP 400 invalid_request_error, param input[3].encrypted_content`; identifiers only. */ export function describeCompactionProviderRejection( rejection: NonNullable, ): string { const parts = [ `HTTP ${rejection.httpStatus}${rejection.type ? ` ${rejection.type}` : ""}${ rejection.code ? ` ${rejection.code}` : "" }`, ]; if (rejection.param) parts.push(`param ${rejection.param}`); return parts.join(", "); }