support provide context

This commit is contained in:
Julian Freeman
2026-02-24 16:36:08 -04:00
parent f183cccd5b
commit d9f0af53c4
5 changed files with 47 additions and 23 deletions

2
src-tauri/Cargo.lock generated
View File

@@ -19,7 +19,7 @@ dependencies = [
[[package]] [[package]]
name = "ai-translate-client" name = "ai-translate-client"
version = "0.2.1" version = "0.3.0"
dependencies = [ dependencies = [
"futures-util", "futures-util",
"reqwest 0.12.28", "reqwest 0.12.28",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "ai-translate-client" name = "ai-translate-client"
version = "0.2.1" version = "0.3.0"
description = "A client using AI models to translate" description = "A client using AI models to translate"
authors = ["Julian"] authors = ["Julian"]
edition = "2021" edition = "2021"

View File

@@ -1,7 +1,7 @@
{ {
"$schema": "https://schema.tauri.app/config/2", "$schema": "https://schema.tauri.app/config/2",
"productName": "ai-translate-client", "productName": "ai-translate-client",
"version": "0.2.1", "version": "0.3.0",
"identifier": "top.volan.ai-translate-client", "identifier": "top.volan.ai-translate-client",
"build": { "build": {
"beforeDevCommand": "pnpm dev", "beforeDevCommand": "pnpm dev",

View File

@@ -204,19 +204,21 @@ const translate = async () => {
isTranslating.value = true; isTranslating.value = true;
targetText.value = ''; targetText.value = '';
const prompt = settings.systemPromptTemplate const systemMessage = settings.systemPromptTemplate
.replace(/{SOURCE_LANG}/g, sourceLang.value.englishName) .replace(/{SOURCE_LANG}/g, sourceLang.value.englishName)
.replace(/{SOURCE_CODE}/g, sourceLang.value.code) .replace(/{SOURCE_CODE}/g, sourceLang.value.code)
.replace(/{TARGET_LANG}/g, targetLang.value.englishName) .replace(/{TARGET_LANG}/g, targetLang.value.englishName)
.replace(/{TARGET_CODE}/g, targetLang.value.code) .replace(/{TARGET_CODE}/g, targetLang.value.code)
.replace(/{SPEAKER_IDENTITY}/g, settings.speakerIdentity) .replace(/{SPEAKER_IDENTITY}/g, settings.speakerIdentity)
.replace(/{TONE_REGISTER}/g, settings.toneRegister) .replace(/{TONE_REGISTER}/g, settings.toneRegister);
.replace(/{TEXT}/g, sourceText.value);
const userMessage = `[Context]\n${settings.context || 'None provided.'}\n\n[Text to Translate]\n${sourceText.value}`;
const requestBody = { const requestBody = {
model: settings.modelName, model: settings.modelName,
messages: [ messages: [
{ role: "user", content: prompt } { role: "system", content: systemMessage },
{ role: "user", content: userMessage }
], ],
stream: settings.enableStreaming stream: settings.enableStreaming
}; };
@@ -333,13 +335,36 @@ const translate = async () => {
</button> </button>
</div> </div>
</div> </div>
<textarea <textarea
v-model="sourceText" v-model="sourceText"
placeholder="请输入待翻译内容..." placeholder="请输入待翻译内容..."
class="flex-1 p-6 resize-none outline-none text-lg leading-relaxed placeholder:text-slate-300 dark:placeholder:text-slate-600 bg-transparent min-h-0" class="flex-1 p-6 resize-none outline-none text-lg leading-relaxed placeholder:text-slate-300 dark:placeholder:text-slate-600 bg-transparent min-h-0"
></textarea> ></textarea>
<div class="p-4 border-t dark:border-slate-800 flex justify-end shrink-0">
<button <!-- Context Input Area -->
<div class="px-6 py-3 bg-slate-50/50 dark:bg-slate-800/20 border-t border-dashed dark:border-slate-800 group/context relative">
<div class="flex items-center justify-between mb-1.5">
<div class="flex items-center gap-1.5">
<FileText class="w-4 h-4 text-slate-400" />
<span class="text-[12px] font-bold text-slate-400 uppercase tracking-widest">情景背景 (可选)</span>
</div>
<button
v-if="settings.context"
@click="settings.context = ''"
class="p-1 hover:bg-slate-200 dark:hover:bg-slate-700 rounded opacity-0 group-hover/context:opacity-100 transition-opacity"
title="清空背景"
>
<Plus class="w-3 h-3 rotate-45 text-slate-400" />
</button>
</div>
<textarea
v-model="settings.context"
placeholder="在此输入背景信息,有助于提升翻译准确度..."
class="w-full bg-transparent border-none outline-none text-sm text-slate-500 dark:text-slate-400 resize-none h-14 leading-normal placeholder:italic placeholder:text-slate-300 dark:placeholder:text-slate-600"
></textarea>
</div>
<div class="p-4 border-t dark:border-slate-800 flex justify-end shrink-0"> <button
@click="translate" @click="translate"
:disabled="isTranslating || !sourceText.trim()" :disabled="isTranslating || !sourceText.trim()"
class="bg-blue-600 hover:bg-blue-700 disabled:bg-blue-300 dark:disabled:bg-blue-900/40 text-white px-6 py-2.5 rounded-lg font-medium transition-all flex items-center gap-2 shadow-sm" class="bg-blue-600 hover:bg-blue-700 disabled:bg-blue-300 dark:disabled:bg-blue-900/40 text-white px-6 py-2.5 rounded-lg font-medium transition-all flex items-center gap-2 shadow-sm"
@@ -636,7 +661,7 @@ const translate = async () => {
class="w-full px-4 py-3 border dark:border-slate-700 rounded-lg bg-transparent focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 outline-none transition-all font-mono text-xs leading-relaxed text-slate-900 dark:text-slate-100" class="w-full px-4 py-3 border dark:border-slate-700 rounded-lg bg-transparent focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 outline-none transition-all font-mono text-xs leading-relaxed text-slate-900 dark:text-slate-100"
></textarea> ></textarea>
<div class="flex flex-wrap gap-2 mt-2"> <div class="flex flex-wrap gap-2 mt-2">
<span v-for="tag in ['{SOURCE_LANG}', '{SOURCE_CODE}', '{TARGET_LANG}', '{TARGET_CODE}', '{SPEAKER_IDENTITY}', '{TONE_REGISTER}', '{TEXT}']" :key="tag" class="px-2 py-1 bg-slate-100 dark:bg-slate-800 text-[10px] font-mono rounded border dark:border-slate-700 text-slate-600 dark:text-slate-400">{{ tag }}</span> <span v-for="tag in ['{SOURCE_LANG}', '{SOURCE_CODE}', '{TARGET_LANG}', '{TARGET_CODE}', '{SPEAKER_IDENTITY}', '{TONE_REGISTER}']" :key="tag" class="px-2 py-1 bg-slate-100 dark:bg-slate-800 text-[10px] font-mono rounded border dark:border-slate-700 text-slate-600 dark:text-slate-400">{{ tag }}</span>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -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. 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: [Constraints]
Speaker Identity: {SPEAKER_IDENTITY}. Ensure all grammatical agreements and self-referential terms in {TARGET_LANG} reflect this. 1. Speaker Identity: {SPEAKER_IDENTITY}. Ensure all grammatical agreements and self-referential terms in {TARGET_LANG} reflect this.
Tone & Register: {TONE_REGISTER}. 2. Tone & Register: {TONE_REGISTER}.
3. Produce ONLY the {TARGET_LANG} translation, without any additional explanations, notes, or commentary.
Produce only the {TARGET_LANG} translation, without any additional explanations or commentary. Please translate the following {SOURCE_LANG} text into {TARGET_LANG}: 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].`;
{TEXT}`;
export interface ApiProfile { export interface ApiProfile {
id: string; id: string;
@@ -70,6 +67,7 @@ export const useSettingsStore = defineStore('settings', () => {
const profiles = useLocalStorage<ApiProfile[]>('api-profiles', []); const profiles = useLocalStorage<ApiProfile[]>('api-profiles', []);
const enableStreaming = useLocalStorage('enable-streaming', true); const enableStreaming = useLocalStorage('enable-streaming', true);
const systemPromptTemplate = useLocalStorage('system-prompt-template', DEFAULT_TEMPLATE); const systemPromptTemplate = useLocalStorage('system-prompt-template', DEFAULT_TEMPLATE);
const context = useLocalStorage('translation-context', '');
// 存储整个对象以保持一致性 // 存储整个对象以保持一致性
const sourceLang = useLocalStorage<Language>('source-lang-v2', LANGUAGES[0]); const sourceLang = useLocalStorage<Language>('source-lang-v2', LANGUAGES[0]);
@@ -100,6 +98,7 @@ export const useSettingsStore = defineStore('settings', () => {
profiles, profiles,
enableStreaming, enableStreaming,
systemPromptTemplate, systemPromptTemplate,
context,
sourceLang, sourceLang,
targetLang, targetLang,
speakerIdentity, speakerIdentity,