tool-question.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Exit, Fiber, Layer } from "effect"
  3. import { PermissionV2 } from "@opencode-ai/core/permission"
  4. import { QuestionV2 } from "@opencode-ai/core/question"
  5. import { SessionV2 } from "@opencode-ai/core/session"
  6. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  7. import { QuestionTool } from "@opencode-ai/core/tool/question"
  8. import { testEffect } from "./lib/effect"
  9. import { toolIdentity, executeTool, settleTool, toolDefinitions } from "./lib/tool"
  10. const sessionID = SessionV2.ID.make("ses_question_tool_test")
  11. const assertions: PermissionV2.AssertInput[] = []
  12. let captured: QuestionV2.AskInput | undefined
  13. let reject = false
  14. let deny = false
  15. const capturedInput = () => captured
  16. const permission = Layer.succeed(
  17. PermissionV2.Service,
  18. PermissionV2.Service.of({
  19. assert: (input) =>
  20. Effect.sync(() => assertions.push(input)).pipe(
  21. Effect.andThen(deny ? Effect.fail(new PermissionV2.DeniedError({ rules: [] })) : Effect.void),
  22. ),
  23. ask: () => Effect.die("unused"),
  24. reply: () => Effect.die("unused"),
  25. get: () => Effect.die("unused"),
  26. forSession: () => Effect.die("unused"),
  27. list: () => Effect.die("unused"),
  28. }),
  29. )
  30. const registry = ToolRegistry.defaultLayer.pipe(Layer.provide(permission))
  31. const question = Layer.succeed(
  32. QuestionV2.Service,
  33. QuestionV2.Service.of({
  34. ask: (input: QuestionV2.AskInput) =>
  35. Effect.sync(() => {
  36. captured = input
  37. }).pipe(Effect.andThen(reject ? Effect.fail(new QuestionV2.RejectedError()) : Effect.succeed([["Build"], []]))),
  38. reply: () => Effect.die("unused"),
  39. reject: () => Effect.die("unused"),
  40. list: () => Effect.die("unused"),
  41. }),
  42. )
  43. const tool = QuestionTool.layer.pipe(Layer.provide(registry), Layer.provide(permission), Layer.provide(question))
  44. const it = testEffect(Layer.mergeAll(permission, registry, question, tool))
  45. describe("QuestionTool", () => {
  46. it.effect("omits a denied built-in question and terminally settles a stale call", () =>
  47. Effect.gen(function* () {
  48. captured = undefined
  49. deny = true
  50. const registry = yield* ToolRegistry.Service
  51. expect(yield* toolDefinitions(registry, [{ action: "question", resource: "*", effect: "deny" }])).toEqual([])
  52. expect(
  53. yield* settleTool(registry, {
  54. sessionID,
  55. ...toolIdentity,
  56. call: { type: "tool-call", id: "call-question-denied", name: "question", input: { questions: [] } },
  57. }),
  58. ).toEqual({ result: { type: "error", value: "Permission denied: question" } })
  59. expect(capturedInput()).toBeUndefined()
  60. deny = false
  61. }),
  62. )
  63. it.effect("registers question and projects user answers without a permission assertion", () =>
  64. Effect.gen(function* () {
  65. assertions.length = 0
  66. captured = undefined
  67. reject = false
  68. deny = false
  69. const registry = yield* ToolRegistry.Service
  70. const questions = [
  71. {
  72. question: "What should happen?",
  73. header: "Action",
  74. options: [{ label: "Build", description: "Build it" }],
  75. },
  76. {
  77. question: "Which environment?",
  78. header: "Environment",
  79. options: [{ label: "Dev", description: "Development" }],
  80. },
  81. ]
  82. expect((yield* toolDefinitions(registry)).map((definition) => definition.name)).toEqual(["question"])
  83. expect(
  84. yield* settleTool(registry, {
  85. sessionID,
  86. ...toolIdentity,
  87. call: { type: "tool-call", id: "call-question", name: "question", input: { questions } },
  88. }),
  89. ).toEqual({
  90. result: {
  91. type: "text",
  92. value:
  93. 'User has answered your questions: "What should happen?"="Build", "Which environment?"="Unanswered". You can now continue with the user\'s answers in mind.',
  94. },
  95. output: {
  96. structured: { answers: [["Build"], []] },
  97. content: [
  98. {
  99. type: "text",
  100. text: 'User has answered your questions: "What should happen?"="Build", "Which environment?"="Unanswered". You can now continue with the user\'s answers in mind.',
  101. },
  102. ],
  103. },
  104. })
  105. expect(assertions).toMatchObject([{ sessionID, action: "question", resources: ["*"] }])
  106. expect(capturedInput()).toEqual({
  107. sessionID,
  108. questions,
  109. tool: { messageID: toolIdentity.assistantMessageID, callID: "call-question" },
  110. })
  111. }),
  112. )
  113. it.effect("does not invent tool ownership metadata without a durable registry source", () =>
  114. Effect.gen(function* () {
  115. captured = undefined
  116. reject = false
  117. deny = false
  118. const registryService = yield* ToolRegistry.Service
  119. yield* executeTool(registryService, {
  120. sessionID,
  121. ...toolIdentity,
  122. call: { type: "tool-call", id: "call-question", name: "question", input: { questions: [] } },
  123. })
  124. expect(capturedInput()).toEqual({
  125. sessionID,
  126. questions: [],
  127. tool: { messageID: toolIdentity.assistantMessageID, callID: "call-question" },
  128. })
  129. }),
  130. )
  131. it.effect("keeps dismissed questions out of model-facing output", () =>
  132. Effect.gen(function* () {
  133. captured = undefined
  134. reject = true
  135. deny = false
  136. const registryService = yield* ToolRegistry.Service
  137. const fiber = yield* executeTool(registryService, {
  138. sessionID,
  139. ...toolIdentity,
  140. call: { type: "tool-call", id: "call-question", name: "question", input: { questions: [] } },
  141. }).pipe(Effect.forkScoped)
  142. const exit = yield* Fiber.await(fiber)
  143. expect(Exit.isFailure(exit)).toBe(true)
  144. }),
  145. )
  146. })