145 lines · 5.9 KB · TypeScript
RawDownload
import { serve } from "@hono/node-server";import { Hono } from "hono";import {  handleAgentAbort,  handleAgentCompact,  handleAgentTurn,  handleExtensionUiResponse,  handleRuntimeEvents,  handleRuntimeSessions,  handleRuntimeStatus,  handleSetupChecks,} from "./http/handlers";import {  handleBrowserFetch,  handleBrowserFrame,  handleBrowserInput,  handleBrowserLocalhosts,  handleBrowserState,  handleBrowserVerb,  handleBrowserViewport,} from "./http/browser-handlers";import {  handleProviderLogin,  handleProviderLoginCancel,  handleProviderLoginJob,  handleProviderLoginRespond,  handleProviderLogout,  handleProviderModels,  handleProvidersList,} from "./http/provider-handlers";import { markAgentRuntimeProcess } from "./provider-hub";import { startAutomationScheduler } from "./automation-scheduler";import {  handleAutomationCreate,  handleAutomationDelete,  handleAutomationPatch,  handleAutomationRun,  handleAutomationsList,  handleGoalDelete,  handleGoalGet,  handleGoalPut,} from "./http/automation-handlers";import { handleSubagentRun, handleSubagentsList } from "./http/subagent-handlers";import { handlePrGet, handlePrMerge } from "./http/pr-handlers";import {  handlePtyClose,  handlePtyInput,  handlePtyOpen,  handlePtyResize,  handlePtyStream,} from "./http/pty-handlers";import { createLitterBridgeGateway } from "./litter-bridge-gateway";import { handleAgentModels } from "./http/model-handlers";import {  handleAllSessions,  handleSessionGet,  handleSessionPatch,  handleSessionsDelete,  handleSessionsList,} from "./http/session-handlers"; markAgentRuntimeProcess();startAutomationScheduler(); const app = new Hono();const litterBridgeGateway = createLitterBridgeGateway(); app.get("/health", (c) =>  c.json({ ok: true, service: "local-studio-agent-runtime", pid: process.pid }),);app.post("/api/litter-bridge/v1", (c) => litterBridgeGateway.handle(c.req.raw)); app.post("/api/agent/turn", (c) => handleAgentTurn(c.req.raw));app.post("/api/agent/abort", (c) => handleAgentAbort(c.req.raw));app.post("/api/agent/compact", (c) => handleAgentCompact(c.req.raw));app.post("/api/agent/runtime/extension-ui", (c) => handleExtensionUiResponse(c.req.raw));app.get("/api/agent/runtime/sessions", () => handleRuntimeSessions());app.get("/api/agent/runtime/status", (c) => handleRuntimeStatus(c.req.raw));app.get("/api/agent/runtime/events", (c) => handleRuntimeEvents(c.req.raw));app.get("/api/agent/setup-checks", () => handleSetupChecks());app.get("/api/agent/models", () => handleAgentModels());app.post("/api/agent/models", (c) => handleAgentModels(c.req.raw));app.get("/api/agent/sessions", (c) => handleSessionsList(c.req.raw));app.delete("/api/agent/sessions", () => handleSessionsDelete());app.get("/api/agent/sessions/all", (c) => handleAllSessions(c.req.raw));app.get("/api/agent/sessions/:id", (c) => handleSessionGet(c.req.raw, c.req.param("id")));app.patch("/api/agent/sessions/:id", (c) => handleSessionPatch(c.req.raw, c.req.param("id"))); app.get("/api/agent/automations", () => handleAutomationsList());app.post("/api/agent/automations", (c) => handleAutomationCreate(c.req.raw));app.patch("/api/agent/automations/:id", (c) => handleAutomationPatch(c.req.raw, c.req.param("id")));app.delete("/api/agent/automations/:id", (c) => handleAutomationDelete(c.req.param("id")));app.post("/api/agent/automations/:id/run", (c) => handleAutomationRun(c.req.param("id")));app.get("/api/agent/pr", (c) => handlePrGet(c.req.raw));app.post("/api/agent/pr/merge", (c) => handlePrMerge(c.req.raw));app.get("/api/agent/subagents", (c) => handleSubagentsList(c.req.raw));app.post("/api/agent/subagents", (c) => handleSubagentRun(c.req.raw));app.get("/api/agent/goal", (c) => handleGoalGet(c.req.raw));app.put("/api/agent/goal", (c) => handleGoalPut(c.req.raw));app.delete("/api/agent/goal", (c) => handleGoalDelete(c.req.raw)); app.get("/api/agent/providers", () => handleProvidersList());app.get("/api/agent/providers/models", () => handleProviderModels());app.get("/api/agent/providers/login/:jobId", (c) =>  handleProviderLoginJob(c.req.raw, c.req.param("jobId")),);app.post("/api/agent/providers/login/:jobId/respond", (c) =>  handleProviderLoginRespond(c.req.raw, c.req.param("jobId")),);app.post("/api/agent/providers/login/:jobId/cancel", (c) =>  handleProviderLoginCancel(c.req.param("jobId")),);app.post("/api/agent/providers/:providerId/login", (c) =>  handleProviderLogin(c.req.raw, c.req.param("providerId")),);app.post("/api/agent/providers/:providerId/logout", (c) =>  handleProviderLogout(c.req.param("providerId")),); app.post("/api/agent/terminal/pty/open", (c) => handlePtyOpen(c.req.raw));app.get("/api/agent/terminal/pty/stream", (c) => handlePtyStream(c.req.raw));app.post("/api/agent/terminal/pty/input", (c) => handlePtyInput(c.req.raw));app.post("/api/agent/terminal/pty/resize", (c) => handlePtyResize(c.req.raw));app.post("/api/agent/terminal/pty/close", (c) => handlePtyClose(c.req.raw)); app.get("/api/agent/browser/fetch", (c) => handleBrowserFetch(c.req.raw));app.get("/api/agent/browser/frame", () => handleBrowserFrame());app.post("/api/agent/browser/input", (c) => handleBrowserInput(c.req.raw));app.get("/api/agent/browser/localhosts", (c) => handleBrowserLocalhosts(c.req.raw));app.get("/api/agent/browser/state", () => handleBrowserState());app.post("/api/agent/browser/viewport", (c) => handleBrowserViewport(c.req.raw));app.post("/api/agent/browser/:verb", (c) => handleBrowserVerb(c.req.raw, c.req.param("verb"))); const port = Number(process.env.PORT) > 0 ? Number(process.env.PORT) : 8081; serve({ fetch: app.fetch, port, hostname: "127.0.0.1" }, (info) => {  litterBridgeGateway.publishMetadata(info.port);  console.log(    `[agent-runtime] listening on http://127.0.0.1:${info.port} (pid ${process.pid}, node ${process.version})`,  );}); process.once("exit", () => litterBridgeGateway.dispose());process.once("SIGINT", () => process.exit(0));process.once("SIGTERM", () => process.exit(0));