Unify prompt repository source handling
This commit is contained in:
@@ -7,11 +7,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
|
||||
"gitea.maximumdirect.net/eric/promptkit/internal/filecatalog"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -21,15 +19,8 @@ var (
|
||||
ErrInvalidPromptDefinition = errors.New("invalid prompt definition configuration")
|
||||
)
|
||||
|
||||
type filesystemRepository struct {
|
||||
dir string
|
||||
sourceRoot contentSourceRoot
|
||||
}
|
||||
|
||||
type fsRepository struct {
|
||||
fsys fs.FS
|
||||
root string
|
||||
sourceRoot contentSourceRoot
|
||||
type sourceRepository struct {
|
||||
source promptDefinitionSource
|
||||
}
|
||||
|
||||
type promptDefinitionFile struct {
|
||||
@@ -70,38 +61,47 @@ type promptOutputContractFile struct {
|
||||
}
|
||||
|
||||
func NewFilesystemRepository(dir string) Repository {
|
||||
return &filesystemRepository{
|
||||
dir: dir,
|
||||
sourceRoot: osContentSourceRoot{root: dir},
|
||||
return &sourceRepository{
|
||||
source: osPromptSource{
|
||||
root: dir,
|
||||
contentRoot: osContentSourceRoot{root: dir},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewFSRepository(fsys fs.FS, root string) Repository {
|
||||
return &fsRepository{
|
||||
fsys: fsys,
|
||||
root: root,
|
||||
sourceRoot: fsContentSourceRoot{fsys: fsys, root: root},
|
||||
return &sourceRepository{
|
||||
source: fsPromptSource{
|
||||
fsys: fsys,
|
||||
root: root,
|
||||
contentRoot: fsContentSourceRoot{fsys: fsys, root: root},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewFileRepository constructs a repository for one operating-system prompt file.
|
||||
func NewFileRepository(fsys fs.FS, file string, sourceDir string) Repository {
|
||||
return &fsRepository{
|
||||
fsys: fsys,
|
||||
root: file,
|
||||
sourceRoot: osContentSourceRoot{
|
||||
root: sourceDir,
|
||||
sourcePathsRelative: true,
|
||||
return &sourceRepository{
|
||||
source: fsPromptSource{
|
||||
fsys: fsys,
|
||||
root: file,
|
||||
contentRoot: osContentSourceRoot{
|
||||
root: sourceDir,
|
||||
sourcePathsRelative: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *filesystemRepository) GetPromptDefinition(ctx context.Context, id string, version string) (*domain.PromptDefinition, error) {
|
||||
func (r *sourceRepository) GetPromptDefinition(ctx context.Context, id string, version string) (*domain.PromptDefinition, error) {
|
||||
if strings.TrimSpace(id) == "" {
|
||||
return nil, fmt.Errorf("%w: prompt id is required", ErrInvalidPromptDefinition)
|
||||
}
|
||||
if r == nil || r.source == nil {
|
||||
return nil, errors.New("failed to read prompt definition directory: source is nil")
|
||||
}
|
||||
|
||||
files, err := filecatalog.FindYAMLFiles(ctx, r.dir)
|
||||
files, err := r.source.findYAMLFiles(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read prompt definition directory: %w", err)
|
||||
}
|
||||
@@ -114,8 +114,8 @@ func (r *filesystemRepository) GetPromptDefinition(ctx context.Context, id strin
|
||||
default:
|
||||
}
|
||||
|
||||
relPath := filecatalog.RelativePath(r.dir, fullPath)
|
||||
data, err := os.ReadFile(fullPath)
|
||||
relPath := r.source.displayPath(fullPath)
|
||||
data, err := r.source.readDefinition(fullPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -149,7 +149,7 @@ func (r *filesystemRepository) GetPromptDefinition(ctx context.Context, id strin
|
||||
|
||||
if len(matches) == 1 {
|
||||
match := matches[0]
|
||||
def, err := normalizePromptDefinition(match.raw, r.sourceRoot, match.sourcePath)
|
||||
def, err := normalizePromptDefinition(match.raw, r.source, match.sourcePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidPromptDefinition, match.path, err)
|
||||
}
|
||||
@@ -159,82 +159,12 @@ func (r *filesystemRepository) GetPromptDefinition(ctx context.Context, id strin
|
||||
return nil, ErrPromptDefinitionNotFound
|
||||
}
|
||||
|
||||
func (r *fsRepository) GetPromptDefinition(ctx context.Context, id string, version string) (*domain.PromptDefinition, error) {
|
||||
return loadPromptDefinition(ctx, r.fsys, r.root, r.sourceRoot, id, version)
|
||||
}
|
||||
|
||||
type promptDefinitionMatch struct {
|
||||
raw *promptDefinitionFile
|
||||
sourcePath string
|
||||
path string
|
||||
}
|
||||
|
||||
func loadPromptDefinition(ctx context.Context, fsys fs.FS, root string, sourceRoot contentSourceRoot, id string, version string) (*domain.PromptDefinition, error) {
|
||||
if strings.TrimSpace(id) == "" {
|
||||
return nil, fmt.Errorf("%w: prompt id is required", ErrInvalidPromptDefinition)
|
||||
}
|
||||
if fsys == nil {
|
||||
return nil, fmt.Errorf("failed to read prompt definition directory: filesystem is nil")
|
||||
}
|
||||
|
||||
files, err := filecatalog.FindFSYAMLFiles(ctx, fsys, root)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read prompt definition directory: %w", err)
|
||||
}
|
||||
var matches []promptDefinitionMatch
|
||||
for _, fullPath := range files {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
relPath := filecatalog.DisplayPath(root, fullPath)
|
||||
data, err := fs.ReadFile(fsys, fullPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
raw, err := decodePromptDefinition(data)
|
||||
if err != nil {
|
||||
if promptDefinitionDataMatches(data, id, version) {
|
||||
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidYAML, relPath, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !promptDefinitionMatches(raw, id, version) {
|
||||
continue
|
||||
}
|
||||
matches = append(matches, promptDefinitionMatch{
|
||||
raw: raw,
|
||||
sourcePath: fullPath,
|
||||
path: relPath,
|
||||
})
|
||||
}
|
||||
|
||||
if len(matches) > 1 {
|
||||
paths := make([]string, 0, len(matches))
|
||||
for _, match := range matches {
|
||||
paths = append(paths, match.path)
|
||||
}
|
||||
if version != "" {
|
||||
return nil, fmt.Errorf("%w: duplicate prompt definition id %q version %q found in: %s", ErrInvalidPromptDefinition, id, version, strings.Join(paths, ", "))
|
||||
}
|
||||
return nil, fmt.Errorf("%w: duplicate prompt definition id %q found in: %s", ErrInvalidPromptDefinition, id, strings.Join(paths, ", "))
|
||||
}
|
||||
|
||||
if len(matches) == 1 {
|
||||
match := matches[0]
|
||||
def, err := normalizePromptDefinition(match.raw, sourceRoot, match.sourcePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %s: %v", ErrInvalidPromptDefinition, match.path, err)
|
||||
}
|
||||
return def, nil
|
||||
}
|
||||
|
||||
return nil, ErrPromptDefinitionNotFound
|
||||
}
|
||||
|
||||
func decodePromptDefinition(data []byte) (*promptDefinitionFile, error) {
|
||||
var raw promptDefinitionFile
|
||||
decoder := yaml.NewDecoder(bytes.NewReader(data))
|
||||
|
||||
Reference in New Issue
Block a user