2026-08-28 00:19:49 +00:00
package combat_semantics
import (
"context"
2026-11-22 00:42:20 +00:00
"encoding/json"
2026-08-28 00:19:49 +00:00
"strings"
2026-11-22 00:42:20 +00:00
"go.maximumdirect.net/promptkit"
2026-08-28 00:19:49 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/scenedescriptions/shape"
)
2026-11-22 00:42:20 +00:00
const Key = "extract/dnd/scene-descriptions/combat_semantics"
const PromptID = "notarius/dnd.scene_descriptions.validate_combat"
2026-08-28 00:19:49 +00:00
type Options struct { }
2026-11-22 00:42:20 +00:00
type Validator struct { engine * promptkit . Engine }
2026-08-28 00:19:49 +00:00
var _ contracts . TypedValidator [ dnd . SceneDescriptionList ] = ( * Validator ) ( nil )
2026-11-22 00:42:20 +00:00
func New ( engine * promptkit . Engine , _ Options ) ( * Validator , error ) {
if engine == nil {
return nil , validatorErrorf ( "native engine must not be nil" )
2026-08-28 00:19:49 +00:00
}
2026-11-22 00:42:20 +00:00
return & Validator { engine : engine } , nil
2026-08-28 00:19:49 +00:00
}
func ( v * Validator ) Name ( ) string { return Key }
func ( v * Validator ) ExecutionClass ( ) contracts . ExecutionClass {
return contracts . ExecutionClassLLMBacked
}
func ( v * Validator ) Validate ( ctx context . Context , req contracts . TypedValidationRequest [ dnd . SceneDescriptionList ] ) ( contracts . ValidationResult , error ) {
if v == nil {
return contracts . ValidationResult { } , validatorErrorf ( "validator must not be nil" )
}
2026-11-22 00:42:20 +00:00
if v . engine == nil {
return contracts . ValidationResult { } , validatorErrorf ( "native engine must not be nil" )
2026-08-28 00:19:49 +00:00
}
sourceInput , scene , err := validateRequest ( ctx , req )
if err != nil {
return contracts . ValidationResult { } , err
}
2026-11-22 00:42:20 +00:00
material := shared . TranscriptPromptMaterial ( sourceInput )
result , err := v . engine . Run ( ctx , promptkit . RunRequest {
PromptID : PromptID , ProfileID : req . LLMProfile , SessionID : req . SessionID ,
Inputs : map [ string ] promptkit . ArtifactRef { "transcript" : promptkit . InlineWithURI ( material . OriginURI , string ( material . Content ) ) } ,
Execution : req . Execution . Execution , TransientRetries : req . Execution . TransientRetries ,
Observer : req . Execution . Observer , OnPrepared : req . Execution . OnPrepared ,
} )
if canceled := ctx . Err ( ) ; canceled != nil {
return contracts . ValidationResult { } , canceled
}
if err != nil {
return contracts . ValidationResult { } , unavailable ( err )
}
if result == nil || result . Validation . Status == promptkit . ValidationFailed || ! result . Validation . IsValid {
return contracts . ValidationResult { } , unavailable ( validatorErrorf ( "native output contract failed: %w" , promptkit . ErrValidation ) )
}
var response classificationResponse
if err := json . Unmarshal ( [ ] byte ( result . RawOutput ) , & response ) ; err != nil {
return contracts . ValidationResult { } , unavailable ( validatorErrorf ( "decode native output: %w" , err ) )
}
assessment , err := interpretResponse ( response , scene . Kind )
if canceled := ctx . Err ( ) ; canceled != nil {
return contracts . ValidationResult { } , canceled
}
2026-08-28 00:19:49 +00:00
if err != nil {
2026-11-22 00:42:20 +00:00
return contracts . ValidationResult { } , unavailable ( err )
2026-08-28 00:19:49 +00:00
}
2026-11-22 00:42:20 +00:00
return assessment , nil
2026-08-28 00:19:49 +00:00
}
func validateRequest ( ctx context . Context , req contracts . TypedValidationRequest [ dnd . SceneDescriptionList ] ) ( contracts . LLMInputMaterial , dnd . SceneDescription , error ) {
if ctx == nil {
return contracts . LLMInputMaterial { } , dnd . SceneDescription { } , validatorErrorf ( "context must not be nil" )
}
if err := ctx . Err ( ) ; err != nil {
return contracts . LLMInputMaterial { } , dnd . SceneDescription { } , validatorErrorf ( "context error before validation: %w" , err )
}
if req . Stage != string ( pipeline . StageExtract ) {
return contracts . LLMInputMaterial { } , dnd . SceneDescription { } , validatorErrorf ( "validation stage must be extract, got %q" , req . Stage )
}
2026-11-22 00:42:20 +00:00
sourceInput , err := shared . PrepareChunkExtraction ( ctx , contracts . TypedExtractionRequest [ dnd . SceneDescriptionList ] { Source : req . Source , Chunk : req . Chunk , SourceInput : req . SourceInput } )
2026-08-28 00:19:49 +00:00
if err != nil {
return contracts . LLMInputMaterial { } , dnd . SceneDescription { } , validatorErrorf ( "prepare chunk source input: %w" , err )
}
if strings . TrimSpace ( req . Chunk . ID ) == "" || req . Chunk . ID != strings . TrimSpace ( req . Chunk . ID ) {
return contracts . LLMInputMaterial { } , dnd . SceneDescription { } , validatorErrorf ( "current chunk ID must be nonblank and trimmed" )
}
if req . Chunk . SourceID != req . Source . ID {
return contracts . LLMInputMaterial { } , dnd . SceneDescription { } , validatorErrorf ( "current chunk source ID %q does not match source %q" , req . Chunk . SourceID , req . Source . ID )
}
if err := source . NewDocumentIndex ( req . Source ) . ValidateRef ( req . Chunk . Ref ) ; err != nil {
return contracts . LLMInputMaterial { } , dnd . SceneDescription { } , validatorErrorf ( "validate current chunk source range: %w" , err )
}
if err := shape . ValidateForStage ( req . Value , req . Stage ) ; err != nil {
return contracts . LLMInputMaterial { } , dnd . SceneDescription { } , validatorErrorf ( "validate scene description shape: %w" , err )
}
scene := req . Value . Scenes [ 0 ]
if scene . ID != req . Chunk . ID {
return contracts . LLMInputMaterial { } , dnd . SceneDescription { } , validatorErrorf ( "scene ID must equal current chunk ID" )
}
if scene . SourceRef != req . Chunk . Ref {
return contracts . LLMInputMaterial { } , dnd . SceneDescription { } , validatorErrorf ( "scene source range must equal current chunk range" )
}
return sourceInput , scene , nil
}
2026-11-22 00:42:20 +00:00
func unavailable ( cause error ) error {
return & contracts . ValidationUnavailableError { Cause : validatorErrorf ( "native assessment unavailable: %w" , cause ) }
2026-08-28 00:19:49 +00:00
}
func Spec ( ) pipeline . ValidatorSpec {
2026-11-22 00:42:20 +00:00
return pipeline . ValidatorSpec { Key : Key , ExecutionClass : contracts . ExecutionClassLLMBacked , Prompts : [ ] string { PromptID } }
2026-08-28 00:19:49 +00:00
}
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
}
2026-11-22 00:42:20 +00:00
return New ( request . Dependencies . Engine , options )
2026-08-28 00:19:49 +00:00
} )
}
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 }