Mount configuration fragments under PromptKit
This commit is contained in:
60
configuration_mounts_test.go
Normal file
60
configuration_mounts_test.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package promptkit_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"testing"
|
||||||
|
"testing/fstest"
|
||||||
|
|
||||||
|
"go.maximumdirect.net/configkit"
|
||||||
|
"go.maximumdirect.net/promptkit"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mountApplication struct {
|
||||||
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type countingMountFS struct {
|
||||||
|
fs.FS
|
||||||
|
opens int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *countingMountFS) Open(name string) (fs.File, error) { c.opens++; return c.FS.Open(name) }
|
||||||
|
|
||||||
|
func TestSettingsAndCollectionMounts(t *testing.T) {
|
||||||
|
files := &countingMountFS{FS: fstest.MapFS{
|
||||||
|
"fragments/root.yaml": {Data: []byte("profiles:\n - id: main\n model: model\n")},
|
||||||
|
"fragments/collection.yaml": {Data: []byte("prompts:\n - id: child\n messages: [{role: user, content_file: body.txt}]\n output: {format: text, validation_mode: basic}\n")},
|
||||||
|
"fragments/item.yaml": {Data: []byte("id: item\nmessages: [{role: user, content: hello}]\noutput: {format: text, validation_mode: basic}\n")},
|
||||||
|
"fragments/body.txt": {Data: []byte("message")},
|
||||||
|
}}
|
||||||
|
namespace := configkit.NewFilesystem("mounts", files)
|
||||||
|
root := promptkit.SettingsInput(namespace.File("fragments/root.yaml"))
|
||||||
|
collection, err := promptkit.CollectionInput("docs", namespace.File("fragments/collection.yaml"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
item := configkit.Input{Source: namespace.File("fragments/item.yaml"), Destination: configkit.Keys("promptkit", "prompts"), PlacementMode: configkit.PlacementModeSequenceItem}
|
||||||
|
for _, input := range []*configkit.Input{&root, &collection, &item} {
|
||||||
|
input.Filesystem = &configkit.FilesystemContext{FS: &namespace, Root: "."}
|
||||||
|
}
|
||||||
|
bundled := promptkit.BundledOpenRouterUnderlay()
|
||||||
|
if files.opens != 0 || root.Imports || !collection.Imports || root.Destination.String() != "/promptkit" || collection.Destination.String() != "/promptkit/collections/docs" {
|
||||||
|
t.Fatalf("mount construction read files or chose wrong defaults: opens=%d", files.opens)
|
||||||
|
}
|
||||||
|
definition := promptkit.Configure(configkit.Definition[mountApplication]{}, func(value *mountApplication) *promptkit.Settings { return &value.PromptKit })
|
||||||
|
result, err := configkit.LoadLayered(configkit.Input{Source: configkit.NoSource()}, configkit.LayeredOptions[mountApplication]{Definition: definition, Underlays: []configkit.Underlay{bundled, {Inputs: []configkit.Input{root, collection, item}}}})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(result.Config.PromptKit.Profiles) < 1 || len(result.Config.PromptKit.Prompts) != 1 || len(result.Config.PromptKit.Collections["docs"].Prompts) != 1 {
|
||||||
|
t.Fatalf("mounted settings = %+v", result.Config.PromptKit)
|
||||||
|
}
|
||||||
|
info, ok := result.Metadata().Lookup(configkit.Keys("promptkit", "collections", "docs", "prompts").Index(0).Key("messages").Index(0).Key("content_file"))
|
||||||
|
if !ok || len(info.Origin.Locations) != 1 {
|
||||||
|
t.Fatalf("origin = %+v", info)
|
||||||
|
}
|
||||||
|
context, ok := result.SourceContext(info.Origin.Locations[0].SourceID)
|
||||||
|
if !ok || context.FS == nil || context.BaseDir != "fragments" {
|
||||||
|
t.Fatalf("context = %+v", context)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,12 +8,13 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"go.maximumdirect.net/configkit"
|
"go.maximumdirect.net/configkit"
|
||||||
|
"go.maximumdirect.net/promptkit/internal/configuration"
|
||||||
"go.maximumdirect.net/promptkit/internal/domain"
|
"go.maximumdirect.net/promptkit/internal/domain"
|
||||||
"go.maximumdirect.net/promptkit/internal/filecatalog"
|
"go.maximumdirect.net/promptkit/internal/filecatalog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CollectionInput mounts source at the selected collection's configuration
|
// CollectionInput mounts source at the selected collection's configuration
|
||||||
// destination, collections/<namespace>, and enables ConfigKit imports for that
|
// destination, /promptkit/collections/<namespace>, and enables ConfigKit imports for that
|
||||||
// input. An invalid namespace returns an error matching ErrInvalidConfig. The
|
// input. An invalid namespace returns an error matching ErrInvalidConfig. The
|
||||||
// source is not read or validated until ConfigKit loads it. Callers may adjust
|
// source is not read or validated until ConfigKit loads it. Callers may adjust
|
||||||
// the returned input, including its filesystem context or imports setting.
|
// the returned input, including its filesystem context or imports setting.
|
||||||
@@ -23,11 +24,23 @@ func CollectionInput(namespace string, source configkit.Source) (configkit.Input
|
|||||||
}
|
}
|
||||||
return configkit.Input{
|
return configkit.Input{
|
||||||
Source: source,
|
Source: source,
|
||||||
Destination: configkit.Keys("collections", namespace),
|
Destination: configkit.Keys("promptkit", "collections", namespace),
|
||||||
Imports: true,
|
Imports: true,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SettingsInput mounts a wrapperless PromptKit settings fragment at /promptkit.
|
||||||
|
// It does not acquire the source; callers may adjust the returned input.
|
||||||
|
func SettingsInput(source configkit.Source) configkit.Input {
|
||||||
|
return configkit.Input{Source: source, Destination: configkit.Keys("promptkit")}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BundledOpenRouterUnderlay selects the bundled OpenRouter settings fragment.
|
||||||
|
// It does not acquire its source until ConfigKit loads it.
|
||||||
|
func BundledOpenRouterUnderlay() configkit.Underlay {
|
||||||
|
return configuration.BundledOpenRouterUnderlay()
|
||||||
|
}
|
||||||
|
|
||||||
// DirectoryInputs selects the YAML files beneath an explicitly chosen host
|
// DirectoryInputs selects the YAML files beneath an explicitly chosen host
|
||||||
// directory as one strict-composition group. The returned order is deterministic;
|
// directory as one strict-composition group. The returned order is deterministic;
|
||||||
// it does not assign precedence among those files. Discovery is recursive over
|
// it does not assign precedence among those files. Discovery is recursive over
|
||||||
|
|||||||
@@ -419,6 +419,9 @@ without custom redaction/history/diff implementations or access to typed config.
|
|||||||
|
|
||||||
## Stage 8 — Mount Bundles And Standalone Fragments Explicitly
|
## Stage 8 — Mount Bundles And Standalone Fragments Explicitly
|
||||||
|
|
||||||
|
Status: complete. Focused mounting tests passed for bundled, collection,
|
||||||
|
ordinary fragment, and sequence-item sources without eager acquisition.
|
||||||
|
|
||||||
Focus: `configuration_sources.go`, public source helpers, and
|
Focus: `configuration_sources.go`, public source helpers, and
|
||||||
`internal/configuration/bundled.go`.
|
`internal/configuration/bundled.go`.
|
||||||
|
|
||||||
|
|||||||
@@ -12,5 +12,5 @@ var bundledFiles embed.FS
|
|||||||
// BundledOpenRouterUnderlay returns the built-in OpenRouter configuration source.
|
// BundledOpenRouterUnderlay returns the built-in OpenRouter configuration source.
|
||||||
func BundledOpenRouterUnderlay() configkit.Underlay {
|
func BundledOpenRouterUnderlay() configkit.Underlay {
|
||||||
filesystem := configkit.NewFilesystem("Promptkit bundled OpenRouter", bundledFiles)
|
filesystem := configkit.NewFilesystem("Promptkit bundled OpenRouter", bundledFiles)
|
||||||
return configkit.Underlay{Inputs: []configkit.Input{{Source: filesystem.File("bundled/openrouter.yaml")}}}
|
return configkit.Underlay{Inputs: []configkit.Input{{Source: filesystem.File("bundled/openrouter.yaml"), Destination: configkit.Keys("promptkit")}}}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user