1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| package tech.javai.a2a;
import org.springframework.web.bind.annotation.*; import java.util.*;
@RestController @RequestMapping("/a2a") public class OrderAgentController {
@GetMapping("/.well-known/agent-card.json") public Map<String, Object> agentCard() { Map<String, Object> card = new LinkedHashMap<>(); card.put("name", "订单查询助手"); card.put("description", "查询电商订单状态、物流"); card.put("url", "http://localhost:8080/a2a"); card.put("version", "1.0.0"); card.put("defaultInputModes", List.of("text/plain")); card.put("defaultOutputModes", List.of("text/plain"));
card.put("capabilities", Map.of( "streaming", true, "pushNotifications", false, "stateTransitionHistory", true ));
card.put("skills", List.of( Map.of( "id", "query-order", "name", "订单查询", "description", "根据订单号查询订单状态和物流", "tags", List.of("订单", "电商", "物流"), "examples", List.of("查询订单 20260909001") ), Map.of( "id", "refund-request", "name", "退款申请", "description", "提交退款申请", "tags", List.of("退款", "售后") ) ));
return card; }
@PostMapping(value = "/", consumes = "application/json", produces = "application/json") public Map<String, Object> sendMessage(@RequestBody JsonRpcRequest request) { Map<String, Object> params = (Map<String, Object>) request.getParams(); Map<String, Object> message = (Map<String, Object>) params.get("message"); List<Map<String, Object>> parts = (List<Map<String, Object>>) message.get("parts"); String userText = (String) parts.get(0).get("text");
String responseText = processOrderQuery(userText);
Map<String, Object> result = new LinkedHashMap<>(); result.put("kind", "message"); result.put("role", "agent"); result.put("messageId", UUID.randomUUID().toString()); result.put("parts", List.of(Map.of("kind", "text", "text", responseText)));
Map<String, Object> response = new LinkedHashMap<>(); response.put("jsonrpc", "2.0"); response.put("id", request.getId()); response.put("result", result); return response; }
private String processOrderQuery(String userText) { if (userText.contains("查询") || userText.contains("订单")) { return "订单 20260909001 状态:shipped,快递公司:顺丰,运单号:SF1234567890"; } return "我是订单查询助手,请告诉我订单号。"; } }
class JsonRpcRequest { private String jsonrpc; private String method; private Object id; private Map<String, Object> params;
public String getJsonrpc() { return jsonrpc; } public void setJsonrpc(String jsonrpc) { this.jsonrpc = jsonrpc; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public Object getId() { return id; } public void setId(Object id) { this.id = id; } public Map<String, Object> getParams() { return params; } public void setParams(Map<String, Object> params) { this.params = params; } }
|