Use native prepared previews and correct diagnostic references
This commit is contained in:
@@ -2,6 +2,7 @@ package scriptorium
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -121,7 +122,7 @@ func (r *SubprocessRunner) RunArtifact(ctx context.Context, req RunArtifactReque
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// RenderArtifact executes `scriptorium render` with JSON output.
|
||||
// RenderArtifact captures the native prepared request as JSON.
|
||||
func (r *SubprocessRunner) RenderArtifact(ctx context.Context, req RenderArtifactRequest) (ArtifactResult, error) {
|
||||
if r == nil {
|
||||
return ArtifactResult{}, fmt.Errorf("scriptorium subprocess runner is nil")
|
||||
@@ -158,6 +159,10 @@ func (r *SubprocessRunner) RenderArtifact(ctx context.Context, req RenderArtifac
|
||||
}
|
||||
}
|
||||
|
||||
output, err := openResultFile(req.OutputPath)
|
||||
if err != nil {
|
||||
return ArtifactResult{}, fmt.Errorf("open scriptorium preview %q: %w", req.OutputPath, err)
|
||||
}
|
||||
runRes, runErr := subprocess.Run(ctx, subprocess.RunRequest{
|
||||
Executable: req.Binary,
|
||||
Args: args,
|
||||
@@ -166,13 +171,17 @@ func (r *SubprocessRunner) RenderArtifact(ctx context.Context, req RenderArtifac
|
||||
EnvOverrides: envOverrides,
|
||||
SensitiveEnvNames: sensitiveNames,
|
||||
DiagnosticOwner: "scriptorium",
|
||||
StdoutLogPath: req.StdoutLogPath,
|
||||
StdoutResult: output,
|
||||
StdoutResultLimit: MaxOutputFileBytes,
|
||||
StderrLogPath: req.StderrLogPath,
|
||||
})
|
||||
if closeErr := output.Close(); closeErr != nil {
|
||||
runErr = errors.Join(runErr, fmt.Errorf("close scriptorium preview %q: %w", req.OutputPath, closeErr))
|
||||
}
|
||||
|
||||
result := ArtifactResult{
|
||||
OutputPath: req.OutputPath,
|
||||
StdoutLogPath: req.StdoutLogPath,
|
||||
StdoutLogPath: runRes.StdoutLogPath,
|
||||
StderrLogPath: req.StderrLogPath,
|
||||
GeneratedConfigPath: req.GeneratedConfigPath,
|
||||
ExitCode: runRes.ExitCode,
|
||||
@@ -196,6 +205,13 @@ func (r *SubprocessRunner) RenderArtifact(ctx context.Context, req RenderArtifac
|
||||
if err := validateNonEmptyOutput(req.OutputPath); err != nil {
|
||||
return result, fmt.Errorf("validate scriptorium render output %q: %w", req.OutputPath, err)
|
||||
}
|
||||
data, err := fileops.ReadFileBounded(req.OutputPath, MaxOutputFileBytes)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("read scriptorium render output %q: %w", req.OutputPath, err)
|
||||
}
|
||||
if !json.Valid(data) {
|
||||
return result, fmt.Errorf("validate scriptorium render output %q: invalid JSON", req.OutputPath)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -281,7 +297,7 @@ func openResultFile(path string) (*os.File, error) {
|
||||
}
|
||||
|
||||
func buildRenderArgs(req RenderArtifactRequest) []string {
|
||||
args := []string{"render", "--prompt", strings.TrimSpace(req.PromptID)}
|
||||
args := []string{"promptkit", "show", "prepared", strings.TrimSpace(req.PromptID)}
|
||||
if cfgPath := strings.TrimSpace(req.ConfigPath); cfgPath != "" {
|
||||
args = append(args, "--config", cfgPath)
|
||||
}
|
||||
@@ -289,14 +305,12 @@ func buildRenderArgs(req RenderArtifactRequest) []string {
|
||||
args = append(args, "--profile", profileID)
|
||||
}
|
||||
for _, kv := range sortedKeyValues(req.InputPaths) {
|
||||
args = append(args, "--input", kv)
|
||||
args = append(args, "--input-file", kv)
|
||||
}
|
||||
for _, kv := range sortedKeyValues(req.Vars) {
|
||||
args = append(args, "--var", kv)
|
||||
}
|
||||
args = append(args, "--timeout", req.Timeout.String())
|
||||
args = append(args, "--format", "json")
|
||||
args = append(args, "--out", req.OutputPath)
|
||||
return args
|
||||
}
|
||||
|
||||
@@ -351,9 +365,7 @@ func writeInvocationConfig(path string, payload invocationPayload) error {
|
||||
"render_format": payload.RenderFormat,
|
||||
"render_prompt_logged": payload.RenderPromptStore,
|
||||
}
|
||||
if payload.Mode == CommandModeRun {
|
||||
data["stdout_destination"] = payload.OutputPath
|
||||
}
|
||||
data["stdout_destination"] = payload.OutputPath
|
||||
return writeGeneratedYAML(path, data)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user