@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
94 lines • 6.87 kB
JavaScript
import { Type } from "typebox";
import { formatGoalToolResponse, goalToolRenderDetails } from "./format.js";
import { renderGoalToolCall, renderGoalToolResult } from "./renderers.js";
import { createGoal, objectiveFullTextFileName, readGoal, updateGoal } from "./store.js";
import { openTodoCompletionError, openTodoTaskContents } from "./todo-gate.js";
import { MODEL_SETTABLE_GOAL_STATUS_VALUES } from "./types.js";
import { objectiveTruncationNotice, validateObjective } from "./validation.js";
export function registerGoalTools(pi, deps) {
pi.registerTool({
name: "create_goal",
sessionBound: true,
label: "Create Goal",
description: "Register a goal for work that outlives this turn: it waits on external state, or the user's requested outcome needs more than one verify-and-fix round before it is true. A single answer, lookup, or one-shot edit needs no goal.\nObjectives over 4,000 characters are cut to at most 4,000 including a truncation marker (the full text is saved to a side file). For longer instructions, put the full objective in a file and refer to that file.\nReplaces the current goal when it is complete and archives it; fails if an unfinished goal exists.",
parameters: Type.Object({
objective: Type.String({
description: "Required. The concrete objective to start pursuing. Text past 4,000 characters is truncated, not rejected. For longer instructions, put the full objective in a file and refer to that file.",
}),
}, { additionalProperties: false }),
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
const ref = deps.goalStoreRef(ctx);
const current = await readGoal(ref);
if (current !== null && current.status !== "complete") {
throw new Error("cannot create a new goal because this thread already has an unfinished goal; use update_goal only when the existing goal is complete");
}
const validatedObjective = validateObjective(params.objective, objectiveFullTextFileName(ref));
const goal = await createGoal(ref, params.objective);
deps.beginAgentGoalAccounting(goal);
deps.refreshGoalUi(ctx, goal);
const notice = validatedObjective.truncated
? objectiveTruncationNotice(objectiveFullTextFileName(ref))
: undefined;
return toolText(formatGoalToolResponse(goal, notice), goalToolRenderDetails(goal, notice));
},
renderCall: (args, theme) => renderGoalToolCall("create_goal", args, theme),
renderResult: (result, options, theme) => renderGoalToolResult(result, options, theme),
});
pi.registerTool({
name: "update_goal",
sessionBound: true,
label: "Update Goal",
description: "Set the existing goal's status to `complete` or `blocked`; the completion audit and blocked audit in the goal continuation prompt decide which, and only a passing audit permits the call.\n`complete` is rejected while todo tasks are open, and stopping work is never by itself a reason to complete; after it succeeds, report the final elapsed time and token usage from the result to the user.\n`blocked` requires a non-empty `reason` (omit `reason` for `complete`); the tool checks only that, so the blocked audit is yours to pass first: never call it while any resumption channel can still deliver, or before the same blocker has survived three goal turns since the goal became active or the user last spoke; a user resume starts a fresh blocked audit.\nA missing user decision is a question for the question tool, not a blocked status, until the user fails to answer it. Retries themselves are unbounded.\nPausing and resuming are user or system actions, not this tool.",
parameters: Type.Object({
status: Type.Union(MODEL_SETTABLE_GOAL_STATUS_VALUES.map((status) => Type.Literal(status)), { description: "Required. Set to complete when achieved or blocked with a non-empty reason." }),
reason: Type.Optional(Type.String({
description: "Required and non-empty when status is blocked; rejected when status is complete.",
})),
}, { additionalProperties: false }),
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
const reason = typeof params.reason === "string" ? params.reason.trim() : undefined;
if (params.status === "blocked" && (reason === undefined || reason.length === 0)) {
throw new Error("reason is required when status is blocked");
}
if (params.status === "complete" && reason !== undefined && reason.length > 0) {
throw new Error("reason must not be provided when status is complete");
}
if (params.status === "complete") {
const openTasks = openTodoTaskContents(ctx.sessionManager.getBranch());
if (openTasks.length > 0)
throw new Error(openTodoCompletionError(openTasks));
}
await deps.accountCurrentAgentTurn(ctx, "active");
const goal = await updateGoal(deps.goalStoreRef(ctx), params.status === "blocked" ? { status: "blocked", reason } : { status: "complete" }, "model");
if (goal.status === "blocked")
deps.markGoalBlockedThisTurn(goal);
else
deps.markGoalCompletedThisTurn(goal);
deps.refreshGoalUi(ctx, goal);
return toolText(formatGoalToolResponse(goal), goalToolRenderDetails(goal));
},
renderCall: (args, theme) => renderGoalToolCall("update_goal", args, theme),
renderResult: (result, options, theme) => renderGoalToolResult(result, options, theme),
});
pi.registerTool({
name: "get_goal",
sessionBound: true,
label: "Get Goal",
description: "Get the current goal for this thread, including status, token and elapsed-time usage.",
exposure: "search",
allowLazyActivation: true,
searchKeywords: ["goal status", "goal usage", "elapsed time", "token usage"],
parameters: Type.Object({}, { additionalProperties: false }),
async execute(_toolCallId, _params, _signal, _onUpdate, ctx) {
const goal = await deps.accountCurrentAgentTurn(ctx, "active");
deps.refreshGoalUi(ctx, goal);
return toolText(formatGoalToolResponse(goal), goalToolRenderDetails(goal));
},
renderCall: (args, theme) => renderGoalToolCall("get_goal", args, theme),
renderResult: (result, options, theme) => renderGoalToolResult(result, options, theme),
});
}
function toolText(text, details) {
return { content: [{ type: "text", text }], details };
}
//# sourceMappingURL=tool-registration.js.map