Prepare structured D&D reference context

This commit is contained in:
2026-11-29 18:09:09 +00:00
parent f84e737b2a
commit e0554209f0
22 changed files with 465 additions and 65 deletions

View File

@@ -49,13 +49,21 @@ var _ contracts.Extractor[dnd.SceneDescriptionList] = (*Extractor)(nil)
type Options struct{}
type Extractor struct{}
type Extractor struct{ context shared.PreparedContext }
func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
if len(references) > 1 {
return nil, extractorErrorf("at most one reference set may be supplied")
}
return &Extractor{}, nil
var set contracts.ReferenceSet
if len(references) == 1 {
set = references[0]
}
context, err := shared.PrepareContext(set)
if err != nil {
return nil, extractorErrorf("prepare references: %w", err)
}
return &Extractor{context: context}, nil
}
func (e *Extractor) Key() string { return Key }
@@ -86,7 +94,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract
value.Scenes[0].ID = chunk.ID
return sceneFallback(&value, chunk, *index)
}
materials := shared.PromptInputs(sourceInput, req.References)
materials := e.context.Inputs(sourceInput)
inputs := make(map[string]promptkit.ArtifactRef, len(materials))
for name, material := range materials {
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))

View File

@@ -50,17 +50,17 @@ func TestExtractPassesOptionalReferencesAndUsesEmptyPlaceholders(t *testing.T) {
}
req.References = contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
"players": {Slot: contracts.ReferenceSlot{Name: "players"}, Items: []contracts.ReferenceItem{{SlotName: "players", Content: []byte("Dana: Mira")}}},
"party": {Slot: contracts.ReferenceSlot{Name: "party"}, Items: []contracts.ReferenceItem{{SlotName: "party", Content: []byte("Mira: ranger")}}},
"glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", Content: []byte("Greencloak: local title")}}},
"party": {Slot: contracts.ReferenceSlot{Name: "party"}, Items: []contracts.ReferenceItem{{SlotName: "party", MediaType: "application/json", Content: []byte(`{"schema_version":"maximumdirect.rpg.party.v1","characters":{"m":{"character":{"name":"Mira","classes":[{"name":"ranger"}]}}}}`)}}},
"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":"g","name":"Greencloak","category":"title","description":"local title"}]}}}`)}}},
}}
if _, err := runExtraction(newExtractor(t, client), context.Background(), req); err != nil {
if _, err := runExtraction(newExtractor(t, client, req.References), context.Background(), req); err != nil {
t.Fatalf("Extract() error = %v, want nil", err)
}
var text string
for _, message := range client.requests[1].Prompt.Messages {
text += message.Content
}
for _, reference := range []string{"Dana: Mira", "Mira: ranger", "Greencloak: local title"} {
for _, reference := range []string{"Dana: Mira", "ranger", "Greencloak", "local title"} {
if strings.Count(text, reference) != 1 {
t.Fatalf("optional input not rendered once: %q", reference)
}

View File

@@ -10,6 +10,15 @@ import (
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)
func TestConstructionRejectsInvalidPartyReference(t *testing.T) {
refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
"party": {Items: []contracts.ReferenceItem{{MediaType: "application/json", Content: []byte("{}")}}},
}}
if _, err := New(Options{}, refs); err == nil {
t.Fatal("invalid party accepted during construction")
}
}
func TestNewRejectsAmbiguousReferences(t *testing.T) {
if _, err := New(Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one reference set") {
t.Fatalf("New() error = %v, want reference-set error", err)
@@ -23,8 +32,8 @@ func TestModuleSpecAndReferenceSlots(t *testing.T) {
want := pipeline.ModuleSpec{
Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, InitialPrompts: []string{PromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.scene_descriptions"}, ArtifactKind: dnd.SceneDescriptionListKind,
ReferenceSlots: []contracts.ReferenceSlot{
{Name: "glossary", Description: "Optional campaign glossary reference material used only to disambiguate scene descriptions.", AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}},
{Name: "party", Description: "Optional party roster reference material used only to disambiguate scene descriptions.", AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}},
{Name: "glossary", Description: "Optional campaign glossary reference material used only to disambiguate scene descriptions.", AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml"}},
{Name: "party", Description: "Optional party roster reference material used only to disambiguate scene descriptions.", AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml"}},
{Name: "players", Description: "Optional player list reference material used only to disambiguate scene descriptions.", AcceptedMediaTypes: []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}},
},
}