/*
RFC-0473: I/O helper for loading Programmatic Surface module contexts from a Sternsystem's
system.md. Extracted from site-kernel-checks so bordbuch.generate in site-kernel-handoff
can read PSEO module context without depending on site-kernel-checks.
- Do not mutate system.md.
- Do not make LLM calls or interpret Blueprint axis policy.
- Do not define validation diagnostics — that lives in site-kernel-checks.
- RFC-0473: extract loadSurfaceModuleContexts from site-kernel-checks for cross-package reuse.
- RFC-0868: inline loadSystemManifest to break dependency on werkstatt-site/content.
*/
import { join } from "node:path";
import { parse as parseYaml } from "yaml";
import { normalizeSurfaceModules, type SurfaceModules } from "../index.ts";
import { createDefaultIO, type WorkspaceIO } from "@warpgogol/werkstatt-shared/kernel/workspace-io";
async function loadSystemManifestRaw(
contentDirectory: string,
io: WorkspaceIO = createDefaultIO().io,
): Promise> {
const systemMdPath = join(contentDirectory, "system.md");
if (!(await io.exists(systemMdPath))) return {};
const content = await io.readFile(systemMdPath);
const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/);
if (!match) return {};
return parseYaml(match[1]) as Record;
}
export interface LoadedModuleContexts {
modules: SurfaceModules;
declaredBlueprints: string[];
supportedLocales: string[];
defaultLocale?: string;
}
function asStringArray(value: unknown): string[] {
return Array.isArray(value) ? value.map(String).filter(Boolean) : [];
}
export async function loadSurfaceModuleContexts(appDir: string): Promise {
const record = await loadSystemManifestRaw(join(appDir, "src", "content"));
const i18n = record.i18n as { default?: string; supported?: Record } | undefined;
const surface = record.surface as { blueprints?: unknown; modules?: unknown } | undefined;
const supportedLocales = i18n?.supported
? Object.keys(i18n.supported)
: i18n?.default
? [i18n.default]
: [];
return {
modules: normalizeSurfaceModules(surface?.modules ?? {}),
declaredBlueprints: asStringArray(surface?.blueprints),
supportedLocales,
defaultLocale: i18n?.default,
};
}