Implement the initial framework skeleton
This commit is contained in:
113
internal/profile/filesystem_repository.go
Normal file
113
internal/profile/filesystem_repository.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user