psnci6hgk 1 month ago
commit e6066ea334

@ -182,6 +182,33 @@ function formatDateTime(ms) {
} catch (e) { return '' }
}
// Date null
function findNextRelaxStart(oc) {
try {
if (!oc || !Array.isArray(oc.schedule)) return null
const now = new Date()
const todayStr = now.getFullYear() + '-' + String(now.getMonth()+1).padStart(2,'0') + '-' + String(now.getDate()).padStart(2,'0')
//
const candidates = []
for (const s of oc.schedule) {
const dateStr = s.date || todayStr
const timeStr = (s.time || '00:00').slice(0,5)
const start = new Date(`${dateStr}T${timeStr}`)
if (start.getTime() > Date.now()) {
const eventText = (s.event || '').toLowerCase()
if (/休息|放松|休闲|看电影|娱乐|咖啡|喝茶/ig.test(eventText)) {
candidates.push(start)
}
}
}
if (!candidates.length) return null
candidates.sort((a,b) => a.getTime() - b.getTime())
return candidates[0]
} catch (e) {
return null
}
}
// activeContact
watch(() => [props.activeContact && props.activeContact.schedule ? props.activeContact.schedule.length : 0, currentScheduleEnd.value], () => {
// schedule currentScheduleEnd
@ -265,12 +292,23 @@ const performAIReply = async (batchId, userText) => {
if (Array.isArray(reply)) reply = reply.join('\n')
reply = String(reply).trim()
if (!reply) reply = '(刚在忙,你说啥?)'
const hadPlaceholder = messages.value.some(m => m.batchId === batchId && m.isPlaceholder)
messages.value = messages.value.map(m =>
m.batchId === batchId && m.isPlaceholder
? { ...m, text: reply, timestamp: Date.now(), isPlaceholder: false }
: m
)
if (!hadPlaceholder) {
// AI
const replyMsg = {
senderId: props.activeContact?.id || 'ai',
text: reply,
timestamp: Date.now(),
batchId: batchId,
isPlaceholder: false
}
messages.value = [...messages.value, replyMsg]
}
emit('update:modelValue', messages.value)
} catch (err) {
console.error('AI回复错误:', err)
@ -282,11 +320,22 @@ const performAIReply = async (batchId, userText) => {
console.error('服务器无响应:', err.request)
errMsg = '服务失联,请检查网络'
}
const hadPlaceholder = messages.value.some(m => m.batchId === batchId && m.isPlaceholder)
messages.value = messages.value.map(m =>
m.batchId === batchId && m.isPlaceholder
? { ...m, text: errMsg, timestamp: Date.now(), isPlaceholder: false }
: m
)
if (!hadPlaceholder) {
const errMsgObj = {
senderId: props.activeContact?.id || 'ai',
text: errMsg,
timestamp: Date.now(),
batchId: batchId,
isPlaceholder: false
}
messages.value = [...messages.value, errMsgObj]
}
emit('update:modelValue', messages.value)
}
}
@ -331,18 +380,7 @@ const handleSend = async () => {
const API_KEY = import.meta.env.VITE_ZHIPU_API_KEY
// AI - batchId
const aiPlaceholderMsg = {
senderId: props.activeContact?.id || 'ai',
text: '对方正在输入中...',
timestamp: Date.now() + 1,
batchId: messageBatchId,
isPlaceholder: true //
}
nextMessages = [...messages.value, aiPlaceholderMsg]
messages.value = nextMessages
emit('update:modelValue', nextMessages)
// ... AI
const isBusy = statusClass.value === 'busy' || statusText.value.startsWith('正在')
@ -351,6 +389,29 @@ const handleSend = async () => {
pendingPlaceholderTs = messageBatchId
if (isBusy) {
// AI
const nextRelax = findNextRelaxStart(props.activeContact || {})
let timeHint = ''
if (nextRelax) {
timeHint = formatDateTime(nextRelax.getTime())
} else if (currentScheduleEnd.value) {
timeHint = formatDateTime(currentScheduleEnd.value)
} else {
timeHint = ''
}
const busyText = `我正在${currentActivity.value || '忙碌'},稍后回复哦` + (timeHint ? `(下次空闲:${timeHint}` : '')
const busyNoticeMsg = {
senderId: props.activeContact?.id || 'oc',
text: busyText,
timestamp: Date.now() + 1,
system: true
}
//
nextMessages = [...messages.value, busyNoticeMsg]
messages.value = nextMessages
emit('update:modelValue', nextMessages)
// 5
let waitMs = 0
if (currentScheduleEnd.value) {

Loading…
Cancel
Save