Collect prompt file paths from effective configuration

This commit is contained in:
2026-11-10 20:35:35 +00:00
parent 7710101da0
commit bb63c2cb10
7 changed files with 87 additions and 68 deletions

View File

@@ -1,11 +1,10 @@
package llm
import (
"bytes"
"fmt"
"path/filepath"
"sort"
"strconv"
"strings"
ck "go.maximumdirect.net/configkit"
pk "go.maximumdirect.net/promptkit"
@@ -19,54 +18,58 @@ func capturedSourcePaths(engine *pk.Engine) ([]string, error) {
}
sources := inspection.Sources()
byID := make(map[string]ck.SourceInfo, len(sources))
hostByID := make(map[string]string, len(sources))
paths := make(map[string]struct{}, len(sources))
hostPath := func(source ck.SourceInfo) string {
if source.Kind == ck.SourceKindFile {
return source.Path
}
return ""
}
for _, source := range sources {
byID[source.ID] = source
if path := hostPath(source); path != "" {
paths[path] = struct{}{}
if source.Kind == ck.SourceKindFile && source.Path != "" {
hostByID[source.ID] = source.Path
paths[source.Path] = struct{}{}
}
}
if len(hostByID) == 0 {
return []string{}, nil
}
type promptList []struct {
Messages []struct {
ContentFile *string `yaml:"content_file"`
} `yaml:"messages"`
}
var effective struct {
Prompts promptList `yaml:"prompts"`
Collections map[string]struct {
Prompts promptList `yaml:"prompts"`
} `yaml:"collections"`
}
var projection bytes.Buffer
if err := inspection.WriteEffective(&projection); err != nil {
return nil, fmt.Errorf("write effective PromptKit configuration: %w", err)
}
if err := yaml.Unmarshal(projection.Bytes(), &effective); err != nil {
return nil, fmt.Errorf("decode effective PromptKit message paths: %w", err)
}
metadata := inspection.Metadata()
for _, path := range metadata.HistoricalPaths() {
segments := strings.Split(strings.TrimPrefix(path.String(), "/"), "/")
// Prompt resources are lists at the root or inside a collection.
if len(segments) == 7 && segments[0] == "collections" {
segments = segments[2:]
capture := func(base ck.Path, prompts promptList) {
for i, prompt := range prompts {
messages := base.Index(i).Key("messages")
for j, message := range prompt.Messages {
if message.ContentFile == nil {
continue
}
field, ok := metadata.Lookup(messages.Index(j).Key("content_file"))
if !ok || len(field.Origin.Locations) != 1 {
continue
}
if host := hostByID[field.Origin.Locations[0].SourceID]; host != "" {
paths[filepath.Join(filepath.Dir(host), filepath.FromSlash(*message.ContentFile))] = struct{}{}
}
}
}
if len(segments) != 5 || segments[0] != "prompts" || segments[2] != "messages" || segments[4] != "content_file" {
continue
}
if _, err := strconv.Atoi(segments[1]); err != nil {
continue
}
if _, err := strconv.Atoi(segments[3]); err != nil {
continue
}
field, ok := metadata.Lookup(path)
if !ok || field.Null || len(field.Origin.Locations) != 1 {
continue
}
source, ok := byID[field.Origin.Locations[0].SourceID]
if !ok || hostPath(source) == "" {
continue
}
explanation, ok := inspection.Explain(path)
if !ok || !explanation.Effective.Present || explanation.Effective.Redacted {
continue
}
var resource string
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(hostPath(source)), filepath.FromSlash(resource))] = struct{}{}
}
capture(ck.Keys("prompts"), effective.Prompts)
for name, collection := range effective.Collections {
capture(ck.Keys("collections", name, "prompts"), collection.Prompts)
}
result := make([]string, 0, len(paths))