tool-skill.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { describe, expect } from "bun:test"
  4. import { Effect, Layer } from "effect"
  5. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  6. import { LayerNode } from "@opencode-ai/util/effect/layer-node"
  7. import { PermissionV2 } from "@opencode-ai/core/permission"
  8. import { AbsolutePath } from "@opencode-ai/core/schema"
  9. import { SessionV2 } from "@opencode-ai/core/session"
  10. import { SkillV2 } from "@opencode-ai/core/skill"
  11. import { SkillTool } from "@opencode-ai/core/tool/skill"
  12. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  13. import { ToolOutputStore } from "@opencode-ai/core/tool-output-store"
  14. import { tmpdir } from "./fixture/tmpdir"
  15. import { Image } from "@opencode-ai/core/image"
  16. import { it } from "./lib/effect"
  17. import { imagePassthrough } from "./lib/image"
  18. import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
  19. import { FSUtil } from "@opencode-ai/util/fs-util"
  20. import { toolIdentity, executeTool, registerToolPlugin, settleTool, toolDefinitions } from "./lib/tool"
  21. const skillToolNode = makeLocationNode({
  22. name: "test/skill-tool-plugin",
  23. layer: Layer.effectDiscard(registerToolPlugin(SkillTool.Plugin)),
  24. deps: [ToolRegistry.toolsNode, FSUtil.node, SkillV2.node, PermissionV2.node],
  25. })
  26. const sessionID = SessionV2.ID.make("ses_skill_tool_test")
  27. describe("SkillTool", () => {
  28. it.live("lists available skills, authorizes the selected ID, and loads model-facing content", () =>
  29. Effect.acquireRelease(
  30. Effect.promise(() => tmpdir()),
  31. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  32. ).pipe(
  33. Effect.flatMap((tmp) =>
  34. Effect.gen(function* () {
  35. const directory = path.join(tmp.path, "effect")
  36. const location = path.join(directory, "SKILL.md")
  37. const reference = path.join(directory, "reference.md")
  38. yield* Effect.promise(() => fs.mkdir(directory, { recursive: true }))
  39. yield* Effect.promise(() =>
  40. Promise.all([fs.writeFile(location, "unused"), fs.writeFile(reference, "reference")]),
  41. )
  42. const info: SkillV2.Info = {
  43. id: SkillV2.ID.make("effect"),
  44. name: SkillV2.Name.make("Effect"),
  45. description: "Use Effect",
  46. location: AbsolutePath.make(location),
  47. content: "# Effect\n\nGuidance",
  48. }
  49. let current = [info]
  50. const assertions: PermissionV2.AssertInput[] = []
  51. let deny = false
  52. const permission = Layer.succeed(
  53. PermissionV2.Service,
  54. PermissionV2.Service.of({
  55. assert: (input) =>
  56. Effect.sync(() => assertions.push(input)).pipe(
  57. Effect.andThen(
  58. deny
  59. ? Effect.fail(
  60. new PermissionV2.BlockedError({
  61. rules: [],
  62. permission: input.action,
  63. resources: input.resources,
  64. }),
  65. )
  66. : Effect.void,
  67. ),
  68. ),
  69. ask: () => Effect.die("unused"),
  70. reply: () => Effect.die("unused"),
  71. get: () => Effect.die("unused"),
  72. forSession: () => Effect.die("unused"),
  73. list: () => Effect.die("unused"),
  74. }),
  75. )
  76. const skills = Layer.succeed(
  77. SkillV2.Service,
  78. SkillV2.Service.of({
  79. transform: (_transform) => Effect.die("unused"),
  80. reload: () => Effect.die("unused"),
  81. sources: () => Effect.die("unused"),
  82. list: () => Effect.succeed(current),
  83. }),
  84. )
  85. const skillToolLayer = AppNodeBuilder.build(
  86. LayerNode.group([ToolRegistry.node, ToolRegistry.toolsNode, skillToolNode]),
  87. [
  88. [PermissionV2.node, permission],
  89. [SkillV2.node, skills],
  90. [ToolOutputStore.node, ToolOutputStore.nodeWithoutConfig],
  91. [Image.node, imagePassthrough],
  92. ],
  93. )
  94. return yield* Effect.gen(function* () {
  95. const registry = yield* ToolRegistry.Service
  96. expect((yield* toolDefinitions(registry))[0]).toMatchObject({
  97. name: "skill",
  98. description: SkillTool.description,
  99. })
  100. expect(
  101. yield* executeTool(registry, {
  102. sessionID,
  103. ...toolIdentity,
  104. call: { type: "tool-call", id: "call-skill", name: "skill", input: { id: "effect" } },
  105. }),
  106. ).toEqual({
  107. type: "text",
  108. value: SkillTool.toModelOutput(info, [reference]),
  109. })
  110. expect(SkillTool.toModelOutput(info, [reference])).toContain(`Base directory for this skill: ${directory}`)
  111. expect(
  112. yield* settleTool(registry, {
  113. sessionID,
  114. ...toolIdentity,
  115. call: { type: "tool-call", id: "call-skill-overflow", name: "skill", input: { id: "effect" } },
  116. }),
  117. ).toMatchObject({
  118. result: { type: "text", value: SkillTool.toModelOutput(info, [reference]) },
  119. output: { structured: { name: "Effect" } },
  120. })
  121. expect(assertions).toMatchObject([
  122. { sessionID, action: "skill", resources: ["effect"], save: ["effect"] },
  123. { sessionID, action: "skill", resources: ["effect"], save: ["effect"] },
  124. ])
  125. expect(
  126. yield* executeTool(registry, {
  127. sessionID,
  128. ...toolIdentity,
  129. call: { type: "tool-call", id: "call-missing-skill", name: "skill", input: { id: "missing" } },
  130. }),
  131. ).toEqual({ type: "error", value: "Unable to load skill missing" })
  132. deny = true
  133. expect(
  134. yield* executeTool(registry, {
  135. sessionID,
  136. ...toolIdentity,
  137. call: { type: "tool-call", id: "call-denied-skill", name: "skill", input: { id: "effect" } },
  138. }),
  139. ).toEqual({ type: "error", value: "Unable to load skill effect" })
  140. deny = false
  141. const flat = SkillV2.Info.make({
  142. id: SkillV2.ID.make("public"),
  143. name: SkillV2.Name.make("Public"),
  144. description: "Public guidance",
  145. location: AbsolutePath.make(path.join(tmp.path, "public.md")),
  146. content: "Public",
  147. })
  148. yield* Effect.promise(() =>
  149. Promise.all([
  150. fs.writeFile(flat.location, "public"),
  151. fs.writeFile(path.join(tmp.path, "secret.md"), "secret"),
  152. ]),
  153. )
  154. current = [flat]
  155. expect(
  156. yield* executeTool(registry, {
  157. sessionID,
  158. ...toolIdentity,
  159. call: { type: "tool-call", id: "call-flat-skill", name: "skill", input: { id: "public" } },
  160. }),
  161. ).toEqual({ type: "text", value: SkillTool.toModelOutput(flat, []) })
  162. }).pipe(Effect.provide(skillToolLayer))
  163. }),
  164. ),
  165. ),
  166. )
  167. })