1
0

tool-skill.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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(
  85. LayerNode.group([Tool.node, skillToolNode]),
  86. [
  87. [Permission.node, permission],
  88. [Skill.node, skills],
  89. [Image.node, imagePassthrough],
  90. ],
  91. )
  92. return yield* Effect.gen(function* () {
  93. const registry = yield* Tool.Service
  94. expect((yield* toolDefinitions(registry))[0]).toMatchObject({
  95. name: "skill",
  96. description: SkillTool.description,
  97. })
  98. expect(
  99. yield* executeTool(registry, {
  100. sessionID,
  101. ...toolIdentity,
  102. call: { type: "tool-call", id: "call-skill", name: "skill", input: { id: "effect" } },
  103. }),
  104. ).toMatchObject({
  105. status: "completed",
  106. content: [{ type: "text", text: SkillTool.toModelOutput(info, [reference]) }],
  107. })
  108. expect(SkillTool.toModelOutput(info, [reference])).toContain(`Base directory for this skill: ${directory}`)
  109. expect(
  110. yield* executeTool(registry, {
  111. sessionID,
  112. ...toolIdentity,
  113. call: { type: "tool-call", id: "call-skill-overflow", name: "skill", input: { id: "effect" } },
  114. }),
  115. ).toEqual({
  116. status: "completed",
  117. output: { name: "Effect", directory, output: SkillTool.toModelOutput(info, [reference]) },
  118. content: [{ type: "text", text: SkillTool.toModelOutput(info, [reference]) }],
  119. metadata: { name: "Effect", directory },
  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({
  132. status: "error",
  133. error: { type: "tool.execution", message: "Unable to load skill missing" },
  134. })
  135. deny = true
  136. expect(
  137. yield* executeTool(registry, {
  138. sessionID,
  139. ...toolIdentity,
  140. call: { type: "tool-call", id: "call-denied-skill", name: "skill", input: { id: "effect" } },
  141. }),
  142. ).toEqual({
  143. status: "error",
  144. error: { type: "permission.rejected", message: "Permission denied: skill" },
  145. })
  146. deny = false
  147. const flat = Skill.Info.make({
  148. id: Skill.ID.make("public"),
  149. name: Skill.Name.make("Public"),
  150. description: "Public guidance",
  151. location: AbsolutePath.make(path.join(tmp.path, "public.md")),
  152. content: "Public",
  153. })
  154. yield* Effect.promise(() =>
  155. Promise.all([
  156. fs.writeFile(flat.location, "public"),
  157. fs.writeFile(path.join(tmp.path, "secret.md"), "secret"),
  158. ]),
  159. )
  160. current = [flat]
  161. expect(
  162. yield* executeTool(registry, {
  163. sessionID,
  164. ...toolIdentity,
  165. call: { type: "tool-call", id: "call-flat-skill", name: "skill", input: { id: "public" } },
  166. }),
  167. ).toMatchObject({
  168. status: "completed",
  169. content: [{ type: "text", text: SkillTool.toModelOutput(flat, []) }],
  170. })
  171. }).pipe(Effect.provide(skillToolLayer))
  172. }),
  173. ),
  174. ),
  175. )
  176. })