Recognize spells from shared glossary documents

This commit is contained in:
2026-11-29 18:04:00 +00:00
parent d406aa6cd2
commit f84e737b2a
12 changed files with 215 additions and 558 deletions

View File

@@ -1,34 +1,30 @@
package catalog
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"mime"
"sort"
"strings"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/integrations/domainrefs"
)
const (
SpellCatalogReferenceSlot = "spell_catalog"
overlaySchemaVersion = "notarius.dnd.spell-catalog-overlay.v1"
)
const SpellCatalogReferenceSlot = "spell_catalog"
// EffectiveCatalog is the immutable spell-name recognition catalog assembled
// from the embedded SRD catalog and an optional set of campaign overlays.
// from the embedded SRD catalog and an optional shared glossary.
type EffectiveCatalog struct {
baseID string
ruleset string
overlayIDs []string
canonicalNames []string
promptSpells []PromptSpell
lookup map[string]string
digest string
baseID string
ruleset string
collectionNames []string
canonicalNames []string
promptSpells []PromptSpell
lookup map[string]string
digest string
}
// PromptSpell is the recognition-only catalog entry supplied to extraction
@@ -38,10 +34,12 @@ type PromptSpell struct {
Aliases []string `json:"aliases"`
}
func (c EffectiveCatalog) BaseID() string { return c.baseID }
func (c EffectiveCatalog) Ruleset() string { return c.ruleset }
func (c EffectiveCatalog) Digest() string { return c.digest }
func (c EffectiveCatalog) OverlayIDs() []string { return append([]string(nil), c.overlayIDs...) }
func (c EffectiveCatalog) BaseID() string { return c.baseID }
func (c EffectiveCatalog) Ruleset() string { return c.ruleset }
func (c EffectiveCatalog) Digest() string { return c.digest }
func (c EffectiveCatalog) CollectionNames() []string {
return append([]string(nil), c.collectionNames...)
}
// CanonicalNames returns the globally sorted canonical spell names without
// exposing the catalog's internal storage.
@@ -73,154 +71,61 @@ func (c EffectiveCatalog) Lookup(name string) (string, bool) {
return canonical, ok
}
// ResolveEffectiveCatalog loads the embedded SRD catalog and applies the
// optional spell-catalog overlay found in its spell_catalog reference slot. It
// does not resolve paths or perform filesystem access.
// ResolveEffectiveCatalog assembles recognition vocabulary from the shared glossary.
func ResolveEffectiveCatalog(references contracts.ReferenceSet) (EffectiveCatalog, error) {
base, err := LoadSRD5E2014()
if err != nil {
return EffectiveCatalog{}, err
}
slot, ok := references.Slots[SpellCatalogReferenceSlot]
if !ok || len(slot.Items) == 0 {
return composeEffectiveCatalog(base, nil)
return composeEffectiveCatalog(base, nil, nil)
}
if len(slot.Items) != 1 {
return EffectiveCatalog{}, fmt.Errorf("reference slot %q must contain zero or one item", SpellCatalogReferenceSlot)
}
item := slot.Items[0]
mediaType, _, err := mime.ParseMediaType(item.MediaType)
if err != nil {
return EffectiveCatalog{}, fmt.Errorf("reference slot %q item media type %q is invalid: %w", SpellCatalogReferenceSlot, item.MediaType, err)
}
if !strings.EqualFold(mediaType, "application/json") {
return EffectiveCatalog{}, fmt.Errorf("reference slot %q item media type %q must be application/json", SpellCatalogReferenceSlot, item.MediaType)
}
overlays, err := decodeOverlayBundle(item.Content)
doc, err := domainrefs.Load(SpellCatalogReferenceSlot, strings.ToLower(mediaType), item.Content)
if err != nil {
return EffectiveCatalog{}, err
}
return composeEffectiveCatalog(base, overlays)
}
type overlayBundle struct {
SchemaVersion string `json:"schema_version"`
Catalogs []overlayCatalog `json:"catalogs"`
}
type overlayCatalog struct {
ID string `json:"id"`
Ruleset string `json:"ruleset"`
Source overlaySource `json:"source"`
Spells []overlaySpell `json:"spells"`
}
type overlaySource struct {
Title string `json:"title"`
Version json.RawMessage `json:"version"`
URL json.RawMessage `json:"url"`
License json.RawMessage `json:"license"`
}
type overlaySpell struct {
Name string `json:"name"`
Aliases json.RawMessage `json:"aliases,omitempty"`
}
func decodeOverlayBundle(content []byte) ([]overlayCatalog, error) {
decoder := json.NewDecoder(bytes.NewReader(content))
decoder.DisallowUnknownFields()
var bundle overlayBundle
if err := decoder.Decode(&bundle); err != nil {
return nil, fmt.Errorf("decode spell catalog overlay: %w", err)
if err := domainrefs.ValidateGlossary(doc); err != nil {
return EffectiveCatalog{}, fmt.Errorf("reference slot %q: %w", SpellCatalogReferenceSlot, err)
}
var trailing any
if err := decoder.Decode(&trailing); err != io.EOF {
return nil, fmt.Errorf("decode spell catalog overlay: multiple JSON values")
names, err := domainrefs.SelectCollections(doc, item.Collections, []string{"spells"})
if err != nil {
return EffectiveCatalog{}, fmt.Errorf("reference slot %q: %w", SpellCatalogReferenceSlot, err)
}
if bundle.SchemaVersion != overlaySchemaVersion {
return nil, fmt.Errorf("spell catalog overlay schema_version %q does not match %q", bundle.SchemaVersion, overlaySchemaVersion)
}
if len(bundle.Catalogs) == 0 {
return nil, fmt.Errorf("spell catalog overlay catalogs must not be empty")
}
seenIDs := make(map[string]struct{}, len(bundle.Catalogs))
for catalogIndex := range bundle.Catalogs {
catalog := &bundle.Catalogs[catalogIndex]
if catalog.ID != strings.TrimSpace(catalog.ID) || catalog.ID == "" {
return nil, fmt.Errorf("catalog[%d] id must be non-empty and trimmed", catalogIndex)
}
if _, exists := seenIDs[catalog.ID]; exists {
return nil, fmt.Errorf("catalog id %q is duplicated", catalog.ID)
}
seenIDs[catalog.ID] = struct{}{}
if catalog.Ruleset != SRD5E2014Ruleset {
return nil, fmt.Errorf("catalog %q ruleset %q does not match %q", catalog.ID, catalog.Ruleset, SRD5E2014Ruleset)
}
if catalog.Source.Title != strings.TrimSpace(catalog.Source.Title) || catalog.Source.Title == "" {
return nil, fmt.Errorf("catalog %q source title must be non-empty and trimmed", catalog.ID)
}
for field, raw := range map[string]json.RawMessage{
"version": catalog.Source.Version,
"url": catalog.Source.URL,
"license": catalog.Source.License,
} {
if _, err := decodeOptionalString(raw); err != nil {
return nil, fmt.Errorf("catalog %q source %s: %w", catalog.ID, field, err)
}
}
if len(catalog.Spells) == 0 {
return nil, fmt.Errorf("catalog %q spells must not be empty", catalog.ID)
}
for spellIndex := range catalog.Spells {
spell := &catalog.Spells[spellIndex]
if spell.Name != strings.TrimSpace(spell.Name) || spell.Name == "" {
return nil, fmt.Errorf("catalog %q spell[%d] name must be non-empty and trimmed", catalog.ID, spellIndex)
}
_, err := decodeAliases(spell.Aliases)
if err != nil {
return nil, fmt.Errorf("catalog %q spell %q aliases: %w", catalog.ID, spell.Name, err)
collections := doc["collections"].(map[string]any)
var entries []recognitionEntry
for _, name := range names {
raw := collections[name].(map[string]any)["entries"].([]any)
ordered := append([]any(nil), raw...)
sort.Slice(ordered, func(i, j int) bool {
return ordered[i].(map[string]any)["id"].(string) < ordered[j].(map[string]any)["id"].(string)
})
for _, value := range ordered {
entry := value.(map[string]any)
spell := recognitionEntry{collection: name, id: entry["id"].(string), name: strings.TrimSpace(entry["name"].(string))}
if aliases, ok := entry["aliases"].([]any); ok {
for _, alias := range aliases {
spell.aliases = append(spell.aliases, strings.TrimSpace(alias.(string)))
}
sort.Strings(spell.aliases)
}
entries = append(entries, spell)
}
}
return bundle.Catalogs, nil
return composeEffectiveCatalog(base, names, entries)
}
func decodeOptionalString(raw json.RawMessage) (string, error) {
if len(raw) == 0 {
return "", nil
}
if bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
return "", fmt.Errorf("must be a string when present")
}
var value string
if err := json.Unmarshal(raw, &value); err != nil {
return "", fmt.Errorf("must be a string when present: %w", err)
}
return value, nil
}
func decodeAliases(raw json.RawMessage) ([]string, error) {
if len(raw) == 0 {
return nil, nil
}
if bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
return nil, fmt.Errorf("must be an array when present")
}
var aliases []string
if err := json.Unmarshal(raw, &aliases); err != nil {
return nil, fmt.Errorf("must be an array of strings: %w", err)
}
for index, alias := range aliases {
if alias != strings.TrimSpace(alias) || alias == "" {
return nil, fmt.Errorf("value at index %d must be non-empty and trimmed", index)
}
}
return aliases, nil
type recognitionEntry struct {
collection, id, name string
aliases []string
}
type effectiveSpell struct {
@@ -235,7 +140,7 @@ type effectiveBuilder struct {
lookup map[string]string
}
func composeEffectiveCatalog(base Catalog, overlays []overlayCatalog) (EffectiveCatalog, error) {
func composeEffectiveCatalog(base Catalog, collections []string, entries []recognitionEntry) (EffectiveCatalog, error) {
builder := effectiveBuilder{
spells: make(map[string]*effectiveSpell, len(base.spells)),
canonicalByKey: make(map[string]string, len(base.lookup)),
@@ -252,23 +157,14 @@ func composeEffectiveCatalog(base Catalog, overlays []overlayCatalog) (Effective
}
}
sort.Slice(overlays, func(i, j int) bool { return overlays[i].ID < overlays[j].ID })
overlayIDs := make([]string, len(overlays))
for i, overlay := range overlays {
overlayIDs[i] = overlay.ID
for _, spell := range overlay.Spells {
canonical, err := builder.ensureCanonical(spell.Name)
if err != nil {
return EffectiveCatalog{}, fmt.Errorf("catalog %q: %w", overlay.ID, err)
}
aliases, err := decodeAliases(spell.Aliases)
if err != nil {
return EffectiveCatalog{}, fmt.Errorf("catalog %q spell %q aliases: %w", overlay.ID, spell.Name, err)
}
for _, alias := range aliases {
if err := builder.addAlias(canonical, alias); err != nil {
return EffectiveCatalog{}, fmt.Errorf("catalog %q spell %q: %w", overlay.ID, spell.Name, err)
}
for _, entry := range entries {
canonical, err := builder.ensureCanonical(entry.name)
if err != nil {
return EffectiveCatalog{}, fmt.Errorf("collection %q entry %q name: %w", entry.collection, entry.id, err)
}
for _, alias := range entry.aliases {
if err := builder.addAlias(canonical, alias); err != nil {
return EffectiveCatalog{}, fmt.Errorf("collection %q entry %q alias %q: %w", entry.collection, entry.id, alias, err)
}
}
}
@@ -289,18 +185,18 @@ func composeEffectiveCatalog(base Catalog, overlays []overlayCatalog) (Effective
promptSpells[index] = PromptSpell{CanonicalName: spell.name, Aliases: aliases}
}
digest, err := effectiveDigest(base, overlays, builder, canonicalNames)
digest, err := effectiveDigest(base, builder, canonicalNames)
if err != nil {
return EffectiveCatalog{}, err
}
return EffectiveCatalog{
baseID: base.ID(),
ruleset: base.Ruleset(),
overlayIDs: overlayIDs,
canonicalNames: canonicalNames,
promptSpells: promptSpells,
lookup: cloneStringMap(builder.lookup),
digest: digest,
baseID: base.ID(),
ruleset: base.Ruleset(),
collectionNames: append([]string(nil), collections...),
canonicalNames: canonicalNames,
promptSpells: promptSpells,
lookup: cloneStringMap(builder.lookup),
digest: digest,
}, nil
}
@@ -310,9 +206,6 @@ func (b *effectiveBuilder) ensureCanonical(name string) (string, error) {
return "", fmt.Errorf("spell %q has an empty lookup key", name)
}
if canonical, exists := b.canonicalByKey[key]; exists {
if canonical != name {
return "", fmt.Errorf("canonical lookup key %q has conflicting display names %q and %q", key, canonical, name)
}
return canonical, nil
}
if canonical, exists := b.lookup[key]; exists {
@@ -346,29 +239,17 @@ func (b *effectiveBuilder) addAlias(canonical string, alias string) error {
return nil
}
b.lookup[key] = canonical
if previous, exists := spell.aliases[key]; !exists || alias < previous {
if _, exists := spell.aliases[key]; !exists {
spell.aliases[key] = alias
}
return nil
}
type effectiveDigestDocument struct {
BaseID string `json:"base_id"`
BaseRuleset string `json:"base_ruleset"`
BaseSource Source `json:"base_source"`
Overlays []effectiveDigestOverlay `json:"overlays"`
Spells []effectiveDigestSpell `json:"spells"`
}
type effectiveDigestOverlay struct {
ID string `json:"id"`
Ruleset string `json:"ruleset"`
Source struct {
Title string `json:"title"`
Version string `json:"version"`
URL string `json:"url"`
License string `json:"license"`
} `json:"source"`
BaseID string `json:"base_id"`
BaseRuleset string `json:"base_ruleset"`
BaseSource Source `json:"base_source"`
Spells []effectiveDigestSpell `json:"spells"`
}
type effectiveDigestSpell struct {
@@ -382,34 +263,13 @@ type effectiveDigestAlias struct {
Display string `json:"display"`
}
func effectiveDigest(base Catalog, overlays []overlayCatalog, builder effectiveBuilder, canonicalNames []string) (string, error) {
func effectiveDigest(base Catalog, builder effectiveBuilder, canonicalNames []string) (string, error) {
document := effectiveDigestDocument{
BaseID: base.ID(),
BaseRuleset: base.Ruleset(),
BaseSource: base.Source(),
Overlays: make([]effectiveDigestOverlay, len(overlays)),
Spells: make([]effectiveDigestSpell, 0, len(canonicalNames)),
}
for index, overlay := range overlays {
document.Overlays[index].ID = overlay.ID
document.Overlays[index].Ruleset = overlay.Ruleset
version, err := decodeOptionalString(overlay.Source.Version)
if err != nil {
return "", fmt.Errorf("catalog %q source version: %w", overlay.ID, err)
}
url, err := decodeOptionalString(overlay.Source.URL)
if err != nil {
return "", fmt.Errorf("catalog %q source url: %w", overlay.ID, err)
}
license, err := decodeOptionalString(overlay.Source.License)
if err != nil {
return "", fmt.Errorf("catalog %q source license: %w", overlay.ID, err)
}
document.Overlays[index].Source.Title = overlay.Source.Title
document.Overlays[index].Source.Version = version
document.Overlays[index].Source.URL = url
document.Overlays[index].Source.License = license
}
for _, name := range canonicalNames {
spell := builder.spells[name]
digestSpell := effectiveDigestSpell{Key: spell.canonicalKey, Name: spell.name}