Bind schema selections throughout execution

This commit is contained in:
2026-11-20 15:36:44 +00:00
parent 2d96dfa696
commit 6fbbaa9cfa
20 changed files with 218 additions and 66 deletions

View File

@@ -24,18 +24,17 @@ func (rejectWireSchemaLoad) Load(uri string) (any, error) {
}
func TestSchemaFilePreparationAndNativeSubmission(t *testing.T) {
firstDir, secondDir := t.TempDir(), t.TempDir()
first, second := filepath.Join(firstDir, "schema.json"), filepath.Join(secondDir, "schema.json")
if err := os.WriteFile(first, []byte(`{"type":"string"}`), 0600); err != nil {
t.Fatal(err)
}
secondDir := t.TempDir()
second := filepath.Join(secondDir, "schema.json")
if err := os.WriteFile(second, []byte(`{"$defs":{"value":{"type":"integer"}},"$ref":"#/$defs/value"}`), 0600); err != nil {
t.Fatal(err)
}
input := configkit.Input{Source: configkit.Mapping("schema test", map[string]any{
"profiles": []any{map[string]any{"id": "profile", "endpoint": "https://example.invalid/v1", "model": "model"}},
"prompts": []any{map[string]any{"id": "prompt", "default_profile": "profile", "messages": []any{map[string]any{"role": "user", "content": "hello"}}, "output": map[string]any{"format": "json", "validation_mode": "json_schema", "schema_path": "schema.json"}}},
"prompts": []any{map[string]any{"id": "prompt", "default_profile": "profile", "messages": []any{map[string]any{"role": "user", "content": "hello"}}, "output": map[string]any{"format": "json", "validation_mode": "json_schema", "schema_file": "schema.json"}}},
})}
filesystem := configkit.NewFilesystem("schema", os.DirFS(secondDir))
input.Filesystem = &configkit.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}
var payload map[string]any
client := &http.Client{Transport: roundTripFunc(func(request *http.Request) (*http.Response, error) {
if err := json.NewDecoder(request.Body).Decode(&payload); err != nil {
@@ -43,7 +42,7 @@ func TestSchemaFilePreparationAndNativeSubmission(t *testing.T) {
}
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`{"choices":[{"message":{"content":"3"}}]}`))}, nil
})}
engine, err := promptkit.NewEngine(promptkit.Config{HTTPClient: client, Sources: promptkit.ConfigurationSources{Main: &input}}, promptkit.WithSchemaFile(first), promptkit.WithSchemaFile(second))
engine, err := promptkit.NewEngine(promptkit.Config{HTTPClient: client, Sources: promptkit.ConfigurationSources{Main: &input}})
if err != nil {
t.Fatal(err)
}
@@ -91,9 +90,11 @@ func TestSchemaFilePreparationDetachesLargeAnnotation(t *testing.T) {
}
input := configkit.Input{Source: configkit.Mapping("schema test", map[string]any{
"profiles": []any{map[string]any{"id": "profile", "endpoint": "https://example.invalid/v1", "model": "model"}},
"prompts": []any{map[string]any{"id": "prompt", "default_profile": "profile", "messages": []any{map[string]any{"role": "user", "content": "hello"}}, "output": map[string]any{"format": "json", "validation_mode": "json_schema", "schema_path": "schema.json"}}},
"prompts": []any{map[string]any{"id": "prompt", "default_profile": "profile", "messages": []any{map[string]any{"role": "user", "content": "hello"}}, "output": map[string]any{"format": "json", "validation_mode": "json_schema", "schema_file": "schema.json"}}},
})}
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &input}}, promptkit.WithSchemaFile(path))
filesystem := configkit.NewFilesystem("schema", os.DirFS(filepath.Dir(path)))
input.Filesystem = &configkit.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &input}})
if err != nil {
t.Fatal(err)
}
@@ -115,22 +116,19 @@ func TestSchemaFilePreparationDetachesLargeAnnotation(t *testing.T) {
func TestSchemaFileRejectsInvalidSourcesAndExternalReferences(t *testing.T) {
dir := t.TempDir()
for _, name := range []string{filepath.Join(dir, "missing.json"), dir} {
if _, err := promptkit.NewEngine(promptkit.Config{}, promptkit.WithSchemaFile(name)); !errors.Is(err, promptkit.ErrInvalidConfig) {
t.Fatalf("source %q = %v", name, err)
}
}
root := filepath.Join(dir, "schema.json")
if err := os.WriteFile(root, []byte(`{"$defs":{"unused":{"$ref":"other.json"}}}`), 0600); err != nil {
t.Fatal(err)
}
input := configkit.Input{Source: configkit.Mapping("schema test", map[string]any{
"profiles": []any{map[string]any{"id": "profile", "endpoint": "https://example.invalid/v1", "model": "model"}},
"prompts": []any{map[string]any{"id": "prompt", "default_profile": "profile", "messages": []any{map[string]any{"role": "user", "content": "hello"}}, "output": map[string]any{"format": "json", "validation_mode": "json_schema", "schema_path": "schema.json"}}},
"prompts": []any{map[string]any{"id": "prompt", "default_profile": "profile", "messages": []any{map[string]any{"role": "user", "content": "hello"}}, "output": map[string]any{"format": "json", "validation_mode": "json_schema", "schema_file": "schema.json"}}},
})}
filesystem := configkit.NewFilesystem("schema", os.DirFS(dir))
input.Filesystem = &configkit.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}
calls := 0
client := &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) { calls++; return nil, errors.New("unexpected dispatch") })}
engine, err := promptkit.NewEngine(promptkit.Config{HTTPClient: client, Sources: promptkit.ConfigurationSources{Main: &input}}, promptkit.WithSchemaFile(root))
engine, err := promptkit.NewEngine(promptkit.Config{HTTPClient: client, Sources: promptkit.ConfigurationSources{Main: &input}})
if err != nil {
t.Fatal(err)
}