-H "Content-Type: application/json" \ -d '{ "template_id": "assistant", "input": "请帮我总结一下 README", "metadata": {"user_id": "alice"}, "middlewares": ["filesystem", "agent_memory"] }' 示例响应: { "agent_id": "agt-...", "text": "...", "status": "ok" }
()
| 36 | // } |
| 37 | |
| 38 | func main() { |
| 39 | apiKey := os.Getenv("ANTHROPIC_API_KEY") |
| 40 | if apiKey == "" { |
| 41 | log.Println("[WARN] ANTHROPIC_API_KEY not set, server will still start but chat requests will fail until a key is provided.") |
| 42 | } |
| 43 | |
| 44 | ctx := context.Background() |
| 45 | |
| 46 | // 1. 创建工具注册表并注册内置工具 |
| 47 | toolRegistry := tools.NewRegistry() |
| 48 | builtin.RegisterAll(toolRegistry) |
| 49 | |
| 50 | // 2. 创建 Sandbox 工厂 |
| 51 | sandboxFactory := sandbox.NewFactory() |
| 52 | |
| 53 | // 3. 创建 Provider 工厂 |
| 54 | providerFactory := &provider.AnthropicFactory{} |
| 55 | |
| 56 | // 4. 创建 Store |
| 57 | storePath := ".aster-server" |
| 58 | jsonStore, err := store.NewJSONStore(storePath) |
| 59 | if err != nil { |
| 60 | log.Fatalf("Failed to create store: %v", err) |
| 61 | } |
| 62 | |
| 63 | // 5. 创建模板注册表, 注册一个简单模板 |
| 64 | templateRegistry := agent.NewTemplateRegistry() |
| 65 | templateRegistry.Register(&types.AgentTemplateDefinition{ |
| 66 | ID: "assistant", |
| 67 | Model: "claude-sonnet-4-5", |
| 68 | SystemPrompt: "You are a helpful assistant with file and memory access. " + |
| 69 | "Use filesystem and memory tools when appropriate.", |
| 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. 创建 Server |
| 82 | srv := server.New(deps) |
| 83 | |
| 84 | // 7. 注册 HTTP 路由 |
| 85 | mux := http.NewServeMux() |
| 86 | mux.Handle("/v1/agents/chat", srv.ChatHandler()) |
| 87 | mux.Handle("/v1/agents/chat/stream", srv.ChatStreamHandler()) |
| 88 | |
| 89 | addr := ":8080" |
| 90 | s := &http.Server{ |
| 91 | Addr: addr, |
| 92 | Handler: mux, |
| 93 | ReadHeaderTimeout: 10 * time.Second, |
| 94 | } |
| 95 |
nothing calls this directly
no test coverage detected