Add schema selection values and ownership
This commit is contained in:
@@ -103,6 +103,8 @@ type cacheControlConfig struct {
|
||||
type promptOutputConfig struct {
|
||||
Format *string `yaml:"format"`
|
||||
ValidationMode *string `yaml:"validation_mode"`
|
||||
Schema any `yaml:"schema"`
|
||||
SchemaFile *string `yaml:"schema_file"`
|
||||
SchemaPath *string `yaml:"schema_path"`
|
||||
RepairAttempts int `yaml:"repair_attempts"`
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit/internal/domain"
|
||||
"go.maximumdirect.net/promptkit/internal/filecatalog"
|
||||
"go.maximumdirect.net/promptkit/internal/jsonvalue"
|
||||
"go.maximumdirect.net/promptkit/internal/prompt"
|
||||
"go.maximumdirect.net/promptkit/internal/promptdef"
|
||||
)
|
||||
@@ -273,6 +274,7 @@ func (state *loaded) lookupPromptEntry(id string) (*promptEntry, error) {
|
||||
}
|
||||
|
||||
func ownPromptDefinition(definition domain.PromptDefinition) *domain.PromptDefinition {
|
||||
definition.Validation = ownOutputContract(definition.Validation)
|
||||
definition.Inputs = append([]domain.PromptInput(nil), definition.Inputs...)
|
||||
definition.Templates = append([]domain.PromptMessageTemplate(nil), definition.Templates...)
|
||||
for index := range definition.Templates {
|
||||
@@ -285,10 +287,17 @@ func ownPromptDefinition(definition domain.PromptDefinition) *domain.PromptDefin
|
||||
}
|
||||
|
||||
func ownPromptInspection(inspection domain.PromptInspection) *domain.PromptInspection {
|
||||
inspection.OutputContract = ownOutputContract(inspection.OutputContract)
|
||||
inspection.Inputs = append([]domain.PromptInput(nil), inspection.Inputs...)
|
||||
return &inspection
|
||||
}
|
||||
|
||||
func ownOutputContract(contract domain.OutputContract) domain.OutputContract {
|
||||
contract.Schema = jsonvalue.CloneTrusted(contract.Schema)
|
||||
contract.SchemaFile = stringPointerValue(contract.SchemaFile)
|
||||
return contract
|
||||
}
|
||||
|
||||
func promptLookupError(err error) error {
|
||||
if errors.Is(err, errPromptNotFound) {
|
||||
return promptdef.ErrPromptDefinitionNotFound
|
||||
|
||||
@@ -60,6 +60,8 @@ type PromptCacheControlResource struct {
|
||||
type PromptOutputResource struct {
|
||||
Format domain.OutputFormat
|
||||
ValidationMode domain.ValidationMode
|
||||
Schema any
|
||||
SchemaFile *string
|
||||
SchemaPath *string
|
||||
RepairAttempts *int
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package domain
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/fs"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -156,6 +157,8 @@ type PromptDefinition struct {
|
||||
Inputs []PromptInput `yaml:"inputs"`
|
||||
Templates []PromptMessageTemplate `yaml:"templates"`
|
||||
Validation OutputContract `yaml:"validation"`
|
||||
SchemaRoot *ResourceBinding `json:"-" yaml:"-"`
|
||||
SchemaFile *ResourceBinding `json:"-" yaml:"-"`
|
||||
}
|
||||
|
||||
// PromptInspection is lightweight metadata for prompt requirement selection.
|
||||
@@ -253,10 +256,18 @@ type ExecutionTarget struct {
|
||||
type OutputContract struct {
|
||||
Format OutputFormat `yaml:"format"`
|
||||
ValidationMode ValidationMode `yaml:"validation_mode"`
|
||||
Schema any `yaml:"schema,omitempty" json:"schema,omitempty"`
|
||||
SchemaFile *string `yaml:"schema_file,omitempty" json:"schema_file,omitempty"`
|
||||
SchemaPath string `yaml:"schema_path"`
|
||||
RepairAttempts int `yaml:"repair_attempts"`
|
||||
}
|
||||
|
||||
// ResourceBinding identifies a resource in its originating filesystem.
|
||||
type ResourceBinding struct {
|
||||
FS fs.FS
|
||||
Path string
|
||||
}
|
||||
|
||||
// RenderedPrompt represents the prompt after template application.
|
||||
type RenderedPrompt struct {
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
|
||||
@@ -22,8 +22,26 @@ func ValidateOutputContract(contract OutputContract) error {
|
||||
return fmt.Errorf("invalid validation mode: %q", contract.ValidationMode)
|
||||
}
|
||||
|
||||
if contract.ValidationMode == ValidationJSONSchema && strings.TrimSpace(contract.SchemaPath) == "" {
|
||||
return errors.New("schema_path is required when validation_mode is json_schema")
|
||||
if contract.ValidationMode == ValidationJSONSchema {
|
||||
count := 0
|
||||
if contract.Schema != nil {
|
||||
count++
|
||||
}
|
||||
if contract.SchemaFile != nil {
|
||||
if strings.TrimSpace(*contract.SchemaFile) == "" {
|
||||
return errors.New("schema_file must not be blank")
|
||||
}
|
||||
count++
|
||||
}
|
||||
if contract.SchemaPath != "" {
|
||||
if strings.TrimSpace(contract.SchemaPath) == "" {
|
||||
return errors.New("schema_path must not be blank")
|
||||
}
|
||||
count++
|
||||
}
|
||||
if count != 1 {
|
||||
return errors.New("exactly one of schema, schema_file, or schema_path is required when validation_mode is json_schema")
|
||||
}
|
||||
}
|
||||
if contract.RepairAttempts < 0 {
|
||||
return errors.New("repair_attempts must be greater than or equal to 0")
|
||||
|
||||
@@ -85,3 +85,34 @@ func TestValidateOutputContract(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaSelection(t *testing.T) {
|
||||
file, blank := "schema.json", " "
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
schema any
|
||||
file *string
|
||||
path string
|
||||
valid bool
|
||||
}{
|
||||
{"missing", nil, nil, "", false},
|
||||
{"false", false, nil, "", true},
|
||||
{"true", true, nil, "", true},
|
||||
{"empty object", map[string]any{}, nil, "", true},
|
||||
{"file", nil, &file, "", true},
|
||||
{"blank file", nil, &blank, "", false},
|
||||
{"conflict", false, &file, "", false},
|
||||
{"path conflict", map[string]any{}, nil, "schema.json", false},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
contract := OutputContract{Format: FormatJSON, ValidationMode: ValidationJSONSchema, Schema: tt.schema, SchemaFile: tt.file, SchemaPath: tt.path}
|
||||
if err := ValidateOutputContract(contract); (err == nil) != tt.valid {
|
||||
t.Fatalf("active selection error = %v", err)
|
||||
}
|
||||
contract.ValidationMode = ValidationNone
|
||||
if err := ValidateOutputContract(contract); err != nil {
|
||||
t.Fatalf("inactive selection: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,26 @@ func newOutputContractTestRunner() (*Runner, outputContractTestCollaborators) {
|
||||
), collaborators
|
||||
}
|
||||
|
||||
func TestResolveInlineSchemaOwnsRuntimeValue(t *testing.T) {
|
||||
value := map[string]any{"properties": map[string]any{"name": map[string]any{"type": "string"}}}
|
||||
override := domain.OutputContract{Format: domain.FormatJSON, ValidationMode: domain.ValidationJSONSchema, Schema: value}
|
||||
contract, err := resolveOutputContract(context.Background(), &domain.PromptDefinition{}, &override)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value["properties"].(map[string]any)["name"] = false
|
||||
if _, ok := contract.Schema.(map[string]any)["properties"].(map[string]any)["name"].(map[string]any); !ok {
|
||||
t.Fatal("schema shares caller storage")
|
||||
}
|
||||
var null map[string]any
|
||||
override.Schema = null
|
||||
override.SchemaPath = "schema.json"
|
||||
contract, err = resolveOutputContract(context.Background(), &domain.PromptDefinition{}, &override)
|
||||
if err != nil || contract.Schema != nil {
|
||||
t.Fatalf("typed null selection: %#v, %v", contract.Schema, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerPreparationNormalizesOutputContractConsistently(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -351,7 +351,7 @@ func (r *Runner) resolvePreparation(
|
||||
recorder.UpdateSelection(observation.Selection{PromptID: def.ID})
|
||||
}
|
||||
promptDefinitionHash := promptSelection.hash
|
||||
effectiveContract, err := resolveOutputContract(def, req.Validation)
|
||||
effectiveContract, err := resolveOutputContract(ctx, def, req.Validation)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: output contract: %v", ErrInvalidRequest, err)
|
||||
}
|
||||
@@ -649,13 +649,20 @@ func validateAPIKey(apiKeyEnv string, apiKey string, apiKeyRequired bool) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func resolveOutputContract(def *domain.PromptDefinition, override *domain.OutputContract) (domain.OutputContract, error) {
|
||||
func resolveOutputContract(ctx context.Context, def *domain.PromptDefinition, override *domain.OutputContract) (domain.OutputContract, error) {
|
||||
contract := def.Validation
|
||||
if override != nil {
|
||||
contract = *override
|
||||
if contract.Format == "" {
|
||||
contract.Format = domain.FormatText
|
||||
}
|
||||
if contract.ValidationMode == domain.ValidationJSONSchema && contract.Schema != nil {
|
||||
value, err := validate.DecodeInlineSchema(ctx, contract.Schema)
|
||||
if err != nil {
|
||||
return domain.OutputContract{}, fmt.Errorf("invalid inline schema value: %w", err)
|
||||
}
|
||||
contract.Schema = value
|
||||
}
|
||||
}
|
||||
if err := domain.ValidateOutputContract(contract); err != nil {
|
||||
return domain.OutputContract{}, err
|
||||
|
||||
@@ -183,6 +183,15 @@ func validateArtifact(ctx context.Context, artifact *domain.Artifact, contract d
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeInlineSchema converts a caller-supplied value to an owned JSON document.
|
||||
func DecodeInlineSchema(ctx context.Context, value any) (any, error) {
|
||||
encoded, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeJSONValue(ctx, encoded)
|
||||
}
|
||||
|
||||
func decodeJSONValue(ctx context.Context, body []byte) (any, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -6,6 +6,12 @@ import (
|
||||
"go.maximumdirect.net/promptkit/internal/domain"
|
||||
)
|
||||
|
||||
// Input describes the selected contract and its optional bound schema file.
|
||||
type Input struct {
|
||||
Contract domain.OutputContract
|
||||
File *domain.ResourceBinding
|
||||
}
|
||||
|
||||
// PreparedValidation validates artifacts against one frozen output contract.
|
||||
// Its cancellation boundary is synchronous: Validate does not detach schema
|
||||
// execution, and an observed context error prevents publication of a result.
|
||||
|
||||
Reference in New Issue
Block a user