| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
- import { createHash } from "node:crypto"
- import { Log } from "../util/log"
- import { describeRoute, generateSpecs, validator, resolver, openAPIRouteHandler } from "hono-openapi"
- import { Hono } from "hono"
- import { cors } from "hono/cors"
- import { proxy } from "hono/proxy"
- import { basicAuth } from "hono/basic-auth"
- import z from "zod"
- import { Provider } from "../provider/provider"
- import { NamedError } from "@opencode-ai/util/error"
- import { LSP } from "../lsp"
- import { Format } from "../format"
- import { TuiRoutes } from "./routes/tui"
- import { Instance } from "../project/instance"
- import { Vcs } from "../project/vcs"
- import { Agent } from "../agent/agent"
- import { Skill } from "../skill"
- import { Auth } from "../auth"
- import { Flag } from "../flag/flag"
- import { Command } from "../command"
- import { Global } from "../global"
- import { WorkspaceContext } from "../control-plane/workspace-context"
- import { WorkspaceID } from "../control-plane/schema"
- import { ProviderID } from "../provider/schema"
- import { WorkspaceRouterMiddleware } from "../control-plane/workspace-router-middleware"
- import { ProjectRoutes } from "./routes/project"
- import { SessionRoutes } from "./routes/session"
- import { PtyRoutes } from "./routes/pty"
- import { McpRoutes } from "./routes/mcp"
- import { FileRoutes } from "./routes/file"
- import { ConfigRoutes } from "./routes/config"
- import { ExperimentalRoutes } from "./routes/experimental"
- import { ProviderRoutes } from "./routes/provider"
- import { EventRoutes } from "./routes/event"
- import { InstanceBootstrap } from "../project/bootstrap"
- import { NotFoundError } from "../storage/db"
- import type { ContentfulStatusCode } from "hono/utils/http-status"
- import { websocket } from "hono/bun"
- import { HTTPException } from "hono/http-exception"
- import { errors } from "./error"
- import { Filesystem } from "@/util/filesystem"
- import { Snapshot } from "@/snapshot"
- import { QuestionRoutes } from "./routes/question"
- import { PermissionRoutes } from "./routes/permission"
- import { GlobalRoutes } from "./routes/global"
- import { MDNS } from "./mdns"
- import { lazy } from "@/util/lazy"
- import { initProjectors } from "./projectors"
- // @ts-ignore This global is needed to prevent ai-sdk from logging warnings to stdout https://github.com/vercel/ai/blob/2dc67e0ef538307f21368db32d5a12345d98831b/packages/ai/src/logger/log-warnings.ts#L85
- globalThis.AI_SDK_LOG_WARNINGS = false
- const csp = (hash = "") =>
- `default-src 'self'; script-src 'self' 'wasm-unsafe-eval'${hash ? ` 'sha256-${hash}'` : ""}; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; media-src 'self' data:; connect-src 'self' data:`
- initProjectors()
- export namespace Server {
- const log = Log.create({ service: "server" })
- export const Default = lazy(() => createApp({}))
- export const createApp = (opts: { cors?: string[] }): Hono => {
- const app = new Hono()
- return app
- .onError((err, c) => {
- log.error("failed", {
- error: err,
- })
- if (err instanceof NamedError) {
- let status: ContentfulStatusCode
- if (err instanceof NotFoundError) status = 404
- else if (err instanceof Provider.ModelNotFoundError) status = 400
- else if (err.name === "ProviderAuthValidationFailed") status = 400
- else if (err.name.startsWith("Worktree")) status = 400
- else status = 500
- return c.json(err.toObject(), { status })
- }
- if (err instanceof HTTPException) return err.getResponse()
- const message = err instanceof Error && err.stack ? err.stack : err.toString()
- return c.json(new NamedError.Unknown({ message }).toObject(), {
- status: 500,
- })
- })
- .use((c, next) => {
- // Allow CORS preflight requests to succeed without auth.
- // Browser clients sending Authorization headers will preflight with OPTIONS.
- if (c.req.method === "OPTIONS") return next()
- const password = Flag.OPENCODE_SERVER_PASSWORD
- if (!password) return next()
- const username = Flag.OPENCODE_SERVER_USERNAME ?? "opencode"
- return basicAuth({ username, password })(c, next)
- })
- .use(async (c, next) => {
- const skipLogging = c.req.path === "/log"
- if (!skipLogging) {
- log.info("request", {
- method: c.req.method,
- path: c.req.path,
- })
- }
- const timer = log.time("request", {
- method: c.req.method,
- path: c.req.path,
- })
- await next()
- if (!skipLogging) {
- timer.stop()
- }
- })
- .use(
- cors({
- origin(input) {
- if (!input) return
- if (input.startsWith("http://localhost:")) return input
- if (input.startsWith("http://127.0.0.1:")) return input
- if (
- input === "tauri://localhost" ||
- input === "http://tauri.localhost" ||
- input === "https://tauri.localhost"
- )
- return input
- // *.opencode.ai (https only, adjust if needed)
- if (/^https:\/\/([a-z0-9-]+\.)*opencode\.ai$/.test(input)) {
- return input
- }
- if (opts?.cors?.includes(input)) {
- return input
- }
- return
- },
- }),
- )
- .route("/global", GlobalRoutes())
- .put(
- "/auth/:providerID",
- describeRoute({
- summary: "Set auth credentials",
- description: "Set authentication credentials",
- operationId: "auth.set",
- responses: {
- 200: {
- description: "Successfully set authentication credentials",
- content: {
- "application/json": {
- schema: resolver(z.boolean()),
- },
- },
- },
- ...errors(400),
- },
- }),
- validator(
- "param",
- z.object({
- providerID: ProviderID.zod,
- }),
- ),
- validator("json", Auth.Info.zod),
- async (c) => {
- const providerID = c.req.valid("param").providerID
- const info = c.req.valid("json")
- await Auth.set(providerID, info)
- return c.json(true)
- },
- )
- .delete(
- "/auth/:providerID",
- describeRoute({
- summary: "Remove auth credentials",
- description: "Remove authentication credentials",
- operationId: "auth.remove",
- responses: {
- 200: {
- description: "Successfully removed authentication credentials",
- content: {
- "application/json": {
- schema: resolver(z.boolean()),
- },
- },
- },
- ...errors(400),
- },
- }),
- validator(
- "param",
- z.object({
- providerID: ProviderID.zod,
- }),
- ),
- async (c) => {
- const providerID = c.req.valid("param").providerID
- await Auth.remove(providerID)
- return c.json(true)
- },
- )
- .use(async (c, next) => {
- if (c.req.path === "/log") return next()
- const rawWorkspaceID = c.req.query("workspace") || c.req.header("x-opencode-workspace")
- const raw = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
- const directory = Filesystem.resolve(
- (() => {
- try {
- return decodeURIComponent(raw)
- } catch {
- return raw
- }
- })(),
- )
- return WorkspaceContext.provide({
- workspaceID: rawWorkspaceID ? WorkspaceID.make(rawWorkspaceID) : undefined,
- async fn() {
- return Instance.provide({
- directory,
- init: InstanceBootstrap,
- async fn() {
- return next()
- },
- })
- },
- })
- })
- .use(WorkspaceRouterMiddleware)
- .get(
- "/doc",
- openAPIRouteHandler(app, {
- documentation: {
- info: {
- title: "opencode",
- version: "0.0.3",
- description: "opencode api",
- },
- openapi: "3.1.1",
- },
- }),
- )
- .use(
- validator(
- "query",
- z.object({
- directory: z.string().optional(),
- workspace: z.string().optional(),
- }),
- ),
- )
- .route("/project", ProjectRoutes())
- .route("/pty", PtyRoutes())
- .route("/config", ConfigRoutes())
- .route("/experimental", ExperimentalRoutes())
- .route("/session", SessionRoutes())
- .route("/permission", PermissionRoutes())
- .route("/question", QuestionRoutes())
- .route("/provider", ProviderRoutes())
- .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) => {
- const [branch, default_branch] = await Promise.all([Vcs.branch(), Vcs.defaultBranch()])
- return c.json({
- 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(Snapshot.FileDiff.array()),
- },
- },
- },
- },
- }),
- validator(
- "query",
- z.object({
- mode: Vcs.Mode,
- }),
- ),
- async (c) => {
- return c.json(await 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 Command.list()
- return c.json(commands)
- },
- )
- .post(
- "/log",
- describeRoute({
- summary: "Write log",
- description: "Write a log entry to the server logs with specified level and metadata.",
- operationId: "app.log",
- responses: {
- 200: {
- description: "Log entry written successfully",
- content: {
- "application/json": {
- schema: resolver(z.boolean()),
- },
- },
- },
- ...errors(400),
- },
- }),
- validator(
- "json",
- z.object({
- service: z.string().meta({ description: "Service name for the log entry" }),
- level: z.enum(["debug", "info", "error", "warn"]).meta({ description: "Log level" }),
- message: z.string().meta({ description: "Log message" }),
- extra: z
- .record(z.string(), z.any())
- .optional()
- .meta({ description: "Additional metadata for the log entry" }),
- }),
- ),
- async (c) => {
- const { service, level, message, extra } = c.req.valid("json")
- const logger = Log.create({ service })
- switch (level) {
- case "debug":
- logger.debug(message, extra)
- break
- case "info":
- logger.info(message, extra)
- break
- case "error":
- logger.error(message, extra)
- break
- case "warn":
- logger.warn(message, extra)
- break
- }
- return c.json(true)
- },
- )
- .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 Agent.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 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) => {
- return c.json(await LSP.status())
- },
- )
- .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 Format.status())
- },
- )
- .all("/*", async (c) => {
- const path = c.req.path
- const response = await proxy(`https://app.opencode.ai${path}`, {
- ...c.req,
- headers: {
- ...c.req.raw.headers,
- host: "app.opencode.ai",
- },
- })
- const match = response.headers.get("content-type")?.includes("text/html")
- ? (await response.clone().text()).match(
- /<script\b(?![^>]*\bsrc\s*=)[^>]*\bid=(['"])oc-theme-preload-script\1[^>]*>([\s\S]*?)<\/script>/i,
- )
- : undefined
- const hash = match ? createHash("sha256").update(match[2]).digest("base64") : ""
- response.headers.set("Content-Security-Policy", csp(hash))
- return response
- })
- }
- export async function openapi() {
- // Cast to break excessive type recursion from long route chains
- const result = await generateSpecs(Default(), {
- documentation: {
- info: {
- title: "opencode",
- version: "1.0.0",
- description: "opencode api",
- },
- openapi: "3.1.1",
- },
- })
- return result
- }
- /** @deprecated do not use this dumb shit */
- export let url: URL
- export function listen(opts: {
- port: number
- hostname: string
- mdns?: boolean
- mdnsDomain?: string
- cors?: string[]
- }) {
- url = new URL(`http://${opts.hostname}:${opts.port}`)
- const app = createApp(opts)
- const args = {
- hostname: opts.hostname,
- idleTimeout: 0,
- fetch: app.fetch,
- websocket: websocket,
- } as const
- const tryServe = (port: number) => {
- try {
- return Bun.serve({ ...args, port })
- } catch {
- return undefined
- }
- }
- const server = opts.port === 0 ? (tryServe(4096) ?? tryServe(0)) : tryServe(opts.port)
- if (!server) throw new Error(`Failed to start server on port ${opts.port}`)
- const shouldPublishMDNS =
- opts.mdns &&
- server.port &&
- opts.hostname !== "127.0.0.1" &&
- opts.hostname !== "localhost" &&
- opts.hostname !== "::1"
- if (shouldPublishMDNS) {
- MDNS.publish(server.port!, opts.mdnsDomain)
- } else if (opts.mdns) {
- log.warn("mDNS enabled but hostname is loopback; skipping mDNS publish")
- }
- const originalStop = server.stop.bind(server)
- server.stop = async (closeActiveConnections?: boolean) => {
- if (shouldPublishMDNS) MDNS.unpublish()
- return originalStop(closeActiveConnections)
- }
- return server
- }
- }
|