agent.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Schema } from "effect"
  3. import { AgentV2 } from "@opencode-ai/core/agent"
  4. import { Config } from "@opencode-ai/core/config"
  5. import { ConfigAgentPlugin } from "@opencode-ai/core/config/plugin/agent"
  6. import { PermissionV2 } from "@opencode-ai/core/permission"
  7. import { testEffect } from "../lib/effect"
  8. const it = testEffect(AgentV2.locationLayer)
  9. const decode = Schema.decodeUnknownSync(Config.Info)
  10. describe("ConfigAgentPlugin.Plugin", () => {
  11. it.effect("applies global permissions between built-in and agent-specific permissions", () =>
  12. Effect.gen(function* () {
  13. const agents = yield* AgentV2.Service
  14. const build = AgentV2.ID.make("build")
  15. const defaults = yield* agents.transform()
  16. yield* defaults((editor) =>
  17. editor.update(build, (agent) => {
  18. agent.mode = "primary"
  19. agent.permissions.push({ action: "bash", resource: "*", effect: "allow" })
  20. }),
  21. )
  22. const config = Config.Service.of({
  23. directories: () => Effect.succeed([]),
  24. get: () =>
  25. Effect.succeed([
  26. new Config.Loaded({
  27. source: { type: "memory" },
  28. info: decode({
  29. permissions: [{ action: "bash", resource: "*", effect: "ask" }],
  30. agents: {
  31. build: {
  32. permissions: [{ action: "bash", resource: "git *", effect: "allow" }],
  33. },
  34. reviewer: {
  35. model: "openrouter/openai/gpt-5",
  36. description: "Review changes",
  37. mode: "subagent",
  38. permissions: [{ action: "edit", resource: "*", effect: "deny" }],
  39. },
  40. removed: { description: "Removed later" },
  41. },
  42. }),
  43. }),
  44. new Config.Loaded({
  45. source: { type: "memory" },
  46. info: decode({
  47. agents: {
  48. reviewer: { variant: "high", hidden: true },
  49. removed: { disabled: true },
  50. },
  51. }),
  52. }),
  53. ]),
  54. })
  55. yield* ConfigAgentPlugin.Plugin.effect.pipe(
  56. Effect.provideService(Config.Service, config),
  57. Effect.provideService(AgentV2.Service, agents),
  58. )
  59. const buildAgent = yield* agents.get(build)
  60. if (!buildAgent) throw new Error("expected configured build agent")
  61. expect(buildAgent.permissions).toEqual([
  62. { action: "bash", resource: "*", effect: "allow" },
  63. { action: "bash", resource: "*", effect: "ask" },
  64. { action: "bash", resource: "git *", effect: "allow" },
  65. ])
  66. expect(PermissionV2.evaluate("bash", "git status", buildAgent.permissions).effect).toBe("allow")
  67. expect(PermissionV2.evaluate("bash", "bun test", buildAgent.permissions).effect).toBe("ask")
  68. const reviewer = yield* agents.get(AgentV2.ID.make("reviewer"))
  69. if (!reviewer) throw new Error("expected configured reviewer agent")
  70. expect(reviewer).toMatchObject({
  71. description: "Review changes",
  72. mode: "subagent",
  73. hidden: true,
  74. model: { providerID: "openrouter", id: "openai/gpt-5", variant: "high" },
  75. })
  76. expect(reviewer.permissions).toEqual([
  77. { action: "bash", resource: "*", effect: "ask" },
  78. { action: "edit", resource: "*", effect: "deny" },
  79. ])
  80. expect(yield* agents.get(AgentV2.ID.make("removed"))).toBeUndefined()
  81. }),
  82. )
  83. it.effect("maps configured agent fields and preserves an unspecified model variant", () =>
  84. Effect.gen(function* () {
  85. const agents = yield* AgentV2.Service
  86. const config = Config.Service.of({
  87. directories: () => Effect.succeed([]),
  88. get: () =>
  89. Effect.succeed([
  90. new Config.Loaded({
  91. source: { type: "memory" },
  92. info: decode({
  93. agents: {
  94. reviewer: {
  95. model: "anthropic/claude-sonnet",
  96. system: "Review carefully.",
  97. description: "Reviews changes",
  98. mode: "subagent",
  99. hidden: true,
  100. color: "warning",
  101. steps: 12,
  102. request: {
  103. headers: { first: "one", shared: "first" },
  104. body: { enabled: true, profile: "review", effort: "medium" },
  105. },
  106. },
  107. },
  108. }),
  109. }),
  110. new Config.Loaded({
  111. source: { type: "memory" },
  112. info: decode({
  113. agents: {
  114. reviewer: {
  115. request: {
  116. headers: { shared: "last", second: "two" },
  117. body: { retries: 2, effort: "high" },
  118. },
  119. },
  120. },
  121. }),
  122. }),
  123. ]),
  124. })
  125. yield* ConfigAgentPlugin.Plugin.effect.pipe(
  126. Effect.provideService(Config.Service, config),
  127. Effect.provideService(AgentV2.Service, agents),
  128. )
  129. const reviewer = yield* agents.get(AgentV2.ID.make("reviewer"))
  130. if (!reviewer) throw new Error("expected configured reviewer agent")
  131. expect(reviewer).toMatchObject({
  132. system: "Review carefully.",
  133. description: "Reviews changes",
  134. mode: "subagent",
  135. hidden: true,
  136. color: "warning",
  137. steps: 12,
  138. model: { providerID: "anthropic", id: "claude-sonnet", variant: undefined },
  139. })
  140. expect(reviewer.request).toEqual({
  141. headers: { first: "one", shared: "last", second: "two" },
  142. body: { enabled: true, profile: "review", retries: 2, effort: "high" },
  143. })
  144. }),
  145. )
  146. it.effect("removes a built-in agent disabled by configuration", () =>
  147. Effect.gen(function* () {
  148. const agents = yield* AgentV2.Service
  149. const build = AgentV2.ID.make("build")
  150. const defaults = yield* agents.transform()
  151. yield* defaults((editor) => editor.update(build, () => {}))
  152. const config = Config.Service.of({
  153. directories: () => Effect.succeed([]),
  154. get: () =>
  155. Effect.succeed([
  156. new Config.Loaded({
  157. source: { type: "memory" },
  158. info: decode({ agents: { build: { disabled: true } } }),
  159. }),
  160. ]),
  161. })
  162. yield* ConfigAgentPlugin.Plugin.effect.pipe(
  163. Effect.provideService(Config.Service, config),
  164. Effect.provideService(AgentV2.Service, agents),
  165. )
  166. expect(yield* agents.get(build)).toBeUndefined()
  167. }),
  168. )
  169. })