使用场景: - 记住用户偏好和设置 - 跟踪任务进度 - 维护会话状态
()
| 27 | // - 维护会话状态 |
| 28 | |
| 29 | func main() { |
| 30 | // 检查 API Key |
| 31 | apiKey := os.Getenv("ANTHROPIC_API_KEY") |
| 32 | if apiKey == "" { |
| 33 | log.Fatal("ANTHROPIC_API_KEY environment variable is required") |
| 34 | } |
| 35 | |
| 36 | ctx := context.Background() |
| 37 | |
| 38 | // 1. 工具注册表 |
| 39 | toolRegistry := tools.NewRegistry() |
| 40 | builtin.RegisterAll(toolRegistry) |
| 41 | |
| 42 | // 2. Sandbox 工厂 |
| 43 | sandboxFactory := sandbox.NewFactory() |
| 44 | |
| 45 | // 3. Provider 工厂 |
| 46 | providerFactory := &provider.AnthropicFactory{} |
| 47 | |
| 48 | // 4. Store |
| 49 | jsonStore, err := store.NewJSONStore(".aster-working-memory") |
| 50 | if err != nil { |
| 51 | log.Fatalf("create store failed: %v", err) |
| 52 | } |
| 53 | |
| 54 | // 5. 模板注册表 |
| 55 | templateRegistry := agent.NewTemplateRegistry() |
| 56 | templateRegistry.Register(&types.AgentTemplateDefinition{ |
| 57 | ID: "task-assistant", |
| 58 | SystemPrompt: `You are a helpful task assistant with Working Memory. |
| 59 | |
| 60 | Your capabilities: |
| 61 | - Remember user information and preferences |
| 62 | - Track task progress across multiple conversations |
| 63 | - Maintain context between sessions |
| 64 | |
| 65 | When you learn something important about the user or their tasks, use the update_working_memory tool to store it. |
| 66 | |
| 67 | Remember: Working Memory is automatically loaded at the start of each conversation, so you always have access to it.`, |
| 68 | Model: "claude-sonnet-4-5", |
| 69 | // 基础工具,working memory 工具由中间件自动注入 |
| 70 | Tools: []any{"Read", "Write", "Bash"}, |
| 71 | }) |
| 72 | |
| 73 | deps := &agent.Dependencies{ |
| 74 | Store: jsonStore, |
| 75 | SandboxFactory: sandboxFactory, |
| 76 | ToolRegistry: toolRegistry, |
| 77 | ProviderFactory: providerFactory, |
| 78 | TemplateRegistry: templateRegistry, |
| 79 | } |
| 80 | |
| 81 | // 6. 工作目录 |
| 82 | workDir := "./workspace" |
| 83 | if err := os.MkdirAll(workDir, 0o755); err != nil { |
| 84 | log.Fatalf("create workspace dir failed: %v", err) |
| 85 | } |
| 86 |
nothing calls this directly
no test coverage detected