server.ts 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873
  1. import { BusEvent } from "@/bus/bus-event"
  2. import { Bus } from "@/bus"
  3. import { GlobalBus } from "@/bus/global"
  4. import { Log } from "../util/log"
  5. import { describeRoute, generateSpecs, validator, resolver, openAPIRouteHandler } from "hono-openapi"
  6. import { Hono } from "hono"
  7. import { cors } from "hono/cors"
  8. import { stream, streamSSE } from "hono/streaming"
  9. import { proxy } from "hono/proxy"
  10. import { Session } from "../session"
  11. import z from "zod"
  12. import { Provider } from "../provider/provider"
  13. import { filter, mapValues, sortBy, pipe } from "remeda"
  14. import { NamedError } from "@opencode-ai/util/error"
  15. import { ModelsDev } from "../provider/models"
  16. import { Ripgrep } from "../file/ripgrep"
  17. import { Config } from "../config/config"
  18. import { File } from "../file"
  19. import { LSP } from "../lsp"
  20. import { Format } from "../format"
  21. import { MessageV2 } from "../session/message-v2"
  22. import { TuiRoute } from "./tui"
  23. import { Instance } from "../project/instance"
  24. import { Project } from "../project/project"
  25. import { Vcs } from "../project/vcs"
  26. import { Agent } from "../agent/agent"
  27. import { Auth } from "../auth"
  28. import { Command } from "../command"
  29. import { ProviderAuth } from "../provider/auth"
  30. import { Global } from "../global"
  31. import { ProjectRoute } from "./project"
  32. import { ToolRegistry } from "../tool/registry"
  33. import { zodToJsonSchema } from "zod-to-json-schema"
  34. import { SessionPrompt } from "../session/prompt"
  35. import { SessionCompaction } from "../session/compaction"
  36. import { SessionRevert } from "../session/revert"
  37. import { lazy } from "../util/lazy"
  38. import { Todo } from "../session/todo"
  39. import { InstanceBootstrap } from "../project/bootstrap"
  40. import { MCP } from "../mcp"
  41. import { Storage } from "../storage/storage"
  42. import type { ContentfulStatusCode } from "hono/utils/http-status"
  43. import { TuiEvent } from "@/cli/cmd/tui/event"
  44. import { Snapshot } from "@/snapshot"
  45. import { SessionSummary } from "@/session/summary"
  46. import { SessionStatus } from "@/session/status"
  47. import { upgradeWebSocket, websocket } from "hono/bun"
  48. import { errors } from "./error"
  49. import { Pty } from "@/pty"
  50. import { PermissionNext } from "@/permission/next"
  51. import { Installation } from "@/installation"
  52. import { MDNS } from "./mdns"
  53. import { Worktree } from "../worktree"
  54. // @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
  55. globalThis.AI_SDK_LOG_WARNINGS = false
  56. export namespace Server {
  57. const log = Log.create({ service: "server" })
  58. let _url: URL | undefined
  59. let _corsWhitelist: string[] = []
  60. export function url(): URL {
  61. return _url ?? new URL("http://localhost:4096")
  62. }
  63. export const Event = {
  64. Connected: BusEvent.define("server.connected", z.object({})),
  65. Disposed: BusEvent.define("global.disposed", z.object({})),
  66. }
  67. const app = new Hono()
  68. export const App = lazy(() =>
  69. app
  70. .onError((err, c) => {
  71. log.error("failed", {
  72. error: err,
  73. })
  74. if (err instanceof NamedError) {
  75. let status: ContentfulStatusCode
  76. if (err instanceof Storage.NotFoundError) status = 404
  77. else if (err instanceof Provider.ModelNotFoundError) status = 400
  78. else if (err.name.startsWith("Worktree")) status = 400
  79. else status = 500
  80. return c.json(err.toObject(), { status })
  81. }
  82. const message = err instanceof Error && err.stack ? err.stack : err.toString()
  83. return c.json(new NamedError.Unknown({ message }).toObject(), {
  84. status: 500,
  85. })
  86. })
  87. .use(async (c, next) => {
  88. const skipLogging = c.req.path === "/log"
  89. if (!skipLogging) {
  90. log.info("request", {
  91. method: c.req.method,
  92. path: c.req.path,
  93. })
  94. }
  95. const timer = log.time("request", {
  96. method: c.req.method,
  97. path: c.req.path,
  98. })
  99. await next()
  100. if (!skipLogging) {
  101. timer.stop()
  102. }
  103. })
  104. .use(
  105. cors({
  106. origin(input) {
  107. if (!input) return
  108. if (input.startsWith("http://localhost:")) return input
  109. if (input.startsWith("http://127.0.0.1:")) return input
  110. if (input === "tauri://localhost" || input === "http://tauri.localhost") return input
  111. // *.opencode.ai (https only, adjust if needed)
  112. if (/^https:\/\/([a-z0-9-]+\.)*opencode\.ai$/.test(input)) {
  113. return input
  114. }
  115. if (_corsWhitelist.includes(input)) {
  116. return input
  117. }
  118. return
  119. },
  120. }),
  121. )
  122. .get(
  123. "/global/health",
  124. describeRoute({
  125. summary: "Get health",
  126. description: "Get health information about the OpenCode server.",
  127. operationId: "global.health",
  128. responses: {
  129. 200: {
  130. description: "Health information",
  131. content: {
  132. "application/json": {
  133. schema: resolver(z.object({ healthy: z.literal(true), version: z.string() })),
  134. },
  135. },
  136. },
  137. },
  138. }),
  139. async (c) => {
  140. return c.json({ healthy: true, version: Installation.VERSION })
  141. },
  142. )
  143. .get(
  144. "/global/event",
  145. describeRoute({
  146. summary: "Get global events",
  147. description: "Subscribe to global events from the OpenCode system using server-sent events.",
  148. operationId: "global.event",
  149. responses: {
  150. 200: {
  151. description: "Event stream",
  152. content: {
  153. "text/event-stream": {
  154. schema: resolver(
  155. z
  156. .object({
  157. directory: z.string(),
  158. payload: BusEvent.payloads(),
  159. })
  160. .meta({
  161. ref: "GlobalEvent",
  162. }),
  163. ),
  164. },
  165. },
  166. },
  167. },
  168. }),
  169. async (c) => {
  170. log.info("global event connected")
  171. return streamSSE(c, async (stream) => {
  172. stream.writeSSE({
  173. data: JSON.stringify({
  174. payload: {
  175. type: "server.connected",
  176. properties: {},
  177. },
  178. }),
  179. })
  180. async function handler(event: any) {
  181. await stream.writeSSE({
  182. data: JSON.stringify(event),
  183. })
  184. }
  185. GlobalBus.on("event", handler)
  186. // Send heartbeat every 30s to prevent WKWebView timeout (60s default)
  187. const heartbeat = setInterval(() => {
  188. stream.writeSSE({
  189. data: JSON.stringify({
  190. payload: {
  191. type: "server.heartbeat",
  192. properties: {},
  193. },
  194. }),
  195. })
  196. }, 30000)
  197. await new Promise<void>((resolve) => {
  198. stream.onAbort(() => {
  199. clearInterval(heartbeat)
  200. GlobalBus.off("event", handler)
  201. resolve()
  202. log.info("global event disconnected")
  203. })
  204. })
  205. })
  206. },
  207. )
  208. .post(
  209. "/global/dispose",
  210. describeRoute({
  211. summary: "Dispose instance",
  212. description: "Clean up and dispose all OpenCode instances, releasing all resources.",
  213. operationId: "global.dispose",
  214. responses: {
  215. 200: {
  216. description: "Global disposed",
  217. content: {
  218. "application/json": {
  219. schema: resolver(z.boolean()),
  220. },
  221. },
  222. },
  223. },
  224. }),
  225. async (c) => {
  226. await Instance.disposeAll()
  227. GlobalBus.emit("event", {
  228. directory: "global",
  229. payload: {
  230. type: Event.Disposed.type,
  231. properties: {},
  232. },
  233. })
  234. return c.json(true)
  235. },
  236. )
  237. .use(async (c, next) => {
  238. const directory = c.req.query("directory") || c.req.header("x-opencode-directory") || process.cwd()
  239. return Instance.provide({
  240. directory,
  241. init: InstanceBootstrap,
  242. async fn() {
  243. return next()
  244. },
  245. })
  246. })
  247. .get(
  248. "/doc",
  249. openAPIRouteHandler(app, {
  250. documentation: {
  251. info: {
  252. title: "opencode",
  253. version: "0.0.3",
  254. description: "opencode api",
  255. },
  256. openapi: "3.1.1",
  257. },
  258. }),
  259. )
  260. .use(validator("query", z.object({ directory: z.string().optional() })))
  261. .route("/project", ProjectRoute)
  262. .get(
  263. "/pty",
  264. describeRoute({
  265. summary: "List PTY sessions",
  266. description: "Get a list of all active pseudo-terminal (PTY) sessions managed by OpenCode.",
  267. operationId: "pty.list",
  268. responses: {
  269. 200: {
  270. description: "List of sessions",
  271. content: {
  272. "application/json": {
  273. schema: resolver(Pty.Info.array()),
  274. },
  275. },
  276. },
  277. },
  278. }),
  279. async (c) => {
  280. return c.json(Pty.list())
  281. },
  282. )
  283. .post(
  284. "/pty",
  285. describeRoute({
  286. summary: "Create PTY session",
  287. description: "Create a new pseudo-terminal (PTY) session for running shell commands and processes.",
  288. operationId: "pty.create",
  289. responses: {
  290. 200: {
  291. description: "Created session",
  292. content: {
  293. "application/json": {
  294. schema: resolver(Pty.Info),
  295. },
  296. },
  297. },
  298. ...errors(400),
  299. },
  300. }),
  301. validator("json", Pty.CreateInput),
  302. async (c) => {
  303. const info = await Pty.create(c.req.valid("json"))
  304. return c.json(info)
  305. },
  306. )
  307. .get(
  308. "/pty/:ptyID",
  309. describeRoute({
  310. summary: "Get PTY session",
  311. description: "Retrieve detailed information about a specific pseudo-terminal (PTY) session.",
  312. operationId: "pty.get",
  313. responses: {
  314. 200: {
  315. description: "Session info",
  316. content: {
  317. "application/json": {
  318. schema: resolver(Pty.Info),
  319. },
  320. },
  321. },
  322. ...errors(404),
  323. },
  324. }),
  325. validator("param", z.object({ ptyID: z.string() })),
  326. async (c) => {
  327. const info = Pty.get(c.req.valid("param").ptyID)
  328. if (!info) {
  329. throw new Storage.NotFoundError({ message: "Session not found" })
  330. }
  331. return c.json(info)
  332. },
  333. )
  334. .put(
  335. "/pty/:ptyID",
  336. describeRoute({
  337. summary: "Update PTY session",
  338. description: "Update properties of an existing pseudo-terminal (PTY) session.",
  339. operationId: "pty.update",
  340. responses: {
  341. 200: {
  342. description: "Updated session",
  343. content: {
  344. "application/json": {
  345. schema: resolver(Pty.Info),
  346. },
  347. },
  348. },
  349. ...errors(400),
  350. },
  351. }),
  352. validator("param", z.object({ ptyID: z.string() })),
  353. validator("json", Pty.UpdateInput),
  354. async (c) => {
  355. const info = await Pty.update(c.req.valid("param").ptyID, c.req.valid("json"))
  356. return c.json(info)
  357. },
  358. )
  359. .delete(
  360. "/pty/:ptyID",
  361. describeRoute({
  362. summary: "Remove PTY session",
  363. description: "Remove and terminate a specific pseudo-terminal (PTY) session.",
  364. operationId: "pty.remove",
  365. responses: {
  366. 200: {
  367. description: "Session removed",
  368. content: {
  369. "application/json": {
  370. schema: resolver(z.boolean()),
  371. },
  372. },
  373. },
  374. ...errors(404),
  375. },
  376. }),
  377. validator("param", z.object({ ptyID: z.string() })),
  378. async (c) => {
  379. await Pty.remove(c.req.valid("param").ptyID)
  380. return c.json(true)
  381. },
  382. )
  383. .get(
  384. "/pty/:ptyID/connect",
  385. describeRoute({
  386. summary: "Connect to PTY session",
  387. description:
  388. "Establish a WebSocket connection to interact with a pseudo-terminal (PTY) session in real-time.",
  389. operationId: "pty.connect",
  390. responses: {
  391. 200: {
  392. description: "Connected session",
  393. content: {
  394. "application/json": {
  395. schema: resolver(z.boolean()),
  396. },
  397. },
  398. },
  399. ...errors(404),
  400. },
  401. }),
  402. validator("param", z.object({ ptyID: z.string() })),
  403. upgradeWebSocket((c) => {
  404. const id = c.req.param("ptyID")
  405. let handler: ReturnType<typeof Pty.connect>
  406. if (!Pty.get(id)) throw new Error("Session not found")
  407. return {
  408. onOpen(_event, ws) {
  409. handler = Pty.connect(id, ws)
  410. },
  411. onMessage(event) {
  412. handler?.onMessage(String(event.data))
  413. },
  414. onClose() {
  415. handler?.onClose()
  416. },
  417. }
  418. }),
  419. )
  420. .get(
  421. "/config",
  422. describeRoute({
  423. summary: "Get configuration",
  424. description: "Retrieve the current OpenCode configuration settings and preferences.",
  425. operationId: "config.get",
  426. responses: {
  427. 200: {
  428. description: "Get config info",
  429. content: {
  430. "application/json": {
  431. schema: resolver(Config.Info),
  432. },
  433. },
  434. },
  435. },
  436. }),
  437. async (c) => {
  438. return c.json(await Config.get())
  439. },
  440. )
  441. .patch(
  442. "/config",
  443. describeRoute({
  444. summary: "Update configuration",
  445. description: "Update OpenCode configuration settings and preferences.",
  446. operationId: "config.update",
  447. responses: {
  448. 200: {
  449. description: "Successfully updated config",
  450. content: {
  451. "application/json": {
  452. schema: resolver(Config.Info),
  453. },
  454. },
  455. },
  456. ...errors(400),
  457. },
  458. }),
  459. validator("json", Config.Info),
  460. async (c) => {
  461. const config = c.req.valid("json")
  462. await Config.update(config)
  463. return c.json(config)
  464. },
  465. )
  466. .get(
  467. "/experimental/tool/ids",
  468. describeRoute({
  469. summary: "List tool IDs",
  470. description:
  471. "Get a list of all available tool IDs, including both built-in tools and dynamically registered tools.",
  472. operationId: "tool.ids",
  473. responses: {
  474. 200: {
  475. description: "Tool IDs",
  476. content: {
  477. "application/json": {
  478. schema: resolver(z.array(z.string()).meta({ ref: "ToolIDs" })),
  479. },
  480. },
  481. },
  482. ...errors(400),
  483. },
  484. }),
  485. async (c) => {
  486. return c.json(await ToolRegistry.ids())
  487. },
  488. )
  489. .get(
  490. "/experimental/tool",
  491. describeRoute({
  492. summary: "List tools",
  493. description:
  494. "Get a list of available tools with their JSON schema parameters for a specific provider and model combination.",
  495. operationId: "tool.list",
  496. responses: {
  497. 200: {
  498. description: "Tools",
  499. content: {
  500. "application/json": {
  501. schema: resolver(
  502. z
  503. .array(
  504. z
  505. .object({
  506. id: z.string(),
  507. description: z.string(),
  508. parameters: z.any(),
  509. })
  510. .meta({ ref: "ToolListItem" }),
  511. )
  512. .meta({ ref: "ToolList" }),
  513. ),
  514. },
  515. },
  516. },
  517. ...errors(400),
  518. },
  519. }),
  520. validator(
  521. "query",
  522. z.object({
  523. provider: z.string(),
  524. model: z.string(),
  525. }),
  526. ),
  527. async (c) => {
  528. const { provider } = c.req.valid("query")
  529. const tools = await ToolRegistry.tools(provider)
  530. return c.json(
  531. tools.map((t) => ({
  532. id: t.id,
  533. description: t.description,
  534. // Handle both Zod schemas and plain JSON schemas
  535. parameters: (t.parameters as any)?._def ? zodToJsonSchema(t.parameters as any) : t.parameters,
  536. })),
  537. )
  538. },
  539. )
  540. .post(
  541. "/instance/dispose",
  542. describeRoute({
  543. summary: "Dispose instance",
  544. description: "Clean up and dispose the current OpenCode instance, releasing all resources.",
  545. operationId: "instance.dispose",
  546. responses: {
  547. 200: {
  548. description: "Instance disposed",
  549. content: {
  550. "application/json": {
  551. schema: resolver(z.boolean()),
  552. },
  553. },
  554. },
  555. },
  556. }),
  557. async (c) => {
  558. await Instance.dispose()
  559. return c.json(true)
  560. },
  561. )
  562. .get(
  563. "/path",
  564. describeRoute({
  565. summary: "Get paths",
  566. description: "Retrieve the current working directory and related path information for the OpenCode instance.",
  567. operationId: "path.get",
  568. responses: {
  569. 200: {
  570. description: "Path",
  571. content: {
  572. "application/json": {
  573. schema: resolver(
  574. z
  575. .object({
  576. home: z.string(),
  577. state: z.string(),
  578. config: z.string(),
  579. worktree: z.string(),
  580. directory: z.string(),
  581. })
  582. .meta({
  583. ref: "Path",
  584. }),
  585. ),
  586. },
  587. },
  588. },
  589. },
  590. }),
  591. async (c) => {
  592. return c.json({
  593. home: Global.Path.home,
  594. state: Global.Path.state,
  595. config: Global.Path.config,
  596. worktree: Instance.worktree,
  597. directory: Instance.directory,
  598. })
  599. },
  600. )
  601. .post(
  602. "/experimental/worktree",
  603. describeRoute({
  604. summary: "Create worktree",
  605. description: "Create a new git worktree for the current project.",
  606. operationId: "worktree.create",
  607. responses: {
  608. 200: {
  609. description: "Worktree created",
  610. content: {
  611. "application/json": {
  612. schema: resolver(Worktree.Info),
  613. },
  614. },
  615. },
  616. ...errors(400),
  617. },
  618. }),
  619. validator("json", Worktree.create.schema),
  620. async (c) => {
  621. const body = c.req.valid("json")
  622. const worktree = await Worktree.create(body)
  623. return c.json(worktree)
  624. },
  625. )
  626. .get(
  627. "/experimental/worktree",
  628. describeRoute({
  629. summary: "List worktrees",
  630. description: "List all sandbox worktrees for the current project.",
  631. operationId: "worktree.list",
  632. responses: {
  633. 200: {
  634. description: "List of worktree directories",
  635. content: {
  636. "application/json": {
  637. schema: resolver(z.array(z.string())),
  638. },
  639. },
  640. },
  641. },
  642. }),
  643. async (c) => {
  644. const sandboxes = await Project.sandboxes(Instance.project.id)
  645. return c.json(sandboxes)
  646. },
  647. )
  648. .get(
  649. "/vcs",
  650. describeRoute({
  651. summary: "Get VCS info",
  652. description: "Retrieve version control system (VCS) information for the current project, such as git branch.",
  653. operationId: "vcs.get",
  654. responses: {
  655. 200: {
  656. description: "VCS info",
  657. content: {
  658. "application/json": {
  659. schema: resolver(Vcs.Info),
  660. },
  661. },
  662. },
  663. },
  664. }),
  665. async (c) => {
  666. const branch = await Vcs.branch()
  667. return c.json({
  668. branch,
  669. })
  670. },
  671. )
  672. .get(
  673. "/session",
  674. describeRoute({
  675. summary: "List sessions",
  676. description: "Get a list of all OpenCode sessions, sorted by most recently updated.",
  677. operationId: "session.list",
  678. responses: {
  679. 200: {
  680. description: "List of sessions",
  681. content: {
  682. "application/json": {
  683. schema: resolver(Session.Info.array()),
  684. },
  685. },
  686. },
  687. },
  688. }),
  689. validator(
  690. "query",
  691. z.object({
  692. start: z.coerce
  693. .number()
  694. .optional()
  695. .meta({ description: "Filter sessions updated on or after this timestamp (milliseconds since epoch)" }),
  696. search: z.string().optional().meta({ description: "Filter sessions by title (case-insensitive)" }),
  697. limit: z.coerce.number().optional().meta({ description: "Maximum number of sessions to return" }),
  698. }),
  699. ),
  700. async (c) => {
  701. const query = c.req.valid("query")
  702. const term = query.search?.toLowerCase()
  703. const sessions: Session.Info[] = []
  704. for await (const session of Session.list()) {
  705. if (query.start !== undefined && session.time.updated < query.start) continue
  706. if (term !== undefined && !session.title.toLowerCase().includes(term)) continue
  707. sessions.push(session)
  708. if (query.limit !== undefined && sessions.length >= query.limit) break
  709. }
  710. return c.json(sessions)
  711. },
  712. )
  713. .get(
  714. "/session/status",
  715. describeRoute({
  716. summary: "Get session status",
  717. description: "Retrieve the current status of all sessions, including active, idle, and completed states.",
  718. operationId: "session.status",
  719. responses: {
  720. 200: {
  721. description: "Get session status",
  722. content: {
  723. "application/json": {
  724. schema: resolver(z.record(z.string(), SessionStatus.Info)),
  725. },
  726. },
  727. },
  728. ...errors(400),
  729. },
  730. }),
  731. async (c) => {
  732. const result = SessionStatus.list()
  733. return c.json(result)
  734. },
  735. )
  736. .get(
  737. "/session/:sessionID",
  738. describeRoute({
  739. summary: "Get session",
  740. description: "Retrieve detailed information about a specific OpenCode session.",
  741. tags: ["Session"],
  742. operationId: "session.get",
  743. responses: {
  744. 200: {
  745. description: "Get session",
  746. content: {
  747. "application/json": {
  748. schema: resolver(Session.Info),
  749. },
  750. },
  751. },
  752. ...errors(400, 404),
  753. },
  754. }),
  755. validator(
  756. "param",
  757. z.object({
  758. sessionID: Session.get.schema,
  759. }),
  760. ),
  761. async (c) => {
  762. const sessionID = c.req.valid("param").sessionID
  763. log.info("SEARCH", { url: c.req.url })
  764. const session = await Session.get(sessionID)
  765. return c.json(session)
  766. },
  767. )
  768. .get(
  769. "/session/:sessionID/children",
  770. describeRoute({
  771. summary: "Get session children",
  772. tags: ["Session"],
  773. description: "Retrieve all child sessions that were forked from the specified parent session.",
  774. operationId: "session.children",
  775. responses: {
  776. 200: {
  777. description: "List of children",
  778. content: {
  779. "application/json": {
  780. schema: resolver(Session.Info.array()),
  781. },
  782. },
  783. },
  784. ...errors(400, 404),
  785. },
  786. }),
  787. validator(
  788. "param",
  789. z.object({
  790. sessionID: Session.children.schema,
  791. }),
  792. ),
  793. async (c) => {
  794. const sessionID = c.req.valid("param").sessionID
  795. const session = await Session.children(sessionID)
  796. return c.json(session)
  797. },
  798. )
  799. .get(
  800. "/session/:sessionID/todo",
  801. describeRoute({
  802. summary: "Get session todos",
  803. description: "Retrieve the todo list associated with a specific session, showing tasks and action items.",
  804. operationId: "session.todo",
  805. responses: {
  806. 200: {
  807. description: "Todo list",
  808. content: {
  809. "application/json": {
  810. schema: resolver(Todo.Info.array()),
  811. },
  812. },
  813. },
  814. ...errors(400, 404),
  815. },
  816. }),
  817. validator(
  818. "param",
  819. z.object({
  820. sessionID: z.string().meta({ description: "Session ID" }),
  821. }),
  822. ),
  823. async (c) => {
  824. const sessionID = c.req.valid("param").sessionID
  825. const todos = await Todo.get(sessionID)
  826. return c.json(todos)
  827. },
  828. )
  829. .post(
  830. "/session",
  831. describeRoute({
  832. summary: "Create session",
  833. description: "Create a new OpenCode session for interacting with AI assistants and managing conversations.",
  834. operationId: "session.create",
  835. responses: {
  836. ...errors(400),
  837. 200: {
  838. description: "Successfully created session",
  839. content: {
  840. "application/json": {
  841. schema: resolver(Session.Info),
  842. },
  843. },
  844. },
  845. },
  846. }),
  847. validator("json", Session.create.schema.optional()),
  848. async (c) => {
  849. const body = c.req.valid("json") ?? {}
  850. const session = await Session.create(body)
  851. return c.json(session)
  852. },
  853. )
  854. .delete(
  855. "/session/:sessionID",
  856. describeRoute({
  857. summary: "Delete session",
  858. description: "Delete a session and permanently remove all associated data, including messages and history.",
  859. operationId: "session.delete",
  860. responses: {
  861. 200: {
  862. description: "Successfully deleted session",
  863. content: {
  864. "application/json": {
  865. schema: resolver(z.boolean()),
  866. },
  867. },
  868. },
  869. ...errors(400, 404),
  870. },
  871. }),
  872. validator(
  873. "param",
  874. z.object({
  875. sessionID: Session.remove.schema,
  876. }),
  877. ),
  878. async (c) => {
  879. const sessionID = c.req.valid("param").sessionID
  880. await Session.remove(sessionID)
  881. return c.json(true)
  882. },
  883. )
  884. .patch(
  885. "/session/:sessionID",
  886. describeRoute({
  887. summary: "Update session",
  888. description: "Update properties of an existing session, such as title or other metadata.",
  889. operationId: "session.update",
  890. responses: {
  891. 200: {
  892. description: "Successfully updated session",
  893. content: {
  894. "application/json": {
  895. schema: resolver(Session.Info),
  896. },
  897. },
  898. },
  899. ...errors(400, 404),
  900. },
  901. }),
  902. validator(
  903. "param",
  904. z.object({
  905. sessionID: z.string(),
  906. }),
  907. ),
  908. validator(
  909. "json",
  910. z.object({
  911. title: z.string().optional(),
  912. time: z
  913. .object({
  914. archived: z.number().optional(),
  915. })
  916. .optional(),
  917. }),
  918. ),
  919. async (c) => {
  920. const sessionID = c.req.valid("param").sessionID
  921. const updates = c.req.valid("json")
  922. const updatedSession = await Session.update(sessionID, (session) => {
  923. if (updates.title !== undefined) {
  924. session.title = updates.title
  925. }
  926. if (updates.time?.archived !== undefined) session.time.archived = updates.time.archived
  927. })
  928. return c.json(updatedSession)
  929. },
  930. )
  931. .post(
  932. "/session/:sessionID/init",
  933. describeRoute({
  934. summary: "Initialize session",
  935. description:
  936. "Analyze the current application and create an AGENTS.md file with project-specific agent configurations.",
  937. operationId: "session.init",
  938. responses: {
  939. 200: {
  940. description: "200",
  941. content: {
  942. "application/json": {
  943. schema: resolver(z.boolean()),
  944. },
  945. },
  946. },
  947. ...errors(400, 404),
  948. },
  949. }),
  950. validator(
  951. "param",
  952. z.object({
  953. sessionID: z.string().meta({ description: "Session ID" }),
  954. }),
  955. ),
  956. validator("json", Session.initialize.schema.omit({ sessionID: true })),
  957. async (c) => {
  958. const sessionID = c.req.valid("param").sessionID
  959. const body = c.req.valid("json")
  960. await Session.initialize({ ...body, sessionID })
  961. return c.json(true)
  962. },
  963. )
  964. .post(
  965. "/session/:sessionID/fork",
  966. describeRoute({
  967. summary: "Fork session",
  968. description: "Create a new session by forking an existing session at a specific message point.",
  969. operationId: "session.fork",
  970. responses: {
  971. 200: {
  972. description: "200",
  973. content: {
  974. "application/json": {
  975. schema: resolver(Session.Info),
  976. },
  977. },
  978. },
  979. },
  980. }),
  981. validator(
  982. "param",
  983. z.object({
  984. sessionID: Session.fork.schema.shape.sessionID,
  985. }),
  986. ),
  987. validator("json", Session.fork.schema.omit({ sessionID: true })),
  988. async (c) => {
  989. const sessionID = c.req.valid("param").sessionID
  990. const body = c.req.valid("json")
  991. const result = await Session.fork({ ...body, sessionID })
  992. return c.json(result)
  993. },
  994. )
  995. .post(
  996. "/session/:sessionID/abort",
  997. describeRoute({
  998. summary: "Abort session",
  999. description: "Abort an active session and stop any ongoing AI processing or command execution.",
  1000. operationId: "session.abort",
  1001. responses: {
  1002. 200: {
  1003. description: "Aborted session",
  1004. content: {
  1005. "application/json": {
  1006. schema: resolver(z.boolean()),
  1007. },
  1008. },
  1009. },
  1010. ...errors(400, 404),
  1011. },
  1012. }),
  1013. validator(
  1014. "param",
  1015. z.object({
  1016. sessionID: z.string(),
  1017. }),
  1018. ),
  1019. async (c) => {
  1020. SessionPrompt.cancel(c.req.valid("param").sessionID)
  1021. return c.json(true)
  1022. },
  1023. )
  1024. .post(
  1025. "/session/:sessionID/share",
  1026. describeRoute({
  1027. summary: "Share session",
  1028. description: "Create a shareable link for a session, allowing others to view the conversation.",
  1029. operationId: "session.share",
  1030. responses: {
  1031. 200: {
  1032. description: "Successfully shared session",
  1033. content: {
  1034. "application/json": {
  1035. schema: resolver(Session.Info),
  1036. },
  1037. },
  1038. },
  1039. ...errors(400, 404),
  1040. },
  1041. }),
  1042. validator(
  1043. "param",
  1044. z.object({
  1045. sessionID: z.string(),
  1046. }),
  1047. ),
  1048. async (c) => {
  1049. const sessionID = c.req.valid("param").sessionID
  1050. await Session.share(sessionID)
  1051. const session = await Session.get(sessionID)
  1052. return c.json(session)
  1053. },
  1054. )
  1055. .get(
  1056. "/session/:sessionID/diff",
  1057. describeRoute({
  1058. summary: "Get message diff",
  1059. description: "Get the file changes (diff) that resulted from a specific user message in the session.",
  1060. operationId: "session.diff",
  1061. responses: {
  1062. 200: {
  1063. description: "Successfully retrieved diff",
  1064. content: {
  1065. "application/json": {
  1066. schema: resolver(Snapshot.FileDiff.array()),
  1067. },
  1068. },
  1069. },
  1070. },
  1071. }),
  1072. validator(
  1073. "param",
  1074. z.object({
  1075. sessionID: SessionSummary.diff.schema.shape.sessionID,
  1076. }),
  1077. ),
  1078. validator(
  1079. "query",
  1080. z.object({
  1081. messageID: SessionSummary.diff.schema.shape.messageID,
  1082. }),
  1083. ),
  1084. async (c) => {
  1085. const query = c.req.valid("query")
  1086. const params = c.req.valid("param")
  1087. const result = await SessionSummary.diff({
  1088. sessionID: params.sessionID,
  1089. messageID: query.messageID,
  1090. })
  1091. return c.json(result)
  1092. },
  1093. )
  1094. .delete(
  1095. "/session/:sessionID/share",
  1096. describeRoute({
  1097. summary: "Unshare session",
  1098. description: "Remove the shareable link for a session, making it private again.",
  1099. operationId: "session.unshare",
  1100. responses: {
  1101. 200: {
  1102. description: "Successfully unshared session",
  1103. content: {
  1104. "application/json": {
  1105. schema: resolver(Session.Info),
  1106. },
  1107. },
  1108. },
  1109. ...errors(400, 404),
  1110. },
  1111. }),
  1112. validator(
  1113. "param",
  1114. z.object({
  1115. sessionID: Session.unshare.schema,
  1116. }),
  1117. ),
  1118. async (c) => {
  1119. const sessionID = c.req.valid("param").sessionID
  1120. await Session.unshare(sessionID)
  1121. const session = await Session.get(sessionID)
  1122. return c.json(session)
  1123. },
  1124. )
  1125. .post(
  1126. "/session/:sessionID/summarize",
  1127. describeRoute({
  1128. summary: "Summarize session",
  1129. description: "Generate a concise summary of the session using AI compaction to preserve key information.",
  1130. operationId: "session.summarize",
  1131. responses: {
  1132. 200: {
  1133. description: "Summarized session",
  1134. content: {
  1135. "application/json": {
  1136. schema: resolver(z.boolean()),
  1137. },
  1138. },
  1139. },
  1140. ...errors(400, 404),
  1141. },
  1142. }),
  1143. validator(
  1144. "param",
  1145. z.object({
  1146. sessionID: z.string().meta({ description: "Session ID" }),
  1147. }),
  1148. ),
  1149. validator(
  1150. "json",
  1151. z.object({
  1152. providerID: z.string(),
  1153. modelID: z.string(),
  1154. auto: z.boolean().optional().default(false),
  1155. }),
  1156. ),
  1157. async (c) => {
  1158. const sessionID = c.req.valid("param").sessionID
  1159. const body = c.req.valid("json")
  1160. const session = await Session.get(sessionID)
  1161. await SessionRevert.cleanup(session)
  1162. const msgs = await Session.messages({ sessionID })
  1163. let currentAgent = await Agent.defaultAgent()
  1164. for (let i = msgs.length - 1; i >= 0; i--) {
  1165. const info = msgs[i].info
  1166. if (info.role === "user") {
  1167. currentAgent = info.agent || (await Agent.defaultAgent())
  1168. break
  1169. }
  1170. }
  1171. await SessionCompaction.create({
  1172. sessionID,
  1173. agent: currentAgent,
  1174. model: {
  1175. providerID: body.providerID,
  1176. modelID: body.modelID,
  1177. },
  1178. auto: body.auto,
  1179. })
  1180. await SessionPrompt.loop(sessionID)
  1181. return c.json(true)
  1182. },
  1183. )
  1184. .get(
  1185. "/session/:sessionID/message",
  1186. describeRoute({
  1187. summary: "Get session messages",
  1188. description: "Retrieve all messages in a session, including user prompts and AI responses.",
  1189. operationId: "session.messages",
  1190. responses: {
  1191. 200: {
  1192. description: "List of messages",
  1193. content: {
  1194. "application/json": {
  1195. schema: resolver(MessageV2.WithParts.array()),
  1196. },
  1197. },
  1198. },
  1199. ...errors(400, 404),
  1200. },
  1201. }),
  1202. validator(
  1203. "param",
  1204. z.object({
  1205. sessionID: z.string().meta({ description: "Session ID" }),
  1206. }),
  1207. ),
  1208. validator(
  1209. "query",
  1210. z.object({
  1211. limit: z.coerce.number().optional(),
  1212. }),
  1213. ),
  1214. async (c) => {
  1215. const query = c.req.valid("query")
  1216. const messages = await Session.messages({
  1217. sessionID: c.req.valid("param").sessionID,
  1218. limit: query.limit,
  1219. })
  1220. return c.json(messages)
  1221. },
  1222. )
  1223. .get(
  1224. "/session/:sessionID/diff",
  1225. describeRoute({
  1226. summary: "Get session diff",
  1227. description: "Get all file changes (diffs) made during this session.",
  1228. operationId: "session.diff",
  1229. responses: {
  1230. 200: {
  1231. description: "List of diffs",
  1232. content: {
  1233. "application/json": {
  1234. schema: resolver(Snapshot.FileDiff.array()),
  1235. },
  1236. },
  1237. },
  1238. ...errors(400, 404),
  1239. },
  1240. }),
  1241. validator(
  1242. "param",
  1243. z.object({
  1244. sessionID: z.string().meta({ description: "Session ID" }),
  1245. }),
  1246. ),
  1247. async (c) => {
  1248. const diff = await Session.diff(c.req.valid("param").sessionID)
  1249. return c.json(diff)
  1250. },
  1251. )
  1252. .get(
  1253. "/session/:sessionID/message/:messageID",
  1254. describeRoute({
  1255. summary: "Get message",
  1256. description: "Retrieve a specific message from a session by its message ID.",
  1257. operationId: "session.message",
  1258. responses: {
  1259. 200: {
  1260. description: "Message",
  1261. content: {
  1262. "application/json": {
  1263. schema: resolver(
  1264. z.object({
  1265. info: MessageV2.Info,
  1266. parts: MessageV2.Part.array(),
  1267. }),
  1268. ),
  1269. },
  1270. },
  1271. },
  1272. ...errors(400, 404),
  1273. },
  1274. }),
  1275. validator(
  1276. "param",
  1277. z.object({
  1278. sessionID: z.string().meta({ description: "Session ID" }),
  1279. messageID: z.string().meta({ description: "Message ID" }),
  1280. }),
  1281. ),
  1282. async (c) => {
  1283. const params = c.req.valid("param")
  1284. const message = await MessageV2.get({
  1285. sessionID: params.sessionID,
  1286. messageID: params.messageID,
  1287. })
  1288. return c.json(message)
  1289. },
  1290. )
  1291. .delete(
  1292. "/session/:sessionID/message/:messageID/part/:partID",
  1293. describeRoute({
  1294. description: "Delete a part from a message",
  1295. operationId: "part.delete",
  1296. responses: {
  1297. 200: {
  1298. description: "Successfully deleted part",
  1299. content: {
  1300. "application/json": {
  1301. schema: resolver(z.boolean()),
  1302. },
  1303. },
  1304. },
  1305. ...errors(400, 404),
  1306. },
  1307. }),
  1308. validator(
  1309. "param",
  1310. z.object({
  1311. sessionID: z.string().meta({ description: "Session ID" }),
  1312. messageID: z.string().meta({ description: "Message ID" }),
  1313. partID: z.string().meta({ description: "Part ID" }),
  1314. }),
  1315. ),
  1316. async (c) => {
  1317. const params = c.req.valid("param")
  1318. await Session.removePart({
  1319. sessionID: params.sessionID,
  1320. messageID: params.messageID,
  1321. partID: params.partID,
  1322. })
  1323. return c.json(true)
  1324. },
  1325. )
  1326. .patch(
  1327. "/session/:sessionID/message/:messageID/part/:partID",
  1328. describeRoute({
  1329. description: "Update a part in a message",
  1330. operationId: "part.update",
  1331. responses: {
  1332. 200: {
  1333. description: "Successfully updated part",
  1334. content: {
  1335. "application/json": {
  1336. schema: resolver(MessageV2.Part),
  1337. },
  1338. },
  1339. },
  1340. ...errors(400, 404),
  1341. },
  1342. }),
  1343. validator(
  1344. "param",
  1345. z.object({
  1346. sessionID: z.string().meta({ description: "Session ID" }),
  1347. messageID: z.string().meta({ description: "Message ID" }),
  1348. partID: z.string().meta({ description: "Part ID" }),
  1349. }),
  1350. ),
  1351. validator("json", MessageV2.Part),
  1352. async (c) => {
  1353. const params = c.req.valid("param")
  1354. const body = c.req.valid("json")
  1355. if (body.id !== params.partID || body.messageID !== params.messageID || body.sessionID !== params.sessionID) {
  1356. throw new Error(
  1357. `Part mismatch: body.id='${body.id}' vs partID='${params.partID}', body.messageID='${body.messageID}' vs messageID='${params.messageID}', body.sessionID='${body.sessionID}' vs sessionID='${params.sessionID}'`,
  1358. )
  1359. }
  1360. const part = await Session.updatePart(body)
  1361. return c.json(part)
  1362. },
  1363. )
  1364. .post(
  1365. "/session/:sessionID/message",
  1366. describeRoute({
  1367. summary: "Send message",
  1368. description: "Create and send a new message to a session, streaming the AI response.",
  1369. operationId: "session.prompt",
  1370. responses: {
  1371. 200: {
  1372. description: "Created message",
  1373. content: {
  1374. "application/json": {
  1375. schema: resolver(
  1376. z.object({
  1377. info: MessageV2.Assistant,
  1378. parts: MessageV2.Part.array(),
  1379. }),
  1380. ),
  1381. },
  1382. },
  1383. },
  1384. ...errors(400, 404),
  1385. },
  1386. }),
  1387. validator(
  1388. "param",
  1389. z.object({
  1390. sessionID: z.string().meta({ description: "Session ID" }),
  1391. }),
  1392. ),
  1393. validator("json", SessionPrompt.PromptInput.omit({ sessionID: true })),
  1394. async (c) => {
  1395. c.status(200)
  1396. c.header("Content-Type", "application/json")
  1397. return stream(c, async (stream) => {
  1398. const sessionID = c.req.valid("param").sessionID
  1399. const body = c.req.valid("json")
  1400. const msg = await SessionPrompt.prompt({ ...body, sessionID })
  1401. stream.write(JSON.stringify(msg))
  1402. })
  1403. },
  1404. )
  1405. .post(
  1406. "/session/:sessionID/prompt_async",
  1407. describeRoute({
  1408. summary: "Send async message",
  1409. description:
  1410. "Create and send a new message to a session asynchronously, starting the session if needed and returning immediately.",
  1411. operationId: "session.prompt_async",
  1412. responses: {
  1413. 204: {
  1414. description: "Prompt accepted",
  1415. },
  1416. ...errors(400, 404),
  1417. },
  1418. }),
  1419. validator(
  1420. "param",
  1421. z.object({
  1422. sessionID: z.string().meta({ description: "Session ID" }),
  1423. }),
  1424. ),
  1425. validator("json", SessionPrompt.PromptInput.omit({ sessionID: true })),
  1426. async (c) => {
  1427. c.status(204)
  1428. c.header("Content-Type", "application/json")
  1429. return stream(c, async () => {
  1430. const sessionID = c.req.valid("param").sessionID
  1431. const body = c.req.valid("json")
  1432. SessionPrompt.prompt({ ...body, sessionID })
  1433. })
  1434. },
  1435. )
  1436. .post(
  1437. "/session/:sessionID/command",
  1438. describeRoute({
  1439. summary: "Send command",
  1440. description: "Send a new command to a session for execution by the AI assistant.",
  1441. operationId: "session.command",
  1442. responses: {
  1443. 200: {
  1444. description: "Created message",
  1445. content: {
  1446. "application/json": {
  1447. schema: resolver(
  1448. z.object({
  1449. info: MessageV2.Assistant,
  1450. parts: MessageV2.Part.array(),
  1451. }),
  1452. ),
  1453. },
  1454. },
  1455. },
  1456. ...errors(400, 404),
  1457. },
  1458. }),
  1459. validator(
  1460. "param",
  1461. z.object({
  1462. sessionID: z.string().meta({ description: "Session ID" }),
  1463. }),
  1464. ),
  1465. validator("json", SessionPrompt.CommandInput.omit({ sessionID: true })),
  1466. async (c) => {
  1467. const sessionID = c.req.valid("param").sessionID
  1468. const body = c.req.valid("json")
  1469. const msg = await SessionPrompt.command({ ...body, sessionID })
  1470. return c.json(msg)
  1471. },
  1472. )
  1473. .post(
  1474. "/session/:sessionID/shell",
  1475. describeRoute({
  1476. summary: "Run shell command",
  1477. description: "Execute a shell command within the session context and return the AI's response.",
  1478. operationId: "session.shell",
  1479. responses: {
  1480. 200: {
  1481. description: "Created message",
  1482. content: {
  1483. "application/json": {
  1484. schema: resolver(MessageV2.Assistant),
  1485. },
  1486. },
  1487. },
  1488. ...errors(400, 404),
  1489. },
  1490. }),
  1491. validator(
  1492. "param",
  1493. z.object({
  1494. sessionID: z.string().meta({ description: "Session ID" }),
  1495. }),
  1496. ),
  1497. validator("json", SessionPrompt.ShellInput.omit({ sessionID: true })),
  1498. async (c) => {
  1499. const sessionID = c.req.valid("param").sessionID
  1500. const body = c.req.valid("json")
  1501. const msg = await SessionPrompt.shell({ ...body, sessionID })
  1502. return c.json(msg)
  1503. },
  1504. )
  1505. .post(
  1506. "/session/:sessionID/revert",
  1507. describeRoute({
  1508. summary: "Revert message",
  1509. description: "Revert a specific message in a session, undoing its effects and restoring the previous state.",
  1510. operationId: "session.revert",
  1511. responses: {
  1512. 200: {
  1513. description: "Updated session",
  1514. content: {
  1515. "application/json": {
  1516. schema: resolver(Session.Info),
  1517. },
  1518. },
  1519. },
  1520. ...errors(400, 404),
  1521. },
  1522. }),
  1523. validator(
  1524. "param",
  1525. z.object({
  1526. sessionID: z.string(),
  1527. }),
  1528. ),
  1529. validator("json", SessionRevert.RevertInput.omit({ sessionID: true })),
  1530. async (c) => {
  1531. const sessionID = c.req.valid("param").sessionID
  1532. log.info("revert", c.req.valid("json"))
  1533. const session = await SessionRevert.revert({
  1534. sessionID,
  1535. ...c.req.valid("json"),
  1536. })
  1537. return c.json(session)
  1538. },
  1539. )
  1540. .post(
  1541. "/session/:sessionID/unrevert",
  1542. describeRoute({
  1543. summary: "Restore reverted messages",
  1544. description: "Restore all previously reverted messages in a session.",
  1545. operationId: "session.unrevert",
  1546. responses: {
  1547. 200: {
  1548. description: "Updated session",
  1549. content: {
  1550. "application/json": {
  1551. schema: resolver(Session.Info),
  1552. },
  1553. },
  1554. },
  1555. ...errors(400, 404),
  1556. },
  1557. }),
  1558. validator(
  1559. "param",
  1560. z.object({
  1561. sessionID: z.string(),
  1562. }),
  1563. ),
  1564. async (c) => {
  1565. const sessionID = c.req.valid("param").sessionID
  1566. const session = await SessionRevert.unrevert({ sessionID })
  1567. return c.json(session)
  1568. },
  1569. )
  1570. .post(
  1571. "/session/:sessionID/permissions/:permissionID",
  1572. describeRoute({
  1573. summary: "Respond to permission",
  1574. deprecated: true,
  1575. description: "Approve or deny a permission request from the AI assistant.",
  1576. operationId: "permission.respond",
  1577. responses: {
  1578. 200: {
  1579. description: "Permission processed successfully",
  1580. content: {
  1581. "application/json": {
  1582. schema: resolver(z.boolean()),
  1583. },
  1584. },
  1585. },
  1586. ...errors(400, 404),
  1587. },
  1588. }),
  1589. validator(
  1590. "param",
  1591. z.object({
  1592. sessionID: z.string(),
  1593. permissionID: z.string(),
  1594. }),
  1595. ),
  1596. validator("json", z.object({ response: PermissionNext.Reply })),
  1597. async (c) => {
  1598. const params = c.req.valid("param")
  1599. PermissionNext.reply({
  1600. requestID: params.permissionID,
  1601. reply: c.req.valid("json").response,
  1602. })
  1603. return c.json(true)
  1604. },
  1605. )
  1606. .post(
  1607. "/permission/:requestID/reply",
  1608. describeRoute({
  1609. summary: "Respond to permission request",
  1610. description: "Approve or deny a permission request from the AI assistant.",
  1611. operationId: "permission.reply",
  1612. responses: {
  1613. 200: {
  1614. description: "Permission processed successfully",
  1615. content: {
  1616. "application/json": {
  1617. schema: resolver(z.boolean()),
  1618. },
  1619. },
  1620. },
  1621. ...errors(400, 404),
  1622. },
  1623. }),
  1624. validator(
  1625. "param",
  1626. z.object({
  1627. requestID: z.string(),
  1628. }),
  1629. ),
  1630. validator("json", z.object({ reply: PermissionNext.Reply, message: z.string().optional() })),
  1631. async (c) => {
  1632. const params = c.req.valid("param")
  1633. const json = c.req.valid("json")
  1634. await PermissionNext.reply({
  1635. requestID: params.requestID,
  1636. reply: json.reply,
  1637. message: json.message,
  1638. })
  1639. return c.json(true)
  1640. },
  1641. )
  1642. .get(
  1643. "/permission",
  1644. describeRoute({
  1645. summary: "List pending permissions",
  1646. description: "Get all pending permission requests across all sessions.",
  1647. operationId: "permission.list",
  1648. responses: {
  1649. 200: {
  1650. description: "List of pending permissions",
  1651. content: {
  1652. "application/json": {
  1653. schema: resolver(PermissionNext.Request.array()),
  1654. },
  1655. },
  1656. },
  1657. },
  1658. }),
  1659. async (c) => {
  1660. const permissions = await PermissionNext.list()
  1661. return c.json(permissions)
  1662. },
  1663. )
  1664. .get(
  1665. "/command",
  1666. describeRoute({
  1667. summary: "List commands",
  1668. description: "Get a list of all available commands in the OpenCode system.",
  1669. operationId: "command.list",
  1670. responses: {
  1671. 200: {
  1672. description: "List of commands",
  1673. content: {
  1674. "application/json": {
  1675. schema: resolver(Command.Info.array()),
  1676. },
  1677. },
  1678. },
  1679. },
  1680. }),
  1681. async (c) => {
  1682. const commands = await Command.list()
  1683. return c.json(commands)
  1684. },
  1685. )
  1686. .get(
  1687. "/config/providers",
  1688. describeRoute({
  1689. summary: "List config providers",
  1690. description: "Get a list of all configured AI providers and their default models.",
  1691. operationId: "config.providers",
  1692. responses: {
  1693. 200: {
  1694. description: "List of providers",
  1695. content: {
  1696. "application/json": {
  1697. schema: resolver(
  1698. z.object({
  1699. providers: Provider.Info.array(),
  1700. default: z.record(z.string(), z.string()),
  1701. }),
  1702. ),
  1703. },
  1704. },
  1705. },
  1706. },
  1707. }),
  1708. async (c) => {
  1709. using _ = log.time("providers")
  1710. const providers = await Provider.list().then((x) => mapValues(x, (item) => item))
  1711. return c.json({
  1712. providers: Object.values(providers),
  1713. default: mapValues(providers, (item) => Provider.sort(Object.values(item.models))[0].id),
  1714. })
  1715. },
  1716. )
  1717. .get(
  1718. "/provider",
  1719. describeRoute({
  1720. summary: "List providers",
  1721. description: "Get a list of all available AI providers, including both available and connected ones.",
  1722. operationId: "provider.list",
  1723. responses: {
  1724. 200: {
  1725. description: "List of providers",
  1726. content: {
  1727. "application/json": {
  1728. schema: resolver(
  1729. z.object({
  1730. all: ModelsDev.Provider.array(),
  1731. default: z.record(z.string(), z.string()),
  1732. connected: z.array(z.string()),
  1733. }),
  1734. ),
  1735. },
  1736. },
  1737. },
  1738. },
  1739. }),
  1740. async (c) => {
  1741. const config = await Config.get()
  1742. const disabled = new Set(config.disabled_providers ?? [])
  1743. const enabled = config.enabled_providers ? new Set(config.enabled_providers) : undefined
  1744. const allProviders = await ModelsDev.get()
  1745. const filteredProviders: Record<string, (typeof allProviders)[string]> = {}
  1746. for (const [key, value] of Object.entries(allProviders)) {
  1747. if ((enabled ? enabled.has(key) : true) && !disabled.has(key)) {
  1748. filteredProviders[key] = value
  1749. }
  1750. }
  1751. const connected = await Provider.list()
  1752. const providers = Object.assign(
  1753. mapValues(filteredProviders, (x) => Provider.fromModelsDevProvider(x)),
  1754. connected,
  1755. )
  1756. return c.json({
  1757. all: Object.values(providers),
  1758. default: mapValues(providers, (item) => Provider.sort(Object.values(item.models))[0].id),
  1759. connected: Object.keys(connected),
  1760. })
  1761. },
  1762. )
  1763. .get(
  1764. "/provider/auth",
  1765. describeRoute({
  1766. summary: "Get provider auth methods",
  1767. description: "Retrieve available authentication methods for all AI providers.",
  1768. operationId: "provider.auth",
  1769. responses: {
  1770. 200: {
  1771. description: "Provider auth methods",
  1772. content: {
  1773. "application/json": {
  1774. schema: resolver(z.record(z.string(), z.array(ProviderAuth.Method))),
  1775. },
  1776. },
  1777. },
  1778. },
  1779. }),
  1780. async (c) => {
  1781. return c.json(await ProviderAuth.methods())
  1782. },
  1783. )
  1784. .post(
  1785. "/provider/:providerID/oauth/authorize",
  1786. describeRoute({
  1787. summary: "OAuth authorize",
  1788. description: "Initiate OAuth authorization for a specific AI provider to get an authorization URL.",
  1789. operationId: "provider.oauth.authorize",
  1790. responses: {
  1791. 200: {
  1792. description: "Authorization URL and method",
  1793. content: {
  1794. "application/json": {
  1795. schema: resolver(ProviderAuth.Authorization.optional()),
  1796. },
  1797. },
  1798. },
  1799. ...errors(400),
  1800. },
  1801. }),
  1802. validator(
  1803. "param",
  1804. z.object({
  1805. providerID: z.string().meta({ description: "Provider ID" }),
  1806. }),
  1807. ),
  1808. validator(
  1809. "json",
  1810. z.object({
  1811. method: z.number().meta({ description: "Auth method index" }),
  1812. }),
  1813. ),
  1814. async (c) => {
  1815. const providerID = c.req.valid("param").providerID
  1816. const { method } = c.req.valid("json")
  1817. const result = await ProviderAuth.authorize({
  1818. providerID,
  1819. method,
  1820. })
  1821. return c.json(result)
  1822. },
  1823. )
  1824. .post(
  1825. "/provider/:providerID/oauth/callback",
  1826. describeRoute({
  1827. summary: "OAuth callback",
  1828. description: "Handle the OAuth callback from a provider after user authorization.",
  1829. operationId: "provider.oauth.callback",
  1830. responses: {
  1831. 200: {
  1832. description: "OAuth callback processed successfully",
  1833. content: {
  1834. "application/json": {
  1835. schema: resolver(z.boolean()),
  1836. },
  1837. },
  1838. },
  1839. ...errors(400),
  1840. },
  1841. }),
  1842. validator(
  1843. "param",
  1844. z.object({
  1845. providerID: z.string().meta({ description: "Provider ID" }),
  1846. }),
  1847. ),
  1848. validator(
  1849. "json",
  1850. z.object({
  1851. method: z.number().meta({ description: "Auth method index" }),
  1852. code: z.string().optional().meta({ description: "OAuth authorization code" }),
  1853. }),
  1854. ),
  1855. async (c) => {
  1856. const providerID = c.req.valid("param").providerID
  1857. const { method, code } = c.req.valid("json")
  1858. await ProviderAuth.callback({
  1859. providerID,
  1860. method,
  1861. code,
  1862. })
  1863. return c.json(true)
  1864. },
  1865. )
  1866. .get(
  1867. "/find",
  1868. describeRoute({
  1869. summary: "Find text",
  1870. description: "Search for text patterns across files in the project using ripgrep.",
  1871. operationId: "find.text",
  1872. responses: {
  1873. 200: {
  1874. description: "Matches",
  1875. content: {
  1876. "application/json": {
  1877. schema: resolver(Ripgrep.Match.shape.data.array()),
  1878. },
  1879. },
  1880. },
  1881. },
  1882. }),
  1883. validator(
  1884. "query",
  1885. z.object({
  1886. pattern: z.string(),
  1887. }),
  1888. ),
  1889. async (c) => {
  1890. const pattern = c.req.valid("query").pattern
  1891. const result = await Ripgrep.search({
  1892. cwd: Instance.directory,
  1893. pattern,
  1894. limit: 10,
  1895. })
  1896. return c.json(result)
  1897. },
  1898. )
  1899. .get(
  1900. "/find/file",
  1901. describeRoute({
  1902. summary: "Find files",
  1903. description: "Search for files or directories by name or pattern in the project directory.",
  1904. operationId: "find.files",
  1905. responses: {
  1906. 200: {
  1907. description: "File paths",
  1908. content: {
  1909. "application/json": {
  1910. schema: resolver(z.string().array()),
  1911. },
  1912. },
  1913. },
  1914. },
  1915. }),
  1916. validator(
  1917. "query",
  1918. z.object({
  1919. query: z.string(),
  1920. dirs: z.enum(["true", "false"]).optional(),
  1921. type: z.enum(["file", "directory"]).optional(),
  1922. limit: z.coerce.number().int().min(1).max(200).optional(),
  1923. }),
  1924. ),
  1925. async (c) => {
  1926. const query = c.req.valid("query").query
  1927. const dirs = c.req.valid("query").dirs
  1928. const type = c.req.valid("query").type
  1929. const limit = c.req.valid("query").limit
  1930. const results = await File.search({
  1931. query,
  1932. limit: limit ?? 10,
  1933. dirs: dirs !== "false",
  1934. type,
  1935. })
  1936. return c.json(results)
  1937. },
  1938. )
  1939. .get(
  1940. "/find/symbol",
  1941. describeRoute({
  1942. summary: "Find symbols",
  1943. description: "Search for workspace symbols like functions, classes, and variables using LSP.",
  1944. operationId: "find.symbols",
  1945. responses: {
  1946. 200: {
  1947. description: "Symbols",
  1948. content: {
  1949. "application/json": {
  1950. schema: resolver(LSP.Symbol.array()),
  1951. },
  1952. },
  1953. },
  1954. },
  1955. }),
  1956. validator(
  1957. "query",
  1958. z.object({
  1959. query: z.string(),
  1960. }),
  1961. ),
  1962. async (c) => {
  1963. /*
  1964. const query = c.req.valid("query").query
  1965. const result = await LSP.workspaceSymbol(query)
  1966. return c.json(result)
  1967. */
  1968. return c.json([])
  1969. },
  1970. )
  1971. .get(
  1972. "/file",
  1973. describeRoute({
  1974. summary: "List files",
  1975. description: "List files and directories in a specified path.",
  1976. operationId: "file.list",
  1977. responses: {
  1978. 200: {
  1979. description: "Files and directories",
  1980. content: {
  1981. "application/json": {
  1982. schema: resolver(File.Node.array()),
  1983. },
  1984. },
  1985. },
  1986. },
  1987. }),
  1988. validator(
  1989. "query",
  1990. z.object({
  1991. path: z.string(),
  1992. }),
  1993. ),
  1994. async (c) => {
  1995. const path = c.req.valid("query").path
  1996. const content = await File.list(path)
  1997. return c.json(content)
  1998. },
  1999. )
  2000. .get(
  2001. "/file/content",
  2002. describeRoute({
  2003. summary: "Read file",
  2004. description: "Read the content of a specified file.",
  2005. operationId: "file.read",
  2006. responses: {
  2007. 200: {
  2008. description: "File content",
  2009. content: {
  2010. "application/json": {
  2011. schema: resolver(File.Content),
  2012. },
  2013. },
  2014. },
  2015. },
  2016. }),
  2017. validator(
  2018. "query",
  2019. z.object({
  2020. path: z.string(),
  2021. }),
  2022. ),
  2023. async (c) => {
  2024. const path = c.req.valid("query").path
  2025. const content = await File.read(path)
  2026. return c.json(content)
  2027. },
  2028. )
  2029. .get(
  2030. "/file/status",
  2031. describeRoute({
  2032. summary: "Get file status",
  2033. description: "Get the git status of all files in the project.",
  2034. operationId: "file.status",
  2035. responses: {
  2036. 200: {
  2037. description: "File status",
  2038. content: {
  2039. "application/json": {
  2040. schema: resolver(File.Info.array()),
  2041. },
  2042. },
  2043. },
  2044. },
  2045. }),
  2046. async (c) => {
  2047. const content = await File.status()
  2048. return c.json(content)
  2049. },
  2050. )
  2051. .post(
  2052. "/log",
  2053. describeRoute({
  2054. summary: "Write log",
  2055. description: "Write a log entry to the server logs with specified level and metadata.",
  2056. operationId: "app.log",
  2057. responses: {
  2058. 200: {
  2059. description: "Log entry written successfully",
  2060. content: {
  2061. "application/json": {
  2062. schema: resolver(z.boolean()),
  2063. },
  2064. },
  2065. },
  2066. ...errors(400),
  2067. },
  2068. }),
  2069. validator(
  2070. "json",
  2071. z.object({
  2072. service: z.string().meta({ description: "Service name for the log entry" }),
  2073. level: z.enum(["debug", "info", "error", "warn"]).meta({ description: "Log level" }),
  2074. message: z.string().meta({ description: "Log message" }),
  2075. extra: z
  2076. .record(z.string(), z.any())
  2077. .optional()
  2078. .meta({ description: "Additional metadata for the log entry" }),
  2079. }),
  2080. ),
  2081. async (c) => {
  2082. const { service, level, message, extra } = c.req.valid("json")
  2083. const logger = Log.create({ service })
  2084. switch (level) {
  2085. case "debug":
  2086. logger.debug(message, extra)
  2087. break
  2088. case "info":
  2089. logger.info(message, extra)
  2090. break
  2091. case "error":
  2092. logger.error(message, extra)
  2093. break
  2094. case "warn":
  2095. logger.warn(message, extra)
  2096. break
  2097. }
  2098. return c.json(true)
  2099. },
  2100. )
  2101. .get(
  2102. "/agent",
  2103. describeRoute({
  2104. summary: "List agents",
  2105. description: "Get a list of all available AI agents in the OpenCode system.",
  2106. operationId: "app.agents",
  2107. responses: {
  2108. 200: {
  2109. description: "List of agents",
  2110. content: {
  2111. "application/json": {
  2112. schema: resolver(Agent.Info.array()),
  2113. },
  2114. },
  2115. },
  2116. },
  2117. }),
  2118. async (c) => {
  2119. const modes = await Agent.list()
  2120. return c.json(modes)
  2121. },
  2122. )
  2123. .get(
  2124. "/mcp",
  2125. describeRoute({
  2126. summary: "Get MCP status",
  2127. description: "Get the status of all Model Context Protocol (MCP) servers.",
  2128. operationId: "mcp.status",
  2129. responses: {
  2130. 200: {
  2131. description: "MCP server status",
  2132. content: {
  2133. "application/json": {
  2134. schema: resolver(z.record(z.string(), MCP.Status)),
  2135. },
  2136. },
  2137. },
  2138. },
  2139. }),
  2140. async (c) => {
  2141. return c.json(await MCP.status())
  2142. },
  2143. )
  2144. .post(
  2145. "/mcp",
  2146. describeRoute({
  2147. summary: "Add MCP server",
  2148. description: "Dynamically add a new Model Context Protocol (MCP) server to the system.",
  2149. operationId: "mcp.add",
  2150. responses: {
  2151. 200: {
  2152. description: "MCP server added successfully",
  2153. content: {
  2154. "application/json": {
  2155. schema: resolver(z.record(z.string(), MCP.Status)),
  2156. },
  2157. },
  2158. },
  2159. ...errors(400),
  2160. },
  2161. }),
  2162. validator(
  2163. "json",
  2164. z.object({
  2165. name: z.string(),
  2166. config: Config.Mcp,
  2167. }),
  2168. ),
  2169. async (c) => {
  2170. const { name, config } = c.req.valid("json")
  2171. const result = await MCP.add(name, config)
  2172. return c.json(result.status)
  2173. },
  2174. )
  2175. .post(
  2176. "/mcp/:name/auth",
  2177. describeRoute({
  2178. summary: "Start MCP OAuth",
  2179. description: "Start OAuth authentication flow for a Model Context Protocol (MCP) server.",
  2180. operationId: "mcp.auth.start",
  2181. responses: {
  2182. 200: {
  2183. description: "OAuth flow started",
  2184. content: {
  2185. "application/json": {
  2186. schema: resolver(
  2187. z.object({
  2188. authorizationUrl: z.string().describe("URL to open in browser for authorization"),
  2189. }),
  2190. ),
  2191. },
  2192. },
  2193. },
  2194. ...errors(400, 404),
  2195. },
  2196. }),
  2197. async (c) => {
  2198. const name = c.req.param("name")
  2199. const supportsOAuth = await MCP.supportsOAuth(name)
  2200. if (!supportsOAuth) {
  2201. return c.json({ error: `MCP server ${name} does not support OAuth` }, 400)
  2202. }
  2203. const result = await MCP.startAuth(name)
  2204. return c.json(result)
  2205. },
  2206. )
  2207. .post(
  2208. "/mcp/:name/auth/callback",
  2209. describeRoute({
  2210. summary: "Complete MCP OAuth",
  2211. description:
  2212. "Complete OAuth authentication for a Model Context Protocol (MCP) server using the authorization code.",
  2213. operationId: "mcp.auth.callback",
  2214. responses: {
  2215. 200: {
  2216. description: "OAuth authentication completed",
  2217. content: {
  2218. "application/json": {
  2219. schema: resolver(MCP.Status),
  2220. },
  2221. },
  2222. },
  2223. ...errors(400, 404),
  2224. },
  2225. }),
  2226. validator(
  2227. "json",
  2228. z.object({
  2229. code: z.string().describe("Authorization code from OAuth callback"),
  2230. }),
  2231. ),
  2232. async (c) => {
  2233. const name = c.req.param("name")
  2234. const { code } = c.req.valid("json")
  2235. const status = await MCP.finishAuth(name, code)
  2236. return c.json(status)
  2237. },
  2238. )
  2239. .post(
  2240. "/mcp/:name/auth/authenticate",
  2241. describeRoute({
  2242. summary: "Authenticate MCP OAuth",
  2243. description: "Start OAuth flow and wait for callback (opens browser)",
  2244. operationId: "mcp.auth.authenticate",
  2245. responses: {
  2246. 200: {
  2247. description: "OAuth authentication completed",
  2248. content: {
  2249. "application/json": {
  2250. schema: resolver(MCP.Status),
  2251. },
  2252. },
  2253. },
  2254. ...errors(400, 404),
  2255. },
  2256. }),
  2257. async (c) => {
  2258. const name = c.req.param("name")
  2259. const supportsOAuth = await MCP.supportsOAuth(name)
  2260. if (!supportsOAuth) {
  2261. return c.json({ error: `MCP server ${name} does not support OAuth` }, 400)
  2262. }
  2263. const status = await MCP.authenticate(name)
  2264. return c.json(status)
  2265. },
  2266. )
  2267. .delete(
  2268. "/mcp/:name/auth",
  2269. describeRoute({
  2270. summary: "Remove MCP OAuth",
  2271. description: "Remove OAuth credentials for an MCP server",
  2272. operationId: "mcp.auth.remove",
  2273. responses: {
  2274. 200: {
  2275. description: "OAuth credentials removed",
  2276. content: {
  2277. "application/json": {
  2278. schema: resolver(z.object({ success: z.literal(true) })),
  2279. },
  2280. },
  2281. },
  2282. ...errors(404),
  2283. },
  2284. }),
  2285. async (c) => {
  2286. const name = c.req.param("name")
  2287. await MCP.removeAuth(name)
  2288. return c.json({ success: true as const })
  2289. },
  2290. )
  2291. .post(
  2292. "/mcp/:name/connect",
  2293. describeRoute({
  2294. description: "Connect an MCP server",
  2295. operationId: "mcp.connect",
  2296. responses: {
  2297. 200: {
  2298. description: "MCP server connected successfully",
  2299. content: {
  2300. "application/json": {
  2301. schema: resolver(z.boolean()),
  2302. },
  2303. },
  2304. },
  2305. },
  2306. }),
  2307. validator("param", z.object({ name: z.string() })),
  2308. async (c) => {
  2309. const { name } = c.req.valid("param")
  2310. await MCP.connect(name)
  2311. return c.json(true)
  2312. },
  2313. )
  2314. .post(
  2315. "/mcp/:name/disconnect",
  2316. describeRoute({
  2317. description: "Disconnect an MCP server",
  2318. operationId: "mcp.disconnect",
  2319. responses: {
  2320. 200: {
  2321. description: "MCP server disconnected successfully",
  2322. content: {
  2323. "application/json": {
  2324. schema: resolver(z.boolean()),
  2325. },
  2326. },
  2327. },
  2328. },
  2329. }),
  2330. validator("param", z.object({ name: z.string() })),
  2331. async (c) => {
  2332. const { name } = c.req.valid("param")
  2333. await MCP.disconnect(name)
  2334. return c.json(true)
  2335. },
  2336. )
  2337. .get(
  2338. "/experimental/resource",
  2339. describeRoute({
  2340. summary: "Get MCP resources",
  2341. description: "Get all available MCP resources from connected servers. Optionally filter by name.",
  2342. operationId: "experimental.resource.list",
  2343. responses: {
  2344. 200: {
  2345. description: "MCP resources",
  2346. content: {
  2347. "application/json": {
  2348. schema: resolver(z.record(z.string(), MCP.Resource)),
  2349. },
  2350. },
  2351. },
  2352. },
  2353. }),
  2354. async (c) => {
  2355. return c.json(await MCP.resources())
  2356. },
  2357. )
  2358. .get(
  2359. "/lsp",
  2360. describeRoute({
  2361. summary: "Get LSP status",
  2362. description: "Get LSP server status",
  2363. operationId: "lsp.status",
  2364. responses: {
  2365. 200: {
  2366. description: "LSP server status",
  2367. content: {
  2368. "application/json": {
  2369. schema: resolver(LSP.Status.array()),
  2370. },
  2371. },
  2372. },
  2373. },
  2374. }),
  2375. async (c) => {
  2376. return c.json(await LSP.status())
  2377. },
  2378. )
  2379. .get(
  2380. "/formatter",
  2381. describeRoute({
  2382. summary: "Get formatter status",
  2383. description: "Get formatter status",
  2384. operationId: "formatter.status",
  2385. responses: {
  2386. 200: {
  2387. description: "Formatter status",
  2388. content: {
  2389. "application/json": {
  2390. schema: resolver(Format.Status.array()),
  2391. },
  2392. },
  2393. },
  2394. },
  2395. }),
  2396. async (c) => {
  2397. return c.json(await Format.status())
  2398. },
  2399. )
  2400. .post(
  2401. "/tui/append-prompt",
  2402. describeRoute({
  2403. summary: "Append TUI prompt",
  2404. description: "Append prompt to the TUI",
  2405. operationId: "tui.appendPrompt",
  2406. responses: {
  2407. 200: {
  2408. description: "Prompt processed successfully",
  2409. content: {
  2410. "application/json": {
  2411. schema: resolver(z.boolean()),
  2412. },
  2413. },
  2414. },
  2415. ...errors(400),
  2416. },
  2417. }),
  2418. validator("json", TuiEvent.PromptAppend.properties),
  2419. async (c) => {
  2420. await Bus.publish(TuiEvent.PromptAppend, c.req.valid("json"))
  2421. return c.json(true)
  2422. },
  2423. )
  2424. .post(
  2425. "/tui/open-help",
  2426. describeRoute({
  2427. summary: "Open help dialog",
  2428. description: "Open the help dialog in the TUI to display user assistance information.",
  2429. operationId: "tui.openHelp",
  2430. responses: {
  2431. 200: {
  2432. description: "Help dialog opened successfully",
  2433. content: {
  2434. "application/json": {
  2435. schema: resolver(z.boolean()),
  2436. },
  2437. },
  2438. },
  2439. },
  2440. }),
  2441. async (c) => {
  2442. // TODO: open dialog
  2443. return c.json(true)
  2444. },
  2445. )
  2446. .post(
  2447. "/tui/open-sessions",
  2448. describeRoute({
  2449. summary: "Open sessions dialog",
  2450. description: "Open the session dialog",
  2451. operationId: "tui.openSessions",
  2452. responses: {
  2453. 200: {
  2454. description: "Session dialog opened successfully",
  2455. content: {
  2456. "application/json": {
  2457. schema: resolver(z.boolean()),
  2458. },
  2459. },
  2460. },
  2461. },
  2462. }),
  2463. async (c) => {
  2464. await Bus.publish(TuiEvent.CommandExecute, {
  2465. command: "session.list",
  2466. })
  2467. return c.json(true)
  2468. },
  2469. )
  2470. .post(
  2471. "/tui/open-themes",
  2472. describeRoute({
  2473. summary: "Open themes dialog",
  2474. description: "Open the theme dialog",
  2475. operationId: "tui.openThemes",
  2476. responses: {
  2477. 200: {
  2478. description: "Theme dialog opened successfully",
  2479. content: {
  2480. "application/json": {
  2481. schema: resolver(z.boolean()),
  2482. },
  2483. },
  2484. },
  2485. },
  2486. }),
  2487. async (c) => {
  2488. await Bus.publish(TuiEvent.CommandExecute, {
  2489. command: "session.list",
  2490. })
  2491. return c.json(true)
  2492. },
  2493. )
  2494. .post(
  2495. "/tui/open-models",
  2496. describeRoute({
  2497. summary: "Open models dialog",
  2498. description: "Open the model dialog",
  2499. operationId: "tui.openModels",
  2500. responses: {
  2501. 200: {
  2502. description: "Model dialog opened successfully",
  2503. content: {
  2504. "application/json": {
  2505. schema: resolver(z.boolean()),
  2506. },
  2507. },
  2508. },
  2509. },
  2510. }),
  2511. async (c) => {
  2512. await Bus.publish(TuiEvent.CommandExecute, {
  2513. command: "model.list",
  2514. })
  2515. return c.json(true)
  2516. },
  2517. )
  2518. .post(
  2519. "/tui/submit-prompt",
  2520. describeRoute({
  2521. summary: "Submit TUI prompt",
  2522. description: "Submit the prompt",
  2523. operationId: "tui.submitPrompt",
  2524. responses: {
  2525. 200: {
  2526. description: "Prompt submitted successfully",
  2527. content: {
  2528. "application/json": {
  2529. schema: resolver(z.boolean()),
  2530. },
  2531. },
  2532. },
  2533. },
  2534. }),
  2535. async (c) => {
  2536. await Bus.publish(TuiEvent.CommandExecute, {
  2537. command: "prompt.submit",
  2538. })
  2539. return c.json(true)
  2540. },
  2541. )
  2542. .post(
  2543. "/tui/clear-prompt",
  2544. describeRoute({
  2545. summary: "Clear TUI prompt",
  2546. description: "Clear the prompt",
  2547. operationId: "tui.clearPrompt",
  2548. responses: {
  2549. 200: {
  2550. description: "Prompt cleared successfully",
  2551. content: {
  2552. "application/json": {
  2553. schema: resolver(z.boolean()),
  2554. },
  2555. },
  2556. },
  2557. },
  2558. }),
  2559. async (c) => {
  2560. await Bus.publish(TuiEvent.CommandExecute, {
  2561. command: "prompt.clear",
  2562. })
  2563. return c.json(true)
  2564. },
  2565. )
  2566. .post(
  2567. "/tui/execute-command",
  2568. describeRoute({
  2569. summary: "Execute TUI command",
  2570. description: "Execute a TUI command (e.g. agent_cycle)",
  2571. operationId: "tui.executeCommand",
  2572. responses: {
  2573. 200: {
  2574. description: "Command executed successfully",
  2575. content: {
  2576. "application/json": {
  2577. schema: resolver(z.boolean()),
  2578. },
  2579. },
  2580. },
  2581. ...errors(400),
  2582. },
  2583. }),
  2584. validator("json", z.object({ command: z.string() })),
  2585. async (c) => {
  2586. const command = c.req.valid("json").command
  2587. await Bus.publish(TuiEvent.CommandExecute, {
  2588. // @ts-expect-error
  2589. command: {
  2590. session_new: "session.new",
  2591. session_share: "session.share",
  2592. session_interrupt: "session.interrupt",
  2593. session_compact: "session.compact",
  2594. messages_page_up: "session.page.up",
  2595. messages_page_down: "session.page.down",
  2596. messages_half_page_up: "session.half.page.up",
  2597. messages_half_page_down: "session.half.page.down",
  2598. messages_first: "session.first",
  2599. messages_last: "session.last",
  2600. agent_cycle: "agent.cycle",
  2601. }[command],
  2602. })
  2603. return c.json(true)
  2604. },
  2605. )
  2606. .post(
  2607. "/tui/show-toast",
  2608. describeRoute({
  2609. summary: "Show TUI toast",
  2610. description: "Show a toast notification in the TUI",
  2611. operationId: "tui.showToast",
  2612. responses: {
  2613. 200: {
  2614. description: "Toast notification shown successfully",
  2615. content: {
  2616. "application/json": {
  2617. schema: resolver(z.boolean()),
  2618. },
  2619. },
  2620. },
  2621. },
  2622. }),
  2623. validator("json", TuiEvent.ToastShow.properties),
  2624. async (c) => {
  2625. await Bus.publish(TuiEvent.ToastShow, c.req.valid("json"))
  2626. return c.json(true)
  2627. },
  2628. )
  2629. .post(
  2630. "/tui/publish",
  2631. describeRoute({
  2632. summary: "Publish TUI event",
  2633. description: "Publish a TUI event",
  2634. operationId: "tui.publish",
  2635. responses: {
  2636. 200: {
  2637. description: "Event published successfully",
  2638. content: {
  2639. "application/json": {
  2640. schema: resolver(z.boolean()),
  2641. },
  2642. },
  2643. },
  2644. ...errors(400),
  2645. },
  2646. }),
  2647. validator(
  2648. "json",
  2649. z.union(
  2650. Object.values(TuiEvent).map((def) => {
  2651. return z
  2652. .object({
  2653. type: z.literal(def.type),
  2654. properties: def.properties,
  2655. })
  2656. .meta({
  2657. ref: "Event" + "." + def.type,
  2658. })
  2659. }),
  2660. ),
  2661. ),
  2662. async (c) => {
  2663. const evt = c.req.valid("json")
  2664. await Bus.publish(Object.values(TuiEvent).find((def) => def.type === evt.type)!, evt.properties)
  2665. return c.json(true)
  2666. },
  2667. )
  2668. .post(
  2669. "/tui/select-session",
  2670. describeRoute({
  2671. summary: "Select session",
  2672. description: "Navigate the TUI to display the specified session.",
  2673. operationId: "tui.selectSession",
  2674. responses: {
  2675. 200: {
  2676. description: "Session selected successfully",
  2677. content: {
  2678. "application/json": {
  2679. schema: resolver(z.boolean()),
  2680. },
  2681. },
  2682. },
  2683. ...errors(400, 404),
  2684. },
  2685. }),
  2686. validator("json", TuiEvent.SessionSelect.properties),
  2687. async (c) => {
  2688. const { sessionID } = c.req.valid("json")
  2689. await Session.get(sessionID)
  2690. await Bus.publish(TuiEvent.SessionSelect, { sessionID })
  2691. return c.json(true)
  2692. },
  2693. )
  2694. .route("/tui/control", TuiRoute)
  2695. .put(
  2696. "/auth/:providerID",
  2697. describeRoute({
  2698. summary: "Set auth credentials",
  2699. description: "Set authentication credentials",
  2700. operationId: "auth.set",
  2701. responses: {
  2702. 200: {
  2703. description: "Successfully set authentication credentials",
  2704. content: {
  2705. "application/json": {
  2706. schema: resolver(z.boolean()),
  2707. },
  2708. },
  2709. },
  2710. ...errors(400),
  2711. },
  2712. }),
  2713. validator(
  2714. "param",
  2715. z.object({
  2716. providerID: z.string(),
  2717. }),
  2718. ),
  2719. validator("json", Auth.Info),
  2720. async (c) => {
  2721. const providerID = c.req.valid("param").providerID
  2722. const info = c.req.valid("json")
  2723. await Auth.set(providerID, info)
  2724. return c.json(true)
  2725. },
  2726. )
  2727. .get(
  2728. "/event",
  2729. describeRoute({
  2730. summary: "Subscribe to events",
  2731. description: "Get events",
  2732. operationId: "event.subscribe",
  2733. responses: {
  2734. 200: {
  2735. description: "Event stream",
  2736. content: {
  2737. "text/event-stream": {
  2738. schema: resolver(BusEvent.payloads()),
  2739. },
  2740. },
  2741. },
  2742. },
  2743. }),
  2744. async (c) => {
  2745. log.info("event connected")
  2746. return streamSSE(c, async (stream) => {
  2747. stream.writeSSE({
  2748. data: JSON.stringify({
  2749. type: "server.connected",
  2750. properties: {},
  2751. }),
  2752. })
  2753. const unsub = Bus.subscribeAll(async (event) => {
  2754. await stream.writeSSE({
  2755. data: JSON.stringify(event),
  2756. })
  2757. if (event.type === Bus.InstanceDisposed.type) {
  2758. stream.close()
  2759. }
  2760. })
  2761. // Send heartbeat every 30s to prevent WKWebView timeout (60s default)
  2762. const heartbeat = setInterval(() => {
  2763. stream.writeSSE({
  2764. data: JSON.stringify({
  2765. type: "server.heartbeat",
  2766. properties: {},
  2767. }),
  2768. })
  2769. }, 30000)
  2770. await new Promise<void>((resolve) => {
  2771. stream.onAbort(() => {
  2772. clearInterval(heartbeat)
  2773. unsub()
  2774. resolve()
  2775. log.info("event disconnected")
  2776. })
  2777. })
  2778. })
  2779. },
  2780. )
  2781. .all("/*", async (c) => {
  2782. const path = c.req.path
  2783. const response = await proxy(`https://app.opencode.ai${path}`, {
  2784. ...c.req,
  2785. headers: {
  2786. ...c.req.raw.headers,
  2787. host: "app.opencode.ai",
  2788. },
  2789. })
  2790. return response
  2791. }),
  2792. )
  2793. export async function openapi() {
  2794. const result = await generateSpecs(App(), {
  2795. documentation: {
  2796. info: {
  2797. title: "opencode",
  2798. version: "1.0.0",
  2799. description: "opencode api",
  2800. },
  2801. openapi: "3.1.1",
  2802. },
  2803. })
  2804. return result
  2805. }
  2806. export function listen(opts: { port: number; hostname: string; mdns?: boolean; cors?: string[] }) {
  2807. _corsWhitelist = opts.cors ?? []
  2808. const args = {
  2809. hostname: opts.hostname,
  2810. idleTimeout: 0,
  2811. fetch: App().fetch,
  2812. websocket: websocket,
  2813. } as const
  2814. const tryServe = (port: number) => {
  2815. try {
  2816. return Bun.serve({ ...args, port })
  2817. } catch {
  2818. return undefined
  2819. }
  2820. }
  2821. const server = opts.port === 0 ? (tryServe(4096) ?? tryServe(0)) : tryServe(opts.port)
  2822. if (!server) throw new Error(`Failed to start server on port ${opts.port}`)
  2823. _url = server.url
  2824. const shouldPublishMDNS =
  2825. opts.mdns &&
  2826. server.port &&
  2827. opts.hostname !== "127.0.0.1" &&
  2828. opts.hostname !== "localhost" &&
  2829. opts.hostname !== "::1"
  2830. if (shouldPublishMDNS) {
  2831. MDNS.publish(server.port!, `opencode-${server.port!}`)
  2832. } else if (opts.mdns) {
  2833. log.warn("mDNS enabled but hostname is loopback; skipping mDNS publish")
  2834. }
  2835. const originalStop = server.stop.bind(server)
  2836. server.stop = async (closeActiveConnections?: boolean) => {
  2837. if (shouldPublishMDNS) MDNS.unpublish()
  2838. return originalStop(closeActiveConnections)
  2839. }
  2840. return server
  2841. }
  2842. }