tool-skill.test.ts 6.3 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 { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  6. import { LayerNode } from "@opencode-ai/core/effect/layer-node"
  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 { ToolOutputStore } from "@opencode-ai/core/tool-output-store"
  14. import { tmpdir } from "./fixture/tmpdir"
  15. import { it } from "./lib/effect"
  16. import { toolIdentity, executeTool, settleTool, toolDefinitions } from "./lib/tool"
  17. const sessionID = SessionV2.ID.make("ses_skill_tool_test")
  18. describe("SkillTool", () => {
  19. it.live("lists available skills, authorizes the selected name, and loads model-facing content", () =>
  20. Effect.acquireRelease(
  21. Effect.promise(() => tmpdir()),
  22. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  23. ).pipe(
  24. Effect.flatMap((tmp) =>
  25. Effect.gen(function* () {
  26. const directory = path.join(tmp.path, "effect")
  27. const location = path.join(directory, "SKILL.md")
  28. const reference = path.join(directory, "reference.md")
  29. yield* Effect.promise(() => fs.mkdir(directory, { recursive: true }))
  30. yield* Effect.promise(() =>
  31. Promise.all([fs.writeFile(location, "unused"), fs.writeFile(reference, "reference")]),
  32. )
  33. const info: SkillV2.Info = {
  34. name: "effect",
  35. description: "Use Effect",
  36. location: AbsolutePath.make(location),
  37. content: "# Effect\n\nGuidance",
  38. }
  39. let current = [info]
  40. const assertions: PermissionV2.AssertInput[] = []
  41. let deny = false
  42. const permission = Layer.succeed(
  43. PermissionV2.Service,
  44. PermissionV2.Service.of({
  45. assert: (input) =>
  46. Effect.sync(() => assertions.push(input)).pipe(
  47. Effect.andThen(deny ? Effect.fail(new PermissionV2.BlockedError({ rules: [] })) : Effect.void),
  48. ),
  49. ask: () => Effect.die("unused"),
  50. reply: () => Effect.die("unused"),
  51. get: () => Effect.die("unused"),
  52. forSession: () => Effect.die("unused"),
  53. list: () => Effect.die("unused"),
  54. }),
  55. )
  56. const skills = Layer.succeed(
  57. SkillV2.Service,
  58. SkillV2.Service.of({
  59. transform: (_transform) => Effect.die("unused"),
  60. reload: () => Effect.die("unused"),
  61. sources: () => Effect.die("unused"),
  62. list: () => Effect.succeed(current),
  63. }),
  64. )
  65. const skillToolLayer = AppNodeBuilder.build(
  66. LayerNode.group([ToolRegistry.node, ToolRegistry.toolsNode, SkillTool.node]),
  67. [
  68. [PermissionV2.node, permission],
  69. [SkillV2.node, skills],
  70. [ToolOutputStore.node, ToolOutputStore.nodeWithoutConfig],
  71. ],
  72. )
  73. return yield* Effect.gen(function* () {
  74. const registry = yield* ToolRegistry.Service
  75. expect((yield* toolDefinitions(registry))[0]).toMatchObject({
  76. name: "skill",
  77. description: SkillTool.description,
  78. })
  79. expect(
  80. yield* executeTool(registry, {
  81. sessionID,
  82. ...toolIdentity,
  83. call: { type: "tool-call", id: "call-skill", name: "skill", input: { name: "effect" } },
  84. }),
  85. ).toEqual({
  86. type: "text",
  87. value: SkillTool.toModelOutput(info, [reference]),
  88. })
  89. expect(SkillTool.toModelOutput(info, [reference])).toContain(`Base directory for this skill: ${directory}`)
  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(skillToolLayer))
  141. }),
  142. ),
  143. ),
  144. )
  145. })