2026-07-23 20:09:59 +00:00
|
|
|
package scenedescriptions
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-11-22 01:26:49 +00:00
|
|
|
"encoding/json"
|
2026-07-23 20:09:59 +00:00
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-11-22 01:26:49 +00:00
|
|
|
"go.maximumdirect.net/promptkit"
|
|
|
|
|
|
2026-07-23 20:09:59 +00:00
|
|
|
"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"
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const Key = "dnd/scene-descriptions"
|
2026-11-22 01:26:49 +00:00
|
|
|
const PromptID = "notarius/dnd.scene_descriptions"
|
|
|
|
|
const CorrectionPromptID = "notarius/dnd.scene_descriptions.repair"
|
2026-07-23 20:09:59 +00:00
|
|
|
|
|
|
|
|
var requiredCapabilities = []string{
|
|
|
|
|
"chunks",
|
|
|
|
|
"source.transcript",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var providedCapabilities = []string{
|
|
|
|
|
"dnd.scene_descriptions",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var referenceSlotDescriptions = shared.ReferenceSlotDescriptions{
|
|
|
|
|
Glossary: "Optional campaign glossary reference material used only to disambiguate scene descriptions.",
|
|
|
|
|
Party: "Optional party roster reference material used only to disambiguate scene descriptions.",
|
|
|
|
|
Players: "Optional player list reference material used only to disambiguate scene descriptions.",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func referenceSlots() []contracts.ReferenceSlot {
|
|
|
|
|
all := shared.ReferenceSlots(referenceSlotDescriptions)
|
|
|
|
|
slots := make([]contracts.ReferenceSlot, 0, 3)
|
|
|
|
|
for _, slot := range all {
|
|
|
|
|
if slot.Name != "roster" {
|
|
|
|
|
slots = append(slots, slot)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return slots
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ contracts.Extractor[dnd.SceneDescriptionList] = (*Extractor)(nil)
|
|
|
|
|
|
|
|
|
|
type Options struct{}
|
|
|
|
|
|
2026-11-22 01:26:49 +00:00
|
|
|
type Extractor struct{ engine *promptkit.Engine }
|
2026-07-23 20:09:59 +00:00
|
|
|
|
2026-11-22 01:26:49 +00:00
|
|
|
func New(engine *promptkit.Engine, _ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
|
|
|
|
|
if engine == nil {
|
|
|
|
|
return nil, extractorErrorf("native engine must not be nil")
|
2026-07-23 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
if len(references) > 1 {
|
|
|
|
|
return nil, extractorErrorf("at most one reference set may be supplied")
|
|
|
|
|
}
|
2026-11-22 01:26:49 +00:00
|
|
|
return &Extractor{engine: engine}, nil
|
2026-07-23 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Extractor) Key() string { return Key }
|
|
|
|
|
|
|
|
|
|
func (e *Extractor) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
|
|
|
|
|
2026-11-22 01:26:49 +00:00
|
|
|
func (e *Extractor) Extract(ctx context.Context, req contracts.TypedExtractionRequest[dnd.SceneDescriptionList]) (contracts.TypedExtractionResult[dnd.SceneDescriptionList], error) {
|
2026-07-23 20:09:59 +00:00
|
|
|
if e == nil {
|
|
|
|
|
return contracts.TypedExtractionResult[dnd.SceneDescriptionList]{}, extractorErrorf("extractor must not be nil")
|
|
|
|
|
}
|
2026-11-22 01:26:49 +00:00
|
|
|
if e.engine == nil {
|
|
|
|
|
return contracts.TypedExtractionResult[dnd.SceneDescriptionList]{}, extractorErrorf("native engine must not be nil")
|
2026-07-23 20:09:59 +00:00
|
|
|
}
|
2026-07-24 14:38:42 +00:00
|
|
|
sourceInput, err := shared.PrepareChunkExtraction(ctx, req)
|
2026-07-23 20:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return contracts.TypedExtractionResult[dnd.SceneDescriptionList]{}, extractorErrorf("%w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 01:26:49 +00:00
|
|
|
chunk := source.Chunk{ID: req.Chunk.ID, Ref: req.Chunk.Ref}
|
|
|
|
|
index := source.NewDocumentIndex(req.Source)
|
|
|
|
|
materials := shared.PromptInputs(sourceInput, req.References)
|
|
|
|
|
inputs := make(map[string]promptkit.ArtifactRef, len(materials))
|
|
|
|
|
for name, material := range materials {
|
|
|
|
|
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
|
|
|
|
}
|
|
|
|
|
initial := promptkit.RunRequest{PromptID: PromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID, Inputs: inputs}
|
|
|
|
|
value, outcome, err := pipeline.RunProducer(ctx, e.engine, req.Operation, initial,
|
|
|
|
|
func(_ context.Context, result *promptkit.RunResult) (*dnd.SceneDescriptionList, []string, error) {
|
|
|
|
|
var response extractionResponse
|
|
|
|
|
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
|
|
|
|
return nil, nil, extractorErrorf("decode native output: %w", err)
|
|
|
|
|
}
|
|
|
|
|
value := mapResponse(response, &chunk)
|
|
|
|
|
return &value, nil, nil
|
|
|
|
|
},
|
|
|
|
|
func(_ context.Context, guidance []string) (promptkit.RunRequest, error) {
|
|
|
|
|
return promptkit.RunRequest{PromptID: CorrectionPromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID,
|
|
|
|
|
Inputs: map[string]promptkit.ArtifactRef{"guidance": promptkit.Inline(strings.Join(guidance, "\n\n"))}}, nil
|
|
|
|
|
},
|
|
|
|
|
func(_ context.Context, final *dnd.SceneDescriptionList) (dnd.SceneDescriptionList, error) {
|
|
|
|
|
return sceneFallback(final, chunk, index)
|
|
|
|
|
})
|
2026-08-26 23:55:23 +00:00
|
|
|
if err != nil {
|
2026-11-22 01:26:49 +00:00
|
|
|
return contracts.TypedExtractionResult[dnd.SceneDescriptionList]{}, extractorErrorf("produce scene description: %w", err)
|
2026-08-26 23:55:23 +00:00
|
|
|
}
|
2026-11-22 01:26:49 +00:00
|
|
|
return contracts.TypedExtractionResult[dnd.SceneDescriptionList]{Value: value, Outcome: outcome}, nil
|
2026-07-23 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mapResponse(response extractionResponse, chunk *source.Chunk) dnd.SceneDescriptionList {
|
|
|
|
|
return dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{
|
|
|
|
|
ID: chunk.ID,
|
|
|
|
|
SourceRef: chunk.Ref,
|
|
|
|
|
Kind: response.Kind,
|
|
|
|
|
Title: strings.TrimSpace(response.Title),
|
|
|
|
|
Summary: strings.TrimSpace(response.Summary),
|
|
|
|
|
}}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ModuleSpec() pipeline.ModuleSpec {
|
|
|
|
|
return pipeline.ModuleSpec{
|
2026-11-22 01:26:49 +00:00
|
|
|
Key: Key,
|
|
|
|
|
Stage: pipeline.StageExtract,
|
|
|
|
|
ExecutionClass: contracts.ExecutionClassLLMBacked,
|
|
|
|
|
Prompts: []string{PromptID, CorrectionPromptID},
|
|
|
|
|
Requires: append([]string(nil), requiredCapabilities...),
|
|
|
|
|
Provides: append([]string(nil), providedCapabilities...),
|
|
|
|
|
ArtifactKind: dnd.SceneDescriptionListKind,
|
|
|
|
|
ReferenceSlots: referenceSlots(),
|
2026-07-23 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Register(registry *pipeline.ExtractorRegistry) error {
|
|
|
|
|
return pipeline.RegisterExtractorBuilder(registry, ModuleSpec(), validateOptions, func(request pipeline.BuildRequest) (contracts.Extractor[dnd.SceneDescriptionList], error) {
|
|
|
|
|
options, err := DecodeOptions(request.Options)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-11-22 01:26:49 +00:00
|
|
|
return New(request.Dependencies.Engine, options, request.References)
|
2026-07-23 20:09:59 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateOptions(options map[string]any) error {
|
|
|
|
|
_, err := DecodeOptions(options)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DecodeOptions(options map[string]any) (Options, error) {
|
|
|
|
|
if err := pipeline.RejectUnknownOptions(options); err != nil {
|
|
|
|
|
return Options{}, extractorErrorf("%w", err)
|
|
|
|
|
}
|
|
|
|
|
return Options{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func extractorErrorf(format string, args ...any) error {
|
|
|
|
|
return fmt.Errorf("dnd scene descriptions extractor: "+format, args...)
|
|
|
|
|
}
|