日志添加
This commit is contained in:
parent
1a7f4d16a1
commit
e3be73121a
@ -21,6 +21,6 @@ public class AgentFoundationApplication {
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void onApplicationReady() {
|
||||
log.info("agent-foundation startup success port={} health=/v1/healthz wecomCallback=/v1/integrations/wecom/callback agentMessages=/v1/agent/messages", serverPort);
|
||||
log.info("agent-foundation 启动成功 port={} health=/v1/healthz wecomCallback=/v1/integrations/wecom/callback agentMessages=/v1/agent/messages", serverPort);
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ public class AgentOrchestratorService {
|
||||
|
||||
public String handleMessage(String agentId, String fromUser, String userText) {
|
||||
AgentIntent intent = intentRecognitionService.recognize(agentId, userText);
|
||||
log.info("agent orchestrator route fromUser={} intent={} group={}", fromUser, intent.code(), intent.mcpGroup());
|
||||
log.info("智能体编排路由 fromUser={} intent={} group={}", fromUser, intent.code(), intent.mcpGroup());
|
||||
if (intent != AgentIntent.GENERAL_CHAT) {
|
||||
return toWecomText(operationReportService.handle(intent, AgentRequestContext.of(agentId, fromUser, userText)));
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ public class IntentRecognitionService {
|
||||
public AgentIntent recognize(String agentId, String userText) {
|
||||
AgentIntent keywordIntent = fallback(userText);
|
||||
if (keywordIntent != AgentIntent.GENERAL_CHAT) {
|
||||
log.info("agent intent recognized by keywords intent={} textPreview={}", keywordIntent.code(), preview(userText));
|
||||
log.info("智能体意图识别命中关键词 intent={} textPreview={}", keywordIntent.code(), preview(userText));
|
||||
return keywordIntent;
|
||||
}
|
||||
|
||||
@ -51,10 +51,10 @@ public class IntentRecognitionService {
|
||||
);
|
||||
ChatResponse response = chatService.chat(new AgentContext(agentId), request);
|
||||
AgentIntent intent = AgentIntent.fromCode(clean(response.content()));
|
||||
log.info("agent intent recognized by llm intent={} textPreview={}", intent.code(), preview(userText));
|
||||
log.info("智能体意图识别命中大模型 intent={} textPreview={}", intent.code(), preview(userText));
|
||||
return intent;
|
||||
} catch (Exception ex) {
|
||||
log.warn("agent intent llm recognition failed fallback=general_chat err={}", ex.getMessage());
|
||||
log.warn("智能体意图大模型识别失败,回退 general_chat err={}", ex.getMessage());
|
||||
return AgentIntent.GENERAL_CHAT;
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,19 +18,19 @@ public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(UnauthorizedException.class)
|
||||
public ResponseEntity<Map<String, Object>> unauthorized(UnauthorizedException ex) {
|
||||
log.warn("request unauthorized: {}", ex.getMessage());
|
||||
log.warn("请求未授权: {}", ex.getMessage());
|
||||
return build(HttpStatus.UNAUTHORIZED, ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
public ResponseEntity<Map<String, Object>> badRequest(IllegalArgumentException ex) {
|
||||
log.warn("request bad: {}", ex.getMessage(), ex);
|
||||
log.warn("请求参数错误: {}", ex.getMessage(), ex);
|
||||
return build(HttpStatus.BAD_REQUEST, ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<Map<String, Object>> generic(Exception ex) {
|
||||
log.error("request failed", ex);
|
||||
log.error("请求处理失败", ex);
|
||||
return build(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
|
||||
}
|
||||
|
||||
|
||||
@ -23,7 +23,7 @@ public class CrmDatabaseService {
|
||||
long start = System.currentTimeMillis();
|
||||
Integer result = jdbcTemplate.queryForObject("SELECT 1", Integer.class);
|
||||
long costMs = System.currentTimeMillis() - start;
|
||||
log.info("crm db ping success costMs={}", costMs);
|
||||
log.info("CRM 数据库连接探测成功 costMs={}", costMs);
|
||||
return Map.of("ok", result != null && result == 1, "costMs", costMs);
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ public class CrmDatabaseService {
|
||||
int safeLimit = clamp(limit, 1, 200);
|
||||
String safeSchema = blankToDefault(schema, "dbo");
|
||||
String safeKeyword = keyword == null ? "" : keyword.trim();
|
||||
log.info("crm db listTables schema={} keyword={} limit={}", safeSchema, safeKeyword, safeLimit);
|
||||
log.info("CRM 数据库查询表列表 schema={} keyword={} limit={}", safeSchema, safeKeyword, safeLimit);
|
||||
return jdbcTemplate.queryForList("""
|
||||
SELECT TOP %d TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
@ -45,7 +45,7 @@ public class CrmDatabaseService {
|
||||
String safeSchema = blankToDefault(schema, "dbo");
|
||||
validateIdentifier("schema", safeSchema);
|
||||
validateIdentifier("table", table);
|
||||
log.info("crm db listColumns schema={} table={}", safeSchema, table);
|
||||
log.info("CRM 数据库查询字段 schema={} table={}", safeSchema, table);
|
||||
return jdbcTemplate.queryForList("""
|
||||
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE, ORDINAL_POSITION
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
@ -59,7 +59,7 @@ public class CrmDatabaseService {
|
||||
validateIdentifier("schema", safeSchema);
|
||||
validateIdentifier("table", table);
|
||||
int safeLimit = clamp(limit, 1, 20);
|
||||
log.info("crm db sampleRows schema={} table={} limit={}", safeSchema, table, safeLimit);
|
||||
log.info("CRM 数据库采样行 schema={} table={} limit={}", safeSchema, table, safeLimit);
|
||||
return jdbcTemplate.queryForList("SELECT TOP " + safeLimit + " * FROM " + quoted(safeSchema) + "." + quoted(table));
|
||||
}
|
||||
|
||||
|
||||
@ -42,13 +42,13 @@ public class WecomCallbackController {
|
||||
@RequestParam("nonce") String nonce,
|
||||
@RequestParam("echostr") String echostr
|
||||
) {
|
||||
log.info("wecom callback verify received safeMode={} timestamp={} nonce={}", wecomCryptoService.isSafeModeEnabled(), timestamp, nonce);
|
||||
log.info("企业微信回调校验接收 safeMode={} timestamp={} nonce={}", wecomCryptoService.isSafeModeEnabled(), timestamp, nonce);
|
||||
String payload = wecomCryptoService.isSafeModeEnabled() ? echostr : null;
|
||||
if (!wecomCryptoService.validateSignature(signature, timestamp, nonce, payload)) {
|
||||
log.warn("wecom callback verify signature invalid timestamp={} nonce={}", timestamp, nonce);
|
||||
log.warn("企业微信回调校验签名无效 timestamp={} nonce={}", timestamp, nonce);
|
||||
throw new IllegalArgumentException("Invalid WeCom signature");
|
||||
}
|
||||
log.info("wecom callback verify success safeMode={}", wecomCryptoService.isSafeModeEnabled());
|
||||
log.info("企业微信回调校验成功 safeMode={}", wecomCryptoService.isSafeModeEnabled());
|
||||
return wecomCryptoService.isSafeModeEnabled() ? wecomCryptoService.decrypt(echostr) : echostr;
|
||||
}
|
||||
|
||||
@ -59,34 +59,34 @@ public class WecomCallbackController {
|
||||
@RequestParam("nonce") String nonce,
|
||||
@RequestBody String body
|
||||
) throws Exception {
|
||||
log.info("wecom callback message received safeMode={} timestamp={} nonce={} bodyLength={}",
|
||||
log.info("企业微信回调消息接收 safeMode={} timestamp={} nonce={} bodyLength={}",
|
||||
wecomCryptoService.isSafeModeEnabled(), timestamp, nonce, body == null ? 0 : body.length());
|
||||
WecomIncomingMessage msg = parseIncomingMessage(signature, timestamp, nonce, body);
|
||||
log.info("wecom callback parsed fromUser={} toUser={} msgType={} contentPreview={}",
|
||||
log.info("企业微信回调解析成功 fromUser={} toUser={} msgType={} contentPreview={}",
|
||||
msg == null ? null : msg.getFromUserName(),
|
||||
msg == null ? null : msg.getToUserName(),
|
||||
msg == null ? null : msg.getMsgType(),
|
||||
preview(msg == null ? null : msg.getContent()));
|
||||
log.info("wecom callback current user FromUserName={}", msg == null ? null : msg.getFromUserName());
|
||||
log.info("企业微信回调当前用户 FromUserName={}", msg == null ? null : msg.getFromUserName());
|
||||
if (msg == null || msg.getMsgType() == null || !"text".equalsIgnoreCase(msg.getMsgType())) {
|
||||
log.info("wecom callback ignored non-text message");
|
||||
log.info("企业微信回调忽略非文本消息");
|
||||
return "success";
|
||||
}
|
||||
|
||||
String userText = msg.getContent() == null ? "" : msg.getContent().trim();
|
||||
if (userText.isBlank()) {
|
||||
log.info("wecom callback ignored blank text");
|
||||
log.info("企业微信回调忽略空文本");
|
||||
return "success";
|
||||
}
|
||||
|
||||
log.info("wecom callback invoking agent orchestrator agentId={} fromUser={}", properties.getWecom().getCrmAgentId(), msg.getFromUserName());
|
||||
log.info("企业微信回调调用智能体编排 agentId={} fromUser={}", properties.getWecom().getCrmAgentId(), msg.getFromUserName());
|
||||
String reply = agentOrchestratorService.handleMessage(properties.getWecom().getCrmAgentId(), msg.getFromUserName(), userText);
|
||||
reply = (reply == null || reply.isBlank())
|
||||
? "Request received, but I could not generate advice right now."
|
||||
? "已收到消息,但暂时无法生成回复。"
|
||||
: reply;
|
||||
log.info("wecom callback agent response replyPreview={}", preview(reply));
|
||||
log.info("企业微信回调智能体响应 replyPreview={}", preview(reply));
|
||||
wecomClient.sendText(msg.getFromUserName(), reply);
|
||||
log.info("wecom callback reply sent toUser={}", msg.getFromUserName());
|
||||
log.info("企业微信回调回复已发送 toUser={}", msg.getFromUserName());
|
||||
return "success";
|
||||
}
|
||||
|
||||
@ -94,20 +94,20 @@ public class WecomCallbackController {
|
||||
if (wecomCryptoService.isSafeModeEnabled()) {
|
||||
WecomEncryptedEnvelope envelope = xmlMapper.readValue(body, WecomEncryptedEnvelope.class);
|
||||
if (envelope == null || envelope.getEncrypt() == null || envelope.getEncrypt().isBlank()) {
|
||||
log.warn("wecom callback encrypted envelope missing encrypt field");
|
||||
log.warn("企业微信回调加密信封缺少 encrypt 字段");
|
||||
throw new IllegalArgumentException("Missing WeCom encrypted payload");
|
||||
}
|
||||
if (!wecomCryptoService.validateSignature(signature, timestamp, nonce, envelope.getEncrypt())) {
|
||||
log.warn("wecom callback encrypted signature invalid timestamp={} nonce={}", timestamp, nonce);
|
||||
log.warn("企业微信回调加密签名无效 timestamp={} nonce={}", timestamp, nonce);
|
||||
throw new IllegalArgumentException("Invalid WeCom signature");
|
||||
}
|
||||
String plainXml = wecomCryptoService.decrypt(envelope.getEncrypt());
|
||||
log.info("wecom callback decrypt success plainXmlLength={}", plainXml.length());
|
||||
log.info("企业微信回调解密成功 plainXmlLength={}", plainXml.length());
|
||||
return xmlMapper.readValue(plainXml, WecomIncomingMessage.class);
|
||||
}
|
||||
|
||||
if (!wecomCryptoService.validateSignature(signature, timestamp, nonce, null)) {
|
||||
log.warn("wecom callback plain signature invalid timestamp={} nonce={}", timestamp, nonce);
|
||||
log.warn("企业微信回调明文签名无效 timestamp={} nonce={}", timestamp, nonce);
|
||||
throw new IllegalArgumentException("Invalid WeCom signature");
|
||||
}
|
||||
return xmlMapper.readValue(body, WecomIncomingMessage.class);
|
||||
|
||||
@ -24,7 +24,7 @@ public class WecomClient {
|
||||
}
|
||||
|
||||
public void sendText(String toUser, String content) {
|
||||
log.info("wecom sendText start toUser={} agentId={} contentPreview={}", toUser, properties.getWecom().getAgentId(), preview(content));
|
||||
log.info("企业微信发送文本开始 toUser={} agentId={} contentPreview={}", toUser, properties.getWecom().getAgentId(), preview(content));
|
||||
String token = getAccessToken();
|
||||
WecomSendMessageRequest req = new WecomSendMessageRequest();
|
||||
req.setToUser(toUser);
|
||||
@ -42,15 +42,15 @@ public class WecomClient {
|
||||
.block();
|
||||
|
||||
if (response == null || !response.success()) {
|
||||
log.error("wecom sendText failed toUser={} err={}", toUser, response == null ? "null" : response.errmsg());
|
||||
log.error("企业微信发送文本失败 toUser={} err={}", toUser, response == null ? "null" : response.errmsg());
|
||||
throw new IllegalStateException("WeCom send failed: " + (response == null ? "null" : response.errmsg()));
|
||||
}
|
||||
log.info("wecom sendText success toUser={}", toUser);
|
||||
log.info("企业微信发送文本成功 toUser={}", toUser);
|
||||
}
|
||||
|
||||
private String getAccessToken() {
|
||||
if (cachedToken != null && Instant.now().isBefore(tokenExpireAt)) {
|
||||
log.info("wecom gettoken use cached token expireAt={}", tokenExpireAt);
|
||||
log.info("企业微信获取 token 使用缓存 tokenExpireAt={}", tokenExpireAt);
|
||||
return cachedToken;
|
||||
}
|
||||
|
||||
@ -64,14 +64,14 @@ public class WecomClient {
|
||||
.block();
|
||||
|
||||
if (response == null || !response.success() || response.accessToken() == null) {
|
||||
log.error("wecom gettoken failed err={}", response == null ? "null" : response.errmsg());
|
||||
log.error("企业微信获取 token 失败 err={}", response == null ? "null" : response.errmsg());
|
||||
throw new IllegalStateException("WeCom gettoken failed: " + (response == null ? "null" : response.errmsg()));
|
||||
}
|
||||
|
||||
cachedToken = response.accessToken();
|
||||
int expires = response.expiresIn() == null ? 7200 : response.expiresIn();
|
||||
tokenExpireAt = Instant.now().plusSeconds(Math.max(300, expires - 120));
|
||||
log.info("wecom gettoken success expireAt={}", tokenExpireAt);
|
||||
log.info("企业微信获取 token 成功 expireAt={}", tokenExpireAt);
|
||||
return cachedToken;
|
||||
}
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@ public class ChatService {
|
||||
public ChatResponse chat(AgentContext context, ChatRequest request) {
|
||||
String traceId = UUID.randomUUID().toString();
|
||||
String model = modelRouter.routeModel(context.agentId(), request.model());
|
||||
log.info("chat start traceId={} agentId={} model={} messageCount={}", traceId, context.agentId(), model, request.messages() == null ? 0 : request.messages().size());
|
||||
log.info("聊天开始 traceId={} agentId={} model={} messageCount={}", traceId, context.agentId(), model, request.messages() == null ? 0 : request.messages().size());
|
||||
DeepseekChatResponse resp = deepseekClient.chat(new DeepseekChatRequest(
|
||||
model,
|
||||
request.messages().stream().map(m -> new DeepseekChatRequest.Message(m.role(), m.content())).toList(),
|
||||
@ -29,7 +29,7 @@ public class ChatService {
|
||||
if (resp != null && resp.choices() != null && !resp.choices().isEmpty() && resp.choices().get(0).message() != null) content = resp.choices().get(0).message().content();
|
||||
ChatResponse.Usage usage = null;
|
||||
if (resp != null && resp.usage() != null) usage = new ChatResponse.Usage(resp.usage().prompt_tokens(), resp.usage().completion_tokens(), resp.usage().total_tokens());
|
||||
log.info("chat success traceId={} totalTokens={} replyPreview={}", traceId, usage == null ? null : usage.totalTokens(), preview(content));
|
||||
log.info("聊天成功 traceId={} totalTokens={} replyPreview={}", traceId, usage == null ? null : usage.totalTokens(), preview(content));
|
||||
return new ChatResponse(traceId, context.agentId(), model, content, usage, Instant.now());
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user