1
0

tool-skill.test.ts 7.3 KB

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