Add schema selection values and ownership
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user