feat: 支持多语言提示词本地化和界面优化

- 添加 prompt_locale 参数支持简体中文、繁体中文和英文提示词本地化
- 移除内置 agents 配置以简化系统架构
- 更新 ContextBuilder 使用动态提示词模板而非硬编码内容
- 在 AgentLoop、Web 接口和 AgentService 中传递 locale 参数
- 添加输出语言指令确保用户界面内容按指定语言生成
- 扩展前端 LanguageSwitcher 组件支持三种语言选项
- 优化 Header 和侧边栏组件的响应式布局和文本截断处理
- 更新测试用例验证不同语言环境下的提示词正确性
This commit is contained in:
2026-06-10 16:11:05 +08:00
parent 9cc3334ea7
commit fc9fd93c36
51 changed files with 7493 additions and 619 deletions

View File

@ -51,7 +51,7 @@ import type {
UiMcpServerDescriptor,
WsEvent,
} from '@/types';
import { getCurrentAppLocale, pickAppText } from '@/lib/i18n/core';
import { getCurrentAppLocale, pickAppText, type AppLocale } from '@/lib/i18n/core';
const API_URL = process.env.NEXT_PUBLIC_API_URL?.trim();
const WS_URL = process.env.NEXT_PUBLIC_WS_URL?.trim();
@ -62,6 +62,15 @@ const REQUEST_TIMEOUT_MS = 8000;
const OUTLOOK_REQUEST_TIMEOUT_MS = 45000;
const SKILL_LEARNING_REQUEST_TIMEOUT_MS = 120000;
export type PromptLocale = 'zh-Hans' | 'zh-Hant' | 'en';
export function promptLocaleForAppLocale(locale: AppLocale): PromptLocale {
if (locale === 'zh-Hant') {
return 'zh-Hant';
}
return locale === 'en-US' ? 'en' : 'zh-Hans';
}
function isBrowser(): boolean {
return typeof window !== 'undefined';
}
@ -271,6 +280,7 @@ export async function sendMessage(
replyToScheduledRunId?: string;
scheduledReplyIntent?: 'revise_once' | 'update_future' | 'continue_task';
thinkingEnabled?: boolean;
promptLocale?: PromptLocale;
}
): Promise<{
response?: string;
@ -281,7 +291,11 @@ export async function sendMessage(
task_status?: string | null;
evidence_status?: string | null;
}> {
const body: Record<string, unknown> = { message, session_id: sessionId };
const body: Record<string, unknown> = {
message,
session_id: sessionId,
prompt_locale: options?.promptLocale || promptLocaleForAppLocale(getCurrentAppLocale()),
};
if (attachments && attachments.length > 0) {
body.attachments = attachments;
}
@ -356,7 +370,11 @@ export function streamMessage(
const res = await fetch(buildApiUrl('/api/chat/stream'), {
method: 'POST',
headers: authHeaders(),
body: JSON.stringify({ message, session_id: sessionId }),
body: JSON.stringify({
message,
session_id: sessionId,
prompt_locale: promptLocaleForAppLocale(getCurrentAppLocale()),
}),
signal: controller.signal,
});

View File

@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest';
import { isAppLocale, normalizeAppLocale, pickAppText } from '@/lib/i18n/core';
describe('app locale normalization', () => {
it('accepts simplified Chinese, English, and traditional Chinese locales', () => {
expect(isAppLocale('zh-CN')).toBe(true);
expect(isAppLocale('en-US')).toBe(true);
expect(isAppLocale('zh-Hant')).toBe(true);
});
it('normalizes common traditional Chinese locale tags', () => {
expect(normalizeAppLocale('zh-TW')).toBe('zh-Hant');
expect(normalizeAppLocale('zh-HK')).toBe('zh-Hant');
expect(normalizeAppLocale('zh-Hant')).toBe('zh-Hant');
});
});
describe('app text picker', () => {
it('returns simplified Chinese text for zh-CN', () => {
expect(pickAppText('zh-CN', '任务状态', 'Task status')).toBe('任务状态');
});
it('returns English text for en-US', () => {
expect(pickAppText('en-US', '任务状态', 'Task status')).toBe('Task status');
});
it('returns traditional Chinese text for zh-Hant', () => {
expect(pickAppText('zh-Hant', '任务状态', 'Task status')).toBe('任務狀態');
expect(pickAppText('zh-Hant', '智能体结果', 'Agent results')).toBe('智慧體結果');
});
});

View File

@ -1,12 +1,12 @@
export const APP_LOCALE_COOKIE = 'beaver_locale';
export const APP_LOCALE_STORAGE_KEY = 'beaver_locale';
export const APP_LOCALES = ['zh-CN', 'en-US'] as const;
export const APP_LOCALES = ['zh-CN', 'en-US', 'zh-Hant'] as const;
export type AppLocale = (typeof APP_LOCALES)[number];
export function isAppLocale(value: string | null | undefined): value is AppLocale {
return value === 'zh-CN' || value === 'en-US';
return value === 'zh-CN' || value === 'en-US' || value === 'zh-Hant';
}
export function normalizeAppLocale(value?: string | null): AppLocale {
@ -14,6 +14,14 @@ export function normalizeAppLocale(value?: string | null): AppLocale {
if (probe.startsWith('en')) {
return 'en-US';
}
if (
probe === 'zh-hant' ||
probe.startsWith('zh-tw') ||
probe.startsWith('zh-hk') ||
probe.startsWith('zh-mo')
) {
return 'zh-Hant';
}
return 'zh-CN';
}
@ -71,6 +79,507 @@ export function getCurrentAppLocale(): AppLocale {
return readBrowserAppLocale();
}
export function pickAppText<T>(locale: AppLocale, zhValue: T, enValue: T): T {
return locale === 'en-US' ? enValue : zhValue;
export function pickAppText<T>(locale: string | null | undefined, zhValue: T, enValue: T): T {
const appLocale = normalizeAppLocale(locale);
if (appLocale === 'en-US') {
return enValue;
}
if (appLocale === 'zh-Hant') {
return toTraditionalValue(zhValue);
}
return zhValue;
}
function toTraditionalValue<T>(value: T): T {
return typeof value === 'string' ? (toTraditionalChinese(value) as T) : value;
}
const SIMPLIFIED_TO_TRADITIONAL_PHRASES: Array<[string, string]> = [
['智能体', '智慧體'],
['Agent Team', 'Agent Team'],
];
const SIMPLIFIED_TO_TRADITIONAL_CHARS: Record<string, string> = {
: '個',
: '為',
: '麼',
: '義',
: '習',
: '書',
: '了',
: '於',
: '雲',
: '產',
: '僅',
: '從',
: '倉',
: '儀',
: '們',
: '優',
: '會',
: '傳',
: '體',
: '餘',
: '側',
: '偵',
: '促',
: '倆',
: '值',
: '假',
: '做',
: '停',
: '儲',
: '像',
: '兒',
: '先',
: '光',
: '關',
: '興',
: '具',
: '內',
: '冊',
: '寫',
: '軍',
: '農',
: '況',
: '凍',
: '淨',
: '準',
: '幾',
: '擊',
: '劃',
: '則',
: '創',
: '初',
: '刪',
: '別',
: '到',
: '製',
: '劑',
: '剩',
: '辦',
: '功',
: '加',
: '務',
: '動',
: '助',
: '勢',
: '包',
: '區',
: '協',
: '單',
: '賣',
: '佔',
: '卡',
: '歷',
: '壓',
: '廁',
: '廂',
: '縣',
: '參',
: '雙',
: '發',
: '變',
: '疊',
: '號',
: '後',
: '向',
: '嗎',
: '啟',
: '員',
: '命',
: '諮',
: '啞',
: '響',
: '喚',
: '問',
: '單',
: '餵',
: '器',
: '團',
: '園',
: '困',
: '圖',
: '場',
: '塊',
: '壞',
: '址',
: '堅',
: '壇',
: '型',
: '垃',
: '域',
: '堆',
: '填',
: '增',
: '牆',
: '聲',
: '處',
: '備',
: '復',
: '夠',
: '頭',
: '獎',
: '好',
: '如',
: '始',
: '委',
: '存',
: '學',
: '寧',
: '它',
: '安',
: '完',
: '實',
: '審',
: '客',
: '憲',
: '寬',
: '對',
: '導',
: '將',
: '爾',
: '嘗',
: '層',
: '屬',
: '歲',
: '島',
: '州',
: '工',
: '幣',
: '師',
: '帳',
: '帶',
: '幫',
: '乾',
: '並',
广: '廣',
: '慶',
: '庫',
: '應',
: '廢',
: '開',
: '異',
: '棄',
: '張',
: '強',
: '歸',
: '當',
: '錄',
: '徹',
: '徑',
: '待',
: '循',
: '憶',
: '誌',
: '憂',
: '念',
: '態',
: '總',
: '恢',
: '息',
: '您',
: '情',
: '想',
: '意',
: '願',
: '戲',
: '戰',
: '戶',
: '執',
: '擴',
: '掃',
: '揚',
: '批',
: '找',
: '技',
: '報',
: '護',
: '抽',
: '擔',
: '擁',
: '擇',
: '按',
: '揮',
: '換',
: '損',
: '據',
: '授',
: '掉',
: '接',
: '控',
: '推',
: '提',
: '插',
: '揭',
: '搜',
: '攜',
: '攝',
: '摘',
: '播',
: '操',
: '支',
: '收',
: '改',
: '放',
: '效',
: '數',
: '文',
: '斷',
: '新',
: '無',
: '時',
: '明',
: '顯',
: '智',
: '暫',
: '更',
: '替',
: '術',
: '機',
: '權',
: '條',
: '來',
: '極',
: '構',
: '標',
: '欄',
: '樹',
: '樣',
: '核',
: '案',
: '檔',
: '檢',
: '樓',
: '次',
: '款',
: '步',
: '殘',
: '段',
: '畢',
: '氣',
: '匯',
: '漢',
: '沒',
: '法',
: '註',
: '洩',
: '測',
: '瀏',
: '消',
: '涉',
: '漲',
: '潤',
: '添',
: '清',
: '渠',
: '渲',
: '溫',
: '滾',
: '滿',
: '漏',
: '演',
: '點',
: '煩',
: '熱',
: '然',
: '照',
: '愛',
: '父',
: '片',
: '版',
: '狀',
: '獨',
: '環',
: '現',
: '理',
: '畫',
: '暢',
: '療',
: '登',
: '監',
: '盤',
: '碼',
: '礎',
: '確',
: '礙',
: '禮',
: '離',
: '種',
: '稱',
: '穩',
: '窗',
: '筆',
: '簽',
: '簡',
: '算',
: '管',
: '類',
: '黏',
: '精',
: '系',
: '級',
线: '線',
: '組',
: '細',
: '終',
: '經',
: '結',
: '絕',
: '統',
: '維',
: '緩',
: '編',
: '縮',
: '缺',
: '網',
: '置',
: '聯',
: '聊',
: '肅',
: '背',
: '能',
: '腳',
: '脫',
: '腦',
: '自動',
: '艦',
: '藝',
: '節',
: '範',
: '薦',
: '獲',
: '營',
: '落',
: '著',
: '藏',
: '慮',
: '虛',
: '雖',
: '行',
: '補',
: '表',
: '裝',
: '規',
: '視',
: '覺',
: '覽',
: '計',
: '訂',
: '認',
: '議',
: '訊',
: '記',
: '講',
: '許',
: '論',
: '設',
访: '訪',
: '證',
: '評',
: '識',
: '訴',
: '試',
: '話',
: '詳',
: '語',
: '誤',
: '請',
: '讀',
: '調',
: '談',
: '謝',
: '谷',
: '帳',
: '負',
: '責',
: '敗',
: '貨',
: '質',
: '資',
: '贓',
: '起',
: '超',
: '躍',
: '路',
: '蹤',
: '車',
: '輪',
: '軟',
: '載',
: '輯',
: '輸',
: '邊',
: '達',
: '過',
: '還',
: '這',
: '進',
: '遠',
: '連',
: '遲',
: '適',
: '選',
: '遞',
: '通',
: '邏',
: '遺',
: '遙',
: '邀',
: '郵',
: '部',
: '配',
: '釋',
: '重',
: '針',
: '鑰',
: '鐘',
: '鈕',
: '錢',
: '鏈',
: '錯',
: '鍵',
: '鏡',
: '長',
: '門',
: '閉',
: '間',
: '隊',
: '階',
: '陽',
: '陰',
: '陳',
: '際',
: '隱',
: '難',
: '雛',
: '需',
: '面',
: '頁',
: '項',
: '順',
: '須',
: '預',
: '題',
: '顏',
: '風',
: '飛',
: '館',
: '驗',
: '高',
: '魚',
: '鮮',
: '鳥',
: '麥',
: '黃',
};
export function toTraditionalChinese(value: string): string {
let converted = value;
for (const [source, target] of SIMPLIFIED_TO_TRADITIONAL_PHRASES) {
converted = converted.split(source).join(target);
}
return Array.from(converted)
.map((char) => SIMPLIFIED_TO_TRADITIONAL_CHARS[char] ?? char)
.join('');
}

View File

@ -40,9 +40,11 @@ describe('buildTaskTimelineView', () => {
const view = buildTaskTimelineView({
task: task(),
liveEvents,
locale: 'en-US',
});
expect(view?.cards.map((card) => card.type)).toEqual(['task_created', 'plan']);
expect(view?.cards.map((card) => card.title)).toEqual(['Task created', 'Execution plan']);
expect(view?.process.events.map((event) => event.event_id)).toEqual(['plan']);
});

View File

@ -1,9 +1,11 @@
import { selectTaskProcess, type SelectTaskProcessInput, type TaskProcessSelection } from '@/lib/task-process';
import { buildTaskTimelineCards } from '@/lib/task-timeline';
import type { AppLocale } from '@/lib/i18n/core';
import type { BackendTask, TaskTimelineCard } from '@/types';
export type BuildTaskTimelineViewInput = Omit<SelectTaskProcessInput, 'task'> & {
task: BackendTask | null;
locale?: AppLocale | string;
};
export type TaskTimelineView = {
@ -16,6 +18,7 @@ export function buildTaskTimelineView({
liveRuns,
liveEvents,
liveArtifacts,
locale,
}: BuildTaskTimelineViewInput): TaskTimelineView | null {
if (!task) return null;
@ -32,6 +35,7 @@ export function buildTaskTimelineView({
processRuns: process.runs,
processEvents: process.events,
processArtifacts: process.artifacts,
locale,
}),
};
}

View File

@ -143,6 +143,48 @@ describe('buildTaskTimelineCards', () => {
expect(cards[6].relatedArtifactIds).toEqual(['artifact-summary']);
});
it('localizes generated milestone titles for English and Traditional Chinese', () => {
const task = makeTask();
const processEvents: ProcessEvent[] = [
{
event_id: 'evt-plan',
run_id: 'run-main',
parent_run_id: null,
kind: 'task_planned',
actor_type: 'agent',
actor_id: 'planner',
actor_name: 'Task Planner',
text: 'Plan created.',
created_at: '2026-05-26T10:01:00.000Z',
},
{
event_id: 'evt-tool-start',
run_id: 'run-main',
parent_run_id: null,
kind: 'tool_call_started',
actor_type: 'mcp',
actor_id: 'user_files_list',
actor_name: 'user_files_list',
text: 'Calling tool: user_files_list.',
created_at: '2026-05-26T10:02:00.000Z',
},
];
const englishCards = buildTaskTimelineCards({ task, processEvents, locale: 'en-US' });
const traditionalCards = buildTaskTimelineCards({ task, processEvents, locale: 'zh-Hant' });
expect(englishCards.map((card) => card.title)).toEqual([
'Task created',
'Execution plan',
'Calling tool: user_files_list',
]);
expect(traditionalCards.map((card) => card.title)).toEqual([
'任務已創建',
'執行計劃',
'調用工具user_files_list',
]);
});
it('appends result and acceptance cards for closed tasks with feedback', () => {
const task = makeTask({
is_open: false,

View File

@ -6,12 +6,14 @@ import type {
TaskTimelineCard,
TaskTimelineCardType,
} from '@/types';
import { getCurrentAppLocale, pickAppText, type AppLocale } from '@/lib/i18n/core';
export type BuildTaskTimelineCardsInput = {
task: BackendTask;
processRuns?: ProcessRun[];
processEvents?: ProcessEvent[];
processArtifacts?: ProcessArtifact[];
locale?: AppLocale | string;
};
const TIMELINE_CARD_TYPES = new Set<TaskTimelineCardType>([
@ -110,36 +112,40 @@ function cardTypeForEvent(event: ProcessEvent): TaskTimelineCardType | null {
}
}
function titleForCard(type: TaskTimelineCardType, actorName?: string): string {
function titleForCard(type: TaskTimelineCardType, actorName?: string, locale: AppLocale | string = getCurrentAppLocale()): string {
switch (type) {
case 'task_created':
return '任务已创建';
return pickAppText(locale, '任务已创建', 'Task created');
case 'plan':
return '执行计划';
return pickAppText(locale, '执行计划', 'Execution plan');
case 'skill':
return '选择 Skill';
return pickAppText(locale, '选择 Skill', 'Skill selected');
case 'tool_call':
return actorName ? `调用工具:${actorName}` : '调用工具';
return actorName
? pickAppText(locale, `调用工具:${actorName}`, `Calling tool: ${actorName}`)
: pickAppText(locale, '调用工具', 'Tool call');
case 'tool_result':
return actorName ? `工具结果:${actorName}` : '工具结果';
return actorName
? pickAppText(locale, `工具结果:${actorName}`, `Tool result: ${actorName}`)
: pickAppText(locale, '工具结果', 'Tool result');
case 'next_step':
return '下一步';
return pickAppText(locale, '下一步', 'Next step');
case 'agent_team':
return '启动 Agent Team';
return pickAppText(locale, '启动 Agent Team', 'Agent team started');
case 'agent_progress':
return actorName || 'Agent 进展';
return actorName || pickAppText(locale, 'Agent 进展', 'Agent progress');
case 'agent_handoff':
return 'Agent 交接';
return pickAppText(locale, 'Agent 交接', 'Agent handoff');
case 'artifact':
return '生成产物';
return pickAppText(locale, '生成产物', 'Artifact generated');
case 'error':
return '执行遇到问题';
return pickAppText(locale, '执行遇到问题', 'Execution issue');
case 'result':
return '本轮结果';
return pickAppText(locale, '本轮结果', 'Run result');
case 'result_history':
return '历史结果版本';
return pickAppText(locale, '历史结果版本', 'Previous result versions');
case 'acceptance':
return '任务验收';
return pickAppText(locale, '任务验收', 'Task acceptance');
}
}
@ -286,7 +292,12 @@ function buildToolResultStatusByCall(processEvents: ProcessEvent[]): Map<string,
return statuses;
}
function buildResultHistoryCard(task: BackendTask, resultCards: TaskTimelineCard[], acceptanceCards: TaskTimelineCard[]): TaskTimelineCard {
function buildResultHistoryCard(
task: BackendTask,
resultCards: TaskTimelineCard[],
acceptanceCards: TaskTimelineCard[],
locale: AppLocale | string,
): TaskTimelineCard {
const versions = resultCards.map((resultCard) => {
const acceptanceCard = acceptanceCards
.filter((card) => card.runId === resultCard.runId)
@ -307,14 +318,18 @@ function buildResultHistoryCard(task: BackendTask, resultCards: TaskTimelineCard
id: `${task.task_id}:result-history`,
taskId: task.task_id,
type: 'result_history',
title: titleForCard('result_history'),
summary: `${resultCards.length} 历史结果版本`,
title: titleForCard('result_history', undefined, locale),
summary: pickAppText(
locale,
`${resultCards.length} 历史结果版本`,
`${resultCards.length} previous result ${resultCards.length === 1 ? 'version' : 'versions'}`,
),
createdAt: resultCards[0]?.createdAt ?? task.created_at,
details: { versions },
};
}
function collapseHistoricalResults(task: BackendTask, cards: TaskTimelineCard[]): TaskTimelineCard[] {
function collapseHistoricalResults(task: BackendTask, cards: TaskTimelineCard[], locale: AppLocale | string): TaskTimelineCard[] {
const resultCards = cards.filter((card) => card.type === 'result');
if (resultCards.length <= 1) return cards;
@ -334,7 +349,7 @@ function collapseHistoricalResults(task: BackendTask, cards: TaskTimelineCard[])
.filter((card) => card.type === 'acceptance' && oldRunIds.has(card.runId))
.sort((a, b) => cardTime(a) - cardTime(b));
const foldedIds = new Set([...oldResults, ...oldAcceptances].map((card) => card.id));
const historyCard = buildResultHistoryCard(task, oldResults, oldAcceptances);
const historyCard = buildResultHistoryCard(task, oldResults, oldAcceptances, locale);
const firstOldResultIndex = cards.findIndex((card) => card.id === oldResults[0].id);
const output: TaskTimelineCard[] = [];
@ -352,6 +367,7 @@ function collapseHistoricalResults(task: BackendTask, cards: TaskTimelineCard[])
export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): TaskTimelineCard[] {
const { task } = input;
const locale = input.locale ?? getCurrentAppLocale();
const processRuns = input.processRuns ?? task.process_runs ?? [];
const processEvents = input.processEvents ?? task.process_events ?? [];
const processArtifacts = input.processArtifacts ?? task.process_artifacts ?? [];
@ -365,7 +381,7 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
id: `${task.task_id}:created`,
taskId: task.task_id,
type: 'task_created',
title: titleForCard('task_created'),
title: titleForCard('task_created', undefined, locale),
summary: firstString(task.short_title, task.description, task.goal),
actorName: task.creator,
status: task.status,
@ -396,7 +412,7 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
runId: event.run_id,
parentRunId: event.parent_run_id,
type,
title: titleForCard(type, event.actor_name),
title: titleForCard(type, event.actor_name, locale),
summary: type === 'result' ? resultSummaryForEvent(task, event) : summaryForEvent(event),
actorName: event.actor_name,
status:
@ -418,7 +434,7 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
runId: run.run_id,
parentRunId: run.parent_run_id,
type: 'agent_progress',
title: titleForCard('agent_progress', run.actor_name),
title: titleForCard('agent_progress', run.actor_name, locale),
summary: firstString(run.summary, run.title),
actorName: run.actor_name,
status: run.status,
@ -435,7 +451,7 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
runId: artifact.run_id,
parentRunId: run?.parent_run_id,
type: 'artifact',
title: titleForCard('artifact'),
title: titleForCard('artifact', undefined, locale),
summary: firstString(artifact.title),
actorName: artifact.actor_name,
createdAt: artifact.created_at,
@ -454,7 +470,7 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
taskId: task.task_id,
runId: lastItem(task.run_ids),
type: 'result',
title: titleForCard('result'),
title: titleForCard('result', undefined, locale),
summary: fallbackResultSummary(task),
status: task.status,
createdAt: task.closed_at ?? task.updated_at ?? task.created_at,
@ -473,7 +489,7 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
taskId: task.task_id,
runId,
type: 'acceptance',
title: titleForCard('acceptance'),
title: titleForCard('acceptance', undefined, locale),
summary: feedbackSummary(feedback),
status: firstString(feedback.acceptance_type),
createdAt,
@ -486,5 +502,5 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
.sort(compareCardsByCreatedAt)
.map(({ card }) => card);
return collapseHistoricalResults(task, sortedCards);
return collapseHistoricalResults(task, sortedCards, locale);
}