tool-question.test.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { describe, expect } from "bun:test"
  2. import { Cause, Effect, Exit, Fiber, Layer } from "effect"
  3. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  4. import { LayerNode } from "@opencode-ai/core/effect/layer-node"
  5. import { Form } from "@opencode-ai/core/form"
  6. import { PermissionV2 } from "@opencode-ai/core/permission"
  7. import { SessionV2 } from "@opencode-ai/core/session"
  8. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  9. import { QuestionTool } from "@opencode-ai/core/tool/question"
  10. import { ToolOutputStore } from "@opencode-ai/core/tool-output-store"
  11. import { testEffect } from "./lib/effect"
  12. import { makeLocationNode } from "@opencode-ai/core/effect/app-node"
  13. import { toolIdentity, executeTool, registerToolPlugin, settleTool, toolDefinitions } from "./lib/tool"
  14. const sessionID = SessionV2.ID.make("ses_question_tool_test")
  15. const assertions: PermissionV2.AssertInput[] = []
  16. let captured: Form.CreateInput | undefined
  17. let reject = false
  18. let deny = false
  19. const capturedInput = () => captured
  20. const questionInput = {
  21. questions: [
  22. {
  23. question: "Continue?",
  24. header: "Continue",
  25. options: [{ label: "Yes", description: "Continue" }],
  26. },
  27. ],
  28. }
  29. const permission = Layer.succeed(
  30. PermissionV2.Service,
  31. PermissionV2.Service.of({
  32. assert: (input) =>
  33. Effect.sync(() => assertions.push(input)).pipe(
  34. Effect.andThen(
  35. deny
  36. ? Effect.fail(
  37. new PermissionV2.BlockedError({
  38. rules: [],
  39. permission: input.action,
  40. resources: input.resources,
  41. }),
  42. )
  43. : Effect.void,
  44. ),
  45. ),
  46. ask: () => Effect.die("unused"),
  47. reply: () => Effect.die("unused"),
  48. get: () => Effect.die("unused"),
  49. forSession: () => Effect.die("unused"),
  50. list: () => Effect.die("unused"),
  51. }),
  52. )
  53. const form = Layer.succeed(
  54. Form.Service,
  55. Form.Service.of({
  56. ask: (input: Form.CreateInput) =>
  57. Effect.sync(() => {
  58. captured = input
  59. }).pipe(
  60. Effect.andThen(
  61. Effect.sync(
  62. (): Form.TerminalState =>
  63. reject ? { status: "cancelled" } : { status: "answered", answer: { q0: "Build", q1: ["Dev"] } },
  64. ),
  65. ),
  66. ),
  67. create: () => Effect.die("unused"),
  68. get: () => Effect.die("unused"),
  69. list: () => Effect.die("unused"),
  70. state: () => Effect.die("unused"),
  71. reply: () => Effect.die("unused"),
  72. cancel: () => Effect.die("unused"),
  73. }),
  74. )
  75. const questionToolNode = makeLocationNode({
  76. name: "test/question-tool-plugin",
  77. layer: Layer.effectDiscard(registerToolPlugin(QuestionTool.Plugin)),
  78. deps: [ToolRegistry.toolsNode, PermissionV2.node, Form.node],
  79. })
  80. const it = testEffect(
  81. AppNodeBuilder.build(LayerNode.group([ToolRegistry.node, ToolRegistry.toolsNode, questionToolNode]), [
  82. [PermissionV2.node, permission],
  83. [Form.node, form],
  84. [ToolOutputStore.node, ToolOutputStore.nodeWithoutConfig],
  85. ]),
  86. )
  87. describe("QuestionTool", () => {
  88. it.effect("omits a catalog-denied question and enforces its leaf permission", () =>
  89. Effect.gen(function* () {
  90. captured = undefined
  91. deny = true
  92. const registry = yield* ToolRegistry.Service
  93. expect(yield* toolDefinitions(registry, [{ action: "question", resource: "*", effect: "deny" }])).toEqual([])
  94. expect(
  95. yield* settleTool(registry, {
  96. sessionID,
  97. ...toolIdentity,
  98. call: { type: "tool-call", id: "call-question-denied", name: "question", input: questionInput },
  99. }),
  100. ).toEqual({
  101. result: { type: "error", value: "Permission denied: question" },
  102. error: {
  103. type: "permission.rejected",
  104. message: "Permission denied: question",
  105. },
  106. })
  107. expect(capturedInput()).toBeUndefined()
  108. deny = false
  109. }),
  110. )
  111. it.effect("registers question and projects user answers without a permission assertion", () =>
  112. Effect.gen(function* () {
  113. assertions.length = 0
  114. captured = undefined
  115. reject = false
  116. deny = false
  117. const registry = yield* ToolRegistry.Service
  118. const questions = [
  119. {
  120. question: "What should happen?",
  121. header: "Action",
  122. options: [{ label: "Build", description: "Build it" }],
  123. },
  124. {
  125. question: "Which environment?",
  126. header: "Environment",
  127. options: [{ label: "Dev", description: "Development" }],
  128. multiple: true,
  129. },
  130. {
  131. question: "Anything else?",
  132. header: "Optional",
  133. options: [],
  134. },
  135. ]
  136. expect((yield* toolDefinitions(registry)).map((definition) => definition.name)).toEqual(["question"])
  137. expect(
  138. yield* settleTool(registry, {
  139. sessionID,
  140. ...toolIdentity,
  141. call: { type: "tool-call", id: "call-question", name: "question", input: { questions } },
  142. }),
  143. ).toEqual({
  144. result: {
  145. type: "text",
  146. value:
  147. 'User has answered your questions: "What should happen?"="Build", "Which environment?"="Dev", "Anything else?"="Unanswered". You can now continue with the user\'s answers in mind.',
  148. },
  149. output: {
  150. structured: { answers: [["Build"], ["Dev"], []] },
  151. content: [
  152. {
  153. type: "text",
  154. text: 'User has answered your questions: "What should happen?"="Build", "Which environment?"="Dev", "Anything else?"="Unanswered". You can now continue with the user\'s answers in mind.',
  155. },
  156. ],
  157. },
  158. })
  159. expect(assertions).toMatchObject([{ sessionID, action: "question", resources: ["*"] }])
  160. expect(capturedInput()).toEqual({
  161. sessionID,
  162. title: "Questions",
  163. metadata: { kind: "question", tool: { messageID: toolIdentity.assistantMessageID, callID: "call-question" } },
  164. fields: [
  165. {
  166. key: "q0",
  167. title: "Action",
  168. description: "What should happen?",
  169. options: [{ value: "Build", label: "Build", description: "Build it" }],
  170. custom: true,
  171. type: "string",
  172. },
  173. {
  174. key: "q1",
  175. title: "Environment",
  176. description: "Which environment?",
  177. options: [{ value: "Dev", label: "Dev", description: "Development" }],
  178. custom: true,
  179. type: "multiselect",
  180. },
  181. {
  182. key: "q2",
  183. title: "Optional",
  184. description: "Anything else?",
  185. options: [],
  186. custom: true,
  187. type: "string",
  188. },
  189. ],
  190. })
  191. }),
  192. )
  193. it.effect("does not invent tool ownership metadata without a durable registry source", () =>
  194. Effect.gen(function* () {
  195. captured = undefined
  196. reject = false
  197. deny = false
  198. const registryService = yield* ToolRegistry.Service
  199. yield* executeTool(registryService, {
  200. sessionID,
  201. ...toolIdentity,
  202. call: { type: "tool-call", id: "call-question", name: "question", input: questionInput },
  203. })
  204. expect(capturedInput()).toEqual({
  205. sessionID,
  206. title: "Questions",
  207. metadata: { kind: "question", tool: { messageID: toolIdentity.assistantMessageID, callID: "call-question" } },
  208. fields: [
  209. {
  210. key: "q0",
  211. title: "Continue",
  212. description: "Continue?",
  213. options: [{ value: "Yes", label: "Yes", description: "Continue" }],
  214. custom: true,
  215. type: "string",
  216. },
  217. ],
  218. })
  219. }),
  220. )
  221. it.effect("keeps dismissed questions out of model-facing output", () =>
  222. Effect.gen(function* () {
  223. captured = undefined
  224. reject = true
  225. deny = false
  226. const registryService = yield* ToolRegistry.Service
  227. const fiber = yield* executeTool(registryService, {
  228. sessionID,
  229. ...toolIdentity,
  230. call: { type: "tool-call", id: "call-question", name: "question", input: questionInput },
  231. }).pipe(Effect.forkScoped)
  232. const exit = yield* Fiber.await(fiber)
  233. expect(Exit.isFailure(exit)).toBe(true)
  234. if (Exit.isFailure(exit)) {
  235. const error = Cause.squash(exit.cause)
  236. expect(error).toBeInstanceOf(QuestionTool.CancelledError)
  237. expect(error).toHaveProperty("message", "The user dismissed this question")
  238. }
  239. }),
  240. )
  241. })