|
| 1 | +import { runtimeDependencyPlanContract } from "./agent-task-recipe.js" |
| 2 | +import { runtimeProfile, type RuntimeProfile, type RuntimeProfileDependency, type RuntimeProfileReadiness } from "./runtime-boundary-contracts.js" |
| 3 | +import type { WorkspaceRecipeExtraPlugin } from "./runtime-contracts.js" |
| 4 | +import { isPlainObject, stripUndefined } from "./object-utils.js" |
| 5 | + |
| 6 | +export interface RuntimeProfileDependencyPlan { |
| 7 | + schema: "wp-codebox/runtime-dependency-plan/v1" |
| 8 | + [key: string]: unknown |
| 9 | +} |
| 10 | + |
| 11 | +export interface RuntimeProfileExecutionPlan { |
| 12 | + schema: "wp-codebox/runtime-profile-execution-plan/v1" |
| 13 | + dependency_plan: RuntimeProfileDependencyPlan |
| 14 | + extra_plugins?: WorkspaceRecipeExtraPlugin[] |
| 15 | + runtime_overlays?: Array<Record<string, unknown>> |
| 16 | + runtime_env?: Record<string, string> |
| 17 | + readiness?: RuntimeProfileReadiness |
| 18 | +} |
| 19 | + |
| 20 | +export interface RuntimeProfilePreflight { |
| 21 | + schema: "wp-codebox/runtime-profile-preflight/v1" |
| 22 | + readiness: RuntimeProfileReadiness |
| 23 | +} |
| 24 | + |
| 25 | +export function compileRuntimeProfile(input: unknown): RuntimeProfileExecutionPlan { |
| 26 | + const profile = runtimeProfile(input) |
| 27 | + const componentPlugins = [ |
| 28 | + ...dependencyPlugins(profile.components, "component", "plugin"), |
| 29 | + ...dependencyPlugins(profile.plugins, "plugin", "plugin"), |
| 30 | + ...dependencyPlugins(profile.mu_plugins, "mu_plugin", "mu-plugin"), |
| 31 | + ...objectPlugins(profile.extra_plugins), |
| 32 | + ...objectPlugins(profile.component_contracts), |
| 33 | + ] |
| 34 | + const runtimeOverlays = [ |
| 35 | + ...overlayDependencies(profile.overlays), |
| 36 | + ...(profile.runtime_overlays ?? []), |
| 37 | + ] |
| 38 | + const readiness = runtimeProfilePreflight(profile).readiness |
| 39 | + |
| 40 | + return stripUndefined({ |
| 41 | + schema: "wp-codebox/runtime-profile-execution-plan/v1", |
| 42 | + dependency_plan: runtimeDependencyPlanContract({ |
| 43 | + provider_plugins: objectPlugins(profile.provider_plugins), |
| 44 | + component_plugins: componentPlugins, |
| 45 | + runtime_overlays: runtimeOverlays, |
| 46 | + runtime_env: profile.env, |
| 47 | + }) as RuntimeProfileDependencyPlan, |
| 48 | + extra_plugins: componentPlugins.length > 0 ? componentPlugins : undefined, |
| 49 | + runtime_overlays: runtimeOverlays.length > 0 ? runtimeOverlays : undefined, |
| 50 | + runtime_env: profile.env, |
| 51 | + readiness, |
| 52 | + }) as RuntimeProfileExecutionPlan |
| 53 | +} |
| 54 | + |
| 55 | +export function runtimeProfilePreflight(input: unknown): RuntimeProfilePreflight { |
| 56 | + const profile = isRuntimeProfile(input) ? input : runtimeProfile(input) |
| 57 | + return { |
| 58 | + schema: "wp-codebox/runtime-profile-preflight/v1", |
| 59 | + readiness: profile.readiness ?? inferredReadiness(profile), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function dependencyPlugins(dependencies: RuntimeProfileDependency[] | undefined, kind: string, loadAs: "plugin" | "mu-plugin"): WorkspaceRecipeExtraPlugin[] { |
| 64 | + return (dependencies ?? []).flatMap((dependency) => { |
| 65 | + if (!dependency.source) return [] |
| 66 | + return [stripUndefined({ |
| 67 | + source: dependency.source, |
| 68 | + slug: dependency.slug, |
| 69 | + pluginFile: stringField(dependency.metadata, "pluginFile"), |
| 70 | + activate: dependency.activate ?? (loadAs === "plugin" ? true : undefined), |
| 71 | + loadAs, |
| 72 | + metadata: stripUndefined({ |
| 73 | + runtimeProfileDependency: stripUndefined({ |
| 74 | + kind, |
| 75 | + target: dependency.target, |
| 76 | + required: dependency.required, |
| 77 | + readiness: dependency.readiness, |
| 78 | + provenance: dependency.provenance, |
| 79 | + }), |
| 80 | + ...(dependency.metadata ?? {}), |
| 81 | + }), |
| 82 | + })] |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +function objectPlugins(entries: Array<Record<string, unknown>> | undefined): WorkspaceRecipeExtraPlugin[] { |
| 87 | + return (entries ?? []).flatMap((entry) => { |
| 88 | + const source = stringField(entry, "source") || stringField(entry, "path") |
| 89 | + if (!source) return [] |
| 90 | + const loadAs: "plugin" | "mu-plugin" = entry.loadAs === "mu-plugin" || entry.load_as === "mu-plugin" ? "mu-plugin" : "plugin" |
| 91 | + return [stripUndefined({ |
| 92 | + source, |
| 93 | + slug: stringField(entry, "slug") || stringField(entry, "component") || stringField(entry, "name"), |
| 94 | + pluginFile: stringField(entry, "pluginFile") || stringField(entry, "plugin_file"), |
| 95 | + activate: typeof entry.activate === "boolean" ? entry.activate : loadAs === "plugin" ? true : undefined, |
| 96 | + sha256: stringField(entry, "sha256"), |
| 97 | + loadAs, |
| 98 | + metadata: isPlainObject(entry.metadata) ? entry.metadata : undefined, |
| 99 | + })] |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +function overlayDependencies(dependencies: RuntimeProfileDependency[] | undefined): Array<Record<string, unknown>> { |
| 104 | + return (dependencies ?? []).map((dependency) => stripUndefined({ |
| 105 | + kind: dependency.kind, |
| 106 | + slug: dependency.slug, |
| 107 | + source: dependency.source, |
| 108 | + target: dependency.target, |
| 109 | + activate: dependency.activate, |
| 110 | + required: dependency.required, |
| 111 | + readiness: dependency.readiness, |
| 112 | + provenance: dependency.provenance, |
| 113 | + metadata: dependency.metadata, |
| 114 | + })) |
| 115 | +} |
| 116 | + |
| 117 | +function inferredReadiness(profile: RuntimeProfile): RuntimeProfileReadiness { |
| 118 | + const dependencies = [ |
| 119 | + ...profile.components, |
| 120 | + ...(profile.plugins ?? []), |
| 121 | + ...(profile.mu_plugins ?? []), |
| 122 | + ...(profile.themes ?? []), |
| 123 | + ...(profile.overlays ?? []), |
| 124 | + ] |
| 125 | + const missing = dependencies |
| 126 | + .filter((dependency) => dependency.required !== false && (dependency.readiness === "missing" || !dependency.source)) |
| 127 | + .map((dependency) => `${dependency.kind}:${dependency.slug}`) |
| 128 | + const blocked = dependencies.some((dependency) => dependency.readiness === "blocked") |
| 129 | + return { |
| 130 | + status: blocked ? "blocked" : missing.length > 0 ? "missing" : "ready", |
| 131 | + checks: { |
| 132 | + dependencies: missing.length === 0 && !blocked, |
| 133 | + }, |
| 134 | + missing, |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +function isRuntimeProfile(input: unknown): input is RuntimeProfile { |
| 139 | + return isPlainObject(input) && input.schema === "wp-codebox/runtime-profile/v1" && Array.isArray(input.components) |
| 140 | +} |
| 141 | + |
| 142 | +function stringField(record: Record<string, unknown> | undefined, key: string): string | undefined { |
| 143 | + if (!record) return undefined |
| 144 | + const value = record[key] |
| 145 | + return typeof value === "string" && value.trim() !== "" ? value.trim() : undefined |
| 146 | +} |
0 commit comments