Added live widget for turn limit

This commit is contained in:
Willem van den Ende 2026-04-21 12:28:13 +01:00
parent ce325cfec9
commit 14282370ec

View File

@ -34,16 +34,71 @@ export function checkTurnLimit(
export default function (pi: ExtensionAPI) { export default function (pi: ExtensionAPI) {
let turnCount = 0; let turnCount = 0;
const maxTurns = getMaxTurns(); let maxTurns = getMaxTurns();
pi.on("agent_start", async (_event, ctx) => { pi.on("agent_start", async (_event, ctx) => {
// Reset counter for each new user prompt // Reset counter for each new user prompt
turnCount = 0; turnCount = 0;
// Clear widget on new session
if (ctx.hasUI) {
ctx.ui.setWidget("turn-limit", undefined);
}
});
pi.registerCommand("turn-limit", {
description: "Set the maximum number of agent turns for this session",
handler: async (args: string, ctx) => {
const trimmed = args.trim();
if (!trimmed) {
ctx.ui.notify("Invalid turn limit. Must be a positive integer.", "error");
return;
}
const parsed = parseInt(trimmed, 10);
if (!Number.isFinite(parsed) || parsed <= 0) {
ctx.ui.notify("Invalid turn limit. Must be a positive integer.", "error");
return;
}
maxTurns = parsed;
ctx.ui.setWidget("turn-limit", [`Turns: ${turnCount}/${maxTurns}`]);
ctx.ui.notify(`Turn limit set to ${parsed}.`, "info");
},
}); });
pi.on("turn_start", async (event, ctx) => { pi.on("turn_start", async (event, ctx) => {
turnCount++; turnCount++;
// Update live widget
if (ctx.hasUI) {
ctx.ui.setWidget("turn-limit", [`Turns: ${turnCount}/${maxTurns}`]);
}
// Boundary confirmation: when we hit maxTurns exactly
if (turnCount === maxTurns) {
if (ctx.hasUI) {
const confirmed = await ctx.ui.confirm(
"Turn limit reached",
`You've used ${maxTurns} turns. Continue?`,
);
if (confirmed) {
// Reset counter and let the turn proceed
turnCount = 0;
if (ctx.hasUI) {
ctx.ui.setWidget("turn-limit", [`Turns: ${turnCount}/${maxTurns}`]);
}
return;
} else {
// User declined — abort
ctx.ui.notify("Agent aborted by user.", "error");
ctx.abort();
return;
}
} else {
// No UI: abort silently (current behavior)
ctx.abort();
return;
}
}
const { exceeded } = checkTurnLimit(event.turnIndex, maxTurns); const { exceeded } = checkTurnLimit(event.turnIndex, maxTurns);
if (exceeded && ctx.hasUI) { if (exceeded && ctx.hasUI) {
ctx.ui.notify( ctx.ui.notify(