From d9f0af53c4b286b493defcad6e023045cb9328b5 Mon Sep 17 00:00:00 2001 From: Julian Freeman Date: Tue, 24 Feb 2026 16:36:08 -0400 Subject: [PATCH] support provide context --- src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 2 +- src-tauri/tauri.conf.json | 2 +- src/App.vue | 49 +++++++++++++++++++++++++++++---------- src/stores/settings.ts | 15 ++++++------ 5 files changed, 47 insertions(+), 23 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index f7457a6..1136a50 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -19,7 +19,7 @@ dependencies = [ [[package]] name = "ai-translate-client" -version = "0.2.1" +version = "0.3.0" dependencies = [ "futures-util", "reqwest 0.12.28", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index da86a31..f0af931 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ai-translate-client" -version = "0.2.1" +version = "0.3.0" description = "A client using AI models to translate" authors = ["Julian"] edition = "2021" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 236b399..b47b62f 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "ai-translate-client", - "version": "0.2.1", + "version": "0.3.0", "identifier": "top.volan.ai-translate-client", "build": { "beforeDevCommand": "pnpm dev", diff --git a/src/App.vue b/src/App.vue index 16e9bae..1f99946 100644 --- a/src/App.vue +++ b/src/App.vue @@ -204,19 +204,21 @@ const translate = async () => { isTranslating.value = true; targetText.value = ''; - const prompt = settings.systemPromptTemplate + const systemMessage = settings.systemPromptTemplate .replace(/{SOURCE_LANG}/g, sourceLang.value.englishName) .replace(/{SOURCE_CODE}/g, sourceLang.value.code) .replace(/{TARGET_LANG}/g, targetLang.value.englishName) .replace(/{TARGET_CODE}/g, targetLang.value.code) .replace(/{SPEAKER_IDENTITY}/g, settings.speakerIdentity) - .replace(/{TONE_REGISTER}/g, settings.toneRegister) - .replace(/{TEXT}/g, sourceText.value); + .replace(/{TONE_REGISTER}/g, settings.toneRegister); + + const userMessage = `[Context]\n${settings.context || 'None provided.'}\n\n[Text to Translate]\n${sourceText.value}`; const requestBody = { model: settings.modelName, messages: [ - { role: "user", content: prompt } + { role: "system", content: systemMessage }, + { role: "user", content: userMessage } ], stream: settings.enableStreaming }; @@ -333,13 +335,36 @@ const translate = async () => { - -
- +
+ + + +
diff --git a/src/stores/settings.ts b/src/stores/settings.ts index 8baafa7..50a6fa4 100644 --- a/src/stores/settings.ts +++ b/src/stores/settings.ts @@ -45,14 +45,11 @@ export const TONE_REGISTER_OPTIONS = [ export const DEFAULT_TEMPLATE = `You are a professional {SOURCE_LANG} ({SOURCE_CODE}) to {TARGET_LANG} ({TARGET_CODE}) translator. Your goal is to accurately convey the meaning and nuances of the original {SOURCE_LANG} text while adhering to {TARGET_LANG} grammar, vocabulary, and cultural sensitivities. -Translation Context & Style Constraints: -Speaker Identity: {SPEAKER_IDENTITY}. Ensure all grammatical agreements and self-referential terms in {TARGET_LANG} reflect this. -Tone & Register: {TONE_REGISTER}. - -Produce only the {TARGET_LANG} translation, without any additional explanations or commentary. Please translate the following {SOURCE_LANG} text into {TARGET_LANG}: - - -{TEXT}`; +[Constraints] +1. Speaker Identity: {SPEAKER_IDENTITY}. Ensure all grammatical agreements and self-referential terms in {TARGET_LANG} reflect this. +2. Tone & Register: {TONE_REGISTER}. +3. Produce ONLY the {TARGET_LANG} translation, without any additional explanations, notes, or commentary. +4. If [Context] is provided, use it strictly to disambiguate polysemous words. DO NOT add any factual information or descriptive details from the [Context] that are not present in the [Text to Translate].`; export interface ApiProfile { id: string; @@ -70,6 +67,7 @@ export const useSettingsStore = defineStore('settings', () => { const profiles = useLocalStorage('api-profiles', []); const enableStreaming = useLocalStorage('enable-streaming', true); const systemPromptTemplate = useLocalStorage('system-prompt-template', DEFAULT_TEMPLATE); + const context = useLocalStorage('translation-context', ''); // 存储整个对象以保持一致性 const sourceLang = useLocalStorage('source-lang-v2', LANGUAGES[0]); @@ -100,6 +98,7 @@ export const useSettingsStore = defineStore('settings', () => { profiles, enableStreaming, systemPromptTemplate, + context, sourceLang, targetLang, speakerIdentity,