Implement the initial framework skeleton

This commit is contained in:
2026-05-04 20:14:31 -05:00
parent a7feb0b097
commit c07320f9d0
18 changed files with 1563 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
package profile
import (
"context"
"errors"
"fmt"
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"strings"
)
var (
ErrProfileNotFound = errors.New("prompt profile not found")
ErrInvalidYAML = errors.New("invalid YAML format")
ErrInvalidProfile = errors.New("invalid profile configuration")
)
type filesystemRepository struct {
dir string
}
func NewFilesystemRepository(dir string) Repository {
return &filesystemRepository{dir: dir}
}
func (r *filesystemRepository) GetProfile(ctx context.Context, id string, version string) (*domain.PromptProfile, error) {
files, err := os.ReadDir(r.dir)
if err != nil {
return nil, fmt.Errorf("failed to read profile directory: %w", err)
}
for _, file := range files {
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 profile file %s: %w", file.Name(), err)
}
var prof domain.PromptProfile
if err := yaml.Unmarshal(data, &prof); err != nil {
if strings.Contains(file.Name(), id) {
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidYAML, file.Name(), err)
}
continue
}
if prof.ID == id {
if version == "" || prof.Version == version {
if err := validateProfile(&prof); err != nil {
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidProfile, file.Name(), err)
}
return &prof, nil
}
}
}
return nil, ErrProfileNotFound
}
func validateProfile(p *domain.PromptProfile) error {
if p.ID == "" {
return errors.New("profile id is required")
}
if p.Version == "" {
return errors.New("profile version is required")
}
if len(p.Templates) == 0 {
return errors.New("at least one prompt template message is required")
}
for i, t := range p.Templates {
if t.Role == "" {
return fmt.Errorf("template message %d is missing role", i)
}
if t.Content == "" {
return fmt.Errorf("template message %d is missing content", i)
}
}
if !isValidOutputFormat(p.OutputFormat) {
return fmt.Errorf("invalid output format: %s", p.OutputFormat)
}
if !isValidValidationMode(p.Validation.ValidationMode) {
return fmt.Errorf("invalid validation mode: %s", p.Validation.ValidationMode)
}
for i, input := range p.ExpectedInputs {
if strings.TrimSpace(input) == "" {
return fmt.Errorf("expected input %d has empty name", i)
}
}
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
}

View File

@@ -0,0 +1,11 @@
package profile
import (
"context"
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
)
// Repository handles loading and storing prompt profiles.
type Repository interface {
GetProfile(ctx context.Context, id string, version string) (*domain.PromptProfile, error)
}

View File

@@ -0,0 +1,76 @@
package profile
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
)
func TestFilesystemRepository_GetProfile(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "profile_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 profile", func(t *testing.T) {
p, err := repo.GetProfile(ctx, "test-profile", "")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if p == nil || p.ID != "test-profile" {
t.Errorf("expected profile test-profile, got %v", p)
}
})
t.Run("invalid YAML", func(t *testing.T) {
_, err := repo.GetProfile(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.GetProfile(ctx, "missing-id", "")
if !errors.Is(err, ErrProfileNotFound) {
t.Errorf("expected ErrProfileNotFound for profile with missing ID, got %v", err)
}
})
t.Run("no templates", func(t *testing.T) {
_, err := repo.GetProfile(ctx, "no-templates", "")
if !errors.Is(err, ErrInvalidProfile) {
t.Errorf("expected ErrInvalidProfile for profile with no templates, got %v", err)
}
})
t.Run("profile not found", func(t *testing.T) {
_, err := repo.GetProfile(ctx, "unknown", "")
if !errors.Is(err, ErrProfileNotFound) {
t.Errorf("expected ErrProfileNotFound, got %v", err)
}
})
}

View File

@@ -0,0 +1,5 @@
id: invalid-yaml
version: 1.0.0
templates:
- role: system
content: [unclosed bracket

View File

@@ -0,0 +1,8 @@
version: 1.0.0
description: Missing ID
templates:
- role: system
content: Hello
output_format: text
validation:
validation_mode: none

View File

@@ -0,0 +1,6 @@
id: no-templates
version: 1.0.0
templates: []
output_format: text
validation:
validation_mode: none

17
internal/profile/testdata/valid.yaml vendored Normal file
View File

@@ -0,0 +1,17 @@
id: test-profile
version: 1.0.0
description: A valid test profile
expected_inputs:
- transcript
- glossary
templates:
- role: system
content: You are a helpful assistant.
- role: user
content: Analyze this: {{.transcript}}
model_defaults:
model: gpt-4o
temperature: 0.7
output_format: markdown
validation:
validation_mode: basic