219 lines
6.9 KiB
Go
219 lines
6.9 KiB
Go
package scriptorium
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
|
)
|
|
|
|
// NoopRunner is a deterministic no-op scriptorium adapter.
|
|
type NoopRunner struct{}
|
|
|
|
func (n *NoopRunner) InspectRequirements(ctx context.Context, req RequirementsRequest) (RequirementsReport, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return RequirementsReport{}, err
|
|
}
|
|
return emptyRequirementsReport(req.PromptID), nil
|
|
}
|
|
|
|
// RunArtifact returns requested output path with placeholder metadata.
|
|
func (n *NoopRunner) RunArtifact(ctx context.Context, req RunArtifactRequest) (ArtifactResult, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return ArtifactResult{}, err
|
|
}
|
|
if err := materializeRunPlaceholders(req); err != nil {
|
|
return ArtifactResult{}, err
|
|
}
|
|
return ArtifactResult{
|
|
OutputPath: req.OutputPath,
|
|
StderrLogPath: req.StderrLogPath,
|
|
GeneratedConfigPath: req.GeneratedConfigPath,
|
|
CommandMode: CommandModeRun,
|
|
PromptID: req.PromptID,
|
|
ProfileID: req.ProfileID,
|
|
Metadata: map[string]any{"placeholder": true},
|
|
}, nil
|
|
}
|
|
|
|
// RenderArtifact returns requested output path with placeholder metadata.
|
|
func (n *NoopRunner) RenderArtifact(ctx context.Context, req RenderArtifactRequest) (ArtifactResult, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return ArtifactResult{}, err
|
|
}
|
|
if err := materializeRenderPlaceholders(req); err != nil {
|
|
return ArtifactResult{}, err
|
|
}
|
|
return ArtifactResult{
|
|
OutputPath: req.OutputPath,
|
|
StderrLogPath: req.StderrLogPath,
|
|
GeneratedConfigPath: req.GeneratedConfigPath,
|
|
CommandMode: CommandModeRender,
|
|
PromptID: req.PromptID,
|
|
ProfileID: req.ProfileID,
|
|
Metadata: map[string]any{"placeholder": true},
|
|
}, nil
|
|
}
|
|
|
|
// FakeRunner captures requests and returns deterministic responses.
|
|
type FakeRunner struct {
|
|
RunRequests []RunArtifactRequest
|
|
RenderRequests []RenderArtifactRequest
|
|
RunErr error
|
|
RenderErr error
|
|
RunResult ArtifactResult
|
|
RenderResult ArtifactResult
|
|
RequirementsRequests []RequirementsRequest
|
|
RequirementsResult *RequirementsReport
|
|
RequirementsErr error
|
|
}
|
|
|
|
func (f *FakeRunner) InspectRequirements(ctx context.Context, req RequirementsRequest) (RequirementsReport, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return RequirementsReport{}, err
|
|
}
|
|
req.Credentials = slices.Clone(req.Credentials)
|
|
req.Vars = cloneStringMap(req.Vars)
|
|
f.RequirementsRequests = append(f.RequirementsRequests, req)
|
|
if f.RequirementsErr != nil {
|
|
return RequirementsReport{}, f.RequirementsErr
|
|
}
|
|
if f.RequirementsResult != nil {
|
|
return *f.RequirementsResult, nil
|
|
}
|
|
return emptyRequirementsReport(req.PromptID), nil
|
|
}
|
|
|
|
func emptyRequirementsReport(prompt string) RequirementsReport {
|
|
return RequirementsReport{PromptID: prompt, Complete: true, Inputs: []RequirementInput{}, Variables: []RequirementVariable{}, Issues: []RequirementIssue{}}
|
|
}
|
|
|
|
// RunArtifact records request and returns configured response.
|
|
func (f *FakeRunner) RunArtifact(ctx context.Context, req RunArtifactRequest) (ArtifactResult, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return ArtifactResult{}, err
|
|
}
|
|
req.Credentials = slices.Clone(req.Credentials)
|
|
f.RunRequests = append(f.RunRequests, req)
|
|
if f.RunErr != nil {
|
|
return ArtifactResult{}, f.RunErr
|
|
}
|
|
if err := materializeRunPlaceholders(req); err != nil {
|
|
return ArtifactResult{}, err
|
|
}
|
|
res := f.RunResult
|
|
if res.OutputPath == "" {
|
|
res.OutputPath = req.OutputPath
|
|
}
|
|
if res.StderrLogPath == "" {
|
|
res.StderrLogPath = req.StderrLogPath
|
|
}
|
|
if res.GeneratedConfigPath == "" {
|
|
res.GeneratedConfigPath = req.GeneratedConfigPath
|
|
}
|
|
if res.CommandMode == "" {
|
|
res.CommandMode = CommandModeRun
|
|
}
|
|
if res.PromptID == "" {
|
|
res.PromptID = req.PromptID
|
|
}
|
|
if res.ProfileID == "" {
|
|
res.ProfileID = req.ProfileID
|
|
}
|
|
if res.Metadata == nil {
|
|
res.Metadata = map[string]any{"fake": true}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// RenderArtifact records request and returns configured response.
|
|
func (f *FakeRunner) RenderArtifact(ctx context.Context, req RenderArtifactRequest) (ArtifactResult, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return ArtifactResult{}, err
|
|
}
|
|
req.Credentials = slices.Clone(req.Credentials)
|
|
f.RenderRequests = append(f.RenderRequests, req)
|
|
if f.RenderErr != nil {
|
|
return ArtifactResult{}, f.RenderErr
|
|
}
|
|
if err := materializeRenderPlaceholders(req); err != nil {
|
|
return ArtifactResult{}, err
|
|
}
|
|
res := f.RenderResult
|
|
if res.OutputPath == "" {
|
|
res.OutputPath = req.OutputPath
|
|
}
|
|
if res.StderrLogPath == "" {
|
|
res.StderrLogPath = req.StderrLogPath
|
|
}
|
|
if res.GeneratedConfigPath == "" {
|
|
res.GeneratedConfigPath = req.GeneratedConfigPath
|
|
}
|
|
if res.CommandMode == "" {
|
|
res.CommandMode = CommandModeRender
|
|
}
|
|
if res.PromptID == "" {
|
|
res.PromptID = req.PromptID
|
|
}
|
|
if res.ProfileID == "" {
|
|
res.ProfileID = req.ProfileID
|
|
}
|
|
if res.Metadata == nil {
|
|
res.Metadata = map[string]any{"fake": true}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func materializeRunPlaceholders(req RunArtifactRequest) error {
|
|
if req.OutputPath != "" {
|
|
if err := fileops.WriteFileAtomic(req.OutputPath, []byte("scriptorium noop/fake run artifact\n"), fileops.WorkspaceFileMode); err != nil {
|
|
return fmt.Errorf("write run output %q: %w", req.OutputPath, err)
|
|
}
|
|
}
|
|
if req.GeneratedConfigPath != "" {
|
|
payload := map[string]any{
|
|
"schema": "scriptorium.generated.v1",
|
|
"placeholder": true,
|
|
"mode": CommandModeRun,
|
|
"prompt_id": req.PromptID,
|
|
"output_path": req.OutputPath,
|
|
}
|
|
if err := writeGeneratedYAML(req.GeneratedConfigPath, payload); err != nil {
|
|
return fmt.Errorf("write generated config %q: %w", req.GeneratedConfigPath, err)
|
|
}
|
|
}
|
|
if req.StderrLogPath != "" {
|
|
if err := fileops.WriteFileAtomic(req.StderrLogPath, []byte("scriptorium noop/fake run stderr placeholder\n"), fileops.WorkspaceFileMode); err != nil {
|
|
return fmt.Errorf("write stderr log %q: %w", req.StderrLogPath, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func materializeRenderPlaceholders(req RenderArtifactRequest) error {
|
|
if req.OutputPath != "" {
|
|
if err := fileops.WriteFileAtomic(req.OutputPath, []byte("{\"placeholder\":true}\n"), fileops.WorkspaceFileMode); err != nil {
|
|
return fmt.Errorf("write render output %q: %w", req.OutputPath, err)
|
|
}
|
|
}
|
|
if req.GeneratedConfigPath != "" {
|
|
payload := map[string]any{
|
|
"schema": "scriptorium.generated.v1",
|
|
"placeholder": true,
|
|
"mode": CommandModeRender,
|
|
"prompt_id": req.PromptID,
|
|
"output_path": req.OutputPath,
|
|
}
|
|
if err := writeGeneratedYAML(req.GeneratedConfigPath, payload); err != nil {
|
|
return fmt.Errorf("write generated config %q: %w", req.GeneratedConfigPath, err)
|
|
}
|
|
}
|
|
if req.StderrLogPath != "" {
|
|
if err := fileops.WriteFileAtomic(req.StderrLogPath, []byte("scriptorium noop/fake render stderr placeholder\n"), fileops.WorkspaceFileMode); err != nil {
|
|
return fmt.Errorf("write stderr log %q: %w", req.StderrLogPath, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|