2026-05-23 14:03:31 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-11-20 06:32:47 +00:00
|
|
|
|
2026-05-23 14:03:31 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"regexp"
|
|
|
|
|
"sort"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func buildSessionYAML(campaign, sessionID, previousSessionID, date, title, audioS3Prefix, audioDir string) ([]byte, error) {
|
|
|
|
|
if strings.TrimSpace(date) == "" && regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(strings.TrimSpace(sessionID)) {
|
|
|
|
|
date = strings.TrimSpace(sessionID)
|
|
|
|
|
}
|
|
|
|
|
type audioS3 struct {
|
|
|
|
|
Prefix string `yaml:"prefix"`
|
|
|
|
|
}
|
|
|
|
|
type inputs struct {
|
|
|
|
|
AudioDir string `yaml:"audio_dir,omitempty"`
|
|
|
|
|
AudioS3 *audioS3 `yaml:"audio_s3,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
type sessionYAML struct {
|
|
|
|
|
Campaign string `yaml:"campaign"`
|
|
|
|
|
SessionID string `yaml:"session_id"`
|
|
|
|
|
PreviousSessionID string `yaml:"previous_session_id,omitempty"`
|
|
|
|
|
Date string `yaml:"date,omitempty"`
|
|
|
|
|
Title string `yaml:"title,omitempty"`
|
|
|
|
|
Inputs inputs `yaml:"inputs"`
|
|
|
|
|
}
|
|
|
|
|
in := inputs{AudioDir: strings.TrimSpace(audioDir)}
|
|
|
|
|
if in.AudioDir == "" {
|
|
|
|
|
prefix := strings.TrimSpace(audioS3Prefix)
|
|
|
|
|
if prefix == "" {
|
|
|
|
|
prefix = "audio/"
|
|
|
|
|
}
|
|
|
|
|
in.AudioS3 = &audioS3{Prefix: prefix}
|
|
|
|
|
}
|
|
|
|
|
data, err := yaml.Marshal(sessionYAML{
|
|
|
|
|
Campaign: strings.TrimSpace(campaign),
|
|
|
|
|
SessionID: strings.TrimSpace(sessionID),
|
|
|
|
|
PreviousSessionID: strings.TrimSpace(previousSessionID),
|
|
|
|
|
Date: strings.TrimSpace(date),
|
|
|
|
|
Title: strings.TrimSpace(title),
|
|
|
|
|
Inputs: in,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return data, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type sessionInitInput struct {
|
|
|
|
|
Campaign string
|
|
|
|
|
CampaignPath string
|
|
|
|
|
TemplateFile string
|
|
|
|
|
SessionID string
|
|
|
|
|
PreviousSessionID string
|
|
|
|
|
Date string
|
|
|
|
|
Title string
|
|
|
|
|
AudioS3Prefix string
|
|
|
|
|
AudioDir string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func buildSessionInitYAML(in sessionInitInput) ([]byte, error) {
|
|
|
|
|
if strings.TrimSpace(in.TemplateFile) == "" {
|
|
|
|
|
return buildSessionYAML(in.Campaign, in.SessionID, in.PreviousSessionID, in.Date, in.Title, in.AudioS3Prefix, in.AudioDir)
|
|
|
|
|
}
|
|
|
|
|
templatePath := resolveSessionInitTemplatePath(in.CampaignPath, in.TemplateFile)
|
|
|
|
|
templateBytes, err := os.ReadFile(templatePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("read session template %q: %w", templatePath, err)
|
|
|
|
|
}
|
|
|
|
|
rendered, err := renderSessionInitTemplate(string(templateBytes), in)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("render session template %q: %w", templatePath, err)
|
|
|
|
|
}
|
|
|
|
|
return []byte(rendered), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resolveSessionInitTemplatePath(campaignPath, templateFile string) string {
|
|
|
|
|
templateFile = strings.TrimSpace(templateFile)
|
|
|
|
|
if filepath.IsAbs(templateFile) {
|
|
|
|
|
return filepath.Clean(templateFile)
|
|
|
|
|
}
|
|
|
|
|
return filepath.Clean(filepath.Join(filepath.Dir(campaignPath), templateFile))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sessionInitTemplatePattern = regexp.MustCompile(`\{\{\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\}\}`)
|
|
|
|
|
|
|
|
|
|
func renderSessionInitTemplate(content string, in sessionInitInput) (string, error) {
|
|
|
|
|
values := map[string]string{
|
|
|
|
|
"session_id": strings.TrimSpace(in.SessionID),
|
|
|
|
|
"previous_session_id": strings.TrimSpace(in.PreviousSessionID),
|
|
|
|
|
"date": strings.TrimSpace(in.Date),
|
|
|
|
|
"title": strings.TrimSpace(in.Title),
|
|
|
|
|
"audio_s3_prefix": strings.TrimSpace(in.AudioS3Prefix),
|
|
|
|
|
"audio_dir": strings.TrimSpace(in.AudioDir),
|
|
|
|
|
}
|
|
|
|
|
used := map[string]struct{}{}
|
|
|
|
|
unknown := map[string]struct{}{}
|
|
|
|
|
missing := map[string]struct{}{}
|
|
|
|
|
rendered := sessionInitTemplatePattern.ReplaceAllStringFunc(content, func(match string) string {
|
|
|
|
|
parts := sessionInitTemplatePattern.FindStringSubmatch(match)
|
|
|
|
|
if len(parts) < 2 {
|
|
|
|
|
return match
|
|
|
|
|
}
|
|
|
|
|
name := parts[1]
|
|
|
|
|
value, ok := values[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
unknown[name] = struct{}{}
|
|
|
|
|
return match
|
|
|
|
|
}
|
|
|
|
|
used[name] = struct{}{}
|
|
|
|
|
if value == "" {
|
|
|
|
|
missing[name] = struct{}{}
|
|
|
|
|
return match
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
})
|
|
|
|
|
if len(unknown) > 0 {
|
|
|
|
|
return "", fmt.Errorf("unsupported template variable(s): %s", sortedStringSet(unknown))
|
|
|
|
|
}
|
|
|
|
|
if len(missing) > 0 {
|
|
|
|
|
return "", fmt.Errorf("missing required template variable value(s): %s", sortedStringSet(missing))
|
|
|
|
|
}
|
|
|
|
|
unused := map[string]struct{}{}
|
|
|
|
|
for _, name := range []string{"previous_session_id", "date", "title", "audio_s3_prefix", "audio_dir"} {
|
|
|
|
|
if values[name] == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if _, ok := used[name]; !ok {
|
|
|
|
|
unused[name] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(unused) > 0 {
|
|
|
|
|
return "", fmt.Errorf("unused template variable value(s): %s", sortedStringSet(unused))
|
|
|
|
|
}
|
|
|
|
|
return rendered, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sortedStringSet(set map[string]struct{}) string {
|
|
|
|
|
items := make([]string, 0, len(set))
|
|
|
|
|
for item := range set {
|
|
|
|
|
items = append(items, item)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(items)
|
|
|
|
|
return strings.Join(items, ", ")
|
|
|
|
|
}
|