tmp
This commit is contained in:
+37
-3
@@ -6,6 +6,7 @@ Page({
|
||||
inputValue: '',
|
||||
isTyping: false,
|
||||
scrollToMessage: '',
|
||||
expandedIds: {},
|
||||
quickQuestions: [
|
||||
'今日待处理订单有哪些?',
|
||||
'最近客户反馈统计',
|
||||
@@ -17,6 +18,13 @@ Page({
|
||||
messageIdCounter: 0,
|
||||
socket: null,
|
||||
|
||||
// 生成消息 id:保证计数器始终为数字,避免出现 msg_NaN
|
||||
genId() {
|
||||
const n = Number(this.messageIdCounter)
|
||||
this.messageIdCounter = Number.isFinite(n) ? n + 1 : 1
|
||||
return `msg_${this.messageIdCounter}`
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.loadChatHistory()
|
||||
this.initSocket()
|
||||
@@ -66,13 +74,18 @@ Page({
|
||||
lastMessage.content += data.content || ''
|
||||
messages = this.data.messages
|
||||
}
|
||||
if (lastMessage.type === 'ai' && data.source == "subagent" && lastMessage.source == data.source) {
|
||||
lastMessage.content += '' + data.content || ''
|
||||
messages = this.data.messages
|
||||
}
|
||||
}
|
||||
|
||||
if (!messages) {
|
||||
const aiResponse = {
|
||||
id: `msg_${++this.messageIdCounter}`,
|
||||
id: this.genId(),
|
||||
_serverId: data.id,
|
||||
type: 'ai',
|
||||
source: data.source,
|
||||
contentType: 'text',
|
||||
content: data.content || '',
|
||||
time: this.formatTime(new Date())
|
||||
@@ -97,6 +110,19 @@ Page({
|
||||
try {
|
||||
const history = wx.getStorageSync('admin_ai_chat_history')
|
||||
if (history && history.length > 0) {
|
||||
// 清洗历史中的无效/重复 id(如 msg_NaN),并把计数器拨到最大 id 之后
|
||||
let max = 0
|
||||
const seen = new Set()
|
||||
history.forEach((m, i) => {
|
||||
const n = parseInt(String(m.id).slice(4), 10)
|
||||
if (Number.isFinite(n) && !seen.has(n)) {
|
||||
seen.add(n)
|
||||
if (n > max) max = n
|
||||
} else {
|
||||
m.id = `msg_h${Date.now()}_${i}`
|
||||
}
|
||||
})
|
||||
this.messageIdCounter = max
|
||||
this.setData({ messages: history })
|
||||
this.scrollToBottom()
|
||||
}
|
||||
@@ -132,7 +158,7 @@ Page({
|
||||
if (!content) return
|
||||
|
||||
const userMessage = {
|
||||
id: `msg_${++this.messageIdCounter}`,
|
||||
id: this.genId(),
|
||||
type: 'user',
|
||||
contentType: 'text',
|
||||
content: content,
|
||||
@@ -187,7 +213,7 @@ Page({
|
||||
|
||||
handleAIError(errorMessage) {
|
||||
const errorResponse = {
|
||||
id: `msg_${++this.messageIdCounter}`,
|
||||
id: this.genId(),
|
||||
type: 'ai',
|
||||
contentType: 'text',
|
||||
content: errorMessage,
|
||||
@@ -203,6 +229,14 @@ Page({
|
||||
this.scrollToBottom()
|
||||
},
|
||||
|
||||
// 展开/折叠 subagent 消息(默认折叠,key 不存在即为折叠态)
|
||||
toggleSubagent(e) {
|
||||
const id = e.currentTarget.dataset.id
|
||||
this.setData({
|
||||
[`expandedIds.${id}`]: !this.data.expandedIds[id]
|
||||
})
|
||||
},
|
||||
|
||||
scrollToBottom() {
|
||||
setTimeout(() => {
|
||||
const lastMessage = this.data.messages[this.data.messages.length - 1]
|
||||
|
||||
Reference in New Issue
Block a user