2026-09-07 04:02:21 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
2026-11-17 22:15:05 +00:00
|
|
|
"encoding/json"
|
2026-09-07 04:02:21 +00:00
|
|
|
"fmt"
|
2026-11-17 22:15:05 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2026-11-20 15:22:57 +00:00
|
|
|
"strings"
|
2026-09-07 04:02:21 +00:00
|
|
|
|
2026-11-17 21:59:19 +00:00
|
|
|
configkit "go.maximumdirect.net/configkit"
|
2026-09-07 04:02:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const maxConfigDocumentBytes int64 = 8 << 20
|
|
|
|
|
|
2026-11-17 22:15:05 +00:00
|
|
|
type documentResolution struct {
|
|
|
|
|
campaign *configkit.Result[CampaignConfig]
|
|
|
|
|
session *configkit.Result[SessionConfig]
|
|
|
|
|
sources map[string]string
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:17:29 +00:00
|
|
|
// ConfigKitResult returns the campaign's actual completed native load result.
|
|
|
|
|
// Constructed campaign values have no result.
|
|
|
|
|
func (cfg *CampaignConfig) ConfigKitResult() *configkit.Result[CampaignConfig] {
|
|
|
|
|
if cfg == nil || cfg.resolution == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return cfg.resolution.campaign
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:24:20 +00:00
|
|
|
// ConfigKitResult returns the session's actual completed native load result.
|
|
|
|
|
// Constructed session values have no result.
|
|
|
|
|
func (cfg *SessionConfig) ConfigKitResult() *configkit.Result[SessionConfig] {
|
|
|
|
|
if cfg == nil || cfg.resolution == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return cfg.resolution.session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sessionFieldExplicit(cfg *SessionConfig, name string) bool {
|
|
|
|
|
result := cfg.ConfigKitResult()
|
|
|
|
|
return result != nil && result.Metadata().IsExplicit(configkit.Keys("inputs", name))
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 22:15:05 +00:00
|
|
|
func documentResource(resolution *configkit.Resolution, path configkit.Path, value *string, invocationDir string, sources map[string]string) {
|
|
|
|
|
if info, ok := resolution.EntryInfo(path); ok && len(info.Origin.Locations) == 1 {
|
|
|
|
|
if source, ok := resolution.Source(info.Origin.Locations[0].SourceID); ok {
|
|
|
|
|
name := source.Path
|
|
|
|
|
if name == "" {
|
|
|
|
|
name = source.Label
|
|
|
|
|
}
|
|
|
|
|
sources[path.String()] = name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
resolveHostResource(resolution, path, value, invocationDir)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 15:22:57 +00:00
|
|
|
func campaignDefinition(invocationDir string, sources map[string]string, expected string) configkit.Definition[CampaignConfig] {
|
2026-11-17 22:15:05 +00:00
|
|
|
return configkit.Definition[CampaignConfig]{Resolve: func(cfg *CampaignConfig, resolution *configkit.Resolution) error {
|
|
|
|
|
documentResource(resolution, configkit.Keys("session_template_file"), &cfg.SessionTemplateFile, invocationDir, sources)
|
|
|
|
|
inputs := configkit.Keys("inputs")
|
|
|
|
|
for _, field := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
value *string
|
|
|
|
|
}{
|
|
|
|
|
{"speakers_file", &cfg.Inputs.SpeakersFile}, {"autocorrect_file", &cfg.Inputs.AutocorrectFile},
|
|
|
|
|
{"glossary_file", &cfg.Inputs.GlossaryFile}, {"party_file", &cfg.Inputs.PartyFile},
|
|
|
|
|
{"spell_catalog_file", &cfg.Inputs.SpellCatalogFile},
|
|
|
|
|
} {
|
|
|
|
|
documentResource(resolution, inputs.Key(field.name), field.value, invocationDir, sources)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2026-11-20 15:22:57 +00:00
|
|
|
}, Validate: func(cfg *CampaignConfig, v *configkit.Validation) {
|
|
|
|
|
validateCampaignConfigKit(cfg, v)
|
|
|
|
|
validateIdentity(v, "campaign_id", expected, CampaignID(cfg))
|
|
|
|
|
}}
|
2026-11-17 22:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:24:20 +00:00
|
|
|
func sessionDefinition(invocationDir string, sources map[string]string, label string, opts SessionLoadOptions) configkit.Definition[SessionConfig] {
|
|
|
|
|
hasSource := false
|
2026-11-17 22:15:05 +00:00
|
|
|
return configkit.Definition[SessionConfig]{Resolve: func(cfg *SessionConfig, resolution *configkit.Resolution) error {
|
2026-11-20 04:24:20 +00:00
|
|
|
info, ok := resolution.EntryInfo(configkit.Keys())
|
|
|
|
|
hasSource = ok && len(info.Origin.Locations) > 0
|
|
|
|
|
if !hasSource {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-11-17 22:15:05 +00:00
|
|
|
inputs := configkit.Keys("inputs")
|
|
|
|
|
for _, field := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
value *string
|
|
|
|
|
}{
|
|
|
|
|
{"audio_dir", &cfg.Inputs.AudioDir}, {"speakers_file", &cfg.Inputs.SpeakersFile},
|
|
|
|
|
{"autocorrect_file", &cfg.Inputs.AutocorrectFile}, {"glossary_file", &cfg.Inputs.GlossaryFile},
|
|
|
|
|
{"spell_catalog_file", &cfg.Inputs.SpellCatalogFile},
|
|
|
|
|
} {
|
|
|
|
|
documentResource(resolution, inputs.Key(field.name), field.value, invocationDir, sources)
|
|
|
|
|
}
|
|
|
|
|
for index := range cfg.Inputs.AudioFiles {
|
|
|
|
|
documentResource(resolution, inputs.Key("audio_files").Index(index), &cfg.Inputs.AudioFiles[index], invocationDir, sources)
|
|
|
|
|
}
|
2026-11-20 04:24:20 +00:00
|
|
|
applyCampaignSessionDefaults(cfg, resolution, opts)
|
2026-11-17 22:15:05 +00:00
|
|
|
return nil
|
2026-11-20 04:24:20 +00:00
|
|
|
}, Validate: func(cfg *SessionConfig, validation *configkit.Validation) {
|
|
|
|
|
// Optional discovery with no selected document leaves acquisition to
|
|
|
|
|
// the caller's next source. There is no session to validate yet.
|
|
|
|
|
if !hasSource {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-20 15:22:57 +00:00
|
|
|
validateIdentity(validation, "session_id", strings.TrimSpace(opts.SessionID), strings.TrimSpace(cfg.SessionID))
|
|
|
|
|
validateIdentity(validation, "previous_session_id", strings.TrimSpace(opts.PreviousSessionID), strings.TrimSpace(cfg.PreviousSessionID))
|
|
|
|
|
if opts.Campaign != nil {
|
|
|
|
|
validateIdentity(validation, "campaign", CampaignID(opts.Campaign), cfg.Campaign)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:24:20 +00:00
|
|
|
data, err := json.Marshal(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
validation.Error(configkit.Keys(), "cannot inspect session configuration")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := rejectSessionTemplatePlaceholders(label, string(data)); err != nil {
|
|
|
|
|
validation.Error(configkit.Keys(), err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if path, err := sessionConfigurationError(cfg); err != nil {
|
|
|
|
|
validation.Error(path, err.Error())
|
|
|
|
|
}
|
2026-11-17 22:15:05 +00:00
|
|
|
}}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:24:20 +00:00
|
|
|
func applyCampaignSessionDefaults(cfg *SessionConfig, resolution *configkit.Resolution, opts SessionLoadOptions) {
|
|
|
|
|
if opts.Campaign == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
campaign := opts.Campaign
|
|
|
|
|
reason := fmt.Sprintf("default from campaign %q (%s)", CampaignID(campaign), opts.CampaignPath)
|
|
|
|
|
configkit.Default(resolution, configkit.Keys("campaign"), &cfg.Campaign, CampaignID(campaign), reason)
|
|
|
|
|
for _, field := range []struct {
|
|
|
|
|
name, value string
|
|
|
|
|
target *string
|
|
|
|
|
}{
|
|
|
|
|
{"speakers_file", campaign.Inputs.SpeakersFile, &cfg.Inputs.SpeakersFile},
|
|
|
|
|
{"autocorrect_file", campaign.Inputs.AutocorrectFile, &cfg.Inputs.AutocorrectFile},
|
|
|
|
|
{"glossary_file", campaign.Inputs.GlossaryFile, &cfg.Inputs.GlossaryFile},
|
|
|
|
|
{"spell_catalog_file", campaign.Inputs.SpellCatalogFile, &cfg.Inputs.SpellCatalogFile},
|
|
|
|
|
} {
|
|
|
|
|
if field.value != "" {
|
|
|
|
|
configkit.Default(resolution, configkit.Keys("inputs", field.name), field.target, field.value, reason)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 15:22:57 +00:00
|
|
|
// LoadCampaignWithIdentity validates an optional asserted identity within the native load.
|
|
|
|
|
func LoadCampaignWithIdentity(path, expected string) (*CampaignConfig, error) {
|
2026-11-17 22:15:05 +00:00
|
|
|
invocationDir, err := os.Getwd()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
sources := make(map[string]string)
|
2026-11-20 15:22:57 +00:00
|
|
|
result, err := configkit.Load(configkit.File(path), configkit.Options[CampaignConfig]{Imports: true, Definition: campaignDefinition(invocationDir, sources, expected)})
|
2026-09-07 04:02:21 +00:00
|
|
|
if err != nil {
|
2026-11-20 15:22:57 +00:00
|
|
|
return nil, fmt.Errorf("load campaign config: %w", err)
|
2026-09-07 04:02:21 +00:00
|
|
|
}
|
2026-11-20 04:17:29 +00:00
|
|
|
cfg := &result.Config
|
2026-11-17 22:15:05 +00:00
|
|
|
cfg.resolution = &documentResolution{campaign: result, sources: sources}
|
2026-11-20 04:17:29 +00:00
|
|
|
return cfg, nil
|
2026-09-07 04:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-17 22:15:05 +00:00
|
|
|
func loadSessionSource(source configkit.Source, label string, opts SessionLoadOptions, imports bool) (*SessionConfig, string, error) {
|
2026-11-18 04:06:29 +00:00
|
|
|
return loadSessionSourceWithFilesystem(source, label, opts, imports, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LoadSessionAuthoringBytes resolves a rendered local document before moving or
|
|
|
|
|
// publishing it. The path supplies its authoring base without writing that file.
|
|
|
|
|
func LoadSessionAuthoringBytes(path string, data []byte, opts SessionLoadOptions) (*SessionConfig, error) {
|
|
|
|
|
absolute, err := filepath.Abs(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
filesystem := &configkit.FilesystemContext{BaseDir: filepath.Dir(absolute), Root: string(filepath.Separator)}
|
|
|
|
|
cfg, _, err := loadSessionSourceWithFilesystem(configkit.Bytes(absolute, data), absolute, opts, true, filesystem)
|
|
|
|
|
return cfg, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadSessionSourceWithFilesystem(source configkit.Source, label string, opts SessionLoadOptions, imports bool, filesystem *configkit.FilesystemContext) (*SessionConfig, string, error) {
|
2026-11-17 22:15:05 +00:00
|
|
|
invocationDir, err := os.Getwd()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
2026-11-18 04:06:29 +00:00
|
|
|
if filesystem != nil {
|
|
|
|
|
invocationDir = filesystem.BaseDir
|
|
|
|
|
}
|
2026-11-17 22:15:05 +00:00
|
|
|
sources := make(map[string]string)
|
2026-11-20 04:24:20 +00:00
|
|
|
result, err := configkit.Load(source, configkit.Options[SessionConfig]{Imports: imports, Filesystem: filesystem, Definition: sessionDefinition(invocationDir, sources, label, opts)})
|
2026-11-17 22:15:05 +00:00
|
|
|
if err != nil {
|
2026-11-20 15:22:57 +00:00
|
|
|
return nil, "", fmt.Errorf("load session config: %w", err)
|
2026-11-17 22:15:05 +00:00
|
|
|
}
|
|
|
|
|
acquired := result.Sources()
|
|
|
|
|
if len(acquired) == 0 {
|
|
|
|
|
return nil, "", nil
|
2026-09-07 04:02:21 +00:00
|
|
|
}
|
2026-11-17 22:15:05 +00:00
|
|
|
selected := acquired[0].Path
|
|
|
|
|
if selected == "" {
|
|
|
|
|
selected = label
|
|
|
|
|
}
|
2026-11-20 04:24:20 +00:00
|
|
|
cfg := &result.Config
|
2026-11-17 22:15:05 +00:00
|
|
|
cfg.resolution = &documentResolution{session: result, sources: sources}
|
2026-11-20 04:24:20 +00:00
|
|
|
return cfg, filepath.Clean(selected), nil
|
2026-11-17 22:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DiscoverSession loads the first selected local candidate, or returns nil when
|
|
|
|
|
// none exists. A selected but invalid source is an error.
|
|
|
|
|
func DiscoverSession(candidates []string, opts SessionLoadOptions) (*SessionConfig, string, error) {
|
|
|
|
|
return loadSessionSource(configkit.SearchConfig("narratio", configkit.SearchPaths(candidates...)), "", opts, true)
|
2026-09-07 04:02:21 +00:00
|
|
|
}
|
2026-11-20 15:22:57 +00:00
|
|
|
|
|
|
|
|
func validateIdentity(v *configkit.Validation, field, expected, actual string) {
|
|
|
|
|
if expected != "" && actual == "" {
|
|
|
|
|
v.Error(configkit.Keys(field), fmt.Sprintf("%s is required when %q is selected", field, expected))
|
|
|
|
|
} else if expected != "" && actual != expected {
|
|
|
|
|
v.Error(configkit.Keys(field), fmt.Sprintf("%s mismatch: selected %q does not match %q", field, expected, actual))
|
|
|
|
|
}
|
|
|
|
|
}
|