service.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { Deferred, Effect, Layer, Schema, ServiceMap } from "effect"
  2. import { Bus } from "@/bus"
  3. import { BusEvent } from "@/bus/bus-event"
  4. import { SessionID, MessageID } from "@/session/schema"
  5. import { Log } from "@/util/log"
  6. import z from "zod"
  7. import { QuestionID } from "./schema"
  8. const log = Log.create({ service: "question" })
  9. export namespace Question {
  10. // Schemas
  11. export const Option = z
  12. .object({
  13. label: z.string().describe("Display text (1-5 words, concise)"),
  14. description: z.string().describe("Explanation of choice"),
  15. })
  16. .meta({ ref: "QuestionOption" })
  17. export type Option = z.infer<typeof Option>
  18. export const Info = z
  19. .object({
  20. question: z.string().describe("Complete question"),
  21. header: z.string().describe("Very short label (max 30 chars)"),
  22. options: z.array(Option).describe("Available choices"),
  23. multiple: z.boolean().optional().describe("Allow selecting multiple choices"),
  24. custom: z.boolean().optional().describe("Allow typing a custom answer (default: true)"),
  25. })
  26. .meta({ ref: "QuestionInfo" })
  27. export type Info = z.infer<typeof Info>
  28. export const Request = z
  29. .object({
  30. id: QuestionID.zod,
  31. sessionID: SessionID.zod,
  32. questions: z.array(Info).describe("Questions to ask"),
  33. tool: z
  34. .object({
  35. messageID: MessageID.zod,
  36. callID: z.string(),
  37. })
  38. .optional(),
  39. })
  40. .meta({ ref: "QuestionRequest" })
  41. export type Request = z.infer<typeof Request>
  42. export const Answer = z.array(z.string()).meta({ ref: "QuestionAnswer" })
  43. export type Answer = z.infer<typeof Answer>
  44. export const Reply = z.object({
  45. answers: z
  46. .array(Answer)
  47. .describe("User answers in order of questions (each answer is an array of selected labels)"),
  48. })
  49. export type Reply = z.infer<typeof Reply>
  50. export const Event = {
  51. Asked: BusEvent.define("question.asked", Request),
  52. Replied: BusEvent.define(
  53. "question.replied",
  54. z.object({
  55. sessionID: SessionID.zod,
  56. requestID: QuestionID.zod,
  57. answers: z.array(Answer),
  58. }),
  59. ),
  60. Rejected: BusEvent.define(
  61. "question.rejected",
  62. z.object({
  63. sessionID: SessionID.zod,
  64. requestID: QuestionID.zod,
  65. }),
  66. ),
  67. }
  68. export class RejectedError extends Schema.TaggedErrorClass<RejectedError>()("QuestionRejectedError", {}) {
  69. override get message() {
  70. return "The user dismissed this question"
  71. }
  72. }
  73. interface PendingEntry {
  74. info: Request
  75. deferred: Deferred.Deferred<Answer[], RejectedError>
  76. }
  77. // Service
  78. export interface Interface {
  79. readonly ask: (input: {
  80. sessionID: SessionID
  81. questions: Info[]
  82. tool?: { messageID: MessageID; callID: string }
  83. }) => Effect.Effect<Answer[], RejectedError>
  84. readonly reply: (input: { requestID: QuestionID; answers: Answer[] }) => Effect.Effect<void>
  85. readonly reject: (requestID: QuestionID) => Effect.Effect<void>
  86. readonly list: () => Effect.Effect<Request[]>
  87. }
  88. export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/Question") {}
  89. export const layer = Layer.effect(
  90. Service,
  91. Effect.gen(function* () {
  92. const pending = new Map<QuestionID, PendingEntry>()
  93. const ask = Effect.fn("Question.ask")(function* (input: {
  94. sessionID: SessionID
  95. questions: Info[]
  96. tool?: { messageID: MessageID; callID: string }
  97. }) {
  98. const id = QuestionID.ascending()
  99. log.info("asking", { id, questions: input.questions.length })
  100. const deferred = yield* Deferred.make<Answer[], RejectedError>()
  101. const info: Request = {
  102. id,
  103. sessionID: input.sessionID,
  104. questions: input.questions,
  105. tool: input.tool,
  106. }
  107. pending.set(id, { info, deferred })
  108. Bus.publish(Event.Asked, info)
  109. return yield* Effect.ensuring(
  110. Deferred.await(deferred),
  111. Effect.sync(() => {
  112. pending.delete(id)
  113. }),
  114. )
  115. })
  116. const reply = Effect.fn("Question.reply")(function* (input: { requestID: QuestionID; answers: Answer[] }) {
  117. const existing = pending.get(input.requestID)
  118. if (!existing) {
  119. log.warn("reply for unknown request", { requestID: input.requestID })
  120. return
  121. }
  122. pending.delete(input.requestID)
  123. log.info("replied", { requestID: input.requestID, answers: input.answers })
  124. Bus.publish(Event.Replied, {
  125. sessionID: existing.info.sessionID,
  126. requestID: existing.info.id,
  127. answers: input.answers,
  128. })
  129. yield* Deferred.succeed(existing.deferred, input.answers)
  130. })
  131. const reject = Effect.fn("Question.reject")(function* (requestID: QuestionID) {
  132. const existing = pending.get(requestID)
  133. if (!existing) {
  134. log.warn("reject for unknown request", { requestID })
  135. return
  136. }
  137. pending.delete(requestID)
  138. log.info("rejected", { requestID })
  139. Bus.publish(Event.Rejected, {
  140. sessionID: existing.info.sessionID,
  141. requestID: existing.info.id,
  142. })
  143. yield* Deferred.fail(existing.deferred, new RejectedError())
  144. })
  145. const list = Effect.fn("Question.list")(function* () {
  146. return Array.from(pending.values(), (x) => x.info)
  147. })
  148. return Service.of({ ask, reply, reject, list })
  149. }),
  150. ).pipe(Layer.fresh)
  151. }