tool-skill.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 { Permission } from "@opencode-ai/core/permission"
  8. import { AbsolutePath } from "@opencode-ai/core/schema"
  9. import { Session } from "@opencode-ai/core/session"
  10. import { Skill } from "@opencode-ai/core/skill"
  11. import { SkillTool } from "@opencode-ai/core/tool/plugin/skill"
  12. import { Tool } from "@opencode-ai/core/tool"
  13. import { tmpdir } from "./fixture/tmpdir"
  14. import { Image } from "@opencode-ai/core/image"
  15. import { it } from "./lib/effect"
  16. import { imagePassthrough } from "./lib/image"
  17. import { permissionLayer } from "./lib/permission"
  18. import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
  19. import { FSUtil } from "@opencode-ai/util/fs-util"
  20. import { toolIdentity, executeTool, registerToolPlugin, toolDefinitions } from "./lib/tool"
  21. const skillToolNode = makeLocationNode({
  22. name: "test/skill-tool-plugin",
  23. layer: Layer.effectDiscard(registerToolPlugin(SkillTool.Plugin)),
  24. deps: [Tool.node, FSUtil.node, Skill.node, Permission.node],
  25. })
  26. const sessionID = Session.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: Skill.Info = {
  43. id: Skill.ID.make("effect"),
  44. name: Skill.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: Permission.AssertInput[] = []
  51. let deny = false
  52. const permission = permissionLayer({
  53. assert: (input) =>
  54. Effect.sync(() => assertions.push(input)).pipe(
  55. Effect.andThen(
  56. deny
  57. ? Effect.fail(
  58. new Permission.BlockedError({
  59. rules: [],
  60. permission: input.action,
  61. resources: input.resources,
  62. }),
  63. )
  64. : Effect.void,
  65. ),
  66. ),
  67. })
  68. const skills = Layer.succeed(
  69. Skill.Service,
  70. Skill.Service.of({
  71. transform: (_transform) => Effect.die("unused"),
  72. reload: () => Effect.die("unused"),
  73. list: () => Effect.succeed(current),
  74. }),
  75. )
  76. const skillToolLayer = AppNodeBuilder.build(LayerNode.group([Tool.node, skillToolNode]), [
  77. [Permission.node, permission],
  78. [Skill.node, skills],
  79. [Image.node, imagePassthrough],
  80. ])
  81. return yield* Effect.gen(function* () {
  82. const registry = yield* Tool.Service
  83. expect((yield* toolDefinitions(registry))[0]).toMatchObject({
  84. name: "skill",
  85. description: SkillTool.description,
  86. })
  87. expect(
  88. yield* executeTool(registry, {
  89. sessionID,
  90. ...toolIdentity,
  91. call: { type: "tool-call", id: "call-skill", name: "skill", input: { id: "effect" } },
  92. }),
  93. ).toMatchObject({
  94. status: "completed",
  95. content: [{ type: "text", text: Skill.toModelOutput(info, [reference]) }],
  96. })
  97. expect(Skill.toModelOutput(info, [reference])).toContain(`Base directory for this skill: ${directory}`)
  98. expect(
  99. yield* executeTool(registry, {
  100. sessionID,
  101. ...toolIdentity,
  102. call: { type: "tool-call", id: "call-skill-overflow", name: "skill", input: { id: "effect" } },
  103. }),
  104. ).toEqual({
  105. status: "completed",
  106. output: { name: "Effect", directory, output: Skill.toModelOutput(info, [reference]) },
  107. content: [{ type: "text", text: Skill.toModelOutput(info, [reference]) }],
  108. metadata: { name: "Effect", directory },
  109. })
  110. expect(assertions).toMatchObject([
  111. { sessionID, action: "skill", resources: ["effect"], save: ["effect"] },
  112. { sessionID, action: "skill", resources: ["effect"], save: ["effect"] },
  113. ])
  114. expect(
  115. yield* executeTool(registry, {
  116. sessionID,
  117. ...toolIdentity,
  118. call: { type: "tool-call", id: "call-missing-skill", name: "skill", input: { id: "missing" } },
  119. }),
  120. ).toEqual({
  121. status: "error",
  122. error: { type: "tool.execution", message: "Unable to load skill missing" },
  123. })
  124. deny = true
  125. expect(
  126. yield* executeTool(registry, {
  127. sessionID,
  128. ...toolIdentity,
  129. call: { type: "tool-call", id: "call-denied-skill", name: "skill", input: { id: "effect" } },
  130. }),
  131. ).toEqual({
  132. status: "error",
  133. error: { type: "permission.rejected", message: "Permission denied: skill" },
  134. })
  135. deny = false
  136. const flat = Skill.Info.make({
  137. id: Skill.ID.make("public"),
  138. name: Skill.Name.make("Public"),
  139. description: "Public guidance",
  140. location: AbsolutePath.make(path.join(tmp.path, "public.md")),
  141. content: "Public",
  142. })
  143. yield* Effect.promise(() =>
  144. Promise.all([
  145. fs.writeFile(flat.location, "public"),
  146. fs.writeFile(path.join(tmp.path, "secret.md"), "secret"),
  147. ]),
  148. )
  149. current = [flat]
  150. expect(
  151. yield* executeTool(registry, {
  152. sessionID,
  153. ...toolIdentity,
  154. call: { type: "tool-call", id: "call-flat-skill", name: "skill", input: { id: "public" } },
  155. }),
  156. ).toMatchObject({
  157. status: "completed",
  158. content: [{ type: "text", text: Skill.toModelOutput(flat, []) }],
  159. })
  160. }).pipe(Effect.provide(skillToolLayer))
  161. }),
  162. ),
  163. ),
  164. )
  165. })