package config import ( "fmt" "path/filepath" "regexp" "slices" "sort" "strings" configkit "go.maximumdirect.net/configkit" ) // LoadPipeline loads pipeline configuration from a YAML file with strict field checking. func LoadPipeline(path string) (*PipelineConfig, error) { return LoadPipelineWithOptions(path, PipelineLoadOptions{}) } // PipelineLoadOptions carries an optional explicit overlay selection. Nil, // empty, and the exact name "default" select the base configuration. type PipelineLoadOptions struct { Overlay *string Search *configkit.Search } // LoadPipelineWithOptions loads pipeline configuration with strict field // checking and optional named-overlay selection. func LoadPipelineWithOptions(path string, opts PipelineLoadOptions) (*PipelineConfig, error) { cfg, err := loadPipelineWithConfigKit(path, opts) if err != nil { return nil, err } return finalizeLoadedPipeline(cfg) } // LoadPipelineOverlayPair resolves two overlays as independent ConfigKit loads. func LoadPipelineOverlayPair(path, leftOverlay, rightOverlay string) (*PipelineConfig, *PipelineConfig, error) { if leftOverlay == rightOverlay { return nil, nil, fmt.Errorf("load pipeline overlays: left and right overlay selections must differ") } leftOptions := PipelineLoadOptions{Overlay: &leftOverlay} rightOptions := PipelineLoadOptions{Overlay: &rightOverlay} left, err := LoadPipelineWithOptions(path, leftOptions) if err != nil { return nil, nil, fmt.Errorf("load left pipeline overlay: %w", err) } right, err := LoadPipelineWithOptions(path, rightOptions) if err != nil { return nil, nil, fmt.Errorf("load right pipeline overlay: %w", err) } return left, right, nil } func finalizeLoadedPipeline(cfg *PipelineConfig) (*PipelineConfig, error) { finalizeArtifactDependencies(cfg) if err := finalizePipelineResolution(cfg); err != nil { return nil, fmt.Errorf("load pipeline config: %w", err) } retainArtifactFamilyDeclarations(cfg) return cfg, nil } // LoadCampaign loads campaign configuration from a YAML file with strict field checking. func LoadCampaign(path string) (*CampaignConfig, error) { return loadCampaignWithConfigKit(path) } // LoadSession loads session configuration from a YAML file with strict field checking. func LoadSession(path string) (*SessionConfig, error) { return LoadSessionWithOptions(path, SessionLoadOptions{}) } // SessionLoadOptions configures expected session identity checks. type SessionLoadOptions struct { SessionID string PreviousSessionID string Overlay *string } // LoadSessionWithOptions loads session configuration from a YAML file with // strict field checking. func LoadSessionWithOptions(path string, opts SessionLoadOptions) (*SessionConfig, error) { cfg, _, err := loadSessionSource(configkit.File(path), path, opts, true) return cfg, err } // LoadSessionBytesWithOptions loads a contextless session snapshot. func LoadSessionBytesWithOptions(label string, data []byte, opts SessionLoadOptions) (*SessionConfig, error) { cfg, _, err := loadSessionSource(configkit.Bytes(label, data), label, opts, false) return cfg, err } func validateSessionIdentity(label string, cfg *SessionConfig, opts SessionLoadOptions) error { if strings.TrimSpace(opts.SessionID) != "" && strings.TrimSpace(cfg.SessionID) != "" && strings.TrimSpace(cfg.SessionID) != strings.TrimSpace(opts.SessionID) { return fmt.Errorf("load session config: session file %q: session_id mismatch: --session-id %q does not match session_id %q", label, strings.TrimSpace(opts.SessionID), strings.TrimSpace(cfg.SessionID)) } if expected := strings.TrimSpace(opts.PreviousSessionID); expected != "" { actual := strings.TrimSpace(cfg.PreviousSessionID) if actual == "" { return fmt.Errorf("load session config: session file %q: previous_session_id is required when --previous-session-id %q is provided", label, expected) } if actual != expected { return fmt.Errorf("load session config: session file %q: previous_session_id mismatch: --previous-session-id %q does not match previous_session_id %q", label, expected, actual) } } return nil } // Load loads and resolves combined pipeline, campaign, and session configuration. // Passing only a session path is supported for package-internal compatibility; // in that form campaign.yml is expected next to the session file. func Load(pipelinePath string, paths ...string) (*Config, error) { campaignPath, sessionPath, err := campaignSessionPaths(paths...) if err != nil { return nil, err } return LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{}) } // LoadWithSessionOptions loads and resolves combined pipeline, campaign, and // session configuration with expected session identity checks. func LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath string, sessionOpts SessionLoadOptions) (*Config, error) { pipelineCfg, err := LoadPipelineWithOptions(pipelinePath, PipelineLoadOptions{Overlay: sessionOpts.Overlay}) if err != nil { return nil, err } campaignCfg, err := LoadCampaign(campaignPath) if err != nil { return nil, err } loaded, err := LoadPipelineCampaign(pipelinePath, pipelineCfg, campaignPath, campaignCfg) if err != nil { return nil, err } return LoadSessionWithPipelineCampaignOptions(loaded, sessionPath, sessionOpts) } // LoadedPipelineCampaign retains one already loaded pipeline and campaign for // subsequent local or remote session resolution. It prevents a command from // reloading the root pipeline after campaign selection. type LoadedPipelineCampaign struct { PipelinePath string Pipeline *PipelineConfig CampaignPath string Campaign *CampaignConfig Party ResolvedParty } // LoadPipelineCampaign combines already loaded pipeline and campaign documents // with their campaign-owned party source. Callers that later resolve a session // retain this context rather than independently reimplementing party loading. func LoadPipelineCampaign(pipelinePath string, pipeline *PipelineConfig, campaignPath string, campaign *CampaignConfig) (LoadedPipelineCampaign, error) { if pipeline == nil { return LoadedPipelineCampaign{}, fmt.Errorf("pipeline config is required") } if campaign == nil { return LoadedPipelineCampaign{}, fmt.Errorf("campaign config is required") } party, err := resolveCampaignParty(campaignPath, campaign) if err != nil { return LoadedPipelineCampaign{}, err } return LoadPipelineCampaignWithParty(pipelinePath, pipeline, campaignPath, campaign, party) } // LoadPipelineCampaignWithParty combines an already loaded pipeline and // campaign with one already resolved campaign-owned party. It is useful when // more than one independently resolved pipeline must be expanded against the // exact same party document. func LoadPipelineCampaignWithParty(pipelinePath string, pipeline *PipelineConfig, campaignPath string, campaign *CampaignConfig, party ResolvedParty) (LoadedPipelineCampaign, error) { if pipeline == nil { return LoadedPipelineCampaign{}, fmt.Errorf("pipeline config is required") } if campaign == nil { return LoadedPipelineCampaign{}, fmt.Errorf("campaign config is required") } if party.Canonical == nil { return LoadedPipelineCampaign{}, fmt.Errorf("resolved campaign party is required") } if err := expandPipelineArtifactFamilies(pipeline, party); err != nil { return LoadedPipelineCampaign{}, err } return LoadedPipelineCampaign{ PipelinePath: pipelinePath, Pipeline: pipeline, CampaignPath: campaignPath, Campaign: campaign, Party: party, }, nil } // LoadSessionWithPipelineCampaignOptions loads one local session and combines // it with an already loaded pipeline and campaign. func LoadSessionWithPipelineCampaignOptions(loaded LoadedPipelineCampaign, sessionPath string, sessionOpts SessionLoadOptions) (*Config, error) { sessionCfg, err := LoadSessionWithOptions(sessionPath, sessionOpts) if err != nil { return nil, err } return ResolveLoadedPipelineCampaign(loaded, sessionPath, sessionCfg, SessionSource{ Source: "session_config", LocalPath: sessionPath, }) } // ResolveLoadedPipelineCampaign combines an already loaded pipeline and // campaign with optional already loaded session data. A nil session preserves // the resolved pipeline/campaign context for callers that need to locate or // retrieve a session without rereading the root pipeline. func ResolveLoadedPipelineCampaign(loaded LoadedPipelineCampaign, sessionPath string, sessionCfg *SessionConfig, sessionSource SessionSource) (*Config, error) { if loaded.Pipeline == nil { return nil, fmt.Errorf("pipeline config is required") } if loaded.Campaign == nil { return nil, fmt.Errorf("campaign config is required") } if loaded.Party.Canonical == nil { party, err := resolveCampaignParty(loaded.CampaignPath, loaded.Campaign) if err != nil { return nil, err } loaded.Party = party } cfg := &Config{ Pipeline: loaded.Pipeline, Campaign: loaded.Campaign, Session: sessionCfg, PipelinePath: loaded.PipelinePath, CampaignPath: loaded.CampaignPath, SessionPath: sessionPath, SessionSource: sessionSource, Party: loaded.Party, } campaignDocument := *loaded.Campaign cfg.CampaignDocument = &campaignDocument if sessionCfg == nil { return cfg, nil } sessionDocument := *sessionCfg sessionDocument.Inputs.AudioFiles = slices.Clone(sessionCfg.Inputs.AudioFiles) if sessionCfg.Inputs.AudioS3 != nil { audio := *sessionCfg.Inputs.AudioS3 sessionDocument.Inputs.AudioS3 = &audio } cfg.SessionDocument = &sessionDocument stableInputs, err := mergeCampaignSession(loaded.Campaign, sessionCfg, loaded.CampaignPath, sessionPath) if err != nil { return nil, err } if strings.TrimSpace(cfg.SessionSource.Source) == "" { cfg.SessionSource.Source = "session_config" } if strings.TrimSpace(cfg.SessionSource.LocalPath) == "" { cfg.SessionSource.LocalPath = sessionPath } cfg.StableInputs = stableInputs return cfg, nil } // Resolve builds final stage-facing configuration from already loaded // pipeline, campaign, and session documents. func Resolve(pipelinePath string, pipelineCfg *PipelineConfig, campaignPath string, campaignCfg *CampaignConfig, sessionPath string, sessionCfg *SessionConfig, sessionSource SessionSource) (*Config, error) { if sessionCfg == nil { return nil, fmt.Errorf("session config is required") } loaded, err := LoadPipelineCampaign(pipelinePath, pipelineCfg, campaignPath, campaignCfg) if err != nil { return nil, err } return ResolveLoadedPipelineCampaign(loaded, sessionPath, sessionCfg, sessionSource) } func campaignSessionPaths(paths ...string) (campaignPath, sessionPath string, err error) { switch len(paths) { case 1: sessionPath = paths[0] campaignPath = filepath.Join(filepath.Dir(sessionPath), "campaign.yml") case 2: campaignPath = paths[0] sessionPath = paths[1] default: return "", "", fmt.Errorf("load config: expected session path or campaign and session paths") } return campaignPath, sessionPath, nil } func mergeCampaignSession(campaignCfg *CampaignConfig, sessionCfg *SessionConfig, campaignPath, sessionPath string) (ResolvedStableInputs, error) { if campaignCfg == nil { return ResolvedStableInputs{}, fmt.Errorf("campaign config is required") } if sessionCfg == nil { return ResolvedStableInputs{}, fmt.Errorf("session config is required") } if campaignCfg.Inputs.SpellCatalogFile != "" && strings.TrimSpace(campaignCfg.Inputs.SpellCatalogFile) == "" { return ResolvedStableInputs{}, fmt.Errorf("campaign.inputs.spell_catalog_file must be non-empty when provided") } if sessionCfg.Inputs.SpellCatalogFile != "" && strings.TrimSpace(sessionCfg.Inputs.SpellCatalogFile) == "" { return ResolvedStableInputs{}, fmt.Errorf("session.inputs.spell_catalog_file must be non-empty when provided") } campaignName := CampaignID(campaignCfg) sessionCampaign := strings.TrimSpace(sessionCfg.Campaign) if sessionCampaign != "" && campaignName != "" && sessionCampaign != campaignName { return ResolvedStableInputs{}, fmt.Errorf( "campaign/session config invalid: session campaign %q does not match campaign config %q", sessionCampaign, campaignName, ) } if sessionCampaign == "" { sessionCfg.Campaign = campaignName } stable := ResolvedStableInputs{ SpeakersFile: selectStableInput( campaignCfg.Inputs.SpeakersFile, sessionCfg.Inputs.SpeakersFile, campaignPath, sessionPath, ), AutocorrectFile: selectStableInput( campaignCfg.Inputs.AutocorrectFile, sessionCfg.Inputs.AutocorrectFile, campaignPath, sessionPath, ), GlossaryFile: selectStableInput( campaignCfg.Inputs.GlossaryFile, sessionCfg.Inputs.GlossaryFile, campaignPath, sessionPath, ), PartyFile: selectStableInput( campaignCfg.Inputs.PartyFile, "", campaignPath, sessionPath, ), SpellCatalogFile: selectStableInput( campaignCfg.Inputs.SpellCatalogFile, sessionCfg.Inputs.SpellCatalogFile, campaignPath, sessionPath, ), } for _, field := range []struct { name string input *ResolvedInputFile }{ {"speakers_file", &stable.SpeakersFile}, {"autocorrect_file", &stable.AutocorrectFile}, {"glossary_file", &stable.GlossaryFile}, {"party_file", &stable.PartyFile}, {"spell_catalog_file", &stable.SpellCatalogFile}, } { metadata := campaignCfg.resolution if field.input.Source == "session_config" { metadata = sessionCfg.resolution } if metadata != nil && metadata.sources["/inputs/"+field.name] != "" { field.input.ConfigPath = metadata.sources["/inputs/"+field.name] } } sessionCfg.Inputs.SpeakersFile = stable.SpeakersFile.Path sessionCfg.Inputs.AutocorrectFile = stable.AutocorrectFile.Path sessionCfg.Inputs.GlossaryFile = stable.GlossaryFile.Path sessionCfg.Inputs.SpellCatalogFile = stable.SpellCatalogFile.Path return stable, nil } // CampaignID returns the canonical campaign identity from campaign config. func CampaignID(cfg *CampaignConfig) string { if cfg == nil { return "" } return strings.TrimSpace(cfg.CampaignID) } func selectStableInput(campaignValue, sessionValue, campaignPath, sessionPath string) ResolvedInputFile { if strings.TrimSpace(sessionValue) != "" { return ResolvedInputFile{ Path: sessionValue, ConfigPath: sessionPath, Source: "session_config", } } return ResolvedInputFile{ Path: campaignValue, ConfigPath: campaignPath, Source: "campaign_config", } } var sessionTemplatePlaceholderPattern = regexp.MustCompile(`\{\{[^}]*\}\}`) var sessionTemplateVariablePattern = regexp.MustCompile(`\{\{\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\}\}`) func rejectSessionTemplatePlaceholders(label, content string) error { placeholders := sessionTemplatePlaceholderPattern.FindAllString(content, -1) if len(placeholders) == 0 { return nil } seen := map[string]struct{}{} vars := make([]string, 0, len(placeholders)) for _, placeholder := range placeholders { name := strings.TrimSpace(placeholder) if match := sessionTemplateVariablePattern.FindStringSubmatch(placeholder); len(match) > 1 { name = match[1] } if _, ok := seen[name]; ok { continue } seen[name] = struct{}{} vars = append(vars, name) } sort.Strings(vars) return fmt.Errorf( "session file %q contains template placeholder(s): %s; session.yml must be concrete; run narratio session init to generate it", label, strings.Join(vars, ", "), ) } func shortName(path, fallback string) string { base := filepath.Base(path) if base == "." || base == string(filepath.Separator) { return fallback } return base }