| 18 | * into one reload per domain. |
| 19 | */ |
| 20 | export function fromPromise(plugin: Plugin) { |
| 21 | return define({ |
| 22 | id: plugin.id, |
| 23 | effect: (host) => |
| 24 | Effect.gen(function* () { |
| 25 | const scope = yield* Scope.Scope |
| 26 | const context = yield* Effect.context<Scope.Scope>() |
| 27 | |
| 28 | // Run a hook registration on the plugin scope and resolve once it is registered. |
| 29 | const register = (effect: Effect.Effect<HostRegistration, never, Scope.Scope>): Promise<Registration> => |
| 30 | Effect.runPromiseWith(context)(Scope.provide(scope)(effect)).then((registration) => ({ |
| 31 | dispose: () => Effect.runPromiseWith(context)(registration.dispose), |
| 32 | })) |
| 33 | |
| 34 | const run = (effect: Effect.Effect<void>) => Effect.runPromiseWith(context)(effect) |
| 35 | |
| 36 | const transform = |
| 37 | <Draft>(domain: { |
| 38 | transform: ( |
| 39 | callback: (draft: Draft) => Effect.Effect<void> | void, |
| 40 | ) => Effect.Effect<HostRegistration, never, Scope.Scope> |
| 41 | }) => |
| 42 | (callback: (draft: Draft) => Promise<void> | void) => |
| 43 | register(domain.transform((draft) => Effect.promise(() => Promise.resolve(callback(draft))))) |
| 44 | |
| 45 | const context2: PluginContext = { |
| 46 | options: host.options, |
| 47 | agent: { |
| 48 | transform: transform(host.agent), |
| 49 | reload: () => run(host.agent.reload()), |
| 50 | }, |
| 51 | aisdk: { |
| 52 | sdk: (callback) => |
| 53 | register(host.aisdk.sdk((event) => Effect.promise(() => Promise.resolve(callback(event))))), |
| 54 | language: (callback) => |
| 55 | register(host.aisdk.language((event) => Effect.promise(() => Promise.resolve(callback(event))))), |
| 56 | }, |
| 57 | catalog: { |
| 58 | transform: transform(host.catalog), |
| 59 | reload: () => run(host.catalog.reload()), |
| 60 | }, |
| 61 | command: { |
| 62 | transform: transform(host.command), |
| 63 | reload: () => run(host.command.reload()), |
| 64 | }, |
| 65 | integration: { |
| 66 | transform: transform(host.integration), |
| 67 | reload: () => run(host.integration.reload()), |
| 68 | connection: { |
| 69 | active: (id) => Effect.runPromiseWith(context)(host.integration.connection.active(id)), |
| 70 | resolve: (connection) => Effect.runPromiseWith(context)(host.integration.connection.resolve(connection)), |
| 71 | }, |
| 72 | }, |
| 73 | plugin: { |
| 74 | add: (input) => { |
| 75 | const child = fromPromise(input) |
| 76 | return run(host.plugin.add(child)) |
| 77 | }, |