- Add pnpm-workspace.yaml for monorepo - Add @mariozechner/pi-coding-agent as root devDependency (TS types) - Create .pi/settings.json with local package reference - Extension auto-loads and hot-reloads via /reload
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
|
|
// ============================================================================
|
|
// Configuration
|
|
// ============================================================================
|
|
|
|
const DEFAULT_MAX_TURNS = 25;
|
|
|
|
function getMaxTurns(): number {
|
|
const env = process.env.PI_MAX_TURNS;
|
|
if (!env) return DEFAULT_MAX_TURNS;
|
|
const parsed = parseInt(env, 10);
|
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_MAX_TURNS;
|
|
}
|
|
|
|
// ============================================================================
|
|
// Pure detection logic (testable)
|
|
// ============================================================================
|
|
|
|
export function checkTurnLimit(
|
|
turnIndex: number,
|
|
maxTurns: number,
|
|
): { exceeded: boolean; turnIndex: number; maxTurns: number } {
|
|
return {
|
|
exceeded: turnIndex > maxTurns,
|
|
turnIndex,
|
|
maxTurns,
|
|
};
|
|
}
|
|
|
|
// ============================================================================
|
|
// Extension handler
|
|
// ============================================================================
|
|
|
|
export default function (pi: ExtensionAPI) {
|
|
let turnCount = 0;
|
|
const maxTurns = getMaxTurns();
|
|
|
|
pi.on("agent_start", async (_event, ctx) => {
|
|
// Reset counter for each new user prompt
|
|
turnCount = 0;
|
|
});
|
|
|
|
pi.on("turn_start", async (event, ctx) => {
|
|
turnCount++;
|
|
|
|
const { exceeded } = checkTurnLimit(event.turnIndex, maxTurns);
|
|
if (exceeded && ctx.hasUI) {
|
|
ctx.ui.notify(
|
|
`Turn limit exceeded: ${maxTurns} turns reached. Agent aborted.`,
|
|
"error",
|
|
);
|
|
ctx.abort();
|
|
}
|
|
});
|
|
}
|