Prepare shared context for all D&D extractors

This commit is contained in:
2026-11-29 18:13:45 +00:00
parent e0554209f0
commit 03e4c0d64d
22 changed files with 189 additions and 176 deletions

View File

@@ -0,0 +1,16 @@
package spells
import (
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)
func TestConstructionRejectsInvalidSharedReference(t *testing.T) {
refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
"glossary": {Items: []contracts.ReferenceItem{{MediaType: "application/json", Content: []byte("{}")}}},
}}
if _, err := New(Options{}, refs); err == nil {
t.Fatal("invalid glossary accepted during construction")
}
}

View File

@@ -65,6 +65,7 @@ var _ contracts.Extractor[dnd.SpellList] = (*Extractor)(nil)
type Options struct{}
type Extractor struct {
context shared.PreparedContext
catalogPromptInput []byte
npcResolver *npcregistry.Resolver
}
@@ -77,6 +78,10 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
if len(references) == 1 {
referenceSet = references[0]
}
context, err := shared.PrepareContext(referenceSet)
if err != nil {
return nil, extractorErrorf("prepare references: %w", err)
}
effectiveCatalog, err := spellcatalog.ResolveEffectiveCatalog(referenceSet)
if err != nil {
return nil, extractorErrorf("resolve effective spell catalog: %w", err)
@@ -89,7 +94,7 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
if err != nil {
return nil, extractorErrorf("prepare NPC registry prompt input: %w", err)
}
return &Extractor{catalogPromptInput: catalogPromptInput, npcResolver: npcResolver}, nil
return &Extractor{context: context, catalogPromptInput: catalogPromptInput, npcResolver: npcResolver}, nil
}
func (e *Extractor) Key() string {
@@ -115,7 +120,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract
return nil, extractorErrorf("resolve NPC registry: %w", err)
}
materials := shared.PromptInputs(sourceInput, req.References)
materials := e.context.Inputs(sourceInput)
inputs := make(map[string]promptkit.ArtifactRef, len(materials)+2)
for name, material := range materials {
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))

View File

@@ -12,7 +12,6 @@ import (
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
spellcatalog "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/spells/catalog"
"go.maximumdirect.net/promptkit"
)
@@ -177,33 +176,21 @@ func TestExtractPassesReferencesAsPromptInputs(t *testing.T) {
req := extractionRequest()
req.References = contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
"players": {Slot: contracts.ReferenceSlot{Name: "players"}, Items: []contracts.ReferenceItem{{SlotName: "players", Content: []byte("Alice: Aria Brightmantle")}}},
"party": {Slot: contracts.ReferenceSlot{Name: "party"}, Items: []contracts.ReferenceItem{{SlotName: "party", Content: []byte("Aria Brightmantle: party cleric")}}},
"glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", Content: []byte("Brightmantle: local temple name")}}},
"party": {Slot: contracts.ReferenceSlot{Name: "party"}, Items: []contracts.ReferenceItem{{SlotName: "party", MediaType: "application/json", Content: []byte(`{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Aria Brightmantle","classes":[{"name":"cleric"}]},"player":{"name":"Alice"}}}}`)}}},
"glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", MediaType: "application/json", Content: []byte(`{"schema_version":"maximumdirect.glossary.v1","collections":{"campaign":{"entries":[{"id":"b","name":"Brightmantle","category":"place","description":"local temple name"}]}}}`)}}},
}}
if _, err := runExtraction(context.Background(), newExtractor(t, client), req); err != nil {
if _, err := runExtraction(context.Background(), newExtractor(t, client, req.References), req); err != nil {
t.Fatalf("Extract() error = %v, want nil", err)
}
text := promptText(client.requests[0])
for _, content := range []string{"Alice: Aria Brightmantle", "Aria Brightmantle: party cleric", "Brightmantle: local temple name", string(req.Chunk.Content)} {
for _, content := range []string{"Alice: Aria Brightmantle", "cleric", "local temple name", string(req.Chunk.Content)} {
if !strings.Contains(text, content) {
t.Fatalf("missing fixed input %q", content)
}
}
}
func TestPromptInputsMapLegacyRosterReferenceToParty(t *testing.T) {
inputs := shared.PromptInputs(spellSourceInput(), contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
"roster": {Slot: contracts.ReferenceSlot{Name: "roster"}, Items: []contracts.ReferenceItem{{SlotName: "roster", Content: []byte("Legacy roster text")}}},
}})
if got := string(inputs["party"].Content); got != "Legacy roster text" {
t.Fatalf("party input = %q, want legacy roster content", got)
}
if _, ok := inputs["roster"]; ok {
t.Fatal("roster prompt input was present; want only party input")
}
}
func TestExtractPreservesValidEmptyCollection(t *testing.T) {
result, err := runExtraction(context.Background(), newExtractor(t, &fakeSpellsLLMClient{content: []byte(`{"spell_casts":[]}`)}), extractionRequest())
if err != nil || result.Value.SpellCasts == nil || len(result.Value.SpellCasts) != 0 {

View File

@@ -36,7 +36,7 @@ func TestModuleSpec(t *testing.T) {
{
Name: "glossary",
Description: "Optional campaign glossary reference material used only for disambiguation.",
AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"},
AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml"},
},
{
Name: NPCRegistryReferenceSlot,
@@ -48,7 +48,7 @@ func TestModuleSpec(t *testing.T) {
{
Name: "party",
Description: "Optional party roster reference material used only for disambiguation.",
AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"},
AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml"},
},
{
Name: "players",
@@ -58,7 +58,7 @@ func TestModuleSpec(t *testing.T) {
{
Name: "roster",
Description: "Deprecated alias for party roster reference material used only for disambiguation.",
AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"},
AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml"},
},
{
Name: "spell_catalog",