2026-07-05 18:27:07 +00:00
|
|
|
package spells
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-17 06:50:08 +00:00
|
|
|
"context"
|
2026-07-05 18:27:07 +00:00
|
|
|
"encoding/json"
|
2026-11-22 00:02:13 +00:00
|
|
|
"strings"
|
2026-07-05 18:27:07 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2026-11-22 00:02:13 +00:00
|
|
|
"gitea.maximumdirect.net/eric/notarius/assets"
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
|
|
|
"go.maximumdirect.net/configkit"
|
|
|
|
|
"go.maximumdirect.net/promptkit"
|
|
|
|
|
|
2026-07-05 18:27:07 +00:00
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
2026-07-20 19:20:56 +00:00
|
|
|
spellcatalog "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/spells/catalog"
|
2026-07-05 18:27:07 +00:00
|
|
|
)
|
|
|
|
|
|
2026-11-22 00:02:13 +00:00
|
|
|
func promptExtractionRequest() contracts.TypedExtractionRequest[dnd.SpellList] {
|
2026-07-05 18:27:07 +00:00
|
|
|
doc := promptSourceDocument()
|
2026-07-17 05:32:11 +00:00
|
|
|
chunk := &source.Chunk{
|
|
|
|
|
ID: "session-alpha:chunk:0",
|
|
|
|
|
SourceID: doc.ID,
|
|
|
|
|
Index: 0,
|
|
|
|
|
Ref: source.SourceRef{
|
|
|
|
|
SourceID: doc.ID,
|
|
|
|
|
StartUnitID: doc.Units[0].ID,
|
|
|
|
|
EndUnitID: doc.Units[len(doc.Units)-1].ID,
|
|
|
|
|
},
|
|
|
|
|
Content: []byte(`{"units":[1,2]}`),
|
|
|
|
|
MediaType: "application/json",
|
|
|
|
|
Units: append([]source.SourceUnit(nil), doc.Units...),
|
|
|
|
|
Metadata: map[string]any{"ignored": "chunk metadata"},
|
2026-07-05 18:27:07 +00:00
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
return contracts.TypedExtractionRequest[dnd.SpellList]{
|
2026-07-05 18:27:07 +00:00
|
|
|
Source: doc,
|
|
|
|
|
Chunk: chunk,
|
2026-11-22 00:02:13 +00:00
|
|
|
Operation: contracts.ProducerOperation[dnd.SpellList]{Controls: contracts.ProducerControls{Mode: contracts.ModeStrict, Cache: contracts.CacheBypass}, ReuseEligible: true, Assess: func(context.Context, dnd.SpellList) (contracts.CandidateAssessment, error) {
|
|
|
|
|
return contracts.CandidateAssessment{}, nil
|
|
|
|
|
}},
|
2026-07-05 18:27:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func promptSourceDocument() *source.SourceDocument {
|
|
|
|
|
return &source.SourceDocument{
|
|
|
|
|
ID: "session-alpha",
|
|
|
|
|
Kind: "transcript",
|
|
|
|
|
Format: "application/vnd.seriatim.minimal+json",
|
|
|
|
|
Digest: "sha256:test",
|
|
|
|
|
Units: []source.SourceUnit{
|
|
|
|
|
{
|
2026-07-07 18:34:23 +00:00
|
|
|
ID: 1,
|
2026-07-05 18:27:07 +00:00
|
|
|
Kind: "transcript_segment",
|
|
|
|
|
Text: "Aria raises her hand and casts Cure Wounds.",
|
2026-07-17 05:19:57 +00:00
|
|
|
Ref: source.SourceRef{SourceID: "session-alpha", StartUnitID: 1, EndUnitID: 1},
|
2026-07-05 18:27:07 +00:00
|
|
|
Metadata: map[string]any{
|
|
|
|
|
"speaker": "Alice",
|
|
|
|
|
"start": json.Number("1.25"),
|
|
|
|
|
"end": json.Number("3.5"),
|
|
|
|
|
"ignored": "not rendered",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-07-07 18:34:23 +00:00
|
|
|
ID: 2,
|
2026-07-05 18:27:07 +00:00
|
|
|
Kind: "transcript_segment",
|
|
|
|
|
Text: "The fighter's wounds begin to close.",
|
2026-07-17 05:19:57 +00:00
|
|
|
Ref: source.SourceRef{SourceID: "session-alpha", StartUnitID: 2, EndUnitID: 2},
|
2026-07-05 18:27:07 +00:00
|
|
|
Metadata: map[string]any{"ignored": "not rendered"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustJSON(t *testing.T, value any) string {
|
|
|
|
|
t.Helper()
|
|
|
|
|
encoded, err := json.Marshal(value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Marshal() error = %v, want nil", err)
|
|
|
|
|
}
|
|
|
|
|
return string(encoded)
|
|
|
|
|
}
|
2026-07-06 14:41:24 -05:00
|
|
|
|
2026-07-17 06:50:08 +00:00
|
|
|
func responseSourceRefs(startUnitID int, endUnitID int) []spellSourceRefResponse {
|
|
|
|
|
return []spellSourceRefResponse{
|
2026-07-06 14:41:24 -05:00
|
|
|
{
|
2026-07-22 14:38:35 +00:00
|
|
|
StartUnitID: startUnitID,
|
|
|
|
|
EndUnitID: endUnitID,
|
2026-07-06 14:41:24 -05:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 06:50:08 +00:00
|
|
|
const spellTranscriptJSON = `{"id":"session-alpha","segments":[{"id":1,"text":"Aria raises her hand and casts Cure Wounds."}]}`
|
|
|
|
|
|
|
|
|
|
func spellSourceInput() contracts.LLMInputMaterial {
|
|
|
|
|
return contracts.NewLLMInputMaterial("source", "application/json", []byte(spellTranscriptJSON), "sha256:transcript", "file:///session-alpha.json")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func spellChunkInput(chunk *source.Chunk) contracts.LLMInputMaterial {
|
|
|
|
|
return contracts.NewLLMInputMaterial("source", chunk.MediaType, chunk.Content, "sha256:chunk", "file:///session-alpha.json")
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 00:02:13 +00:00
|
|
|
func extractionRequest() contracts.TypedExtractionRequest[dnd.SpellList] {
|
2026-07-17 06:50:08 +00:00
|
|
|
req := promptExtractionRequest()
|
|
|
|
|
req.SourceInput = spellChunkInput(req.Chunk)
|
|
|
|
|
req.SessionID = "session-123"
|
|
|
|
|
req.LLMProfile = "profile-spells"
|
|
|
|
|
return req
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 00:02:13 +00:00
|
|
|
func mismatchedSourceInputRequest(req contracts.TypedExtractionRequest[dnd.SpellList]) contracts.TypedExtractionRequest[dnd.SpellList] {
|
2026-07-17 06:50:08 +00:00
|
|
|
req.SourceInput = spellSourceInput()
|
|
|
|
|
return req
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 00:02:13 +00:00
|
|
|
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *Extractor {
|
2026-07-17 06:50:08 +00:00
|
|
|
t.Helper()
|
2026-11-22 00:02:13 +00:00
|
|
|
extractor, err := New(newSpellEngine(t, client), Options{}, references...)
|
2026-07-17 06:50:08 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("New() error = %v, want nil", err)
|
|
|
|
|
}
|
|
|
|
|
return extractor
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 19:20:56 +00:00
|
|
|
func spellCatalogReference(content string) contracts.ReferenceSet {
|
|
|
|
|
return contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
|
|
|
|
spellcatalog.SpellCatalogReferenceSlot: {
|
|
|
|
|
Items: []contracts.ReferenceItem{{
|
|
|
|
|
SlotName: spellcatalog.SpellCatalogReferenceSlot,
|
|
|
|
|
MediaType: "application/json",
|
|
|
|
|
Content: []byte(content),
|
|
|
|
|
}},
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func overlaySpellCatalogReference() contracts.ReferenceSet {
|
|
|
|
|
return spellCatalogReference(`{"schema_version":"notarius.dnd.spell-catalog-overlay.v1","catalogs":[{"id":"campaign.example","ruleset":"dnd-5e-2014","source":{"title":"Private campaign source","version":"1","url":"file:///private-source.json","license":"private"},"spells":[{"name":"Aegis of Emberfall","aliases":["Emberfall Aegis"]}]}]}`)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 06:50:08 +00:00
|
|
|
type fakeSpellsLLMClient struct {
|
|
|
|
|
response extractionResponse
|
|
|
|
|
content []byte
|
|
|
|
|
err error
|
2026-11-22 00:02:13 +00:00
|
|
|
requests []promptkit.GenerateRequest
|
|
|
|
|
outputs []string
|
2026-07-17 06:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-22 00:02:13 +00:00
|
|
|
func (client *fakeSpellsLLMClient) Generate(_ context.Context, req promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
|
|
|
client.requests = append(client.requests, req)
|
2026-07-17 06:50:08 +00:00
|
|
|
if client.err != nil {
|
2026-11-22 00:02:13 +00:00
|
|
|
return nil, client.err
|
2026-07-17 06:50:08 +00:00
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
if len(client.outputs) > 0 {
|
|
|
|
|
content := client.outputs[0]
|
|
|
|
|
if len(client.outputs) > 1 {
|
|
|
|
|
client.outputs = client.outputs[1:]
|
2026-07-22 14:38:35 +00:00
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
return &promptkit.GenerateResponse{Content: content}, nil
|
|
|
|
|
}
|
|
|
|
|
content := client.content
|
|
|
|
|
if len(content) == 0 {
|
2026-07-17 06:50:08 +00:00
|
|
|
var err error
|
|
|
|
|
content, err = json.Marshal(client.response)
|
|
|
|
|
if err != nil {
|
2026-11-22 00:02:13 +00:00
|
|
|
return nil, err
|
2026-07-17 06:50:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
return &promptkit.GenerateResponse{Content: string(content)}, nil
|
2026-07-17 06:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-22 00:02:13 +00:00
|
|
|
func promptText(request promptkit.GenerateRequest) string {
|
|
|
|
|
var text []string
|
|
|
|
|
for _, message := range request.Prompt.Messages {
|
|
|
|
|
text = append(text, message.Content)
|
2026-08-26 23:24:20 +00:00
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
return strings.Join(text, "\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newSpellEngine(t *testing.T, client promptkit.LLMClient) *promptkit.Engine {
|
|
|
|
|
t.Helper()
|
|
|
|
|
type settings struct {
|
|
|
|
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
2026-07-17 06:50:08 +00:00
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
definition := promptkit.Configure(configkit.Definition[settings]{}, func(value *settings) *promptkit.Settings { return &value.PromptKit })
|
|
|
|
|
filesystem := configkit.NewFilesystem("Notarius assets", assets.FS())
|
|
|
|
|
config, err := configkit.LoadLayered(configkit.Input{Source: configkit.Bytes("spell provider fixture", []byte(`promptkit:
|
|
|
|
|
profiles:
|
|
|
|
|
- id: profile-spells
|
|
|
|
|
endpoint: https://example.invalid/v1
|
|
|
|
|
model: spell-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)
|
2026-07-06 14:41:24 -05:00
|
|
|
}
|
2026-11-22 00:02:13 +00:00
|
|
|
engine, err := promptkit.NewFromConfig(config, promptkit.RuntimeConfig{}, promptkit.WithLLMClient(client))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return engine
|
2026-07-06 14:41:24 -05:00
|
|
|
}
|