tool-skill.test.ts 7.0 KB

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