Construct prompt engines from shared configuration

This commit is contained in:
2026-11-23 04:23:59 +00:00
parent 1ca5011049
commit 9977cd9130
21 changed files with 153 additions and 253 deletions

View File

@@ -1,23 +1,16 @@
package llm
import (
"bytes"
"fmt"
"path/filepath"
"sort"
"gitea.maximumdirect.net/eric/audita/v4/internal/appconfig"
ck "go.maximumdirect.net/configkit"
pk "go.maximumdirect.net/promptkit"
"go.yaml.in/yaml/v4"
)
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()
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 {
@@ -26,31 +19,8 @@ func capturedSourcePaths(engine *pk.Engine) ([]string, error) {
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()
capture := func(base ck.Path, prompts promptList) {
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 {
@@ -58,20 +28,21 @@ func capturedSourcePaths(engine *pk.Engine) ([]string, error) {
continue
}
field, ok := metadata.Lookup(messages.Index(j).Key("content_file"))
if !ok || len(field.Origin.Locations) != 1 {
if !ok {
continue
}
if host := hostByID[field.Origin.Locations[0].SourceID]; host != "" {
paths[filepath.Join(filepath.Dir(host), filepath.FromSlash(*message.ContentFile))] = struct{}{}
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("prompts"), effective.Prompts)
for name, collection := range effective.Collections {
capture(ck.Keys("collections", name, "prompts"), collection.Prompts)
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)