@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
75 lines • 2.72 kB
JavaScript
import { Box, Container, Spacer, Text } from "@earendil-works/pi-tui";
import { theme } from "../theme/theme.js";
/**
* Component that renders a custom session entry from extensions.
* The host owns transcript spacing; renderer output should provide only its content.
*/
export class CustomEntryComponent extends Container {
constructor(entry, renderer) {
super();
this._expanded = false;
this.entry = entry;
this.renderer = renderer;
this.rebuild();
}
/** Rendered entirely by children that this class rebuilds on every state change. */
getRenderRevision() {
return this.childRenderRevision();
}
get customEntry() {
return this.entry;
}
hasContent() {
return this.customComponent !== undefined;
}
setExpanded(expanded) {
if (this._expanded !== expanded) {
this._expanded = expanded;
this.rebuild();
}
}
invalidate() {
super.invalidate();
this.rebuild();
}
rebuild() {
this.clear();
this.customComponent = undefined;
let component;
try {
component = this.renderer(this.entry, { expanded: this._expanded }, theme);
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
const box = new Box(1, 1, (text) => theme.bg("customMessageBg", text));
box.addChild(new Text(theme.fg("error", `[${this.entry.customType}] renderer failed: ${message}`), 0, 0));
component = box;
}
if (!component) {
return;
}
this.customComponent = component;
this.addChild(new Spacer(1));
this.addChild(component);
}
}
/**
* Index of the card `entry` replaces in place: the transcript card directly before
* `insertIndex` (skipping only `isTransient` children such as a status line), when it
* renders an entry of the same custom type and the renderer's `replaces` option accepts
* the pair. Returns -1 when `entry` becomes a new card.
*/
export function replacedEntryCardIndex(children, insertIndex, entry, options, isTransient = () => false) {
if (options?.replaces === undefined)
return -1;
let previousIndex = insertIndex - 1;
while (previousIndex >= 0 && isTransient(children[previousIndex]))
previousIndex--;
const previous = children[previousIndex];
if (!(previous instanceof CustomEntryComponent))
return -1;
if (previous.customEntry.customType !== entry.customType)
return -1;
return options.replaces(previous.customEntry, entry) ? previousIndex : -1;
}
//# sourceMappingURL=custom-entry.js.map