boot.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. export * as PluginBoot from "./boot"
  2. import { Context, Deferred, Effect, Layer, Scope } from "effect"
  3. import { AccountV2 } from "../account"
  4. import { AgentV2 } from "../agent"
  5. import { Catalog } from "../catalog"
  6. import { EventV2 } from "../event"
  7. import { Npm } from "../npm"
  8. import { PluginV2 } from "../plugin"
  9. import { AccountPlugin } from "./account"
  10. import { EnvPlugin } from "./env"
  11. import { ModelsDevPlugin } from "./models-dev"
  12. import { ProviderPlugins } from "./provider"
  13. type Plugin = {
  14. id: PluginV2.ID
  15. effect: PluginV2.Effect<
  16. Catalog.Service | AgentV2.Service | AccountV2.Service | Npm.Service | EventV2.Service | PluginV2.Service
  17. >
  18. }
  19. export interface Interface {
  20. readonly wait: () => Effect.Effect<void>
  21. }
  22. export class Service extends Context.Service<Service, Interface>()("@opencode/v2/PluginBoot") {}
  23. export const layer = Layer.effect(
  24. Service,
  25. Effect.gen(function* () {
  26. const agent = yield* AgentV2.Service
  27. const catalog = yield* Catalog.Service
  28. const plugin = yield* PluginV2.Service
  29. const accounts = yield* AccountV2.Service
  30. const npm = yield* Npm.Service
  31. const events = yield* EventV2.Service
  32. const done = yield* Deferred.make<void>()
  33. const add = Effect.fn("PluginBoot.add")(function* (input: Plugin) {
  34. yield* plugin.add({
  35. id: input.id,
  36. effect: input.effect.pipe(
  37. Effect.provideService(Catalog.Service, catalog),
  38. Effect.provideService(AgentV2.Service, agent),
  39. Effect.provideService(AccountV2.Service, accounts),
  40. Effect.provideService(Npm.Service, npm),
  41. Effect.provideService(EventV2.Service, events),
  42. Effect.provideService(PluginV2.Service, plugin),
  43. ),
  44. })
  45. })
  46. const boot = Effect.gen(function* () {
  47. yield* add(EnvPlugin)
  48. yield* add(AccountPlugin)
  49. for (const item of ProviderPlugins) {
  50. yield* add(item)
  51. }
  52. yield* add(ModelsDevPlugin)
  53. }).pipe(Effect.withSpan("PluginBoot.boot"))
  54. yield* boot.pipe(
  55. Effect.exit,
  56. Effect.flatMap((exit) => Deferred.done(done, exit)),
  57. Effect.forkScoped,
  58. )
  59. return Service.of({
  60. wait: () => Deferred.await(done),
  61. })
  62. }),
  63. )
  64. export const defaultLayer = layer.pipe(
  65. Layer.provide(AgentV2.defaultLayer),
  66. Layer.provide(Catalog.defaultLayer),
  67. Layer.provide(EventV2.defaultLayer),
  68. Layer.provide(PluginV2.defaultLayer),
  69. Layer.provide(AccountV2.defaultLayer),
  70. Layer.provide(Npm.defaultLayer),
  71. )