tool-skill.test.ts 6.2 KB

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