Store session history, preferences, and dated records in one database per home. Commit complete turns, checkpoint tool batches, and compact history by prefix. Add memory search and writes, with previews for ambiguous corrections. Review conversations in the background and support SQLite backup and restore.
136 lines
4.5 KiB
Go
136 lines
4.5 KiB
Go
// PicoClaw - Ultra-lightweight personal AI agent
|
|
|
|
package agent
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/logger"
|
|
"github.com/sipeed/picoclaw/pkg/providers"
|
|
)
|
|
|
|
// SetupTurn extracts the one-time initialization phase, returning a
|
|
// turnExecution populated with history, messages, and candidate selection.
|
|
// It replaces lines 56-145 of the original runTurn.
|
|
func (p *Pipeline) SetupTurn(ctx context.Context, ts *turnState) (*turnExecution, error) {
|
|
cfg := p.Cfg
|
|
maxMediaSize := cfg.Agents.Defaults.GetMaxMediaSize()
|
|
|
|
var history []providers.Message
|
|
var summary string
|
|
if !ts.opts.NoHistory {
|
|
snap, err := p.al.contextManager.beginTurn(ctx, ts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
history = snap.Messages
|
|
summary = boundedSessionSummary(snap.Summary, ts.agent.ContextWindow, ts.agent.MaxTokens)
|
|
}
|
|
|
|
contextualSkills := ts.activeSkills
|
|
if ts.agent.ContextBuilder != nil {
|
|
contextualSkills = ts.agent.ContextBuilder.ResolveActiveSkillsForContext(ts.activeSkills)
|
|
}
|
|
ts.recordSkillContextSnapshot(skillContextTriggerInitialBuild, contextualSkills)
|
|
initialPromptReq := promptBuildRequestForTurn(ts, history, summary, ts.userMessage, ts.media, cfg)
|
|
initialPromptReq.ActiveSkills = append([]string(nil), contextualSkills...)
|
|
messages := ts.agent.ContextBuilder.BuildMessagesFromPrompt(initialPromptReq)
|
|
currentTurnStart := len(messages)
|
|
if strings.TrimSpace(ts.userMessage) != "" || len(ts.media) > 0 {
|
|
currentTurnStart = len(messages) - 1
|
|
}
|
|
|
|
messages = resolveMediaRefs(messages, p.MediaStore, maxMediaSize, currentTurnStart)
|
|
|
|
if !ts.opts.NoHistory {
|
|
toolDefs := turnToolDefs(ts.agent.Tools.ToProviderDefs(), ts)
|
|
if isOverContextBudget(ts.agent.ContextWindow, messages, toolDefs, ts.agent.MaxTokens) {
|
|
logger.WarnCF("agent", "Proactive compression: context budget exceeded before LLM call",
|
|
map[string]any{"session_key": ts.sessionKey})
|
|
originalHistoryCount := len(history)
|
|
var fit bool
|
|
history, messages, fit = trimHistoryToFitContextWindow(
|
|
history,
|
|
func(trimmedHistory []providers.Message) []providers.Message {
|
|
rebuildPromptReq := promptBuildRequestForTurn(
|
|
ts,
|
|
trimmedHistory,
|
|
summary,
|
|
ts.userMessage,
|
|
ts.media,
|
|
cfg,
|
|
)
|
|
rebuildPromptReq.ActiveSkills = append([]string(nil), contextualSkills...)
|
|
rebuilt := ts.agent.ContextBuilder.BuildMessagesFromPrompt(rebuildPromptReq)
|
|
rebuiltCurrentTurnStart := len(rebuilt)
|
|
if strings.TrimSpace(ts.userMessage) != "" || len(ts.media) > 0 {
|
|
rebuiltCurrentTurnStart = len(rebuilt) - 1
|
|
}
|
|
return resolveMediaRefs(rebuilt, p.MediaStore, maxMediaSize, rebuiltCurrentTurnStart)
|
|
},
|
|
ts.agent.ContextWindow,
|
|
toolDefs,
|
|
ts.agent.MaxTokens,
|
|
)
|
|
if dropped := originalHistoryCount - len(history); dropped > 0 {
|
|
logger.WarnCF("agent", "Trimmed rebuilt history after proactive compaction", map[string]any{
|
|
"session_key": ts.sessionKey,
|
|
"dropped_msgs": dropped,
|
|
"remaining_msgs": len(history),
|
|
"context_window": ts.agent.ContextWindow,
|
|
"max_tokens": ts.agent.MaxTokens,
|
|
"still_overlimit": !fit,
|
|
})
|
|
} else if !fit {
|
|
logger.WarnCF("agent", "Context still exceeds budget "+
|
|
"after proactive compaction rebuild", map[string]any{
|
|
"session_key": ts.sessionKey,
|
|
"history_msgs": len(history),
|
|
"context_window": ts.agent.ContextWindow,
|
|
"max_tokens": ts.agent.MaxTokens,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
if !ts.opts.NoHistory && (strings.TrimSpace(ts.userMessage) != "" || len(ts.media) > 0) {
|
|
rootMsg := userPromptMessage(ts.userMessage, ts.media)
|
|
ts.addTurnMessage(rootMsg)
|
|
}
|
|
|
|
activeCandidates, activeModel, usedLight := p.al.selectCandidates(ts.agent, ts.userMessage, messages)
|
|
activeProvider := ts.agent.Provider
|
|
if usedLight && ts.agent.LightProvider != nil {
|
|
activeProvider = ts.agent.LightProvider
|
|
}
|
|
activeModelName := strings.TrimSpace(ts.agent.Model)
|
|
if usedLight {
|
|
activeModelName = strings.TrimSpace(sideQuestionModelName(ts.agent, true))
|
|
}
|
|
activeModelName = resolvedCandidateModelName(activeCandidates, activeModelName)
|
|
|
|
exec := newTurnExecution(
|
|
ts.agent,
|
|
ts.opts,
|
|
history,
|
|
summary,
|
|
messages,
|
|
)
|
|
exec.currentTurnStart = currentTurnStart
|
|
exec.activeCandidates = activeCandidates
|
|
exec.activeModel = activeModel
|
|
exec.activeModelConfig = resolveActiveModelConfig(
|
|
p.Cfg,
|
|
ts.agent.Workspace,
|
|
activeCandidates,
|
|
activeModel,
|
|
p.Cfg.Agents.Defaults.Provider,
|
|
)
|
|
exec.llmModelName = activeModelName
|
|
exec.activeProvider = activeProvider
|
|
exec.usedLight = usedLight
|
|
|
|
return exec, nil
|
|
}
|