tool-skill.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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(`Base directory for this skill: ${directory}`)
  88. expect(
  89. yield* settleTool(registry, {
  90. sessionID,
  91. ...toolIdentity,
  92. call: { type: "tool-call", id: "call-skill-overflow", name: "skill", input: { name: "effect" } },
  93. }),
  94. ).toMatchObject({
  95. result: { type: "text", value: SkillTool.toModelOutput(info, [reference]) },
  96. output: { structured: { name: "effect" } },
  97. })
  98. expect(assertions).toMatchObject([
  99. { sessionID, action: "skill", resources: ["effect"], save: ["effect"] },
  100. { sessionID, action: "skill", resources: ["effect"], save: ["effect"] },
  101. ])
  102. expect(
  103. yield* executeTool(registry, {
  104. sessionID,
  105. ...toolIdentity,
  106. call: { type: "tool-call", id: "call-missing-skill", name: "skill", input: { name: "missing" } },
  107. }),
  108. ).toEqual({ type: "error", value: "Unable to load skill missing" })
  109. deny = true
  110. expect(
  111. yield* executeTool(registry, {
  112. sessionID,
  113. ...toolIdentity,
  114. call: { type: "tool-call", id: "call-denied-skill", name: "skill", input: { name: "effect" } },
  115. }),
  116. ).toEqual({ type: "error", value: "Unable to load skill effect" })
  117. deny = false
  118. const flat = SkillV2.Info.make({
  119. name: "public",
  120. description: "Public guidance",
  121. location: AbsolutePath.make(path.join(tmp.path, "public.md")),
  122. content: "Public",
  123. })
  124. yield* Effect.promise(() =>
  125. Promise.all([
  126. fs.writeFile(flat.location, "public"),
  127. fs.writeFile(path.join(tmp.path, "secret.md"), "secret"),
  128. ]),
  129. )
  130. current = [flat]
  131. expect(
  132. yield* executeTool(registry, {
  133. sessionID,
  134. ...toolIdentity,
  135. call: { type: "tool-call", id: "call-flat-skill", name: "skill", input: { name: "public" } },
  136. }),
  137. ).toEqual({ type: "text", value: SkillTool.toModelOutput(flat, []) })
  138. }).pipe(Effect.provide(layer))
  139. }),
  140. ),
  141. ),
  142. )
  143. })