144 lines
3.8 KiB
Go
144 lines
3.8 KiB
Go
package profile
|
|
|
|
import (
|
|
"bytes"
|
|
"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) {
|
|
if strings.TrimSpace(id) == "" {
|
|
return nil, fmt.Errorf("%w: profile id is required", ErrInvalidProfile)
|
|
}
|
|
|
|
files, err := os.ReadDir(r.dir)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read profile 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 profile file %s: %w", file.Name(), err)
|
|
}
|
|
|
|
var prof domain.PromptProfile
|
|
decoder := yaml.NewDecoder(bytes.NewReader(data))
|
|
decoder.KnownFields(true)
|
|
if err := decoder.Decode(&prof); 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 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 !isValidMessageRole(t.Role) {
|
|
return fmt.Errorf("template message %d has invalid role %q", i, t.Role)
|
|
}
|
|
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)
|
|
}
|
|
if p.Validation.RepairAttempts < 0 {
|
|
return errors.New("validation.repair_attempts must be greater than or equal to 0")
|
|
}
|
|
if p.Validation.ValidationMode == domain.ValidationJSONSchema && strings.TrimSpace(p.Validation.SchemaPath) == "" {
|
|
return errors.New("validation.schema_path is required when validation_mode is json_schema")
|
|
}
|
|
if p.Validation.Format != "" && p.Validation.Format != p.OutputFormat {
|
|
return fmt.Errorf("validation format %q does not match output format %q", p.Validation.Format, p.OutputFormat)
|
|
}
|
|
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
|
|
}
|
|
|
|
func isValidMessageRole(role string) bool {
|
|
switch role {
|
|
case "system", "user", "assistant", "developer":
|
|
return true
|
|
}
|
|
return false
|
|
}
|