question.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { Hono } from "hono"
  2. import { describeRoute, validator } from "hono-openapi"
  3. import { resolver } from "hono-openapi"
  4. import { Question } from "../question"
  5. import z from "zod"
  6. import { errors } from "./error"
  7. export const QuestionRoute = new Hono()
  8. .get(
  9. "/",
  10. describeRoute({
  11. summary: "List pending questions",
  12. description: "Get all pending question requests across all sessions.",
  13. operationId: "question.list",
  14. responses: {
  15. 200: {
  16. description: "List of pending questions",
  17. content: {
  18. "application/json": {
  19. schema: resolver(Question.Request.array()),
  20. },
  21. },
  22. },
  23. },
  24. }),
  25. async (c) => {
  26. const questions = await Question.list()
  27. return c.json(questions)
  28. },
  29. )
  30. .post(
  31. "/:requestID/reply",
  32. describeRoute({
  33. summary: "Reply to question request",
  34. description: "Provide answers to a question request from the AI assistant.",
  35. operationId: "question.reply",
  36. responses: {
  37. 200: {
  38. description: "Question answered successfully",
  39. content: {
  40. "application/json": {
  41. schema: resolver(z.boolean()),
  42. },
  43. },
  44. },
  45. ...errors(400, 404),
  46. },
  47. }),
  48. validator(
  49. "param",
  50. z.object({
  51. requestID: z.string(),
  52. }),
  53. ),
  54. validator("json", Question.Reply),
  55. async (c) => {
  56. const params = c.req.valid("param")
  57. const json = c.req.valid("json")
  58. await Question.reply({
  59. requestID: params.requestID,
  60. answers: json.answers,
  61. })
  62. return c.json(true)
  63. },
  64. )
  65. .post(
  66. "/:requestID/reject",
  67. describeRoute({
  68. summary: "Reject question request",
  69. description: "Reject a question request from the AI assistant.",
  70. operationId: "question.reject",
  71. responses: {
  72. 200: {
  73. description: "Question rejected successfully",
  74. content: {
  75. "application/json": {
  76. schema: resolver(z.boolean()),
  77. },
  78. },
  79. },
  80. ...errors(400, 404),
  81. },
  82. }),
  83. validator(
  84. "param",
  85. z.object({
  86. requestID: z.string(),
  87. }),
  88. ),
  89. async (c) => {
  90. const params = c.req.valid("param")
  91. await Question.reject(params.requestID)
  92. return c.json(true)
  93. },
  94. )