/** * Abort Route * * POST /abort - Abort the current execution. */ import { Hono } from "hono"; import type { Runtime, Action, ExecutionAbortedAction } from "@gizmo-ai/runtime"; import type { AbortResponse, ErrorResponse } from "../types.ts"; /** * Create the abort route * * Note: The runtime's dispatch accepts ExecutionAction types internally, * so we create a properly typed abort action. */ export function createAbortRoute< S extends Record, A extends Action >(runtime: Runtime): Hono { const app = new Hono(); app.post("/", async (c) => { try { const state = runtime.getState(); const execution = state.execution as { id: string | null; state: string; }; if (execution.state !== "pending") { return c.json( { error: "No active execution to abort" }, 400 ); } // Create a properly typed abort action (PayloadAction with .payload) const abortAction: ExecutionAbortedAction = { type: "RUNTIME_EXECUTION_ABORTED", payload: { executionId: execution.id!, }, }; // The runtime accepts execution actions even when typed with user actions // This is safe because ExecutionAbortedAction extends Action (runtime.dispatch as (action: Action) => void)(abortAction); return c.json({ status: "aborted", executionId: execution.id!, }); } catch (error) { return c.json( { error: `Abort failed: ${error instanceof Error ? error.message : String(error)}` }, 500 ); } }); return app; }