tool-skill.test.ts 6.2 KB

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