2026-11-17 22:21:55 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
2026-11-30 00:14:07 +00:00
|
|
|
"bytes"
|
2026-11-17 22:21:55 +00:00
|
|
|
"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"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
|
}
|
2026-11-30 00:14:07 +00:00
|
|
|
if result := doc.ConfigKitResult(); result != nil {
|
|
|
|
|
var output bytes.Buffer
|
|
|
|
|
if err := result.WriteEffective(&output); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return output.Bytes(), nil
|
|
|
|
|
}
|
|
|
|
|
inputs := map[string]any{"speakers_file": doc.Inputs.SpeakersFile}
|
|
|
|
|
if doc.Inputs.GlossaryFile != nil {
|
|
|
|
|
inputs["glossary_file"] = *doc.Inputs.GlossaryFile
|
|
|
|
|
}
|
|
|
|
|
if doc.Inputs.Glossary != nil {
|
|
|
|
|
inputs["glossary"] = doc.Inputs.Glossary
|
|
|
|
|
}
|
|
|
|
|
if doc.Inputs.Autocorrect != nil {
|
|
|
|
|
inputs["autocorrect"] = doc.Inputs.Autocorrect
|
|
|
|
|
}
|
|
|
|
|
narratio := map[string]any{"inputs": inputs}
|
|
|
|
|
if doc.SessionTemplateFile != "" {
|
|
|
|
|
narratio["session_template_file"] = doc.SessionTemplateFile
|
|
|
|
|
}
|
|
|
|
|
value := map[string]any{"schema_version": "maximumdirect.rpg.campaign.v1", "id": doc.CampaignID, "references": map[string]any{"party": doc.Inputs.PartyFile}, "narratio": narratio}
|
|
|
|
|
if doc.Inputs.Setting != nil {
|
|
|
|
|
value["setting"] = *doc.Inputs.Setting
|
|
|
|
|
}
|
|
|
|
|
if doc.Inputs.System != nil {
|
|
|
|
|
value["system"] = *doc.Inputs.System
|
|
|
|
|
}
|
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()
|
|
|
|
|
}
|
2026-11-30 00:31:36 +00:00
|
|
|
// Materialized sessions carry the resolved values needed independently of the campaign.
|
|
|
|
|
inputValue := func(value string) *string {
|
|
|
|
|
if value == "" {
|
2026-11-20 13:38:21 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return &value
|
|
|
|
|
}
|
2026-11-17 22:21:55 +00:00
|
|
|
inputs := struct {
|
2026-11-30 00:14:07 +00:00
|
|
|
AudioDir string `yaml:"audio_dir,omitempty"`
|
|
|
|
|
AudioFiles []string `yaml:"audio_files,omitempty"`
|
|
|
|
|
AudioS3 *SessionAudioS3Input `yaml:"audio_s3,omitempty"`
|
|
|
|
|
SpeakersFile *string `yaml:"speakers_file,omitempty"`
|
|
|
|
|
Extra map[string]any `yaml:",inline"`
|
2026-11-20 13:38:21 +00:00
|
|
|
}{doc.Inputs.AudioDir, doc.Inputs.AudioFiles, doc.Inputs.AudioS3,
|
2026-11-30 00:31:36 +00:00
|
|
|
inputValue(doc.Inputs.SpeakersFile),
|
|
|
|
|
glossaryFields(result != nil, metadata, doc.Inputs.Setting, doc.Inputs.System, doc.Inputs.GlossaryFile, doc.Inputs.Glossary, false)}
|
2026-11-30 00:14:07 +00:00
|
|
|
autocorrectPath := configkit.Keys("inputs", "autocorrect")
|
|
|
|
|
if result != nil && metadata.IsExplicit(autocorrectPath) {
|
|
|
|
|
if info, ok := metadata.Lookup(autocorrectPath); ok && info.Null {
|
|
|
|
|
inputs.Extra["autocorrect"] = nil
|
|
|
|
|
} else {
|
|
|
|
|
inputs.Extra["autocorrect"] = doc.Inputs.Autocorrect
|
|
|
|
|
}
|
2026-11-30 00:31:36 +00:00
|
|
|
} else if doc.Inputs.Autocorrect != nil {
|
2026-11-30 00:14:07 +00:00
|
|
|
inputs.Extra["autocorrect"] = doc.Inputs.Autocorrect
|
|
|
|
|
}
|
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
|
|
|
|
2026-11-30 00:31:36 +00:00
|
|
|
// RenderAutocorrection produces the Seriatim rule-list envelope.
|
|
|
|
|
func RenderAutocorrection(rules []AutocorrectionRule) ([]byte, error) {
|
|
|
|
|
if rules == nil {
|
|
|
|
|
rules = []AutocorrectionRule{}
|
|
|
|
|
}
|
|
|
|
|
return yaml.Marshal(struct {
|
|
|
|
|
Autocorrect []AutocorrectionRule `yaml:"autocorrect"`
|
|
|
|
|
}{Autocorrect: rules})
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-27 16:19:11 +00:00
|
|
|
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
|
|
|
|
|
}
|