2026-07-23 20:17:18 +00:00
// Package shape validates required D&D scene description fields.
package shape
import (
"context"
"fmt"
"strings"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
2026-11-26 02:50:41 +00:00
scenefacts "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/scenedescriptions"
2026-07-23 20:17:18 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
)
const (
Key = "extract/dnd/scene-descriptions/shape"
ReasonCode = "invalid_scene_description_shape"
)
type Options struct { }
type Validator struct { }
var _ contracts . TypedValidator [ dnd . SceneDescriptionList ] = ( * Validator ) ( nil )
func New ( Options ) * Validator { return & Validator { } }
func ( v * Validator ) Name ( ) string { return Key }
func ( v * Validator ) ExecutionClass ( ) contracts . ExecutionClass {
return contracts . ExecutionClassDeterministic
}
func ( v * Validator ) Validate ( _ context . Context , req contracts . TypedValidationRequest [ dnd . SceneDescriptionList ] ) ( contracts . ValidationResult , error ) {
2026-08-29 01:24:45 +00:00
issues , corrections := assess ( req . Value , req . Stage == string ( pipeline . StageExtract ) )
if len ( issues ) != 0 {
return contracts . ValidationResult {
Approved : false ,
ReasonCode : ReasonCode ,
Message : diagnostics . Aggregate ( "invalid scene description shape" , issues ) ,
CorrectionGuidance : corrections . Guidance ( "Correct the rejected scene description and return the complete replacement response" ) ,
} , nil
2026-07-23 20:17:18 +00:00
}
return contracts . ValidationResult { Approved : true } , nil
}
2026-08-29 01:24:45 +00:00
func assess ( value dnd . SceneDescriptionList , exactlyOne bool ) ( [ ] string , diagnostics . Corrections ) {
2026-11-26 02:50:41 +00:00
var issues [ ] string
2026-08-29 01:24:45 +00:00
var corrections diagnostics . Corrections
2026-11-26 02:50:41 +00:00
facts := scenefacts . Check ( value )
if exactlyOne && len ( value . Scenes ) > 0 && len ( value . Scenes ) != 1 {
2026-07-23 20:17:18 +00:00
issues = append ( issues , "extraction must contain exactly one scene" )
2026-08-29 01:24:45 +00:00
corrections . Add ( "scene" , "Return exactly one scene description for the supplied extraction chunk." , "" )
2026-07-23 20:17:18 +00:00
}
2026-11-26 02:50:41 +00:00
for _ , fact := range facts {
if fact . Field == scenefacts . ScenesMissing || fact . Field == scenefacts . ScenesEmpty {
message := "scenes must be present"
if fact . Field == scenefacts . ScenesEmpty {
message = "scenes must not be empty"
}
issues = append ( issues , message )
corrections . Add ( "scene" , "Return one scene description for the supplied extraction chunk." , "" )
continue
}
scene := value . Scenes [ fact . Index ]
prefix := fmt . Sprintf ( "scenes[%d]" , fact . Index )
2026-08-29 01:24:45 +00:00
record := fmt . Sprintf ( "Affected scene titled %s with kind %s, citing %s." , diagnostics . Quote ( strings . TrimSpace ( scene . Title ) ) , diagnostics . Quote ( string ( scene . Kind ) ) , diagnostics . SourceRefRange ( scene . SourceRef ) )
2026-11-26 02:50:41 +00:00
switch fact . Field {
case scenefacts . ID :
2026-07-23 20:17:18 +00:00
issues = append ( issues , prefix + ".id must be non-empty and trimmed" )
2026-08-29 01:24:45 +00:00
corrections . Add ( "scene" , "Return exactly one scene description for the supplied extraction chunk." , record )
2026-11-26 02:50:41 +00:00
case scenefacts . Kind :
2026-07-23 20:17:18 +00:00
issues = append ( issues , prefix + ".kind is unsupported: " + diagnostics . Quote ( string ( scene . Kind ) ) )
2026-08-29 01:24:45 +00:00
corrections . Add ( "kind" , "Set `kind` to exactly one of `combat`, `narrative`, `recap`, or `meta`." , record )
2026-11-26 02:50:41 +00:00
case scenefacts . Title :
2026-07-23 20:17:18 +00:00
issues = append ( issues , prefix + ".title must be non-empty and trimmed" )
2026-08-29 01:24:45 +00:00
corrections . Add ( "title" , "Provide a concise, nonblank, trimmed title grounded in the supplied scene transcript." , record )
2026-11-26 02:50:41 +00:00
case scenefacts . Summary :
2026-07-23 20:17:18 +00:00
issues = append ( issues , prefix + ".summary must be non-empty and trimmed" )
2026-08-29 01:24:45 +00:00
corrections . Add ( "summary" , "Provide a concise, nonblank, trimmed summary grounded in the supplied scene transcript." , record )
2026-11-26 02:50:41 +00:00
case scenefacts . SourceRef :
2026-07-23 20:17:18 +00:00
issues = append ( issues , prefix + ".source_ref must have a trimmed source ID and positive unit IDs" )
2026-08-29 01:24:45 +00:00
corrections . Add ( "scene" , "Return exactly one scene description for the supplied extraction chunk." , record )
2026-07-23 20:17:18 +00:00
}
}
2026-08-29 01:24:45 +00:00
return issues , corrections
2026-07-23 20:17:18 +00:00
}
func Spec ( ) pipeline . ValidatorSpec {
return pipeline . ValidatorSpec { Key : Key , ExecutionClass : contracts . ExecutionClassDeterministic }
}
func Register ( registry * pipeline . ValidatorRegistry ) error {
return pipeline . RegisterTypedValidatorBuilder ( registry , dnd . SceneDescriptionListKind , Spec ( ) , validateOptions , func ( request pipeline . BuildRequest ) ( contracts . TypedValidator [ dnd . SceneDescriptionList ] , error ) {
options , err := DecodeOptions ( request . Options )
if err != nil {
return nil , err
}
return New ( options ) , nil
} )
}
func DecodeOptions ( options map [ string ] any ) ( Options , error ) {
if err := pipeline . RejectUnknownOptions ( options ) ; err != nil {
return Options { } , err
}
return Options { } , nil
}
func validateOptions ( options map [ string ] any ) error { _ , err := DecodeOptions ( options ) ; return err }