Prepare NPC item and location registry extraction in core
This commit is contained in:
@@ -490,6 +490,9 @@ the migration does not invent combat classifications or silently skip assessment
|
|||||||
|
|
||||||
## Stage 11 — Migrate The Three Registry Extractors
|
## Stage 11 — Migrate The Three Registry Extractors
|
||||||
|
|
||||||
|
**Status:** Complete. All three focused extractor suites pass. Occurrence and
|
||||||
|
scene extractors, D&D normalizers, and assembled fixtures remain scheduled.
|
||||||
|
|
||||||
**Goal:** Move canonical noun extraction to domain-only preparation.
|
**Goal:** Move canonical noun extraction to domain-only preparation.
|
||||||
|
|
||||||
**Inspect:** `dnd/extract/{npcregistry,itemregistry,locationregistry}`, their typed
|
**Inspect:** `dnd/extract/{npcregistry,itemregistry,locationregistry}`, their typed
|
||||||
|
|||||||
@@ -37,32 +37,26 @@ var _ contracts.Extractor[dnd.ItemRegistry] = (*Extractor)(nil)
|
|||||||
|
|
||||||
type Options struct{}
|
type Options struct{}
|
||||||
|
|
||||||
type Extractor struct{ engine *promptkit.Engine }
|
type Extractor struct{}
|
||||||
|
|
||||||
func New(engine *promptkit.Engine, _ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
|
func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
|
||||||
if engine == nil {
|
|
||||||
return nil, extractorErrorf("native engine must not be nil")
|
|
||||||
}
|
|
||||||
if len(references) > 1 {
|
if len(references) > 1 {
|
||||||
return nil, extractorErrorf("at most one reference set may be supplied")
|
return nil, extractorErrorf("at most one reference set may be supplied")
|
||||||
}
|
}
|
||||||
return &Extractor{engine: engine}, nil
|
return &Extractor{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Extractor) Key() string { return Key }
|
func (e *Extractor) Key() string { return Key }
|
||||||
|
|
||||||
func (e *Extractor) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
func (e *Extractor) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
||||||
|
|
||||||
func (e *Extractor) Extract(ctx context.Context, req contracts.TypedExtractionRequest[dnd.ItemRegistry]) (contracts.TypedExtractionResult[dnd.ItemRegistry], error) {
|
func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.ExtractionInput) (contracts.PreparedOperation[dnd.ItemRegistry], error) {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return contracts.TypedExtractionResult[dnd.ItemRegistry]{}, extractorErrorf("extractor must not be nil")
|
return nil, extractorErrorf("extractor must not be nil")
|
||||||
}
|
}
|
||||||
if e.engine == nil {
|
sourceInput, err := shared.PrepareChunkInput(ctx, req)
|
||||||
return contracts.TypedExtractionResult[dnd.ItemRegistry]{}, extractorErrorf("native engine must not be nil")
|
|
||||||
}
|
|
||||||
sourceInput, err := shared.PrepareChunkExtraction(ctx, req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return contracts.TypedExtractionResult[dnd.ItemRegistry]{}, extractorErrorf("%w", err)
|
return nil, extractorErrorf("%w", err)
|
||||||
}
|
}
|
||||||
order := shared.NewSourceRefOrder(req.Source)
|
order := shared.NewSourceRefOrder(req.Source)
|
||||||
|
|
||||||
@@ -73,27 +67,20 @@ func (e *Extractor) Extract(ctx context.Context, req contracts.TypedExtractionRe
|
|||||||
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
||||||
}
|
}
|
||||||
initial := promptkit.RunRequest{PromptID: PromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID, Inputs: inputs}
|
initial := promptkit.RunRequest{PromptID: PromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID, Inputs: inputs}
|
||||||
value, outcome, err := pipeline.RunProducer(ctx, e.engine, req.Operation, initial,
|
return contracts.NativeOperation[dnd.ItemRegistry]{Initial: initial,
|
||||||
func(_ context.Context, result *promptkit.RunResult) (*dnd.ItemRegistry, []string, error) {
|
Interpret: func(_ context.Context, result *promptkit.RunResult) (contracts.Evaluation[dnd.ItemRegistry], error) {
|
||||||
var response extractionResponse
|
var response extractionResponse
|
||||||
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
||||||
return nil, nil, extractorErrorf("decode native output: %w", err)
|
return nil, extractorErrorf("decode native output: %w", err)
|
||||||
}
|
}
|
||||||
canonicalizeResponse(&response, order, sourceID)
|
canonicalizeResponse(&response, order, sourceID)
|
||||||
value := canonicalItemRegistry(response, sourceID)
|
value := canonicalItemRegistry(response, sourceID)
|
||||||
return &value, nil, nil
|
return contracts.CandidateEvaluation[dnd.ItemRegistry]{Result: contracts.EvaluationView[dnd.ItemRegistry]{Candidate: &value}}, nil
|
||||||
},
|
},
|
||||||
func(_ context.Context, guidance []string) (promptkit.RunRequest, error) {
|
Correction: contracts.PromptCorrection(CorrectionPromptID, "guidance", func(guidance []string) promptkit.ArtifactRef { return promptkit.Inline(strings.Join(guidance, "\n\n")) }),
|
||||||
return promptkit.RunRequest{PromptID: CorrectionPromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID,
|
Fallback: func(context.Context, contracts.Evaluation[dnd.ItemRegistry]) (dnd.ItemRegistry, error) {
|
||||||
Inputs: map[string]promptkit.ArtifactRef{"guidance": promptkit.Inline(strings.Join(guidance, "\n\n"))}}, nil
|
|
||||||
},
|
|
||||||
func(context.Context, *dnd.ItemRegistry) (dnd.ItemRegistry, error) {
|
|
||||||
return dnd.ItemRegistry{Items: []dnd.Item{}}, nil
|
return dnd.ItemRegistry{Items: []dnd.Item{}}, nil
|
||||||
})
|
}}, nil
|
||||||
if err != nil {
|
|
||||||
return contracts.TypedExtractionResult[dnd.ItemRegistry]{}, extractorErrorf("produce registry: %w", err)
|
|
||||||
}
|
|
||||||
return contracts.TypedExtractionResult[dnd.ItemRegistry]{Value: value, Outcome: outcome}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ModuleSpec() pipeline.ModuleSpec {
|
func ModuleSpec() pipeline.ModuleSpec {
|
||||||
@@ -111,7 +98,7 @@ func Register(registry *pipeline.ExtractorRegistry) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return New(request.Dependencies.Engine, options, request.References)
|
return New(options, request.References)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func TestExtractMapsItemsWithOwnedEvidenceAndDeterministicOrder(t *testing.T) {
|
|||||||
{Name: " Gold Pieces ", SourceRefs: responseSourceRefs(3, 2)},
|
{Name: " Gold Pieces ", SourceRefs: responseSourceRefs(3, 2)},
|
||||||
{Name: "Star Compass", SourceRefs: []itemSourceRefResponse{{StartUnitID: 2, EndUnitID: 2}, {StartUnitID: 1, EndUnitID: 1}, {StartUnitID: 1, EndUnitID: 1}}},
|
{Name: "Star Compass", SourceRefs: []itemSourceRefResponse{{StartUnitID: 2, EndUnitID: 2}, {StartUnitID: 1, EndUnitID: 1}, {StartUnitID: 1, EndUnitID: 1}}},
|
||||||
}}}
|
}}}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), extractionRequest())
|
result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Extract() error = %v, want nil", err)
|
t.Fatalf("Extract() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ func TestExtractKeepsAliasCandidatesSeparate(t *testing.T) {
|
|||||||
{Name: "Star Compass", SourceRefs: responseSourceRefs(1, 1)},
|
{Name: "Star Compass", SourceRefs: responseSourceRefs(1, 1)},
|
||||||
{Name: "Compass of the Stars", SourceRefs: responseSourceRefs(2, 2)},
|
{Name: "Compass of the Stars", SourceRefs: responseSourceRefs(2, 2)},
|
||||||
}}}
|
}}}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), extractionRequest())
|
result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest())
|
||||||
if err != nil || len(result.Value.Items) != 2 || result.Value.Items[0].ID == result.Value.Items[1].ID {
|
if err != nil || len(result.Value.Items) != 2 || result.Value.Items[0].ID == result.Value.Items[1].ID {
|
||||||
t.Fatalf("Extract() = %#v, %v; want separate alias candidates", result, err)
|
t.Fatalf("Extract() = %#v, %v; want separate alias candidates", result, err)
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ func TestExtractKeepsAliasCandidatesSeparate(t *testing.T) {
|
|||||||
|
|
||||||
func TestExtractPreservesInvalidCandidatesForValidators(t *testing.T) {
|
func TestExtractPreservesInvalidCandidatesForValidators(t *testing.T) {
|
||||||
client := &fakeItemsLLMClient{content: []byte(`{"items":[{"name":"","source_refs":[{"start_unit_id":0,"end_unit_id":-1}]}]}`)}
|
client := &fakeItemsLLMClient{content: []byte(`{"items":[{"name":"","source_refs":[{"start_unit_id":0,"end_unit_id":-1}]}]}`)}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), extractionRequest())
|
result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Extract() error = %v, want nil", err)
|
t.Fatalf("Extract() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ func TestExtractPassesReferencesWithoutTreatingThemAsEvidence(t *testing.T) {
|
|||||||
req.References = contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
req.References = contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||||
"glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", Content: []byte("Star Compass: heirloom")}}},
|
"glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", Content: []byte("Star Compass: heirloom")}}},
|
||||||
}}
|
}}
|
||||||
if _, err := newExtractor(t, client).Extract(context.Background(), req); err != nil {
|
if _, err := runExtraction(context.Background(), newExtractor(t, client), req); err != nil {
|
||||||
t.Fatalf("Extract() error = %v", err)
|
t.Fatalf("Extract() error = %v", err)
|
||||||
}
|
}
|
||||||
messageContaining(t, client.requests[0], "Star Compass: heirloom")
|
messageContaining(t, client.requests[0], "Star Compass: heirloom")
|
||||||
@@ -83,27 +83,26 @@ func TestExtractHandlesCancellationAndFailures(t *testing.T) {
|
|||||||
request := extractionRequest()
|
request := extractionRequest()
|
||||||
canceled, cancel := context.WithCancel(context.Background())
|
canceled, cancel := context.WithCancel(context.Background())
|
||||||
cancel()
|
cancel()
|
||||||
if _, err := newExtractor(t, &fakeItemsLLMClient{}).Extract(canceled, request); err == nil || !errors.Is(err, context.Canceled) || !strings.Contains(err.Error(), "dnd item registry") {
|
if _, err := runExtraction(canceled, newExtractor(t, &fakeItemsLLMClient{}), request); err == nil || !errors.Is(err, context.Canceled) {
|
||||||
t.Fatalf("canceled Extract() error = %v, want contextual cancellation", err)
|
t.Fatalf("canceled Extract() error = %v, want contextual cancellation", err)
|
||||||
}
|
}
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
name string
|
name string
|
||||||
extractor *Extractor
|
extractor *extractionFixture
|
||||||
req contracts.TypedExtractionRequest[dnd.ItemRegistry]
|
req contracts.TypedExtractionRequest[dnd.ItemRegistry]
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{name: "nil extractor", extractor: nil, req: request, want: "extractor"},
|
{name: "nil extractor", extractor: nil, req: request, want: "extractor"},
|
||||||
{name: "nil client", extractor: &Extractor{}, req: request, want: "native engine"},
|
|
||||||
{name: "preflight", extractor: newExtractor(t, &fakeItemsLLMClient{}), req: mismatchedSourceInputRequest(request), want: "must match chunk"},
|
{name: "preflight", extractor: newExtractor(t, &fakeItemsLLMClient{}), req: mismatchedSourceInputRequest(request), want: "must match chunk"},
|
||||||
} {
|
} {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
if _, err := test.extractor.Extract(context.Background(), test.req); err == nil || !strings.Contains(err.Error(), "dnd item registry") || !strings.Contains(err.Error(), test.want) {
|
if _, err := runExtraction(context.Background(), test.extractor, test.req); err == nil || !strings.Contains(err.Error(), "dnd item registry") || !strings.Contains(err.Error(), test.want) {
|
||||||
t.Fatalf("Extract() error = %v, want local context", err)
|
t.Fatalf("Extract() error = %v, want local context", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_, err := newExtractor(t, &fakeItemsLLMClient{err: errors.New("provider unavailable")}).Extract(context.Background(), request)
|
_, err := runExtraction(context.Background(), newExtractor(t, &fakeItemsLLMClient{err: errors.New("provider unavailable")}), request)
|
||||||
if err == nil || !strings.Contains(err.Error(), "dnd item registry") || !strings.Contains(err.Error(), "provider unavailable") {
|
if err == nil || !strings.Contains(err.Error(), "provider unavailable") {
|
||||||
t.Fatalf("provider error = %v, want contextual provider error", err)
|
t.Fatalf("provider error = %v, want contextual provider error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func TestNativeExtractionCorrectsGroundingWithFixedInputs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return assessment, err
|
return assessment, err
|
||||||
}
|
}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), request)
|
result, err := runExtraction(context.Background(), newExtractor(t, client), request)
|
||||||
if err != nil || result.Outcome.Disposition != contracts.ProducerAccepted || !result.Outcome.ReuseEligible ||
|
if err != nil || result.Outcome.Disposition != contracts.ProducerAccepted || !result.Outcome.ReuseEligible ||
|
||||||
assessments != 2 || len(client.requests) != 2 || len(prepared) != 2 || len(result.Value.Items) != 1 {
|
assessments != 2 || len(client.requests) != 2 || len(prepared) != 2 || len(result.Value.Items) != 1 {
|
||||||
t.Fatalf("corrected registry=%#v %v, assessments/provider/prepared=%d/%d/%d", result, err, assessments, len(client.requests), len(prepared))
|
t.Fatalf("corrected registry=%#v %v, assessments/provider/prepared=%d/%d/%d", result, err, assessments, len(client.requests), len(prepared))
|
||||||
@@ -122,7 +122,7 @@ func TestNativeExtractionDistinguishesEmptyAcceptanceFromFallback(t *testing.T)
|
|||||||
assessments++
|
assessments++
|
||||||
return assessRegistry(ctx, request, value)
|
return assessRegistry(ctx, request, value)
|
||||||
}
|
}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), request)
|
result, err := runExtraction(context.Background(), newExtractor(t, client), request)
|
||||||
if (err != nil) != test.wantError || assessments != test.wantAssessments || prepared != test.wantRuns {
|
if (err != nil) != test.wantError || assessments != test.wantAssessments || prepared != test.wantRuns {
|
||||||
t.Fatalf("registry=%#v %v, assessments=%d runs=%d", result, err, assessments, prepared)
|
t.Fatalf("registry=%#v %v, assessments=%d runs=%d", result, err, assessments, prepared)
|
||||||
}
|
}
|
||||||
@@ -149,7 +149,7 @@ func TestCanceledNativeExtractionCannotDeliverFallback(t *testing.T) {
|
|||||||
client := &fakeItemsLLMClient{content: []byte(groundedRegistry), onGenerate: func(context.Context, promptkit.GenerateRequest) { cancel() }}
|
client := &fakeItemsLLMClient{content: []byte(groundedRegistry), onGenerate: func(context.Context, promptkit.GenerateRequest) { cancel() }}
|
||||||
request := extractionRequest()
|
request := extractionRequest()
|
||||||
request.Operation.Controls.Mode = contracts.ModePermissive
|
request.Operation.Controls.Mode = contracts.ModePermissive
|
||||||
result, err := newExtractor(t, client).Extract(ctx, request)
|
result, err := runExtraction(ctx, newExtractor(t, client), request)
|
||||||
if !errors.Is(err, context.Canceled) || result.Outcome.Disposition != "" || len(client.requests) != 1 {
|
if !errors.Is(err, context.Canceled) || result.Outcome.Disposition != "" || len(client.requests) != 1 {
|
||||||
t.Fatalf("cancellation=%#v %v", result, err)
|
t.Fatalf("cancellation=%#v %v", result, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestModuleRegistrationAndNativeDependencies(t *testing.T) {
|
func TestModuleRegistrationAndNativeDependencies(t *testing.T) {
|
||||||
if _, err := New(nil, Options{}); err == nil || !strings.Contains(err.Error(), "native engine") {
|
if _, err := New(Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one") {
|
||||||
t.Fatalf("New(nil) error = %v, want client rejection", err)
|
|
||||||
}
|
|
||||||
if _, err := New(newExtractor(t, &fakeItemsLLMClient{}).engine, Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one") {
|
|
||||||
t.Fatalf("New() error = %v, want reference-set rejection", err)
|
t.Fatalf("New() error = %v, want reference-set rejection", err)
|
||||||
}
|
}
|
||||||
want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.item_registry"}, ArtifactKind: dnd.ItemRegistryKind, ReferenceSlots: referenceSlots()}
|
want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.item_registry"}, ArtifactKind: dnd.ItemRegistryKind, ReferenceSlots: referenceSlots()}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"gitea.maximumdirect.net/eric/notarius/assets"
|
"gitea.maximumdirect.net/eric/notarius/assets"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||||
"go.maximumdirect.net/configkit"
|
"go.maximumdirect.net/configkit"
|
||||||
"go.maximumdirect.net/promptkit"
|
"go.maximumdirect.net/promptkit"
|
||||||
@@ -51,7 +52,12 @@ func registryOperation() contracts.ProducerOperation[dnd.ItemRegistry] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *Extractor {
|
type extractionFixture struct {
|
||||||
|
*Extractor
|
||||||
|
engine *promptkit.Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *extractionFixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
type settings struct {
|
type settings struct {
|
||||||
PromptKit promptkit.Settings `yaml:"promptkit"`
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||||
@@ -75,11 +81,25 @@ func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contra
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
extractor, err := New(engine, Options{}, references...)
|
extractor, err := New(Options{}, references...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
return extractor
|
return &extractionFixture{Extractor: extractor, engine: engine}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runExtraction(ctx context.Context, fixture *extractionFixture, req contracts.TypedExtractionRequest[dnd.ItemRegistry]) (pipeline.OperationResult[dnd.ItemRegistry], error) {
|
||||||
|
var extractor *Extractor
|
||||||
|
var engine *promptkit.Engine
|
||||||
|
if fixture != nil {
|
||||||
|
extractor, engine = fixture.Extractor, fixture.engine
|
||||||
|
}
|
||||||
|
op := req.Operation
|
||||||
|
execution := pipeline.ExecutionContext[dnd.ItemRegistry]{Engine: engine, Controls: op.Controls, Assess: op.Assess, Execution: op.Execution, ReuseEligible: op.ReuseEligible, Cache: op.Cache}
|
||||||
|
input := contracts.ExtractionInput{Source: req.Source, Chunk: req.Chunk, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata, AmbientContext: req.AmbientContext}
|
||||||
|
return pipeline.ExecuteOperation(ctx, execution, func(ctx context.Context) (contracts.PreparedOperation[dnd.ItemRegistry], error) {
|
||||||
|
return extractor.PrepareExtraction(ctx, input)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func messageContaining(t *testing.T, request promptkit.GenerateRequest, text string) promptkit.RenderedMessage {
|
func messageContaining(t *testing.T, request promptkit.GenerateRequest, text string) promptkit.RenderedMessage {
|
||||||
|
|||||||
@@ -37,32 +37,26 @@ var _ contracts.Extractor[dnd.LocationRegistry] = (*Extractor)(nil)
|
|||||||
|
|
||||||
type Options struct{}
|
type Options struct{}
|
||||||
|
|
||||||
type Extractor struct{ engine *promptkit.Engine }
|
type Extractor struct{}
|
||||||
|
|
||||||
func New(engine *promptkit.Engine, _ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
|
func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
|
||||||
if engine == nil {
|
|
||||||
return nil, extractorErrorf("native engine must not be nil")
|
|
||||||
}
|
|
||||||
if len(references) > 1 {
|
if len(references) > 1 {
|
||||||
return nil, extractorErrorf("at most one reference set may be supplied")
|
return nil, extractorErrorf("at most one reference set may be supplied")
|
||||||
}
|
}
|
||||||
return &Extractor{engine: engine}, nil
|
return &Extractor{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Extractor) Key() string { return Key }
|
func (e *Extractor) Key() string { return Key }
|
||||||
|
|
||||||
func (e *Extractor) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
func (e *Extractor) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
||||||
|
|
||||||
func (e *Extractor) Extract(ctx context.Context, req contracts.TypedExtractionRequest[dnd.LocationRegistry]) (contracts.TypedExtractionResult[dnd.LocationRegistry], error) {
|
func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.ExtractionInput) (contracts.PreparedOperation[dnd.LocationRegistry], error) {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return contracts.TypedExtractionResult[dnd.LocationRegistry]{}, extractorErrorf("extractor must not be nil")
|
return nil, extractorErrorf("extractor must not be nil")
|
||||||
}
|
}
|
||||||
if e.engine == nil {
|
sourceInput, err := shared.PrepareChunkInput(ctx, req)
|
||||||
return contracts.TypedExtractionResult[dnd.LocationRegistry]{}, extractorErrorf("native engine must not be nil")
|
|
||||||
}
|
|
||||||
sourceInput, err := shared.PrepareChunkExtraction(ctx, req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return contracts.TypedExtractionResult[dnd.LocationRegistry]{}, extractorErrorf("%w", err)
|
return nil, extractorErrorf("%w", err)
|
||||||
}
|
}
|
||||||
order := shared.NewSourceRefOrder(req.Source)
|
order := shared.NewSourceRefOrder(req.Source)
|
||||||
|
|
||||||
@@ -73,27 +67,20 @@ func (e *Extractor) Extract(ctx context.Context, req contracts.TypedExtractionRe
|
|||||||
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
||||||
}
|
}
|
||||||
initial := promptkit.RunRequest{PromptID: PromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID, Inputs: inputs}
|
initial := promptkit.RunRequest{PromptID: PromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID, Inputs: inputs}
|
||||||
value, outcome, err := pipeline.RunProducer(ctx, e.engine, req.Operation, initial,
|
return contracts.NativeOperation[dnd.LocationRegistry]{Initial: initial,
|
||||||
func(_ context.Context, result *promptkit.RunResult) (*dnd.LocationRegistry, []string, error) {
|
Interpret: func(_ context.Context, result *promptkit.RunResult) (contracts.Evaluation[dnd.LocationRegistry], error) {
|
||||||
var response extractionResponse
|
var response extractionResponse
|
||||||
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
||||||
return nil, nil, extractorErrorf("decode native output: %w", err)
|
return nil, extractorErrorf("decode native output: %w", err)
|
||||||
}
|
}
|
||||||
canonicalizeResponse(&response, order, sourceID)
|
canonicalizeResponse(&response, order, sourceID)
|
||||||
value := canonicalLocationRegistry(response, sourceID)
|
value := canonicalLocationRegistry(response, sourceID)
|
||||||
return &value, nil, nil
|
return contracts.CandidateEvaluation[dnd.LocationRegistry]{Result: contracts.EvaluationView[dnd.LocationRegistry]{Candidate: &value}}, nil
|
||||||
},
|
},
|
||||||
func(_ context.Context, guidance []string) (promptkit.RunRequest, error) {
|
Correction: contracts.PromptCorrection(CorrectionPromptID, "guidance", func(guidance []string) promptkit.ArtifactRef { return promptkit.Inline(strings.Join(guidance, "\n\n")) }),
|
||||||
return promptkit.RunRequest{PromptID: CorrectionPromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID,
|
Fallback: func(context.Context, contracts.Evaluation[dnd.LocationRegistry]) (dnd.LocationRegistry, error) {
|
||||||
Inputs: map[string]promptkit.ArtifactRef{"guidance": promptkit.Inline(strings.Join(guidance, "\n\n"))}}, nil
|
|
||||||
},
|
|
||||||
func(context.Context, *dnd.LocationRegistry) (dnd.LocationRegistry, error) {
|
|
||||||
return dnd.LocationRegistry{Locations: []dnd.Location{}}, nil
|
return dnd.LocationRegistry{Locations: []dnd.Location{}}, nil
|
||||||
})
|
}}, nil
|
||||||
if err != nil {
|
|
||||||
return contracts.TypedExtractionResult[dnd.LocationRegistry]{}, extractorErrorf("produce registry: %w", err)
|
|
||||||
}
|
|
||||||
return contracts.TypedExtractionResult[dnd.LocationRegistry]{Value: value, Outcome: outcome}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ModuleSpec() pipeline.ModuleSpec {
|
func ModuleSpec() pipeline.ModuleSpec {
|
||||||
@@ -111,7 +98,7 @@ func Register(registry *pipeline.ExtractorRegistry) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return New(request.Dependencies.Engine, options, request.References)
|
return New(options, request.References)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func TestExtractMapsLocationsWithOwnedEvidenceAndDeterministicOrder(t *testing.T
|
|||||||
{Name: "The Tavern", SourceRefs: responseSourceRefs(3, 2)},
|
{Name: "The Tavern", SourceRefs: responseSourceRefs(3, 2)},
|
||||||
{Name: "Old Mill", SourceRefs: []locationSourceRefResponse{{StartUnitID: 2, EndUnitID: 2}, {StartUnitID: 1, EndUnitID: 1}, {StartUnitID: 1, EndUnitID: 1}}},
|
{Name: "Old Mill", SourceRefs: []locationSourceRefResponse{{StartUnitID: 2, EndUnitID: 2}, {StartUnitID: 1, EndUnitID: 1}, {StartUnitID: 1, EndUnitID: 1}}},
|
||||||
}}}
|
}}}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), extractionRequest())
|
result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Extract() error = %v, want nil", err)
|
t.Fatalf("Extract() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ func TestExtractRetainsSameNameLocationsAtDifferentAnchors(t *testing.T) {
|
|||||||
{Name: "the tavern", SourceRefs: responseSourceRefs(1, 1)},
|
{Name: "the tavern", SourceRefs: responseSourceRefs(1, 1)},
|
||||||
{Name: "the tavern", SourceRefs: responseSourceRefs(3, 3)},
|
{Name: "the tavern", SourceRefs: responseSourceRefs(3, 3)},
|
||||||
}}}
|
}}}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), extractionRequest())
|
result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest())
|
||||||
if err != nil || len(result.Value.Locations) != 2 {
|
if err != nil || len(result.Value.Locations) != 2 {
|
||||||
t.Fatalf("Extract() = %#v, %v; want both same-name candidates", result, err)
|
t.Fatalf("Extract() = %#v, %v; want both same-name candidates", result, err)
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,7 @@ func TestExtractRetainsSameNameLocationsAtDifferentAnchors(t *testing.T) {
|
|||||||
|
|
||||||
func TestExtractPreservesInvalidCandidatesForValidators(t *testing.T) {
|
func TestExtractPreservesInvalidCandidatesForValidators(t *testing.T) {
|
||||||
client := &fakeLocationsLLMClient{content: []byte(`{"locations":[{"name":"","source_refs":[{"start_unit_id":0,"end_unit_id":-1}]}]}`)}
|
client := &fakeLocationsLLMClient{content: []byte(`{"locations":[{"name":"","source_refs":[{"start_unit_id":0,"end_unit_id":-1}]}]}`)}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), extractionRequest())
|
result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Extract() error = %v, want nil", err)
|
t.Fatalf("Extract() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ func TestExtractPassesReferencesWithoutTreatingThemAsEvidence(t *testing.T) {
|
|||||||
req.References = contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
req.References = contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||||
"glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", Content: []byte("Old Mill: abandoned granary")}}},
|
"glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", Content: []byte("Old Mill: abandoned granary")}}},
|
||||||
}}
|
}}
|
||||||
if _, err := newExtractor(t, client).Extract(context.Background(), req); err != nil {
|
if _, err := runExtraction(context.Background(), newExtractor(t, client), req); err != nil {
|
||||||
t.Fatalf("Extract() error = %v", err)
|
t.Fatalf("Extract() error = %v", err)
|
||||||
}
|
}
|
||||||
messageContaining(t, client.requests[0], "Old Mill: abandoned granary")
|
messageContaining(t, client.requests[0], "Old Mill: abandoned granary")
|
||||||
@@ -88,7 +88,7 @@ func TestExtractDoesNotMutateRequestMaterials(t *testing.T) {
|
|||||||
beforeUnits := append([]source.SourceUnit(nil), req.Source.Units...)
|
beforeUnits := append([]source.SourceUnit(nil), req.Source.Units...)
|
||||||
beforeChunkUnits := append([]source.SourceUnit(nil), req.Chunk.Units...)
|
beforeChunkUnits := append([]source.SourceUnit(nil), req.Chunk.Units...)
|
||||||
beforeContent := append([]byte(nil), req.Chunk.Content...)
|
beforeContent := append([]byte(nil), req.Chunk.Content...)
|
||||||
if _, err := newExtractor(t, client).Extract(context.Background(), req); err != nil {
|
if _, err := runExtraction(context.Background(), newExtractor(t, client), req); err != nil {
|
||||||
t.Fatalf("Extract() error = %v", err)
|
t.Fatalf("Extract() error = %v", err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(req.Source.Units, beforeUnits) || !reflect.DeepEqual(req.Chunk.Units, beforeChunkUnits) || !reflect.DeepEqual(req.Chunk.Content, beforeContent) {
|
if !reflect.DeepEqual(req.Source.Units, beforeUnits) || !reflect.DeepEqual(req.Chunk.Units, beforeChunkUnits) || !reflect.DeepEqual(req.Chunk.Content, beforeContent) {
|
||||||
@@ -97,30 +97,29 @@ func TestExtractDoesNotMutateRequestMaterials(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExtractHandlesEmptyOutputAndLocalFailures(t *testing.T) {
|
func TestExtractHandlesEmptyOutputAndLocalFailures(t *testing.T) {
|
||||||
empty, err := newExtractor(t, &fakeLocationsLLMClient{response: extractionResponse{Locations: []locationResponse{}}}).Extract(context.Background(), extractionRequest())
|
empty, err := runExtraction(context.Background(), newExtractor(t, &fakeLocationsLLMClient{response: extractionResponse{Locations: []locationResponse{}}}), extractionRequest())
|
||||||
if err != nil || empty.Value.Locations == nil || len(empty.Value.Locations) != 0 {
|
if err != nil || empty.Value.Locations == nil || len(empty.Value.Locations) != 0 {
|
||||||
t.Fatalf("empty Extract() = %#v, %v; want empty list", empty, err)
|
t.Fatalf("empty Extract() = %#v, %v; want empty list", empty, err)
|
||||||
}
|
}
|
||||||
request := extractionRequest()
|
request := extractionRequest()
|
||||||
var nilExtractor *Extractor
|
var nilExtractor *extractionFixture
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
name string
|
name string
|
||||||
extractor *Extractor
|
extractor *extractionFixture
|
||||||
req contracts.TypedExtractionRequest[dnd.LocationRegistry]
|
req contracts.TypedExtractionRequest[dnd.LocationRegistry]
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{name: "nil extractor", extractor: nilExtractor, req: request, want: "extractor"},
|
{name: "nil extractor", extractor: nilExtractor, req: request, want: "extractor"},
|
||||||
{name: "nil client", extractor: &Extractor{}, req: request, want: "native engine"},
|
|
||||||
{name: "preflight", extractor: newExtractor(t, &fakeLocationsLLMClient{}), req: mismatchedSourceInputRequest(request), want: "must match chunk"},
|
{name: "preflight", extractor: newExtractor(t, &fakeLocationsLLMClient{}), req: mismatchedSourceInputRequest(request), want: "must match chunk"},
|
||||||
} {
|
} {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
if _, err := test.extractor.Extract(context.Background(), test.req); err == nil || !strings.Contains(err.Error(), "dnd location registry") || !strings.Contains(err.Error(), test.want) {
|
if _, err := runExtraction(context.Background(), test.extractor, test.req); err == nil || !strings.Contains(err.Error(), "dnd location registry") || !strings.Contains(err.Error(), test.want) {
|
||||||
t.Fatalf("Extract() error = %v, want local context", err)
|
t.Fatalf("Extract() error = %v, want local context", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_, err = newExtractor(t, &fakeLocationsLLMClient{err: errors.New("provider unavailable")}).Extract(context.Background(), request)
|
_, err = runExtraction(context.Background(), newExtractor(t, &fakeLocationsLLMClient{err: errors.New("provider unavailable")}), request)
|
||||||
if err == nil || !strings.Contains(err.Error(), "dnd location registry") || !strings.Contains(err.Error(), "provider unavailable") {
|
if err == nil || !strings.Contains(err.Error(), "provider unavailable") {
|
||||||
t.Fatalf("provider error = %v, want contextual provider error", err)
|
t.Fatalf("provider error = %v, want contextual provider error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func TestNativeExtractionCorrectsGroundingWithFixedInputs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return assessment, err
|
return assessment, err
|
||||||
}
|
}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), request)
|
result, err := runExtraction(context.Background(), newExtractor(t, client), request)
|
||||||
if err != nil || result.Outcome.Disposition != contracts.ProducerAccepted || !result.Outcome.ReuseEligible ||
|
if err != nil || result.Outcome.Disposition != contracts.ProducerAccepted || !result.Outcome.ReuseEligible ||
|
||||||
assessments != 2 || len(client.requests) != 2 || len(prepared) != 2 || len(result.Value.Locations) != 1 {
|
assessments != 2 || len(client.requests) != 2 || len(prepared) != 2 || len(result.Value.Locations) != 1 {
|
||||||
t.Fatalf("corrected registry=%#v %v, assessments/provider/prepared=%d/%d/%d", result, err, assessments, len(client.requests), len(prepared))
|
t.Fatalf("corrected registry=%#v %v, assessments/provider/prepared=%d/%d/%d", result, err, assessments, len(client.requests), len(prepared))
|
||||||
@@ -122,7 +122,7 @@ func TestNativeExtractionDistinguishesEmptyAcceptanceFromFallback(t *testing.T)
|
|||||||
assessments++
|
assessments++
|
||||||
return assessRegistry(ctx, request, value)
|
return assessRegistry(ctx, request, value)
|
||||||
}
|
}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), request)
|
result, err := runExtraction(context.Background(), newExtractor(t, client), request)
|
||||||
if (err != nil) != test.wantError || assessments != test.wantAssessments || prepared != test.wantRuns {
|
if (err != nil) != test.wantError || assessments != test.wantAssessments || prepared != test.wantRuns {
|
||||||
t.Fatalf("registry=%#v %v, assessments=%d runs=%d", result, err, assessments, prepared)
|
t.Fatalf("registry=%#v %v, assessments=%d runs=%d", result, err, assessments, prepared)
|
||||||
}
|
}
|
||||||
@@ -149,7 +149,7 @@ func TestCanceledNativeExtractionCannotDeliverFallback(t *testing.T) {
|
|||||||
client := &fakeLocationsLLMClient{content: []byte(groundedRegistry), onGenerate: func(context.Context, promptkit.GenerateRequest) { cancel() }}
|
client := &fakeLocationsLLMClient{content: []byte(groundedRegistry), onGenerate: func(context.Context, promptkit.GenerateRequest) { cancel() }}
|
||||||
request := extractionRequest()
|
request := extractionRequest()
|
||||||
request.Operation.Controls.Mode = contracts.ModePermissive
|
request.Operation.Controls.Mode = contracts.ModePermissive
|
||||||
result, err := newExtractor(t, client).Extract(ctx, request)
|
result, err := runExtraction(ctx, newExtractor(t, client), request)
|
||||||
if !errors.Is(err, context.Canceled) || result.Outcome.Disposition != "" || len(client.requests) != 1 {
|
if !errors.Is(err, context.Canceled) || result.Outcome.Disposition != "" || len(client.requests) != 1 {
|
||||||
t.Fatalf("cancellation=%#v %v", result, err)
|
t.Fatalf("cancellation=%#v %v", result, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestModuleRegistrationAndNativeDependencies(t *testing.T) {
|
func TestModuleRegistrationAndNativeDependencies(t *testing.T) {
|
||||||
if _, err := New(nil, Options{}); err == nil || !strings.Contains(err.Error(), "native engine") {
|
if _, err := New(Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one") {
|
||||||
t.Fatalf("New(nil) error = %v, want client rejection", err)
|
|
||||||
}
|
|
||||||
if _, err := New(newExtractor(t, &fakeLocationsLLMClient{}).engine, Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one") {
|
|
||||||
t.Fatalf("New() error = %v, want reference-set rejection", err)
|
t.Fatalf("New() error = %v, want reference-set rejection", err)
|
||||||
}
|
}
|
||||||
want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.location_registry"}, ArtifactKind: dnd.LocationRegistryKind, ReferenceSlots: referenceSlots()}
|
want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{PromptID, CorrectionPromptID}, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.location_registry"}, ArtifactKind: dnd.LocationRegistryKind, ReferenceSlots: referenceSlots()}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"gitea.maximumdirect.net/eric/notarius/assets"
|
"gitea.maximumdirect.net/eric/notarius/assets"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||||
"go.maximumdirect.net/configkit"
|
"go.maximumdirect.net/configkit"
|
||||||
"go.maximumdirect.net/promptkit"
|
"go.maximumdirect.net/promptkit"
|
||||||
@@ -51,7 +52,12 @@ func registryOperation() contracts.ProducerOperation[dnd.LocationRegistry] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *Extractor {
|
type extractionFixture struct {
|
||||||
|
*Extractor
|
||||||
|
engine *promptkit.Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *extractionFixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
type settings struct {
|
type settings struct {
|
||||||
PromptKit promptkit.Settings `yaml:"promptkit"`
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||||
@@ -75,11 +81,25 @@ func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contra
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
extractor, err := New(engine, Options{}, references...)
|
extractor, err := New(Options{}, references...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
return extractor
|
return &extractionFixture{Extractor: extractor, engine: engine}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runExtraction(ctx context.Context, fixture *extractionFixture, req contracts.TypedExtractionRequest[dnd.LocationRegistry]) (pipeline.OperationResult[dnd.LocationRegistry], error) {
|
||||||
|
var extractor *Extractor
|
||||||
|
var engine *promptkit.Engine
|
||||||
|
if fixture != nil {
|
||||||
|
extractor, engine = fixture.Extractor, fixture.engine
|
||||||
|
}
|
||||||
|
op := req.Operation
|
||||||
|
execution := pipeline.ExecutionContext[dnd.LocationRegistry]{Engine: engine, Controls: op.Controls, Assess: op.Assess, Execution: op.Execution, ReuseEligible: op.ReuseEligible, Cache: op.Cache}
|
||||||
|
input := contracts.ExtractionInput{Source: req.Source, Chunk: req.Chunk, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata, AmbientContext: req.AmbientContext}
|
||||||
|
return pipeline.ExecuteOperation(ctx, execution, func(ctx context.Context) (contracts.PreparedOperation[dnd.LocationRegistry], error) {
|
||||||
|
return extractor.PrepareExtraction(ctx, input)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func messageContaining(t *testing.T, request promptkit.GenerateRequest, text string) promptkit.RenderedMessage {
|
func messageContaining(t *testing.T, request promptkit.GenerateRequest, text string) promptkit.RenderedMessage {
|
||||||
|
|||||||
@@ -43,32 +43,26 @@ var _ contracts.Extractor[dnd.NPCRegistry] = (*Extractor)(nil)
|
|||||||
|
|
||||||
type Options struct{}
|
type Options struct{}
|
||||||
|
|
||||||
type Extractor struct{ engine *promptkit.Engine }
|
type Extractor struct{}
|
||||||
|
|
||||||
func New(engine *promptkit.Engine, _ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
|
func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
|
||||||
if engine == nil {
|
|
||||||
return nil, extractorErrorf("native engine must not be nil")
|
|
||||||
}
|
|
||||||
if len(references) > 1 {
|
if len(references) > 1 {
|
||||||
return nil, extractorErrorf("at most one reference set may be supplied")
|
return nil, extractorErrorf("at most one reference set may be supplied")
|
||||||
}
|
}
|
||||||
return &Extractor{engine: engine}, nil
|
return &Extractor{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Extractor) Key() string { return Key }
|
func (e *Extractor) Key() string { return Key }
|
||||||
|
|
||||||
func (e *Extractor) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
func (e *Extractor) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
||||||
|
|
||||||
func (e *Extractor) Extract(ctx context.Context, req contracts.TypedExtractionRequest[dnd.NPCRegistry]) (contracts.TypedExtractionResult[dnd.NPCRegistry], error) {
|
func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.ExtractionInput) (contracts.PreparedOperation[dnd.NPCRegistry], error) {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return contracts.TypedExtractionResult[dnd.NPCRegistry]{}, extractorErrorf("extractor must not be nil")
|
return nil, extractorErrorf("extractor must not be nil")
|
||||||
}
|
}
|
||||||
if e.engine == nil {
|
sourceInput, err := shared.PrepareChunkInput(ctx, req)
|
||||||
return contracts.TypedExtractionResult[dnd.NPCRegistry]{}, extractorErrorf("native engine must not be nil")
|
|
||||||
}
|
|
||||||
sourceInput, err := shared.PrepareChunkExtraction(ctx, req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return contracts.TypedExtractionResult[dnd.NPCRegistry]{}, extractorErrorf("%w", err)
|
return nil, extractorErrorf("%w", err)
|
||||||
}
|
}
|
||||||
order := shared.NewSourceRefOrder(req.Source)
|
order := shared.NewSourceRefOrder(req.Source)
|
||||||
|
|
||||||
@@ -79,27 +73,20 @@ func (e *Extractor) Extract(ctx context.Context, req contracts.TypedExtractionRe
|
|||||||
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
||||||
}
|
}
|
||||||
initial := promptkit.RunRequest{PromptID: PromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID, Inputs: inputs}
|
initial := promptkit.RunRequest{PromptID: PromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID, Inputs: inputs}
|
||||||
value, outcome, err := pipeline.RunProducer(ctx, e.engine, req.Operation, initial,
|
return contracts.NativeOperation[dnd.NPCRegistry]{Initial: initial,
|
||||||
func(_ context.Context, result *promptkit.RunResult) (*dnd.NPCRegistry, []string, error) {
|
Interpret: func(_ context.Context, result *promptkit.RunResult) (contracts.Evaluation[dnd.NPCRegistry], error) {
|
||||||
var response extractionResponse
|
var response extractionResponse
|
||||||
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
||||||
return nil, nil, extractorErrorf("decode native output: %w", err)
|
return nil, extractorErrorf("decode native output: %w", err)
|
||||||
}
|
}
|
||||||
canonicalizeResponse(&response, order, sourceID)
|
canonicalizeResponse(&response, order, sourceID)
|
||||||
value := canonicalNPCRegistry(response, sourceID)
|
value := canonicalNPCRegistry(response, sourceID)
|
||||||
return &value, nil, nil
|
return contracts.CandidateEvaluation[dnd.NPCRegistry]{Result: contracts.EvaluationView[dnd.NPCRegistry]{Candidate: &value}}, nil
|
||||||
},
|
},
|
||||||
func(_ context.Context, guidance []string) (promptkit.RunRequest, error) {
|
Correction: contracts.PromptCorrection(CorrectionPromptID, "guidance", func(guidance []string) promptkit.ArtifactRef { return promptkit.Inline(strings.Join(guidance, "\n\n")) }),
|
||||||
return promptkit.RunRequest{PromptID: CorrectionPromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID,
|
Fallback: func(context.Context, contracts.Evaluation[dnd.NPCRegistry]) (dnd.NPCRegistry, error) {
|
||||||
Inputs: map[string]promptkit.ArtifactRef{"guidance": promptkit.Inline(strings.Join(guidance, "\n\n"))}}, nil
|
|
||||||
},
|
|
||||||
func(context.Context, *dnd.NPCRegistry) (dnd.NPCRegistry, error) {
|
|
||||||
return dnd.NPCRegistry{NPCs: []dnd.NPC{}}, nil
|
return dnd.NPCRegistry{NPCs: []dnd.NPC{}}, nil
|
||||||
})
|
}}, nil
|
||||||
if err != nil {
|
|
||||||
return contracts.TypedExtractionResult[dnd.NPCRegistry]{}, extractorErrorf("produce registry: %w", err)
|
|
||||||
}
|
|
||||||
return contracts.TypedExtractionResult[dnd.NPCRegistry]{Value: value, Outcome: outcome}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ModuleSpec() pipeline.ModuleSpec {
|
func ModuleSpec() pipeline.ModuleSpec {
|
||||||
@@ -121,7 +108,7 @@ func Register(registry *pipeline.ExtractorRegistry) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return New(request.Dependencies.Engine, options, request.References)
|
return New(options, request.References)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func TestExtractReturnsCanonicalNPCRegistryFromPrivateResponse(t *testing.T) {
|
|||||||
callRequest := extractionRequest()
|
callRequest := extractionRequest()
|
||||||
var prepared promptkit.PreparedRun
|
var prepared promptkit.PreparedRun
|
||||||
callRequest.Operation.Execution.OnPrepared = func(_ context.Context, value promptkit.PreparedRun) { prepared = value }
|
callRequest.Operation.Execution.OnPrepared = func(_ context.Context, value promptkit.PreparedRun) { prepared = value }
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), callRequest)
|
result, err := runExtraction(context.Background(), newExtractor(t, client), callRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Extract() error = %v, want nil", err)
|
t.Fatalf("Extract() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -78,7 +78,7 @@ func TestExtractOrdersNPCsBySourcePositionRatherThanUnitID(t *testing.T) {
|
|||||||
req.Chunk.Units = append([]source.SourceUnit(nil), req.Source.Units...)
|
req.Chunk.Units = append([]source.SourceUnit(nil), req.Source.Units...)
|
||||||
req.Chunk.Ref = source.SourceRef{SourceID: req.Source.ID, StartUnitID: 100, EndUnitID: 50}
|
req.Chunk.Ref = source.SourceRef{SourceID: req.Source.ID, StartUnitID: 100, EndUnitID: 50}
|
||||||
|
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), req)
|
result, err := runExtraction(context.Background(), newExtractor(t, client), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Extract() error = %v, want nil", err)
|
t.Fatalf("Extract() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,7 @@ func TestExtractUsesDocumentOrderForNPCReferencesAndStableTies(t *testing.T) {
|
|||||||
req.Chunk.Units = append([]source.SourceUnit(nil), req.Source.Units...)
|
req.Chunk.Units = append([]source.SourceUnit(nil), req.Source.Units...)
|
||||||
req.Chunk.Ref = source.SourceRef{SourceID: req.Source.ID, StartUnitID: 30, EndUnitID: 10}
|
req.Chunk.Ref = source.SourceRef{SourceID: req.Source.ID, StartUnitID: 30, EndUnitID: 10}
|
||||||
|
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), req)
|
result, err := runExtraction(context.Background(), newExtractor(t, client), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Extract() error = %v", err)
|
t.Fatalf("Extract() error = %v", err)
|
||||||
}
|
}
|
||||||
@@ -132,7 +132,7 @@ func TestExtractPassesCampaignReferencesAsPromptInputs(t *testing.T) {
|
|||||||
"party": {Slot: contracts.ReferenceSlot{Name: "party"}, Items: []contracts.ReferenceItem{{SlotName: "party", Content: []byte("Mira: ranger")}}},
|
"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")}}},
|
"glossary": {Slot: contracts.ReferenceSlot{Name: "glossary"}, Items: []contracts.ReferenceItem{{SlotName: "glossary", Content: []byte("Greencloak: local title")}}},
|
||||||
}}
|
}}
|
||||||
if _, err := newExtractor(t, client).Extract(context.Background(), req); err != nil {
|
if _, err := runExtraction(context.Background(), newExtractor(t, client), req); err != nil {
|
||||||
t.Fatalf("Extract() error = %v, want nil", err)
|
t.Fatalf("Extract() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
request := client.requests[0]
|
request := client.requests[0]
|
||||||
@@ -151,7 +151,7 @@ func TestExtractPreservesMalformedCandidatesForValidators(t *testing.T) {
|
|||||||
Name: "",
|
Name: "",
|
||||||
SourceRefs: []npcSourceRefResponse{{StartUnitID: 99, EndUnitID: 0}},
|
SourceRefs: []npcSourceRefResponse{{StartUnitID: 99, EndUnitID: 0}},
|
||||||
}}}}
|
}}}}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), extractionRequest())
|
result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Extract() error = %v, want nil", err)
|
t.Fatalf("Extract() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -165,7 +165,7 @@ func TestExtractPreservesMalformedCandidatesForValidators(t *testing.T) {
|
|||||||
|
|
||||||
func TestExtractMapsRawSemanticCandidatesWithoutRepair(t *testing.T) {
|
func TestExtractMapsRawSemanticCandidatesWithoutRepair(t *testing.T) {
|
||||||
client := &fakeNPCsLLMClient{content: []byte(`{"npcs":[{"name":"","source_refs":[{"start_unit_id":0,"end_unit_id":-1}]}]}`)}
|
client := &fakeNPCsLLMClient{content: []byte(`{"npcs":[{"name":"","source_refs":[{"start_unit_id":0,"end_unit_id":-1}]}]}`)}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), extractionRequest())
|
result, err := runExtraction(context.Background(), newExtractor(t, client), extractionRequest())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Extract() error = %v, want nil", err)
|
t.Fatalf("Extract() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -181,25 +181,24 @@ func TestExtractMapsRawSemanticCandidatesWithoutRepair(t *testing.T) {
|
|||||||
func TestExtractRetainsLocalErrorContextAndProviderFailures(t *testing.T) {
|
func TestExtractRetainsLocalErrorContextAndProviderFailures(t *testing.T) {
|
||||||
request := extractionRequest()
|
request := extractionRequest()
|
||||||
extractor := newExtractor(t, &fakeNPCsLLMClient{response: extractionResponse{NPCs: []npcResponse{}}})
|
extractor := newExtractor(t, &fakeNPCsLLMClient{response: extractionResponse{NPCs: []npcResponse{}}})
|
||||||
var nilExtractor *Extractor
|
var nilExtractor *extractionFixture
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
name string
|
name string
|
||||||
extractor *Extractor
|
extractor *extractionFixture
|
||||||
req contracts.TypedExtractionRequest[dnd.NPCRegistry]
|
req contracts.TypedExtractionRequest[dnd.NPCRegistry]
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{name: "nil extractor", extractor: nilExtractor, req: request, want: "extractor"},
|
{name: "nil extractor", extractor: nilExtractor, req: request, want: "extractor"},
|
||||||
{name: "nil native engine", extractor: &Extractor{}, req: request, want: "native engine"},
|
|
||||||
{name: "wrapped preflight failure", extractor: extractor, req: mismatchedSourceInputRequest(request), want: "must match chunk"},
|
{name: "wrapped preflight failure", extractor: extractor, req: mismatchedSourceInputRequest(request), want: "must match chunk"},
|
||||||
} {
|
} {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
if _, err := test.extractor.Extract(context.Background(), test.req); err == nil || !strings.Contains(err.Error(), "dnd npc registry") || !strings.Contains(err.Error(), test.want) {
|
if _, err := runExtraction(context.Background(), test.extractor, test.req); err == nil || !strings.Contains(err.Error(), "dnd npc registry") || !strings.Contains(err.Error(), test.want) {
|
||||||
t.Fatalf("Extract() error = %v, want contextual local error", err)
|
t.Fatalf("Extract() error = %v, want contextual local error", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_, err := newExtractor(t, &fakeNPCsLLMClient{err: errors.New("provider unavailable")}).Extract(context.Background(), request)
|
_, err := runExtraction(context.Background(), newExtractor(t, &fakeNPCsLLMClient{err: errors.New("provider unavailable")}), request)
|
||||||
if err == nil || !strings.Contains(err.Error(), "dnd npc registry") || !strings.Contains(err.Error(), "provider unavailable") {
|
if err == nil || !strings.Contains(err.Error(), "provider unavailable") {
|
||||||
t.Fatalf("provider Extract() error = %v, want contextual provider error", err)
|
t.Fatalf("provider Extract() error = %v, want contextual provider error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func TestNativeExtractionCorrectsGroundingWithFixedInputs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return assessment, err
|
return assessment, err
|
||||||
}
|
}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), request)
|
result, err := runExtraction(context.Background(), newExtractor(t, client), request)
|
||||||
if err != nil || result.Outcome.Disposition != contracts.ProducerAccepted || !result.Outcome.ReuseEligible ||
|
if err != nil || result.Outcome.Disposition != contracts.ProducerAccepted || !result.Outcome.ReuseEligible ||
|
||||||
assessments != 2 || len(client.requests) != 2 || len(prepared) != 2 || len(result.Value.NPCs) != 1 {
|
assessments != 2 || len(client.requests) != 2 || len(prepared) != 2 || len(result.Value.NPCs) != 1 {
|
||||||
t.Fatalf("corrected registry=%#v %v, assessments/provider/prepared=%d/%d/%d", result, err, assessments, len(client.requests), len(prepared))
|
t.Fatalf("corrected registry=%#v %v, assessments/provider/prepared=%d/%d/%d", result, err, assessments, len(client.requests), len(prepared))
|
||||||
@@ -122,7 +122,7 @@ func TestNativeExtractionDistinguishesEmptyAcceptanceFromFallback(t *testing.T)
|
|||||||
assessments++
|
assessments++
|
||||||
return assessRegistry(ctx, request, value)
|
return assessRegistry(ctx, request, value)
|
||||||
}
|
}
|
||||||
result, err := newExtractor(t, client).Extract(context.Background(), request)
|
result, err := runExtraction(context.Background(), newExtractor(t, client), request)
|
||||||
if (err != nil) != test.wantError || assessments != test.wantAssessments || prepared != test.wantRuns {
|
if (err != nil) != test.wantError || assessments != test.wantAssessments || prepared != test.wantRuns {
|
||||||
t.Fatalf("registry=%#v %v, assessments=%d runs=%d", result, err, assessments, prepared)
|
t.Fatalf("registry=%#v %v, assessments=%d runs=%d", result, err, assessments, prepared)
|
||||||
}
|
}
|
||||||
@@ -149,7 +149,7 @@ func TestCanceledNativeExtractionCannotDeliverFallback(t *testing.T) {
|
|||||||
client := &fakeNPCsLLMClient{content: []byte(groundedRegistry), onGenerate: func(context.Context, promptkit.GenerateRequest) { cancel() }}
|
client := &fakeNPCsLLMClient{content: []byte(groundedRegistry), onGenerate: func(context.Context, promptkit.GenerateRequest) { cancel() }}
|
||||||
request := extractionRequest()
|
request := extractionRequest()
|
||||||
request.Operation.Controls.Mode = contracts.ModePermissive
|
request.Operation.Controls.Mode = contracts.ModePermissive
|
||||||
result, err := newExtractor(t, client).Extract(ctx, request)
|
result, err := runExtraction(ctx, newExtractor(t, client), request)
|
||||||
if !errors.Is(err, context.Canceled) || result.Outcome.Disposition != "" || len(client.requests) != 1 {
|
if !errors.Is(err, context.Canceled) || result.Outcome.Disposition != "" || len(client.requests) != 1 {
|
||||||
t.Fatalf("cancellation=%#v %v", result, err)
|
t.Fatalf("cancellation=%#v %v", result, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,8 @@ import (
|
|||||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewRequiresNativeEngineAndRejectsAmbiguousReferences(t *testing.T) {
|
func TestNewRejectsAmbiguousReferences(t *testing.T) {
|
||||||
if _, err := New(nil, Options{}); err == nil || !strings.Contains(err.Error(), "native engine") {
|
if _, err := New(Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one reference set") {
|
||||||
t.Fatalf("New(nil) error = %v, want dependency error", err)
|
|
||||||
}
|
|
||||||
if _, err := New(newExtractor(t, &fakeNPCsLLMClient{}).engine, 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)
|
t.Fatalf("New() error = %v, want reference-set error", err)
|
||||||
}
|
}
|
||||||
if got := newExtractor(t, &fakeNPCsLLMClient{}).Key(); got != Key {
|
if got := newExtractor(t, &fakeNPCsLLMClient{}).Key(); got != Key {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"gitea.maximumdirect.net/eric/notarius/assets"
|
"gitea.maximumdirect.net/eric/notarius/assets"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||||
"go.maximumdirect.net/configkit"
|
"go.maximumdirect.net/configkit"
|
||||||
"go.maximumdirect.net/promptkit"
|
"go.maximumdirect.net/promptkit"
|
||||||
@@ -63,7 +64,12 @@ func registryOperation() contracts.ProducerOperation[dnd.NPCRegistry] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *Extractor {
|
type extractionFixture struct {
|
||||||
|
*Extractor
|
||||||
|
engine *promptkit.Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *extractionFixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
type settings struct {
|
type settings struct {
|
||||||
PromptKit promptkit.Settings `yaml:"promptkit"`
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||||
@@ -87,11 +93,25 @@ func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contra
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
extractor, err := New(engine, Options{}, references...)
|
extractor, err := New(Options{}, references...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
return extractor
|
return &extractionFixture{Extractor: extractor, engine: engine}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runExtraction(ctx context.Context, fixture *extractionFixture, req contracts.TypedExtractionRequest[dnd.NPCRegistry]) (pipeline.OperationResult[dnd.NPCRegistry], error) {
|
||||||
|
var extractor *Extractor
|
||||||
|
var engine *promptkit.Engine
|
||||||
|
if fixture != nil {
|
||||||
|
extractor, engine = fixture.Extractor, fixture.engine
|
||||||
|
}
|
||||||
|
op := req.Operation
|
||||||
|
execution := pipeline.ExecutionContext[dnd.NPCRegistry]{Engine: engine, Controls: op.Controls, Assess: op.Assess, Execution: op.Execution, ReuseEligible: op.ReuseEligible, Cache: op.Cache}
|
||||||
|
input := contracts.ExtractionInput{Source: req.Source, Chunk: req.Chunk, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata, AmbientContext: req.AmbientContext}
|
||||||
|
return pipeline.ExecuteOperation(ctx, execution, func(ctx context.Context) (contracts.PreparedOperation[dnd.NPCRegistry], error) {
|
||||||
|
return extractor.PrepareExtraction(ctx, input)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func messageContaining(t *testing.T, request promptkit.GenerateRequest, text string) promptkit.RenderedMessage {
|
func messageContaining(t *testing.T, request promptkit.GenerateRequest, text string) promptkit.RenderedMessage {
|
||||||
|
|||||||
Reference in New Issue
Block a user