import fs from "node:fs/promises";
import path from "node:path";
import { z } from "zod";
import { resolvePath } from "./path-utils.js";
const ExitPlanParams = z.object({
    plan_path: z.string().describe("Path to the plan markdown file; must be under .gg/plans/"),
});
export function createExitPlanTool(cwd, onExitPlan) {
    return {
        name: "exit_plan",
        description: "Submit a .gg/plans/ markdown plan for user review and leave the active research phase. " +
            "The user can approve it for implementation, reject it with feedback, or dismiss the review.",
        parameters: ExitPlanParams,
        executionMode: "sequential",
        async execute({ plan_path }) {
            const resolved = resolvePath(cwd, plan_path);
            const plansDir = path.join(cwd, ".gg", "plans");
            const relative = path.relative(plansDir, resolved);
            if (relative.startsWith("..") || path.isAbsolute(relative)) {
                return `Error: plan_path must be under .gg/plans/. Got: ${plan_path}`;
            }
            try {
                const content = await fs.readFile(resolved, "utf-8");
                if (!content.trim()) {
                    return "Error: Plan file is empty. Write your plan before calling exit_plan.";
                }
            }
            catch {
                return `Error: Could not read plan file at ${plan_path}. Make sure it exists.`;
            }
            return onExitPlan(resolved);
        },
    };
}
//# sourceMappingURL=exit-plan.js.map