support chat mode

This commit is contained in:
Julian Freeman
2026-04-03 18:36:12 -04:00
parent ce4a42eec2
commit 41494ebad0
3 changed files with 691 additions and 3 deletions

View File

@@ -127,6 +127,50 @@ export interface HistoryItem {
modelName: string;
}
export interface Participant {
name: string;
gender: string;
language: Language;
tone: string;
}
export interface ChatMessage {
id: string;
sender: 'me' | 'partner';
original: string;
translated: string;
timestamp: string;
}
export interface ChatSession {
id: string;
title: string;
me: Participant;
partner: Participant;
messages: ChatMessage[];
lastActivity: string;
}
export const CONVERSATION_SYSTEM_PROMPT_TEMPLATE = `You are a professional real-time conversation translator.
Current Context:
- Role A (Me): {ME_NAME}, Gender: {ME_GENDER}, Language: {ME_LANG}.
- Role B (Partner): {PART_NAME}, Gender: {PART_GENDER}, Language: {PART_LANG}.
[Conversation History]
{HISTORY_BLOCK}
[Current Task]
Translate the incoming text from {FROM_LANG} to {TO_LANG}.
[Constraints]
1. Contextual Awareness: Use the [Conversation History] to resolve pronouns (it, that, etc.) and maintain consistency.
2. Tone & Register:
- If translating for 'Me', strictly use the tone: {MY_TONE}.
- If translating for 'Partner', auto-detect and preserve their original tone/emotion.
3. Natural Flow: Keep the translation concise and natural for a chat environment. Avoid "translationese".
4. Strictly avoid over-translation: Do not add extra information not present in the source text.
5. Output ONLY the translated text, no explanations.`;
export const useSettingsStore = defineStore('settings', () => {
const isDark = useLocalStorage('is-dark', false);
const apiBaseUrl = useLocalStorage('api-base-url', 'http://localhost:11434/v1');
@@ -162,6 +206,10 @@ export const useSettingsStore = defineStore('settings', () => {
const logs = ref<{ id: string; timestamp: string; type: 'request' | 'response' | 'error'; content: any; curl?: string }[]>([]);
const history = useLocalStorage<HistoryItem[]>('translation-history-v1', []);
// 对话模式状态
const chatSessions = useLocalStorage<ChatSession[]>('chat-sessions-v1', []);
const activeSessionId = useLocalStorage<string | null>('active-session-id-v1', null);
const addLog = (type: 'request' | 'response' | 'error', content: any, curl?: string) => {
const now = new Date();
const timestamp = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
@@ -202,6 +250,72 @@ export const useSettingsStore = defineStore('settings', () => {
}
};
// 对话模式方法
const createSession = (me: Participant, partner: Participant) => {
const id = crypto.randomUUID();
const now = new Date();
const timestamp = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
const newSession: ChatSession = {
id,
title: `${partner.name} 的对话`,
me,
partner,
messages: [],
lastActivity: timestamp
};
chatSessions.value.unshift(newSession);
activeSessionId.value = id;
return id;
};
const deleteSession = (id: string) => {
chatSessions.value = chatSessions.value.filter(s => s.id !== id);
if (activeSessionId.value === id) {
activeSessionId.value = chatSessions.value[0]?.id || null;
}
};
const addMessageToSession = (sessionId: string, sender: 'me' | 'partner', original: string, translated: string = '') => {
const session = chatSessions.value.find(s => s.id === sessionId);
if (session) {
const now = new Date();
const timestamp = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
const newMessage: ChatMessage = {
id: crypto.randomUUID(),
sender,
original,
translated,
timestamp
};
session.messages.push(newMessage);
session.lastActivity = timestamp;
// 将活跃会话移至顶部
const index = chatSessions.value.findIndex(s => s.id === sessionId);
if (index > 0) {
const [s] = chatSessions.value.splice(index, 1);
chatSessions.value.unshift(s);
}
return newMessage.id;
}
return null;
};
const updateChatMessage = (sessionId: string, messageId: string, updates: Partial<ChatMessage>) => {
const session = chatSessions.value.find(s => s.id === sessionId);
if (session) {
const message = session.messages.find(m => m.id === messageId);
if (message) {
Object.assign(message, updates);
}
}
};
return {
isDark,
apiBaseUrl,
@@ -220,8 +334,14 @@ export const useSettingsStore = defineStore('settings', () => {
toneRegister,
logs,
history,
chatSessions,
activeSessionId,
addLog,
addHistory,
updateHistoryItem
updateHistoryItem,
createSession,
deleteSession,
addMessageToSession,
updateChatMessage
};
});