mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-05-15 02:49:16 +08:00
fix: 修复增量导入后索引失效的问题 resolve #81
This commit is contained in:
@@ -233,7 +233,7 @@ export function generateIncrementalSessions(
|
||||
|
||||
if (isFirst) {
|
||||
// 第一条新消息:检查是否能并入最后一个已有会话
|
||||
if (lastSession && msg.ts - lastSession.end_ts <= gapThreshold * 1000) {
|
||||
if (lastSession && msg.ts - lastSession.end_ts <= gapThreshold) {
|
||||
// 并入已有会话
|
||||
currentSessionId = lastSession.id
|
||||
currentEndTs = lastSession.end_ts
|
||||
@@ -244,7 +244,7 @@ export function generateIncrementalSessions(
|
||||
} else {
|
||||
// 后续消息:检查与上一条的时间差
|
||||
const prevMsg = newMessages[i - 1]
|
||||
if (msg.ts - prevMsg.ts > gapThreshold * 1000) {
|
||||
if (msg.ts - prevMsg.ts > gapThreshold) {
|
||||
// 如果之前在追加已有会话,先更新它
|
||||
if (currentSessionId && appendCount > 0) {
|
||||
updateSessionEndAndCount.run(currentEndTs, appendCount, currentSessionId)
|
||||
|
||||
@@ -85,20 +85,29 @@ function handleMessageTimestampsChange(timestamps: number[]) {
|
||||
}
|
||||
|
||||
// 处理当前可见消息变化(用于联动高亮时间线)
|
||||
function handleVisibleMessageChange(messageId: number) {
|
||||
function handleVisibleMessageChange(payload: { id: number; timestamp: number }) {
|
||||
if (!sessionsCache.value.length) return
|
||||
|
||||
// 根据消息 ID 查找所属会话
|
||||
// 由于会话是按 firstMessageId 排序的,我们找最后一个 firstMessageId <= messageId 的会话
|
||||
// 优先按时间范围匹配所属会话,避免增量导入后 messageId 与时间顺序不一致导致联动错误。
|
||||
let targetSession: { id: number } | undefined
|
||||
for (const session of sessionsCache.value) {
|
||||
if (session.firstMessageId <= messageId) {
|
||||
if (payload.timestamp >= session.startTs && payload.timestamp <= session.endTs) {
|
||||
targetSession = session
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 兜底:若时间匹配失败,再退回旧的 messageId 规则,保证历史行为可用。
|
||||
if (!targetSession) {
|
||||
for (const session of sessionsCache.value) {
|
||||
if (session.firstMessageId <= payload.id) {
|
||||
targetSession = session
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (targetSession && targetSession.id !== activeSessionId.value) {
|
||||
activeSessionId.value = targetSession.id
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ const emit = defineEmits<{
|
||||
/** 消息数量变化 */
|
||||
(e: 'count-change', count: number): void
|
||||
/** 当前可见消息变化(用于联动时间线) */
|
||||
(e: 'visible-message-change', messageId: number): void
|
||||
(e: 'visible-message-change', payload: { id: number; timestamp: number }): void
|
||||
/** 跳转到指定消息(用于查看上下文) */
|
||||
(e: 'jump-to-message', messageId: number): void
|
||||
/** 滚动到底部(外部模式专用,用于加载下一个块) */
|
||||
@@ -429,7 +429,8 @@ function updateVisibleMessage() {
|
||||
const message = messages.value[middleItem.index]
|
||||
if (message && message.id !== lastEmittedMessageId) {
|
||||
lastEmittedMessageId = message.id
|
||||
emit('visible-message-change', message.id)
|
||||
// 同时上报消息 ID 与时间戳,供父组件优先按时间范围匹配会话。
|
||||
emit('visible-message-change', { id: message.id, timestamp: message.timestamp })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* 会话时间线组件
|
||||
* 使用 @tanstack/vue-virtual 实现虚拟滚动
|
||||
*/
|
||||
import { ref, computed, watch, onMounted, nextTick } from 'vue'
|
||||
import { ref, computed, watch, nextTick } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useVirtualizer } from '@tanstack/vue-virtual'
|
||||
import BatchSummaryModal from './BatchSummaryModal.vue'
|
||||
@@ -287,10 +287,6 @@ watch(
|
||||
},
|
||||
{ immediate: true }
|
||||
)
|
||||
|
||||
onMounted(() => {
|
||||
loadSessions()
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in New Issue
Block a user