diff --git a/pkg/agent/context.go b/pkg/agent/context.go index 21d79f3..71c6b3d 100644 --- a/pkg/agent/context.go +++ b/pkg/agent/context.go @@ -1,36 +1,25 @@ package agent import ( - "encoding/base64" "fmt" - "mime" "os" "path/filepath" "runtime" "strings" "time" - "clawgo/pkg/config" "clawgo/pkg/logger" "clawgo/pkg/providers" "clawgo/pkg/skills" ) type ContextBuilder struct { - workspace string - skillsLoader *skills.SkillsLoader - memory *MemoryStore - toolsSummary func() []string // Function to get tool summaries dynamically - summaryPolicy systemSummaryPolicy + workspace string + skillsLoader *skills.SkillsLoader + memory *MemoryStore + toolsSummary func() []string // Function to get tool summaries dynamically } -const ( - maxInlineMediaFileBytes int64 = 5 * 1024 * 1024 - maxInlineMediaTotalBytes int64 = 12 * 1024 * 1024 - maxSystemTaskSummaries = 4 - maxSystemTaskSummariesChars = 2400 -) - func getGlobalConfigDir() string { home, err := os.UserHomeDir() if err != nil { @@ -39,19 +28,18 @@ func getGlobalConfigDir() string { return filepath.Join(home, ".clawgo") } -func NewContextBuilder(workspace string, memCfg config.MemoryConfig, summaryCfg config.SystemSummaryPolicyConfig, toolsSummaryFunc func() []string) *ContextBuilder { - // Built-in skills: the current project's skills directory. - // Use the skills/ directory under the current working directory. +func NewContextBuilder(workspace string, toolsSummaryFunc func() []string) *ContextBuilder { + // builtin skills: 当前项目的 skills 目录 + // 使用当前工作目录下的 skills/ 目录 wd, _ := os.Getwd() builtinSkillsDir := filepath.Join(wd, "skills") globalSkillsDir := filepath.Join(getGlobalConfigDir(), "skills") return &ContextBuilder{ - workspace: workspace, - skillsLoader: skills.NewSkillsLoader(workspace, globalSkillsDir, builtinSkillsDir), - memory: NewMemoryStore(workspace, memCfg), - toolsSummary: toolsSummaryFunc, - summaryPolicy: systemSummaryPolicyFromConfig(summaryCfg), + workspace: workspace, + skillsLoader: skills.NewSkillsLoader(workspace, globalSkillsDir, builtinSkillsDir), + memory: NewMemoryStore(workspace), + toolsSummary: toolsSummaryFunc, } } @@ -75,20 +63,15 @@ You are clawgo, a helpful AI assistant. ## Workspace Your workspace is at: %s -- Memory: %s/memory/MEMORY.md -- Daily Notes: %s/memory/YYYYMM/YYYYMMDD.md +- Long-term Memory: %s/MEMORY.md +- Daily Notes: %s/memory/YYYY-MM-DD.md - Skills: %s/skills/{skill-name}/SKILL.md %s -## Important Rules - -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. - -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) +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.`, + now, runtime, workspacePath, workspacePath, workspacePath, workspacePath, toolsSection, workspacePath, workspacePath) } func (cb *ContextBuilder) buildToolsSection() string { @@ -167,7 +150,6 @@ func (cb *ContextBuilder) BuildMessages(history []providers.Message, summary str messages := []providers.Message{} systemPrompt := cb.BuildSystemPrompt() - filteredHistory, systemSummaries := extractSystemTaskSummariesFromHistoryWithPolicy(history, cb.summaryPolicy) // Add Current Session info if provided if channel != "" && chatID != "" { @@ -192,17 +174,11 @@ func (cb *ContextBuilder) BuildMessages(history []providers.Message, summary str } logger.DebugCF("agent", "System prompt preview", map[string]interface{}{ - logger.FieldPreview: preview, + "preview": preview, }) if summary != "" { - summary = sanitizeSummaryForPrompt(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) + systemPrompt += "\n\n## Summary of Previous Conversation\n\n" + summary } messages = append(messages, providers.Message{ @@ -210,249 +186,16 @@ func (cb *ContextBuilder) BuildMessages(history []providers.Message, summary str Content: systemPrompt, }) - messages = append(messages, filteredHistory...) + messages = append(messages, history...) - userMsg := providers.Message{ + messages = append(messages, providers.Message{ Role: "user", Content: currentMessage, - } - if len(media) > 0 { - userMsg.ContentParts = buildUserContentParts(currentMessage, media) - } - messages = append(messages, userMsg) + }) 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 { messages = append(messages, providers.Message{ Role: "tool", @@ -504,111 +247,3 @@ func (cb *ContextBuilder) GetSkillsInfo() map[string]interface{} { "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 -} diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index 49eb3c2..4cd2c6c 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -77,9 +77,10 @@ func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers editFileTool := tools.NewEditFileTool(workspace) toolsRegistry.Register(editFileTool) - // Register memory search tool + // Register memory tools memorySearchTool := tools.NewMemorySearchTool(workspace) toolsRegistry.Register(memorySearchTool) + toolsRegistry.Register(tools.NewMemoryWriteTool(workspace)) // Register parallel execution tool (leveraging Go's concurrency) toolsRegistry.Register(tools.NewParallelTool(toolsRegistry)) diff --git a/pkg/tools/memory_write.go b/pkg/tools/memory_write.go new file mode 100644 index 0000000..0a48fe5 --- /dev/null +++ b/pkg/tools/memory_write.go @@ -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 +}