| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import { describeRoute, resolver, validator } from "hono-openapi"
- import { Hono } from "hono"
- import type { UpgradeWebSocket } from "hono/ws"
- import { Effect } from "effect"
- import z from "zod"
- import { Format } from "../../format"
- import { TuiRoutes } from "./tui"
- import { Instance } from "../../project/instance"
- import { Vcs } from "../../project/vcs"
- import { Agent } from "../../agent/agent"
- import { Skill } from "../../skill"
- import { Global } from "../../global"
- import { LSP } from "../../lsp"
- import { Command } from "../../command"
- import { QuestionRoutes } from "./question"
- import { PermissionRoutes } from "./permission"
- import { Flag } from "@/flag/flag"
- import { ExperimentalHttpApiServer } from "./httpapi/server"
- import { ProjectRoutes } from "./project"
- import { SessionRoutes } from "./session"
- import { PtyRoutes } from "./pty"
- import { McpRoutes } from "./mcp"
- import { FileRoutes } from "./file"
- import { ConfigRoutes } from "./config"
- import { ExperimentalRoutes } from "./experimental"
- import { ProviderRoutes } from "./provider"
- import { EventRoutes } from "./event"
- import { SyncRoutes } from "./sync"
- import { WorkspaceRouterMiddleware } from "./middleware"
- import { AppRuntime } from "@/effect/app-runtime"
- export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
- const app = new Hono()
- .use(WorkspaceRouterMiddleware(upgrade))
- .route("/project", ProjectRoutes())
- .route("/pty", PtyRoutes(upgrade))
- .route("/config", ConfigRoutes())
- .route("/experimental", ExperimentalRoutes())
- .route("/session", SessionRoutes())
- .route("/permission", PermissionRoutes())
- if (Flag.OPENCODE_EXPERIMENTAL_HTTPAPI) {
- const handler = ExperimentalHttpApiServer.webHandler().handler
- app.all("/question", (c) => handler(c.req.raw)).all("/question/*", (c) => handler(c.req.raw))
- }
- return app
- .route("/question", QuestionRoutes())
- .route("/provider", ProviderRoutes())
- .route("/sync", SyncRoutes())
- .route("/", FileRoutes())
- .route("/", EventRoutes())
- .route("/mcp", McpRoutes())
- .route("/tui", TuiRoutes())
- .post(
- "/instance/dispose",
- describeRoute({
- summary: "Dispose instance",
- description: "Clean up and dispose the current OpenCode instance, releasing all resources.",
- operationId: "instance.dispose",
- responses: {
- 200: {
- description: "Instance disposed",
- content: {
- "application/json": {
- schema: resolver(z.boolean()),
- },
- },
- },
- },
- }),
- async (c) => {
- await Instance.dispose()
- return c.json(true)
- },
- )
- .get(
- "/path",
- describeRoute({
- summary: "Get paths",
- description: "Retrieve the current working directory and related path information for the OpenCode instance.",
- operationId: "path.get",
- responses: {
- 200: {
- description: "Path",
- content: {
- "application/json": {
- schema: resolver(
- z
- .object({
- home: z.string(),
- state: z.string(),
- config: z.string(),
- worktree: z.string(),
- directory: z.string(),
- })
- .meta({
- ref: "Path",
- }),
- ),
- },
- },
- },
- },
- }),
- async (c) => {
- return c.json({
- home: Global.Path.home,
- state: Global.Path.state,
- config: Global.Path.config,
- worktree: Instance.worktree,
- directory: Instance.directory,
- })
- },
- )
- .get(
- "/vcs",
- describeRoute({
- summary: "Get VCS info",
- description: "Retrieve version control system (VCS) information for the current project, such as git branch.",
- operationId: "vcs.get",
- responses: {
- 200: {
- description: "VCS info",
- content: {
- "application/json": {
- schema: resolver(Vcs.Info),
- },
- },
- },
- },
- }),
- async (c) => {
- return c.json(
- await AppRuntime.runPromise(
- Effect.gen(function* () {
- const vcs = yield* Vcs.Service
- const [branch, default_branch] = yield* Effect.all([vcs.branch(), vcs.defaultBranch()], {
- concurrency: 2,
- })
- return { branch, default_branch }
- }),
- ),
- )
- },
- )
- .get(
- "/vcs/diff",
- describeRoute({
- summary: "Get VCS diff",
- description: "Retrieve the current git diff for the working tree or against the default branch.",
- operationId: "vcs.diff",
- responses: {
- 200: {
- description: "VCS diff",
- content: {
- "application/json": {
- schema: resolver(Vcs.FileDiff.array()),
- },
- },
- },
- },
- }),
- validator(
- "query",
- z.object({
- mode: Vcs.Mode,
- }),
- ),
- async (c) => {
- return c.json(
- await AppRuntime.runPromise(
- Effect.gen(function* () {
- const vcs = yield* Vcs.Service
- return yield* vcs.diff(c.req.valid("query").mode)
- }),
- ),
- )
- },
- )
- .get(
- "/command",
- describeRoute({
- summary: "List commands",
- description: "Get a list of all available commands in the OpenCode system.",
- operationId: "command.list",
- responses: {
- 200: {
- description: "List of commands",
- content: {
- "application/json": {
- schema: resolver(Command.Info.array()),
- },
- },
- },
- },
- }),
- async (c) => {
- const commands = await AppRuntime.runPromise(Command.Service.use((svc) => svc.list()))
- return c.json(commands)
- },
- )
- .get(
- "/agent",
- describeRoute({
- summary: "List agents",
- description: "Get a list of all available AI agents in the OpenCode system.",
- operationId: "app.agents",
- responses: {
- 200: {
- description: "List of agents",
- content: {
- "application/json": {
- schema: resolver(Agent.Info.array()),
- },
- },
- },
- },
- }),
- async (c) => {
- const modes = await AppRuntime.runPromise(Agent.Service.use((svc) => svc.list()))
- return c.json(modes)
- },
- )
- .get(
- "/skill",
- describeRoute({
- summary: "List skills",
- description: "Get a list of all available skills in the OpenCode system.",
- operationId: "app.skills",
- responses: {
- 200: {
- description: "List of skills",
- content: {
- "application/json": {
- schema: resolver(Skill.Info.array()),
- },
- },
- },
- },
- }),
- async (c) => {
- const skills = await AppRuntime.runPromise(
- Effect.gen(function* () {
- const skill = yield* Skill.Service
- return yield* skill.all()
- }),
- )
- return c.json(skills)
- },
- )
- .get(
- "/lsp",
- describeRoute({
- summary: "Get LSP status",
- description: "Get LSP server status",
- operationId: "lsp.status",
- responses: {
- 200: {
- description: "LSP server status",
- content: {
- "application/json": {
- schema: resolver(LSP.Status.array()),
- },
- },
- },
- },
- }),
- async (c) => {
- const items = await AppRuntime.runPromise(LSP.Service.use((lsp) => lsp.status()))
- return c.json(items)
- },
- )
- .get(
- "/formatter",
- describeRoute({
- summary: "Get formatter status",
- description: "Get formatter status",
- operationId: "formatter.status",
- responses: {
- 200: {
- description: "Formatter status",
- content: {
- "application/json": {
- schema: resolver(Format.Status.array()),
- },
- },
- },
- },
- }),
- async (c) => {
- return c.json(await AppRuntime.runPromise(Format.Service.use((svc) => svc.status())))
- },
- )
- }
|