server.ts 49 KB

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