import * as fs from 'node:fs'; import * as path from 'node:path'; import { buildActiveBehavioralSpec } from './behavioral-spec.js'; /** The resolved behavioral-spec shape returned by `buildActiveBehavioralSpec`. */ export type BehavioralSpec = ReturnType; import { loadCustomPersonas } from './custom-personas.js'; import type { AgentPersona } from './personas.js'; export interface LoadedSkill { name: string; content: string; } /** * Raw disk read of `{waggleDir}/system-prompt.md`. Returns null if missing * or empty. Runtime code should prefer `loadSystemPromptWithOverrides`, which * also returns the behavioral spec and custom personas. */ export function loadSystemPrompt(waggleDir: string): string | null { const filePath = path.join(waggleDir, 'system-prompt.md'); if (!fs.existsSync(filePath)) return null; const content = fs.readFileSync(filePath, 'utf-8').trim(); return content || null; } /** Bundle of every prompt input the agent runtime needs. */ export interface ComposedSystemPrompt { /** User's literal system-prompt.md content, or null if absent. */ userPromptMd: string | null; /** The active behavioral spec. */ behavioralSpec: BehavioralSpec; /** Custom personas discovered in `waggleDir/personas/*.json`. */ customPersonas: AgentPersona[]; } /** * Composes the runtime prompt inputs: reads `system-prompt.md`, builds the * behavioral spec and surfaces any custom personas the user has added. * * Callers typically pass the bundle to their orchestrator: * * const composed = loadSystemPromptWithOverrides(waggleDir); * const orchestrator = new Orchestrator({ * systemPromptAddendum: composed.userPromptMd, * behavioralSpec: composed.behavioralSpec, * extraPersonas: composed.customPersonas, * }); * * Does not throw on missing files — any piece absent resolves to an empty * value so a fresh install just gets the compile-time baseline. */ export function loadSystemPromptWithOverrides(waggleDir: string): ComposedSystemPrompt { const userPromptMd = loadSystemPrompt(waggleDir); const behavioralSpec = buildActiveBehavioralSpec(); const customPersonas = loadCustomPersonas(waggleDir); return { userPromptMd, behavioralSpec, customPersonas }; } export function loadSkills(waggleDir: string): LoadedSkill[] { const skillsDir = path.join(waggleDir, 'skills'); if (!fs.existsSync(skillsDir)) return []; const files = fs.readdirSync(skillsDir).filter(f => f.endsWith('.md')); return files.map(f => ({ name: f.replace(/\.md$/, ''), content: fs.readFileSync(path.join(skillsDir, f), 'utf-8').trim(), })); }