/** * Invoke Route * * POST /invoke - Start a new agent invocation with input. */ import { Hono } from "hono"; import type { Runtime, Action } from "@gizmo-ai/runtime"; import type { InvokeRequest, InvokeResponse, ErrorResponse } from "../types.ts"; /** * Create the invoke route */ export function createInvokeRoute< S extends Record, A extends Action >(runtime: Runtime): Hono { const app = new Hono(); app.post("/", async (c) => { let body: InvokeRequest; try { body = await c.req.json(); } catch (error) { return c.json( { error: `Invalid JSON: ${error instanceof Error ? error.message : String(error)}` }, 400 ); } if (!body.input || typeof body.input !== "string") { return c.json( { error: "Input is required and must be a string" }, 400 ); } try { const handle = runtime.execute(body.input); // Wait for turn to be included await handle.included; // Get execution info from state const state = runtime.getState(); const execution = state.execution as { id: string; turnIds: string[]; }; const response: InvokeResponse = { executionId: execution.id, turnId: execution.turnIds[execution.turnIds.length - 1], }; return c.json(response, 202); } catch (error) { return c.json( { error: `Invocation failed: ${error instanceof Error ? error.message : String(error)}` }, 500 ); } }); return app; }