tool-skill.test.ts 7.5 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 { 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 { ToolOutputStore } from "@opencode-ai/core/tool-output-store"
  13. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  14. import { tmpdir } from "./fixture/tmpdir"
  15. import { it } from "./lib/effect"
  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. const truncations: ToolOutputStore.TruncateInput[] = []
  42. let truncate = (input: ToolOutputStore.TruncateInput): Effect.Effect<ToolOutputStore.TruncateResult> =>
  43. Effect.succeed({ content: input.content, truncated: false })
  44. let bootWaited = false
  45. const boot = Layer.succeed(
  46. PluginBoot.Service,
  47. PluginBoot.Service.of({
  48. wait: () =>
  49. Effect.sync(() => {
  50. bootWaited = true
  51. }),
  52. }),
  53. )
  54. const permission = Layer.succeed(
  55. PermissionV2.Service,
  56. PermissionV2.Service.of({
  57. assert: (input) =>
  58. Effect.sync(() => assertions.push(input)).pipe(
  59. Effect.andThen(deny ? Effect.fail(new PermissionV2.DeniedError({ rules: [] })) : Effect.void),
  60. ),
  61. ask: () => Effect.die("unused"),
  62. reply: () => Effect.die("unused"),
  63. get: () => Effect.die("unused"),
  64. forSession: () => Effect.die("unused"),
  65. list: () => Effect.die("unused"),
  66. }),
  67. )
  68. const skills = Layer.succeed(
  69. SkillV2.Service,
  70. SkillV2.Service.of({
  71. transform: () => 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 resources = Layer.succeed(
  78. ToolOutputStore.Service,
  79. ToolOutputStore.Service.of({
  80. limits: () => Effect.die("unused"),
  81. write: () => Effect.die("unused"),
  82. truncate: (input) => Effect.sync(() => truncations.push(input)).pipe(Effect.andThen(truncate(input))),
  83. bound: (input) => Effect.succeed({ output: input.output, outputPaths: [] }),
  84. cleanup: () => Effect.die("unused"),
  85. }),
  86. )
  87. const tool = SkillTool.layer.pipe(
  88. Layer.provide(registry),
  89. Layer.provide(FSUtil.defaultLayer),
  90. Layer.provide(boot),
  91. Layer.provide(skills),
  92. Layer.provide(resources),
  93. )
  94. const layer = Layer.mergeAll(permission, skills, registry, boot, resources, tool)
  95. return yield* Effect.gen(function* () {
  96. const registry = yield* ToolRegistry.Service
  97. expect(bootWaited).toBe(true)
  98. expect((yield* registry.definitions())[0]).toMatchObject({
  99. name: "skill",
  100. description: SkillTool.description,
  101. })
  102. expect(
  103. yield* registry.execute({
  104. sessionID,
  105. call: { type: "tool-call", id: "call-skill", name: "skill", input: { name: "effect" } },
  106. }),
  107. ).toEqual({
  108. type: "text",
  109. value: SkillTool.toModelOutput(info, [reference]),
  110. })
  111. expect(truncations).toEqual([
  112. { sessionID, toolCallID: "call-skill", content: SkillTool.toModelOutput(info, [reference]) },
  113. ])
  114. truncate = (input) =>
  115. Effect.succeed({
  116. content: "HEAD\n\n... output truncated; full content saved to /tmp/tool-output/tool_opaque ...\n\nTAIL",
  117. truncated: true,
  118. outputPath: "/tmp/tool-output/tool_opaque",
  119. })
  120. expect(
  121. yield* registry.settle({
  122. sessionID,
  123. call: { type: "tool-call", id: "call-skill-overflow", name: "skill", input: { name: "effect" } },
  124. }),
  125. ).toMatchObject({
  126. result: { type: "text", value: expect.stringContaining("/tmp/tool-output/tool_opaque") },
  127. output: {
  128. structured: { truncated: true, outputPath: "/tmp/tool-output/tool_opaque" },
  129. },
  130. })
  131. expect(assertions).toEqual([
  132. { sessionID, action: "skill", resources: ["effect"], save: ["effect"] },
  133. { sessionID, action: "skill", resources: ["effect"], save: ["effect"] },
  134. ])
  135. expect(
  136. yield* registry.execute({
  137. sessionID,
  138. call: { type: "tool-call", id: "call-missing-skill", name: "skill", input: { name: "missing" } },
  139. }),
  140. ).toEqual({ type: "error", value: "Unable to load skill missing" })
  141. deny = true
  142. expect(
  143. yield* registry.execute({
  144. sessionID,
  145. call: { type: "tool-call", id: "call-denied-skill", name: "skill", input: { name: "effect" } },
  146. }),
  147. ).toEqual({ type: "error", value: "Unable to load skill effect" })
  148. deny = false
  149. const flat = new SkillV2.Info({
  150. name: "public",
  151. description: "Public guidance",
  152. location: AbsolutePath.make(path.join(tmp.path, "public.md")),
  153. content: "Public",
  154. })
  155. yield* Effect.promise(() =>
  156. Promise.all([
  157. fs.writeFile(flat.location, "public"),
  158. fs.writeFile(path.join(tmp.path, "secret.md"), "secret"),
  159. ]),
  160. )
  161. current = [flat]
  162. truncate = (input) => Effect.succeed({ content: input.content, truncated: false })
  163. expect(
  164. yield* registry.execute({
  165. sessionID,
  166. call: { type: "tool-call", id: "call-flat-skill", name: "skill", input: { name: "public" } },
  167. }),
  168. ).toEqual({ type: "text", value: SkillTool.toModelOutput(flat, []) })
  169. }).pipe(Effect.provide(layer))
  170. }),
  171. ),
  172. ),
  173. )
  174. })