Files
audita/internal/framework/llm/source_paths.go

66 lines
1.9 KiB
Go

package llm
import (
"fmt"
"path/filepath"
"sort"
"strconv"
"strings"
ck "gitea.maximumdirect.net/eric/ConfigKit"
pk "gitea.maximumdirect.net/eric/promptkit"
"gopkg.in/yaml.v3"
)
func capturedSourcePaths(engine *pk.Engine) ([]string, error) {
inspection, err := engine.InspectConfiguration()
if err != nil {
return nil, fmt.Errorf("inspect configuration: %w", err)
}
sources := inspection.Sources()
byID := make(map[string]ck.SourceInfo, len(sources))
paths := make(map[string]struct{}, len(sources))
for _, source := range sources {
byID[source.ID] = source
if source.Kind == ck.SourceKindFile && source.Path != "" {
paths[source.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" {
continue
}
if messageIndex, err := strconv.Atoi(segments[4]); err != nil || messageIndex < 0 {
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 || source.Kind != ck.SourceKindFile || source.Path == "" {
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(source.Path), filepath.FromSlash(resource))] = struct{}{}
}
result := make([]string, 0, len(paths))
for path := range paths {
result = append(result, path)
}
sort.Strings(result)
return result, nil
}