feat: Phase 2 — chat templates, reasoning parser, collapsible TUI, mouse support
- internal/llama/template.go: ChatML/Llama3 template engine with stop tokens - internal/agent/reasoning.go: parse <think>/<reasoning> tags from LLM output - companion/promptbuilder.go: output structured BuildResult, add date/time context - agent/agent.go: wire templates + reasoning split into message loop - storage: add reasoning TEXT column to messages table + migration - channel/tui.go: collapsible reasoning (tab toggle), mouse scroll, polished dimmed style - config: CHAT_TEMPLATE env var (chatml|llama3|none) - plan.md: mark Phase 2 complete, add themes TODO
This commit is contained in:
@@ -45,7 +45,7 @@ func NewEngine(cfg *config.Config, db *storage.DB) *Engine {
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) BuildPrompt(ctx context.Context, input *PromptInput) (string, error) {
|
||||
func (e *Engine) BuildPrompt(ctx context.Context, input *PromptInput) (*BuildResult, error) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
||||
@@ -54,7 +54,7 @@ func (e *Engine) BuildPrompt(ctx context.Context, input *PromptInput) (string, e
|
||||
mood := e.mood.Current()
|
||||
observations := e.reflection.RecentObservations(5)
|
||||
|
||||
prompt := e.promptBuilder.Build(&BuildInput{
|
||||
result := e.promptBuilder.Build(&BuildInput{
|
||||
SystemPrompt: e.cfg.SystemPrompt,
|
||||
Traits: traits,
|
||||
Relationship: rel,
|
||||
@@ -66,7 +66,7 @@ func (e *Engine) BuildPrompt(ctx context.Context, input *PromptInput) (string, e
|
||||
})
|
||||
|
||||
e.lastContact = time.Now()
|
||||
return prompt, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (e *Engine) WriteDiary(ctx context.Context) error {
|
||||
|
||||
@@ -3,9 +3,9 @@ package companion
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.db123.ir/db123/divacode/internal/config"
|
||||
"git.db123.ir/db123/divacode/pkg/types"
|
||||
)
|
||||
|
||||
type PromptBuilder struct {
|
||||
@@ -16,26 +16,29 @@ func NewPromptBuilder(cfg *config.Config) *PromptBuilder {
|
||||
return &PromptBuilder{cfg: cfg}
|
||||
}
|
||||
|
||||
func (pb *PromptBuilder) Build(in *BuildInput) string {
|
||||
type BuildResult struct {
|
||||
SystemContent string
|
||||
}
|
||||
|
||||
func (pb *PromptBuilder) Build(in *BuildInput) *BuildResult {
|
||||
var b strings.Builder
|
||||
|
||||
b.WriteString("[System]\n")
|
||||
now := time.Now()
|
||||
b.WriteString(fmt.Sprintf("Current date and time: %s\n", now.Format("2006-01-02 15:04:05 (MST)")))
|
||||
b.WriteString(in.SystemPrompt)
|
||||
b.WriteString("\n\n")
|
||||
|
||||
b.WriteString("### Current Personality\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")
|
||||
b.WriteString(fmt.Sprintf("\nMood: %s\n", in.Mood))
|
||||
|
||||
if in.Relationship != nil && in.Relationship.Familiarity > 0.1 {
|
||||
b.WriteString(fmt.Sprintf("\n### Relationship Context\n"))
|
||||
b.WriteString("\nRelationship:\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 {
|
||||
@@ -44,14 +47,14 @@ func (pb *PromptBuilder) Build(in *BuildInput) string {
|
||||
}
|
||||
|
||||
if len(in.Observations) > 0 {
|
||||
b.WriteString("\n### Recent Observations\n")
|
||||
b.WriteString("\nObservations:\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")
|
||||
b.WriteString("\nMemories:\n")
|
||||
for i, m := range in.Memories {
|
||||
if i >= 5 {
|
||||
break
|
||||
@@ -60,19 +63,5 @@ func (pb *PromptBuilder) Build(in *BuildInput) string {
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
return &BuildResult{SystemContent: b.String()}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user