/** * State Route * * GET /state - Get full state snapshot. * GET /state/:slice - Get a specific state slice. */ import { Hono } from "hono"; import type { Runtime, Action } from "@gizmo-ai/runtime"; import type { ErrorResponse } from "../types.ts"; /** * Create the state routes */ export function createStateRoute< S extends Record, A extends Action >(runtime: Runtime, serializer: (value: unknown) => string): Hono { const app = new Hono(); // GET /state - full state snapshot app.get("/", (c) => { try { const state = runtime.getState(); return c.json(JSON.parse(serializer(state))); } catch (error) { return c.json( { error: `Failed to serialize state: ${error instanceof Error ? error.message : String(error)}` }, 500 ); } }); // GET /state/:slice - specific slice app.get("/:slice", (c) => { try { const sliceName = c.req.param("slice"); const state = runtime.getState(); if (!(sliceName in state)) { return c.json( { error: `Slice "${sliceName}" not found` }, 404 ); } const slice = state[sliceName as keyof S]; return c.json(JSON.parse(serializer(slice))); } catch (error) { return c.json( { error: `Failed to serialize state: ${error instanceof Error ? error.message : String(error)}` }, 500 ); } }); return app; }