tool-skill.test.ts 6.4 KB

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