Merge branch 'dev' into hugo-shipping-notice
This commit is contained in:
commit
ca32191c77
30 files changed
+1181
-79
No files matched your search
@@ -10,6 +10,7 @@ import { config } from '@/config';
|
||||
import { useMessage } from '@/shared/composables/useMessage';
|
||||
import { useIsMobile } from '@/shared/composables/useIsMobile';
|
||||
import { useTheme } from '@/shared/composables/useTheme';
|
||||
import { listChatMessages } from '../api/chat.api';
|
||||
import { stripReasoningPrelude } from '../utils/chatText';
|
||||
import type { ChatDeps } from './chat.deps';
|
||||
import { useChat } from './useChat';
|
||||
@@ -38,6 +39,39 @@ import {
|
||||
useChatPageVoice,
|
||||
useChatPageToolRun,
|
||||
} from '../page';
|
||||
import type { ChatMessage } from '../types/chat.types';
|
||||
|
||||
const getLatestAssistantMessage = (items: ChatMessage[]) => {
|
||||
for (let i = items.length - 1; i >= 0; i -= 1) {
|
||||
if (items[i]?.role === 'assistant') {
|
||||
return items[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const hasMeaningfulAssistantMessage = (message: ChatMessage | null | undefined) => {
|
||||
if (!message) return false;
|
||||
if (String(message.content || '').trim()) return true;
|
||||
return Boolean(message.type && message.type !== 'text');
|
||||
};
|
||||
|
||||
const shouldApplyServerConversationSnapshot = (localMessages: ChatMessage[], serverMessages: ChatMessage[]) => {
|
||||
if (serverMessages.length < localMessages.length) return false;
|
||||
|
||||
const localAssistant = getLatestAssistantMessage(localMessages);
|
||||
if (!hasMeaningfulAssistantMessage(localAssistant)) return true;
|
||||
|
||||
const serverAssistant = getLatestAssistantMessage(serverMessages);
|
||||
if (!hasMeaningfulAssistantMessage(serverAssistant)) return false;
|
||||
|
||||
const localContent = String(localAssistant?.content || '').trim();
|
||||
const serverContent = String(serverAssistant?.content || '').trim();
|
||||
if (!localContent) return true;
|
||||
if (!serverContent) return false;
|
||||
if (serverContent.length < localContent.length) return false;
|
||||
return serverContent.includes(localContent);
|
||||
};
|
||||
|
||||
export function useChatPage() {
|
||||
const { t, locale } = useI18n();
|
||||
@@ -291,6 +325,28 @@ export function useChatPage() {
|
||||
getChatContext: shipping.isShippingNoticeAgent ? shipping.getCurrentShippingChatContext : undefined,
|
||||
onAssistantMessageFinished: shipping.finalizeShippingAssistantMessage,
|
||||
refreshHistoryAfterSend,
|
||||
refreshConversationAfterSend: async () => {
|
||||
const currentConversationId = conversationId.value;
|
||||
const currentAgent = currentAgentId.value;
|
||||
if (!currentConversationId || !currentAgent) return;
|
||||
|
||||
// [STEP 1] 发送刚结束时先保留当前内存快照,避免服务端尚未完整落库的旧消息把新回答覆盖掉。
|
||||
const localSnapshot = [...messages.value];
|
||||
const loaded = await listChatMessages(currentConversationId, currentAgent);
|
||||
const serverMessages = Array.isArray(loaded) ? loaded : [];
|
||||
|
||||
// [STEP 2] 若用户已切到其他 Agent/会话,丢弃这个迟到的刷新结果,避免把右侧问答区又切回旧会话。
|
||||
if (conversationId.value !== currentConversationId || currentAgentId.value !== currentAgent) {
|
||||
return;
|
||||
}
|
||||
|
||||
// [STEP 3] 仅当服务端快照不比本地旧时才覆盖,修复“答案刚出来就消失”和后续发送被锁死的问题。
|
||||
if (!shouldApplyServerConversationSnapshot(localSnapshot, serverMessages)) {
|
||||
return;
|
||||
}
|
||||
|
||||
loadConversationState(currentConversationId, serverMessages);
|
||||
},
|
||||
tryHandleShippingAttachmentAutoFlow: shipping.tryHandleShippingAttachmentAutoFlow,
|
||||
tryHandleLeaveSend: leave.tryHandleLeaveSend,
|
||||
tryHandleShippingSend: shipping.tryHandleShippingSend,
|
||||
|
||||
Reference in new issue
Block a user