@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
158 lines • 6.28 kB
JavaScript
import { Text } from "@earendil-works/pi-tui";
import { formatGoalElapsedSeconds, formatTokensCompact, goalStatusLabel, } from "./format.js";
import { GOAL_STATUS_VALUES, goalDisplayStatus, isRecord } from "./types.js";
const OBJECTIVE_PREVIEW_WIDTH = 120;
const COLLAPSED_OBJECTIVE_LINES = 2;
const CALL_OBJECTIVE_BUDGET = 80;
const CALL_REASON_BUDGET = 60;
export function renderGoalToolCall(toolName, args, theme) {
const title = theme.fg("toolTitle", theme.bold(toolName));
const params = isRecord(args) ? args : {};
if (toolName === "create_goal" && typeof params.objective === "string") {
const preview = objectiveCallPreview(params.objective);
return new Text(preview === "" ? title : `${title} ${theme.fg("muted", preview)}`, 0, 0);
}
if (toolName === "update_goal" && typeof params.status === "string") {
const statusColor = params.status === "complete" ? "success" : "error";
let line = `${title} ${theme.fg("muted", "→")} ${theme.fg(statusColor, params.status)}`;
if (typeof params.reason === "string" && params.reason.trim().length > 0) {
line += theme.fg("muted", ` - ${shorten(params.reason.trim(), CALL_REASON_BUDGET)}`);
}
return new Text(line, 0, 0);
}
return new Text(title, 0, 0);
}
export function renderGoalToolResult(result, options, theme) {
const text = result.content.find((block) => block.type === "text")?.text ?? "";
const details = resolveRenderDetails(result.details, text);
if (details === undefined)
return new Text(theme.fg("toolOutput", text), 0, 0);
if (details.goal === null) {
const lines = [theme.fg("dim", "No active goal is set.")];
appendNotice(lines, details.notice, theme);
return new Text(lines.join("\n"), 0, 0);
}
const lines = goalWidgetLines(details.goal, options.expanded, theme);
appendNotice(lines, details.notice, theme);
return new Text(lines.join("\n"), 0, 0);
}
function goalWidgetLines(goal, expanded, theme) {
const displayStatus = goalDisplayStatus(goal);
const status = theme.fg(statusColor(displayStatus), theme.bold(`${statusGlyph(displayStatus)} ${goalStatusLabel(goal.status, goal.continuationStoppedAt)}`));
const usage = ` • ${formatTokensCompact(goal.tokensUsed)} tokens • ${formatGoalElapsedSeconds(goal.timeUsedSeconds)}`;
const lines = [`${status}${theme.fg("muted", usage)}`];
const objectiveLines = goal.objective
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length > 0);
if (expanded) {
for (const line of objectiveLines)
lines.push(theme.fg("toolOutput", ` ${line}`));
}
else {
for (const line of objectiveLines.slice(0, COLLAPSED_OBJECTIVE_LINES)) {
lines.push(theme.fg("toolOutput", ` ${shorten(line, OBJECTIVE_PREVIEW_WIDTH)}`));
}
const hidden = objectiveLines.length - COLLAPSED_OBJECTIVE_LINES;
if (hidden > 0)
lines.push(theme.fg("dim", ` … +${hidden} more lines`));
}
if (goal.blockedReason !== undefined && goal.blockedReason.length > 0) {
lines.push(theme.fg("warning", ` ⚠ ${goal.blockedReason}`));
}
if (expanded) {
const created = isoTimestamp(goal.createdAt);
const updated = isoTimestamp(goal.updatedAt);
lines.push(theme.fg("dim", ` created ${created} • updated ${updated}`));
}
return lines;
}
function appendNotice(lines, notice, theme) {
if (notice !== undefined && notice.length > 0)
lines.push(theme.fg("dim", ` ${notice}`));
}
function resolveRenderDetails(details, text) {
const direct = parseRenderDetails(details);
if (direct !== undefined)
return direct;
const trimmed = text.trim();
if (!trimmed.startsWith("{"))
return undefined;
try {
return parseRenderDetails(JSON.parse(trimmed));
}
catch {
return undefined;
}
}
function parseRenderDetails(value) {
if (!isRecord(value) || !("goal" in value))
return undefined;
const goal = value.goal;
const notice = typeof value.notice === "string" ? value.notice : undefined;
if (goal === null)
return { goal: null, ...(notice === undefined ? {} : { notice }) };
if (!isGoalSnapshotLike(goal))
return undefined;
return { goal, ...(notice === undefined ? {} : { notice }) };
}
function isGoalSnapshotLike(value) {
return (isRecord(value) &&
typeof value.objective === "string" &&
typeof value.status === "string" &&
GOAL_STATUS_VALUES.includes(value.status) &&
typeof value.tokensUsed === "number" &&
typeof value.timeUsedSeconds === "number" &&
typeof value.createdAt === "number" &&
typeof value.updatedAt === "number" &&
(value.continuationStoppedAt === undefined ||
(typeof value.continuationStoppedAt === "number" &&
Number.isSafeInteger(value.continuationStoppedAt) &&
value.continuationStoppedAt >= 0)));
}
function statusGlyph(status) {
switch (status) {
case "active":
return "●";
case "paused":
return "◌";
case "blocked":
return "■";
case "complete":
return "✓";
}
}
function statusColor(status) {
switch (status) {
case "active":
return "accent";
case "paused":
return "muted";
case "blocked":
return "error";
case "complete":
return "success";
}
}
function objectiveCallPreview(objective) {
const lines = objective
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length > 0);
const first = lines[0] ?? "";
if (first === "")
return "";
const truncated = shorten(first, CALL_OBJECTIVE_BUDGET);
if (lines.length > 1 && truncated === first)
return `${first}…`;
return truncated;
}
function isoTimestamp(epochSeconds) {
return new Date(epochSeconds * 1000).toISOString();
}
function shorten(value, max) {
if (value.length <= max)
return value;
return `${value.slice(0, max - 1)}…`;
}
//# sourceMappingURL=renderers.js.map