(rec, kind, payload, deps)
| 137 | * @param {{ sendPrompt:(p:string)=>Promise<void>, runTurn?:Function, log?:Function }} deps |
| 138 | */ |
| 139 | export async function dispatchAction(rec, kind, payload, deps) { |
| 140 | if (kind === "refresh") { |
| 141 | const state = await pushState(rec, deps && deps.log); |
| 142 | return { ok: true, message: "Refreshed", state }; |
| 143 | } |
| 144 | if (kind === "recheck_env") { |
| 145 | const state = await pushState(rec, deps && deps.log, { recheckEnv: true }); |
| 146 | return { ok: true, message: "Re-checked environment", state }; |
| 147 | } |
| 148 | if (kind === "autopilot_start") { |
| 149 | return await startAutopilot(rec, payload, deps); |
| 150 | } |
| 151 | if (kind === "autopilot_stop") { |
| 152 | if (rec.autopilot && rec.autopilot.running) { |
| 153 | rec.autopilot.cancelled = true; |
| 154 | broadcast(rec, { type: "autopilot", autopilot: rec.autopilot }); |
| 155 | return { ok: true, message: "Stopping after the current step finishes…" }; |
| 156 | } |
| 157 | return { ok: false, error: "Autopilot isn't running." }; |
| 158 | } |
| 159 | if (kind === "autopilot_dismiss") { |
| 160 | if (rec.autopilot && !rec.autopilot.running) { |
| 161 | rec.autopilot = null; |
| 162 | await pushState(rec, deps && deps.log); |
| 163 | } |
| 164 | return { ok: true, message: "Dismissed" }; |
| 165 | } |
| 166 | const prompt = buildPrompt(kind, payload, rec.repoPath); |
| 167 | if (!prompt) return { ok: false, error: "Unknown action: " + kind }; |
| 168 | if (!deps || typeof deps.sendPrompt !== "function") { |
| 169 | return { ok: false, error: "Session not ready" }; |
| 170 | } |
| 171 | const label = ACTION_LABELS[kind] || kind; |
| 172 | try { |
| 173 | await deps.sendPrompt(prompt); |
| 174 | if (deps.log) deps.log("Java Modernization Studio → " + label, { ephemeral: true }); |
| 175 | return { ok: true, label, message: "Sent to the agent: " + label, prompt }; |
| 176 | } catch (e) { |
| 177 | return { ok: false, error: e.message }; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Cap the request body the /action endpoint will buffer. Actions carry tiny JSON |
| 182 | // payloads; anything large is a bug or abuse, so we reject rather than buffer it. |
no test coverage detected