Load campaign and session imports from their sources
This commit is contained in:
@@ -4,10 +4,10 @@ import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
@@ -80,51 +80,30 @@ type SessionLoadOptions struct {
|
||||
// LoadSessionWithOptions loads session configuration from a YAML file with
|
||||
// strict field checking.
|
||||
func LoadSessionWithOptions(path string, opts SessionLoadOptions) (*SessionConfig, error) {
|
||||
sessionBytes, err := fileops.ReadFileBounded(path, maxConfigDocumentBytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load session config: session file %q: open: %w", path, err)
|
||||
}
|
||||
return LoadSessionBytesWithOptions(path, sessionBytes, opts)
|
||||
cfg, _, err := loadSessionSource(configkit.File(path), path, opts, true)
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
// LoadSessionBytesWithOptions loads session configuration from YAML bytes with
|
||||
// strict field checking.
|
||||
// LoadSessionBytesWithOptions loads a contextless session snapshot.
|
||||
func LoadSessionBytesWithOptions(label string, data []byte, opts SessionLoadOptions) (*SessionConfig, error) {
|
||||
if err := rejectSessionTemplatePlaceholders(label, string(data)); err != nil {
|
||||
return nil, fmt.Errorf("load session config: %w", err)
|
||||
}
|
||||
cfg, _, err := loadSessionSource(configkit.Bytes(label, data), label, opts, false)
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
cfg, err := loadSessionWithConfigKit(label, data)
|
||||
if err != nil {
|
||||
return nil, 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 nil, 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),
|
||||
)
|
||||
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 expectedPreviousSessionID := strings.TrimSpace(opts.PreviousSessionID); expectedPreviousSessionID != "" {
|
||||
actualPreviousSessionID := strings.TrimSpace(cfg.PreviousSessionID)
|
||||
if actualPreviousSessionID == "" {
|
||||
return nil, fmt.Errorf(
|
||||
"load session config: session file %q: previous_session_id is required when --previous-session-id %q is provided",
|
||||
label,
|
||||
expectedPreviousSessionID,
|
||||
)
|
||||
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 actualPreviousSessionID != expectedPreviousSessionID {
|
||||
return nil, fmt.Errorf(
|
||||
"load session config: session file %q: previous_session_id mismatch: --previous-session-id %q does not match previous_session_id %q",
|
||||
label,
|
||||
expectedPreviousSessionID,
|
||||
actualPreviousSessionID,
|
||||
)
|
||||
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 cfg, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load loads and resolves combined pipeline, campaign, and session configuration.
|
||||
@@ -252,9 +231,18 @@ func ResolveLoadedPipelineCampaign(loaded LoadedPipelineCampaign, sessionPath st
|
||||
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 {
|
||||
@@ -356,6 +344,22 @@ func mergeCampaignSession(campaignCfg *CampaignConfig, sessionCfg *SessionConfig
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user