Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
4.3 KB
Raw Blame
import type { Hono } from "hono"
import { describeRoute } from "hono-openapi"
import { z } from "zod"
import { jsonValidator, orgMemberRoute, paramValidator } from "../../middleware/index.js"
import { invalidRequestSchema, jsonResponse, notFoundSchema, unauthorizedSchema } from "../../openapi.js"
import type { WorkerRouteVariables } from "./shared.js"
import { fetchWorkerRuntimeJson, getWorkerByIdForOrg, parseWorkerIdParam, workerIdParamSchema } from "./shared.js"

const workerRuntimeResponseSchema = z.object({}).passthrough().meta({ ref: "WorkerRuntimeResponse" })
const omniRushWebAccessRequiredSchema = z.object({
  error: z.literal("omnirush_web_access_required"),
  message: z.string(),
}).meta({ ref: "WorkerRuntimeOmniRushWebAccessRequiredError" })

export function registerWorkerRuntimeRoutes<T extends { Variables: WorkerRouteVariables }>(app: Hono<T>) {
  app.get(
    "/v1/workers/:id/runtime",
    describeRoute({
      tags: ["Workers", "Worker Runtime"],
      summary: "Get worker runtime status",
      description: "Fetches runtime version and status information from a specific worker's runtime endpoint.",
      responses: {
        200: jsonResponse("Worker runtime information returned successfully.", workerRuntimeResponseSchema),
        400: jsonResponse("The worker runtime path parameters were invalid.", invalidRequestSchema),
        401: jsonResponse("The caller must be signed in to read worker runtime information.", unauthorizedSchema),
        403: jsonResponse("OmniRush.ai Web access is required to use a cloud worker runtime.", omniRushWebAccessRequiredSchema),
        404: jsonResponse("The worker could not be found.", notFoundSchema),
      },
    }),
    orgMemberRoute({ useUserOrganizations: true }),
    paramValidator(workerIdParamSchema),
    async (c) => {
    const orgId = c.get("activeOrganizationId")
    const params = c.req.valid("param")

    if (!orgId) {
      return c.json({ error: "worker_not_found" }, 404)
    }

    let workerId
    try {
      workerId = parseWorkerIdParam(params.id)
    } catch {
      return c.json({ error: "worker_not_found" }, 404)
    }

    const worker = await getWorkerByIdForOrg(workerId, orgId)
    if (!worker) {
      return c.json({ error: "worker_not_found" }, 404)
    }

    const runtime = await fetchWorkerRuntimeJson({
      worker,
      path: "/runtime/versions",
    })

    return new Response(JSON.stringify(runtime.payload), {
      status: runtime.status,
      headers: {
        "Content-Type": "application/json",
      },
    })
    },
  )

  app.post(
    "/v1/workers/:id/runtime/upgrade",
    describeRoute({
      tags: ["Workers", "Worker Runtime"],
      summary: "Upgrade worker runtime",
      description: "Forwards a runtime upgrade request to a specific worker and returns the worker runtime's response.",
      responses: {
        200: jsonResponse("Worker runtime upgrade request completed successfully.", workerRuntimeResponseSchema),
        400: jsonResponse("The runtime upgrade request was invalid.", invalidRequestSchema),
        401: jsonResponse("The caller must be signed in to upgrade a worker runtime.", unauthorizedSchema),
        403: jsonResponse("OmniRush.ai Web access is required to upgrade a cloud worker runtime.", omniRushWebAccessRequiredSchema),
        404: jsonResponse("The worker could not be found.", notFoundSchema),
      },
    }),
    orgMemberRoute({ useUserOrganizations: true }),
    paramValidator(workerIdParamSchema),
    jsonValidator(z.object({}).passthrough()),
    async (c) => {
    const orgId = c.get("activeOrganizationId")
    const params = c.req.valid("param")
    const body = c.req.valid("json")

    if (!orgId) {
      return c.json({ error: "worker_not_found" }, 404)
    }

    let workerId
    try {
      workerId = parseWorkerIdParam(params.id)
    } catch {
      return c.json({ error: "worker_not_found" }, 404)
    }

    const worker = await getWorkerByIdForOrg(workerId, orgId)
    if (!worker) {
      return c.json({ error: "worker_not_found" }, 404)
    }

    const runtime = await fetchWorkerRuntimeJson({
      worker,
      path: "/runtime/upgrade",
      method: "POST",
      body,
    })

    return new Response(JSON.stringify(runtime.payload), {
      status: runtime.status,
      headers: {
        "Content-Type": "application/json",
      },
    })
    },
  )
}