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-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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func campaignDefinition(invocationDir string, sources map[string]string) configkit.Definition[CampaignConfig] {
|
|
|
|
|
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
|
|
|
|
|
}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sessionDefinition(invocationDir string, sources map[string]string) configkit.Definition[SessionConfig] {
|
|
|
|
|
return configkit.Definition[SessionConfig]{Resolve: func(cfg *SessionConfig, resolution *configkit.Resolution) error {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-07 04:02:21 +00:00
|
|
|
func loadCampaignWithConfigKit(path 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)
|
|
|
|
|
result, err := configkit.Load(configkit.File(path), configkit.Options[CampaignConfig]{Imports: true, Definition: campaignDefinition(invocationDir, sources)})
|
2026-09-07 04:02:21 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, describeConfigKitLoadError("campaign", err)
|
|
|
|
|
}
|
2026-11-17 22:15:05 +00:00
|
|
|
cfg := result.Config
|
|
|
|
|
cfg.resolution = &documentResolution{campaign: result, sources: sources}
|
|
|
|
|
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) {
|
|
|
|
|
invocationDir, err := os.Getwd()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
|
|
|
|
sources := make(map[string]string)
|
|
|
|
|
result, err := configkit.Load(source, configkit.Options[SessionConfig]{Imports: imports, Definition: sessionDefinition(invocationDir, sources)})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, "", describeConfigKitLoadError("session", err)
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
cfg := result.Config
|
|
|
|
|
data, err := json.Marshal(cfg)
|
2026-09-07 04:02:21 +00:00
|
|
|
if err != nil {
|
2026-11-17 22:15:05 +00:00
|
|
|
return nil, "", fmt.Errorf("inspect session config: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if err := rejectSessionTemplatePlaceholders(selected, string(data)); err != nil {
|
|
|
|
|
return nil, "", fmt.Errorf("load session config: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if err := validateSessionIdentity(selected, &cfg, opts); err != nil {
|
|
|
|
|
return nil, "", err
|
2026-09-07 04:02:21 +00:00
|
|
|
}
|
2026-11-17 22:15:05 +00:00
|
|
|
cfg.resolution = &documentResolution{session: result, sources: sources}
|
|
|
|
|
return &cfg, filepath.Clean(selected), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|