()
| 44 | } |
| 45 | |
| 46 | func main() { |
| 47 | fmt.Println("=== A2A 协议示例 ===") |
| 48 | fmt.Println() |
| 49 | |
| 50 | // 1. 创建 Actor System |
| 51 | system := actor.NewSystem("a2a-demo") |
| 52 | defer system.Shutdown() |
| 53 | fmt.Println("✅ Actor System 已创建") |
| 54 | fmt.Println() |
| 55 | |
| 56 | // 2. 创建并注册 Agent Actor |
| 57 | agent := &SimpleAgent{} |
| 58 | system.Spawn(agent, "demo-agent") |
| 59 | fmt.Println("✅ Agent Actor 已注册: demo-agent") |
| 60 | fmt.Println() |
| 61 | |
| 62 | // 3. 创建 A2A Server |
| 63 | taskStore := a2a.NewInMemoryTaskStore() |
| 64 | a2aServer := a2a.NewServer(system, taskStore) |
| 65 | fmt.Println("✅ A2A Server 已创建") |
| 66 | fmt.Println() |
| 67 | |
| 68 | // 4. 获取 Agent Card |
| 69 | fmt.Println("--- 步骤 1: 获取 Agent Card ---") |
| 70 | card, err := a2aServer.GetAgentCard("demo-agent") |
| 71 | if err != nil { |
| 72 | log.Fatalf("获取 Agent Card 失败: %v", err) |
| 73 | } |
| 74 | |
| 75 | cardJSON, _ := json.MarshalIndent(card, "", " ") |
| 76 | fmt.Printf("Agent Card:\n%s\n\n", cardJSON) |
| 77 | |
| 78 | // 5. 发送消息 (message/send) |
| 79 | fmt.Println("--- 步骤 2: 发送消息 (message/send) ---") |
| 80 | params := a2a.MessageSendParams{ |
| 81 | Message: a2a.Message{ |
| 82 | MessageID: "msg-001", |
| 83 | Role: "user", |
| 84 | Parts: []a2a.Part{ |
| 85 | {Kind: "text", Text: "你好! 这是一个 A2A 协议测试消息。"}, |
| 86 | }, |
| 87 | Kind: "message", |
| 88 | }, |
| 89 | ContextID: "context-001", |
| 90 | } |
| 91 | paramsJSON, _ := json.Marshal(params) |
| 92 | |
| 93 | req := &a2a.JSONRPCRequest{ |
| 94 | JSONRPC: "2.0", |
| 95 | ID: "req-001", |
| 96 | Method: "message/send", |
| 97 | Params: json.RawMessage(paramsJSON), |
| 98 | } |
| 99 | |
| 100 | ctx := context.Background() |
| 101 | resp := a2aServer.HandleRequest(ctx, "demo-agent", req) |
| 102 | |
| 103 | if resp.Error != nil { |
nothing calls this directly
no test coverage detected