Prepare shared context for all D&D extractors
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package enemyevents
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,7 @@ var _ contracts.Extractor[dnd.EnemyEventList] = (*Extractor)(nil)
|
||||
type Options struct{}
|
||||
|
||||
type Extractor struct {
|
||||
context shared.PreparedContext
|
||||
grounding *groundingResolver
|
||||
}
|
||||
|
||||
@@ -44,11 +45,15 @@ 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)
|
||||
}
|
||||
grounding, err := newGroundingResolver(referenceSet)
|
||||
if err != nil {
|
||||
return nil, extractorErrorf("prepare grounding: %w", err)
|
||||
}
|
||||
return &Extractor{grounding: grounding}, nil
|
||||
return &Extractor{context: context, grounding: grounding}, nil
|
||||
}
|
||||
|
||||
func (e *Extractor) Key() string { return Key }
|
||||
@@ -87,7 +92,7 @@ func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.Extract
|
||||
|
||||
order := shared.NewSourceRefOrderForSource(req.Source, req.SourceIndex)
|
||||
sourceID := req.Source.ID
|
||||
materials := shared.PromptInputs(sourceInput, req.References)
|
||||
materials := e.context.Inputs(sourceInput)
|
||||
for name, material := range grounding.PromptInputs() {
|
||||
materials[name] = material
|
||||
}
|
||||
|
||||
@@ -25,9 +25,17 @@ func TestExtractMapsEnemyEventsInSourceOrder(t *testing.T) {
|
||||
}}}
|
||||
|
||||
req := enemyExtractionRequest(t)
|
||||
req.References.Slots["party"] = contracts.ResolvedReferenceSlot{Items: []contracts.ReferenceItem{{
|
||||
MediaType: "application/json",
|
||||
Content: []byte(`{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Aria","aliases":["Star"]},"player":{"name":"Alice"}}}}`),
|
||||
}}}
|
||||
req.References.Slots["glossary"] = contracts.ResolvedReferenceSlot{Items: []contracts.ReferenceItem{{
|
||||
MediaType: "application/json",
|
||||
Content: []byte(`{"schema_version":"maximumdirect.glossary.v1","collections":{"campaign":{"entries":[{"id":"w","name":"Watchtower","category":"place","aliases":["The Tower"]}]}}}`),
|
||||
}}}
|
||||
index := source.NewDocumentIndex(req.Source)
|
||||
req.SourceIndex = &index
|
||||
result, err := runExtraction(context.Background(), newEnemyExtractor(t, client), req)
|
||||
result, err := runExtraction(context.Background(), newEnemyExtractor(t, client, req.References), req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -50,6 +58,15 @@ func TestExtractMapsEnemyEventsInSourceOrder(t *testing.T) {
|
||||
t.Fatalf("LLM calls = %d, want 1", len(client.requests))
|
||||
}
|
||||
request := client.requests[0]
|
||||
var prompt strings.Builder
|
||||
for _, message := range request.Prompt.Messages {
|
||||
prompt.WriteString(message.Content)
|
||||
}
|
||||
for _, name := range []string{"Aria", "Star", "Alice", "Watchtower", "The Tower"} {
|
||||
if !strings.Contains(prompt.String(), name) {
|
||||
t.Fatalf("prepared context omitted %q", name)
|
||||
}
|
||||
}
|
||||
if request.Prompt.SessionID != "session-123" {
|
||||
t.Fatal("session selection lost")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user