Prepare shared context for all D&D extractors
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package combatturns
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,7 @@ var _ contracts.Extractor[dnd.CombatTurnList] = (*Extractor)(nil)
|
||||
type Options struct{}
|
||||
|
||||
type Extractor struct {
|
||||
context shared.PreparedContext
|
||||
npcResolver *npcregistry.Resolver
|
||||
sceneResolver *sceneregistry.Resolver
|
||||
}
|
||||
@@ -82,6 +83,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)
|
||||
}
|
||||
npcResolver, err := npcregistry.NewResolver(referenceSet)
|
||||
if err != nil {
|
||||
return nil, extractorErrorf("prepare NPC registry prompt input: %w", err)
|
||||
@@ -90,7 +95,7 @@ func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
|
||||
if err != nil {
|
||||
return nil, extractorErrorf("prepare scene eligibility: %w", err)
|
||||
}
|
||||
return &Extractor{npcResolver: npcResolver, sceneResolver: sceneResolver}, nil
|
||||
return &Extractor{context: context, npcResolver: npcResolver, sceneResolver: sceneResolver}, nil
|
||||
}
|
||||
|
||||
func (e *Extractor) Key() string { return Key }
|
||||
@@ -136,7 +141,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract
|
||||
}
|
||||
|
||||
sourceID := req.Source.ID
|
||||
materials := shared.PromptInputs(sourceInput, req.References)
|
||||
materials := e.context.Inputs(sourceInput)
|
||||
materials[NPCRegistryReferenceSlot] = npcRegistry.PromptInput()
|
||||
inputs := make(map[string]promptkit.ArtifactRef, len(materials))
|
||||
for name, material := range materials {
|
||||
|
||||
@@ -125,8 +125,8 @@ func TestExtractPassesReferencesAndNPCGroundingWithoutUsingItAsEvidence(t *testi
|
||||
client := &fakeCombatTurnsLLMClient{response: extractionResponse{CombatTurns: []combatTurnResponse{}}}
|
||||
references := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
"players": {Slot: contracts.ReferenceSlot{Name: "players"}, Items: []contracts.ReferenceItem{{SlotName: "players", Content: []byte("Alice: Aria")}}},
|
||||
"party": {Slot: contracts.ReferenceSlot{Name: "party"}, Items: []contracts.ReferenceItem{{SlotName: "party", Content: []byte("Aria: cleric")}}},
|
||||
"glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", Content: []byte("ogre: a large foe")}}},
|
||||
"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","classes":[{"name":"cleric"}]}}}}`)}}},
|
||||
"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":"o","name":"ogre","category":"foe","description":"a large foe"}]}}}`)}}},
|
||||
NPCRegistryReferenceSlot: {Slot: contracts.ReferenceSlot{Name: NPCRegistryReferenceSlot}, Items: []contracts.ReferenceItem{{SlotName: NPCRegistryReferenceSlot, MediaType: "application/json", Content: npcRegistryJSON(t)}}},
|
||||
}}
|
||||
req := extractionRequest(t)
|
||||
@@ -143,7 +143,7 @@ func TestExtractPassesReferencesAndNPCGroundingWithoutUsingItAsEvidence(t *testi
|
||||
for _, message := range messages {
|
||||
all += message.Content
|
||||
}
|
||||
for _, material := range []string{"Alice: Aria", "Aria: cleric", "ogre: a large foe"} {
|
||||
for _, material := range []string{"Alice: Aria", "cleric", "a large foe"} {
|
||||
if strings.Count(all, material) != 1 {
|
||||
t.Fatalf("campaign input not rendered once: %q", material)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user