diff --git a/src/components/ConversationView.vue b/src/components/ConversationView.vue index ad9e8f4..7f16e88 100644 --- a/src/components/ConversationView.vue +++ b/src/components/ConversationView.vue @@ -132,28 +132,36 @@ const generateCurl = (baseUrl: string, key: string, body: any) => { return `curl "${fullUrl}" \\\n -H "Content-Type: application/json" \\\n -H "Authorization: Bearer ${maskedKey}" \\\n -d '${JSON.stringify(body, null, 2)}'`; }; -const translateMessage = async (sender: 'me' | 'partner') => { +const translateMessage = async (sender: 'me' | 'partner', retranslateId?: string) => { if (!activeSession.value || isTranslating.value) return; - const text = sender === 'me' ? myInput.value.trim() : partnerInput.value.trim(); - if (!text) return; + let text = ''; + let messageId = ''; + + if (retranslateId) { + const msg = activeSession.value.messages.find(m => m.id === retranslateId); + if (!msg) return; + text = msg.original; + messageId = retranslateId; + settings.updateChatMessage(activeSession.value.id, messageId, { translated: '', evaluation: undefined }); + } else { + text = sender === 'me' ? myInput.value.trim() : partnerInput.value.trim(); + if (!text) return; + + const newId = settings.addMessageToSession(activeSession.value.id, sender, text, ''); + if (!newId) return; + messageId = newId; + if (sender === 'me') myInput.value = ''; else partnerInput.value = ''; + } isTranslating.value = true; - - // 1. Add message to session - const messageId = settings.addMessageToSession(activeSession.value.id, sender, text, ''); - if (!messageId) { - isTranslating.value = false; - return; - } currentStreamingMessageId.value = messageId; - - if (sender === 'me') myInput.value = ''; else partnerInput.value = ''; await scrollToBottom(); // 2. Prepare Context const historyLimit = 10; const sessionMessages = activeSession.value.messages; + // 重新翻译时,排除当前这条消息本身作为历史 const recentMessages = sessionMessages.filter(m => m.id !== messageId).slice(-historyLimit); const historyBlock = recentMessages.map(m => { @@ -187,7 +195,7 @@ const translateMessage = async (sender: 'me' | 'partner') => { stream: settings.enableStreaming }; - settings.addLog('request', { type: 'conversation', ...requestBody }, generateCurl(settings.apiBaseUrl, settings.apiKey, requestBody)); + settings.addLog('request', { type: retranslateId ? 'conversation-retranslate' : 'conversation', ...requestBody }, generateCurl(settings.apiBaseUrl, settings.apiKey, requestBody)); try { const response = await invoke('translate', { @@ -215,6 +223,11 @@ const translateMessage = async (sender: 'me' | 'partner') => { } }; +const deleteMessage = (messageId: string) => { + if (!activeSession.value) return; + settings.deleteChatMessage(activeSession.value.id, messageId); +}; + const evaluateMessage = async (messageId: string, force = false) => { if (!activeSession.value) return; const msg = activeSession.value.messages.find(m => m.id === messageId); @@ -540,7 +553,7 @@ onUnmounted(() => window.removeEventListener('click', handleGlobalClick));
+ +
diff --git a/src/stores/settings.ts b/src/stores/settings.ts index ad8ccaf..5fac1fd 100644 --- a/src/stores/settings.ts +++ b/src/stores/settings.ts @@ -386,6 +386,13 @@ export const useSettingsStore = defineStore('settings', () => { } }; + const deleteChatMessage = (sessionId: string, messageId: string) => { + const session = chatSessions.value.find(s => s.id === sessionId); + if (session) { + session.messages = session.messages.filter(m => m.id !== messageId); + } + }; + return { isDark, apiBaseUrl, @@ -415,6 +422,7 @@ export const useSettingsStore = defineStore('settings', () => { createSession, deleteSession, addMessageToSession, - updateChatMessage + updateChatMessage, + deleteChatMessage }; });