@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
79 lines • 3.8 kB
JavaScript
import { Box, Container, Markdown, MouseRegion, Spacer, Text } from "@earendil-works/pi-tui";
import { sharedMarkdownTheme, theme } from "../theme/theme.js";
import { keyText } from "./keybinding-hints.js";
const TERMINAL_ESCAPE_SEQUENCE = /(?:\u001B\]|\u009D)[\s\S]*?(?:\u0007|\u001B\\|\u009C)|(?:\u001B\[|\u009B)[0-?]*[ -/]*[@-~]/g;
function sanitizeCompactionSummary(summary) {
return summary
.replace(TERMINAL_ESCAPE_SEQUENCE, "")
.replace(/\r\n?/g, "\n")
.replace(/[\u0000-\u0009\u000B-\u001F\u007F-\u009F]/g, "");
}
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function formatCompactionDetails(details) {
if (!isRecord(details))
return undefined;
if (details.schema !== "senpi.compaction.openai-remote.v1")
return undefined;
const retained = typeof details.retainedInputItemCount === "number" ? details.retainedInputItemCount : undefined;
const requested = typeof details.requestInputItemCount === "number" ? details.requestInputItemCount : undefined;
const retainedText = retained === undefined ? "native replay active" : `${retained.toLocaleString()} retained items`;
const requestedText = requested === undefined ? "" : ` from ${requested.toLocaleString()} OpenAI input items`;
const route = details.transport === "websocket" ? "OpenAI Responses WebSocket compaction" : "OpenAI remote compact API";
return `${route}: ${retainedText}${requestedText}`;
}
/**
* Component that renders a compaction message with collapsed/expanded state.
* Uses same background color as custom messages for visual consistency.
*/
export class CompactionSummaryMessageComponent extends Box {
constructor(message, markdownTheme = sharedMarkdownTheme()) {
super(1, 1, (t) => theme.bg("customMessageBg", t));
this.expanded = false;
this.message = message;
this.markdownTheme = markdownTheme;
this.updateDisplay();
}
/** Rendered entirely by children that this class rebuilds on every state change. */
getRenderRevision() {
return this.childRenderRevision();
}
setExpanded(expanded) {
this.expanded = expanded;
this.updateDisplay();
}
invalidate() {
super.invalidate();
this.updateDisplay();
}
updateDisplay() {
this.clear();
const content = new Container();
const tokenStr = this.message.tokensBefore.toLocaleString();
const details = formatCompactionDetails(this.message.details);
const label = theme.fg("customMessageLabel", `\x1b[1m[compaction]\x1b[22m`);
content.addChild(new Text(label, 0, 0));
content.addChild(new Spacer(1));
if (this.expanded) {
const detailLine = details ? `\n${details}\n\n` : "\n\n";
const header = `**Compacted from ${tokenStr} tokens**${detailLine}`;
content.addChild(new Markdown(header + sanitizeCompactionSummary(this.message.summary), 0, 0, this.markdownTheme, {
color: (text) => theme.fg("customMessageText", text),
}));
}
else {
const prefix = details ? `${details}; ` : "";
content.addChild(new Text(theme.fg("customMessageText", `${prefix}compacted from ${tokenStr} tokens (`) +
theme.fg("dim", keyText("app.tools.expand")) +
theme.fg("customMessageText", " to expand)"), 0, 0));
}
this.addChild(new MouseRegion(content, (event) => {
if (event.type !== "click" || event.button !== "left")
return undefined;
this.setExpanded(!this.expanded);
return { handled: true };
}));
}
}
//# sourceMappingURL=compaction-summary-message.js.map