@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
124 lines • 5 kB
JavaScript
import { Container, claimFrameRow, currentRenderRevision, renderAtFrameRow, } from "@earendil-works/pi-tui";
import { AssistantMessageComponent } from "./assistant-message.js";
import { CustomEntryComponent } from "./custom-entry.js";
import { explorationCall } from "./exploration-call.js";
import { ExplorationGroup } from "./exploration-group.js";
import { projectRulesOfCall } from "./exploration-rules.js";
import { ProgressiveTranscriptContainer, } from "./progressive-transcript-container.js";
import { ToolExecutionComponent } from "./tool-execution.js";
/**
* Original children remain the lifecycle/ID/anchor model. A non-owning projection groups their
* presentation before progressive hydration, so compact frames never render hidden tool results.
* Re-projecting also handles partial args, late text, and visibility changes without moving cards.
*/
export class ExplorationTranscriptContainer extends Container {
constructor(options) {
super();
this.groups = new WeakMap();
/** A card's exploration call is a function of its state, so it is recomputed only after the card changes. */
this.calls = new WeakMap();
this.display = new ProgressiveTranscriptContainer(options);
}
render(width) {
this.display.children = this.project();
return renderAtFrameRow(this.display, width, claimFrameRow(this));
}
/**
* Grouping reads card state (exploration call, detail text, rules). Every such change moves the
* render revision clock, so an unmoved clock over the same children proves the grouping is unchanged.
*/
project() {
const memo = this.projection;
if (memo && memo.clock === currentRenderRevision() && sameComponents(memo.source, this.children)) {
return memo.projected;
}
const projected = [];
let group;
let members = [];
let calls = [];
let rules = [];
// Members are handed to the group once it is complete: per added member it rescanned the group.
const closeGroup = () => {
group?.setMembers(members, calls, rules);
group = undefined;
};
for (const child of this.children) {
const call = child instanceof ToolExecutionComponent ? this.explorationCallOf(child) : undefined;
if (child instanceof ToolExecutionComponent && call) {
if (!group) {
group = this.groups.get(child) ?? new ExplorationGroup();
this.groups.set(child, group);
projected.push(group);
members = [];
calls = [];
rules = [];
}
members.push(child);
calls.push({ component: child, call });
continue;
}
const childRules = group && child instanceof CustomEntryComponent ? projectRulesOfCall(child, calls) : undefined;
if (group && child instanceof AssistantMessageComponent && child.isExplorationDetail) {
members.push(child);
}
else if (group && childRules) {
members.push(child);
rules = [...rules, ...childRules];
}
else {
closeGroup();
projected.push(child);
}
}
closeGroup();
this.projection = { source: [...this.children], clock: currentRenderRevision(), projected };
return projected;
}
handleMouse(event) {
return this.display.handleMouse(event);
}
explorationCallOf(child) {
const revision = child.getRenderRevision();
if (revision === undefined)
return explorationCall(child);
const cached = this.calls.get(child);
if (cached?.revision === revision)
return cached.call;
const call = explorationCall(child);
this.calls.set(child, { revision, call });
return call;
}
/** Theme and capability changes reach the original children; the painted projection's cache must drop too. */
invalidate() {
this.display.invalidateCache();
this.calls = new WeakMap();
super.invalidate();
}
clear() {
this.display.detachAll();
this.groups = new WeakMap();
this.projection = undefined;
super.clear();
}
detachAll() {
this.display.detachAll();
this.groups = new WeakMap();
this.projection = undefined;
super.detachAll();
}
dispose() {
this.display.detachAll();
this.display.dispose();
super.dispose();
}
}
function sameComponents(left, right) {
if (left.length !== right.length)
return false;
for (let index = 0; index < left.length; index++) {
if (left[index] !== right[index])
return false;
}
return true;
}
//# sourceMappingURL=exploration-transcript-container.js.map