Complete session defaults within the ConfigKit lifecycle
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -69,8 +68,10 @@ func LoadSession(path string) (*SessionConfig, error) {
|
||||
return LoadSessionWithOptions(path, SessionLoadOptions{})
|
||||
}
|
||||
|
||||
// SessionLoadOptions configures expected session identity checks.
|
||||
// SessionLoadOptions supplies campaign defaults and expected session identities.
|
||||
type SessionLoadOptions struct {
|
||||
Campaign *CampaignConfig
|
||||
CampaignPath string
|
||||
SessionID string
|
||||
PreviousSessionID string
|
||||
Overlay *string
|
||||
@@ -89,19 +90,35 @@ func LoadSessionBytesWithOptions(label string, data []byte, opts SessionLoadOpti
|
||||
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))
|
||||
// SessionIdentityError identifies an explicit identity assertion that the
|
||||
// completed session configuration does not satisfy.
|
||||
type SessionIdentityError struct {
|
||||
Source string
|
||||
Field string
|
||||
Expected string
|
||||
Actual string
|
||||
}
|
||||
|
||||
func (e *SessionIdentityError) Error() string {
|
||||
if e.Actual == "" {
|
||||
return fmt.Sprintf("load session config: session file %q: %s is required when %q is selected", e.Source, e.Field, e.Expected)
|
||||
}
|
||||
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 fmt.Sprintf("load session config: session file %q: %s mismatch: selected %q does not match %q", e.Source, e.Field, e.Expected, e.Actual)
|
||||
}
|
||||
|
||||
func validateSessionIdentity(label string, cfg *SessionConfig, opts SessionLoadOptions) error {
|
||||
for _, assertion := range []struct{ field, expected, actual string }{
|
||||
{"session_id", opts.SessionID, cfg.SessionID},
|
||||
{"previous_session_id", opts.PreviousSessionID, cfg.PreviousSessionID},
|
||||
} {
|
||||
expected := strings.TrimSpace(assertion.expected)
|
||||
if expected != "" && strings.TrimSpace(assertion.actual) != expected {
|
||||
return &SessionIdentityError{Source: label, Field: assertion.field, Expected: expected, Actual: assertion.actual}
|
||||
}
|
||||
}
|
||||
if opts.Campaign != nil && cfg.Campaign != CampaignID(opts.Campaign) {
|
||||
return &SessionIdentityError{Source: label, Field: "campaign", Expected: CampaignID(opts.Campaign), Actual: cfg.Campaign}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -192,6 +209,8 @@ func LoadPipelineCampaignWithParty(pipelinePath string, pipeline *PipelineConfig
|
||||
// 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) {
|
||||
sessionOpts.Campaign = loaded.Campaign
|
||||
sessionOpts.CampaignPath = loaded.CampaignPath
|
||||
sessionCfg, err := LoadSessionWithOptions(sessionPath, sessionOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -230,20 +249,13 @@ func ResolveLoadedPipelineCampaign(loaded LoadedPipelineCampaign, sessionPath st
|
||||
SessionSource: sessionSource,
|
||||
Party: loaded.Party,
|
||||
}
|
||||
campaignDocument := *loaded.Campaign
|
||||
cfg.CampaignDocument = &campaignDocument
|
||||
cfg.CampaignDocument = loaded.Campaign
|
||||
cfg.SessionDocument = sessionCfg
|
||||
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)
|
||||
stableInputs, err := deriveStableInputs(loaded.Campaign, sessionCfg, loaded.CampaignPath, sessionPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -284,86 +296,44 @@ func campaignSessionPaths(paths ...string) (campaignPath, sessionPath string, er
|
||||
return campaignPath, sessionPath, nil
|
||||
}
|
||||
|
||||
func mergeCampaignSession(campaignCfg *CampaignConfig, sessionCfg *SessionConfig, campaignPath, sessionPath string) (ResolvedStableInputs, error) {
|
||||
func deriveStableInputs(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,
|
||||
),
|
||||
if sessionCfg.Campaign != CampaignID(campaignCfg) {
|
||||
return ResolvedStableInputs{}, &SessionIdentityError{Source: sessionPath, Field: "campaign", Expected: CampaignID(campaignCfg), Actual: sessionCfg.Campaign}
|
||||
}
|
||||
stable := ResolvedStableInputs{}
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
input *ResolvedInputFile
|
||||
name, value string
|
||||
target *ResolvedInputFile
|
||||
campaignOnly bool
|
||||
}{
|
||||
{"speakers_file", &stable.SpeakersFile}, {"autocorrect_file", &stable.AutocorrectFile},
|
||||
{"glossary_file", &stable.GlossaryFile}, {"party_file", &stable.PartyFile},
|
||||
{"spell_catalog_file", &stable.SpellCatalogFile},
|
||||
{"speakers_file", sessionCfg.Inputs.SpeakersFile, &stable.SpeakersFile, false},
|
||||
{"autocorrect_file", sessionCfg.Inputs.AutocorrectFile, &stable.AutocorrectFile, false},
|
||||
{"glossary_file", sessionCfg.Inputs.GlossaryFile, &stable.GlossaryFile, false},
|
||||
{"party_file", campaignCfg.Inputs.PartyFile, &stable.PartyFile, true},
|
||||
{"spell_catalog_file", sessionCfg.Inputs.SpellCatalogFile, &stable.SpellCatalogFile, false},
|
||||
} {
|
||||
metadata := campaignCfg.resolution
|
||||
if field.input.Source == "session_config" {
|
||||
metadata = sessionCfg.resolution
|
||||
reference := ResolvedInputFile{Path: field.value, ConfigPath: sessionPath, Source: "session_config"}
|
||||
metadata := sessionCfg.resolution
|
||||
fromCampaign := field.campaignOnly
|
||||
if sessionCfg.ConfigKitResult() != nil && !sessionFieldExplicit(sessionCfg, field.name) {
|
||||
fromCampaign = true
|
||||
}
|
||||
if fromCampaign {
|
||||
reference.ConfigPath = campaignPath
|
||||
reference.Source = "campaign_config"
|
||||
metadata = campaignCfg.resolution
|
||||
}
|
||||
if metadata != nil && metadata.sources["/inputs/"+field.name] != "" {
|
||||
field.input.ConfigPath = metadata.sources["/inputs/"+field.name]
|
||||
reference.ConfigPath = metadata.sources["/inputs/"+field.name]
|
||||
}
|
||||
*field.target = reference
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -375,21 +345,6 @@ func CampaignID(cfg *CampaignConfig) string {
|
||||
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*\}\}`)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user