Bugfixes and improved alignment with architecture blueprint

This commit is contained in:
2026-05-04 20:36:12 -05:00
parent c07320f9d0
commit fa070a296d
10 changed files with 158 additions and 34 deletions

View File

@@ -1,6 +1,7 @@
package profile
import (
"bytes"
"context"
"errors"
"fmt"
@@ -26,12 +27,22 @@ func NewFilesystemRepository(dir string) Repository {
}
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
}
@@ -43,8 +54,10 @@ func (r *filesystemRepository) GetProfile(ctx context.Context, id string, versio
}
var prof domain.PromptProfile
if err := yaml.Unmarshal(data, &prof); err != nil {
if strings.Contains(file.Name(), id) {
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
@@ -75,8 +88,8 @@ func validateProfile(p *domain.PromptProfile) error {
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 !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)
@@ -88,6 +101,15 @@ func validateProfile(p *domain.PromptProfile) error {
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)
@@ -111,3 +133,11 @@ func isValidValidationMode(m domain.ValidationMode) bool {
}
return false
}
func isValidMessageRole(role string) bool {
switch role {
case "system", "user", "assistant", "developer":
return true
}
return false
}