|
| 1 | +export * as SdkInstances from "./instances" |
| 2 | + |
| 3 | +import { Instance } from "@opencode-ai/core/instance" |
| 4 | +import { LocationServiceMap } from "@opencode-ai/core/location-service-map" |
| 5 | +import { Plugin } from "@opencode-ai/core/plugin" |
| 6 | +import type { InstancePlugins } from "@opencode-ai/core/plugin/instance" |
| 7 | +import { SdkPlugins } from "@opencode-ai/core/plugin/sdk" |
| 8 | +import { Location } from "@opencode-ai/schema/location" |
| 9 | +import type { Session } from "@opencode-ai/schema/session" |
| 10 | +import { makeGlobalNode } from "@opencode-ai/util/effect/app-node" |
| 11 | +import type { LayerNode } from "@opencode-ai/util/effect/layer-node" |
| 12 | +import { Duration, Effect, Layer, LayerMap, Scope } from "effect" |
| 13 | + |
| 14 | +export interface Configuration { |
| 15 | + readonly plugins: InstancePlugins.List |
| 16 | +} |
| 17 | + |
| 18 | +export interface Options { |
| 19 | + /** Select a sharing key within the Session's current Location. Must not initialize plugins. */ |
| 20 | + readonly key: (session: Session.Info) => string |
| 21 | + /** Reconstruct configuration on a cache miss. Resources belong to the instance Scope. */ |
| 22 | + readonly configure: (key: string) => Effect.Effect<Configuration, unknown, Scope.Scope> |
| 23 | +} |
| 24 | + |
| 25 | +/** Replaces the host's `Instance.node`; `replacements` resolves lazily so instances inherit the final host graph. */ |
| 26 | +export function node(options: Options, replacements: () => LayerNode.Replacements) { |
| 27 | + return makeGlobalNode({ |
| 28 | + service: Instance.Service, |
| 29 | + layer: layer(options, replacements), |
| 30 | + deps: [LocationServiceMap.node, SdkPlugins.node], |
| 31 | + }) |
| 32 | +} |
| 33 | + |
| 34 | +export function layer(options: Options, replacements: () => LayerNode.Replacements) { |
| 35 | + return Layer.effect( |
| 36 | + Instance.Service, |
| 37 | + Effect.gen(function* () { |
| 38 | + const scope = yield* Effect.scope |
| 39 | + const locations = yield* LocationServiceMap.Service |
| 40 | + const sdk = yield* SdkPlugins.Service |
| 41 | + const key = (session: Session.Info) => ({ |
| 42 | + key: options.key(session), |
| 43 | + ...LocationServiceMap.canonical(session.location), |
| 44 | + }) |
| 45 | + const provide = (session: Session.Info) => Effect.provide(instances.get(key(session))) |
| 46 | + const instances: LayerMap.LayerMap<ReturnType<typeof key>, Instance.Services> = yield* LayerMap.make( |
| 47 | + (input: ReturnType<typeof key>) => |
| 48 | + Layer.unwrap( |
| 49 | + Effect.gen(function* () { |
| 50 | + const configuration = yield* options.configure(input.key).pipe(Effect.orDie) |
| 51 | + // A host/instance ID collision fails the whole plugin generation, which leaves no inventory |
| 52 | + // trace to check after activation. Reject it before constructing anything. |
| 53 | + const collisions = configuration.plugins.filter((plugin) => |
| 54 | + sdk.all().some((host) => host.id === plugin.id), |
| 55 | + ) |
| 56 | + if (collisions.length > 0) |
| 57 | + yield* Effect.die( |
| 58 | + new Error( |
| 59 | + `Instance plugin IDs collide with host plugins: ${collisions.map((plugin) => plugin.id).join(", ")}`, |
| 60 | + ), |
| 61 | + ) |
| 62 | + return Instance.layer(Location.Ref.make({ directory: input.directory, workspaceID: input.workspaceID }), { |
| 63 | + plugins: configuration.plugins, |
| 64 | + replacements: [ |
| 65 | + ...replacements(), |
| 66 | + // Instances borrow this selector and the host's Location map instead of retaining |
| 67 | + // their Layer scopes; retaining the selector would block its shutdown on its own entries. |
| 68 | + Instance.node.replace(Layer.succeed(Instance.Service, { provide })), |
| 69 | + LocationServiceMap.node.replace(Layer.succeed(LocationServiceMap.Service, locations)), |
| 70 | + ], |
| 71 | + }).pipe( |
| 72 | + Layer.tap((context) => |
| 73 | + Effect.gen(function* () { |
| 74 | + const plugins = yield* Plugin.Service |
| 75 | + yield* plugins.awaitActivation |
| 76 | + const failed = (yield* plugins.list()).filter( |
| 77 | + (plugin) => |
| 78 | + plugin.state.status === "failed" && |
| 79 | + configuration.plugins.some((configured) => configured.id === plugin.id), |
| 80 | + ) |
| 81 | + if (failed.length > 0) |
| 82 | + yield* Effect.die( |
| 83 | + new Error(`Instance plugin setup failed: ${failed.map((plugin) => plugin.id).join(", ")}`), |
| 84 | + ) |
| 85 | + }).pipe(Effect.provide(context)), |
| 86 | + ), |
| 87 | + ) |
| 88 | + }), |
| 89 | + ).pipe( |
| 90 | + // Eviction can close the lookup's scope; do not make that fiber wait on itself. |
| 91 | + Layer.tapCause(() => |
| 92 | + instances.invalidate(input).pipe(Effect.forkIn(scope, { startImmediately: true }), Effect.asVoid), |
| 93 | + ), |
| 94 | + ), |
| 95 | + { idleTimeToLive: Duration.infinity }, |
| 96 | + ) |
| 97 | + return Instance.Service.of({ provide }) |
| 98 | + }), |
| 99 | + ) |
| 100 | +} |
0 commit comments