add memory_write tool and align memory path guidance

This commit is contained in:
DBT
2026-02-23 09:53:59 +00:00
parent e038c00ebc
commit 756bfe2302
3 changed files with 134 additions and 387 deletions

View File

@@ -1,36 +1,25 @@
package agent package agent
import ( import (
"encoding/base64"
"fmt" "fmt"
"mime"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"time" "time"
"clawgo/pkg/config"
"clawgo/pkg/logger" "clawgo/pkg/logger"
"clawgo/pkg/providers" "clawgo/pkg/providers"
"clawgo/pkg/skills" "clawgo/pkg/skills"
) )
type ContextBuilder struct { type ContextBuilder struct {
workspace string workspace string
skillsLoader *skills.SkillsLoader skillsLoader *skills.SkillsLoader
memory *MemoryStore memory *MemoryStore
toolsSummary func() []string // Function to get tool summaries dynamically toolsSummary func() []string // Function to get tool summaries dynamically
summaryPolicy systemSummaryPolicy
} }
const (
maxInlineMediaFileBytes int64 = 5 * 1024 * 1024
maxInlineMediaTotalBytes int64 = 12 * 1024 * 1024
maxSystemTaskSummaries = 4
maxSystemTaskSummariesChars = 2400
)
func getGlobalConfigDir() string { func getGlobalConfigDir() string {
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
@@ -39,19 +28,18 @@ func getGlobalConfigDir() string {
return filepath.Join(home, ".clawgo") return filepath.Join(home, ".clawgo")
} }
func NewContextBuilder(workspace string, memCfg config.MemoryConfig, summaryCfg config.SystemSummaryPolicyConfig, toolsSummaryFunc func() []string) *ContextBuilder { func NewContextBuilder(workspace string, toolsSummaryFunc func() []string) *ContextBuilder {
// Built-in skills: the current project's skills directory. // builtin skills: 当前项目的 skills 目录
// Use the skills/ directory under the current working directory. // 使用当前工作目录下的 skills/ 目录
wd, _ := os.Getwd() wd, _ := os.Getwd()
builtinSkillsDir := filepath.Join(wd, "skills") builtinSkillsDir := filepath.Join(wd, "skills")
globalSkillsDir := filepath.Join(getGlobalConfigDir(), "skills") globalSkillsDir := filepath.Join(getGlobalConfigDir(), "skills")
return &ContextBuilder{ return &ContextBuilder{
workspace: workspace, workspace: workspace,
skillsLoader: skills.NewSkillsLoader(workspace, globalSkillsDir, builtinSkillsDir), skillsLoader: skills.NewSkillsLoader(workspace, globalSkillsDir, builtinSkillsDir),
memory: NewMemoryStore(workspace, memCfg), memory: NewMemoryStore(workspace),
toolsSummary: toolsSummaryFunc, toolsSummary: toolsSummaryFunc,
summaryPolicy: systemSummaryPolicyFromConfig(summaryCfg),
} }
} }
@@ -75,20 +63,15 @@ You are clawgo, a helpful AI assistant.
## Workspace ## Workspace
Your workspace is at: %s Your workspace is at: %s
- Memory: %s/memory/MEMORY.md - Long-term Memory: %s/MEMORY.md
- Daily Notes: %s/memory/YYYYMM/YYYYMMDD.md - Daily Notes: %s/memory/YYYY-MM-DD.md
- Skills: %s/skills/{skill-name}/SKILL.md - Skills: %s/skills/{skill-name}/SKILL.md
%s %s
## Important Rules Always be helpful, accurate, and concise. When using tools, explain what you're doing.
When remembering something long-term, write to %s/MEMORY.md and use daily notes at %s/memory/YYYY-MM-DD.md for short-term logs.`,
1. **ALWAYS use tools** - When you need to perform an action (schedule reminders, send messages, execute commands, etc.), you MUST call the appropriate tool. Do NOT just say you'll do it or pretend to do it. now, runtime, workspacePath, workspacePath, workspacePath, workspacePath, toolsSection, workspacePath, workspacePath)
2. **Be helpful and accurate** - When using tools, briefly explain what you're doing.
3. **Memory** - When remembering something, write to %s/memory/MEMORY.md. Prompt memory context is digest-only; use memory_search to retrieve detailed notes when needed.`,
now, runtime, workspacePath, workspacePath, workspacePath, workspacePath, toolsSection, workspacePath)
} }
func (cb *ContextBuilder) buildToolsSection() string { func (cb *ContextBuilder) buildToolsSection() string {
@@ -167,7 +150,6 @@ func (cb *ContextBuilder) BuildMessages(history []providers.Message, summary str
messages := []providers.Message{} messages := []providers.Message{}
systemPrompt := cb.BuildSystemPrompt() systemPrompt := cb.BuildSystemPrompt()
filteredHistory, systemSummaries := extractSystemTaskSummariesFromHistoryWithPolicy(history, cb.summaryPolicy)
// Add Current Session info if provided // Add Current Session info if provided
if channel != "" && chatID != "" { if channel != "" && chatID != "" {
@@ -192,17 +174,11 @@ func (cb *ContextBuilder) BuildMessages(history []providers.Message, summary str
} }
logger.DebugCF("agent", "System prompt preview", logger.DebugCF("agent", "System prompt preview",
map[string]interface{}{ map[string]interface{}{
logger.FieldPreview: preview, "preview": preview,
}) })
if summary != "" { if summary != "" {
summary = sanitizeSummaryForPrompt(summary) systemPrompt += "\n\n## Summary of Previous Conversation\n\n" + summary
if summary != "" {
systemPrompt += "\n\n## Summary of Previous Conversation\n\n" + summary
}
}
if len(systemSummaries) > 0 {
systemPrompt += "\n\n## Recent System Task Summaries\n\n" + formatSystemTaskSummariesWithPolicy(systemSummaries, cb.summaryPolicy)
} }
messages = append(messages, providers.Message{ messages = append(messages, providers.Message{
@@ -210,249 +186,16 @@ func (cb *ContextBuilder) BuildMessages(history []providers.Message, summary str
Content: systemPrompt, Content: systemPrompt,
}) })
messages = append(messages, filteredHistory...) messages = append(messages, history...)
userMsg := providers.Message{ messages = append(messages, providers.Message{
Role: "user", Role: "user",
Content: currentMessage, Content: currentMessage,
} })
if len(media) > 0 {
userMsg.ContentParts = buildUserContentParts(currentMessage, media)
}
messages = append(messages, userMsg)
return messages return messages
} }
func sanitizeSummaryForPrompt(summary string) string {
text := strings.TrimSpace(summary)
if text == "" {
return ""
}
lines := strings.Split(text, "\n")
filtered := make([]string, 0, len(lines))
for _, line := range lines {
lower := strings.ToLower(strings.TrimSpace(line))
if strings.Contains(lower, "autonomy round ") ||
strings.Contains(lower, "auto-learn round ") ||
strings.Contains(lower, "autonomy mode started.") ||
strings.Contains(lower, "[system:") {
continue
}
filtered = append(filtered, line)
}
return strings.TrimSpace(strings.Join(filtered, "\n"))
}
func extractSystemTaskSummariesFromHistory(history []providers.Message) ([]providers.Message, []string) {
return extractSystemTaskSummariesFromHistoryWithPolicy(history, defaultSystemSummaryPolicy())
}
func extractSystemTaskSummariesFromHistoryWithPolicy(history []providers.Message, policy systemSummaryPolicy) ([]providers.Message, []string) {
if len(history) == 0 {
return nil, nil
}
filtered := make([]providers.Message, 0, len(history))
summaries := make([]string, 0, maxSystemTaskSummaries)
for _, msg := range history {
if strings.EqualFold(strings.TrimSpace(msg.Role), "assistant") && isSystemTaskSummaryMessageWithPolicy(msg.Content, policy) {
summaries = append(summaries, strings.TrimSpace(msg.Content))
continue
}
filtered = append(filtered, msg)
}
if len(summaries) > maxSystemTaskSummaries {
summaries = summaries[len(summaries)-maxSystemTaskSummaries:]
}
return filtered, summaries
}
func isSystemTaskSummaryMessage(content string) bool {
return isSystemTaskSummaryMessageWithPolicy(content, defaultSystemSummaryPolicy())
}
func isSystemTaskSummaryMessageWithPolicy(content string, policy systemSummaryPolicy) bool {
text := strings.TrimSpace(content)
if text == "" {
return false
}
lower := strings.ToLower(text)
marker := strings.ToLower(strings.TrimSpace(policy.marker))
completed := strings.ToLower(strings.TrimSpace(policy.completedPrefix))
outcome := strings.ToLower(strings.TrimSpace(policy.outcomePrefix))
return strings.HasPrefix(lower, marker) ||
(strings.Contains(lower, marker) && strings.Contains(lower, completed) && strings.Contains(lower, outcome))
}
func formatSystemTaskSummaries(summaries []string) string {
return formatSystemTaskSummariesWithPolicy(summaries, defaultSystemSummaryPolicy())
}
func formatSystemTaskSummariesWithPolicy(summaries []string, policy systemSummaryPolicy) string {
if len(summaries) == 0 {
return ""
}
completedItems := make([]string, 0, len(summaries))
changeItems := make([]string, 0, len(summaries))
outcomeItems := make([]string, 0, len(summaries))
for _, raw := range summaries {
entry := parseSystemTaskSummaryWithPolicy(raw, policy)
if entry.completed != "" {
completedItems = append(completedItems, entry.completed)
}
if entry.changes != "" {
changeItems = append(changeItems, entry.changes)
}
if entry.outcome != "" {
outcomeItems = append(outcomeItems, entry.outcome)
}
}
var sb strings.Builder
writeSection := func(title string, items []string) {
if len(items) == 0 {
return
}
if sb.Len() > 0 {
sb.WriteString("\n\n")
}
sb.WriteString("### " + title + "\n")
for i, item := range items {
sb.WriteString(fmt.Sprintf("- %d. %s\n", i+1, truncateString(item, maxSystemTaskSummariesChars)))
}
}
writeSection(policy.completedSectionTitle, completedItems)
writeSection(policy.changesSectionTitle, changeItems)
writeSection(policy.outcomesSectionTitle, outcomeItems)
if sb.Len() == 0 {
for i, s := range summaries {
if i > 0 {
sb.WriteString("\n")
}
sb.WriteString(fmt.Sprintf("- %d. %s\n", i+1, truncateString(strings.TrimSpace(s), maxSystemTaskSummariesChars)))
}
}
return strings.TrimSpace(sb.String())
}
type systemTaskSummaryEntry struct {
completed string
changes string
outcome string
}
func parseSystemTaskSummary(raw string) systemTaskSummaryEntry {
return parseSystemTaskSummaryWithPolicy(raw, defaultSystemSummaryPolicy())
}
func parseSystemTaskSummaryWithPolicy(raw string, policy systemSummaryPolicy) systemTaskSummaryEntry {
text := strings.TrimSpace(raw)
if text == "" {
return systemTaskSummaryEntry{}
}
lines := strings.Split(text, "\n")
entry := systemTaskSummaryEntry{}
for _, line := range lines {
trimmed := strings.TrimSpace(line)
lower := strings.ToLower(trimmed)
switch {
case strings.HasPrefix(lower, strings.ToLower(policy.completedPrefix)):
entry.completed = strings.TrimSpace(trimmed[len(policy.completedPrefix):])
case strings.HasPrefix(lower, strings.ToLower(policy.changesPrefix)):
entry.changes = strings.TrimSpace(trimmed[len(policy.changesPrefix):])
case strings.HasPrefix(lower, strings.ToLower(policy.outcomePrefix)):
entry.outcome = strings.TrimSpace(trimmed[len(policy.outcomePrefix):])
}
}
firstUsefulLine := ""
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if trimmed == "" || strings.HasPrefix(trimmed, "#") {
continue
}
if strings.HasPrefix(strings.ToLower(trimmed), strings.ToLower(policy.completedPrefix)) ||
strings.HasPrefix(strings.ToLower(trimmed), strings.ToLower(policy.changesPrefix)) ||
strings.HasPrefix(strings.ToLower(trimmed), strings.ToLower(policy.outcomePrefix)) {
continue
}
firstUsefulLine = trimmed
break
}
if firstUsefulLine == "" {
firstUsefulLine = truncateString(text, 160)
}
if entry.completed == "" {
entry.completed = firstUsefulLine
}
if entry.changes == "" {
entry.changes = "No explicit file-level changes noted."
}
if entry.outcome == "" {
entry.outcome = firstUsefulLine
}
return entry
}
type systemSummaryPolicy struct {
marker string
completedPrefix string
changesPrefix string
outcomePrefix string
completedSectionTitle string
changesSectionTitle string
outcomesSectionTitle string
}
func defaultSystemSummaryPolicy() systemSummaryPolicy {
return systemSummaryPolicy{
marker: "## System Task Summary",
completedPrefix: "- Completed:",
changesPrefix: "- Changes:",
outcomePrefix: "- Outcome:",
completedSectionTitle: "Completed Actions",
changesSectionTitle: "Change Summaries",
outcomesSectionTitle: "Execution Outcomes",
}
}
func systemSummaryPolicyFromConfig(cfg config.SystemSummaryPolicyConfig) systemSummaryPolicy {
p := defaultSystemSummaryPolicy()
p.marker = strings.TrimSpace(cfg.Marker)
p.completedPrefix = strings.TrimSpace(cfg.CompletedPrefix)
p.changesPrefix = strings.TrimSpace(cfg.ChangesPrefix)
p.outcomePrefix = strings.TrimSpace(cfg.OutcomePrefix)
p.completedSectionTitle = strings.TrimSpace(cfg.CompletedTitle)
p.changesSectionTitle = strings.TrimSpace(cfg.ChangesTitle)
p.outcomesSectionTitle = strings.TrimSpace(cfg.OutcomesTitle)
if strings.TrimSpace(p.marker) == "" {
p.marker = defaultSystemSummaryPolicy().marker
}
if strings.TrimSpace(p.completedPrefix) == "" {
p.completedPrefix = defaultSystemSummaryPolicy().completedPrefix
}
if strings.TrimSpace(p.changesPrefix) == "" {
p.changesPrefix = defaultSystemSummaryPolicy().changesPrefix
}
if strings.TrimSpace(p.outcomePrefix) == "" {
p.outcomePrefix = defaultSystemSummaryPolicy().outcomePrefix
}
if strings.TrimSpace(p.completedSectionTitle) == "" {
p.completedSectionTitle = defaultSystemSummaryPolicy().completedSectionTitle
}
if strings.TrimSpace(p.changesSectionTitle) == "" {
p.changesSectionTitle = defaultSystemSummaryPolicy().changesSectionTitle
}
if strings.TrimSpace(p.outcomesSectionTitle) == "" {
p.outcomesSectionTitle = defaultSystemSummaryPolicy().outcomesSectionTitle
}
return p
}
func (cb *ContextBuilder) AddToolResult(messages []providers.Message, toolCallID, toolName, result string) []providers.Message { func (cb *ContextBuilder) AddToolResult(messages []providers.Message, toolCallID, toolName, result string) []providers.Message {
messages = append(messages, providers.Message{ messages = append(messages, providers.Message{
Role: "tool", Role: "tool",
@@ -504,111 +247,3 @@ func (cb *ContextBuilder) GetSkillsInfo() map[string]interface{} {
"names": skillNames, "names": skillNames,
} }
} }
func buildUserContentParts(text string, media []string) []providers.MessageContentPart {
parts := make([]providers.MessageContentPart, 0, 1+len(media))
notes := make([]string, 0)
var totalInlineBytes int64
if strings.TrimSpace(text) != "" {
parts = append(parts, providers.MessageContentPart{
Type: "input_text",
Text: text,
})
}
for _, mediaPath := range media {
p := strings.TrimSpace(mediaPath)
if p == "" {
continue
}
if strings.HasPrefix(strings.ToLower(p), "http://") || strings.HasPrefix(strings.ToLower(p), "https://") {
notes = append(notes, fmt.Sprintf("Attachment kept as URL only and not inlined: %s", p))
continue
}
dataURL, mimeType, filename, sizeBytes, ok := buildFileDataURL(p)
if !ok {
notes = append(notes, fmt.Sprintf("Attachment could not be read and was skipped: %s", p))
continue
}
if sizeBytes > maxInlineMediaFileBytes {
notes = append(notes, fmt.Sprintf("Attachment too large and was not inlined (%s, %d bytes > %d bytes).", filename, sizeBytes, maxInlineMediaFileBytes))
continue
}
if totalInlineBytes+sizeBytes > maxInlineMediaTotalBytes {
notes = append(notes, fmt.Sprintf("Attachment skipped to keep request size bounded (%s).", filename))
continue
}
totalInlineBytes += sizeBytes
if strings.HasPrefix(mimeType, "image/") {
parts = append(parts, providers.MessageContentPart{
Type: "input_image",
ImageURL: dataURL,
MIMEType: mimeType,
Filename: filename,
})
continue
}
parts = append(parts, providers.MessageContentPart{
Type: "input_file",
FileData: dataURL,
MIMEType: mimeType,
Filename: filename,
})
}
if len(notes) > 0 {
parts = append(parts, providers.MessageContentPart{
Type: "input_text",
Text: "Attachment handling notes:\n- " + strings.Join(notes, "\n- "),
})
}
return parts
}
func buildFileDataURL(path string) (dataURL, mimeType, filename string, sizeBytes int64, ok bool) {
stat, err := os.Stat(path)
if err != nil || stat.IsDir() {
return "", "", "", 0, false
}
content, err := os.ReadFile(path)
if err != nil {
return "", "", "", 0, false
}
if len(content) == 0 {
return "", "", "", 0, false
}
filename = filepath.Base(path)
mimeType = detectMIMEType(path)
encoded := base64.StdEncoding.EncodeToString(content)
return fmt.Sprintf("data:%s;base64,%s", mimeType, encoded), mimeType, filename, stat.Size(), true
}
func detectMIMEType(path string) string {
ext := strings.ToLower(filepath.Ext(path))
mimeType := mime.TypeByExtension(ext)
if mimeType == "" {
switch ext {
case ".pdf":
mimeType = "application/pdf"
case ".doc":
mimeType = "application/msword"
case ".docx":
mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
case ".ppt":
mimeType = "application/vnd.ms-powerpoint"
case ".pptx":
mimeType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
case ".xls":
mimeType = "application/vnd.ms-excel"
case ".xlsx":
mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
}
}
if mimeType == "" {
mimeType = "application/octet-stream"
}
return mimeType
}

View File

@@ -77,9 +77,10 @@ func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers
editFileTool := tools.NewEditFileTool(workspace) editFileTool := tools.NewEditFileTool(workspace)
toolsRegistry.Register(editFileTool) toolsRegistry.Register(editFileTool)
// Register memory search tool // Register memory tools
memorySearchTool := tools.NewMemorySearchTool(workspace) memorySearchTool := tools.NewMemorySearchTool(workspace)
toolsRegistry.Register(memorySearchTool) toolsRegistry.Register(memorySearchTool)
toolsRegistry.Register(tools.NewMemoryWriteTool(workspace))
// Register parallel execution tool (leveraging Go's concurrency) // Register parallel execution tool (leveraging Go's concurrency)
toolsRegistry.Register(tools.NewParallelTool(toolsRegistry)) toolsRegistry.Register(tools.NewParallelTool(toolsRegistry))

111
pkg/tools/memory_write.go Normal file
View File

@@ -0,0 +1,111 @@
package tools
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
type MemoryWriteTool struct {
workspace string
}
func NewMemoryWriteTool(workspace string) *MemoryWriteTool {
return &MemoryWriteTool{workspace: workspace}
}
func (t *MemoryWriteTool) Name() string {
return "memory_write"
}
func (t *MemoryWriteTool) Description() string {
return "Write memory entries to long-term MEMORY.md or daily memory/YYYY-MM-DD.md. Use longterm for durable preferences/decisions, daily for raw logs."
}
func (t *MemoryWriteTool) Parameters() map[string]interface{} {
return map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"content": map[string]interface{}{
"type": "string",
"description": "Memory text to write",
},
"kind": map[string]interface{}{
"type": "string",
"description": "Target memory kind: longterm or daily",
"default": "daily",
},
"append": map[string]interface{}{
"type": "boolean",
"description": "Append mode (default true)",
"default": true,
},
},
"required": []string{"content"},
}
}
func (t *MemoryWriteTool) Execute(ctx context.Context, args map[string]interface{}) (string, error) {
content, _ := args["content"].(string)
content = strings.TrimSpace(content)
if content == "" {
return "", fmt.Errorf("content is required")
}
kind, _ := args["kind"].(string)
kind = strings.ToLower(strings.TrimSpace(kind))
if kind == "" {
kind = "daily"
}
appendMode := true
if v, ok := args["append"].(bool); ok {
appendMode = v
}
switch kind {
case "longterm", "memory", "permanent":
path := filepath.Join(t.workspace, "MEMORY.md")
if appendMode {
return t.appendWithTimestamp(path, content)
}
if err := os.WriteFile(path, []byte(content+"\n"), 0644); err != nil {
return "", err
}
return fmt.Sprintf("Wrote long-term memory: %s", path), nil
case "daily", "log", "today":
memDir := filepath.Join(t.workspace, "memory")
if err := os.MkdirAll(memDir, 0755); err != nil {
return "", err
}
path := filepath.Join(memDir, time.Now().Format("2006-01-02")+".md")
if appendMode {
return t.appendWithTimestamp(path, content)
}
if err := os.WriteFile(path, []byte(content+"\n"), 0644); err != nil {
return "", err
}
return fmt.Sprintf("Wrote daily memory: %s", path), nil
default:
return "", fmt.Errorf("invalid kind '%s', expected longterm or daily", kind)
}
}
func (t *MemoryWriteTool) appendWithTimestamp(path, content string) (string, error) {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return "", err
}
line := fmt.Sprintf("- [%s] %s\n", time.Now().Format("15:04"), content)
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return "", err
}
defer f.Close()
if _, err := f.WriteString(line); err != nil {
return "", err
}
return fmt.Sprintf("Appended memory to %s", path), nil
}