2026-11-17 22:21:55 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2026-11-20 13:38:21 +00:00
|
|
|
configkit "go.maximumdirect.net/configkit"
|
2026-11-17 22:21:55 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type renderedCampaignInputs struct {
|
2026-11-27 16:19:11 +00:00
|
|
|
SpeakersFile string `yaml:"speakers_file,omitempty"`
|
|
|
|
|
AutocorrectFile string `yaml:"autocorrect_file,omitempty"`
|
|
|
|
|
PartyFile string `yaml:"party_file,omitempty"`
|
|
|
|
|
SpellCatalogFile string `yaml:"spell_catalog_file,omitempty"`
|
|
|
|
|
Extra map[string]any `yaml:",inline"`
|
2026-11-17 22:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RenderCampaignDocument serializes the loaded effective campaign document.
|
|
|
|
|
func RenderCampaignDocument(cfg *Config) ([]byte, error) {
|
|
|
|
|
if cfg == nil {
|
|
|
|
|
return nil, fmt.Errorf("campaign document is required")
|
|
|
|
|
}
|
|
|
|
|
doc := cfg.CampaignDocument
|
|
|
|
|
if doc == nil {
|
|
|
|
|
doc = cfg.Campaign
|
|
|
|
|
}
|
|
|
|
|
if doc == nil {
|
|
|
|
|
return nil, fmt.Errorf("campaign document is required")
|
|
|
|
|
}
|
|
|
|
|
value := struct {
|
|
|
|
|
CampaignID string `yaml:"campaign_id"`
|
|
|
|
|
SessionTemplateFile string `yaml:"session_template_file,omitempty"`
|
|
|
|
|
Inputs renderedCampaignInputs `yaml:"inputs"`
|
|
|
|
|
}{doc.CampaignID, doc.SessionTemplateFile, renderedCampaignInputs{
|
2026-11-27 16:19:11 +00:00
|
|
|
doc.Inputs.SpeakersFile, doc.Inputs.AutocorrectFile, doc.Inputs.PartyFile, doc.Inputs.SpellCatalogFile, glossaryFields(doc.ConfigKitResult() != nil, campaignMetadata(doc), doc.Inputs.Setting, doc.Inputs.System, doc.Inputs.GlossaryFile, doc.Inputs.Glossary, false),
|
2026-11-17 22:21:55 +00:00
|
|
|
}}
|
|
|
|
|
return yaml.Marshal(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RenderSessionDocument serializes the session's own fields and actual identity.
|
|
|
|
|
func RenderSessionDocument(cfg *Config, sessionID string) ([]byte, error) {
|
|
|
|
|
if cfg == nil || cfg.Session == nil {
|
|
|
|
|
return nil, fmt.Errorf("session document is required")
|
|
|
|
|
}
|
|
|
|
|
doc := cfg.SessionDocument
|
|
|
|
|
if doc == nil {
|
|
|
|
|
doc = cfg.Session
|
|
|
|
|
}
|
|
|
|
|
id := cfg.Session.SessionID
|
|
|
|
|
if id == "" {
|
|
|
|
|
id = sessionID
|
|
|
|
|
}
|
2026-11-20 13:38:21 +00:00
|
|
|
result := doc.ConfigKitResult()
|
|
|
|
|
var metadata configkit.Metadata
|
|
|
|
|
if result != nil {
|
|
|
|
|
metadata = result.Metadata()
|
|
|
|
|
}
|
|
|
|
|
// Materialized sessions retain authored overrides and receiving-campaign defaults.
|
|
|
|
|
inputValue := func(name, value string) *string {
|
|
|
|
|
if result != nil {
|
|
|
|
|
if !metadata.IsExplicit(configkit.Keys("inputs", name)) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
} else if value == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return &value
|
|
|
|
|
}
|
2026-11-17 22:21:55 +00:00
|
|
|
inputs := struct {
|
|
|
|
|
AudioDir string `yaml:"audio_dir,omitempty"`
|
|
|
|
|
AudioFiles []string `yaml:"audio_files,omitempty"`
|
|
|
|
|
AudioS3 *SessionAudioS3Input `yaml:"audio_s3,omitempty"`
|
2026-11-20 13:38:21 +00:00
|
|
|
SpeakersFile *string `yaml:"speakers_file,omitempty"`
|
|
|
|
|
AutocorrectFile *string `yaml:"autocorrect_file,omitempty"`
|
2026-11-20 04:24:20 +00:00
|
|
|
SpellCatalogFile *string `yaml:"spell_catalog_file,omitempty"`
|
2026-11-27 16:19:11 +00:00
|
|
|
Extra map[string]any `yaml:",inline"`
|
2026-11-20 13:38:21 +00:00
|
|
|
}{doc.Inputs.AudioDir, doc.Inputs.AudioFiles, doc.Inputs.AudioS3,
|
|
|
|
|
inputValue("speakers_file", doc.Inputs.SpeakersFile),
|
|
|
|
|
inputValue("autocorrect_file", doc.Inputs.AutocorrectFile),
|
2026-11-27 16:19:11 +00:00
|
|
|
inputValue("spell_catalog_file", doc.Inputs.SpellCatalogFile), glossaryFields(result != nil, metadata, doc.Inputs.Setting, doc.Inputs.System, doc.Inputs.GlossaryFile, doc.Inputs.Glossary, true)}
|
2026-11-17 22:21:55 +00:00
|
|
|
value := struct {
|
|
|
|
|
SessionID string `yaml:"session_id"`
|
|
|
|
|
PreviousSessionID string `yaml:"previous_session_id,omitempty"`
|
|
|
|
|
Campaign string `yaml:"campaign,omitempty"`
|
|
|
|
|
Date string `yaml:"date,omitempty"`
|
|
|
|
|
Title string `yaml:"title,omitempty"`
|
|
|
|
|
Inputs any `yaml:"inputs"`
|
|
|
|
|
}{id, cfg.Session.PreviousSessionID, cfg.Session.Campaign, doc.Date, doc.Title, inputs}
|
|
|
|
|
return yaml.Marshal(value)
|
|
|
|
|
}
|
2026-11-27 16:19:11 +00:00
|
|
|
|
|
|
|
|
func campaignMetadata(cfg *CampaignConfig) configkit.Metadata {
|
|
|
|
|
if r := cfg.ConfigKitResult(); r != nil {
|
|
|
|
|
return r.Metadata()
|
|
|
|
|
}
|
|
|
|
|
return configkit.Metadata{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func glossaryFields(hasResult bool, metadata configkit.Metadata, setting, system, file *string, glossary map[string]any, authoredOnly bool) map[string]any {
|
|
|
|
|
result := map[string]any{}
|
|
|
|
|
for _, field := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
value *string
|
|
|
|
|
}{{"setting", setting}, {"system", system}, {"glossary_file", file}} {
|
|
|
|
|
path := configkit.Keys("inputs", field.name)
|
|
|
|
|
info, found := metadata.Lookup(path)
|
|
|
|
|
explicit := hasResult && metadata.IsExplicit(path)
|
|
|
|
|
if (authoredOnly && hasResult && !explicit) || (!explicit && field.value == nil) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if found && info.Null {
|
|
|
|
|
result[field.name] = nil
|
|
|
|
|
} else {
|
|
|
|
|
result[field.name] = OptionalString(field.value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
path := configkit.Keys("inputs", "glossary")
|
|
|
|
|
explicit := hasResult && metadata.IsExplicit(path)
|
|
|
|
|
if !(authoredOnly && hasResult && !explicit) && (explicit || glossary != nil) {
|
|
|
|
|
if glossary == nil {
|
|
|
|
|
result["glossary"] = nil
|
|
|
|
|
} else {
|
|
|
|
|
result["glossary"] = glossary
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|