Capture schema roots from configuration sources
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
||||
|
||||
type document struct {
|
||||
Version *string `yaml:"version"`
|
||||
SchemaRoot *string `yaml:"schema_root"`
|
||||
Backends []backendConfig `yaml:"backends"`
|
||||
Profiles []profileConfig `yaml:"profiles"`
|
||||
Prompts []promptConfig `yaml:"prompts"`
|
||||
@@ -25,6 +26,7 @@ type document struct {
|
||||
|
||||
type collectionConfig struct {
|
||||
ReleaseVersion *string `yaml:"release_version"`
|
||||
SchemaRoot *string `yaml:"schema_root"`
|
||||
Backends []backendConfig `yaml:"backends"`
|
||||
Profiles []profileConfig `yaml:"profiles"`
|
||||
Prompts []promptConfig `yaml:"prompts"`
|
||||
|
||||
@@ -40,6 +40,19 @@ func (state *loaded) materializePrompts() error {
|
||||
}
|
||||
return metadata.Lookup(path)
|
||||
}
|
||||
applicationRoot, err := state.captureSchemaRoot(state.result.Config.SchemaRoot, configkit.Keys("schema_root"), lookupOrigin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
roots := make(map[string]*domain.ResourceBinding, len(state.result.Config.Collections))
|
||||
for _, name := range sortedKeys(state.result.Config.Collections) {
|
||||
collection := state.result.Config.Collections[name]
|
||||
root, err := state.captureSchemaRoot(collection.SchemaRoot, configkit.Keys("collections", name, "schema_root"), lookupOrigin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
roots[name] = root
|
||||
}
|
||||
for _, resources := range resourceSets(&state.result.Config) {
|
||||
for index, raw := range resources.prompts {
|
||||
promptBase := resources.bases.prompts.Index(index)
|
||||
@@ -51,12 +64,39 @@ func (state *loaded) materializePrompts() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
entry.definition.SchemaRoot = applicationRoot
|
||||
if root := roots[resources.namespace]; resources.isCollection && root != nil {
|
||||
entry.definition.SchemaRoot = root
|
||||
}
|
||||
state.prompts[fullID] = entry
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (state *loaded) captureSchemaRoot(root *string, fieldPath configkit.Path, lookupOrigin func(configkit.Path) (configkit.FieldInfo, bool)) (*domain.ResourceBinding, error) {
|
||||
if root == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if strings.TrimSpace(*root) == "" {
|
||||
return nil, fmt.Errorf("%s: schema_root must not be blank", fieldPath)
|
||||
}
|
||||
field, found := lookupOrigin(fieldPath)
|
||||
if !found || len(field.Origin.Locations) != 1 {
|
||||
return nil, fmt.Errorf("%s: schema_root needs one contributing source with a filesystem context", fieldPath)
|
||||
}
|
||||
sourceID := field.Origin.Locations[0].SourceID
|
||||
context, found := state.result.SourceContext(sourceID)
|
||||
if !found || context.FS == nil {
|
||||
return nil, fmt.Errorf("%s: schema_root source %s has no filesystem context", fieldPath, sourceID)
|
||||
}
|
||||
resolved, _, err := filecatalog.ResolveFSPath(".", context.BaseDir, *root)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: invalid schema_root: %w", fieldPath, err)
|
||||
}
|
||||
return &domain.ResourceBinding{FS: context.FS, Path: resolved}, nil
|
||||
}
|
||||
|
||||
func (state *loaded) materializePrompt(id string, raw promptConfig, base configkit.Path, lookupOrigin func(configkit.Path) (configkit.FieldInfo, bool)) (*promptEntry, error) {
|
||||
if len(raw.Messages) == 0 {
|
||||
return nil, fmt.Errorf("%s: at least one message is required", base.Key("messages"))
|
||||
@@ -275,6 +315,14 @@ func (state *loaded) lookupPromptEntry(id string) (*promptEntry, error) {
|
||||
|
||||
func ownPromptDefinition(definition domain.PromptDefinition) *domain.PromptDefinition {
|
||||
definition.Validation = ownOutputContract(definition.Validation)
|
||||
if definition.SchemaRoot != nil {
|
||||
root := *definition.SchemaRoot
|
||||
definition.SchemaRoot = &root
|
||||
}
|
||||
if definition.SchemaFile != nil {
|
||||
file := *definition.SchemaFile
|
||||
definition.SchemaFile = &file
|
||||
}
|
||||
definition.Inputs = append([]domain.PromptInput(nil), definition.Inputs...)
|
||||
definition.Templates = append([]domain.PromptMessageTemplate(nil), definition.Templates...)
|
||||
for index := range definition.Templates {
|
||||
|
||||
102
internal/configuration/schema_roots_test.go
Normal file
102
internal/configuration/schema_roots_test.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
type countingSchemaFS struct {
|
||||
opens int
|
||||
files fstest.MapFS
|
||||
}
|
||||
|
||||
func (f *countingSchemaFS) Open(name string) (fs.File, error) {
|
||||
if name != "." && name != "app" && name != "operator" {
|
||||
f.opens++
|
||||
}
|
||||
return f.files.Open(name)
|
||||
}
|
||||
|
||||
func TestSchemaRootsCaptureEffectiveContexts(t *testing.T) {
|
||||
first := &countingSchemaFS{files: fstest.MapFS{"app/placeholder": {Data: []byte("application")}}}
|
||||
second := &countingSchemaFS{files: fstest.MapFS{"operator/placeholder": {Data: []byte("operator")}}}
|
||||
firstFS := configkit.NewFilesystem("application", first)
|
||||
secondFS := configkit.NewFilesystem("override", second)
|
||||
prompt := map[string]any{"id": "p", "messages": []any{map[string]any{"role": "user", "content": "hello"}}, "output": map[string]any{"format": "text", "validation_mode": "none"}}
|
||||
base := configkit.Input{Source: configkit.Mapping("base", map[string]any{
|
||||
"schema_root": "schemas", "prompts": []any{prompt},
|
||||
"collections": map[string]any{"alpha": map[string]any{"schema_root": "old", "prompts": []any{prompt}}, "beta": map[string]any{"schema_root": "old", "prompts": []any{prompt}}},
|
||||
}), Filesystem: &configkit.FilesystemContext{FS: &firstFS, Root: ".", BaseDir: "app"}}
|
||||
override := configkit.Input{Source: configkit.Mapping("override", map[string]any{"collections": map[string]any{"alpha": map[string]any{"schema_root": "new"}, "beta": map[string]any{"schema_root": nil}}}), Filesystem: &configkit.FilesystemContext{FS: &secondFS, Root: ".", BaseDir: "operator"}}
|
||||
state, err := load(override, []configkit.Underlay{{Inputs: []configkit.Input{base}}})
|
||||
if err != nil {
|
||||
var loadErr *configkit.LoadError
|
||||
if errors.As(err, &loadErr) {
|
||||
t.Fatalf("%v: %+v", err, loadErr.Diagnostics())
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
for id, want := range map[string]string{"p": "app/schemas", "alpha/p": "operator/new", "beta/p": "app/schemas"} {
|
||||
definition, _, err := state.prompt(id)
|
||||
if err != nil || definition.SchemaRoot == nil || definition.SchemaRoot.Path != want {
|
||||
t.Fatalf("%s root = %+v, %v", id, definition, err)
|
||||
}
|
||||
}
|
||||
if first.opens != 0 || second.opens != 0 {
|
||||
t.Fatalf("root capture opened files: %d, %d", first.opens, second.opens)
|
||||
}
|
||||
for id, resource := range map[string]string{"p": "app/placeholder", "alpha/p": "operator/placeholder", "beta/p": "app/placeholder"} {
|
||||
definition, _, err := state.prompt(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
content, err := fs.ReadFile(definition.SchemaRoot.FS, resource)
|
||||
want := "application"
|
||||
if id == "alpha/p" {
|
||||
want = "operator"
|
||||
}
|
||||
if err != nil || string(content) != want {
|
||||
t.Fatalf("%s source = %q, %v", id, content, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaRootRequiresValueAndContext(t *testing.T) {
|
||||
filesystem := configkit.NewFilesystem("fixture", fstest.MapFS{})
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
root any
|
||||
context *configkit.FilesystemContext
|
||||
valid bool
|
||||
}{
|
||||
{"absent", nil, nil, true},
|
||||
{"blank", " ", &configkit.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}, false},
|
||||
{"missing context", "schemas", nil, false},
|
||||
{"explicit dot", ".", &configkit.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
input := configkit.Input{Source: configkit.Mapping("root", map[string]any{"schema_root": tc.root}), Filesystem: tc.context}
|
||||
_, err := load(input, nil)
|
||||
if (err == nil) != tc.valid {
|
||||
var loadErr *configkit.LoadError
|
||||
if errors.As(err, &loadErr) {
|
||||
t.Fatalf("%v: %+v", err, loadErr.Diagnostics())
|
||||
}
|
||||
t.Fatalf("root error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaRootValidationUsesEffectiveValue(t *testing.T) {
|
||||
filesystem := configkit.NewFilesystem("fixture", fstest.MapFS{})
|
||||
underlay := configkit.Input{Source: configkit.Mapping("old", map[string]any{"schema_root": " "})}
|
||||
main := configkit.Input{Source: configkit.Mapping("new", map[string]any{"schema_root": "."}), Filesystem: &configkit.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}}
|
||||
if _, err := load(main, []configkit.Underlay{{Inputs: []configkit.Input{underlay}}}); err != nil {
|
||||
t.Fatalf("overwritten invalid root was validated: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user