plugin.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import { describe, expect } from "bun:test"
  2. import { ToolFailure } from "@opencode-ai/ai"
  3. import { Context, Effect, Exit, Fiber, Schema, Stream } from "effect"
  4. import { Plugin as EffectPlugin } from "@opencode-ai/plugin/effect"
  5. import { Config as ConfigSchema } from "@opencode-ai/schema/config"
  6. import { Agent } from "@opencode-ai/core/agent"
  7. import { Bus } from "@opencode-ai/core/bus"
  8. import { Plugin } from "@opencode-ai/core/plugin"
  9. import { PluginHost } from "@opencode-ai/core/plugin/host"
  10. import { Session } from "@opencode-ai/core/session"
  11. import { SessionMessage } from "@opencode-ai/core/session/message"
  12. import { Tool } from "@opencode-ai/core/tool"
  13. import { testEffect } from "./lib/effect"
  14. import { PluginTestLayer } from "./plugin/fixture"
  15. const it = testEffect(PluginTestLayer)
  16. class Secret extends Context.Service<Secret, string>()("@opencode/test/PluginSecret") {}
  17. const versioned = <R>(plugin: EffectPlugin.Plugin<R>, version = "1") => ({ ...plugin, version })
  18. describe("Plugin", () => {
  19. it.live("selects one public event type through the plugin context", () =>
  20. Effect.gen(function* () {
  21. const plugins = yield* Plugin.Service
  22. const bus = yield* Bus.Service
  23. const host = yield* PluginHost.make(plugins)
  24. const received = yield* host.event
  25. .subscribe("config.updated")
  26. .pipe(Stream.runHead, Effect.forkScoped({ startImmediately: true }))
  27. yield* Effect.sleep("10 millis")
  28. yield* bus.publish(Plugin.Event.Updated, {})
  29. yield* bus.publish(ConfigSchema.Event.Updated, {})
  30. expect((yield* Fiber.join(received)).valueOrUndefined?.type).toBe("config.updated")
  31. }),
  32. )
  33. it.live("exposes all public events through a wildcard plugin subscription", () =>
  34. Effect.gen(function* () {
  35. const plugins = yield* Plugin.Service
  36. const bus = yield* Bus.Service
  37. const host = yield* PluginHost.make(plugins)
  38. const received = yield* host.event
  39. .subscribe()
  40. .pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped({ startImmediately: true }))
  41. yield* Effect.sleep("10 millis")
  42. yield* bus.publish(Plugin.Event.Updated, {})
  43. yield* bus.publish(ConfigSchema.Event.Updated, {})
  44. expect(Array.from(yield* Fiber.join(received), (event) => event.type)).toEqual([
  45. "plugin.updated",
  46. "config.updated",
  47. ])
  48. }),
  49. )
  50. it.effect("rejects unknown runtime plugin event types", () =>
  51. Effect.gen(function* () {
  52. const plugins = yield* Plugin.Service
  53. const host = yield* PluginHost.make(plugins)
  54. const subscribe = host.event.subscribe as unknown as (type: string) => Stream.Stream<never, Error>
  55. const failure = yield* subscribe("unknown.event").pipe(Stream.runDrain, Effect.flip)
  56. expect(failure.message).toBe("Unknown plugin event type: unknown.event")
  57. }),
  58. )
  59. it.effect("replaces plugins by ID and version", () =>
  60. Effect.gen(function* () {
  61. const plugins = yield* Plugin.Service
  62. const agents = yield* Agent.Service
  63. const bus = yield* Bus.Service
  64. let description = "first"
  65. let updates = 0
  66. const unsubscribe = yield* bus.listen((event) =>
  67. Effect.sync(() => {
  68. if (event.type === Plugin.Event.Updated.type) updates++
  69. }),
  70. )
  71. const managed = () =>
  72. EffectPlugin.define({
  73. id: "managed",
  74. effect: (ctx) =>
  75. ctx.agent
  76. .transform((agents) =>
  77. agents.update("configured", (agent) => {
  78. agent.description = description
  79. }),
  80. )
  81. .pipe(Effect.asVoid),
  82. })
  83. yield* plugins.activate([versioned(managed(), "1")])
  84. expect((yield* agents.get(Agent.ID.make("configured")))?.description).toBe("first")
  85. description = "second"
  86. yield* plugins.activate([versioned(managed(), "2")])
  87. expect((yield* agents.get(Agent.ID.make("configured")))?.description).toBe("second")
  88. description = "third"
  89. yield* plugins.activate([versioned(managed(), "2")])
  90. expect(updates).toBe(2)
  91. expect((yield* agents.get(Agent.ID.make("configured")))?.description).toBe("second")
  92. yield* plugins.activate([])
  93. expect(yield* agents.get(Agent.ID.make("configured"))).toBeUndefined()
  94. expect(updates).toBe(3)
  95. yield* unsubscribe
  96. }),
  97. )
  98. it.effect("rejects duplicate IDs before replacing active plugins", () =>
  99. Effect.gen(function* () {
  100. const plugins = yield* Plugin.Service
  101. const active = Plugin.ID.make("active")
  102. const duplicate = "duplicate"
  103. yield* plugins.activate([{ id: active, version: "1", effect: () => Effect.void }])
  104. const result = yield* plugins
  105. .activate([
  106. { id: duplicate, version: "1", effect: () => Effect.void },
  107. { id: duplicate, version: "1", effect: () => Effect.void },
  108. ])
  109. .pipe(Effect.exit)
  110. expect(Exit.isFailure(result)).toBe(true)
  111. expect(yield* plugins.list()).toEqual([{ id: active }])
  112. }),
  113. )
  114. it.effect("skips failed plugins and loads the rest", () =>
  115. Effect.gen(function* () {
  116. const plugins = yield* Plugin.Service
  117. const agents = yield* Agent.Service
  118. let fail = true
  119. const good = EffectPlugin.define({
  120. id: "good",
  121. effect: (ctx) =>
  122. ctx.agent
  123. .transform((agents) =>
  124. agents.update("configured", (agent) => {
  125. agent.description = "loaded"
  126. }),
  127. )
  128. .pipe(Effect.asVoid),
  129. })
  130. const bad = EffectPlugin.define({
  131. id: "bad",
  132. effect: () => {
  133. if (fail) return Effect.die(new Error("materialization failed"))
  134. return Effect.void
  135. },
  136. })
  137. yield* plugins.activate([versioned(good), versioned(bad)])
  138. expect(yield* plugins.list()).toEqual([{ id: Plugin.ID.make("good") }])
  139. expect((yield* agents.get(Agent.ID.make("configured")))?.description).toBe("loaded")
  140. fail = false
  141. yield* plugins.activate([versioned(good), versioned(bad, "2")])
  142. expect(yield* plugins.list()).toEqual([{ id: Plugin.ID.make("good") }, { id: Plugin.ID.make("bad") }])
  143. }),
  144. )
  145. it.effect("restores the previous plugin when its replacement fails", () =>
  146. Effect.gen(function* () {
  147. const plugins = yield* Plugin.Service
  148. const agents = yield* Agent.Service
  149. const previous = EffectPlugin.define({
  150. id: "managed",
  151. effect: (ctx) =>
  152. ctx.agent
  153. .transform((agents) =>
  154. agents.update("configured", (agent) => {
  155. agent.description = "previous"
  156. }),
  157. )
  158. .pipe(Effect.asVoid),
  159. })
  160. const replacement = EffectPlugin.define({
  161. id: "managed",
  162. effect: (ctx) =>
  163. Effect.gen(function* () {
  164. yield* ctx.agent.transform((agents) =>
  165. agents.update("configured", (agent) => {
  166. agent.description = "replacement"
  167. }),
  168. )
  169. return yield* Effect.die(new Error("replacement failed"))
  170. }),
  171. })
  172. yield* plugins.activate([versioned(previous)])
  173. yield* plugins.activate([versioned(replacement, "2")])
  174. expect(yield* plugins.list()).toEqual([{ id: Plugin.ID.make("managed") }])
  175. expect((yield* agents.get(Agent.ID.make("configured")))?.description).toBe("previous")
  176. }),
  177. )
  178. it.effect("deactivates a plugin when replacement and restoration fail", () =>
  179. Effect.gen(function* () {
  180. const plugins = yield* Plugin.Service
  181. const agents = yield* Agent.Service
  182. let loads = 0
  183. const previous = EffectPlugin.define({
  184. id: "managed",
  185. effect: (ctx) => {
  186. loads++
  187. if (loads > 1) return Effect.die(new Error("restoration failed"))
  188. return ctx.agent
  189. .transform((agents) =>
  190. agents.update("configured", (agent) => {
  191. agent.description = "previous"
  192. }),
  193. )
  194. .pipe(Effect.asVoid)
  195. },
  196. })
  197. const replacement = EffectPlugin.define({
  198. id: "managed",
  199. effect: () => Effect.die(new Error("replacement failed")),
  200. })
  201. yield* plugins.activate([versioned(previous)])
  202. yield* plugins.activate([versioned(replacement, "2")])
  203. expect(yield* plugins.list()).toEqual([])
  204. expect(yield* agents.get(Agent.ID.make("configured"))).toBeUndefined()
  205. }),
  206. )
  207. it.effect("closes the previous generation in reverse order", () =>
  208. Effect.gen(function* () {
  209. const plugins = yield* Plugin.Service
  210. const closed: string[] = []
  211. yield* plugins.activate(
  212. ["first", "second"].map((id) => ({
  213. id,
  214. version: "1",
  215. effect: () => Effect.addFinalizer(() => Effect.sync(() => closed.push(id))),
  216. })),
  217. )
  218. yield* plugins.activate([])
  219. expect(closed).toEqual(["second", "first"])
  220. }),
  221. )
  222. it.effect("isolates plugins from ambient services", () =>
  223. Effect.gen(function* () {
  224. const plugins = yield* Plugin.Service
  225. let visible = true
  226. const plugin = EffectPlugin.define({
  227. id: "isolated",
  228. effect: () =>
  229. Effect.serviceOption(Secret).pipe(
  230. Effect.tap((secret) => Effect.sync(() => (visible = secret._tag === "Some"))),
  231. Effect.asVoid,
  232. ),
  233. })
  234. yield* plugins.activate([versioned(plugin)]).pipe(Effect.provideService(Secret, "secret"))
  235. expect(visible).toBe(false)
  236. }),
  237. )
  238. it.effect("registers location tools through the plugin context", () =>
  239. Effect.gen(function* () {
  240. const plugins = yield* Plugin.Service
  241. const registry = yield* Tool.Service
  242. const plugin = EffectPlugin.define({
  243. id: "tool-plugin",
  244. effect: (ctx) =>
  245. ctx.tool
  246. .transform((draft) =>
  247. draft.add({
  248. name: "plugin_tool",
  249. options: { codemode: false },
  250. description: "Plugin tool",
  251. input: Schema.Struct({}),
  252. output: Schema.Struct({ ok: Schema.Boolean }),
  253. execute: () => Effect.succeed({ output: { ok: true } }),
  254. }),
  255. )
  256. .pipe(Effect.orDie),
  257. })
  258. yield* plugins.activate([versioned(plugin)])
  259. expect((yield* registry.snapshot()).definitions.map((tool) => tool.name)).toContain("plugin_tool")
  260. yield* plugins.activate([])
  261. expect((yield* registry.snapshot()).definitions.map((tool) => tool.name)).not.toContain("plugin_tool")
  262. }),
  263. )
  264. it.effect("namespaces tool names and routes codemode registrations through execute", () =>
  265. Effect.gen(function* () {
  266. const plugins = yield* Plugin.Service
  267. const registry = yield* Tool.Service
  268. const tool = (name: string, description: string, options?: Tool.Options) => ({
  269. name,
  270. options,
  271. description,
  272. input: Schema.Struct({}),
  273. output: Schema.Struct({ ok: Schema.Boolean }),
  274. execute: () => Effect.succeed({ output: { ok: true } }),
  275. })
  276. const plugin = EffectPlugin.define({
  277. id: "grouped-tools",
  278. effect: (ctx) =>
  279. ctx.tool
  280. .transform((draft) => {
  281. draft.add(tool("plain", "Plain", { codemode: false }))
  282. draft.add(tool("look/up", "Lookup", { namespace: "context7", codemode: false }))
  283. draft.add(tool("search", "Search", { namespace: "context7" }))
  284. })
  285. .pipe(Effect.orDie),
  286. })
  287. yield* plugins.activate([versioned(plugin)])
  288. expect((yield* registry.snapshot()).definitions.map((tool) => tool.name)).toEqual([
  289. "context7_look_up",
  290. "plain",
  291. "execute",
  292. ])
  293. }),
  294. )
  295. it.effect("fires before/after tool hooks with mutable events around execution", () =>
  296. Effect.gen(function* () {
  297. const plugins = yield* Plugin.Service
  298. const registry = yield* Tool.Service
  299. const executed: unknown[] = []
  300. const seen: {
  301. before?: unknown
  302. after?: { input: unknown; status: string; content: unknown; metadata: unknown }
  303. } = {}
  304. const plugin = EffectPlugin.define({
  305. id: "tool-hooks",
  306. effect: (ctx) =>
  307. Effect.gen(function* () {
  308. yield* ctx.tool
  309. .transform((draft) =>
  310. draft.add({
  311. name: "echo",
  312. options: { codemode: false },
  313. description: "Echo",
  314. input: Schema.Struct({ text: Schema.String }),
  315. output: Schema.Struct({ text: Schema.String }),
  316. execute: ({ text }) =>
  317. Effect.sync(() => executed.push({ text })).pipe(Effect.as({ output: { text } })),
  318. }),
  319. )
  320. .pipe(Effect.orDie)
  321. yield* ctx.tool
  322. .hook("execute.before", (event) =>
  323. Effect.sync(() => {
  324. seen.before = event.input
  325. event.input = { text: "before-mutated" }
  326. }),
  327. )
  328. .pipe(Effect.asVoid)
  329. yield* ctx.tool
  330. .hook("execute.after", (event) =>
  331. Effect.sync(() => {
  332. seen.after = {
  333. input: event.input,
  334. status: event.status,
  335. content: event.status === "completed" ? event.result.content : undefined,
  336. metadata: event.status === "completed" ? event.result.metadata : event.error.metadata,
  337. }
  338. if (event.status !== "completed") return
  339. event.result = {
  340. ...event.result,
  341. content: [{ type: "text", text: "after-mutated" }],
  342. metadata: { rewritten: true },
  343. }
  344. }),
  345. )
  346. .pipe(Effect.asVoid)
  347. yield* ctx.tool
  348. .hook("execute.after", (event) =>
  349. Effect.sync(() => {
  350. if (event.status === "completed" && Array.isArray(event.result.content))
  351. event.result.content.splice(0)
  352. }),
  353. )
  354. .pipe(Effect.asVoid)
  355. }),
  356. })
  357. yield* plugins.activate([versioned(plugin)])
  358. const toolSet = yield* registry.snapshot()
  359. const execution = yield* toolSet.execute({
  360. sessionID: Session.ID.make("ses_hooks"),
  361. agent: Agent.ID.make("build"),
  362. messageID: SessionMessage.ID.make("msg_hooks"),
  363. call: { type: "tool-call", id: "call-hooks", name: "echo", input: { text: "original" } },
  364. })
  365. expect(seen.before).toEqual({ text: "original" })
  366. expect(executed).toEqual([{ text: "before-mutated" }])
  367. expect(seen.after).toEqual({
  368. input: { text: "before-mutated" },
  369. status: "completed",
  370. content: [{ type: "text", text: '{"text":"before-mutated"}' }],
  371. metadata: undefined,
  372. })
  373. expect(execution).toMatchObject({
  374. content: [{ type: "text", text: '{"text":"before-mutated"}' }],
  375. metadata: { rewritten: true },
  376. })
  377. }),
  378. )
  379. it.effect("rejects tool execution when an execute.before hook fails", () =>
  380. Effect.gen(function* () {
  381. const plugins = yield* Plugin.Service
  382. const registry = yield* Tool.Service
  383. const executed: unknown[] = []
  384. const plugin = EffectPlugin.define({
  385. id: "tool-hook-reject",
  386. effect: (ctx) =>
  387. Effect.gen(function* () {
  388. yield* ctx.tool
  389. .transform((draft) =>
  390. draft.add({
  391. name: "echo",
  392. options: { codemode: false },
  393. description: "Echo",
  394. input: Schema.Struct({ text: Schema.String }),
  395. output: Schema.Struct({ text: Schema.String }),
  396. execute: ({ text }) =>
  397. Effect.sync(() => executed.push({ text })).pipe(Effect.as({ output: { text } })),
  398. }),
  399. )
  400. .pipe(Effect.orDie)
  401. yield* ctx.tool
  402. .hook("execute.before", () => new ToolFailure({ message: "write disabled" }))
  403. .pipe(Effect.asVoid)
  404. }),
  405. })
  406. yield* plugins.activate([versioned(plugin)])
  407. const toolSet = yield* registry.snapshot()
  408. const failure = yield* toolSet
  409. .execute({
  410. sessionID: Session.ID.make("ses_hook_reject"),
  411. agent: Agent.ID.make("build"),
  412. messageID: SessionMessage.ID.make("msg_hook_reject"),
  413. call: { type: "tool-call", id: "call-hook-reject", name: "echo", input: { text: "original" } },
  414. })
  415. .pipe(Effect.flip)
  416. expect(failure).toMatchObject({ _tag: "Tool.Error", message: "write disabled" })
  417. expect(executed).toEqual([])
  418. }),
  419. )
  420. })