186 lines
6.8 KiB
Go
186 lines
6.8 KiB
Go
package npcoccurrences
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/assets"
|
|
"go.maximumdirect.net/configkit"
|
|
"go.maximumdirect.net/promptkit"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
npccodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/npcregistry"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity"
|
|
)
|
|
|
|
func extractionRequest() contracts.TypedExtractionRequest[dnd.NPCOccurrenceList] {
|
|
doc := sourceDocument()
|
|
chunk := &source.Chunk{
|
|
ID: "session-alpha:chunk:0", SourceID: doc.ID, Index: 0,
|
|
Ref: source.SourceRef{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 30},
|
|
Content: []byte(`{"units":[10,2,7,5,20,30]}`), MediaType: "application/json",
|
|
Units: append([]source.SourceUnit(nil), doc.Units...),
|
|
}
|
|
return contracts.TypedExtractionRequest[dnd.NPCOccurrenceList]{
|
|
Source: doc, Chunk: chunk,
|
|
SourceInput: contracts.NewLLMInputMaterial("source", chunk.MediaType, chunk.Content, "sha256:chunk", "file:///session-alpha.json"),
|
|
SessionID: "session-123", LLMProfile: "profile-npc-occurrences",
|
|
Operation: contracts.ProducerOperation[dnd.NPCOccurrenceList]{Controls: contracts.ProducerControls{Mode: contracts.ModeStrict, Cache: contracts.CacheBypass}, ReuseEligible: true, Assess: func(context.Context, dnd.NPCOccurrenceList) (contracts.CandidateAssessment, error) {
|
|
return contracts.CandidateAssessment{}, nil
|
|
}},
|
|
}
|
|
}
|
|
|
|
func sourceDocument() *source.SourceDocument {
|
|
return &source.SourceDocument{
|
|
ID: "session-alpha", Kind: "transcript", Format: "application/vnd.seriatim.minimal+json", Digest: "sha256:source",
|
|
Units: []source.SourceUnit{
|
|
{ID: 10, Kind: "transcript_segment", Text: "Mira Thorn is mentioned."},
|
|
{ID: 2, Kind: "transcript_segment", Text: "Mira Thorn speaks to the party."},
|
|
{ID: 7, Kind: "transcript_segment", Text: "Mira Thorn watches nearby."},
|
|
{ID: 5, Kind: "transcript_segment", Text: "Mira Thorn joins the party in combat."},
|
|
{ID: 20, Kind: "transcript_segment", Text: "Mira Thorn attacks the party."},
|
|
{ID: 30, Kind: "transcript_segment", Text: "Mira Thorn performs a ritual."},
|
|
},
|
|
}
|
|
}
|
|
|
|
func requiredRegistryReferences(t *testing.T, names ...string) contracts.ReferenceSet {
|
|
t.Helper()
|
|
npcs := make([]dnd.NPC, len(names))
|
|
for index, name := range names {
|
|
npcs[index] = dnd.NPC{
|
|
ID: identity.DeriveID(name), Name: name,
|
|
SourceRefs: []source.SourceRef{{SourceID: "other-session", StartUnitID: index + 1, EndUnitID: index + 1}},
|
|
}
|
|
}
|
|
content, err := npccodec.New().Encode(dnd.NPCRegistry{NPCs: npcs})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
|
NPCRegistryReferenceSlot: {
|
|
Slot: contracts.ReferenceSlot{Name: NPCRegistryReferenceSlot},
|
|
Items: []contracts.ReferenceItem{{SlotName: NPCRegistryReferenceSlot, MediaType: npccodec.MediaType, Content: content, Origin: contracts.ReferenceOrigin{Type: "generated"}}},
|
|
},
|
|
}}
|
|
}
|
|
|
|
func occurrenceRefs(start, end int) []occurrenceSourceRefResponse {
|
|
return []occurrenceSourceRefResponse{{StartUnitID: start, EndUnitID: end}}
|
|
}
|
|
|
|
func occurrenceNames(value dnd.NPCOccurrenceList) []string {
|
|
names := make([]string, len(value.Occurrences))
|
|
for index, occurrence := range value.Occurrences {
|
|
names[index] = occurrence.Name
|
|
}
|
|
return names
|
|
}
|
|
|
|
func occurrenceKinds(value dnd.NPCOccurrenceList) []dnd.NPCOccurrenceKind {
|
|
kinds := make([]dnd.NPCOccurrenceKind, len(value.Occurrences))
|
|
for index, occurrence := range value.Occurrences {
|
|
kinds[index] = occurrence.Kind
|
|
}
|
|
return kinds
|
|
}
|
|
|
|
func mismatchedSourceInputRequest(req contracts.TypedExtractionRequest[dnd.NPCOccurrenceList]) contracts.TypedExtractionRequest[dnd.NPCOccurrenceList] {
|
|
req.SourceInput = contracts.NewLLMInputMaterial("source", "application/json", []byte(`{"different":true}`), "sha256:other", "")
|
|
return req
|
|
}
|
|
|
|
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *Extractor {
|
|
t.Helper()
|
|
type settings struct {
|
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
|
}
|
|
definition := promptkit.Configure(configkit.Definition[settings]{}, func(value *settings) *promptkit.Settings { return &value.PromptKit })
|
|
filesystem := configkit.NewFilesystem("Notarius assets", assets.FS())
|
|
result, err := configkit.LoadLayered(configkit.Input{Source: configkit.Bytes("occurrence fixture", []byte(`promptkit:
|
|
profiles:
|
|
- id: profile-npc-occurrences
|
|
endpoint: https://example.invalid/v1
|
|
model: occurrence-test-model
|
|
api_key_required: false
|
|
`))}, configkit.LayeredOptions[settings]{Definition: definition, Underlays: []configkit.Underlay{
|
|
promptkit.BundledOpenRouterUnderlay(),
|
|
{Inputs: []configkit.Input{{Source: filesystem.File("notarius.yml"), Imports: true, Filesystem: &configkit.FilesystemContext{FS: &filesystem, Root: "."}}}},
|
|
}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
engine, err := promptkit.NewFromConfig(result, promptkit.RuntimeConfig{}, promptkit.WithLLMClient(client))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
extractor, err := New(engine, Options{}, references...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return extractor
|
|
}
|
|
|
|
func renderedInput(t *testing.T, request promptkit.GenerateRequest, name string) []byte {
|
|
t.Helper()
|
|
key := map[string]string{"transcript": "units", "npc_registry": "npcs", "location_registry": "locations"}[name]
|
|
for _, message := range request.Prompt.Messages {
|
|
start := strings.Index(message.Content, "{")
|
|
if start < 0 {
|
|
continue
|
|
}
|
|
content := []byte(message.Content[start:])
|
|
var fields map[string]json.RawMessage
|
|
if json.Unmarshal(content, &fields) == nil && fields[key] != nil {
|
|
return bytes.TrimSpace(content)
|
|
}
|
|
}
|
|
t.Fatalf("missing rendered %s input", name)
|
|
return nil
|
|
}
|
|
|
|
type fakeOccurrencesLLMClient struct {
|
|
response extractionResponse
|
|
responses []string
|
|
raw string
|
|
err error
|
|
errors []error
|
|
requests []promptkit.GenerateRequest
|
|
onGenerate func(context.Context, promptkit.GenerateRequest)
|
|
}
|
|
|
|
func (client *fakeOccurrencesLLMClient) Generate(ctx context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
index := len(client.requests)
|
|
client.requests = append(client.requests, request)
|
|
if client.onGenerate != nil {
|
|
client.onGenerate(ctx, request)
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if client.err != nil {
|
|
return nil, client.err
|
|
}
|
|
if index < len(client.errors) && client.errors[index] != nil {
|
|
return nil, client.errors[index]
|
|
}
|
|
content := client.raw
|
|
if index < len(client.responses) {
|
|
content = client.responses[index]
|
|
}
|
|
if content == "" {
|
|
encoded, err := json.Marshal(client.response)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
content = string(encoded)
|
|
}
|
|
return &promptkit.GenerateResponse{Content: content}, nil
|
|
}
|