package sourcerelatedness import ( "context" "fmt" "strings" "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/validate/spells/spellpayload" ) const Key = "extract/dnd/spells/source_relatedness" const WarningReasonCode = "spell_not_near_source" var _ contracts.Validator = (*Validator)(nil) type Validator struct{} func New() *Validator { return &Validator{} } func (v *Validator) Name() string { return Key } func (v *Validator) ExecutionClass() contracts.ExecutionClass { return contracts.ExecutionClassDeterministic } func (v *Validator) Validate(ctx context.Context, req contracts.ValidationRequest) (contracts.ValidationResult, error) { payload, err := spellpayload.ValidationRequestPayload(req) if err != nil { return contracts.ValidationResult{Approved: true}, nil } if err := spellpayload.ValidateShape(payload); err != nil { return contracts.ValidationResult{Approved: true}, nil } var warnings []contracts.Warning for spellIndex, spell := range payload.SpellCasts { if !spellAppearsInCitedText(req.Source, spell) { warnings = append(warnings, contracts.Warning{ Scope: fmt.Sprintf("spell_casts[%d]", spellIndex), ReasonCode: WarningReasonCode, Message: fmt.Sprintf("spell %q was not found in cited source text", strings.TrimSpace(spell.Spell)), }) } } return contracts.ValidationResult{Approved: true, Warnings: warnings}, nil } func Spec() pipeline.ValidatorSpec { return pipeline.ValidatorSpec{ Key: Key, ExecutionClass: contracts.ExecutionClassDeterministic, } } func Register(registry *pipeline.ValidatorRegistry) error { return registry.RegisterWithSpec(Spec(), func() (contracts.Validator, error) { return New(), nil }) } func spellAppearsInCitedText(doc *source.SourceDocument, spell spellpayload.SpellCast) bool { name := strings.ToLower(strings.TrimSpace(spell.Spell)) if name == "" { return true } for _, ref := range spellpayload.SourceRefCandidates(doc, spell) { text, ok := spellpayload.CitedText(doc, ref) if !ok { continue } if strings.Contains(strings.ToLower(text), name) { return true } } return false }