tui.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Hono, type Context } from "hono"
  2. import { describeRoute, resolver, validator } from "hono-openapi"
  3. import { z } from "zod"
  4. import { AsyncQueue } from "../util/queue"
  5. const TuiRequest = z.object({
  6. path: z.string(),
  7. body: z.any(),
  8. })
  9. type TuiRequest = z.infer<typeof TuiRequest>
  10. const request = new AsyncQueue<TuiRequest>()
  11. const response = new AsyncQueue<any>()
  12. export async function callTui(ctx: Context) {
  13. const body = await ctx.req.json()
  14. request.push({
  15. path: ctx.req.path,
  16. body,
  17. })
  18. return response.next()
  19. }
  20. export const TuiRoute = new Hono()
  21. .get(
  22. "/next",
  23. describeRoute({
  24. summary: "Get next TUI request",
  25. description: "Retrieve the next TUI (Terminal User Interface) request from the queue for processing.",
  26. operationId: "tui.control.next",
  27. responses: {
  28. 200: {
  29. description: "Next TUI request",
  30. content: {
  31. "application/json": {
  32. schema: resolver(TuiRequest),
  33. },
  34. },
  35. },
  36. },
  37. }),
  38. async (c) => {
  39. const req = await request.next()
  40. return c.json(req)
  41. },
  42. )
  43. .post(
  44. "/response",
  45. describeRoute({
  46. summary: "Submit TUI response",
  47. description: "Submit a response to the TUI request queue to complete a pending request.",
  48. operationId: "tui.control.response",
  49. responses: {
  50. 200: {
  51. description: "Response submitted successfully",
  52. content: {
  53. "application/json": {
  54. schema: resolver(z.boolean()),
  55. },
  56. },
  57. },
  58. },
  59. }),
  60. validator("json", z.any()),
  61. async (c) => {
  62. const body = c.req.valid("json")
  63. response.push(body)
  64. return c.json(true)
  65. },
  66. )