Refactor: load execution profiles from YAML and split prompt definitions into promptdef repository
This commit is contained in:
151
internal/promptdef/filesystem_repository.go
Normal file
151
internal/promptdef/filesystem_repository.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package promptdef
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrPromptDefinitionNotFound = errors.New("prompt definition not found")
|
||||
ErrInvalidYAML = errors.New("invalid YAML format")
|
||||
ErrInvalidPromptDefinition = errors.New("invalid prompt definition configuration")
|
||||
)
|
||||
|
||||
type filesystemRepository struct {
|
||||
dir string
|
||||
}
|
||||
|
||||
func NewFilesystemRepository(dir string) Repository {
|
||||
return &filesystemRepository{dir: dir}
|
||||
}
|
||||
|
||||
func (r *filesystemRepository) GetPromptDefinition(ctx context.Context, id string, version string) (*domain.PromptDefinition, error) {
|
||||
if strings.TrimSpace(id) == "" {
|
||||
return nil, fmt.Errorf("%w: prompt id is required", ErrInvalidPromptDefinition)
|
||||
}
|
||||
|
||||
files, err := os.ReadDir(r.dir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read prompt definition directory: %w", err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
if file.IsDir() || (!strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml")) {
|
||||
continue
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(r.dir, file.Name())
|
||||
data, err := os.ReadFile(fullPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read prompt definition file %s: %w", file.Name(), err)
|
||||
}
|
||||
|
||||
var def domain.PromptDefinition
|
||||
decoder := yaml.NewDecoder(bytes.NewReader(data))
|
||||
decoder.KnownFields(true)
|
||||
if err := decoder.Decode(&def); err != nil {
|
||||
if strings.TrimSuffix(strings.TrimSuffix(file.Name(), ".yaml"), ".yml") == id {
|
||||
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidYAML, file.Name(), err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if def.ID != id {
|
||||
continue
|
||||
}
|
||||
if version != "" && def.Version != version {
|
||||
continue
|
||||
}
|
||||
if err := validatePromptDefinition(&def); err != nil {
|
||||
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidPromptDefinition, file.Name(), err)
|
||||
}
|
||||
return &def, nil
|
||||
}
|
||||
|
||||
return nil, ErrPromptDefinitionNotFound
|
||||
}
|
||||
|
||||
func validatePromptDefinition(d *domain.PromptDefinition) error {
|
||||
if d.ID == "" {
|
||||
return errors.New("prompt id is required")
|
||||
}
|
||||
if d.Version == "" {
|
||||
return errors.New("prompt version is required")
|
||||
}
|
||||
if len(d.Templates) == 0 {
|
||||
return errors.New("at least one prompt template message is required")
|
||||
}
|
||||
if len(d.Inputs) == 0 {
|
||||
return errors.New("at least one prompt input is required")
|
||||
}
|
||||
for i, input := range d.Inputs {
|
||||
if strings.TrimSpace(input.Name) == "" {
|
||||
return fmt.Errorf("input %d has empty name", i)
|
||||
}
|
||||
}
|
||||
for i, t := range d.Templates {
|
||||
if !isValidMessageRole(t.Role) {
|
||||
return fmt.Errorf("template message %d has invalid role %q", i, t.Role)
|
||||
}
|
||||
if strings.TrimSpace(t.Content) == "" && strings.TrimSpace(t.ContentFile) == "" {
|
||||
return fmt.Errorf("template message %d must provide content or content_file", i)
|
||||
}
|
||||
if strings.TrimSpace(t.Content) != "" && strings.TrimSpace(t.ContentFile) != "" {
|
||||
return fmt.Errorf("template message %d cannot set both content and content_file", i)
|
||||
}
|
||||
}
|
||||
if !isValidOutputFormat(d.OutputFormat) {
|
||||
return fmt.Errorf("invalid output format: %s", d.OutputFormat)
|
||||
}
|
||||
if !isValidValidationMode(d.Validation.ValidationMode) {
|
||||
return fmt.Errorf("invalid validation mode: %s", d.Validation.ValidationMode)
|
||||
}
|
||||
if d.Validation.RepairAttempts < 0 {
|
||||
return errors.New("validation.repair_attempts must be greater than or equal to 0")
|
||||
}
|
||||
if d.Validation.ValidationMode == domain.ValidationJSONSchema && strings.TrimSpace(d.Validation.SchemaPath) == "" {
|
||||
return errors.New("validation.schema_path is required when validation_mode is json_schema")
|
||||
}
|
||||
if d.Validation.Format != "" && d.Validation.Format != d.OutputFormat {
|
||||
return fmt.Errorf("validation format %q does not match output format %q", d.Validation.Format, d.OutputFormat)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isValidOutputFormat(f domain.OutputFormat) bool {
|
||||
switch f {
|
||||
case domain.FormatText, domain.FormatMarkdown, domain.FormatJSON:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isValidValidationMode(m domain.ValidationMode) bool {
|
||||
switch m {
|
||||
case domain.ValidationNone, domain.ValidationBasic, domain.ValidationJSON, domain.ValidationJSONSchema:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isValidMessageRole(role string) bool {
|
||||
switch role {
|
||||
case "system", "user", "assistant", "developer":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
12
internal/promptdef/repository.go
Normal file
12
internal/promptdef/repository.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package promptdef
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
|
||||
)
|
||||
|
||||
// Repository loads prompt definitions.
|
||||
type Repository interface {
|
||||
GetPromptDefinition(ctx context.Context, id string, version string) (*domain.PromptDefinition, error)
|
||||
}
|
||||
106
internal/promptdef/repository_test.go
Normal file
106
internal/promptdef/repository_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package promptdef
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
|
||||
)
|
||||
|
||||
func TestFilesystemRepository_GetPromptDefinition(t *testing.T) {
|
||||
tmpDir, err := os.MkdirTemp("", "promptdef_test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
testDataDir := "testdata"
|
||||
files, err := os.ReadDir(testDataDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read testdata: %v", err)
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
src := filepath.Join(testDataDir, f.Name())
|
||||
dst := filepath.Join(tmpDir, f.Name())
|
||||
data, err := os.ReadFile(src)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(dst, data, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
repo := NewFilesystemRepository(tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("valid prompt definition", func(t *testing.T) {
|
||||
p, err := repo.GetPromptDefinition(ctx, "test-profile", "")
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if p == nil || p.ID != "test-profile" {
|
||||
t.Errorf("expected prompt definition test-profile, got %v", p)
|
||||
}
|
||||
if p.Version != "1.0.0" {
|
||||
t.Fatalf("expected version 1.0.0, got %q", p.Version)
|
||||
}
|
||||
if len(p.Inputs) != 2 || p.Inputs[0].Name != "transcript" || p.Inputs[1].Name != "glossary" {
|
||||
t.Fatalf("unexpected inputs: %#v", p.Inputs)
|
||||
}
|
||||
if len(p.Templates) != 2 {
|
||||
t.Fatalf("expected 2 templates, got %d", len(p.Templates))
|
||||
}
|
||||
if p.Templates[0].Role != "system" || p.Templates[1].Role != "user" {
|
||||
t.Fatalf("unexpected template roles: %#v", p.Templates)
|
||||
}
|
||||
if p.OutputFormat != domain.FormatMarkdown {
|
||||
t.Fatalf("expected output format markdown, got %q", p.OutputFormat)
|
||||
}
|
||||
if p.Validation.ValidationMode != domain.ValidationBasic {
|
||||
t.Fatalf("expected validation mode basic, got %q", p.Validation.ValidationMode)
|
||||
}
|
||||
if p.DefaultProfile != "test-exec" {
|
||||
t.Fatalf("expected default profile test-exec, got %q", p.DefaultProfile)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid YAML", func(t *testing.T) {
|
||||
_, err := repo.GetPromptDefinition(ctx, "invalid_yaml", "")
|
||||
if !errors.Is(err, ErrInvalidYAML) {
|
||||
t.Errorf("expected ErrInvalidYAML, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("missing ID", func(t *testing.T) {
|
||||
_, err := repo.GetPromptDefinition(ctx, "missing-id", "")
|
||||
if !errors.Is(err, ErrPromptDefinitionNotFound) {
|
||||
t.Errorf("expected ErrPromptDefinitionNotFound for profile with missing ID, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no templates", func(t *testing.T) {
|
||||
_, err := repo.GetPromptDefinition(ctx, "no-templates", "")
|
||||
if !errors.Is(err, ErrInvalidPromptDefinition) {
|
||||
t.Errorf("expected ErrInvalidPromptDefinition for profile with no templates, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("json schema mode missing schema path", func(t *testing.T) {
|
||||
_, err := repo.GetPromptDefinition(ctx, "json-schema-missing-path", "")
|
||||
if !errors.Is(err, ErrInvalidPromptDefinition) {
|
||||
t.Errorf("expected ErrInvalidPromptDefinition for json_schema profile without schema_path, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("prompt definition not found", func(t *testing.T) {
|
||||
_, err := repo.GetPromptDefinition(ctx, "unknown", "")
|
||||
if !errors.Is(err, ErrPromptDefinitionNotFound) {
|
||||
t.Errorf("expected ErrPromptDefinitionNotFound, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
5
internal/promptdef/testdata/invalid_yaml.yaml
vendored
Normal file
5
internal/promptdef/testdata/invalid_yaml.yaml
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
id: invalid-yaml
|
||||
version: 1.0.0
|
||||
templates:
|
||||
- role: system
|
||||
content: [unclosed bracket
|
||||
11
internal/promptdef/testdata/json_schema_missing_path.yaml
vendored
Normal file
11
internal/promptdef/testdata/json_schema_missing_path.yaml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
id: json-schema-missing-path
|
||||
version: "1.0.0"
|
||||
inputs:
|
||||
- name: transcript
|
||||
required: true
|
||||
templates:
|
||||
- role: user
|
||||
content: "Return JSON"
|
||||
output_format: json
|
||||
validation:
|
||||
validation_mode: json_schema
|
||||
11
internal/promptdef/testdata/missing_id.yaml
vendored
Normal file
11
internal/promptdef/testdata/missing_id.yaml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
version: 1.0.0
|
||||
description: Missing ID
|
||||
inputs:
|
||||
- name: transcript
|
||||
required: true
|
||||
templates:
|
||||
- role: system
|
||||
content: Hello
|
||||
output_format: text
|
||||
validation:
|
||||
validation_mode: none
|
||||
11
internal/promptdef/testdata/negative_timeout.yaml
vendored
Normal file
11
internal/promptdef/testdata/negative_timeout.yaml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
id: negative-timeout
|
||||
version: "1.0.0"
|
||||
inputs:
|
||||
- name: transcript
|
||||
required: true
|
||||
templates:
|
||||
- role: user
|
||||
content: "Say hi"
|
||||
output_format: text
|
||||
validation:
|
||||
validation_mode: none
|
||||
9
internal/promptdef/testdata/no_templates.yaml
vendored
Normal file
9
internal/promptdef/testdata/no_templates.yaml
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
id: no-templates
|
||||
version: 1.0.0
|
||||
inputs:
|
||||
- name: transcript
|
||||
required: true
|
||||
templates: []
|
||||
output_format: text
|
||||
validation:
|
||||
validation_mode: none
|
||||
19
internal/promptdef/testdata/valid.yaml
vendored
Normal file
19
internal/promptdef/testdata/valid.yaml
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
id: test-profile
|
||||
version: "1.0.0"
|
||||
default_profile: test-exec
|
||||
description: A valid test prompt definition
|
||||
inputs:
|
||||
- name: transcript
|
||||
required: true
|
||||
content_type: text/markdown
|
||||
- name: glossary
|
||||
required: false
|
||||
content_type: text/yaml
|
||||
templates:
|
||||
- role: system
|
||||
content: "You are a helpful assistant."
|
||||
- role: user
|
||||
content: 'Analyze this: {{input "transcript"}}'
|
||||
output_format: markdown
|
||||
validation:
|
||||
validation_mode: basic
|
||||
Reference in New Issue
Block a user