Upgrade PromptKit and assemble native configuration sources
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/readonlyfs"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
@@ -99,43 +100,43 @@ func (r *AssetRegistry) FallbackProfileDigest() (string, error) {
|
||||
return digest, err
|
||||
}
|
||||
|
||||
func (r *AssetRegistry) PromptKitOptions() ([]promptkit.Option, error) {
|
||||
options, _, err := r.promptKitOptions()
|
||||
return options, err
|
||||
}
|
||||
|
||||
func (r *AssetRegistry) promptKitOptions() ([]promptkit.Option, string, error) {
|
||||
promptFS, err := r.PromptFS()
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("prepare prompt assets: %w", err)
|
||||
// PromptKitConfigurationSources returns the registered prompt and fallback
|
||||
// profile assets as native PromptKit underlay groups. Schema assets remain a
|
||||
// separate PromptKit option because they are runtime validation resources.
|
||||
func (r *AssetRegistry) PromptKitConfigurationSources() (promptkit.ConfigurationSources, []promptkit.Option, error) {
|
||||
if r == nil {
|
||||
return promptkit.ConfigurationSources{}, nil, fmt.Errorf("asset registry must not be nil")
|
||||
}
|
||||
underlays, err := promptKitFilesystemUnderlays(r.prompts, "notarius-prompts")
|
||||
if err != nil {
|
||||
return promptkit.ConfigurationSources{}, nil, fmt.Errorf("prepare prompt configuration sources: %w", err)
|
||||
}
|
||||
profiles, err := promptKitFilesystemUnderlays(r.fallbackProfiles, "notarius-fallback-profiles")
|
||||
if err != nil {
|
||||
return promptkit.ConfigurationSources{}, nil, fmt.Errorf("prepare fallback profile configuration sources: %w", err)
|
||||
}
|
||||
underlays = append(underlays, profiles...)
|
||||
schemaFS, err := r.SchemaFS()
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("prepare schema assets: %w", err)
|
||||
return promptkit.ConfigurationSources{}, nil, fmt.Errorf("prepare schema assets: %w", err)
|
||||
}
|
||||
options := []promptkit.Option{
|
||||
promptkit.WithPromptFS(promptFS, "."),
|
||||
return promptkit.ConfigurationSources{Underlays: underlays}, []promptkit.Option{
|
||||
promptkit.WithSchemaFS(schemaFS, "."),
|
||||
}
|
||||
fallbackFS, fallbackDigest, hasFallback, err := r.fallbackProfileAssets()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if hasFallback {
|
||||
options = append(options, promptkit.WithFallbackProfileFS(fallbackFS, "."))
|
||||
}
|
||||
return options, fallbackDigest, nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *AssetRegistry) promptKitFallbackProfileOption() (promptkit.Option, bool, error) {
|
||||
fallbackFS, _, hasFallback, err := r.fallbackProfileAssets()
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
func promptKitFilesystemUnderlays(sources []AssetSource, label string) ([]configkit.Underlay, error) {
|
||||
underlays := make([]configkit.Underlay, 0, len(sources))
|
||||
for index, source := range sources {
|
||||
inputs, err := promptkit.FilesystemInputs(fmt.Sprintf("%s-%d", label, index), source.FS, source.Root)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select filesystem inputs: %w", err)
|
||||
}
|
||||
if len(inputs) != 0 {
|
||||
underlays = append(underlays, configkit.Underlay{Inputs: inputs})
|
||||
}
|
||||
}
|
||||
if !hasFallback {
|
||||
return nil, false, nil
|
||||
}
|
||||
return promptkit.WithFallbackProfileFS(fallbackFS, "."), true, nil
|
||||
return underlays, nil
|
||||
}
|
||||
|
||||
func (r *AssetRegistry) fallbackProfileAssets() (fs.FS, string, bool, error) {
|
||||
|
||||
54
internal/framework/llm/asset_registry_sources_test.go
Normal file
54
internal/framework/llm/asset_registry_sources_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func TestPromptKitConfigurationSourcesKeepsNativeFilesystemGroups(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
for index, name := range []string{"first", "second"} {
|
||||
files := fstest.MapFS{
|
||||
"prompts/prompt.yaml": {Data: []byte("prompts: {}\n")},
|
||||
}
|
||||
if err := registry.RegisterPromptFS(files, "."); err != nil {
|
||||
t.Fatalf("register prompt source %d (%s): %v", index, name, err)
|
||||
}
|
||||
}
|
||||
fallbackProfiles := fstest.MapFS{
|
||||
"profiles.yaml": {Data: []byte("profiles: {}\n")},
|
||||
}
|
||||
if err := registry.RegisterFallbackProfileFS(fallbackProfiles, "."); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
schemas := fstest.MapFS{"schema.json": {Data: []byte(`{"type":"object"}`)}}
|
||||
if err := registry.RegisterSchemaFS(schemas, "."); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sources, options, err := registry.PromptKitConfigurationSources()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sources.Main != nil {
|
||||
t.Fatalf("maintained sources unexpectedly set a main input: %+v", sources.Main)
|
||||
}
|
||||
if len(sources.Underlays) != 3 {
|
||||
t.Fatalf("underlays = %d, want two prompt groups and one profile group", len(sources.Underlays))
|
||||
}
|
||||
for index, underlay := range sources.Underlays {
|
||||
if len(underlay.Inputs) != 1 {
|
||||
t.Fatalf("underlay %d has %d inputs, want 1", index, len(underlay.Inputs))
|
||||
}
|
||||
input := underlay.Inputs[0]
|
||||
if input.Imports {
|
||||
t.Fatalf("maintained input %d unexpectedly enables imports", index)
|
||||
}
|
||||
if input.Filesystem == nil || input.Filesystem.FS == nil {
|
||||
t.Fatalf("maintained input %d has no native filesystem context", index)
|
||||
}
|
||||
}
|
||||
if len(options) != 1 {
|
||||
t.Fatalf("runtime options = %d, want one schema option", len(options))
|
||||
}
|
||||
}
|
||||
@@ -56,10 +56,6 @@ type LLMProfileRecorder struct {
|
||||
var _ contracts.StructuredLLMClient = (*PromptKitClient)(nil)
|
||||
var _ contracts.LLMProfileManifestProvider = (*PromptKitClient)(nil)
|
||||
|
||||
func PromptKitLocalBackendOption(cfg PromptKitLocalBackendConfig) promptkit.Option {
|
||||
return promptkit.WithBackend(promptkit.LocalBackend(cfg.Endpoint, cfg.ConcurrencyLimit))
|
||||
}
|
||||
|
||||
func NewPromptKitClient(cfg PromptKitClientConfig) (*PromptKitClient, error) {
|
||||
if cfg.Assets == nil {
|
||||
return nil, fmt.Errorf("PromptKit client assets must not be nil")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
@@ -121,13 +122,13 @@ func (i *PromptKitProfileInspector) InspectProfile(ctx context.Context, profileI
|
||||
}, nil
|
||||
}
|
||||
|
||||
func promptKitProfileSourceEngineOptions(cfg PromptKitProfileSourceConfig) (PromptKitProfileSourceConfig, []promptkit.Option, error) {
|
||||
func promptKitProfileConfigurationSources(cfg PromptKitProfileSourceConfig) (PromptKitProfileSourceConfig, promptkit.ConfigurationSources, error) {
|
||||
source := PromptKitProfileSourceConfig{
|
||||
ProfileDir: strings.TrimSpace(cfg.ProfileDir),
|
||||
ProfileFile: strings.TrimSpace(cfg.ProfileFile),
|
||||
}
|
||||
if source.ProfileDir != "" && source.ProfileFile != "" {
|
||||
return PromptKitProfileSourceConfig{}, nil, fmt.Errorf("PromptKit profile_dir and profile_file are mutually exclusive")
|
||||
return PromptKitProfileSourceConfig{}, promptkit.ConfigurationSources{}, fmt.Errorf("PromptKit profile_dir and profile_file are mutually exclusive")
|
||||
}
|
||||
if cfg.LocalBackend != nil {
|
||||
localBackend := *cfg.LocalBackend
|
||||
@@ -135,12 +136,31 @@ func promptKitProfileSourceEngineOptions(cfg PromptKitProfileSourceConfig) (Prom
|
||||
source.LocalBackend = &localBackend
|
||||
}
|
||||
|
||||
var options []promptkit.Option
|
||||
var underlays []configkit.Underlay
|
||||
if source.ProfileFile != "" {
|
||||
options = append(options, promptkit.WithProfileFile(source.ProfileFile))
|
||||
underlays = append(underlays, configkit.Underlay{Inputs: []configkit.Input{{
|
||||
Source: configkit.File(source.ProfileFile),
|
||||
Imports: true,
|
||||
}}})
|
||||
} else if source.ProfileDir != "" {
|
||||
inputs, err := promptkit.DirectoryInputs(source.ProfileDir)
|
||||
if err != nil {
|
||||
return PromptKitProfileSourceConfig{}, promptkit.ConfigurationSources{}, fmt.Errorf("select PromptKit profile directory: %w", err)
|
||||
}
|
||||
for index := range inputs {
|
||||
inputs[index].Imports = true
|
||||
}
|
||||
if len(inputs) != 0 {
|
||||
underlays = append(underlays, configkit.Underlay{Inputs: inputs})
|
||||
}
|
||||
}
|
||||
if source.LocalBackend != nil {
|
||||
options = append(options, PromptKitLocalBackendOption(*source.LocalBackend))
|
||||
backend := promptkit.LocalBackend(source.LocalBackend.Endpoint, source.LocalBackend.ConcurrencyLimit)
|
||||
underlays = append(underlays, configkit.Underlay{Inputs: []configkit.Input{{
|
||||
Source: promptkit.BackendSource("notarius-local-backend", map[string]promptkit.BackendConfig{
|
||||
promptkit.BackendLocal: backend,
|
||||
}),
|
||||
}}})
|
||||
}
|
||||
return source, options, nil
|
||||
return source, promptkit.ConfigurationSources{Underlays: underlays}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user