182 lines
6.8 KiB
Java
182 lines
6.8 KiB
Java
|
|
package com.wecom.robot.controller;
|
||
|
|
|
||
|
|
import com.wecom.robot.config.WecomConfig;
|
||
|
|
import com.wecom.robot.dto.ApiResponse;
|
||
|
|
import com.wecom.robot.dto.ChatCompletionRequest;
|
||
|
|
import com.wecom.robot.entity.Message;
|
||
|
|
import com.wecom.robot.service.AiService;
|
||
|
|
import com.wecom.robot.service.SessionManagerService;
|
||
|
|
import com.wecom.robot.util.WXBizMsgCrypt;
|
||
|
|
import lombok.RequiredArgsConstructor;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
|
||
|
|
import java.util.Base64;
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
@Slf4j
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/debug")
|
||
|
|
@RequiredArgsConstructor
|
||
|
|
public class DebugController {
|
||
|
|
|
||
|
|
private final WecomConfig wecomConfig;
|
||
|
|
private final AiService aiService;
|
||
|
|
private final SessionManagerService sessionManagerService;
|
||
|
|
|
||
|
|
@GetMapping("/config")
|
||
|
|
public ApiResponse<Map<String, Object>> getConfig() {
|
||
|
|
Map<String, Object> config = new HashMap<>();
|
||
|
|
config.put("corpId", wecomConfig.getCorpId());
|
||
|
|
config.put("token", wecomConfig.getToken());
|
||
|
|
config.put("encodingAesKey", wecomConfig.getEncodingAesKey());
|
||
|
|
config.put("encodingAesKeyLength", wecomConfig.getEncodingAesKey() != null ? wecomConfig.getEncodingAesKey().length() : 0);
|
||
|
|
|
||
|
|
try {
|
||
|
|
byte[] aesKey = Base64.getDecoder().decode(wecomConfig.getEncodingAesKey() + "=");
|
||
|
|
config.put("aesKeyLength", aesKey.length);
|
||
|
|
config.put("aesKeyHex", bytesToHex(aesKey));
|
||
|
|
} catch (Exception e) {
|
||
|
|
config.put("aesKeyError", e.getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
return ApiResponse.success(config);
|
||
|
|
}
|
||
|
|
|
||
|
|
@PostMapping("/decrypt")
|
||
|
|
public ApiResponse<Map<String, Object>> testDecrypt(
|
||
|
|
@RequestParam String msgSignature,
|
||
|
|
@RequestParam String timestamp,
|
||
|
|
@RequestParam String nonce,
|
||
|
|
@RequestBody String encryptedXml) {
|
||
|
|
|
||
|
|
Map<String, Object> result = new HashMap<>();
|
||
|
|
result.put("msgSignature", msgSignature);
|
||
|
|
result.put("timestamp", timestamp);
|
||
|
|
result.put("nonce", nonce);
|
||
|
|
|
||
|
|
try {
|
||
|
|
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(
|
||
|
|
wecomConfig.getToken(),
|
||
|
|
wecomConfig.getEncodingAesKey(),
|
||
|
|
wecomConfig.getCorpId()
|
||
|
|
);
|
||
|
|
|
||
|
|
String decrypted = wxcpt.DecryptMsg(msgSignature, timestamp, nonce, encryptedXml);
|
||
|
|
result.put("decrypted", decrypted);
|
||
|
|
result.put("success", true);
|
||
|
|
} catch (Exception e) {
|
||
|
|
result.put("error", e.getMessage());
|
||
|
|
result.put("success", false);
|
||
|
|
log.error("解密测试失败", e);
|
||
|
|
}
|
||
|
|
|
||
|
|
return ApiResponse.success(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
@GetMapping("/verify-url")
|
||
|
|
public ApiResponse<Map<String, Object>> testVerifyUrl(
|
||
|
|
@RequestParam String msgSignature,
|
||
|
|
@RequestParam String timestamp,
|
||
|
|
@RequestParam String nonce,
|
||
|
|
@RequestParam String echostr) {
|
||
|
|
|
||
|
|
Map<String, Object> result = new HashMap<>();
|
||
|
|
result.put("msgSignature", msgSignature);
|
||
|
|
result.put("timestamp", timestamp);
|
||
|
|
result.put("nonce", nonce);
|
||
|
|
result.put("echostr", echostr);
|
||
|
|
|
||
|
|
try {
|
||
|
|
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(
|
||
|
|
wecomConfig.getToken(),
|
||
|
|
wecomConfig.getEncodingAesKey(),
|
||
|
|
wecomConfig.getCorpId()
|
||
|
|
);
|
||
|
|
|
||
|
|
String decrypted = wxcpt.VerifyURL(msgSignature, timestamp, nonce, echostr);
|
||
|
|
result.put("decrypted", decrypted);
|
||
|
|
result.put("success", true);
|
||
|
|
} catch (Exception e) {
|
||
|
|
result.put("error", e.getMessage());
|
||
|
|
result.put("success", false);
|
||
|
|
log.error("URL验证测试失败", e);
|
||
|
|
}
|
||
|
|
|
||
|
|
return ApiResponse.success(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
@GetMapping("/ai/context")
|
||
|
|
public ApiResponse<Map<String, Object>> getLastAiContext() {
|
||
|
|
Map<String, Object> result = new HashMap<>();
|
||
|
|
|
||
|
|
result.put("systemPrompt", aiService.getSystemPrompt());
|
||
|
|
result.put("lastRequestJson", aiService.getLastRequestJson());
|
||
|
|
result.put("lastConfidence", aiService.getLastConfidence());
|
||
|
|
|
||
|
|
List<ChatCompletionRequest.Message> messages = aiService.getLastRequestMessages();
|
||
|
|
result.put("messageCount", messages.size());
|
||
|
|
|
||
|
|
List<Map<String, String>> messageList = new java.util.ArrayList<>();
|
||
|
|
for (ChatCompletionRequest.Message msg : messages) {
|
||
|
|
Map<String, String> msgMap = new HashMap<>();
|
||
|
|
msgMap.put("role", msg.getRole());
|
||
|
|
msgMap.put("content", msg.getContent());
|
||
|
|
messageList.add(msgMap);
|
||
|
|
}
|
||
|
|
result.put("messages", messageList);
|
||
|
|
|
||
|
|
return ApiResponse.success(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
@GetMapping("/ai/session/{sessionId}/context")
|
||
|
|
public ApiResponse<Map<String, Object>> getSessionAiContext(
|
||
|
|
@PathVariable String sessionId,
|
||
|
|
@RequestParam(required = false, defaultValue = "测试消息") String testMessage) {
|
||
|
|
|
||
|
|
Map<String, Object> result = new HashMap<>();
|
||
|
|
|
||
|
|
result.put("sessionId", sessionId);
|
||
|
|
result.put("systemPrompt", aiService.getSystemPrompt());
|
||
|
|
|
||
|
|
List<Message> history = sessionManagerService.getSessionMessages(sessionId);
|
||
|
|
result.put("historyCount", history.size());
|
||
|
|
|
||
|
|
List<Map<String, Object>> historyList = new java.util.ArrayList<>();
|
||
|
|
for (Message msg : history) {
|
||
|
|
Map<String, Object> msgMap = new HashMap<>();
|
||
|
|
msgMap.put("senderType", msg.getSenderType());
|
||
|
|
msgMap.put("senderId", msg.getSenderId());
|
||
|
|
msgMap.put("content", msg.getContent());
|
||
|
|
msgMap.put("msgType", msg.getMsgType());
|
||
|
|
msgMap.put("createdAt", msg.getCreatedAt() != null ? msg.getCreatedAt().toString() : null);
|
||
|
|
historyList.add(msgMap);
|
||
|
|
}
|
||
|
|
result.put("history", historyList);
|
||
|
|
|
||
|
|
List<ChatCompletionRequest.Message> contextMessages = aiService.buildMessagesForTest(history, testMessage);
|
||
|
|
result.put("contextMessageCount", contextMessages.size());
|
||
|
|
|
||
|
|
List<Map<String, String>> contextList = new java.util.ArrayList<>();
|
||
|
|
for (ChatCompletionRequest.Message msg : contextMessages) {
|
||
|
|
Map<String, String> msgMap = new HashMap<>();
|
||
|
|
msgMap.put("role", msg.getRole());
|
||
|
|
msgMap.put("content", msg.getContent());
|
||
|
|
contextList.add(msgMap);
|
||
|
|
}
|
||
|
|
result.put("contextMessages", contextList);
|
||
|
|
|
||
|
|
return ApiResponse.success(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
private String bytesToHex(byte[] bytes) {
|
||
|
|
StringBuilder sb = new StringBuilder();
|
||
|
|
for (byte b : bytes) {
|
||
|
|
sb.append(String.format("%02x", b));
|
||
|
|
}
|
||
|
|
return sb.toString();
|
||
|
|
}
|
||
|
|
}
|