This commit is contained in:
张成
2026-03-17 15:10:27 +08:00
parent f071215ad5
commit 39a5b49213
5 changed files with 70 additions and 52 deletions

View File

@@ -135,12 +135,16 @@ class ChatManager {
*/
async get_chat_detail(sn_code, mqttClient, params = {}) {
const { platform = 'boss', ...rest } = params;
console.log(`[聊天管理] 开始获取设备 ${sn_code} 的沟通详情`);
const friendId = rest.friendId != null ? Number(rest.friendId) : NaN;
if (!Number.isFinite(friendId) || friendId <= 0) {
throw new Error('缺少必要参数friendId需为有效正数');
}
console.log(`[聊天管理] 开始获取设备 ${sn_code} 的沟通详情`, { friendId });
const response = await mqttClient.publishAndWait(sn_code, {
platform,
action: 'get_chat_detail',
data: rest
data: { ...rest, friendId }
});
const ok = response && (response.code === 200 || response.code === 0);
@@ -285,6 +289,16 @@ class ChatManager {
const jobInfo = detail.job || {};
// 用 messages 判定整条对话最后一条是否来自 HR只有最后一条是 HR 才需要回复
const last_in_messages = messages[messages.length - 1];
const last_from_uid = last_in_messages && last_in_messages.from ? this._normalizeUid(last_in_messages.from.uid) : null;
const hr_uid_str = this._normalizeUid(hr_uid);
const is_last_from_hr = hr_uid_str && last_from_uid === hr_uid_str && !this._isSystemMessage(last_in_messages);
if (!is_last_from_hr) {
this._saveReplyIntentLog(options, '', jobInfo, '', '', false, '最后一条消息不是HR发的', null);
return { replied: false, reason: '最后一条消息不是HR发的' };
}
const hrList = this._filterHrReplyableMessages(messages, geek_uid);
if (hrList.length === 0) {
this._saveReplyIntentLog(options, '', jobInfo, '', '', false, '无HR可回复消息已过滤系统与己方', null);
@@ -292,11 +306,6 @@ class ChatManager {
}
const last = hrList[hrList.length - 1];
if (!last.from || last.from.uid !== hr_uid) {
this._saveReplyIntentLog(options, '', jobInfo, '', '', false, '最后一条可回复消息不是HR', null);
return { replied: false, reason: '最后一条可回复消息不是HR' };
}
const body = last.body || {};
const hr_message_text =
(typeof body.text === 'string' && body.text) ||

View File

@@ -337,12 +337,20 @@ class MqttDispatcher {
? firstMsg.body.text
: null;
// 兼容 uid 为数字或 { low, high } 两种格式
const toUidStr = (uid) => {
if (uid == null) return null;
if (typeof uid === 'number' && !Number.isNaN(uid)) return String(uid);
if (typeof uid === 'object' && typeof uid.low === 'number') return String(uid.low);
return null;
};
const normalized = {
sn_code,
type: payload.type || null,
version: payload.version || null,
from_uid: fromUidObj && typeof fromUidObj.low === 'number' ? String(fromUidObj.low) : null,
to_uid: toUidObj && typeof toUidObj.low === 'number' ? String(toUidObj.low) : null,
from_uid: toUidStr(fromUidObj),
to_uid: toUidStr(toUidObj),
text,
raw: payload
};
@@ -406,12 +414,16 @@ class MqttDispatcher {
// 调用现有 AI 自动回复流程(基于 get_chat_detail + getReplyContentFromDetail
try {
if (normalized.from_uid && this.mqttClient) {
const friendIdNum = normalized.from_uid != null ? Number(normalized.from_uid) : 0;
const hasValidFriendId = friendIdNum > 0 && Number.isFinite(friendIdNum);
if (hasValidFriendId && this.mqttClient) {
const result = await chatManager.auto_reply_with_ai(sn_code, this.mqttClient, {
friendId: Number(normalized.from_uid),
friendId: friendIdNum,
platform: 'boss'
});
console.log('[MQTT Boss 消息] AI 自动回复结果:', result);
} else if (!hasValidFriendId && normalized.from_uid != null) {
console.warn('[MQTT Boss 消息] 跳过 AI 回复friendId 无效或为 0', { from_uid: normalized.from_uid, friendIdNum });
}
} catch (e) {
console.warn('[MQTT Boss 消息] AI 自动回复失败:', e.message);