Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(voice-input): 按地区设置默认识别语言 (#553)

Dash8d2e8ed7a19e
diff --git a/apps/desktop/src/renderer/voice-input/__tests__/voiceInputData.test.ts b/apps/desktop/src/renderer/voice-input/__tests__/voiceInputData.test.ts
index 379eb1109f..686b0b674f 100644
--- a/apps/desktop/src/renderer/voice-input/__tests__/voiceInputData.test.ts
+++ b/apps/desktop/src/renderer/voice-input/__tests__/voiceInputData.test.ts
@@ -2,9 +2,11 @@ import { describe, expect, it } from 'vitest';
import {
createVoiceInputShortcutFromMacNativeKeys,
+ getDefaultVoiceInputSettings,
isVoiceInputMacNativeKeyboardShortcutPressed,
isVoiceInputMacNativeKeyboardShortcutTargetDown,
isVoiceInputModifierShortcut,
+ normalizeVoiceInputSettings,
normalizeVoiceInputShortcut,
voiceInputShortcutNeedsMacNativeListener,
} from '../../../shared/voiceInputData';
@@ -14,6 +16,20 @@ import {
getVoiceInputBareModifierCodeFromEvent,
} from '../shortcut';
+describe('voice input language defaults', () => {
+ it('defaults Global to auto and Mainland China to Simplified Chinese', () => {
+ expect(getDefaultVoiceInputSettings('darwin', 'global').language).toBe('auto');
+ expect(getDefaultVoiceInputSettings('darwin', 'cn').language).toBe('zh-CN');
+ expect(getDefaultVoiceInputSettings('darwin', 'dev').language).toBe('auto');
+ });
+
+ it('keeps an explicit language instead of replacing it with the region default', () => {
+ expect(normalizeVoiceInputSettings({ language: 'en' }, 'darwin', 'cn').language).toBe('en');
+ expect(normalizeVoiceInputSettings({ language: 'auto' }, 'darwin', 'cn').language).toBe('auto');
+ expect(normalizeVoiceInputSettings({}, 'darwin', 'cn').language).toBe('zh-CN');
+ });
+});
+
describe('voice input shortcut normalization', () => {
it('keeps legacy keyboard shortcuts compatible', () => {
expect(normalizeVoiceInputShortcut({
diff --git a/apps/desktop/src/shared/voiceInputData.ts b/apps/desktop/src/shared/voiceInputData.ts
index 0922730e76..2b1a7a3e2d 100644
--- a/apps/desktop/src/shared/voiceInputData.ts
+++ b/apps/desktop/src/shared/voiceInputData.ts
@@ -5,7 +5,9 @@ import type {
DictationDictionaryLearningEntryState,
DictationRefinementContext,
} from '@cindy/voice-input-core';
+import type { CindyRegion } from '@cindy/maker-shared/brand-identity';
+import { CURRENT_CINDY_REGION } from './brandRegion';
import { SUPPORTED_LOCALES, type SupportedLocale } from './locale';
import type { IpcErrorCode } from './ipc-errors';
@@ -387,9 +389,15 @@ export function isVoiceInputMacNativeKeyboardShortcutTargetDown(
return typeof expectedKeyCode === 'number' && keys.includes(`KeyCode:${expectedKeyCode}`);
}
-export function getDefaultVoiceInputSettings(platform?: string): VoiceInputSettings {
+export function getDefaultVoiceInputSettings(
+ platform?: string,
+ region: CindyRegion = CURRENT_CINDY_REGION,
+): VoiceInputSettings {
return {
- language: 'auto',
+ // Global cannot assume a spoken language; the Mainland China build uses
+ // Chinese as its product default. A persisted user choice still wins in
+ // normalizeVoiceInputSettings below.
+ language: region === 'cn' ? 'zh-CN' : 'auto',
microphoneDeviceId: null,
muteSystemAudio: true,
playInteractionSound: true,
@@ -407,8 +415,9 @@ export function getDefaultVoiceInputSettings(platform?: string): VoiceInputSetti
export function normalizeVoiceInputSettings(
raw: unknown,
platform?: string,
+ region: CindyRegion = CURRENT_CINDY_REGION,
): VoiceInputSettings {
- const defaults = getDefaultVoiceInputSettings(platform);
+ const defaults = getDefaultVoiceInputSettings(platform, region);
if (!raw || typeof raw !== 'object') return defaults;
const candidate = raw as Partial<VoiceInputSettings>;
const legacyPlayStartSound = (candidate as { playStartSound?: unknown }).playStartSound;
diff --git a/apps/mobile/src/__tests__/mobileCindyVoiceSession.test.ts b/apps/mobile/src/__tests__/mobileCindyVoiceSession.test.ts
index a9e9e44665..97c176833e 100644
--- a/apps/mobile/src/__tests__/mobileCindyVoiceSession.test.ts
+++ b/apps/mobile/src/__tests__/mobileCindyVoiceSession.test.ts
@@ -7,6 +7,7 @@ vi.mock('expo-constants', () => ({
},
}));
vi.mock('@/config/env', () => ({
+ AUTH_REGION: 'global',
VOICE_API_BASE_URL: 'https://voice.example.com/',
}));
vi.mock('@/auth/secureStorage', () => ({
@@ -245,6 +246,14 @@ describe('createMobileCindyVoiceCredential', () => {
});
});
+ it.each([
+ ['cn', 'zh-CN'],
+ ['global', 'auto'],
+ ['dev', 'auto'],
+ ] as const)('uses the %s build voice language default', (region, language) => {
+ expect(createMobileCindyVoiceCredential('device-1', region).settings?.language).toBe(language);
+ });
+
it('rejects an empty host device id', () => {
expect(() => createMobileCindyVoiceCredential(' ')).toThrow('host device id is required');
});
diff --git a/apps/mobile/src/session/mobileCindyVoiceSession.ts b/apps/mobile/src/session/mobileCindyVoiceSession.ts
index 3ba07d91d7..66e502430a 100644
--- a/apps/mobile/src/session/mobileCindyVoiceSession.ts
+++ b/apps/mobile/src/session/mobileCindyVoiceSession.ts
@@ -1,7 +1,11 @@
import Constants from 'expo-constants';
import { ApiError, type ApiFetchOptions } from '@/api/client';
-import { VOICE_API_BASE_URL } from '@/config/env';
+import {
+ AUTH_REGION,
+ VOICE_API_BASE_URL,
+ type CindyAuthRegion,
+} from '@/config/env';
import { i18n } from '@/i18n';
import type {
MobileVoiceCredentialSyncAsr,
@@ -231,7 +235,10 @@ const CINDY_MANAGED_REFINER_CHAIN = [
] as const satisfies readonly MobileVoiceCredentialSyncRefiner[];
/** Builds the provider-neutral profile graph without persisting any inference key. */
-export function createMobileCindyVoiceCredential(hostDeviceId: string): StoredMobileVoiceCredential {
+export function createMobileCindyVoiceCredential(
+ hostDeviceId: string,
+ region: CindyAuthRegion = AUTH_REGION,
+): StoredMobileVoiceCredential {
const normalizedHostDeviceId = hostDeviceId.trim();
if (!normalizedHostDeviceId) throw new Error('host device id is required');
const baseUrl = requireVoiceBaseUrl();
@@ -247,7 +254,9 @@ export function createMobileCindyVoiceCredential(hostDeviceId: string): StoredMo
refiner: { ...CINDY_MANAGED_REFINER_CHAIN[0] },
refinerProviderChain: CINDY_MANAGED_REFINER_CHAIN.map((item) => ({ ...item })),
settings: {
- language: 'auto',
+ // Global and dev builds let ASR detect the spoken language. The Mainland
+ // China build keeps Chinese as its product default.
+ language: region === 'cn' ? 'zh-CN' : 'auto',
refinementEnabled: true,
playInteractionSound: true,
},