@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
99 lines • 3.71 kB
JavaScript
import { goalDisplayStatus } from "./types.js";
export function formatGoalElapsedSeconds(value) {
const seconds = Math.max(0, Math.trunc(value));
if (seconds < 60)
return `${seconds}s`;
const minutes = Math.trunc(seconds / 60);
if (minutes < 60)
return `${minutes}m`;
const hours = Math.trunc(minutes / 60);
const remainingMinutes = minutes % 60;
if (hours >= 24) {
const days = Math.trunc(hours / 24);
const remainingHours = hours % 24;
return `${days}d ${remainingHours}h ${remainingMinutes}m`;
}
if (remainingMinutes === 0)
return `${hours}h`;
return `${hours}h ${remainingMinutes}m`;
}
export function formatTokensCompact(value) {
const abs = Math.abs(value);
if (abs >= 1_000_000)
return `${formatOneDecimal(value / 1_000_000)}M`;
if (abs >= 1_000)
return `${formatOneDecimal(value / 1_000)}K`;
return `${Math.trunc(value)}`;
}
export function goalStatusLabel(status, continuationStoppedAt) {
if (continuationStoppedAt !== undefined && (status === "active" || status === "paused")) {
return "stopped: no progress (send a message or /goal resume)";
}
switch (status) {
case "active":
return "active";
case "paused":
return "paused";
case "blocked":
return "blocked";
case "complete":
return "complete";
}
}
export function formatGoalForTool(goal) {
if (!goal)
return "No active goal is set.";
const lines = [
`Objective: ${goal.objective}`,
`Status: ${goalStatusLabel(goal.status, goal.continuationStoppedAt)}`,
`Time used: ${formatGoalElapsedSeconds(goal.timeUsedSeconds)}`,
`Tokens used: ${formatTokensCompact(goal.tokensUsed)}`,
];
if (goal.blockedReason)
lines.push(`Blocked reason: ${goal.blockedReason}`);
if (goal.completedAt)
lines.push(`Completed at: ${new Date(goal.completedAt * 1000).toISOString()}`);
return lines.join("\n");
}
export function goalToolRenderDetails(goal, notice) {
return {
goal: goal === null ? null : goalToolSnapshot(goal),
...(notice === undefined ? {} : { notice }),
};
}
export function goalToolResponse(goal) {
return {
goal: goal === null ? null : goalToolSnapshot(goal),
...(goal?.status === "active" && goal.continuationStoppedAt !== undefined
? {
continuation: {
status: "stale_stopped",
message: "The goal stopped because progress was stale. It resumes when the user sends a message or runs /goal resume.",
},
}
: {}),
};
}
export function formatGoalToolResponse(goal, notice) {
const response = JSON.stringify(goalToolResponse(goal), null, 2);
return notice === undefined ? response : `${response}\n${notice}`;
}
function goalToolSnapshot(goal) {
return {
threadId: goal.threadId,
objective: goal.objective,
status: goalDisplayStatus(goal),
...(goal.continuationStoppedAt === undefined ? {} : { continuationStoppedAt: goal.continuationStoppedAt }),
tokensUsed: goal.tokensUsed,
timeUsedSeconds: goal.timeUsedSeconds,
createdAt: goal.createdAt,
updatedAt: goal.updatedAt,
...(goal.blockedReason === undefined ? {} : { blockedReason: goal.blockedReason }),
...(goal.blockedAt === undefined ? {} : { blockedAt: goal.blockedAt }),
};
}
function formatOneDecimal(value) {
const rounded = value.toFixed(1);
return rounded.endsWith(".0") ? rounded.slice(0, -2) : rounded;
}
//# sourceMappingURL=format.js.map