@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
89 lines • 4.04 kB
JavaScript
import { Container, MouseRegion, truncateToWidth } from "@earendil-works/pi-tui";
import { isCancelledWithAnswersLine, } from "../../../core/extensions/builtin/ask-user/format.js";
import { ASK_USER_QUESTION_ENTRY } from "../../../core/extensions/builtin/ask-user/notify.js";
import { stripAnsi } from "../../../utils/ansi.js";
import { theme } from "../theme/theme.js";
export { parseAskUserAnswerFrame } from "../../../core/extensions/builtin/ask-user/format.js";
/** Custom entries are display metadata, excluded from model context. Older sessions may lack them. */
export function getAskUserAnswerHeaders(entries, requestId) {
for (let index = entries.length - 1; index >= 0; index--) {
const entry = entries[index];
if (entry.type !== "custom" || entry.customType !== ASK_USER_QUESTION_ENTRY)
continue;
const data = entry.data;
if (typeof data !== "object" || data === null || !("requestId" in data) || data.requestId !== requestId)
continue;
if (!("headers" in data) || !Array.isArray(data.headers))
continue;
const headers = data.headers;
return headers.filter((header) => typeof header === "string");
}
return [];
}
function summaryRows(frame, headers) {
const fallbackHeaders = headers.length > 0 ? headers : [frame.requestId];
if (/^(?:The user did not answer|The user dismissed|The pending question|This session has no user)/.test(frame.body)) {
return fallbackHeaders.map((header) => `↳ ${header}: (no answer)`);
}
const answers = [];
let comment;
let unanswered = [];
const lines = frame.body.split("\n");
// A cancelled question's answers follow its cancel line; a failure reason above it is no answer.
const cancelledAt = lines.findIndex(isCancelledWithAnswersLine);
for (const line of lines.slice(cancelledAt + 1)) {
const separator = line.indexOf(": ");
if (separator < 1)
continue;
const header = line.slice(0, separator);
const value = line.slice(separator + 2);
if (header === "The user responded")
comment = value;
else if (header === "Unanswered")
unanswered = value.split(", ");
else
answers.push(`↳ ${header}: ${value}`);
}
if (comment !== undefined) {
answers.push(`↳ ${headers[0] ?? unanswered[0] ?? frame.requestId}: ${JSON.stringify(comment)}`);
}
else
answers.push(...unanswered.map((header) => `↳ ${header}: (no answer)`));
if (cancelledAt >= 0)
answers.unshift("↳ (cancelled)");
return answers.length > 0 ? answers : fallbackHeaders.map((header) => `↳ ${header}: (no answer)`);
}
/** A compact display over the unchanged user-message body; normal mouse dispatch requests the redraw. */
export class AskUserAnswerChip extends MouseRegion {
constructor(frame, headers, fullBody) {
const rows = summaryRows(frame, headers).map((row) => stripAnsi(row).replace(/\s+/g, " "));
const summary = {
render: (width) => rows.map((row) => theme.fg("muted", truncateToWidth(row, width, "…"))),
invalidate() { },
};
const content = new Container();
content.addChild(summary);
let expanded = false;
super(content, (event) => {
if (event.button !== "left")
return undefined;
if (event.type === "press")
return { handled: true, render: false };
if (event.type !== "click" || (event.clickCount ?? 1) !== 1)
return undefined;
expanded = !expanded;
content.detachAll();
content.addChild(expanded ? fullBody : summary);
return { handled: true };
});
this.fullBody = fullBody;
}
invalidate() {
super.invalidate();
this.fullBody.invalidate();
}
dispose() {
this.fullBody.dispose?.();
}
}
//# sourceMappingURL=ask-user-answer-chip.js.map