server.ts 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
  1. import { Log } from "../util/log"
  2. import { Bus } from "../bus"
  3. import { describeRoute, generateSpecs, validator, resolver, openAPIRouteHandler } from "hono-openapi"
  4. import { Hono } from "hono"
  5. import { cors } from "hono/cors"
  6. import { stream, streamSSE } from "hono/streaming"
  7. import { proxy } from "hono/proxy"
  8. import { Session } from "../session"
  9. import z from "zod"
  10. import { Provider } from "../provider/provider"
  11. import { mapValues } from "remeda"
  12. import { NamedError } from "@opencode-ai/util/error"
  13. import { ModelsDev } from "../provider/models"
  14. import { Ripgrep } from "../file/ripgrep"
  15. import { Config } from "../config/config"
  16. import { File } from "../file"
  17. import { LSP } from "../lsp"
  18. import { Format } from "../format"
  19. import { MessageV2 } from "../session/message-v2"
  20. import { TuiRoute } from "./tui"
  21. import { Permission } from "../permission"
  22. import { Instance } from "../project/instance"
  23. import { Agent } from "../agent/agent"
  24. import { Auth } from "../auth"
  25. import { Command } from "../command"
  26. import { ProviderAuth } from "../provider/auth"
  27. import { Global } from "../global"
  28. import { ProjectRoute } from "./project"
  29. import { ToolRegistry } from "../tool/registry"
  30. import { zodToJsonSchema } from "zod-to-json-schema"
  31. import { SessionPrompt } from "../session/prompt"
  32. import { SessionCompaction } from "../session/compaction"
  33. import { SessionRevert } from "../session/revert"
  34. import { lazy } from "../util/lazy"
  35. import { Todo } from "../session/todo"
  36. import { InstanceBootstrap } from "../project/bootstrap"
  37. import { MCP } from "../mcp"
  38. import { Storage } from "../storage/storage"
  39. import type { ContentfulStatusCode } from "hono/utils/http-status"
  40. import { TuiEvent } from "@/cli/cmd/tui/event"
  41. import { Snapshot } from "@/snapshot"
  42. import { SessionSummary } from "@/session/summary"
  43. import { GlobalBus } from "@/bus/global"
  44. import { SessionStatus } from "@/session/status"
  45. import { ShareNext } from "@/share/share-next"
  46. // @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
  47. globalThis.AI_SDK_LOG_WARNINGS = false
  48. const ERRORS = {
  49. 400: {
  50. description: "Bad request",
  51. content: {
  52. "application/json": {
  53. schema: resolver(
  54. z
  55. .object({
  56. data: z.any(),
  57. errors: z.array(z.record(z.string(), z.any())),
  58. success: z.literal(false),
  59. })
  60. .meta({
  61. ref: "BadRequestError",
  62. }),
  63. ),
  64. },
  65. },
  66. },
  67. 404: {
  68. description: "Not found",
  69. content: {
  70. "application/json": {
  71. schema: resolver(Storage.NotFoundError.Schema),
  72. },
  73. },
  74. },
  75. } as const
  76. function errors(...codes: number[]) {
  77. return Object.fromEntries(codes.map((code) => [code, ERRORS[code as keyof typeof ERRORS]]))
  78. }
  79. export namespace Server {
  80. const log = Log.create({ service: "server" })
  81. export const Event = {
  82. Connected: Bus.event("server.connected", z.object({})),
  83. }
  84. const app = new Hono()
  85. export const App = lazy(() =>
  86. app
  87. .onError((err, c) => {
  88. log.error("failed", {
  89. error: err,
  90. })
  91. if (err instanceof NamedError) {
  92. let status: ContentfulStatusCode
  93. if (err instanceof Storage.NotFoundError) status = 404
  94. else if (err instanceof Provider.ModelNotFoundError) status = 400
  95. else status = 500
  96. return c.json(err.toObject(), { status })
  97. }
  98. const message = err instanceof Error && err.stack ? err.stack : err.toString()
  99. return c.json(new NamedError.Unknown({ message }).toObject(), {
  100. status: 500,
  101. })
  102. })
  103. .use(async (c, next) => {
  104. const skipLogging = c.req.path === "/log"
  105. if (!skipLogging) {
  106. log.info("request", {
  107. method: c.req.method,
  108. path: c.req.path,
  109. })
  110. }
  111. const timer = log.time("request", {
  112. method: c.req.method,
  113. path: c.req.path,
  114. })
  115. await next()
  116. if (!skipLogging) {
  117. timer.stop()
  118. }
  119. })
  120. .use(cors())
  121. .get(
  122. "/global/event",
  123. describeRoute({
  124. description: "Get events",
  125. operationId: "global.event",
  126. responses: {
  127. 200: {
  128. description: "Event stream",
  129. content: {
  130. "text/event-stream": {
  131. schema: resolver(
  132. z
  133. .object({
  134. directory: z.string(),
  135. payload: Bus.payloads(),
  136. })
  137. .meta({
  138. ref: "GlobalEvent",
  139. }),
  140. ),
  141. },
  142. },
  143. },
  144. },
  145. }),
  146. async (c) => {
  147. log.info("global event connected")
  148. return streamSSE(c, async (stream) => {
  149. async function handler(event: any) {
  150. await stream.writeSSE({
  151. data: JSON.stringify(event),
  152. })
  153. }
  154. GlobalBus.on("event", handler)
  155. await new Promise<void>((resolve) => {
  156. stream.onAbort(() => {
  157. GlobalBus.off("event", handler)
  158. resolve()
  159. log.info("global event disconnected")
  160. })
  161. })
  162. })
  163. },
  164. )
  165. .use(async (c, next) => {
  166. const directory = c.req.query("directory") ?? c.req.header("x-opencode-directory") ?? process.cwd()
  167. return Instance.provide({
  168. directory,
  169. init: InstanceBootstrap,
  170. async fn() {
  171. return next()
  172. },
  173. })
  174. })
  175. .get(
  176. "/doc",
  177. openAPIRouteHandler(app, {
  178. documentation: {
  179. info: {
  180. title: "opencode",
  181. version: "0.0.3",
  182. description: "opencode api",
  183. },
  184. openapi: "3.1.1",
  185. },
  186. }),
  187. )
  188. .use(validator("query", z.object({ directory: z.string().optional() })))
  189. .route("/project", ProjectRoute)
  190. .get(
  191. "/config",
  192. describeRoute({
  193. description: "Get config info",
  194. operationId: "config.get",
  195. responses: {
  196. 200: {
  197. description: "Get config info",
  198. content: {
  199. "application/json": {
  200. schema: resolver(Config.Info),
  201. },
  202. },
  203. },
  204. },
  205. }),
  206. async (c) => {
  207. return c.json(await Config.get())
  208. },
  209. )
  210. .patch(
  211. "/config",
  212. describeRoute({
  213. description: "Update config",
  214. operationId: "config.update",
  215. responses: {
  216. 200: {
  217. description: "Successfully updated config",
  218. content: {
  219. "application/json": {
  220. schema: resolver(Config.Info),
  221. },
  222. },
  223. },
  224. ...errors(400),
  225. },
  226. }),
  227. validator("json", Config.Info),
  228. async (c) => {
  229. const config = c.req.valid("json")
  230. await Config.update(config)
  231. return c.json(config)
  232. },
  233. )
  234. .get(
  235. "/experimental/tool/ids",
  236. describeRoute({
  237. description: "List all tool IDs (including built-in and dynamically registered)",
  238. operationId: "tool.ids",
  239. responses: {
  240. 200: {
  241. description: "Tool IDs",
  242. content: {
  243. "application/json": {
  244. schema: resolver(z.array(z.string()).meta({ ref: "ToolIDs" })),
  245. },
  246. },
  247. },
  248. ...errors(400),
  249. },
  250. }),
  251. async (c) => {
  252. return c.json(await ToolRegistry.ids())
  253. },
  254. )
  255. .get(
  256. "/experimental/tool",
  257. describeRoute({
  258. description: "List tools with JSON schema parameters for a provider/model",
  259. operationId: "tool.list",
  260. responses: {
  261. 200: {
  262. description: "Tools",
  263. content: {
  264. "application/json": {
  265. schema: resolver(
  266. z
  267. .array(
  268. z
  269. .object({
  270. id: z.string(),
  271. description: z.string(),
  272. parameters: z.any(),
  273. })
  274. .meta({ ref: "ToolListItem" }),
  275. )
  276. .meta({ ref: "ToolList" }),
  277. ),
  278. },
  279. },
  280. },
  281. ...errors(400),
  282. },
  283. }),
  284. validator(
  285. "query",
  286. z.object({
  287. provider: z.string(),
  288. model: z.string(),
  289. }),
  290. ),
  291. async (c) => {
  292. const { provider, model } = c.req.valid("query")
  293. const tools = await ToolRegistry.tools(provider, model)
  294. return c.json(
  295. tools.map((t) => ({
  296. id: t.id,
  297. description: t.description,
  298. // Handle both Zod schemas and plain JSON schemas
  299. parameters: (t.parameters as any)?._def ? zodToJsonSchema(t.parameters as any) : t.parameters,
  300. })),
  301. )
  302. },
  303. )
  304. .post(
  305. "/instance/dispose",
  306. describeRoute({
  307. description: "Dispose the current instance",
  308. operationId: "instance.dispose",
  309. responses: {
  310. 200: {
  311. description: "Instance disposed",
  312. content: {
  313. "application/json": {
  314. schema: resolver(z.boolean()),
  315. },
  316. },
  317. },
  318. },
  319. }),
  320. async (c) => {
  321. await Instance.dispose()
  322. return c.json(true)
  323. },
  324. )
  325. .get(
  326. "/path",
  327. describeRoute({
  328. description: "Get the current path",
  329. operationId: "path.get",
  330. responses: {
  331. 200: {
  332. description: "Path",
  333. content: {
  334. "application/json": {
  335. schema: resolver(
  336. z
  337. .object({
  338. state: z.string(),
  339. config: z.string(),
  340. worktree: z.string(),
  341. directory: z.string(),
  342. })
  343. .meta({
  344. ref: "Path",
  345. }),
  346. ),
  347. },
  348. },
  349. },
  350. },
  351. }),
  352. async (c) => {
  353. return c.json({
  354. state: Global.Path.state,
  355. config: Global.Path.config,
  356. worktree: Instance.worktree,
  357. directory: Instance.directory,
  358. })
  359. },
  360. )
  361. .get(
  362. "/session",
  363. describeRoute({
  364. description: "List all sessions",
  365. operationId: "session.list",
  366. responses: {
  367. 200: {
  368. description: "List of sessions",
  369. content: {
  370. "application/json": {
  371. schema: resolver(Session.Info.array()),
  372. },
  373. },
  374. },
  375. },
  376. }),
  377. async (c) => {
  378. const sessions = await Array.fromAsync(Session.list())
  379. sessions.sort((a, b) => b.time.updated - a.time.updated)
  380. return c.json(sessions)
  381. },
  382. )
  383. .get(
  384. "/session/status",
  385. describeRoute({
  386. description: "Get session status",
  387. operationId: "session.status",
  388. responses: {
  389. 200: {
  390. description: "Get session status",
  391. content: {
  392. "application/json": {
  393. schema: resolver(z.record(z.string(), SessionStatus.Info)),
  394. },
  395. },
  396. },
  397. ...errors(400),
  398. },
  399. }),
  400. async (c) => {
  401. const result = SessionStatus.list()
  402. return c.json(result)
  403. },
  404. )
  405. .get(
  406. "/session/:id",
  407. describeRoute({
  408. description: "Get session",
  409. operationId: "session.get",
  410. responses: {
  411. 200: {
  412. description: "Get session",
  413. content: {
  414. "application/json": {
  415. schema: resolver(Session.Info),
  416. },
  417. },
  418. },
  419. ...errors(400, 404),
  420. },
  421. }),
  422. validator(
  423. "param",
  424. z.object({
  425. id: Session.get.schema,
  426. }),
  427. ),
  428. async (c) => {
  429. const sessionID = c.req.valid("param").id
  430. const session = await Session.get(sessionID)
  431. return c.json(session)
  432. },
  433. )
  434. .get(
  435. "/session/:id/children",
  436. describeRoute({
  437. description: "Get a session's children",
  438. operationId: "session.children",
  439. responses: {
  440. 200: {
  441. description: "List of children",
  442. content: {
  443. "application/json": {
  444. schema: resolver(Session.Info.array()),
  445. },
  446. },
  447. },
  448. ...errors(400, 404),
  449. },
  450. }),
  451. validator(
  452. "param",
  453. z.object({
  454. id: Session.children.schema,
  455. }),
  456. ),
  457. async (c) => {
  458. const sessionID = c.req.valid("param").id
  459. const session = await Session.children(sessionID)
  460. return c.json(session)
  461. },
  462. )
  463. .get(
  464. "/session/:id/todo",
  465. describeRoute({
  466. description: "Get the todo list for a session",
  467. operationId: "session.todo",
  468. responses: {
  469. 200: {
  470. description: "Todo list",
  471. content: {
  472. "application/json": {
  473. schema: resolver(Todo.Info.array()),
  474. },
  475. },
  476. },
  477. ...errors(400, 404),
  478. },
  479. }),
  480. validator(
  481. "param",
  482. z.object({
  483. id: z.string().meta({ description: "Session ID" }),
  484. }),
  485. ),
  486. async (c) => {
  487. const sessionID = c.req.valid("param").id
  488. const todos = await Todo.get(sessionID)
  489. return c.json(todos)
  490. },
  491. )
  492. .post(
  493. "/session",
  494. describeRoute({
  495. description: "Create a new session",
  496. operationId: "session.create",
  497. responses: {
  498. ...errors(400),
  499. 200: {
  500. description: "Successfully created session",
  501. content: {
  502. "application/json": {
  503. schema: resolver(Session.Info),
  504. },
  505. },
  506. },
  507. },
  508. }),
  509. validator("json", Session.create.schema.optional()),
  510. async (c) => {
  511. const body = c.req.valid("json") ?? {}
  512. const session = await Session.create(body)
  513. return c.json(session)
  514. },
  515. )
  516. .delete(
  517. "/session/:id",
  518. describeRoute({
  519. description: "Delete a session and all its data",
  520. operationId: "session.delete",
  521. responses: {
  522. 200: {
  523. description: "Successfully deleted session",
  524. content: {
  525. "application/json": {
  526. schema: resolver(z.boolean()),
  527. },
  528. },
  529. },
  530. ...errors(400, 404),
  531. },
  532. }),
  533. validator(
  534. "param",
  535. z.object({
  536. id: Session.remove.schema,
  537. }),
  538. ),
  539. async (c) => {
  540. const sessionID = c.req.valid("param").id
  541. await Session.remove(sessionID)
  542. await Bus.publish(TuiEvent.CommandExecute, {
  543. command: "session.list",
  544. })
  545. return c.json(true)
  546. },
  547. )
  548. .patch(
  549. "/session/:id",
  550. describeRoute({
  551. description: "Update session properties",
  552. operationId: "session.update",
  553. responses: {
  554. 200: {
  555. description: "Successfully updated session",
  556. content: {
  557. "application/json": {
  558. schema: resolver(Session.Info),
  559. },
  560. },
  561. },
  562. ...errors(400, 404),
  563. },
  564. }),
  565. validator(
  566. "param",
  567. z.object({
  568. id: z.string(),
  569. }),
  570. ),
  571. validator(
  572. "json",
  573. z.object({
  574. title: z.string().optional(),
  575. }),
  576. ),
  577. async (c) => {
  578. const sessionID = c.req.valid("param").id
  579. const updates = c.req.valid("json")
  580. const updatedSession = await Session.update(sessionID, (session) => {
  581. if (updates.title !== undefined) {
  582. session.title = updates.title
  583. }
  584. })
  585. return c.json(updatedSession)
  586. },
  587. )
  588. .post(
  589. "/session/:id/init",
  590. describeRoute({
  591. description: "Analyze the app and create an AGENTS.md file",
  592. operationId: "session.init",
  593. responses: {
  594. 200: {
  595. description: "200",
  596. content: {
  597. "application/json": {
  598. schema: resolver(z.boolean()),
  599. },
  600. },
  601. },
  602. ...errors(400, 404),
  603. },
  604. }),
  605. validator(
  606. "param",
  607. z.object({
  608. id: z.string().meta({ description: "Session ID" }),
  609. }),
  610. ),
  611. validator("json", Session.initialize.schema.omit({ sessionID: true })),
  612. async (c) => {
  613. const sessionID = c.req.valid("param").id
  614. const body = c.req.valid("json")
  615. await Session.initialize({ ...body, sessionID })
  616. return c.json(true)
  617. },
  618. )
  619. .post(
  620. "/session/:id/fork",
  621. describeRoute({
  622. description: "Fork an existing session at a specific message",
  623. operationId: "session.fork",
  624. responses: {
  625. 200: {
  626. description: "200",
  627. content: {
  628. "application/json": {
  629. schema: resolver(Session.Info),
  630. },
  631. },
  632. },
  633. },
  634. }),
  635. validator(
  636. "param",
  637. z.object({
  638. id: Session.fork.schema.shape.sessionID,
  639. }),
  640. ),
  641. validator("json", Session.fork.schema.omit({ sessionID: true })),
  642. async (c) => {
  643. const sessionID = c.req.valid("param").id
  644. const body = c.req.valid("json")
  645. const result = await Session.fork({ ...body, sessionID })
  646. return c.json(result)
  647. },
  648. )
  649. .post(
  650. "/session/:id/abort",
  651. describeRoute({
  652. description: "Abort a session",
  653. operationId: "session.abort",
  654. responses: {
  655. 200: {
  656. description: "Aborted session",
  657. content: {
  658. "application/json": {
  659. schema: resolver(z.boolean()),
  660. },
  661. },
  662. },
  663. ...errors(400, 404),
  664. },
  665. }),
  666. validator(
  667. "param",
  668. z.object({
  669. id: z.string(),
  670. }),
  671. ),
  672. async (c) => {
  673. SessionPrompt.cancel(c.req.valid("param").id)
  674. return c.json(true)
  675. },
  676. )
  677. .post(
  678. "/session/:id/share",
  679. describeRoute({
  680. description: "Share a session",
  681. operationId: "session.share",
  682. responses: {
  683. 200: {
  684. description: "Successfully shared session",
  685. content: {
  686. "application/json": {
  687. schema: resolver(Session.Info),
  688. },
  689. },
  690. },
  691. ...errors(400, 404),
  692. },
  693. }),
  694. validator(
  695. "param",
  696. z.object({
  697. id: z.string(),
  698. }),
  699. ),
  700. async (c) => {
  701. const id = c.req.valid("param").id
  702. await Session.share(id)
  703. const session = await Session.get(id)
  704. return c.json(session)
  705. },
  706. )
  707. .get(
  708. "/session/:id/diff",
  709. describeRoute({
  710. description: "Get the diff that resulted from this user message",
  711. operationId: "session.diff",
  712. responses: {
  713. 200: {
  714. description: "Successfully retrieved diff",
  715. content: {
  716. "application/json": {
  717. schema: resolver(Snapshot.FileDiff.array()),
  718. },
  719. },
  720. },
  721. },
  722. }),
  723. validator(
  724. "param",
  725. z.object({
  726. id: SessionSummary.diff.schema.shape.sessionID,
  727. }),
  728. ),
  729. validator(
  730. "query",
  731. z.object({
  732. messageID: SessionSummary.diff.schema.shape.messageID,
  733. }),
  734. ),
  735. async (c) => {
  736. const query = c.req.valid("query")
  737. const params = c.req.valid("param")
  738. const result = await SessionSummary.diff({
  739. sessionID: params.id,
  740. messageID: query.messageID,
  741. })
  742. return c.json(result)
  743. },
  744. )
  745. .delete(
  746. "/session/:id/share",
  747. describeRoute({
  748. description: "Unshare the session",
  749. operationId: "session.unshare",
  750. responses: {
  751. 200: {
  752. description: "Successfully unshared session",
  753. content: {
  754. "application/json": {
  755. schema: resolver(Session.Info),
  756. },
  757. },
  758. },
  759. ...errors(400, 404),
  760. },
  761. }),
  762. validator(
  763. "param",
  764. z.object({
  765. id: Session.unshare.schema,
  766. }),
  767. ),
  768. async (c) => {
  769. const id = c.req.valid("param").id
  770. await Session.unshare(id)
  771. const session = await Session.get(id)
  772. return c.json(session)
  773. },
  774. )
  775. .post(
  776. "/session/:id/summarize",
  777. describeRoute({
  778. description: "Summarize the session",
  779. operationId: "session.summarize",
  780. responses: {
  781. 200: {
  782. description: "Summarized session",
  783. content: {
  784. "application/json": {
  785. schema: resolver(z.boolean()),
  786. },
  787. },
  788. },
  789. ...errors(400, 404),
  790. },
  791. }),
  792. validator(
  793. "param",
  794. z.object({
  795. id: z.string().meta({ description: "Session ID" }),
  796. }),
  797. ),
  798. validator(
  799. "json",
  800. z.object({
  801. providerID: z.string(),
  802. modelID: z.string(),
  803. }),
  804. ),
  805. async (c) => {
  806. const id = c.req.valid("param").id
  807. const body = c.req.valid("json")
  808. const msgs = await Session.messages({ sessionID: id })
  809. let currentAgent = "build"
  810. for (let i = msgs.length - 1; i >= 0; i--) {
  811. const info = msgs[i].info
  812. if (info.role === "user") {
  813. currentAgent = info.agent || "build"
  814. break
  815. }
  816. }
  817. await SessionCompaction.create({
  818. sessionID: id,
  819. agent: currentAgent,
  820. model: {
  821. providerID: body.providerID,
  822. modelID: body.modelID,
  823. },
  824. })
  825. await SessionPrompt.loop(id)
  826. return c.json(true)
  827. },
  828. )
  829. .get(
  830. "/session/:id/message",
  831. describeRoute({
  832. description: "List messages for a session",
  833. operationId: "session.messages",
  834. responses: {
  835. 200: {
  836. description: "List of messages",
  837. content: {
  838. "application/json": {
  839. schema: resolver(MessageV2.WithParts.array()),
  840. },
  841. },
  842. },
  843. ...errors(400, 404),
  844. },
  845. }),
  846. validator(
  847. "param",
  848. z.object({
  849. id: z.string().meta({ description: "Session ID" }),
  850. }),
  851. ),
  852. validator(
  853. "query",
  854. z.object({
  855. limit: z.coerce.number().optional(),
  856. }),
  857. ),
  858. async (c) => {
  859. const query = c.req.valid("query")
  860. const messages = await Session.messages({
  861. sessionID: c.req.valid("param").id,
  862. limit: query.limit,
  863. })
  864. return c.json(messages)
  865. },
  866. )
  867. .get(
  868. "/session/:id/diff",
  869. describeRoute({
  870. description: "Get the diff for this session",
  871. operationId: "session.diff",
  872. responses: {
  873. 200: {
  874. description: "List of diffs",
  875. content: {
  876. "application/json": {
  877. schema: resolver(Snapshot.FileDiff.array()),
  878. },
  879. },
  880. },
  881. ...errors(400, 404),
  882. },
  883. }),
  884. validator(
  885. "param",
  886. z.object({
  887. id: z.string().meta({ description: "Session ID" }),
  888. }),
  889. ),
  890. async (c) => {
  891. const diff = await Session.diff(c.req.valid("param").id)
  892. return c.json(diff)
  893. },
  894. )
  895. .get(
  896. "/session/:id/message/:messageID",
  897. describeRoute({
  898. description: "Get a message from a session",
  899. operationId: "session.message",
  900. responses: {
  901. 200: {
  902. description: "Message",
  903. content: {
  904. "application/json": {
  905. schema: resolver(
  906. z.object({
  907. info: MessageV2.Info,
  908. parts: MessageV2.Part.array(),
  909. }),
  910. ),
  911. },
  912. },
  913. },
  914. ...errors(400, 404),
  915. },
  916. }),
  917. validator(
  918. "param",
  919. z.object({
  920. id: z.string().meta({ description: "Session ID" }),
  921. messageID: z.string().meta({ description: "Message ID" }),
  922. }),
  923. ),
  924. async (c) => {
  925. const params = c.req.valid("param")
  926. const message = await MessageV2.get({
  927. sessionID: params.id,
  928. messageID: params.messageID,
  929. })
  930. return c.json(message)
  931. },
  932. )
  933. .post(
  934. "/session/:id/message",
  935. describeRoute({
  936. description: "Create and send a new message to a session",
  937. operationId: "session.prompt",
  938. responses: {
  939. 200: {
  940. description: "Created message",
  941. content: {
  942. "application/json": {
  943. schema: resolver(
  944. z.object({
  945. info: MessageV2.Assistant,
  946. parts: MessageV2.Part.array(),
  947. }),
  948. ),
  949. },
  950. },
  951. },
  952. ...errors(400, 404),
  953. },
  954. }),
  955. validator(
  956. "param",
  957. z.object({
  958. id: z.string().meta({ description: "Session ID" }),
  959. }),
  960. ),
  961. validator("json", SessionPrompt.PromptInput.omit({ sessionID: true })),
  962. async (c) => {
  963. c.status(200)
  964. c.header("Content-Type", "application/json")
  965. return stream(c, async (stream) => {
  966. const sessionID = c.req.valid("param").id
  967. const body = c.req.valid("json")
  968. const msg = await SessionPrompt.prompt({ ...body, sessionID })
  969. stream.write(JSON.stringify(msg))
  970. })
  971. },
  972. )
  973. .post(
  974. "/session/:id/command",
  975. describeRoute({
  976. description: "Send a new command to a session",
  977. operationId: "session.command",
  978. responses: {
  979. 200: {
  980. description: "Created message",
  981. content: {
  982. "application/json": {
  983. schema: resolver(
  984. z.object({
  985. info: MessageV2.Assistant,
  986. parts: MessageV2.Part.array(),
  987. }),
  988. ),
  989. },
  990. },
  991. },
  992. ...errors(400, 404),
  993. },
  994. }),
  995. validator(
  996. "param",
  997. z.object({
  998. id: z.string().meta({ description: "Session ID" }),
  999. }),
  1000. ),
  1001. validator("json", SessionPrompt.CommandInput.omit({ sessionID: true })),
  1002. async (c) => {
  1003. const sessionID = c.req.valid("param").id
  1004. const body = c.req.valid("json")
  1005. const msg = await SessionPrompt.command({ ...body, sessionID })
  1006. return c.json(msg)
  1007. },
  1008. )
  1009. .post(
  1010. "/session/:id/shell",
  1011. describeRoute({
  1012. description: "Run a shell command",
  1013. operationId: "session.shell",
  1014. responses: {
  1015. 200: {
  1016. description: "Created message",
  1017. content: {
  1018. "application/json": {
  1019. schema: resolver(MessageV2.Assistant),
  1020. },
  1021. },
  1022. },
  1023. ...errors(400, 404),
  1024. },
  1025. }),
  1026. validator(
  1027. "param",
  1028. z.object({
  1029. id: z.string().meta({ description: "Session ID" }),
  1030. }),
  1031. ),
  1032. validator("json", SessionPrompt.ShellInput.omit({ sessionID: true })),
  1033. async (c) => {
  1034. const sessionID = c.req.valid("param").id
  1035. const body = c.req.valid("json")
  1036. const msg = await SessionPrompt.shell({ ...body, sessionID })
  1037. return c.json(msg)
  1038. },
  1039. )
  1040. .post(
  1041. "/session/:id/revert",
  1042. describeRoute({
  1043. description: "Revert a message",
  1044. operationId: "session.revert",
  1045. responses: {
  1046. 200: {
  1047. description: "Updated session",
  1048. content: {
  1049. "application/json": {
  1050. schema: resolver(Session.Info),
  1051. },
  1052. },
  1053. },
  1054. ...errors(400, 404),
  1055. },
  1056. }),
  1057. validator(
  1058. "param",
  1059. z.object({
  1060. id: z.string(),
  1061. }),
  1062. ),
  1063. validator("json", SessionRevert.RevertInput.omit({ sessionID: true })),
  1064. async (c) => {
  1065. const id = c.req.valid("param").id
  1066. log.info("revert", c.req.valid("json"))
  1067. const session = await SessionRevert.revert({
  1068. sessionID: id,
  1069. ...c.req.valid("json"),
  1070. })
  1071. return c.json(session)
  1072. },
  1073. )
  1074. .post(
  1075. "/session/:id/unrevert",
  1076. describeRoute({
  1077. description: "Restore all reverted messages",
  1078. operationId: "session.unrevert",
  1079. responses: {
  1080. 200: {
  1081. description: "Updated session",
  1082. content: {
  1083. "application/json": {
  1084. schema: resolver(Session.Info),
  1085. },
  1086. },
  1087. },
  1088. ...errors(400, 404),
  1089. },
  1090. }),
  1091. validator(
  1092. "param",
  1093. z.object({
  1094. id: z.string(),
  1095. }),
  1096. ),
  1097. async (c) => {
  1098. const id = c.req.valid("param").id
  1099. const session = await SessionRevert.unrevert({ sessionID: id })
  1100. return c.json(session)
  1101. },
  1102. )
  1103. .post(
  1104. "/session/:id/permissions/:permissionID",
  1105. describeRoute({
  1106. description: "Respond to a permission request",
  1107. responses: {
  1108. 200: {
  1109. description: "Permission processed successfully",
  1110. content: {
  1111. "application/json": {
  1112. schema: resolver(z.boolean()),
  1113. },
  1114. },
  1115. },
  1116. ...errors(400, 404),
  1117. },
  1118. }),
  1119. validator(
  1120. "param",
  1121. z.object({
  1122. id: z.string(),
  1123. permissionID: z.string(),
  1124. }),
  1125. ),
  1126. validator("json", z.object({ response: Permission.Response })),
  1127. async (c) => {
  1128. const params = c.req.valid("param")
  1129. const id = params.id
  1130. const permissionID = params.permissionID
  1131. Permission.respond({
  1132. sessionID: id,
  1133. permissionID,
  1134. response: c.req.valid("json").response,
  1135. })
  1136. return c.json(true)
  1137. },
  1138. )
  1139. .get(
  1140. "/command",
  1141. describeRoute({
  1142. description: "List all commands",
  1143. operationId: "command.list",
  1144. responses: {
  1145. 200: {
  1146. description: "List of commands",
  1147. content: {
  1148. "application/json": {
  1149. schema: resolver(Command.Info.array()),
  1150. },
  1151. },
  1152. },
  1153. },
  1154. }),
  1155. async (c) => {
  1156. const commands = await Command.list()
  1157. return c.json(commands)
  1158. },
  1159. )
  1160. .get(
  1161. "/config/providers",
  1162. describeRoute({
  1163. description: "List all providers",
  1164. operationId: "config.providers",
  1165. responses: {
  1166. 200: {
  1167. description: "List of providers",
  1168. content: {
  1169. "application/json": {
  1170. schema: resolver(
  1171. z.object({
  1172. providers: ModelsDev.Provider.array(),
  1173. default: z.record(z.string(), z.string()),
  1174. }),
  1175. ),
  1176. },
  1177. },
  1178. },
  1179. },
  1180. }),
  1181. async (c) => {
  1182. using _ = log.time("providers")
  1183. const providers = await Provider.list().then((x) => mapValues(x, (item) => item.info))
  1184. return c.json({
  1185. providers: Object.values(providers),
  1186. default: mapValues(providers, (item) => Provider.sort(Object.values(item.models))[0].id),
  1187. })
  1188. },
  1189. )
  1190. .get(
  1191. "/provider",
  1192. describeRoute({
  1193. description: "List all providers",
  1194. operationId: "provider.list",
  1195. responses: {
  1196. 200: {
  1197. description: "List of providers",
  1198. content: {
  1199. "application/json": {
  1200. schema: resolver(
  1201. z.object({
  1202. all: ModelsDev.Provider.array(),
  1203. default: z.record(z.string(), z.string()),
  1204. connected: z.array(z.string()),
  1205. }),
  1206. ),
  1207. },
  1208. },
  1209. },
  1210. },
  1211. }),
  1212. async (c) => {
  1213. const providers = await ModelsDev.get()
  1214. const connected = await Provider.list().then((x) => Object.keys(x))
  1215. return c.json({
  1216. all: Object.values(providers),
  1217. default: mapValues(providers, (item) => Provider.sort(Object.values(item.models))[0].id),
  1218. connected,
  1219. })
  1220. },
  1221. )
  1222. .get(
  1223. "/provider/auth",
  1224. describeRoute({
  1225. description: "Get provider authentication methods",
  1226. operationId: "provider.auth",
  1227. responses: {
  1228. 200: {
  1229. description: "Provider auth methods",
  1230. content: {
  1231. "application/json": {
  1232. schema: resolver(z.record(z.string(), z.array(ProviderAuth.Method))),
  1233. },
  1234. },
  1235. },
  1236. },
  1237. }),
  1238. async (c) => {
  1239. return c.json(await ProviderAuth.methods())
  1240. },
  1241. )
  1242. .post(
  1243. "/provider/:id/oauth/authorize",
  1244. describeRoute({
  1245. description: "Authorize a provider using OAuth",
  1246. operationId: "provider.oauth.authorize",
  1247. responses: {
  1248. 200: {
  1249. description: "Authorization URL and method",
  1250. content: {
  1251. "application/json": {
  1252. schema: resolver(ProviderAuth.Authorization.optional()),
  1253. },
  1254. },
  1255. },
  1256. ...errors(400),
  1257. },
  1258. }),
  1259. validator(
  1260. "param",
  1261. z.object({
  1262. id: z.string().meta({ description: "Provider ID" }),
  1263. }),
  1264. ),
  1265. validator(
  1266. "json",
  1267. z.object({
  1268. method: z.number().meta({ description: "Auth method index" }),
  1269. }),
  1270. ),
  1271. async (c) => {
  1272. const id = c.req.valid("param").id
  1273. const { method } = c.req.valid("json")
  1274. const result = await ProviderAuth.authorize({
  1275. providerID: id,
  1276. method,
  1277. })
  1278. return c.json(result)
  1279. },
  1280. )
  1281. .post(
  1282. "/provider/:id/oauth/callback",
  1283. describeRoute({
  1284. description: "Handle OAuth callback for a provider",
  1285. operationId: "provider.oauth.callback",
  1286. responses: {
  1287. 200: {
  1288. description: "OAuth callback processed successfully",
  1289. content: {
  1290. "application/json": {
  1291. schema: resolver(z.boolean()),
  1292. },
  1293. },
  1294. },
  1295. ...errors(400),
  1296. },
  1297. }),
  1298. validator(
  1299. "param",
  1300. z.object({
  1301. id: z.string().meta({ description: "Provider ID" }),
  1302. }),
  1303. ),
  1304. validator(
  1305. "json",
  1306. z.object({
  1307. method: z.number().meta({ description: "Auth method index" }),
  1308. code: z.string().optional().meta({ description: "OAuth authorization code" }),
  1309. }),
  1310. ),
  1311. async (c) => {
  1312. const id = c.req.valid("param").id
  1313. const { method, code } = c.req.valid("json")
  1314. await ProviderAuth.callback({
  1315. providerID: id,
  1316. method,
  1317. code,
  1318. })
  1319. return c.json(true)
  1320. },
  1321. )
  1322. .get(
  1323. "/find",
  1324. describeRoute({
  1325. description: "Find text in files",
  1326. operationId: "find.text",
  1327. responses: {
  1328. 200: {
  1329. description: "Matches",
  1330. content: {
  1331. "application/json": {
  1332. schema: resolver(Ripgrep.Match.shape.data.array()),
  1333. },
  1334. },
  1335. },
  1336. },
  1337. }),
  1338. validator(
  1339. "query",
  1340. z.object({
  1341. pattern: z.string(),
  1342. }),
  1343. ),
  1344. async (c) => {
  1345. const pattern = c.req.valid("query").pattern
  1346. const result = await Ripgrep.search({
  1347. cwd: Instance.directory,
  1348. pattern,
  1349. limit: 10,
  1350. })
  1351. return c.json(result)
  1352. },
  1353. )
  1354. .get(
  1355. "/find/file",
  1356. describeRoute({
  1357. description: "Find files",
  1358. operationId: "find.files",
  1359. responses: {
  1360. 200: {
  1361. description: "File paths",
  1362. content: {
  1363. "application/json": {
  1364. schema: resolver(z.string().array()),
  1365. },
  1366. },
  1367. },
  1368. },
  1369. }),
  1370. validator(
  1371. "query",
  1372. z.object({
  1373. query: z.string(),
  1374. dirs: z.enum(["true", "false"]).optional(),
  1375. }),
  1376. ),
  1377. async (c) => {
  1378. const query = c.req.valid("query").query
  1379. const dirs = c.req.valid("query").dirs
  1380. const results = await File.search({
  1381. query,
  1382. limit: 10,
  1383. dirs: dirs !== "false",
  1384. })
  1385. return c.json(results)
  1386. },
  1387. )
  1388. .get(
  1389. "/find/symbol",
  1390. describeRoute({
  1391. description: "Find workspace symbols",
  1392. operationId: "find.symbols",
  1393. responses: {
  1394. 200: {
  1395. description: "Symbols",
  1396. content: {
  1397. "application/json": {
  1398. schema: resolver(LSP.Symbol.array()),
  1399. },
  1400. },
  1401. },
  1402. },
  1403. }),
  1404. validator(
  1405. "query",
  1406. z.object({
  1407. query: z.string(),
  1408. }),
  1409. ),
  1410. async (c) => {
  1411. /*
  1412. const query = c.req.valid("query").query
  1413. const result = await LSP.workspaceSymbol(query)
  1414. return c.json(result)
  1415. */
  1416. return c.json([])
  1417. },
  1418. )
  1419. .get(
  1420. "/file",
  1421. describeRoute({
  1422. description: "List files and directories",
  1423. operationId: "file.list",
  1424. responses: {
  1425. 200: {
  1426. description: "Files and directories",
  1427. content: {
  1428. "application/json": {
  1429. schema: resolver(File.Node.array()),
  1430. },
  1431. },
  1432. },
  1433. },
  1434. }),
  1435. validator(
  1436. "query",
  1437. z.object({
  1438. path: z.string(),
  1439. }),
  1440. ),
  1441. async (c) => {
  1442. const path = c.req.valid("query").path
  1443. const content = await File.list(path)
  1444. return c.json(content)
  1445. },
  1446. )
  1447. .get(
  1448. "/file/content",
  1449. describeRoute({
  1450. description: "Read a file",
  1451. operationId: "file.read",
  1452. responses: {
  1453. 200: {
  1454. description: "File content",
  1455. content: {
  1456. "application/json": {
  1457. schema: resolver(File.Content),
  1458. },
  1459. },
  1460. },
  1461. },
  1462. }),
  1463. validator(
  1464. "query",
  1465. z.object({
  1466. path: z.string(),
  1467. }),
  1468. ),
  1469. async (c) => {
  1470. const path = c.req.valid("query").path
  1471. const content = await File.read(path)
  1472. return c.json(content)
  1473. },
  1474. )
  1475. .get(
  1476. "/file/status",
  1477. describeRoute({
  1478. description: "Get file status",
  1479. operationId: "file.status",
  1480. responses: {
  1481. 200: {
  1482. description: "File status",
  1483. content: {
  1484. "application/json": {
  1485. schema: resolver(File.Info.array()),
  1486. },
  1487. },
  1488. },
  1489. },
  1490. }),
  1491. async (c) => {
  1492. const content = await File.status()
  1493. return c.json(content)
  1494. },
  1495. )
  1496. .post(
  1497. "/log",
  1498. describeRoute({
  1499. description: "Write a log entry to the server logs",
  1500. operationId: "app.log",
  1501. responses: {
  1502. 200: {
  1503. description: "Log entry written successfully",
  1504. content: {
  1505. "application/json": {
  1506. schema: resolver(z.boolean()),
  1507. },
  1508. },
  1509. },
  1510. ...errors(400),
  1511. },
  1512. }),
  1513. validator(
  1514. "json",
  1515. z.object({
  1516. service: z.string().meta({ description: "Service name for the log entry" }),
  1517. level: z.enum(["debug", "info", "error", "warn"]).meta({ description: "Log level" }),
  1518. message: z.string().meta({ description: "Log message" }),
  1519. extra: z
  1520. .record(z.string(), z.any())
  1521. .optional()
  1522. .meta({ description: "Additional metadata for the log entry" }),
  1523. }),
  1524. ),
  1525. async (c) => {
  1526. const { service, level, message, extra } = c.req.valid("json")
  1527. const logger = Log.create({ service })
  1528. switch (level) {
  1529. case "debug":
  1530. logger.debug(message, extra)
  1531. break
  1532. case "info":
  1533. logger.info(message, extra)
  1534. break
  1535. case "error":
  1536. logger.error(message, extra)
  1537. break
  1538. case "warn":
  1539. logger.warn(message, extra)
  1540. break
  1541. }
  1542. return c.json(true)
  1543. },
  1544. )
  1545. .get(
  1546. "/agent",
  1547. describeRoute({
  1548. description: "List all agents",
  1549. operationId: "app.agents",
  1550. responses: {
  1551. 200: {
  1552. description: "List of agents",
  1553. content: {
  1554. "application/json": {
  1555. schema: resolver(Agent.Info.array()),
  1556. },
  1557. },
  1558. },
  1559. },
  1560. }),
  1561. async (c) => {
  1562. const modes = await Agent.list()
  1563. return c.json(modes)
  1564. },
  1565. )
  1566. .get(
  1567. "/mcp",
  1568. describeRoute({
  1569. description: "Get MCP server status",
  1570. operationId: "mcp.status",
  1571. responses: {
  1572. 200: {
  1573. description: "MCP server status",
  1574. content: {
  1575. "application/json": {
  1576. schema: resolver(z.record(z.string(), MCP.Status)),
  1577. },
  1578. },
  1579. },
  1580. },
  1581. }),
  1582. async (c) => {
  1583. return c.json(await MCP.status())
  1584. },
  1585. )
  1586. .post(
  1587. "/mcp",
  1588. describeRoute({
  1589. description: "Add MCP server dynamically",
  1590. operationId: "mcp.add",
  1591. responses: {
  1592. 200: {
  1593. description: "MCP server added successfully",
  1594. content: {
  1595. "application/json": {
  1596. schema: resolver(z.record(z.string(), MCP.Status)),
  1597. },
  1598. },
  1599. },
  1600. ...errors(400),
  1601. },
  1602. }),
  1603. validator(
  1604. "json",
  1605. z.object({
  1606. name: z.string(),
  1607. config: Config.Mcp,
  1608. }),
  1609. ),
  1610. async (c) => {
  1611. const { name, config } = c.req.valid("json")
  1612. const result = await MCP.add(name, config)
  1613. return c.json(result.status)
  1614. },
  1615. )
  1616. .get(
  1617. "/lsp",
  1618. describeRoute({
  1619. description: "Get LSP server status",
  1620. operationId: "lsp.status",
  1621. responses: {
  1622. 200: {
  1623. description: "LSP server status",
  1624. content: {
  1625. "application/json": {
  1626. schema: resolver(LSP.Status.array()),
  1627. },
  1628. },
  1629. },
  1630. },
  1631. }),
  1632. async (c) => {
  1633. return c.json(await LSP.status())
  1634. },
  1635. )
  1636. .get(
  1637. "/formatter",
  1638. describeRoute({
  1639. description: "Get formatter status",
  1640. operationId: "formatter.status",
  1641. responses: {
  1642. 200: {
  1643. description: "Formatter status",
  1644. content: {
  1645. "application/json": {
  1646. schema: resolver(Format.Status.array()),
  1647. },
  1648. },
  1649. },
  1650. },
  1651. }),
  1652. async (c) => {
  1653. return c.json(await Format.status())
  1654. },
  1655. )
  1656. .post(
  1657. "/tui/append-prompt",
  1658. describeRoute({
  1659. description: "Append prompt to the TUI",
  1660. operationId: "tui.appendPrompt",
  1661. responses: {
  1662. 200: {
  1663. description: "Prompt processed successfully",
  1664. content: {
  1665. "application/json": {
  1666. schema: resolver(z.boolean()),
  1667. },
  1668. },
  1669. },
  1670. ...errors(400),
  1671. },
  1672. }),
  1673. validator("json", TuiEvent.PromptAppend.properties),
  1674. async (c) => {
  1675. await Bus.publish(TuiEvent.PromptAppend, c.req.valid("json"))
  1676. return c.json(true)
  1677. },
  1678. )
  1679. .post(
  1680. "/tui/open-help",
  1681. describeRoute({
  1682. description: "Open the help dialog",
  1683. operationId: "tui.openHelp",
  1684. responses: {
  1685. 200: {
  1686. description: "Help dialog opened successfully",
  1687. content: {
  1688. "application/json": {
  1689. schema: resolver(z.boolean()),
  1690. },
  1691. },
  1692. },
  1693. },
  1694. }),
  1695. async (c) => {
  1696. // TODO: open dialog
  1697. return c.json(true)
  1698. },
  1699. )
  1700. .post(
  1701. "/tui/open-sessions",
  1702. describeRoute({
  1703. description: "Open the session dialog",
  1704. operationId: "tui.openSessions",
  1705. responses: {
  1706. 200: {
  1707. description: "Session dialog opened successfully",
  1708. content: {
  1709. "application/json": {
  1710. schema: resolver(z.boolean()),
  1711. },
  1712. },
  1713. },
  1714. },
  1715. }),
  1716. async (c) => {
  1717. await Bus.publish(TuiEvent.CommandExecute, {
  1718. command: "session.list",
  1719. })
  1720. return c.json(true)
  1721. },
  1722. )
  1723. .post(
  1724. "/tui/open-themes",
  1725. describeRoute({
  1726. description: "Open the theme dialog",
  1727. operationId: "tui.openThemes",
  1728. responses: {
  1729. 200: {
  1730. description: "Theme dialog opened successfully",
  1731. content: {
  1732. "application/json": {
  1733. schema: resolver(z.boolean()),
  1734. },
  1735. },
  1736. },
  1737. },
  1738. }),
  1739. async (c) => {
  1740. await Bus.publish(TuiEvent.CommandExecute, {
  1741. command: "session.list",
  1742. })
  1743. return c.json(true)
  1744. },
  1745. )
  1746. .post(
  1747. "/tui/open-models",
  1748. describeRoute({
  1749. description: "Open the model dialog",
  1750. operationId: "tui.openModels",
  1751. responses: {
  1752. 200: {
  1753. description: "Model dialog opened successfully",
  1754. content: {
  1755. "application/json": {
  1756. schema: resolver(z.boolean()),
  1757. },
  1758. },
  1759. },
  1760. },
  1761. }),
  1762. async (c) => {
  1763. await Bus.publish(TuiEvent.CommandExecute, {
  1764. command: "model.list",
  1765. })
  1766. return c.json(true)
  1767. },
  1768. )
  1769. .post(
  1770. "/tui/submit-prompt",
  1771. describeRoute({
  1772. description: "Submit the prompt",
  1773. operationId: "tui.submitPrompt",
  1774. responses: {
  1775. 200: {
  1776. description: "Prompt submitted successfully",
  1777. content: {
  1778. "application/json": {
  1779. schema: resolver(z.boolean()),
  1780. },
  1781. },
  1782. },
  1783. },
  1784. }),
  1785. async (c) => {
  1786. await Bus.publish(TuiEvent.CommandExecute, {
  1787. command: "prompt.submit",
  1788. })
  1789. return c.json(true)
  1790. },
  1791. )
  1792. .post(
  1793. "/tui/clear-prompt",
  1794. describeRoute({
  1795. description: "Clear the prompt",
  1796. operationId: "tui.clearPrompt",
  1797. responses: {
  1798. 200: {
  1799. description: "Prompt cleared successfully",
  1800. content: {
  1801. "application/json": {
  1802. schema: resolver(z.boolean()),
  1803. },
  1804. },
  1805. },
  1806. },
  1807. }),
  1808. async (c) => {
  1809. await Bus.publish(TuiEvent.CommandExecute, {
  1810. command: "prompt.clear",
  1811. })
  1812. return c.json(true)
  1813. },
  1814. )
  1815. .post(
  1816. "/tui/execute-command",
  1817. describeRoute({
  1818. description: "Execute a TUI command (e.g. agent_cycle)",
  1819. operationId: "tui.executeCommand",
  1820. responses: {
  1821. 200: {
  1822. description: "Command executed successfully",
  1823. content: {
  1824. "application/json": {
  1825. schema: resolver(z.boolean()),
  1826. },
  1827. },
  1828. },
  1829. ...errors(400),
  1830. },
  1831. }),
  1832. validator("json", z.object({ command: z.string() })),
  1833. async (c) => {
  1834. const command = c.req.valid("json").command
  1835. await Bus.publish(TuiEvent.CommandExecute, {
  1836. // @ts-expect-error
  1837. command: {
  1838. session_new: "session.new",
  1839. session_share: "session.share",
  1840. session_interrupt: "session.interrupt",
  1841. session_compact: "session.compact",
  1842. messages_page_up: "session.page.up",
  1843. messages_page_down: "session.page.down",
  1844. messages_half_page_up: "session.half.page.up",
  1845. messages_half_page_down: "session.half.page.down",
  1846. messages_first: "session.first",
  1847. messages_last: "session.last",
  1848. agent_cycle: "agent.cycle",
  1849. }[command],
  1850. })
  1851. return c.json(true)
  1852. },
  1853. )
  1854. .post(
  1855. "/tui/show-toast",
  1856. describeRoute({
  1857. description: "Show a toast notification in the TUI",
  1858. operationId: "tui.showToast",
  1859. responses: {
  1860. 200: {
  1861. description: "Toast notification shown successfully",
  1862. content: {
  1863. "application/json": {
  1864. schema: resolver(z.boolean()),
  1865. },
  1866. },
  1867. },
  1868. },
  1869. }),
  1870. validator("json", TuiEvent.ToastShow.properties),
  1871. async (c) => {
  1872. await Bus.publish(TuiEvent.ToastShow, c.req.valid("json"))
  1873. return c.json(true)
  1874. },
  1875. )
  1876. .post(
  1877. "/tui/publish",
  1878. describeRoute({
  1879. description: "Publish a TUI event",
  1880. operationId: "tui.publish",
  1881. responses: {
  1882. 200: {
  1883. description: "Event published successfully",
  1884. content: {
  1885. "application/json": {
  1886. schema: resolver(z.boolean()),
  1887. },
  1888. },
  1889. },
  1890. ...errors(400),
  1891. },
  1892. }),
  1893. validator(
  1894. "json",
  1895. z.union(
  1896. Object.values(TuiEvent).map((def) => {
  1897. return z
  1898. .object({
  1899. type: z.literal(def.type),
  1900. properties: def.properties,
  1901. })
  1902. .meta({
  1903. ref: "Event" + "." + def.type,
  1904. })
  1905. }),
  1906. ),
  1907. ),
  1908. async (c) => {
  1909. const evt = c.req.valid("json")
  1910. await Bus.publish(Object.values(TuiEvent).find((def) => def.type === evt.type)!, evt.properties)
  1911. return c.json(true)
  1912. },
  1913. )
  1914. .route("/tui/control", TuiRoute)
  1915. .put(
  1916. "/auth/:id",
  1917. describeRoute({
  1918. description: "Set authentication credentials",
  1919. operationId: "auth.set",
  1920. responses: {
  1921. 200: {
  1922. description: "Successfully set authentication credentials",
  1923. content: {
  1924. "application/json": {
  1925. schema: resolver(z.boolean()),
  1926. },
  1927. },
  1928. },
  1929. ...errors(400),
  1930. },
  1931. }),
  1932. validator(
  1933. "param",
  1934. z.object({
  1935. id: z.string(),
  1936. }),
  1937. ),
  1938. validator("json", Auth.Info),
  1939. async (c) => {
  1940. const id = c.req.valid("param").id
  1941. const info = c.req.valid("json")
  1942. await Auth.set(id, info)
  1943. return c.json(true)
  1944. },
  1945. )
  1946. .get(
  1947. "/event",
  1948. describeRoute({
  1949. description: "Get events",
  1950. operationId: "event.subscribe",
  1951. responses: {
  1952. 200: {
  1953. description: "Event stream",
  1954. content: {
  1955. "text/event-stream": {
  1956. schema: resolver(Bus.payloads()),
  1957. },
  1958. },
  1959. },
  1960. },
  1961. }),
  1962. async (c) => {
  1963. log.info("event connected")
  1964. return streamSSE(c, async (stream) => {
  1965. stream.writeSSE({
  1966. data: JSON.stringify({
  1967. type: "server.connected",
  1968. properties: {},
  1969. }),
  1970. })
  1971. const unsub = Bus.subscribeAll(async (event) => {
  1972. await stream.writeSSE({
  1973. data: JSON.stringify(event),
  1974. })
  1975. })
  1976. await new Promise<void>((resolve) => {
  1977. stream.onAbort(() => {
  1978. unsub()
  1979. resolve()
  1980. log.info("event disconnected")
  1981. })
  1982. })
  1983. })
  1984. },
  1985. )
  1986. .all("/*", async (c) => {
  1987. return proxy(`https://desktop.dev.opencode.ai${c.req.path}`, {
  1988. ...c.req,
  1989. headers: {
  1990. host: "desktop.dev.opencode.ai",
  1991. },
  1992. })
  1993. }),
  1994. )
  1995. export async function openapi() {
  1996. const result = await generateSpecs(App(), {
  1997. documentation: {
  1998. info: {
  1999. title: "opencode",
  2000. version: "1.0.0",
  2001. description: "opencode api",
  2002. },
  2003. openapi: "3.1.1",
  2004. },
  2005. })
  2006. return result
  2007. }
  2008. export function listen(opts: { port: number; hostname: string }) {
  2009. const server = Bun.serve({
  2010. port: opts.port,
  2011. hostname: opts.hostname,
  2012. idleTimeout: 0,
  2013. fetch: App().fetch,
  2014. })
  2015. return server
  2016. }
  2017. }