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:
@@ -72,7 +72,7 @@ func (a *Agent) HandleMessage(ctx context.Context, content string) (string, erro
|
||||
slog.Warn("memory search failed", "error", err)
|
||||
}
|
||||
|
||||
prompt, err := a.companion.BuildPrompt(ctx, &companion.PromptInput{
|
||||
buildResult, err := a.companion.BuildPrompt(ctx, &companion.PromptInput{
|
||||
UserMessage: content,
|
||||
RecentMessages: a.messages,
|
||||
Memories: memories,
|
||||
@@ -81,11 +81,30 @@ func (a *Agent) HandleMessage(ctx context.Context, content string) (string, erro
|
||||
return "", fmt.Errorf("build prompt: %w", err)
|
||||
}
|
||||
|
||||
segments := []types.PromptSegment{
|
||||
{Type: types.SegSystem, Content: buildResult.SystemContent},
|
||||
}
|
||||
start := 0
|
||||
if len(a.messages) > 10 {
|
||||
start = len(a.messages) - 10
|
||||
}
|
||||
for _, msg := range a.messages[start:] {
|
||||
segType := types.SegUser
|
||||
if msg.Role == types.RoleAssistant {
|
||||
segType = types.SegAssistant
|
||||
}
|
||||
segments = append(segments, types.PromptSegment{Type: segType, Content: msg.Content})
|
||||
}
|
||||
|
||||
tmpl := llama.NewTemplateEngine(a.cfg.ChatTemplate)
|
||||
prompt := tmpl.Build(segments)
|
||||
|
||||
resp, err := a.llm.Complete(&llama.CompletionRequest{
|
||||
Prompt: prompt,
|
||||
Temperature: a.cfg.Temperature,
|
||||
TopP: a.cfg.TopP,
|
||||
MaxTokens: a.cfg.MaxTokens,
|
||||
Stop: tmpl.StopTokens(),
|
||||
Stream: false,
|
||||
CachePrompt: true,
|
||||
})
|
||||
@@ -93,10 +112,13 @@ func (a *Agent) HandleMessage(ctx context.Context, content string) (string, erro
|
||||
return "", fmt.Errorf("llm complete: %w", err)
|
||||
}
|
||||
|
||||
parsed := ParseResponse(resp.Content)
|
||||
|
||||
assistantMsg := types.Message{
|
||||
ID: fmt.Sprintf("%x", time.Now().UnixNano()),
|
||||
Role: types.RoleAssistant,
|
||||
Content: resp.Content,
|
||||
Content: parsed.Content,
|
||||
Reasoning: parsed.Reasoning,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
a.messages = append(a.messages, assistantMsg)
|
||||
@@ -108,7 +130,7 @@ func (a *Agent) HandleMessage(ctx context.Context, content string) (string, erro
|
||||
}
|
||||
}()
|
||||
|
||||
return resp.Content, nil
|
||||
return parsed.Content, nil
|
||||
}
|
||||
|
||||
func (a *Agent) Messages() []types.Message {
|
||||
@@ -130,8 +152,8 @@ func (a *Agent) saveMessage(msg types.Message) {
|
||||
toolCallJSON = string(b)
|
||||
}
|
||||
err := a.store.Exec(
|
||||
"INSERT INTO messages (id, conversation_id, role, content, tool_call, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
msg.ID, a.conversationID, string(msg.Role), msg.Content, toolCallJSON, msg.CreatedAt.Format(time.RFC3339),
|
||||
"INSERT INTO messages (id, conversation_id, role, content, reasoning, tool_call, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
msg.ID, a.conversationID, string(msg.Role), msg.Content, msg.Reasoning, toolCallJSON, msg.CreatedAt.Format(time.RFC3339),
|
||||
)
|
||||
if err != nil {
|
||||
slog.Warn("failed to save message", "error", err)
|
||||
|
||||
41
internal/agent/reasoning.go
Normal file
41
internal/agent/reasoning.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ParsedResponse struct {
|
||||
Reasoning string
|
||||
Content string
|
||||
}
|
||||
|
||||
func ParseResponse(raw string) ParsedResponse {
|
||||
tags := []struct {
|
||||
open string
|
||||
close string
|
||||
}{
|
||||
{"<reasoning>", "</reasoning>"},
|
||||
{"<reason>", "</reason>"},
|
||||
{"<thinking>", "</thinking>"},
|
||||
{"<think>", "</think>"},
|
||||
{"", ""},
|
||||
}
|
||||
|
||||
for _, t := range tags {
|
||||
if t.open == "" {
|
||||
break
|
||||
}
|
||||
idxOpen := strings.Index(raw, t.open)
|
||||
idxClose := strings.LastIndex(raw, t.close)
|
||||
if idxOpen != -1 && idxClose != -1 && idxClose > idxOpen {
|
||||
reasoning := raw[idxOpen+len(t.open) : idxClose]
|
||||
content := raw[:idxOpen] + raw[idxClose+len(t.close):]
|
||||
return ParsedResponse{
|
||||
Reasoning: strings.TrimSpace(reasoning),
|
||||
Content: strings.TrimSpace(content),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ParsedResponse{Content: strings.TrimSpace(raw)}
|
||||
}
|
||||
@@ -23,6 +23,22 @@ var (
|
||||
Foreground(lipgloss.Color("#10B981")).
|
||||
Bold(true)
|
||||
|
||||
reasoningLabelStyle = lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("#6B7280")).
|
||||
Background(lipgloss.Color("#1F2937")).
|
||||
Padding(0, 1).
|
||||
Italic(true)
|
||||
|
||||
reasoningContentStyle = lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("#6B7280")).
|
||||
Italic(true)
|
||||
|
||||
reasoningHeaderStyle = lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("#6B7280")).
|
||||
Background(lipgloss.Color("#1F2937")).
|
||||
Padding(0, 1).
|
||||
Bold(true)
|
||||
|
||||
inputStyle = lipgloss.NewStyle().
|
||||
BorderStyle(lipgloss.NormalBorder()).
|
||||
BorderForeground(lipgloss.Color("#374151"))
|
||||
@@ -40,20 +56,23 @@ type model struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
|
||||
viewport viewport.Model
|
||||
input string
|
||||
state sessionState
|
||||
messages []types.Message
|
||||
ready bool
|
||||
viewport viewport.Model
|
||||
input string
|
||||
state sessionState
|
||||
messages []types.Message
|
||||
reasoningExpanded map[string]bool
|
||||
reasoningOrder []string
|
||||
ready bool
|
||||
}
|
||||
|
||||
func NewTUIModel(a *agent.Agent) tea.Model {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
return &model{
|
||||
agent: a,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
state: stateReady,
|
||||
agent: a,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
state: stateReady,
|
||||
reasoningExpanded: make(map[string]bool),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +93,11 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
return m, nil
|
||||
|
||||
case tea.MouseMsg:
|
||||
var cmd tea.Cmd
|
||||
m.viewport, cmd = m.viewport.Update(msg)
|
||||
return m, cmd
|
||||
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "ctrl+q":
|
||||
@@ -88,6 +112,15 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, m.sendMessage(input)
|
||||
}
|
||||
|
||||
case "tab":
|
||||
for i := len(m.messages) - 1; i >= 0; i-- {
|
||||
if m.messages[i].Role == types.RoleAssistant && m.messages[i].Reasoning != "" {
|
||||
id := m.messages[i].ID
|
||||
m.reasoningExpanded[id] = !m.reasoningExpanded[id]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
case "backspace":
|
||||
if len(m.input) > 0 {
|
||||
m.input = m.input[:len(m.input)-1]
|
||||
@@ -107,7 +140,22 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
Content: fmt.Sprintf("error: %v", msg.err),
|
||||
})
|
||||
} else {
|
||||
m.messages = m.agent.Messages()
|
||||
newMsgs := m.agent.Messages()
|
||||
for _, msg := range newMsgs {
|
||||
if msg.Role == types.RoleAssistant && msg.Reasoning != "" {
|
||||
found := false
|
||||
for _, id := range m.reasoningOrder {
|
||||
if id == msg.ID {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
m.reasoningOrder = append(m.reasoningOrder, msg.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
m.messages = newMsgs
|
||||
}
|
||||
m.viewport.GotoBottom()
|
||||
}
|
||||
@@ -134,14 +182,26 @@ func (m *model) View() string {
|
||||
default:
|
||||
prefix = "system"
|
||||
}
|
||||
b.WriteString(style.Render(prefix+": ") + msg.Content + "\n\n")
|
||||
b.WriteString(style.Render(prefix+": ") + msg.Content + "\n")
|
||||
|
||||
if msg.Role == types.RoleAssistant && msg.Reasoning != "" {
|
||||
if m.reasoningExpanded[msg.ID] {
|
||||
b.WriteString(reasoningHeaderStyle.Render(" reasoning ") + "\n")
|
||||
b.WriteString(reasoningContentStyle.Render(msg.Reasoning) + "\n")
|
||||
} else {
|
||||
label := reasoningLabelStyle.Render(" ` reasoning (tab)")
|
||||
b.WriteString(label + "\n")
|
||||
}
|
||||
}
|
||||
|
||||
b.WriteString("\n")
|
||||
}
|
||||
|
||||
m.viewport.SetContent(b.String())
|
||||
|
||||
prompt := "> "
|
||||
if m.state == stateWaiting {
|
||||
prompt = "waiting... "
|
||||
prompt = "... "
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s\n%s%s", m.viewport.View(), inputStyle.Render(prompt), m.input)
|
||||
|
||||
@@ -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()}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,9 @@ type Config struct {
|
||||
MemoryTopK int
|
||||
MemoryMinScore float64
|
||||
|
||||
// Chat template
|
||||
ChatTemplate string
|
||||
|
||||
// Embedding
|
||||
EmbeddingModel string
|
||||
|
||||
@@ -76,6 +79,8 @@ func Load() *Config {
|
||||
MemoryTopK: getEnvInt("MEMORY_TOP_K", 5),
|
||||
MemoryMinScore: getEnvFloat("MEMORY_MIN_SCORE", 0.6),
|
||||
|
||||
ChatTemplate: getEnv("CHAT_TEMPLATE", "chatml"),
|
||||
|
||||
EmbeddingModel: getEnv("EMBEDDING_MODEL", ""),
|
||||
|
||||
TraitChangeMaxDaily: getEnvFloat("TRAIT_CHANGE_MAX_DAILY", 0.005),
|
||||
@@ -104,6 +109,11 @@ func (c *Config) Validate() error {
|
||||
if c.DiaryHour < 0 || c.DiaryHour > 23 {
|
||||
return fmt.Errorf("DIARY_HOUR must be 0-23")
|
||||
}
|
||||
switch c.ChatTemplate {
|
||||
case "chatml", "llama3", "none":
|
||||
default:
|
||||
return fmt.Errorf("CHAT_TEMPLATE must be chatml, llama3, or none")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -15,12 +15,13 @@ type Client struct {
|
||||
}
|
||||
|
||||
type CompletionRequest struct {
|
||||
Prompt string `json:"prompt"`
|
||||
Temperature float64 `json:"temperature"`
|
||||
TopP float64 `json:"top_p"`
|
||||
MaxTokens int `json:"n_predict"`
|
||||
Stream bool `json:"stream"`
|
||||
CachePrompt bool `json:"cache_prompt"`
|
||||
Prompt string `json:"prompt"`
|
||||
Temperature float64 `json:"temperature"`
|
||||
TopP float64 `json:"top_p"`
|
||||
MaxTokens int `json:"n_predict"`
|
||||
Stop []string `json:"stop,omitempty"`
|
||||
Stream bool `json:"stream"`
|
||||
CachePrompt bool `json:"cache_prompt"`
|
||||
}
|
||||
|
||||
type CompletionResponse struct {
|
||||
|
||||
71
internal/llama/template.go
Normal file
71
internal/llama/template.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package llama
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.db123.ir/db123/divacode/pkg/types"
|
||||
)
|
||||
|
||||
type TemplateEngine struct {
|
||||
format string
|
||||
}
|
||||
|
||||
func NewTemplateEngine(format string) *TemplateEngine {
|
||||
return &TemplateEngine{format: format}
|
||||
}
|
||||
|
||||
func (te *TemplateEngine) Build(segments []types.PromptSegment) string {
|
||||
switch te.format {
|
||||
case "chatml":
|
||||
return te.buildChatML(segments)
|
||||
case "llama3":
|
||||
return te.buildLlama3(segments)
|
||||
default:
|
||||
return te.buildRaw(segments)
|
||||
}
|
||||
}
|
||||
|
||||
func (te *TemplateEngine) StopTokens() []string {
|
||||
switch te.format {
|
||||
case "chatml":
|
||||
return []string{"<|im_end|>"}
|
||||
case "llama3":
|
||||
return []string{"<|eot_id|>"}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (te *TemplateEngine) buildChatML(segments []types.PromptSegment) string {
|
||||
var b strings.Builder
|
||||
for _, seg := range segments {
|
||||
b.WriteString(fmt.Sprintf("<|im_start|>%s\n%s<|im_end|>\n", seg.Type, seg.Content))
|
||||
}
|
||||
b.WriteString("<|im_start|>assistant\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (te *TemplateEngine) buildLlama3(segments []types.PromptSegment) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("<|begin_of_text|>")
|
||||
for _, seg := range segments {
|
||||
b.WriteString(fmt.Sprintf("<|start_header_id|>%s<|end_header_id|>\n\n%s<|eot_id|>", seg.Type, seg.Content))
|
||||
}
|
||||
b.WriteString("<|start_header_id|>assistant<|end_header_id|>\n\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (te *TemplateEngine) buildRaw(segments []types.PromptSegment) string {
|
||||
var b strings.Builder
|
||||
for _, seg := range segments {
|
||||
switch seg.Type {
|
||||
case types.SegSystem:
|
||||
b.WriteString("[System]\n" + seg.Content + "\n\n")
|
||||
case types.SegUser:
|
||||
b.WriteString("User: " + seg.Content + "\n")
|
||||
}
|
||||
}
|
||||
b.WriteString("You:")
|
||||
return b.String()
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package storage
|
||||
var migrations = []string{
|
||||
divacodeSchema,
|
||||
companionSchema,
|
||||
addReasoningColumn,
|
||||
}
|
||||
|
||||
const divacodeSchema = `
|
||||
@@ -17,6 +18,7 @@ CREATE TABLE IF NOT EXISTS messages (
|
||||
conversation_id TEXT NOT NULL REFERENCES conversations(id),
|
||||
role TEXT NOT NULL CHECK(role IN ('user','assistant','system','tool')),
|
||||
content TEXT NOT NULL,
|
||||
reasoning TEXT,
|
||||
tool_call TEXT,
|
||||
token_count INTEGER,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
@@ -111,3 +113,5 @@ CREATE TABLE IF NOT EXISTS agent_strategies (
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
`
|
||||
|
||||
const addReasoningColumn = `ALTER TABLE messages ADD COLUMN reasoning TEXT;`
|
||||
|
||||
@@ -29,7 +29,12 @@ func Open(path string) (*DB, error) {
|
||||
|
||||
func (db *DB) Migrate() error {
|
||||
for _, m := range migrations {
|
||||
if err := db.Exec(m); err != nil {
|
||||
err := db.Exec(m)
|
||||
if err != nil && m == addReasoningColumn {
|
||||
// may already exist on fresh DBs
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user