tool-skill.test.ts 6.7 KB

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