|
| 1 | +import type { PluginDiscoveryResult } from "../plugins/discovery.js"; |
| 2 | +import { extractPluginInstallRecordsFromInstalledPluginIndex } from "../plugins/installed-plugin-index-install-records.js"; |
| 3 | +import { resolvePluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js"; |
| 4 | +import type { PluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.types.js"; |
| 5 | +import type { PluginRegistry } from "../plugins/registry-types.js"; |
| 6 | +import { |
| 7 | + resolvePluginRuntimeLoadContext, |
| 8 | + type PluginRuntimeLoadContext, |
| 9 | +} from "../plugins/runtime/load-context.js"; |
| 10 | +import type { PreparedModelRuntimeInput } from "./prepared-model-runtime.types.js"; |
| 11 | + |
| 12 | +const preparedPluginRuntimeLoadContext = Symbol("preparedPluginRuntimeLoadContext"); |
| 13 | +const emptyPluginDiscovery: PluginDiscoveryResult = { candidates: [], diagnostics: [] }; |
| 14 | + |
| 15 | +type PreparedPluginRegistry = PluginRegistry & { |
| 16 | + [preparedPluginRuntimeLoadContext]?: PluginRuntimeLoadContext; |
| 17 | +}; |
| 18 | + |
| 19 | +function setPreparedPluginRuntimeLoadContext( |
| 20 | + registry: PluginRegistry, |
| 21 | + context: PluginRuntimeLoadContext, |
| 22 | +): void { |
| 23 | + (registry as PreparedPluginRegistry)[preparedPluginRuntimeLoadContext] = context; |
| 24 | +} |
| 25 | + |
| 26 | +function preparePluginLoadContext( |
| 27 | + input: PreparedModelRuntimeInput, |
| 28 | + env: NodeJS.ProcessEnv, |
| 29 | + registry: PluginRegistry | undefined, |
| 30 | + metadataSnapshot: PluginMetadataSnapshot, |
| 31 | +): PluginRuntimeLoadContext & { metadataSnapshot: PluginMetadataSnapshot } { |
| 32 | + const { config, workspaceDir } = input; |
| 33 | + // The prepared owner already resolved metadata for this exact config/env/workspace tuple. |
| 34 | + // Missing discovery facts stay empty here instead of reopening cold channel discovery. |
| 35 | + const preparedMetadataSnapshot = metadataSnapshot.discovery |
| 36 | + ? metadataSnapshot |
| 37 | + : { ...metadataSnapshot, discovery: emptyPluginDiscovery }; |
| 38 | + const context = { |
| 39 | + ...resolvePluginRuntimeLoadContext({ |
| 40 | + config, |
| 41 | + env, |
| 42 | + workspaceDir, |
| 43 | + metadataSnapshot: preparedMetadataSnapshot, |
| 44 | + manifestRegistry: metadataSnapshot.manifestRegistry, |
| 45 | + }), |
| 46 | + metadataSnapshot, |
| 47 | + installRecords: extractPluginInstallRecordsFromInstalledPluginIndex(metadataSnapshot.index), |
| 48 | + }; |
| 49 | + if (registry) { |
| 50 | + // The prepared registry is the lifecycle-owned carrier; standalone callers keep the cold path. |
| 51 | + setPreparedPluginRuntimeLoadContext(registry, context); |
| 52 | + } |
| 53 | + return context; |
| 54 | +} |
| 55 | + |
| 56 | +/** Resolves and attaches the plugin facts owned by one prepared workspace generation. */ |
| 57 | +export function prepareOwnedPluginLoadContext( |
| 58 | + input: PreparedModelRuntimeInput, |
| 59 | + env: NodeJS.ProcessEnv, |
| 60 | + registry: PluginRegistry | undefined, |
| 61 | +): PluginMetadataSnapshot { |
| 62 | + const metadataSnapshot = resolvePluginMetadataSnapshot({ |
| 63 | + config: input.config, |
| 64 | + env, |
| 65 | + ...(input.workspaceDir ? { workspaceDir: input.workspaceDir } : {}), |
| 66 | + }); |
| 67 | + preparePluginLoadContext(input, env, registry, metadataSnapshot); |
| 68 | + return metadataSnapshot; |
| 69 | +} |
| 70 | + |
| 71 | +/** Reads plugin facts carried by a lifecycle-owned prepared runtime snapshot. */ |
| 72 | +export const getPreparedPluginRuntimeLoadContext = ( |
| 73 | + registry: PluginRegistry | undefined, |
| 74 | +): PluginRuntimeLoadContext | undefined => |
| 75 | + (registry as PreparedPluginRegistry | undefined)?.[preparedPluginRuntimeLoadContext]; |
0 commit comments