(rec, payload, deps)
| 91 | * @param {{ runTurn:(p:string)=>Promise<any>, log?:Function }} deps |
| 92 | */ |
| 93 | export async function startAutopilot(rec, payload, deps) { |
| 94 | if (rec.autopilot && rec.autopilot.running) { |
| 95 | return { ok: false, error: "Autopilot is already running." }; |
| 96 | } |
| 97 | if (!deps || typeof deps.runTurn !== "function") { |
| 98 | return { ok: false, error: "This session can't drive Autopilot." }; |
| 99 | } |
| 100 | const p = payload || {}; |
| 101 | const start = await buildState(rec); |
| 102 | if (!start || !start.ok) { |
| 103 | return { ok: false, error: "Can't read the repo to start Autopilot." }; |
| 104 | } |
| 105 | if (start.doctor && start.doctor.overall === "blocked" && !p.force) { |
| 106 | return { ok: false, error: "Your environment isn't ready. Open Readiness and install the missing tools first." }; |
| 107 | } |
| 108 | const scope = p.scope === "all" ? "all" : "phase"; |
| 109 | const startRank = start.ordering ? start.ordering.activeRank : null; |
| 110 | const run = makeRun({ scope, maxSteps: clampSteps(p.maxSteps), startRank }); |
| 111 | rec.autopilot = run; |
| 112 | broadcast(rec, { type: "autopilot", autopilot: run }); |
| 113 | |
| 114 | const runDeps = { |
| 115 | snapshot: () => buildState(rec), |
| 116 | runTurn: deps.runTurn, |
| 117 | buildStepPrompt: (step) => buildPrompt("auto_step", { title: step.title, section: step.section }, rec.repoPath), |
| 118 | onProgress: (r, state) => { |
| 119 | if (state) broadcast(rec, { type: "state", state }); |
| 120 | else broadcast(rec, { type: "autopilot", autopilot: r }); |
| 121 | }, |
| 122 | log: deps.log, |
| 123 | }; |
| 124 | rec.autopilotPromise = runAutopilot(run, runDeps) |
| 125 | .then(() => pushState(rec, deps.log)) |
| 126 | .catch((e) => { |
| 127 | if (deps.log) deps.log("Autopilot run failed: " + e.message, { level: "error" }); |
| 128 | }); |
| 129 | return { ok: true, message: "Autopilot started", autopilot: run }; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Handle a cockpit action. |
no test coverage detected