feat(companion): add reflection engine, diary system, and dynamic prompt builder
This commit is contained in:
78
internal/companion/promptbuilder.go
Normal file
78
internal/companion/promptbuilder.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package companion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.db123.ir/db123/divacode/internal/config"
|
||||
"git.db123.ir/db123/divacode/pkg/types"
|
||||
)
|
||||
|
||||
type PromptBuilder struct {
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
func NewPromptBuilder(cfg *config.Config) *PromptBuilder {
|
||||
return &PromptBuilder{cfg: cfg}
|
||||
}
|
||||
|
||||
func (pb *PromptBuilder) Build(in *BuildInput) string {
|
||||
var b strings.Builder
|
||||
|
||||
b.WriteString("[System]\n")
|
||||
b.WriteString(in.SystemPrompt)
|
||||
b.WriteString("\n\n")
|
||||
|
||||
b.WriteString("### Current Personality\n")
|
||||
for _, t := range in.Traits {
|
||||
label := string(t.Trait)
|
||||
pct := int(t.Value * 100)
|
||||
b.WriteString(fmt.Sprintf("- %s: %d%%\n", label, pct))
|
||||
}
|
||||
|
||||
b.WriteString("\n### Current Mood\n")
|
||||
b.WriteString(string(in.Mood))
|
||||
b.WriteString("\n")
|
||||
|
||||
if in.Relationship != nil && in.Relationship.Familiarity > 0.1 {
|
||||
b.WriteString(fmt.Sprintf("\n### Relationship Context\n"))
|
||||
b.WriteString(fmt.Sprintf("- Familiarity: %d%%\n", int(in.Relationship.Familiarity*100)))
|
||||
b.WriteString(fmt.Sprintf("- Trust: %d%%\n", int(in.Relationship.Trust*100)))
|
||||
if len(in.Relationship.SharedTopics) > 0 {
|
||||
b.WriteString(fmt.Sprintf("- Shared interests: %s\n", strings.Join(in.Relationship.SharedTopics, ", ")))
|
||||
}
|
||||
}
|
||||
|
||||
if len(in.Observations) > 0 {
|
||||
b.WriteString("\n### Recent Observations\n")
|
||||
for _, o := range in.Observations {
|
||||
b.WriteString(fmt.Sprintf("- %s\n", o.Content))
|
||||
}
|
||||
}
|
||||
|
||||
if len(in.Memories) > 0 {
|
||||
b.WriteString("\n### Relevant Memories\n")
|
||||
for i, m := range in.Memories {
|
||||
if i >= 5 {
|
||||
break
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("- %s\n", m))
|
||||
}
|
||||
}
|
||||
|
||||
b.WriteString("\n### Recent Conversation\n")
|
||||
start := 0
|
||||
if len(in.Messages) > 10 {
|
||||
start = len(in.Messages) - 10
|
||||
}
|
||||
for _, msg := range in.Messages[start:] {
|
||||
prefix := "User"
|
||||
if msg.Role == types.RoleAssistant {
|
||||
prefix = "You"
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("%s: %s\n", prefix, msg.Content))
|
||||
}
|
||||
|
||||
b.WriteString("\nYou:")
|
||||
return b.String()
|
||||
}
|
||||
Reference in New Issue
Block a user