tool-skill.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. wait: () =>
  47. Effect.sync(() => {
  48. bootWaited = true
  49. }),
  50. }),
  51. )
  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(deny ? Effect.fail(new PermissionV2.DeniedError({ rules: [] })) : Effect.void),
  58. ),
  59. ask: () => Effect.die("unused"),
  60. reply: () => Effect.die("unused"),
  61. get: () => Effect.die("unused"),
  62. forSession: () => Effect.die("unused"),
  63. list: () => Effect.die("unused"),
  64. }),
  65. )
  66. const skills = Layer.succeed(
  67. SkillV2.Service,
  68. SkillV2.Service.of({
  69. transform: () => Effect.die("unused"),
  70. sources: () => Effect.die("unused"),
  71. list: () => Effect.succeed(current),
  72. }),
  73. )
  74. const registry = ToolRegistry.defaultLayer.pipe(Layer.provide(permission))
  75. const tool = SkillTool.layer.pipe(
  76. Layer.provide(registry),
  77. Layer.provide(permission),
  78. Layer.provide(FSUtil.defaultLayer),
  79. Layer.provide(boot),
  80. Layer.provide(skills),
  81. )
  82. const layer = Layer.mergeAll(permission, skills, registry, boot, tool)
  83. return yield* Effect.gen(function* () {
  84. const registry = yield* ToolRegistry.Service
  85. expect(bootWaited).toBe(true)
  86. expect((yield* toolDefinitions(registry))[0]).toMatchObject({
  87. name: "skill",
  88. description: SkillTool.description,
  89. })
  90. expect(
  91. yield* executeTool(registry, {
  92. sessionID,
  93. ...toolIdentity,
  94. call: { type: "tool-call", id: "call-skill", name: "skill", input: { name: "effect" } },
  95. }),
  96. ).toEqual({
  97. type: "text",
  98. value: SkillTool.toModelOutput(info, [reference]),
  99. })
  100. expect(SkillTool.toModelOutput(info, [reference])).toContain(
  101. `Base directory for this skill: ${pathToFileURL(directory).href}`,
  102. )
  103. expect(
  104. yield* settleTool(registry, {
  105. sessionID,
  106. ...toolIdentity,
  107. call: { type: "tool-call", id: "call-skill-overflow", name: "skill", input: { name: "effect" } },
  108. }),
  109. ).toMatchObject({
  110. result: { type: "text", value: SkillTool.toModelOutput(info, [reference]) },
  111. output: { structured: { name: "effect" } },
  112. })
  113. expect(assertions).toMatchObject([
  114. { sessionID, action: "skill", resources: ["effect"], save: ["effect"] },
  115. { sessionID, action: "skill", resources: ["effect"], save: ["effect"] },
  116. ])
  117. expect(
  118. yield* executeTool(registry, {
  119. sessionID,
  120. ...toolIdentity,
  121. call: { type: "tool-call", id: "call-missing-skill", name: "skill", input: { name: "missing" } },
  122. }),
  123. ).toEqual({ type: "error", value: "Unable to load skill missing" })
  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: { name: "effect" } },
  130. }),
  131. ).toEqual({ type: "error", value: "Unable to load skill effect" })
  132. deny = false
  133. const flat = new SkillV2.Info({
  134. name: "public",
  135. description: "Public guidance",
  136. location: AbsolutePath.make(path.join(tmp.path, "public.md")),
  137. content: "Public",
  138. })
  139. yield* Effect.promise(() =>
  140. Promise.all([
  141. fs.writeFile(flat.location, "public"),
  142. fs.writeFile(path.join(tmp.path, "secret.md"), "secret"),
  143. ]),
  144. )
  145. current = [flat]
  146. expect(
  147. yield* executeTool(registry, {
  148. sessionID,
  149. ...toolIdentity,
  150. call: { type: "tool-call", id: "call-flat-skill", name: "skill", input: { name: "public" } },
  151. }),
  152. ).toEqual({ type: "text", value: SkillTool.toModelOutput(flat, []) })
  153. }).pipe(Effect.provide(layer))
  154. }),
  155. ),
  156. ),
  157. )
  158. })