Share effective Scriptorium request context
This commit is contained in:
@@ -214,6 +214,8 @@ Check: `go test ./internal/adapters/scriptorium`; compile affected test doubles.
|
|||||||
|
|
||||||
## Stage 5: Share effective Scriptorium request context
|
## Stage 5: Share effective Scriptorium request context
|
||||||
|
|
||||||
|
**Status: Complete.**
|
||||||
|
|
||||||
**Purpose:** Ensure discovery, preview, generation, and fingerprint variables
|
**Purpose:** Ensure discovery, preview, generation, and fingerprint variables
|
||||||
agree without resolving future artifact files.
|
agree without resolving future artifact files.
|
||||||
|
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ type Runner interface {
|
|||||||
InspectRequirements(ctx context.Context, req RequirementsRequest) (RequirementsReport, error)
|
InspectRequirements(ctx context.Context, req RequirementsRequest) (RequirementsReport, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequirementsRequest selects a prompt and variables for read-only discovery.
|
// RequestContext holds the effective selections shared by Scriptorium operations.
|
||||||
type RequirementsRequest struct {
|
type RequestContext struct {
|
||||||
Binary string
|
Binary string
|
||||||
ConfigPath string
|
ConfigPath string
|
||||||
PromptID string
|
PromptID string
|
||||||
@@ -34,6 +34,9 @@ type RequirementsRequest struct {
|
|||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RequirementsRequest selects a prompt and variables for read-only discovery.
|
||||||
|
type RequirementsRequest = RequestContext
|
||||||
|
|
||||||
type RequirementsReport struct {
|
type RequirementsReport struct {
|
||||||
PromptID string `json:"prompt_id"`
|
PromptID string `json:"prompt_id"`
|
||||||
ProfileID string `json:"profile_id"`
|
ProfileID string `json:"profile_id"`
|
||||||
|
|||||||
@@ -431,85 +431,6 @@ func resolveScriptoriumOutputPath(paths artifacts.SessionPaths, configured strin
|
|||||||
return filepath.Join(paths.Root, rel), nil
|
return filepath.Join(paths.Root, rel), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveScriptoriumTimeout(topLevel, artifact string) (time.Duration, error) {
|
|
||||||
raw := strings.TrimSpace(artifact)
|
|
||||||
if raw == "" {
|
|
||||||
raw = strings.TrimSpace(topLevel)
|
|
||||||
}
|
|
||||||
if raw == "" {
|
|
||||||
raw = "10m"
|
|
||||||
}
|
|
||||||
d, err := time.ParseDuration(raw)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("parse duration %q: %w", raw, err)
|
|
||||||
}
|
|
||||||
if d <= 0 {
|
|
||||||
return 0, fmt.Errorf("duration must be > 0")
|
|
||||||
}
|
|
||||||
return d, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func buildScriptoriumVars(varsCfg map[string]any, session *config.SessionConfig) (map[string]string, error) {
|
|
||||||
if len(varsCfg) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
vars := map[string]string{}
|
|
||||||
keys := make([]string, 0, len(varsCfg))
|
|
||||||
for key := range varsCfg {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
sort.Strings(keys)
|
|
||||||
|
|
||||||
for _, key := range keys {
|
|
||||||
value := varsCfg[key]
|
|
||||||
switch typed := value.(type) {
|
|
||||||
case bool:
|
|
||||||
if !typed {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
derived, ok, err := deriveSessionVarValue(key, session)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if ok {
|
|
||||||
vars[key] = derived
|
|
||||||
}
|
|
||||||
case string:
|
|
||||||
vars[key] = typed
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("var %q has unsupported type %T", key, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(vars) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
return vars, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func deriveSessionVarValue(name string, session *config.SessionConfig) (string, bool, error) {
|
|
||||||
switch name {
|
|
||||||
case "session_id":
|
|
||||||
if session == nil || strings.TrimSpace(session.SessionID) == "" {
|
|
||||||
return "", false, nil
|
|
||||||
}
|
|
||||||
return strings.TrimSpace(session.SessionID), true, nil
|
|
||||||
case "session_date":
|
|
||||||
if session == nil || strings.TrimSpace(session.Date) == "" {
|
|
||||||
return "", false, nil
|
|
||||||
}
|
|
||||||
return strings.TrimSpace(session.Date), true, nil
|
|
||||||
case "campaign_name":
|
|
||||||
if session == nil || strings.TrimSpace(session.Campaign) == "" {
|
|
||||||
return "", false, nil
|
|
||||||
}
|
|
||||||
return strings.TrimSpace(session.Campaign), true, nil
|
|
||||||
case "previous_session_id":
|
|
||||||
return "", false, nil
|
|
||||||
default:
|
|
||||||
return "", false, fmt.Errorf("unsupported boolean var %q", name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func requireNonEmptyFile(path string, label string) error {
|
func requireNonEmptyFile(path string, label string) error {
|
||||||
data, err := readExternalResult(path, label)
|
data, err := readExternalResult(path, label)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ import (
|
|||||||
|
|
||||||
type analyzeArtifactPreparation struct {
|
type analyzeArtifactPreparation struct {
|
||||||
name string
|
name string
|
||||||
promptID string
|
requestContext scriptorium.RequestContext
|
||||||
profileID string
|
|
||||||
inputPaths map[string]string
|
inputPaths map[string]string
|
||||||
vars map[string]string
|
vars map[string]string
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
@@ -102,11 +101,11 @@ func prepareAnalyzeArtifact(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vars, err := buildScriptoriumVars(artifactCfg.Vars, env.Config.Session)
|
requestContext, err := AnalyzeScriptoriumContext(env.Config, artifactCfg, execution.SessionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("analyze: resolve vars for artifact %q: %w", artifactName, err)
|
return nil, fmt.Errorf("analyze: resolve scriptorium context for artifact %q: %w", artifactName, err)
|
||||||
}
|
}
|
||||||
vars = withScriptoriumStickySessionVar(vars, execution.SessionID)
|
vars := requestContext.Vars
|
||||||
|
|
||||||
canonicalOutputPath, err := resolveScriptoriumOutputPath(paths, artifactCfg.OutputPath)
|
canonicalOutputPath, err := resolveScriptoriumOutputPath(paths, artifactCfg.OutputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -130,16 +129,12 @@ func prepareAnalyzeArtifact(
|
|||||||
renderConfigPath = filepath.Join(runLayout.ConfigDir, "scriptorium."+artifactName+".render.generated.yml")
|
renderConfigPath = filepath.Join(runLayout.ConfigDir, "scriptorium."+artifactName+".render.generated.yml")
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout, err := resolveScriptoriumTimeout(env.Config.Pipeline.Scriptorium.Timeout, artifactCfg.Timeout)
|
timeout := requestContext.Timeout
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("analyze: resolve timeout for artifact %q: %w", artifactName, err)
|
|
||||||
}
|
|
||||||
renderEnabled := resolveRenderDebugEnabled(env.Config.Pipeline.Scriptorium.RenderDebug, artifactCfg.RenderDebug)
|
renderEnabled := resolveRenderDebugEnabled(env.Config.Pipeline.Scriptorium.RenderDebug, artifactCfg.RenderDebug)
|
||||||
|
|
||||||
return &analyzeArtifactPreparation{
|
return &analyzeArtifactPreparation{
|
||||||
name: artifactName,
|
name: artifactName,
|
||||||
promptID: artifactCfg.PromptID,
|
requestContext: requestContext,
|
||||||
profileID: artifactCfg.ProfileID,
|
|
||||||
inputPaths: inputPaths,
|
inputPaths: inputPaths,
|
||||||
vars: vars,
|
vars: vars,
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
@@ -187,11 +182,11 @@ func invokeAnalyzeRender(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
req := scriptorium.RenderArtifactRequest{
|
req := scriptorium.RenderArtifactRequest{
|
||||||
Binary: execution.Env.Config.Pipeline.Scriptorium.Binary,
|
Binary: prepared.requestContext.Binary,
|
||||||
ConfigPath: execution.Env.Config.Pipeline.Scriptorium.ConfigPath,
|
ConfigPath: prepared.requestContext.ConfigPath,
|
||||||
Credentials: execution.Env.Config.Pipeline.Scriptorium.Credentials,
|
Credentials: prepared.requestContext.Credentials,
|
||||||
PromptID: prepared.promptID,
|
PromptID: prepared.requestContext.PromptID,
|
||||||
ProfileID: prepared.profileID,
|
ProfileID: prepared.requestContext.ProfileID,
|
||||||
InputPaths: prepared.inputPaths,
|
InputPaths: prepared.inputPaths,
|
||||||
Vars: prepared.vars,
|
Vars: prepared.vars,
|
||||||
OutputPath: prepared.renderOutputPath,
|
OutputPath: prepared.renderOutputPath,
|
||||||
@@ -242,11 +237,11 @@ func invokeAnalyzeArtifact(
|
|||||||
prepared *analyzeArtifactPreparation,
|
prepared *analyzeArtifactPreparation,
|
||||||
) (analyzeArtifactInvocation, error) {
|
) (analyzeArtifactInvocation, error) {
|
||||||
req := scriptorium.RunArtifactRequest{
|
req := scriptorium.RunArtifactRequest{
|
||||||
Binary: execution.Env.Config.Pipeline.Scriptorium.Binary,
|
Binary: prepared.requestContext.Binary,
|
||||||
ConfigPath: execution.Env.Config.Pipeline.Scriptorium.ConfigPath,
|
ConfigPath: prepared.requestContext.ConfigPath,
|
||||||
Credentials: execution.Env.Config.Pipeline.Scriptorium.Credentials,
|
Credentials: prepared.requestContext.Credentials,
|
||||||
PromptID: prepared.promptID,
|
PromptID: prepared.requestContext.PromptID,
|
||||||
ProfileID: prepared.profileID,
|
ProfileID: prepared.requestContext.ProfileID,
|
||||||
InputPaths: prepared.inputPaths,
|
InputPaths: prepared.inputPaths,
|
||||||
Vars: prepared.vars,
|
Vars: prepared.vars,
|
||||||
OutputPath: prepared.runOutputPath,
|
OutputPath: prepared.runOutputPath,
|
||||||
|
|||||||
@@ -269,11 +269,10 @@ func effectiveAnalyzeFingerprintVariables(
|
|||||||
if execution.Env == nil || execution.Env.Config == nil {
|
if execution.Env == nil || execution.Env.Config == nil {
|
||||||
return nil, fmt.Errorf("analysis fingerprint requires resolved stage configuration")
|
return nil, fmt.Errorf("analysis fingerprint requires resolved stage configuration")
|
||||||
}
|
}
|
||||||
variables, err := buildScriptoriumVars(configured, execution.Env.Config.Session)
|
variables, err := effectiveScriptoriumVars(configured, execution.Env.Config.Session, execution.SessionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
variables = withScriptoriumStickySessionVar(variables, execution.SessionID)
|
|
||||||
keys := make([]string, 0, len(variables))
|
keys := make([]string, 0, len(variables))
|
||||||
for key := range variables {
|
for key := range variables {
|
||||||
keys = append(keys, key)
|
keys = append(keys, key)
|
||||||
|
|||||||
94
internal/stage/scriptorium_context_test.go
Normal file
94
internal/stage/scriptorium_context_test.go
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package stage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||||
|
configkit "go.maximumdirect.net/configkit"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAnalysisContextMatchesExpandedArtifactRequestsAndFingerprint(t *testing.T) {
|
||||||
|
env, m, fake := setupAnalyzeEnv(t)
|
||||||
|
paths := sessionPathsForEnv(env, m.SessionID)
|
||||||
|
writeAnalyzeFile(t, filepath.Join(paths.TranscriptsDir, "final.trimmed.json"), `{"segments":[]}`)
|
||||||
|
party, err := config.ParseParty([]byte("schema_version: narratio.party.v1\ncharacters:\n alice:\n player: {name: Player}\n character: {name: Alice, classes: [{name: wizard}]}\n"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
scriptorium := env.Config.Pipeline.Scriptorium
|
||||||
|
scriptorium.ConfigPath = "/tmp/scriptorium.yml"
|
||||||
|
scriptorium.Credentials = []configkit.Secret{configkit.SecretEnv("SYNTHETIC_KEY")}
|
||||||
|
scriptorium.RenderDebug = true
|
||||||
|
scriptorium.Artifacts = nil
|
||||||
|
scriptorium.ArtifactFamilies = map[string]config.ScriptoriumArtifactFamilyConfig{
|
||||||
|
"character": {Enabled: true, ForEach: "party.characters", PromptID: "dnd.character", ProfileID: "local", Timeout: "45s", OutputPathPattern: "artifacts/{character_id}.md", Inputs: map[string]config.ScriptoriumInputConfig{"transcript": {Source: "narratio.transcript.final_trimmed", Required: true}}, MemberVars: map[string]string{"character_name": "character.name"}, Vars: map[string]any{"session_id": "wrong", "session_date": true, "empty": "", "ignored": false}},
|
||||||
|
}
|
||||||
|
env.Config.Session.Date = "2026-05-03"
|
||||||
|
catalog, err := config.DeriveArtifactCatalog(env.Config.Pipeline, config.ResolvedParty{Canonical: party})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
env.Config.ArtifactCatalog = catalog
|
||||||
|
artifact := env.Config.ConcreteArtifacts()["character_alice"]
|
||||||
|
want, err := AnalyzeScriptoriumContext(env.Config, artifact, m.SessionID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if want.Timeout != 45*time.Second || want.Vars["character_name"] != "Alice" || want.Vars["session_date"] != "2026-05-03" || want.Vars["session_id"] != "narratio-session-2026-05-03" || want.Vars["empty"] != "" {
|
||||||
|
t.Fatalf("context = %+v", want)
|
||||||
|
}
|
||||||
|
if _, exists := want.Vars["ignored"]; exists {
|
||||||
|
t.Fatal("false variable unexpectedly supplied")
|
||||||
|
}
|
||||||
|
if _, err := (analyzeStage{}).Run(context.Background(), env, m); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(fake.RenderRequests) != 1 || len(fake.RunRequests) != 1 {
|
||||||
|
t.Fatalf("requests = %+v / %+v", fake.RenderRequests, fake.RunRequests)
|
||||||
|
}
|
||||||
|
render, run := fake.RenderRequests[0], fake.RunRequests[0]
|
||||||
|
if run.Binary != want.Binary || run.ConfigPath != want.ConfigPath || run.PromptID != want.PromptID || run.ProfileID != want.ProfileID || run.Timeout != want.Timeout || !reflect.DeepEqual(run.Vars, want.Vars) || !reflect.DeepEqual(run.Credentials, want.Credentials) || render.Binary != want.Binary || render.ConfigPath != want.ConfigPath || render.PromptID != want.PromptID || render.ProfileID != want.ProfileID || render.Timeout != want.Timeout || !reflect.DeepEqual(render.Vars, want.Vars) || !reflect.DeepEqual(render.Credentials, want.Credentials) {
|
||||||
|
t.Fatalf("context = %+v, render = %+v, run = %+v", want, render, run)
|
||||||
|
}
|
||||||
|
fingerprintVars, err := effectiveAnalyzeFingerprintVariables(artifact.Vars, analyzeExecutionContext{Env: env, SessionID: m.SessionID})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
got := make(map[string]string, len(fingerprintVars))
|
||||||
|
for _, variable := range fingerprintVars {
|
||||||
|
got[variable.Name] = variable.Value
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(got, want.Vars) {
|
||||||
|
t.Fatalf("fingerprint variables = %v, want %v", got, want.Vars)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrimContextMatchesPreviewAndGeneration(t *testing.T) {
|
||||||
|
env, m, scr, _ := setupTrimEnv(t)
|
||||||
|
paths := sessionPathsForEnv(env, m.SessionID)
|
||||||
|
normalized := filepath.Join(paths.TranscriptsDir, "final.json")
|
||||||
|
writeFile(t, normalized, `{"segments":[{"id":1},{"id":2}]}`)
|
||||||
|
setCurrentBuiltInEvidence(t, m, "normalize", artifacts.TranscriptOutputKindFinal, artifacts.ArtifactTranscriptFinal, normalized)
|
||||||
|
scr.BoundsBody = `{"trim_action":"trim","start_segment_id":1,"end_segment_id":2}`
|
||||||
|
env.Config.Pipeline.Trim.Bounds.RenderDebug = true
|
||||||
|
env.Config.Pipeline.Scriptorium.Credentials = []configkit.Secret{configkit.SecretEnv("SYNTHETIC_KEY")}
|
||||||
|
want, err := TrimScriptoriumContext(env.Config, m.SessionID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := (trimStage{}).Run(context.Background(), env, m); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(scr.RenderRequests) != 1 || len(scr.RunRequests) != 1 {
|
||||||
|
t.Fatalf("requests = %+v / %+v", scr.RenderRequests, scr.RunRequests)
|
||||||
|
}
|
||||||
|
render, run := scr.RenderRequests[0], scr.RunRequests[0]
|
||||||
|
if run.Binary != want.Binary || run.ConfigPath != want.ConfigPath || run.PromptID != want.PromptID || run.ProfileID != want.ProfileID || run.Timeout != want.Timeout || !reflect.DeepEqual(run.Vars, want.Vars) || !reflect.DeepEqual(run.Credentials, want.Credentials) || render.Binary != want.Binary || render.ConfigPath != want.ConfigPath || render.PromptID != want.PromptID || render.ProfileID != want.ProfileID || render.Timeout != want.Timeout || !reflect.DeepEqual(render.Vars, want.Vars) || !reflect.DeepEqual(render.Credentials, want.Credentials) {
|
||||||
|
t.Fatalf("context = %+v, render = %+v, run = %+v", want, render, run)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,52 @@
|
|||||||
package stage
|
package stage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AnalyzeScriptoriumContext resolves the context for a concrete artifact.
|
||||||
|
func AnalyzeScriptoriumContext(cfg *config.Config, artifact config.ScriptoriumArtifactConfig, sessionID string) (scriptorium.RequestContext, error) {
|
||||||
|
if cfg == nil || cfg.Pipeline == nil || cfg.Pipeline.Scriptorium == nil {
|
||||||
|
return scriptorium.RequestContext{}, fmt.Errorf("resolved scriptorium configuration is required")
|
||||||
|
}
|
||||||
|
vars, err := effectiveScriptoriumVars(artifact.Vars, cfg.Session, sessionID)
|
||||||
|
if err != nil {
|
||||||
|
return scriptorium.RequestContext{}, err
|
||||||
|
}
|
||||||
|
return scriptoriumContext(cfg.Pipeline.Scriptorium, artifact.PromptID, artifact.ProfileID, artifact.Timeout, vars)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TrimScriptoriumContext resolves the context for configured transcript bounds.
|
||||||
|
func TrimScriptoriumContext(cfg *config.Config, sessionID string) (scriptorium.RequestContext, error) {
|
||||||
|
if cfg == nil || cfg.Pipeline == nil || cfg.Pipeline.Scriptorium == nil || cfg.Pipeline.Trim == nil {
|
||||||
|
return scriptorium.RequestContext{}, fmt.Errorf("resolved scriptorium and trim configuration is required")
|
||||||
|
}
|
||||||
|
bounds := cfg.Pipeline.Trim.Bounds
|
||||||
|
return scriptoriumContext(cfg.Pipeline.Scriptorium, bounds.PromptID, bounds.ProfileID, bounds.Timeout, withScriptoriumStickySessionVar(nil, sessionID))
|
||||||
|
}
|
||||||
|
|
||||||
|
func scriptoriumContext(cfg *config.ScriptoriumConfig, prompt, profile, timeout string, vars map[string]string) (scriptorium.RequestContext, error) {
|
||||||
|
duration, err := resolveScriptoriumTimeout(cfg.Timeout, timeout)
|
||||||
|
if err != nil {
|
||||||
|
return scriptorium.RequestContext{}, err
|
||||||
|
}
|
||||||
|
return scriptorium.RequestContext{Binary: cfg.Binary, ConfigPath: cfg.ConfigPath, PromptID: prompt, ProfileID: profile, Vars: vars, Credentials: cfg.Credentials, Timeout: duration}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func effectiveScriptoriumVars(configured map[string]any, session *config.SessionConfig, sessionID string) (map[string]string, error) {
|
||||||
|
vars, err := buildScriptoriumVars(configured, session)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return withScriptoriumStickySessionVar(vars, sessionID), nil
|
||||||
|
}
|
||||||
|
|
||||||
func withScriptoriumStickySessionVar(vars map[string]string, sessionID string) map[string]string {
|
func withScriptoriumStickySessionVar(vars map[string]string, sessionID string) map[string]string {
|
||||||
out := make(map[string]string, len(vars)+1)
|
out := make(map[string]string, len(vars)+1)
|
||||||
for k, v := range vars {
|
for k, v := range vars {
|
||||||
@@ -19,3 +60,82 @@ func withScriptoriumStickySessionVar(vars map[string]string, sessionID string) m
|
|||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resolveScriptoriumTimeout(topLevel, artifact string) (time.Duration, error) {
|
||||||
|
raw := strings.TrimSpace(artifact)
|
||||||
|
if raw == "" {
|
||||||
|
raw = strings.TrimSpace(topLevel)
|
||||||
|
}
|
||||||
|
if raw == "" {
|
||||||
|
raw = "10m"
|
||||||
|
}
|
||||||
|
d, err := time.ParseDuration(raw)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("parse duration %q: %w", raw, err)
|
||||||
|
}
|
||||||
|
if d <= 0 {
|
||||||
|
return 0, fmt.Errorf("duration must be > 0")
|
||||||
|
}
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildScriptoriumVars(varsCfg map[string]any, session *config.SessionConfig) (map[string]string, error) {
|
||||||
|
if len(varsCfg) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
vars := map[string]string{}
|
||||||
|
keys := make([]string, 0, len(varsCfg))
|
||||||
|
for key := range varsCfg {
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
|
||||||
|
for _, key := range keys {
|
||||||
|
value := varsCfg[key]
|
||||||
|
switch typed := value.(type) {
|
||||||
|
case bool:
|
||||||
|
if !typed {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
derived, ok, err := deriveSessionVarValue(key, session)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
|
vars[key] = derived
|
||||||
|
}
|
||||||
|
case string:
|
||||||
|
vars[key] = typed
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("var %q has unsupported type %T", key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(vars) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return vars, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func deriveSessionVarValue(name string, session *config.SessionConfig) (string, bool, error) {
|
||||||
|
switch name {
|
||||||
|
case "session_id":
|
||||||
|
if session == nil || strings.TrimSpace(session.SessionID) == "" {
|
||||||
|
return "", false, nil
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(session.SessionID), true, nil
|
||||||
|
case "session_date":
|
||||||
|
if session == nil || strings.TrimSpace(session.Date) == "" {
|
||||||
|
return "", false, nil
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(session.Date), true, nil
|
||||||
|
case "campaign_name":
|
||||||
|
if session == nil || strings.TrimSpace(session.Campaign) == "" {
|
||||||
|
return "", false, nil
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(session.Campaign), true, nil
|
||||||
|
case "previous_session_id":
|
||||||
|
return "", false, nil
|
||||||
|
default:
|
||||||
|
return "", false, fmt.Errorf("unsupported boolean var %q", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -128,15 +128,16 @@ func (trimStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*Stag
|
|||||||
boundsStderrLogPath = filepath.Join(runLayout.LogsDir, "scriptorium.bounds.stderr.log")
|
boundsStderrLogPath = filepath.Join(runLayout.LogsDir, "scriptorium.bounds.stderr.log")
|
||||||
boundsGeneratedConfigPath = filepath.Join(runLayout.ConfigDir, "scriptorium.bounds.generated.yml")
|
boundsGeneratedConfigPath = filepath.Join(runLayout.ConfigDir, "scriptorium.bounds.generated.yml")
|
||||||
}
|
}
|
||||||
boundsTimeout, err := resolveScriptoriumTimeout(env.Config.Pipeline.Scriptorium.Timeout, boundsCfg.Timeout)
|
requestContext, err := TrimScriptoriumContext(env.Config, sessionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("trim: resolve bounds timeout: %w", err)
|
return nil, fmt.Errorf("trim: resolve bounds request: %w", err)
|
||||||
}
|
}
|
||||||
|
boundsTimeout := requestContext.Timeout
|
||||||
|
|
||||||
inputPaths := map[string]string{
|
inputPaths := map[string]string{
|
||||||
boundsCfg.TranscriptInputName: normalizedPath,
|
boundsCfg.TranscriptInputName: normalizedPath,
|
||||||
}
|
}
|
||||||
vars := withScriptoriumStickySessionVar(nil, sessionID)
|
vars := requestContext.Vars
|
||||||
|
|
||||||
metadata["bounds_prompt_id"] = boundsCfg.PromptID
|
metadata["bounds_prompt_id"] = boundsCfg.PromptID
|
||||||
metadata["bounds_profile_id"] = boundsCfg.ProfileID
|
metadata["bounds_profile_id"] = boundsCfg.ProfileID
|
||||||
@@ -165,11 +166,11 @@ func (trimStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*Stag
|
|||||||
}
|
}
|
||||||
|
|
||||||
renderReq := scriptorium.RenderArtifactRequest{
|
renderReq := scriptorium.RenderArtifactRequest{
|
||||||
Binary: env.Config.Pipeline.Scriptorium.Binary,
|
Binary: requestContext.Binary,
|
||||||
ConfigPath: env.Config.Pipeline.Scriptorium.ConfigPath,
|
ConfigPath: requestContext.ConfigPath,
|
||||||
Credentials: env.Config.Pipeline.Scriptorium.Credentials,
|
Credentials: requestContext.Credentials,
|
||||||
PromptID: boundsCfg.PromptID,
|
PromptID: requestContext.PromptID,
|
||||||
ProfileID: boundsCfg.ProfileID,
|
ProfileID: requestContext.ProfileID,
|
||||||
InputPaths: inputPaths,
|
InputPaths: inputPaths,
|
||||||
Vars: vars,
|
Vars: vars,
|
||||||
OutputPath: renderOutputPath,
|
OutputPath: renderOutputPath,
|
||||||
@@ -209,11 +210,11 @@ func (trimStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*Stag
|
|||||||
}
|
}
|
||||||
|
|
||||||
boundsReq := scriptorium.RunArtifactRequest{
|
boundsReq := scriptorium.RunArtifactRequest{
|
||||||
Binary: env.Config.Pipeline.Scriptorium.Binary,
|
Binary: requestContext.Binary,
|
||||||
ConfigPath: env.Config.Pipeline.Scriptorium.ConfigPath,
|
ConfigPath: requestContext.ConfigPath,
|
||||||
Credentials: env.Config.Pipeline.Scriptorium.Credentials,
|
Credentials: requestContext.Credentials,
|
||||||
PromptID: boundsCfg.PromptID,
|
PromptID: requestContext.PromptID,
|
||||||
ProfileID: boundsCfg.ProfileID,
|
ProfileID: requestContext.ProfileID,
|
||||||
InputPaths: inputPaths,
|
InputPaths: inputPaths,
|
||||||
Vars: vars,
|
Vars: vars,
|
||||||
OutputPath: boundsOutputPath,
|
OutputPath: boundsOutputPath,
|
||||||
|
|||||||
Reference in New Issue
Block a user