tool-question.test.ts 8.0 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/util/effect/layer-node"
  5. import { Form } from "@opencode-ai/core/form"
  6. import { Permission } from "@opencode-ai/core/permission"
  7. import { Session } from "@opencode-ai/core/session"
  8. import { Tool } from "@opencode-ai/core/tool"
  9. import { QuestionTool } from "@opencode-ai/core/tool/plugin/question"
  10. import { Image } from "@opencode-ai/core/image"
  11. import { testEffect } from "./lib/effect"
  12. import { imagePassthrough } from "./lib/image"
  13. import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
  14. import { toolIdentity, executeTool, registerToolPlugin, toolDefinitions } from "./lib/tool"
  15. const sessionID = Session.ID.make("ses_question_tool_test")
  16. const assertions: Permission.AssertInput[] = []
  17. let captured: Form.CreateInput | undefined
  18. let reject = false
  19. let deny = false
  20. const capturedInput = () => captured
  21. const questionInput = {
  22. questions: [
  23. {
  24. question: "Continue?",
  25. header: "Continue",
  26. options: [{ label: "Yes", description: "Continue" }],
  27. },
  28. ],
  29. }
  30. const permission = Layer.succeed(
  31. Permission.Service,
  32. Permission.Service.of({
  33. assert: (input) =>
  34. Effect.sync(() => assertions.push(input)).pipe(
  35. Effect.andThen(
  36. deny
  37. ? Effect.fail(
  38. new Permission.BlockedError({
  39. rules: [],
  40. permission: input.action,
  41. resources: input.resources,
  42. }),
  43. )
  44. : Effect.void,
  45. ),
  46. ),
  47. ask: () => Effect.die("unused"),
  48. reply: () => Effect.die("unused"),
  49. get: () => Effect.die("unused"),
  50. forSession: () => Effect.die("unused"),
  51. list: () => Effect.die("unused"),
  52. }),
  53. )
  54. const form = Layer.succeed(
  55. Form.Service,
  56. Form.Service.of({
  57. ask: (input: Form.CreateInput) =>
  58. Effect.sync(() => {
  59. captured = input
  60. }).pipe(
  61. Effect.andThen(
  62. Effect.sync(
  63. (): Form.TerminalState =>
  64. reject ? { status: "cancelled" } : { status: "answered", answer: { q0: "Build", q1: ["Dev"] } },
  65. ),
  66. ),
  67. ),
  68. create: () => Effect.die("unused"),
  69. get: () => Effect.die("unused"),
  70. list: () => Effect.die("unused"),
  71. state: () => Effect.die("unused"),
  72. reply: () => Effect.die("unused"),
  73. cancel: () => Effect.die("unused"),
  74. }),
  75. )
  76. const questionToolNode = makeLocationNode({
  77. name: "test/question-tool-plugin",
  78. layer: Layer.effectDiscard(registerToolPlugin(QuestionTool.Plugin)),
  79. deps: [Tool.node, Permission.node, Form.node],
  80. })
  81. const it = testEffect(
  82. AppNodeBuilder.build(LayerNode.group([Tool.node, questionToolNode]), [
  83. [Permission.node, permission],
  84. [Form.node, form],
  85. [Image.node, imagePassthrough],
  86. ]),
  87. )
  88. describe("QuestionTool", () => {
  89. it.effect("omits a catalog-denied question and enforces its leaf permission", () =>
  90. Effect.gen(function* () {
  91. captured = undefined
  92. deny = true
  93. const registry = yield* Tool.Service
  94. expect(
  95. (yield* toolDefinitions(registry, [{ action: "question", resource: "*", effect: "deny" }])).map(
  96. (tool) => tool.name,
  97. ),
  98. ).toEqual(["execute"])
  99. expect(
  100. yield* executeTool(registry, {
  101. sessionID,
  102. ...toolIdentity,
  103. call: { type: "tool-call", id: "call-question-denied", name: "question", input: questionInput },
  104. }),
  105. ).toEqual({
  106. status: "error",
  107. error: {
  108. type: "permission.rejected",
  109. message: "Permission denied: question",
  110. },
  111. })
  112. expect(capturedInput()).toBeUndefined()
  113. deny = false
  114. }),
  115. )
  116. it.effect("registers question and projects user answers without a permission assertion", () =>
  117. Effect.gen(function* () {
  118. assertions.length = 0
  119. captured = undefined
  120. reject = false
  121. deny = false
  122. const registry = yield* Tool.Service
  123. const questions = [
  124. {
  125. question: "What should happen?",
  126. header: "Action",
  127. options: [{ label: "Build", description: "Build it" }],
  128. },
  129. {
  130. question: "Which environment?",
  131. header: "Environment",
  132. options: [{ label: "Dev", description: "Development" }],
  133. multiple: true,
  134. },
  135. {
  136. question: "Anything else?",
  137. header: "Optional",
  138. options: [],
  139. },
  140. ]
  141. expect((yield* toolDefinitions(registry)).map((definition) => definition.name)).toEqual(["question", "execute"])
  142. expect(
  143. yield* executeTool(registry, {
  144. sessionID,
  145. ...toolIdentity,
  146. call: { type: "tool-call", id: "call-question", name: "question", input: { questions } },
  147. }),
  148. ).toEqual({
  149. status: "completed",
  150. output: { 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. metadata: { answers: [["Build"], ["Dev"], []] },
  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.messageID, 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* Tool.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.messageID, 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* Tool.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. })