UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

84 lines • 3.96 kB
import { parseGoalCommand } from "./command.js"; import { formatGoalForTool, goalStatusLabel } from "./format.js"; import { clearGoal, createGoal, readGoal, updateGoal } from "./store.js"; const GOAL_USAGE = "Usage: /goal <objective>"; const GOAL_EMPTY_HINT = "No goal is currently set."; const REPLACE_GOAL_CHOICE = "Replace current goal"; const CANCEL_REPLACE_GOAL_CHOICE = "Cancel"; export function registerGoalCommand(pi, deps) { pi.registerCommand("goal", { description: "Set, inspect, pause, resume, or clear the persistent goal", handler: async (rawArgs, ctx) => { const command = parseGoalCommand(rawArgs); try { switch (command.kind) { case "show": { const goal = await readGoal(deps.goalStoreRef(ctx)); deps.refreshGoalUi(ctx, goal); ctx.ui.notify(goal === null ? `${GOAL_USAGE}\n${GOAL_EMPTY_HINT}` : formatGoalForTool(goal), goal ? "info" : "warning"); return; } case "setObjective": { await setGoalObjective(pi, ctx, command.objective, deps); return; } case "setStatus": { if (command.status === "paused") { await deps.accountCurrentAgentTurn(ctx, "active"); } const goal = await updateGoal(deps.goalStoreRef(ctx), { status: command.status }, "user"); if (goal.status === "active") { deps.beginAgentGoalAccounting(goal); } else { deps.stopAgentGoalAccounting(goal.id); } deps.refreshGoalUi(ctx, goal); ctx.ui.notify(`Goal ${goalStatusLabel(goal.status)}\n${formatGoalForTool(goal)}`, "info"); deps.queueGoalContinuation(pi, ctx, goal); return; } case "clear": { await deps.accountCurrentAgentTurn(ctx, "active"); const cleared = await clearGoal(deps.goalStoreRef(ctx)); deps.clearAgentGoalAccounting(); deps.refreshGoalUi(ctx, null); ctx.ui.notify(cleared ? "Goal cleared" : "No goal to clear\nThis thread does not currently have a goal.", cleared ? "info" : "warning"); return; } } } catch (error) { ctx.ui.notify(error instanceof Error ? error.message : String(error), "error"); } }, }); } async function setGoalObjective(pi, ctx, objective, deps) { const ref = deps.goalStoreRef(ctx); const current = await readGoal(ref); if (current !== null) { const shouldReplace = await confirmReplaceGoal(ctx, objective); if (!shouldReplace) return; } if (current?.status === "active") { await deps.accountCurrentAgentTurn(ctx, "active"); } const goal = current === null ? await createGoal(ref, objective) : await updateGoal(ref, { objective }, "user"); if (goal.status === "active") deps.beginAgentGoalAccounting(goal); deps.refreshGoalUi(ctx, goal); ctx.ui.notify(`Goal ${goalStatusLabel(goal.status)}\n${formatGoalForTool(goal)}`, "info"); deps.queueGoalContinuation(pi, ctx, goal); } async function confirmReplaceGoal(ctx, objective) { if (!ctx.hasUI) return true; const choice = await ctx.ui.select(`Replace goal?\nNew objective: ${objective}`, [ REPLACE_GOAL_CHOICE, CANCEL_REPLACE_GOAL_CHOICE, ]); return choice === REPLACE_GOAL_CHOICE; } //# sourceMappingURL=command-registration.js.map