|
|
|
|
@ -300,20 +300,40 @@ public class SyncWorker extends Worker {
|
|
|
|
|
com.google.gson.JsonArray messages = msgListResp.body().getAsJsonArray("data");
|
|
|
|
|
String finalAnswer = null;
|
|
|
|
|
|
|
|
|
|
// 遍历寻找 role=assistant 且 type=answer 的内容
|
|
|
|
|
for (com.google.gson.JsonElement el : messages) {
|
|
|
|
|
com.google.gson.JsonObject m = el.getAsJsonObject();
|
|
|
|
|
if ("assistant".equals(m.get("role").getAsString()) &&
|
|
|
|
|
"answer".equals(m.get("type").getAsString())) {
|
|
|
|
|
if ("assistant".equals(m.get("role").getAsString()) && "answer".equals(m.get("type").getAsString())) {
|
|
|
|
|
finalAnswer = m.get("content").getAsString();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (finalAnswer != null && !finalAnswer.isEmpty()) {
|
|
|
|
|
// 插入本地数据库并弹通知
|
|
|
|
|
saveChatMessageToLocal(finalAnswer);
|
|
|
|
|
AiNotificationHelper.sendAiNotification(getApplicationContext(), finalAnswer);
|
|
|
|
|
if (finalAnswer != null) {
|
|
|
|
|
// --- 核心手术点:解析 AI 的决策 JSON ---
|
|
|
|
|
try {
|
|
|
|
|
com.google.gson.JsonObject responseObj = com.google.gson.JsonParser.parseString(finalAnswer).getAsJsonObject();
|
|
|
|
|
|
|
|
|
|
// 只有当 action 为 reply 时才处理
|
|
|
|
|
if (responseObj.has("action") && "reply".equals(responseObj.get("action").getAsString())) {
|
|
|
|
|
String assistantMsg = responseObj.get("content").getAsString();
|
|
|
|
|
|
|
|
|
|
// 1. 存入本地数据库 (以便用户进入聊天界面能看到)
|
|
|
|
|
saveChatMessageToLocal(assistantMsg);
|
|
|
|
|
|
|
|
|
|
// 2. [关键] 调用通知辅助类,弹出手机顶部通知
|
|
|
|
|
// 使用 getApplicationContext() 确保后台上下文安全
|
|
|
|
|
net.micode.notes.tool.AiNotificationHelper.sendAiNotification(
|
|
|
|
|
getApplicationContext(),
|
|
|
|
|
assistantMsg
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
android.util.Log.d("MiChatSync", "AI 决定主动关怀,已发送通知栏提醒");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 兜底逻辑:如果 AI 返回的不是 JSON (可能是纯文本),默认入库并通知
|
|
|
|
|
saveChatMessageToLocal(finalAnswer);
|
|
|
|
|
net.micode.notes.tool.AiNotificationHelper.sendAiNotification(getApplicationContext(), finalAnswer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|