Load bundled and operator prompt resources
This commit is contained in:
@@ -7,12 +7,12 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
ck "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
pk "gitea.maximumdirect.net/eric/promptkit"
|
||||
ck "go.maximumdirect.net/configkit"
|
||||
pk "go.maximumdirect.net/promptkit"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func capturedSourcePaths(engine *pk.Engine) ([]string, error) {
|
||||
func capturedSourcePaths(engine *pk.Engine, hostRoot string) ([]string, error) {
|
||||
inspection, err := engine.InspectConfiguration()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("inspect configuration: %w", err)
|
||||
@@ -21,20 +21,36 @@ func capturedSourcePaths(engine *pk.Engine) ([]string, error) {
|
||||
sources := inspection.Sources()
|
||||
byID := make(map[string]ck.SourceInfo, len(sources))
|
||||
paths := make(map[string]struct{}, len(sources))
|
||||
hostPath := func(source ck.SourceInfo) string {
|
||||
if source.Kind == ck.SourceKindFile {
|
||||
return source.Path
|
||||
}
|
||||
if hostRoot != "" && source.Kind == ck.SourceKindFilesystem && strings.HasPrefix(source.Label, operatorFilesystem+"/") {
|
||||
return filepath.Join(hostRoot, filepath.FromSlash(source.Path))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
for _, source := range sources {
|
||||
byID[source.ID] = source
|
||||
if source.Kind == ck.SourceKindFile && source.Path != "" {
|
||||
paths[source.Path] = struct{}{}
|
||||
if path := hostPath(source); path != "" {
|
||||
paths[path] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
metadata := inspection.Metadata()
|
||||
for _, path := range metadata.HistoricalPaths() {
|
||||
segments := strings.Split(strings.TrimPrefix(path.String(), "/"), "/")
|
||||
if len(segments) != 6 || segments[0] != "prompts" || segments[3] != "messages" || segments[5] != "content_file" {
|
||||
// Prompt resources are lists at the root or inside a collection.
|
||||
if len(segments) == 7 && segments[0] == "collections" {
|
||||
segments = segments[2:]
|
||||
}
|
||||
if len(segments) != 5 || segments[0] != "prompts" || segments[2] != "messages" || segments[4] != "content_file" {
|
||||
continue
|
||||
}
|
||||
if messageIndex, err := strconv.Atoi(segments[4]); err != nil || messageIndex < 0 {
|
||||
if _, err := strconv.Atoi(segments[1]); err != nil {
|
||||
continue
|
||||
}
|
||||
if _, err := strconv.Atoi(segments[3]); err != nil {
|
||||
continue
|
||||
}
|
||||
field, ok := metadata.Lookup(path)
|
||||
@@ -42,7 +58,7 @@ func capturedSourcePaths(engine *pk.Engine) ([]string, error) {
|
||||
continue
|
||||
}
|
||||
source, ok := byID[field.Origin.Locations[0].SourceID]
|
||||
if !ok || source.Kind != ck.SourceKindFile || source.Path == "" {
|
||||
if !ok || hostPath(source) == "" {
|
||||
continue
|
||||
}
|
||||
explanation, ok := inspection.Explain(path)
|
||||
@@ -53,7 +69,7 @@ func capturedSourcePaths(engine *pk.Engine) ([]string, error) {
|
||||
if err := yaml.Unmarshal([]byte(explanation.Effective.YAML), &resource); err != nil {
|
||||
return nil, fmt.Errorf("decode captured message resource at %s: %w", path.String(), err)
|
||||
}
|
||||
paths[filepath.Join(filepath.Dir(source.Path), filepath.FromSlash(resource))] = struct{}{}
|
||||
paths[filepath.Join(filepath.Dir(hostPath(source)), filepath.FromSlash(resource))] = struct{}{}
|
||||
}
|
||||
|
||||
result := make([]string, 0, len(paths))
|
||||
|
||||
Reference in New Issue
Block a user