Bugfixes and improved alignment with architecture blueprint
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
|
||||
)
|
||||
|
||||
func TestFilesystemRepository_GetProfile(t *testing.T) {
|
||||
@@ -44,6 +46,24 @@ func TestFilesystemRepository_GetProfile(t *testing.T) {
|
||||
if p == nil || p.ID != "test-profile" {
|
||||
t.Errorf("expected profile test-profile, got %v", p)
|
||||
}
|
||||
if p.Version != "1.0.0" {
|
||||
t.Fatalf("expected version 1.0.0, got %q", p.Version)
|
||||
}
|
||||
if len(p.ExpectedInputs) != 2 || p.ExpectedInputs[0] != "transcript" || p.ExpectedInputs[1] != "glossary" {
|
||||
t.Fatalf("unexpected expected_inputs: %#v", p.ExpectedInputs)
|
||||
}
|
||||
if len(p.Templates) != 2 {
|
||||
t.Fatalf("expected 2 templates, got %d", len(p.Templates))
|
||||
}
|
||||
if p.Templates[0].Role != "system" || p.Templates[1].Role != "user" {
|
||||
t.Fatalf("unexpected template roles: %#v", p.Templates)
|
||||
}
|
||||
if p.OutputFormat != domain.FormatMarkdown {
|
||||
t.Fatalf("expected output format markdown, got %q", p.OutputFormat)
|
||||
}
|
||||
if p.Validation.ValidationMode != domain.ValidationBasic {
|
||||
t.Fatalf("expected validation mode basic, got %q", p.Validation.ValidationMode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid YAML", func(t *testing.T) {
|
||||
@@ -67,6 +87,13 @@ func TestFilesystemRepository_GetProfile(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("json schema mode missing schema path", func(t *testing.T) {
|
||||
_, err := repo.GetProfile(ctx, "json-schema-missing-path", "")
|
||||
if !errors.Is(err, ErrInvalidProfile) {
|
||||
t.Errorf("expected ErrInvalidProfile for json_schema profile without schema_path, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("profile not found", func(t *testing.T) {
|
||||
_, err := repo.GetProfile(ctx, "unknown", "")
|
||||
if !errors.Is(err, ErrProfileNotFound) {
|
||||
|
||||
8
internal/profile/testdata/json_schema_missing_path.yaml
vendored
Normal file
8
internal/profile/testdata/json_schema_missing_path.yaml
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
id: json-schema-missing-path
|
||||
version: "1.0.0"
|
||||
templates:
|
||||
- role: user
|
||||
content: "Return JSON"
|
||||
output_format: json
|
||||
validation:
|
||||
validation_mode: json_schema
|
||||
6
internal/profile/testdata/valid.yaml
vendored
6
internal/profile/testdata/valid.yaml
vendored
@@ -1,14 +1,14 @@
|
||||
id: test-profile
|
||||
version: 1.0.0
|
||||
version: "1.0.0"
|
||||
description: A valid test profile
|
||||
expected_inputs:
|
||||
- transcript
|
||||
- glossary
|
||||
templates:
|
||||
- role: system
|
||||
content: You are a helpful assistant.
|
||||
content: "You are a helpful assistant."
|
||||
- role: user
|
||||
content: Analyze this: {{.transcript}}
|
||||
content: 'Analyze this: {{input "transcript"}}'
|
||||
model_defaults:
|
||||
model: gpt-4o
|
||||
temperature: 0.7
|
||||
|
||||
Reference in New Issue
Block a user