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

53 lines
1.5 KiB
Go

package llm
import (
"path/filepath"
"sort"
"gitea.maximumdirect.net/eric/audita/v4/internal/appconfig"
ck "go.maximumdirect.net/configkit"
pk "go.maximumdirect.net/promptkit"
)
func capturedSourcePaths(loaded *ck.Result[appconfig.Config]) ([]string, error) {
sources := loaded.Sources()
hostByID := make(map[string]string, len(sources))
paths := make(map[string]struct{}, len(sources))
for _, source := range sources {
if source.Kind == ck.SourceKindFile && source.Path != "" {
hostByID[source.ID] = source.Path
paths[source.Path] = struct{}{}
}
}
metadata := loaded.Metadata()
capture := func(base ck.Path, prompts []pk.PromptSettings) {
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 {
continue
}
for _, location := range field.Origin.Locations {
if host := hostByID[location.SourceID]; host != "" {
paths[filepath.Join(filepath.Dir(host), filepath.FromSlash(*message.ContentFile))] = struct{}{}
}
}
}
}
}
capture(ck.Keys("promptkit", "prompts"), loaded.Config.PromptKit.Prompts)
for name, collection := range loaded.Config.PromptKit.Collections {
capture(ck.Keys("promptkit", "collections", name, "prompts"), collection.Prompts)
}
result := make([]string, 0, len(paths))
for path := range paths {
result = append(result, path)
}
sort.Strings(result)
return result, nil
}