206 lines
6.4 KiB
Go
206 lines
6.4 KiB
Go
|
|
package shared
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"mime"
|
||
|
|
"sort"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||
|
|
"gitea.maximumdirect.net/eric/notarius/internal/integrations/domainrefs"
|
||
|
|
)
|
||
|
|
|
||
|
|
// PreparedContext holds immutable, task-specific reference material.
|
||
|
|
type PreparedContext struct {
|
||
|
|
party, glossary, players contracts.LLMInputMaterial
|
||
|
|
}
|
||
|
|
|
||
|
|
type partyMember struct {
|
||
|
|
Name string `json:"name"`
|
||
|
|
Aliases []string `json:"aliases,omitempty"`
|
||
|
|
Player string `json:"player,omitempty"`
|
||
|
|
Classes []partyClass `json:"classes,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type partyClass struct {
|
||
|
|
Name string `json:"name"`
|
||
|
|
Level any `json:"level,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type glossaryEntry struct {
|
||
|
|
Name string `json:"name"`
|
||
|
|
Aliases []string `json:"aliases,omitempty"`
|
||
|
|
Category string `json:"category"`
|
||
|
|
Description *string `json:"description,omitempty"`
|
||
|
|
Plural *string `json:"plural,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// PrepareContext validates supplied typed references and prepares model-facing views.
|
||
|
|
func PrepareContext(references contracts.ReferenceSet) (PreparedContext, error) {
|
||
|
|
if err := ValidateReferenceSelections(references); err != nil {
|
||
|
|
return PreparedContext{}, err
|
||
|
|
}
|
||
|
|
party, partyBound, err := prepareParty(references.Slots["party"], "party")
|
||
|
|
if err != nil {
|
||
|
|
return PreparedContext{}, err
|
||
|
|
}
|
||
|
|
roster, _, err := prepareParty(references.Slots["roster"], "roster")
|
||
|
|
if err != nil {
|
||
|
|
return PreparedContext{}, err
|
||
|
|
}
|
||
|
|
if !partyBound {
|
||
|
|
party = roster
|
||
|
|
}
|
||
|
|
glossary, err := prepareGlossary(references.Slots["glossary"])
|
||
|
|
if err != nil {
|
||
|
|
return PreparedContext{}, err
|
||
|
|
}
|
||
|
|
return PreparedContext{
|
||
|
|
party: party,
|
||
|
|
glossary: glossary,
|
||
|
|
players: ReferencePromptMaterial("players", references.Slots["players"]),
|
||
|
|
}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// Inputs combines fixed reference context with the current transcript.
|
||
|
|
func (context PreparedContext) Inputs(source contracts.LLMInputMaterial) contracts.LLMInputSet {
|
||
|
|
return contracts.LLMInputSet{
|
||
|
|
"transcript": TranscriptPromptMaterial(source),
|
||
|
|
"party": context.party.Clone(),
|
||
|
|
"glossary": context.glossary.Clone(),
|
||
|
|
"players": context.players.Clone(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func referenceDocument(slot contracts.ResolvedReferenceSlot, name string) (map[string]any, *contracts.ReferenceItem, error) {
|
||
|
|
if len(slot.Items) == 0 {
|
||
|
|
return nil, nil, nil
|
||
|
|
}
|
||
|
|
if len(slot.Items) != 1 {
|
||
|
|
return nil, nil, fmt.Errorf("reference slot %q must contain zero or one item", name)
|
||
|
|
}
|
||
|
|
item := &slot.Items[0]
|
||
|
|
mediaType, _, err := mime.ParseMediaType(item.MediaType)
|
||
|
|
if err != nil {
|
||
|
|
return nil, nil, fmt.Errorf("reference slot %q: invalid media type: %w", name, err)
|
||
|
|
}
|
||
|
|
doc, err := domainrefs.Load(name, strings.ToLower(mediaType), item.Content)
|
||
|
|
if err != nil {
|
||
|
|
return nil, nil, err
|
||
|
|
}
|
||
|
|
return doc, item, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func prepareParty(slot contracts.ResolvedReferenceSlot, name string) (contracts.LLMInputMaterial, bool, error) {
|
||
|
|
doc, item, err := referenceDocument(slot, name)
|
||
|
|
if err != nil {
|
||
|
|
return contracts.LLMInputMaterial{}, false, err
|
||
|
|
}
|
||
|
|
if item == nil {
|
||
|
|
return contextMaterial("party", nil), false, nil
|
||
|
|
}
|
||
|
|
if err := domainrefs.ValidateParty(doc); err != nil {
|
||
|
|
return contracts.LLMInputMaterial{}, false, fmt.Errorf("reference slot %q: %w", name, err)
|
||
|
|
}
|
||
|
|
characters := doc["characters"].(map[string]any)
|
||
|
|
ids := make([]string, 0, len(characters))
|
||
|
|
for id := range characters {
|
||
|
|
ids = append(ids, id)
|
||
|
|
}
|
||
|
|
sort.Strings(ids)
|
||
|
|
members := make([]partyMember, 0, len(ids))
|
||
|
|
for _, id := range ids {
|
||
|
|
entry := characters[id].(map[string]any)
|
||
|
|
character := entry["character"].(map[string]any)
|
||
|
|
member := partyMember{Name: character["name"].(string), Aliases: stringList(character["aliases"])}
|
||
|
|
if player, ok := entry["player"].(map[string]any); ok {
|
||
|
|
member.Player = player["name"].(string)
|
||
|
|
}
|
||
|
|
if classes, ok := character["classes"].([]any); ok {
|
||
|
|
for _, raw := range classes {
|
||
|
|
class := raw.(map[string]any)
|
||
|
|
member.Classes = append(member.Classes, partyClass{Name: class["name"].(string), Level: class["level"]})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
members = append(members, member)
|
||
|
|
}
|
||
|
|
content, err := json.Marshal(members)
|
||
|
|
if err != nil {
|
||
|
|
return contracts.LLMInputMaterial{}, false, fmt.Errorf("reference slot %q projection: %w", name, err)
|
||
|
|
}
|
||
|
|
if len(members) == 0 {
|
||
|
|
content = nil
|
||
|
|
}
|
||
|
|
return contextMaterial("party", content), true, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func prepareGlossary(slot contracts.ResolvedReferenceSlot) (contracts.LLMInputMaterial, error) {
|
||
|
|
doc, item, err := referenceDocument(slot, "glossary")
|
||
|
|
if err != nil {
|
||
|
|
return contracts.LLMInputMaterial{}, err
|
||
|
|
}
|
||
|
|
if item == nil {
|
||
|
|
return contextMaterial("glossary", nil), nil
|
||
|
|
}
|
||
|
|
if err := domainrefs.ValidateGlossary(doc); err != nil {
|
||
|
|
return contracts.LLMInputMaterial{}, fmt.Errorf("reference slot %q: %w", "glossary", err)
|
||
|
|
}
|
||
|
|
collections := doc["collections"].(map[string]any)
|
||
|
|
var defaults []string
|
||
|
|
for _, name := range []string{"campaign", "setting"} {
|
||
|
|
if _, ok := collections[name]; ok {
|
||
|
|
defaults = append(defaults, name)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
names, err := domainrefs.SelectCollections(doc, item.Collections, defaults)
|
||
|
|
if err != nil {
|
||
|
|
return contracts.LLMInputMaterial{}, fmt.Errorf("reference slot %q: %w", "glossary", err)
|
||
|
|
}
|
||
|
|
var projected []glossaryEntry
|
||
|
|
for _, name := range names {
|
||
|
|
entries := append([]any(nil), collections[name].(map[string]any)["entries"].([]any)...)
|
||
|
|
sort.Slice(entries, func(i, j int) bool {
|
||
|
|
return entries[i].(map[string]any)["id"].(string) < entries[j].(map[string]any)["id"].(string)
|
||
|
|
})
|
||
|
|
for _, raw := range entries {
|
||
|
|
entry := raw.(map[string]any)
|
||
|
|
value := glossaryEntry{Name: entry["name"].(string), Category: entry["category"].(string), Aliases: stringList(entry["aliases"])}
|
||
|
|
if description, ok := entry["description"].(string); ok {
|
||
|
|
value.Description = &description
|
||
|
|
}
|
||
|
|
if plural, ok := entry["plural"].(string); ok {
|
||
|
|
value.Plural = &plural
|
||
|
|
}
|
||
|
|
projected = append(projected, value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
var content []byte
|
||
|
|
if len(projected) != 0 {
|
||
|
|
content, err = json.Marshal(projected)
|
||
|
|
if err != nil {
|
||
|
|
return contracts.LLMInputMaterial{}, fmt.Errorf("reference slot %q projection: %w", "glossary", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return contextMaterial("glossary", content), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func stringList(value any) []string {
|
||
|
|
items, ok := value.([]any)
|
||
|
|
if !ok {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
result := make([]string, len(items))
|
||
|
|
for i, item := range items {
|
||
|
|
result[i] = item.(string)
|
||
|
|
}
|
||
|
|
return result
|
||
|
|
}
|
||
|
|
|
||
|
|
func contextMaterial(name string, content []byte) contracts.LLMInputMaterial {
|
||
|
|
if len(content) == 0 {
|
||
|
|
content = []byte(" ")
|
||
|
|
}
|
||
|
|
return contracts.NewLLMInputMaterial(name, "text/plain", content, "", "")
|
||
|
|
}
|