187 lines
4.4 KiB
Go
187 lines
4.4 KiB
Go
package profile
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
var (
|
|
ErrProfileNotFound = errors.New("execution profile not found")
|
|
ErrInvalidYAML = errors.New("invalid YAML format")
|
|
ErrInvalidProfile = errors.New("invalid execution profile configuration")
|
|
ErrRawAPIKeyNotAllowed = errors.New("raw api_key is not allowed; use api_key_env")
|
|
)
|
|
|
|
type filesystemRepository struct {
|
|
dir string
|
|
}
|
|
|
|
func NewFilesystemRepository(dir string) Repository {
|
|
return &filesystemRepository{dir: dir}
|
|
}
|
|
|
|
func (r *filesystemRepository) GetProfile(ctx context.Context, id string) (*domain.ExecutionProfile, error) {
|
|
if strings.TrimSpace(id) == "" {
|
|
return nil, fmt.Errorf("%w: profile id is required", ErrInvalidProfile)
|
|
}
|
|
|
|
files, err := r.yamlFiles(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read profile directory: %w", err)
|
|
}
|
|
|
|
var matches []profileMatch
|
|
for _, fullPath := range files {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
default:
|
|
}
|
|
|
|
relPath := r.relativePath(fullPath)
|
|
fileMatch := profileIDFromFileName(filepath.Base(fullPath)) == id
|
|
data, err := os.ReadFile(fullPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read profile file %s: %w", relPath, err)
|
|
}
|
|
|
|
var prof domain.ExecutionProfile
|
|
decoder := yaml.NewDecoder(bytes.NewReader(data))
|
|
decoder.KnownFields(true)
|
|
if err := decoder.Decode(&prof); err != nil {
|
|
idMatch := fileMatch || profileFileHasID(data, id)
|
|
if strings.Contains(err.Error(), "field api_key not found") {
|
|
if idMatch {
|
|
return nil, fmt.Errorf("%w: %s", ErrRawAPIKeyNotAllowed, relPath)
|
|
}
|
|
continue
|
|
}
|
|
if idMatch {
|
|
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidYAML, relPath, err)
|
|
}
|
|
continue
|
|
}
|
|
|
|
if prof.ID != id {
|
|
continue
|
|
}
|
|
if err := validateProfile(&prof); err != nil {
|
|
if errors.Is(err, ErrRawAPIKeyNotAllowed) {
|
|
return nil, fmt.Errorf("%w: %s", err, relPath)
|
|
}
|
|
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidProfile, relPath, err)
|
|
}
|
|
matches = append(matches, profileMatch{
|
|
profile: &prof,
|
|
path: relPath,
|
|
})
|
|
}
|
|
|
|
if len(matches) > 1 {
|
|
paths := make([]string, 0, len(matches))
|
|
for _, match := range matches {
|
|
paths = append(paths, match.path)
|
|
}
|
|
return nil, fmt.Errorf("%w: duplicate execution profile id %q found in: %s", ErrInvalidProfile, id, strings.Join(paths, ", "))
|
|
}
|
|
|
|
if len(matches) == 1 {
|
|
return matches[0].profile, nil
|
|
}
|
|
|
|
return nil, ErrProfileNotFound
|
|
}
|
|
|
|
type profileMatch struct {
|
|
profile *domain.ExecutionProfile
|
|
path string
|
|
}
|
|
|
|
func (r *filesystemRepository) yamlFiles(ctx context.Context) ([]string, error) {
|
|
var files []string
|
|
err := filepath.WalkDir(r.dir, func(path string, d os.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
default:
|
|
}
|
|
if d.IsDir() {
|
|
return nil
|
|
}
|
|
if !isYAMLFile(d.Name()) {
|
|
return nil
|
|
}
|
|
files = append(files, path)
|
|
return nil
|
|
})
|
|
sort.Strings(files)
|
|
return files, err
|
|
}
|
|
|
|
func (r *filesystemRepository) relativePath(path string) string {
|
|
rel, err := filepath.Rel(r.dir, path)
|
|
if err != nil {
|
|
return filepath.Clean(path)
|
|
}
|
|
return filepath.Clean(rel)
|
|
}
|
|
|
|
func isYAMLFile(name string) bool {
|
|
return strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml")
|
|
}
|
|
|
|
func profileIDFromFileName(name string) string {
|
|
name = strings.TrimSuffix(name, ".yaml")
|
|
name = strings.TrimSuffix(name, ".yml")
|
|
return name
|
|
}
|
|
|
|
func profileFileHasID(data []byte, id string) bool {
|
|
var raw struct {
|
|
ID string `yaml:"id"`
|
|
}
|
|
if err := yaml.NewDecoder(bytes.NewReader(data)).Decode(&raw); err != nil {
|
|
return false
|
|
}
|
|
return strings.TrimSpace(raw.ID) == id
|
|
}
|
|
|
|
func validateProfile(p *domain.ExecutionProfile) error {
|
|
if strings.TrimSpace(p.ID) == "" {
|
|
return errors.New("id is required")
|
|
}
|
|
if strings.TrimSpace(p.Endpoint) == "" {
|
|
return errors.New("endpoint is required")
|
|
}
|
|
if strings.TrimSpace(p.Model) == "" {
|
|
return errors.New("model is required")
|
|
}
|
|
|
|
if p.Temperature < 0 || p.Temperature > 2 {
|
|
return errors.New("temperature must be between 0 and 2")
|
|
}
|
|
if p.MaxTokens < 0 {
|
|
return errors.New("max_tokens must be greater than or equal to 0")
|
|
}
|
|
if p.TopP < 0 || p.TopP > 1 {
|
|
return errors.New("top_p must be between 0 and 1")
|
|
}
|
|
if p.TimeoutSeconds < 0 {
|
|
return errors.New("timeout_seconds must be greater than or equal to 0")
|
|
}
|
|
|
|
return nil
|
|
}
|