HandleJSONRPC 处理 JSON-RPC 2.0 请求 POST /a2a/{agentId}
(c *gin.Context)
| 61 | // HandleJSONRPC 处理 JSON-RPC 2.0 请求 |
| 62 | // POST /a2a/{agentId} |
| 63 | func (h *Handler) HandleJSONRPC(c *gin.Context) { |
| 64 | ctx := c.Request.Context() |
| 65 | agentID := c.Param("agentId") |
| 66 | |
| 67 | if agentID == "" { |
| 68 | c.JSON(http.StatusBadRequest, gin.H{ |
| 69 | "success": false, |
| 70 | "error": gin.H{ |
| 71 | "code": "bad_request", |
| 72 | "message": "Missing agentId parameter", |
| 73 | }, |
| 74 | }) |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | // 解析 JSON-RPC 请求 |
| 79 | var req JSONRPCRequest |
| 80 | if err := c.ShouldBindJSON(&req); err != nil { |
| 81 | // 返回 JSON-RPC 错误响应 |
| 82 | c.JSON(http.StatusBadRequest, &JSONRPCResponse{ |
| 83 | JSONRPC: "2.0", |
| 84 | ID: nil, // 解析错误时 ID 可能为 nil |
| 85 | Error: &RPCError{ |
| 86 | Code: -32700, // Parse error |
| 87 | Message: "Invalid JSON was received", |
| 88 | Data: err.Error(), |
| 89 | }, |
| 90 | }) |
| 91 | return |
| 92 | } |
| 93 | |
| 94 | logging.Info(ctx, "a2a.jsonrpc_request", map[string]any{ |
| 95 | "agent_id": agentID, |
| 96 | "method": req.Method, |
| 97 | "id": req.ID, |
| 98 | }) |
| 99 | |
| 100 | // 验证 JSON-RPC 版本 |
| 101 | if req.JSONRPC != "2.0" { |
| 102 | c.JSON(http.StatusBadRequest, &JSONRPCResponse{ |
| 103 | JSONRPC: "2.0", |
| 104 | ID: req.ID, |
| 105 | Error: &RPCError{ |
| 106 | Code: -32600, // Invalid Request |
| 107 | Message: "Invalid JSON-RPC version, must be '2.0'", |
| 108 | }, |
| 109 | }) |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | // 处理请求 |
| 114 | resp := h.server.HandleRequest(ctx, agentID, &req) |
| 115 | |
| 116 | // 记录响应 |
| 117 | if resp.Error != nil { |
| 118 | logging.Warn(ctx, "a2a.jsonrpc_error", map[string]any{ |
| 119 | "agent_id": agentID, |
| 120 | "method": req.Method, |