import type { ExtensionAPI, ExtensionContext, } from '@earendil-works/pi-coding-agent'; import { startsUserTurn } from '../shared/settle.ts'; import { type CheckCommand, detectCheckCommand, hasTestSetup, } from './detect.ts'; import { type ResolveCommand, type ResolveTestSetup, registerDoneGuard, } from './guard.ts'; import { createDoneState, resetDoneRun } from './model.ts'; export default function registerDone(pi: ExtensionAPI): void { const state = createDoneState(); const cache = new Map(); const testCache = new Map(); const resolveCommand: ResolveCommand = ( ctx: Pick, ) => { if (cache.has(ctx.cwd)) { return cache.get(ctx.cwd); } const command = detectCheckCommand(ctx.cwd); cache.set(ctx.cwd, command); return command; }; const resolveTestSetup: ResolveTestSetup = ( ctx: Pick, ) => { const cached = testCache.get(ctx.cwd); if (cached !== undefined) { return cached; } const hasTests = hasTestSetup(ctx.cwd); testCache.set(ctx.cwd, hasTests); return hasTests; }; registerDoneGuard({ pi, state, resolveCommand, resolveTestSetup }); pi.on('input', (event) => { if (startsUserTurn(event)) { resetDoneRun(state); } }); pi.on('session_start', () => { resetDoneRun(state); }); }