@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
75 lines • 2.16 kB
JavaScript
export function isComponent(value) {
return ((typeof value === "object" || typeof value === "function") &&
value !== null &&
typeof Reflect.get(value, "render") === "function" &&
typeof Reflect.get(value, "invalidate") === "function");
}
export class ToolRendererBoundary {
constructor(component, fallback, onFailure) {
this.componentDisposed = false;
this.failed = false;
this.component = component;
this.fallback = fallback;
this.onFailure = onFailure;
}
render(width) {
if (!this.failed) {
try {
const lines = this.component.render(width);
if (Array.isArray(lines)) {
const safeLines = [];
for (const line of lines) {
if (typeof line !== "string") {
throw new TypeError("Tool renderer returned a non-string line");
}
safeLines.push(line);
}
return safeLines;
}
}
catch {
this.fail();
return this.renderFallback(width);
}
this.fail();
}
return this.renderFallback(width);
}
invalidate() {
if (!this.failed) {
try {
this.component.invalidate();
}
catch {
this.fail();
}
}
this.fallback?.invalidate();
}
dispose() {
this.disposeComponent();
this.fallback?.dispose?.();
}
fail() {
if (this.failed)
return;
this.failed = true;
this.onFailure();
this.disposeComponent();
}
disposeComponent() {
if (this.componentDisposed)
return;
this.componentDisposed = true;
try {
this.component.dispose?.();
}
catch {
return;
}
}
renderFallback(width) {
return this.fallback?.render(width) ?? [];
}
}
//# sourceMappingURL=tool-renderer-boundary.js.map