This commit is contained in:
lpf
2026-02-13 14:35:23 +08:00
parent 308d69271a
commit 47a5f4b602
3 changed files with 76 additions and 22 deletions

View File

@@ -48,6 +48,9 @@ type AgentLoop struct {
func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers.LLMProvider, cs *cron.CronService) *AgentLoop { func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers.LLMProvider, cs *cron.CronService) *AgentLoop {
workspace := cfg.WorkspacePath() workspace := cfg.WorkspacePath()
os.MkdirAll(workspace, 0755) os.MkdirAll(workspace, 0755)
logger.InfoCF("agent", "Agent workspace initialized", map[string]interface{}{
"workspace": workspace,
})
toolsRegistry := tools.NewToolRegistry() toolsRegistry := tools.NewToolRegistry()
toolsRegistry.Register(&tools.ReadFileTool{}) toolsRegistry.Register(&tools.ReadFileTool{})

View File

@@ -11,6 +11,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"clawgo/pkg/logger"
) )
// MemoryStore manages persistent memory for the agent. // MemoryStore manages persistent memory for the agent.
@@ -29,7 +31,38 @@ func NewMemoryStore(workspace string) *MemoryStore {
memoryFile := filepath.Join(memoryDir, "MEMORY.md") memoryFile := filepath.Join(memoryDir, "MEMORY.md")
// Ensure memory directory exists // Ensure memory directory exists
os.MkdirAll(memoryDir, 0755) if err := os.MkdirAll(memoryDir, 0755); err != nil {
logger.ErrorCF("memory", "Failed to create memory directory", map[string]interface{}{
"memory_dir": memoryDir,
"error": err.Error(),
})
}
// Ensure MEMORY.md exists for first run (even without onboard).
if _, err := os.Stat(memoryFile); os.IsNotExist(err) {
initial := `# Long-term Memory
This file stores important information that should persist across sessions.
## User Information
(Important facts about user)
## Preferences
(User preferences learned over time)
## Important Notes
(Things to remember)
`
if writeErr := os.WriteFile(memoryFile, []byte(initial), 0644); writeErr != nil {
logger.ErrorCF("memory", "Failed to initialize MEMORY.md", map[string]interface{}{
"memory_file": memoryFile,
"error": writeErr.Error(),
})
}
}
return &MemoryStore{ return &MemoryStore{
workspace: workspace, workspace: workspace,
@@ -40,8 +73,8 @@ func NewMemoryStore(workspace string) *MemoryStore {
// getTodayFile returns the path to today's daily note file (memory/YYYYMM/YYYYMMDD.md). // getTodayFile returns the path to today's daily note file (memory/YYYYMM/YYYYMMDD.md).
func (ms *MemoryStore) getTodayFile() string { func (ms *MemoryStore) getTodayFile() string {
today := time.Now().Format("20060102") // YYYYMMDD today := time.Now().Format("20060102") // YYYYMMDD
monthDir := today[:6] // YYYYMM monthDir := today[:6] // YYYYMM
filePath := filepath.Join(ms.memoryDir, monthDir, today+".md") filePath := filepath.Join(ms.memoryDir, monthDir, today+".md")
return filePath return filePath
} }
@@ -57,6 +90,9 @@ func (ms *MemoryStore) ReadLongTerm() string {
// WriteLongTerm writes content to the long-term memory file (MEMORY.md). // WriteLongTerm writes content to the long-term memory file (MEMORY.md).
func (ms *MemoryStore) WriteLongTerm(content string) error { func (ms *MemoryStore) WriteLongTerm(content string) error {
if err := os.MkdirAll(ms.memoryDir, 0755); err != nil {
return err
}
return os.WriteFile(ms.memoryFile, []byte(content), 0644) return os.WriteFile(ms.memoryFile, []byte(content), 0644)
} }
@@ -77,7 +113,9 @@ func (ms *MemoryStore) AppendToday(content string) error {
// Ensure month directory exists // Ensure month directory exists
monthDir := filepath.Dir(todayFile) monthDir := filepath.Dir(todayFile)
os.MkdirAll(monthDir, 0755) if err := os.MkdirAll(monthDir, 0755); err != nil {
return err
}
var existingContent string var existingContent string
if data, err := os.ReadFile(todayFile); err == nil { if data, err := os.ReadFile(todayFile); err == nil {
@@ -104,8 +142,8 @@ func (ms *MemoryStore) GetRecentDailyNotes(days int) string {
for i := 0; i < days; i++ { for i := 0; i < days; i++ {
date := time.Now().AddDate(0, 0, -i) date := time.Now().AddDate(0, 0, -i)
dateStr := date.Format("20060102") // YYYYMMDD dateStr := date.Format("20060102") // YYYYMMDD
monthDir := dateStr[:6] // YYYYMM monthDir := dateStr[:6] // YYYYMM
filePath := filepath.Join(ms.memoryDir, monthDir, dateStr+".md") filePath := filepath.Join(ms.memoryDir, monthDir, dateStr+".md")
if data, err := os.ReadFile(filePath); err == nil { if data, err := os.ReadFile(filePath); err == nil {

View File

@@ -126,23 +126,36 @@ func (t *MemorySearchTool) Execute(ctx context.Context, args map[string]interfac
func (t *MemorySearchTool) getMemoryFiles() []string { func (t *MemorySearchTool) getMemoryFiles() []string {
var files []string var files []string
seen := map[string]struct{}{}
// Check main MEMORY.md addIfExists := func(path string) {
mainMem := filepath.Join(t.workspace, "MEMORY.md") if _, ok := seen[path]; ok {
if _, err := os.Stat(mainMem); err == nil { return
files = append(files, mainMem) }
} if _, err := os.Stat(path); err == nil {
files = append(files, path)
// Check memory/ directory seen[path] = struct{}{}
memDir := filepath.Join(t.workspace, "memory")
entries, err := os.ReadDir(memDir)
if err == nil {
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".md") {
files = append(files, filepath.Join(memDir, entry.Name()))
}
} }
} }
// Check long-term memory in both legacy and current locations.
addIfExists(filepath.Join(t.workspace, "MEMORY.md"))
addIfExists(filepath.Join(t.workspace, "memory", "MEMORY.md"))
// Check memory/ directory recursively (e.g., memory/YYYYMM/YYYYMMDD.md).
memDir := filepath.Join(t.workspace, "memory")
_ = filepath.Walk(memDir, func(path string, info os.FileInfo, err error) error {
if err != nil || info == nil || info.IsDir() {
return nil
}
if strings.HasSuffix(strings.ToLower(info.Name()), ".md") {
if _, ok := seen[path]; !ok {
files = append(files, path)
seen[path] = struct{}{}
}
}
return nil
})
return files return files
} }