Capture prompt schema selections and source references
This commit is contained in:
@@ -54,20 +54,20 @@ func (state *loaded) materializePrompts() error {
|
||||
roots[name] = root
|
||||
}
|
||||
for _, resources := range resourceSets(&state.result.Config) {
|
||||
root := applicationRoot
|
||||
if selected := roots[resources.namespace]; resources.isCollection && selected != nil {
|
||||
root = selected
|
||||
}
|
||||
for index, raw := range resources.prompts {
|
||||
promptBase := resources.bases.prompts.Index(index)
|
||||
if strings.TrimSpace(raw.ID) == "" {
|
||||
return fmt.Errorf("%s: prompt ID must not be blank", promptBase)
|
||||
}
|
||||
fullID := indexedResourceID(resources.namespace, raw.ID)
|
||||
entry, err := state.materializePrompt(fullID, raw, promptBase, lookupOrigin)
|
||||
entry, err := state.materializePrompt(fullID, raw, promptBase, root, lookupOrigin)
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -78,30 +78,15 @@ func (state *loaded) captureSchemaRoot(root *string, fieldPath configkit.Path, l
|
||||
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
|
||||
bound, _, err := state.bindResource(fieldPath, *root, "schema_root", lookupOrigin)
|
||||
return bound, err
|
||||
}
|
||||
|
||||
func (state *loaded) materializePrompt(id string, raw promptConfig, base configkit.Path, lookupOrigin func(configkit.Path) (configkit.FieldInfo, bool)) (*promptEntry, error) {
|
||||
func (state *loaded) materializePrompt(id string, raw promptConfig, base configkit.Path, root *domain.ResourceBinding, 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"))
|
||||
}
|
||||
definition := domain.PromptDefinition{ID: id}
|
||||
definition := domain.PromptDefinition{ID: id, SchemaRoot: root}
|
||||
if raw.DefaultProfile != nil {
|
||||
if strings.TrimSpace(*raw.DefaultProfile) == "" {
|
||||
return nil, fmt.Errorf("%s: default profile must not be blank", base.Key("default_profile"))
|
||||
@@ -130,6 +115,22 @@ func (state *loaded) materializePrompt(id string, raw promptConfig, base configk
|
||||
definition.Templates = messages
|
||||
entry := &promptEntry{resource: raw, requirementInputs: inputMetadata, messages: compiledMessages}
|
||||
definition.Validation = contract
|
||||
if contract.ValidationMode == domain.ValidationJSONSchema {
|
||||
fieldPath := base.Key("output")
|
||||
if contract.SchemaFile != nil {
|
||||
definition.SchemaFile, _, err = state.bindResource(fieldPath.Key("schema_file"), *contract.SchemaFile, "schema_file", lookupOrigin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if contract.SchemaPath != "" {
|
||||
if root == nil {
|
||||
return nil, fmt.Errorf("%s: schema_path requires an applicable schema_root", fieldPath.Key("schema_path"))
|
||||
}
|
||||
if _, _, err := filecatalog.ResolveFSPath(root.Path, root.Path, contract.SchemaPath); err != nil {
|
||||
return nil, fmt.Errorf("%s: invalid schema_path: %w", fieldPath.Key("schema_path"), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if raw.SessionID != nil {
|
||||
var err error
|
||||
entry.session, err = prompt.CompileTemplate("session_id", definition.SessionID)
|
||||
@@ -248,8 +249,13 @@ func capturePromptOutput(output *promptOutputConfig, base configkit.Path) (domai
|
||||
Format: domain.OutputFormat(*output.Format),
|
||||
ValidationMode: domain.ValidationMode(*output.ValidationMode),
|
||||
RepairAttempts: output.RepairAttempts,
|
||||
Schema: jsonvalue.CloneTrusted(output.Schema),
|
||||
SchemaFile: stringPointerValue(output.SchemaFile),
|
||||
}
|
||||
if output.SchemaPath != nil {
|
||||
if contract.ValidationMode == domain.ValidationJSONSchema && strings.TrimSpace(*output.SchemaPath) == "" {
|
||||
return domain.OutputContract{}, fmt.Errorf("%s: schema_path must not be blank", base.Key("output").Key("schema_path"))
|
||||
}
|
||||
contract.SchemaPath = *output.SchemaPath
|
||||
}
|
||||
if err := domain.ValidateOutputContract(contract); err != nil {
|
||||
@@ -270,24 +276,32 @@ func stringPointerValue(value *string) *string {
|
||||
return stringPointer(*value)
|
||||
}
|
||||
|
||||
func (state *loaded) readContent(fieldPath configkit.Path, name string, lookupOrigin func(configkit.Path) (configkit.FieldInfo, bool)) (string, error) {
|
||||
func (state *loaded) bindResource(fieldPath configkit.Path, name, label string, lookupOrigin func(configkit.Path) (configkit.FieldInfo, bool)) (*domain.ResourceBinding, string, error) {
|
||||
if strings.TrimSpace(name) == "" {
|
||||
return "", fmt.Errorf("%s: content_file path must not be blank", fieldPath)
|
||||
return nil, "", fmt.Errorf("%s: %s path must not be blank", fieldPath, label)
|
||||
}
|
||||
field, found := lookupOrigin(fieldPath)
|
||||
if !found || len(field.Origin.Locations) != 1 {
|
||||
return "", fmt.Errorf("%s: content_file needs one contributing source with a filesystem context", fieldPath)
|
||||
return nil, "", fmt.Errorf("%s: %s needs one contributing source with a filesystem context", fieldPath, label)
|
||||
}
|
||||
sourceID := field.Origin.Locations[0].SourceID
|
||||
context, found := state.result.SourceContext(sourceID)
|
||||
if !found || context.FS == nil {
|
||||
return "", fmt.Errorf("%s: content_file source %s has no filesystem context", fieldPath, sourceID)
|
||||
return nil, "", fmt.Errorf("%s: %s source %s has no filesystem context", fieldPath, label, sourceID)
|
||||
}
|
||||
resolved, _, err := filecatalog.ResolveFSPath(".", context.BaseDir, name)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: content_file path is outside its source root or invalid: %w", fieldPath, err)
|
||||
return nil, "", fmt.Errorf("%s: %s path is outside its source root or invalid: %w", fieldPath, label, err)
|
||||
}
|
||||
file, err := context.FS.Open(resolved)
|
||||
return &domain.ResourceBinding{FS: context.FS, Path: resolved}, sourceID, nil
|
||||
}
|
||||
|
||||
func (state *loaded) readContent(fieldPath configkit.Path, name string, lookupOrigin func(configkit.Path) (configkit.FieldInfo, bool)) (string, error) {
|
||||
binding, sourceID, err := state.bindResource(fieldPath, name, "content_file", lookupOrigin)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
file, err := binding.FS.Open(binding.Path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: open content_file from source %s: %w", fieldPath, sourceID, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user