118 lines
7.3 KiB
Go
118 lines
7.3 KiB
Go
|
|
package spells
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||
|
|
spellcodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/spells"
|
||
|
|
)
|
||
|
|
|
||
|
|
type pilotInput struct{ document *source.SourceDocument }
|
||
|
|
|
||
|
|
func (pilotInput) Key() string { return "pilot/input" }
|
||
|
|
func (input pilotInput) Parse(context.Context, contracts.ParseRequest) (*source.SourceDocument, error) {
|
||
|
|
return input.document, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type pilotChunker struct{}
|
||
|
|
|
||
|
|
func (pilotChunker) Key() string { return "pilot/chunk" }
|
||
|
|
func (pilotChunker) ReferenceSlots() []contracts.ReferenceSlot { return nil }
|
||
|
|
func (pilotChunker) Plan(_ context.Context, request contracts.ChunkRequest) (contracts.ChunkPlanResult, error) {
|
||
|
|
return contracts.ChunkPlanResult{Plan: source.ChunkPlan{SourceDigest: request.Source.Digest, Ranges: []source.ChunkRange{{StartUnitID: request.Source.Units[0].ID, EndUnitID: request.Source.Units[len(request.Source.Units)-1].ID}}}}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type pilotMerger struct{}
|
||
|
|
|
||
|
|
func (pilotMerger) Key() string { return "pilot/merge" }
|
||
|
|
func (pilotMerger) Merge(_ context.Context, request contracts.TypedMergeRequest[dnd.SpellList]) (contracts.TypedMergeResult[dnd.SpellList], error) {
|
||
|
|
return contracts.TypedMergeResult[dnd.SpellList]{Value: request.ExtractOutputs[0].Value}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type pilotNormalizer struct{}
|
||
|
|
|
||
|
|
func (pilotNormalizer) Key() string { return "pilot/normalize" }
|
||
|
|
func (pilotNormalizer) ReferenceSlots() []contracts.ReferenceSlot { return nil }
|
||
|
|
func (pilotNormalizer) Normalize(_ context.Context, request contracts.TypedNormalizeRequest[dnd.SpellList]) (contracts.TypedNormalizeResult[dnd.SpellList], error) {
|
||
|
|
return contracts.TypedNormalizeResult[dnd.SpellList]{Value: request.MergeOutput.Value}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type pilotOutput struct{}
|
||
|
|
|
||
|
|
func (pilotOutput) Key() string { return "pilot/output" }
|
||
|
|
func (pilotOutput) Encode(context.Context, contracts.OutputRequest) (contracts.OutputResult, error) {
|
||
|
|
return contracts.OutputResult{}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type pilotValidator struct{ assessments *int }
|
||
|
|
|
||
|
|
func (pilotValidator) Name() string { return "pilot/check" }
|
||
|
|
func (pilotValidator) ExecutionClass() contracts.ExecutionClass {
|
||
|
|
return contracts.ExecutionClassDeterministic
|
||
|
|
}
|
||
|
|
func (validator pilotValidator) Validate(_ context.Context, request contracts.TypedValidationRequest[dnd.SpellList]) (contracts.ValidationResult, error) {
|
||
|
|
*validator.assessments++
|
||
|
|
if len(request.Value.SpellCasts) == 0 {
|
||
|
|
return contracts.ValidationResult{ReasonCode: "missing_spell", CorrectionGuidance: "Return the observed spell."}, nil
|
||
|
|
}
|
||
|
|
return contracts.ValidationResult{Approved: true}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type pilotCache map[string][]byte
|
||
|
|
|
||
|
|
func (cache pilotCache) Lookup(_ context.Context, _ contracts.CacheMode, key string) ([]byte, error) {
|
||
|
|
return cache[key], nil
|
||
|
|
}
|
||
|
|
func (cache pilotCache) Store(_ context.Context, _ contracts.CacheMode, key string, value []byte) error {
|
||
|
|
cache[key] = append([]byte(nil), value...)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRegisteredSpellExtractionCorrectsAndReusesAcceptedOutput(t *testing.T) {
|
||
|
|
doc := promptSourceDocument()
|
||
|
|
var err error
|
||
|
|
doc.Digest, err = source.DigestDocument(doc)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
client := &fakeSpellsLLMClient{outputs: []string{`{"spell_casts":[]}`, `{"spell_casts":[{"caster":"Aria","spell":"Cure Wounds","source_refs":[{"start_unit_id":1,"end_unit_id":1}]}]}`}}
|
||
|
|
engine := newSpellEngine(t, client)
|
||
|
|
catalog := pipeline.ModuleCatalog{Inputs: pipeline.NewInputAdapterRegistry(), Chunkers: pipeline.NewChunkerRegistry(), ArtifactCodecs: pipeline.NewArtifactCodecRegistry(), Extractors: pipeline.NewExtractorRegistry(), Mergers: pipeline.NewMergerRegistry(), Normalizers: pipeline.NewNormalizerRegistry(), Validators: pipeline.NewValidatorRegistry(), ValidatorChains: pipeline.NewValidatorChainRegistry(), Outputs: pipeline.NewOutputEncoderRegistry()}
|
||
|
|
check := func(err error) {
|
||
|
|
t.Helper()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
check(catalog.Inputs.RegisterWithSpec(pipeline.ModuleSpec{Key: "pilot/input", Stage: pipeline.StageInput, ExecutionClass: contracts.ExecutionClassDeterministic, Provides: []string{"source.transcript"}}, func() (contracts.InputAdapter, error) { return pilotInput{doc}, nil }))
|
||
|
|
check(catalog.Chunkers.RegisterWithSpec(pipeline.ModuleSpec{Key: "pilot/chunk", Stage: pipeline.StageChunk, ExecutionClass: contracts.ExecutionClassDeterministic, Provides: []string{"chunks"}}, func() (contracts.Chunker, error) { return pilotChunker{}, nil }))
|
||
|
|
check(pipeline.RegisterArtifactCodec(catalog.ArtifactCodecs, spellcodec.New()))
|
||
|
|
check(Register(catalog.Extractors))
|
||
|
|
check(pipeline.RegisterMerger(catalog.Mergers, pipeline.ModuleSpec{Key: "pilot/merge", Stage: pipeline.StageMerge, ArtifactKind: dnd.SpellListKind, ExecutionClass: contracts.ExecutionClassDeterministic}, func() (contracts.Merger[dnd.SpellList], error) { return pilotMerger{}, nil }))
|
||
|
|
check(pipeline.RegisterNormalizer(catalog.Normalizers, pipeline.ModuleSpec{Key: "pilot/normalize", Stage: pipeline.StageNormalize, ArtifactKind: dnd.SpellListKind, ExecutionClass: contracts.ExecutionClassDeterministic}, func() (contracts.Normalizer[dnd.SpellList], error) { return pilotNormalizer{}, nil }))
|
||
|
|
check(catalog.Outputs.RegisterWithSpec(pipeline.ModuleSpec{Key: "pilot/output", Stage: pipeline.StageOutput, ExecutionClass: contracts.ExecutionClassDeterministic}, func() (contracts.OutputEncoder, error) { return pilotOutput{}, nil }))
|
||
|
|
assessments := 0
|
||
|
|
check(pipeline.RegisterTypedValidator(catalog.Validators, dnd.SpellListKind, pipeline.ValidatorSpec{Key: "pilot/check", ExecutionClass: contracts.ExecutionClassDeterministic}, func() (contracts.TypedValidator[dnd.SpellList], error) { return pilotValidator{&assessments}, nil }))
|
||
|
|
turns := 1
|
||
|
|
profile := pipeline.PipelineProfile{ID: "pilot", Input: pipeline.Binding("pilot/input"), Chunk: pipeline.Binding("pilot/chunk"), Output: pipeline.Binding("pilot/output"), LLMProfile: "profile-spells", MaxAdditionalTurns: &turns,
|
||
|
|
Artifacts: map[string]pipeline.ArtifactLaneProfile{"spells": {Extract: pipeline.ModuleBinding{Module: Key, Validators: pipeline.ValidatorOverride{Set: true, Validators: []pipeline.ModuleBinding{pipeline.Binding("pilot/check")}}}, Merge: pipeline.Binding("pilot/merge"), Normalize: pipeline.Binding("pilot/normalize")}}}
|
||
|
|
resolved, err := pipeline.ResolvePipeline(profile, pipeline.ResolveOptions{}, catalog)
|
||
|
|
check(err)
|
||
|
|
resolved, _, err = pipeline.MaterializeReferences(resolved, catalog, pipeline.ReferenceMaterializationOptions{})
|
||
|
|
check(err)
|
||
|
|
prepared, err := pipeline.Prepare(resolved, pipeline.Registries{Inputs: catalog.Inputs, Chunkers: catalog.Chunkers, ArtifactCodecs: catalog.ArtifactCodecs, Extractors: catalog.Extractors, Mergers: catalog.Mergers, Normalizers: catalog.Normalizers, Validators: catalog.Validators, ValidatorChains: catalog.ValidatorChains, Outputs: catalog.Outputs}, pipeline.ModuleDependencies{Engine: engine})
|
||
|
|
check(err)
|
||
|
|
cache := pilotCache{}
|
||
|
|
for range 2 {
|
||
|
|
_, err := pipeline.New().Run(context.Background(), pipeline.RunInput{Prepared: prepared, RawInput: []byte("input"), Cache: cache, Workers: 1})
|
||
|
|
check(err)
|
||
|
|
}
|
||
|
|
if len(client.requests) != 2 || assessments != 2 || len(cache) != 1 {
|
||
|
|
t.Fatalf("native calls=%d assessments=%d cache entries=%d", len(client.requests), assessments, len(cache))
|
||
|
|
}
|
||
|
|
}
|