opencode_offline/packages/opencode/src/server/server.ts

1248 lines
33 KiB
TypeScript
Raw Normal View History

2025-06-01 02:41:00 +08:00
import { Log } from "../util/log"
import { Bus } from "../bus"
import { describeRoute, generateSpecs, openAPISpecs } from "hono-openapi"
import { Hono } from "hono"
import { streamSSE } from "hono/streaming"
import { Session } from "../session"
2025-06-01 02:41:00 +08:00
import { resolver, validator as zValidator } from "hono-openapi/zod"
import { z } from "zod"
import { Provider } from "../provider/provider"
2025-06-02 03:01:57 +08:00
import { App } from "../app/app"
2025-06-20 20:48:42 +08:00
import { mapValues } from "remeda"
2025-06-10 02:01:11 +08:00
import { NamedError } from "../util/error"
import { ModelsDev } from "../provider/models"
import { Ripgrep } from "../file/ripgrep"
2025-06-19 10:20:03 +08:00
import { Config } from "../config/config"
import { File } from "../file"
import { LSP } from "../lsp"
import { MessageV2 } from "../session/message-v2"
2025-07-22 07:53:22 +08:00
import { callTui, TuiRoute } from "./tui"
2025-07-31 22:34:43 +08:00
import { Permission } from "../permission"
2025-08-03 06:50:19 +08:00
import { lazy } from "../util/lazy"
import { Agent } from "../agent/agent"
import { Auth } from "../auth"
2025-06-10 02:01:11 +08:00
const ERRORS = {
400: {
description: "Bad request",
content: {
"application/json": {
schema: resolver(
z
.object({
data: z.record(z.string(), z.any()),
})
.openapi({
ref: "Error",
}),
),
},
},
},
} as const
2025-05-19 02:13:04 +08:00
export namespace Server {
2025-06-01 02:41:00 +08:00
const log = Log.create({ service: "server" })
2025-05-19 02:13:04 +08:00
export type Routes = ReturnType<typeof app>
2025-05-19 02:13:04 +08:00
export const Event = {
Connected: Bus.event("server.connected", z.object({})),
}
2025-08-03 06:50:19 +08:00
export const app = lazy(() => {
2025-06-01 02:41:00 +08:00
const app = new Hono()
2025-05-19 10:30:41 +08:00
const result = app
2025-06-05 08:49:28 +08:00
.onError((err, c) => {
2025-06-10 02:01:11 +08:00
if (err instanceof NamedError) {
return c.json(err.toObject(), {
status: 400,
})
}
return c.json(new NamedError.Unknown({ message: err.toString() }).toObject(), {
status: 400,
})
2025-06-05 08:49:28 +08:00
})
.use(async (c, next) => {
const skipLogging = c.req.path === "/log"
if (!skipLogging) {
log.info("request", {
method: c.req.method,
path: c.req.path,
})
}
const start = Date.now()
await next()
if (!skipLogging) {
log.info("response", {
duration: Date.now() - start,
})
}
2025-06-04 01:00:27 +08:00
})
2025-05-19 10:30:41 +08:00
.get(
"/doc",
2025-05-19 10:30:41 +08:00
openAPISpecs(app, {
documentation: {
info: {
title: "opencode",
version: "0.0.3",
2025-05-19 10:30:41 +08:00
description: "opencode api",
},
openapi: "3.1.1",
2025-05-19 10:30:41 +08:00
},
}),
)
2025-05-29 23:32:55 +08:00
.get(
"/event",
describeRoute({
description: "Get events",
2025-07-31 13:00:29 +08:00
operationId: "event.subscribe",
2025-05-29 23:32:55 +08:00
responses: {
200: {
description: "Event stream",
content: {
"application/json": {
schema: resolver(
Bus.payloads().openapi({
ref: "Event",
}),
),
},
},
},
},
}),
async (c) => {
2025-06-01 02:41:00 +08:00
log.info("event connected")
2025-05-29 23:32:55 +08:00
return streamSSE(c, async (stream) => {
stream.writeSSE({
data: JSON.stringify({
type: "server.connected",
properties: {},
}),
2025-06-01 02:41:00 +08:00
})
2025-05-29 23:32:55 +08:00
const unsub = Bus.subscribeAll(async (event) => {
await stream.writeSSE({
data: JSON.stringify(event),
2025-06-01 02:41:00 +08:00
})
})
2025-05-29 23:32:55 +08:00
await new Promise<void>((resolve) => {
stream.onAbort(() => {
2025-06-01 02:41:00 +08:00
unsub()
resolve()
log.info("event disconnected")
})
})
})
2025-05-29 23:32:55 +08:00
},
)
.get(
"/app",
describeRoute({
description: "Get app info",
2025-07-31 13:00:29 +08:00
operationId: "app.get",
responses: {
200: {
description: "200",
content: {
"application/json": {
schema: resolver(App.Info),
},
},
},
},
}),
async (c) => {
return c.json(App.info())
},
)
2025-06-19 10:20:03 +08:00
.post(
"/app/init",
2025-06-04 02:24:45 +08:00
describeRoute({
description: "Initialize the app",
2025-07-31 13:00:29 +08:00
operationId: "app.init",
2025-06-04 02:24:45 +08:00
responses: {
200: {
description: "Initialize the app",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
async (c) => {
await App.initialize()
return c.json(true)
},
)
.get(
"/config",
2025-06-04 00:38:48 +08:00
describeRoute({
description: "Get config info",
2025-07-31 13:00:29 +08:00
operationId: "config.get",
2025-06-04 00:38:48 +08:00
responses: {
200: {
description: "Get config info",
2025-06-04 00:38:48 +08:00
content: {
"application/json": {
schema: resolver(Config.Info),
2025-06-04 00:38:48 +08:00
},
},
},
},
}),
async (c) => {
return c.json(await Config.get())
2025-06-04 00:38:48 +08:00
},
)
.get(
"/session",
2025-06-02 03:01:57 +08:00
describeRoute({
description: "List all sessions",
2025-07-31 13:00:29 +08:00
operationId: "session.list",
2025-06-02 03:01:57 +08:00
responses: {
200: {
description: "List of sessions",
2025-06-02 03:01:57 +08:00
content: {
"application/json": {
schema: resolver(Session.Info.array()),
2025-06-02 03:01:57 +08:00
},
},
},
},
}),
async (c) => {
const sessions = await Array.fromAsync(Session.list())
sessions.sort((a, b) => b.time.updated - a.time.updated)
return c.json(sessions)
2025-06-02 03:01:57 +08:00
},
)
2025-08-07 08:24:36 +08:00
.get(
2025-08-08 03:28:18 +08:00
"/session/:id",
2025-08-07 08:24:36 +08:00
describeRoute({
description: "Get session",
operationId: "session.get",
responses: {
200: {
description: "Get session",
content: {
"application/json": {
schema: resolver(Session.Info),
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string(),
}),
),
async (c) => {
const sessionID = c.req.valid("param").id
const session = await Session.get(sessionID)
return c.json(session)
},
)
.get(
"/session/:id/children",
describeRoute({
description: "Get a session's children",
operationId: "session.children",
responses: {
200: {
description: "List of children",
content: {
"application/json": {
schema: resolver(Session.Info.array()),
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string(),
}),
),
async (c) => {
const sessionID = c.req.valid("param").id
const session = await Session.children(sessionID)
return c.json(session)
},
)
2025-05-19 10:30:41 +08:00
.post(
"/session",
2025-05-19 10:30:41 +08:00
describeRoute({
description: "Create a new session",
2025-07-31 13:00:29 +08:00
operationId: "session.create",
2025-05-19 10:30:41 +08:00
responses: {
2025-06-10 02:01:11 +08:00
...ERRORS,
2025-05-19 10:30:41 +08:00
200: {
description: "Successfully created session",
content: {
"application/json": {
2025-05-29 23:38:55 +08:00
schema: resolver(Session.Info),
2025-05-20 23:11:06 +08:00
},
},
},
},
}),
zValidator(
"json",
z
.object({
parentID: z.string().optional(),
title: z.string().optional(),
})
.optional(),
),
2025-05-20 23:11:06 +08:00
async (c) => {
const body = c.req.valid("json") ?? {}
const session = await Session.create(body.parentID, body.title)
2025-06-01 02:41:00 +08:00
return c.json(session)
2025-05-20 23:11:06 +08:00
},
)
.delete(
"/session/:id",
2025-05-27 06:06:41 +08:00
describeRoute({
description: "Delete a session and all its data",
2025-07-31 13:00:29 +08:00
operationId: "session.delete",
2025-05-27 06:06:41 +08:00
responses: {
200: {
description: "Successfully deleted session",
2025-05-27 06:06:41 +08:00
content: {
"application/json": {
schema: resolver(z.boolean()),
2025-05-27 06:06:41 +08:00
},
},
},
},
}),
zValidator(
"param",
2025-05-27 06:06:41 +08:00
z.object({
id: z.string(),
2025-05-27 06:06:41 +08:00
}),
),
async (c) => {
await Session.remove(c.req.valid("param").id)
return c.json(true)
2025-05-27 06:06:41 +08:00
},
)
.patch(
"/session/:id",
describeRoute({
description: "Update session properties",
operationId: "session.update",
responses: {
200: {
description: "Successfully updated session",
content: {
"application/json": {
schema: resolver(Session.Info),
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string(),
}),
),
zValidator(
"json",
z.object({
title: z.string().optional(),
}),
),
async (c) => {
const sessionID = c.req.valid("param").id
const updates = c.req.valid("json")
const updatedSession = await Session.update(sessionID, (session) => {
if (updates.title !== undefined) {
session.title = updates.title
}
})
return c.json(updatedSession)
},
)
2025-05-20 23:11:06 +08:00
.post(
"/session/:id/init",
2025-06-21 03:22:41 +08:00
describeRoute({
description: "Analyze the app and create an AGENTS.md file",
2025-07-31 13:00:29 +08:00
operationId: "session.init",
2025-06-21 03:22:41 +08:00
responses: {
200: {
description: "200",
2025-06-21 03:22:41 +08:00
content: {
"application/json": {
schema: resolver(z.boolean()),
2025-06-21 03:22:41 +08:00
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string().openapi({ description: "Session ID" }),
}),
),
2025-06-21 03:22:41 +08:00
zValidator(
"json",
z.object({
2025-07-14 05:22:11 +08:00
messageID: z.string(),
providerID: z.string(),
modelID: z.string(),
2025-06-21 03:22:41 +08:00
}),
),
async (c) => {
const sessionID = c.req.valid("param").id
2025-06-21 03:22:41 +08:00
const body = c.req.valid("json")
await Session.initialize({ ...body, sessionID })
return c.json(true)
2025-06-21 03:22:41 +08:00
},
)
.post(
"/session/:id/abort",
2025-05-20 23:11:06 +08:00
describeRoute({
description: "Abort a session",
2025-07-31 13:00:29 +08:00
operationId: "session.abort",
2025-05-20 23:11:06 +08:00
responses: {
200: {
description: "Aborted session",
2025-05-20 23:11:06 +08:00
content: {
"application/json": {
schema: resolver(z.boolean()),
2025-05-19 10:30:41 +08:00
},
},
},
},
}),
2025-05-27 06:10:10 +08:00
zValidator(
"param",
2025-05-27 06:10:10 +08:00
z.object({
id: z.string(),
2025-05-27 06:10:10 +08:00
}),
),
2025-05-19 10:30:41 +08:00
async (c) => {
return c.json(Session.abort(c.req.valid("param").id))
2025-05-19 10:30:41 +08:00
},
)
2025-05-28 03:34:46 +08:00
.post(
"/session/:id/share",
2025-05-28 03:34:46 +08:00
describeRoute({
description: "Share a session",
2025-07-31 13:00:29 +08:00
operationId: "session.share",
2025-05-28 03:34:46 +08:00
responses: {
200: {
description: "Successfully shared session",
2025-05-28 03:34:46 +08:00
content: {
"application/json": {
schema: resolver(Session.Info),
2025-05-28 03:34:46 +08:00
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string(),
}),
),
2025-05-28 03:34:46 +08:00
async (c) => {
const id = c.req.valid("param").id
await Session.share(id)
const session = await Session.get(id)
return c.json(session)
2025-05-28 03:34:46 +08:00
},
)
.delete(
"/session/:id/share",
2025-05-29 03:07:51 +08:00
describeRoute({
description: "Unshare the session",
2025-07-31 13:00:29 +08:00
operationId: "session.unshare",
2025-05-29 03:07:51 +08:00
responses: {
200: {
description: "Successfully unshared session",
2025-05-29 03:07:51 +08:00
content: {
"application/json": {
schema: resolver(Session.Info),
2025-05-29 03:07:51 +08:00
},
},
},
},
}),
zValidator(
"param",
2025-05-29 03:07:51 +08:00
z.object({
id: z.string(),
2025-05-29 03:07:51 +08:00
}),
),
async (c) => {
const id = c.req.valid("param").id
await Session.unshare(id)
const session = await Session.get(id)
return c.json(session)
2025-05-29 03:07:51 +08:00
},
2025-05-30 01:17:56 +08:00
)
.post(
"/session/:id/summarize",
describeRoute({
description: "Summarize the session",
2025-07-31 13:00:29 +08:00
operationId: "session.summarize",
responses: {
200: {
description: "Summarized session",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string().openapi({ description: "Session ID" }),
}),
),
zValidator(
"json",
z.object({
providerID: z.string(),
modelID: z.string(),
}),
),
async (c) => {
const id = c.req.valid("param").id
const body = c.req.valid("json")
await Session.summarize({ ...body, sessionID: id })
return c.json(true)
},
)
.get(
"/session/:id/message",
2025-05-30 01:17:56 +08:00
describeRoute({
description: "List messages for a session",
2025-07-31 13:00:29 +08:00
operationId: "session.messages",
2025-05-30 01:17:56 +08:00
responses: {
200: {
description: "List of messages",
2025-05-30 01:17:56 +08:00
content: {
"application/json": {
2025-07-14 05:22:11 +08:00
schema: resolver(
z
.object({
info: MessageV2.Info,
parts: MessageV2.Part.array(),
})
.array(),
),
2025-05-30 01:17:56 +08:00
},
},
},
},
}),
zValidator(
"param",
2025-05-30 01:17:56 +08:00
z.object({
id: z.string().openapi({ description: "Session ID" }),
2025-05-30 01:17:56 +08:00
}),
),
async (c) => {
const messages = await Session.messages(c.req.valid("param").id)
return c.json(messages)
2025-05-30 01:17:56 +08:00
},
2025-05-29 03:07:51 +08:00
)
2025-07-31 22:34:43 +08:00
.get(
"/session/:id/message/:messageID",
describeRoute({
description: "Get a message from a session",
2025-07-31 23:19:42 +08:00
operationId: "session.message",
2025-07-31 22:34:43 +08:00
responses: {
200: {
description: "Message",
content: {
"application/json": {
schema: resolver(
z.object({
info: MessageV2.Info,
parts: MessageV2.Part.array(),
}),
),
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string().openapi({ description: "Session ID" }),
messageID: z.string().openapi({ description: "Message ID" }),
}),
),
async (c) => {
const params = c.req.valid("param")
const message = await Session.getMessage(params.id, params.messageID)
return c.json(message)
},
)
2025-05-19 02:13:04 +08:00
.post(
"/session/:id/message",
2025-05-29 03:07:51 +08:00
describeRoute({
description: "Create and send a new message to a session",
2025-07-31 13:00:29 +08:00
operationId: "session.chat",
2025-05-29 03:07:51 +08:00
responses: {
200: {
description: "Created message",
2025-05-29 03:07:51 +08:00
content: {
"application/json": {
schema: resolver(MessageV2.Assistant),
2025-05-29 03:07:51 +08:00
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string().openapi({ description: "Session ID" }),
}),
),
2025-07-19 01:42:50 +08:00
zValidator("json", Session.ChatInput.omit({ sessionID: true })),
2025-05-19 02:13:04 +08:00
async (c) => {
const sessionID = c.req.valid("param").id
2025-06-01 02:41:00 +08:00
const body = c.req.valid("json")
const msg = await Session.chat({ ...body, sessionID })
2025-06-01 02:41:00 +08:00
return c.json(msg)
2025-05-18 09:31:42 +08:00
},
2025-05-29 00:53:22 +08:00
)
2025-08-14 01:28:51 +08:00
.post(
2025-08-14 03:25:51 +08:00
"/session/:id/shell",
2025-08-14 01:28:51 +08:00
describeRoute({
2025-08-14 03:25:51 +08:00
description: "Run a shell command",
operationId: "session.shell",
2025-08-14 01:28:51 +08:00
responses: {
200: {
description: "Created message",
content: {
"application/json": {
schema: resolver(MessageV2.Assistant),
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string().openapi({ description: "Session ID" }),
}),
),
zValidator("json", Session.CommandInput.omit({ sessionID: true })),
async (c) => {
const sessionID = c.req.valid("param").id
const body = c.req.valid("json")
2025-08-14 03:25:51 +08:00
const msg = await Session.shell({ ...body, sessionID })
2025-08-14 01:28:51 +08:00
return c.json(msg)
},
)
.post(
"/session/:id/revert",
describeRoute({
description: "Revert a message",
2025-07-31 13:00:29 +08:00
operationId: "session.revert",
responses: {
200: {
description: "Updated session",
content: {
"application/json": {
schema: resolver(Session.Info),
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string(),
}),
),
zValidator("json", Session.RevertInput.omit({ sessionID: true })),
async (c) => {
const id = c.req.valid("param").id
2025-07-24 08:42:02 +08:00
log.info("revert", c.req.valid("json"))
const session = await Session.revert({ sessionID: id, ...c.req.valid("json") })
return c.json(session)
},
)
.post(
"/session/:id/unrevert",
describeRoute({
description: "Restore all reverted messages",
2025-07-31 13:00:29 +08:00
operationId: "session.unrevert",
responses: {
200: {
description: "Updated session",
content: {
"application/json": {
schema: resolver(Session.Info),
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string(),
}),
),
async (c) => {
const id = c.req.valid("param").id
const session = await Session.unrevert({ sessionID: id })
return c.json(session)
},
)
2025-07-31 22:34:43 +08:00
.post(
"/session/:id/permissions/:permissionID",
describeRoute({
description: "Respond to a permission request",
responses: {
200: {
description: "Permission processed successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
zValidator(
"param",
z.object({
id: z.string(),
permissionID: z.string(),
}),
),
zValidator("json", z.object({ response: Permission.Response })),
async (c) => {
const params = c.req.valid("param")
const id = params.id
const permissionID = params.permissionID
Permission.respond({ sessionID: id, permissionID, response: c.req.valid("json").response })
return c.json(true)
},
)
.get(
"/config/providers",
2025-05-29 00:53:22 +08:00
describeRoute({
description: "List all providers",
2025-07-31 13:00:29 +08:00
operationId: "config.providers",
2025-05-29 00:53:22 +08:00
responses: {
200: {
description: "List of providers",
content: {
"application/json": {
2025-06-06 02:59:07 +08:00
schema: resolver(
z.object({
providers: ModelsDev.Provider.array(),
2025-06-06 02:59:07 +08:00
default: z.record(z.string(), z.string()),
}),
),
2025-05-29 00:53:22 +08:00
},
},
},
},
}),
async (c) => {
const providers = await Provider.list().then((x) => mapValues(x, (item) => item.info))
2025-06-06 02:59:07 +08:00
return c.json({
providers: Object.values(providers),
default: mapValues(providers, (item) => Provider.sort(Object.values(item.models))[0].id),
2025-06-06 02:59:07 +08:00
})
2025-05-29 00:53:22 +08:00
},
2025-06-01 02:41:00 +08:00
)
.get(
"/find",
describeRoute({
description: "Find text in files",
2025-07-31 13:00:29 +08:00
operationId: "find.text",
responses: {
200: {
description: "Matches",
content: {
"application/json": {
schema: resolver(Ripgrep.Match.shape.data.array()),
},
},
},
},
}),
zValidator(
"query",
z.object({
pattern: z.string(),
}),
),
async (c) => {
const app = App.info()
const pattern = c.req.valid("query").pattern
const result = await Ripgrep.search({
cwd: app.path.cwd,
pattern,
limit: 10,
})
return c.json(result)
},
)
.get(
"/find/file",
describeRoute({
description: "Find files",
2025-07-31 13:00:29 +08:00
operationId: "find.files",
responses: {
200: {
description: "File paths",
content: {
"application/json": {
schema: resolver(z.string().array()),
},
},
},
},
}),
zValidator(
"query",
z.object({
query: z.string(),
}),
),
async (c) => {
const query = c.req.valid("query").query
const app = App.info()
const result = await Ripgrep.files({
cwd: app.path.cwd,
query,
limit: 10,
})
return c.json(result)
},
)
.get(
"/find/symbol",
describeRoute({
description: "Find workspace symbols",
2025-07-31 13:00:29 +08:00
operationId: "find.symbols",
responses: {
200: {
description: "Symbols",
content: {
"application/json": {
2025-07-09 08:04:55 +08:00
schema: resolver(LSP.Symbol.array()),
},
},
},
},
}),
zValidator(
"query",
z.object({
query: z.string(),
}),
),
async (c) => {
const query = c.req.valid("query").query
const result = await LSP.workspaceSymbol(query)
return c.json(result)
},
)
.get(
"/file",
describeRoute({
description: "Read a file",
2025-07-31 13:00:29 +08:00
operationId: "file.read",
responses: {
200: {
description: "File content",
content: {
"application/json": {
schema: resolver(
z.object({
type: z.enum(["raw", "patch"]),
content: z.string(),
}),
),
},
},
},
},
}),
zValidator(
"query",
z.object({
path: z.string(),
}),
),
async (c) => {
const path = c.req.valid("query").path
const content = await File.read(path)
log.info("read file", {
path,
content: content.content,
})
return c.json(content)
},
)
.get(
"/file/status",
describeRoute({
description: "Get file status",
2025-07-31 13:00:29 +08:00
operationId: "file.status",
responses: {
200: {
description: "File status",
content: {
"application/json": {
2025-07-09 21:16:10 +08:00
schema: resolver(File.Info.array()),
},
},
},
},
}),
async (c) => {
const content = await File.status()
return c.json(content)
},
)
2025-07-09 21:16:10 +08:00
.post(
"/log",
describeRoute({
description: "Write a log entry to the server logs",
2025-07-31 13:00:29 +08:00
operationId: "app.log",
2025-07-09 21:16:10 +08:00
responses: {
200: {
description: "Log entry written successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
zValidator(
"json",
z.object({
service: z.string().openapi({ description: "Service name for the log entry" }),
2025-07-09 23:00:03 +08:00
level: z.enum(["debug", "info", "error", "warn"]).openapi({ description: "Log level" }),
2025-07-09 21:16:10 +08:00
message: z.string().openapi({ description: "Log message" }),
extra: z
.record(z.string(), z.any())
.optional()
.openapi({ 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) {
2025-07-09 23:00:03 +08:00
case "debug":
logger.debug(message, extra)
break
2025-07-09 21:16:10 +08:00
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)
},
)
2025-07-10 03:44:59 +08:00
.get(
"/agent",
2025-07-10 03:44:59 +08:00
describeRoute({
description: "List all agents",
operationId: "app.agents",
2025-07-10 03:44:59 +08:00
responses: {
200: {
description: "List of agents",
2025-07-10 03:44:59 +08:00
content: {
"application/json": {
schema: resolver(Agent.Info.array()),
2025-07-10 03:44:59 +08:00
},
},
},
},
}),
async (c) => {
const modes = await Agent.list()
2025-07-10 03:44:59 +08:00
return c.json(modes)
},
)
2025-07-22 07:53:22 +08:00
.post(
2025-07-23 00:14:14 +08:00
"/tui/append-prompt",
2025-07-22 07:53:22 +08:00
describeRoute({
2025-07-23 00:14:14 +08:00
description: "Append prompt to the TUI",
2025-07-31 13:00:29 +08:00
operationId: "tui.appendPrompt",
2025-07-22 07:53:22 +08:00
responses: {
200: {
description: "Prompt processed successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
zValidator(
"json",
z.object({
text: z.string(),
}),
),
async (c) => c.json(await callTui(c)),
)
.post(
"/tui/open-help",
describeRoute({
description: "Open the help dialog",
2025-07-31 13:00:29 +08:00
operationId: "tui.openHelp",
2025-07-22 07:53:22 +08:00
responses: {
200: {
description: "Help dialog opened successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
async (c) => c.json(await callTui(c)),
)
2025-08-01 00:24:23 +08:00
.post(
"/tui/open-sessions",
describeRoute({
description: "Open the session dialog",
operationId: "tui.openSessions",
responses: {
200: {
description: "Session dialog opened successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
async (c) => c.json(await callTui(c)),
)
.post(
"/tui/open-themes",
describeRoute({
description: "Open the theme dialog",
operationId: "tui.openThemes",
responses: {
200: {
description: "Theme dialog opened successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
async (c) => c.json(await callTui(c)),
)
.post(
"/tui/open-models",
describeRoute({
description: "Open the model dialog",
operationId: "tui.openModels",
responses: {
200: {
description: "Model dialog opened successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
async (c) => c.json(await callTui(c)),
)
.post(
"/tui/submit-prompt",
describeRoute({
description: "Submit the prompt",
operationId: "tui.submitPrompt",
responses: {
200: {
description: "Prompt submitted successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
async (c) => c.json(await callTui(c)),
)
.post(
"/tui/clear-prompt",
describeRoute({
description: "Clear the prompt",
operationId: "tui.clearPrompt",
responses: {
200: {
description: "Prompt cleared successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
async (c) => c.json(await callTui(c)),
)
.post(
"/tui/execute-command",
describeRoute({
2025-08-15 20:43:30 +08:00
description: "Execute a TUI command (e.g. agent_cycle)",
2025-08-01 00:24:23 +08:00
operationId: "tui.executeCommand",
responses: {
200: {
description: "Command executed successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
zValidator(
"json",
z.object({
command: z.string(),
}),
),
async (c) => c.json(await callTui(c)),
)
2025-08-15 21:39:58 +08:00
.post(
"/tui/show-toast",
describeRoute({
description: "Show a toast notification in the TUI",
operationId: "tui.showToast",
responses: {
200: {
description: "Toast notification shown successfully",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
},
}),
zValidator(
"json",
z.object({
title: z.string().optional(),
message: z.string(),
variant: z.enum(["info", "success", "warning", "error"]),
}),
),
async (c) => c.json(await callTui(c)),
)
2025-07-22 07:53:22 +08:00
.route("/tui/control", TuiRoute)
.put(
"/auth/:id",
describeRoute({
description: "Set authentication credentials",
operationId: "auth.set",
responses: {
200: {
description: "Successfully set authentication credentials",
content: {
"application/json": {
schema: resolver(z.boolean()),
},
},
},
...ERRORS,
},
}),
zValidator(
"param",
z.object({
id: z.string(),
}),
),
zValidator("json", Auth.Info),
async (c) => {
const id = c.req.valid("param").id
const info = c.req.valid("json")
await Auth.set(id, info)
return c.json(true)
},
)
2025-05-19 10:30:41 +08:00
2025-06-01 02:41:00 +08:00
return result
2025-08-03 06:50:19 +08:00
})
2025-05-19 10:30:41 +08:00
export async function openapi() {
2025-06-01 02:41:00 +08:00
const a = app()
2025-05-19 10:30:41 +08:00
const result = await generateSpecs(a, {
documentation: {
info: {
title: "opencode",
version: "1.0.0",
description: "opencode api",
},
openapi: "3.1.1",
2025-05-19 10:30:41 +08:00
},
2025-06-01 02:41:00 +08:00
})
return result
2025-05-19 02:28:08 +08:00
}
2025-05-19 02:13:04 +08:00
2025-06-25 08:52:09 +08:00
export function listen(opts: { port: number; hostname: string }) {
2025-05-19 02:28:08 +08:00
const server = Bun.serve({
2025-06-25 08:52:09 +08:00
port: opts.port,
hostname: opts.hostname,
2025-05-19 02:13:04 +08:00
idleTimeout: 0,
2025-05-19 02:28:08 +08:00
fetch: app().fetch,
2025-06-01 02:41:00 +08:00
})
return server
2025-05-18 09:31:42 +08:00
}
}