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(i18n): 动态翻译表单校验错误

davide57d4aa4956d
diff --git a/apps/mobile/app/automations/[deviceId].tsx b/apps/mobile/app/automations/[deviceId].tsx
index 0da3d170b1..ed13b085e9 100644
--- a/apps/mobile/app/automations/[deviceId].tsx
+++ b/apps/mobile/app/automations/[deviceId].tsx
@@ -67,6 +67,7 @@ import {
createTemplateParamDefaults,
deriveMobileScheduleSessionMode,
hasMobileScheduleRealBinding,
+ localizeScheduleDraftValidation,
MOBILE_SCHEDULE_PENDING_SESSION_ID,
updateDraftAgentKind,
updateDraftBoundSessionId,
@@ -79,6 +80,7 @@ import {
validateTemplateParamValues,
validateMobileScheduleDraft,
type MobileScheduleDraft,
+ type ScheduleDraftValidation,
} from '@/scheduler/scheduleFormModel';
import { useRemoteScheduleEventSnapshot } from '@/scheduler/remoteScheduleEvents';
import {
@@ -151,7 +153,7 @@ export default function AutomationsScreen() {
const [formMode, setFormMode] = useState<'create' | 'edit' | null>(null);
const [formDraft, setFormDraft] = useState<MobileScheduleDraft | null>(null);
const [formScheduleId, setFormScheduleId] = useState<string | null>(null);
- const [formError, setFormError] = useState<string | null>(null);
+ const [formError, setFormError] = useState<string | ScheduleDraftValidation | null>(null);
const [templates, setTemplates] = useState<RemoteScheduleTemplate[]>([]);
const [templatesLoading, setTemplatesLoading] = useState(false);
const [templateError, setTemplateError] = useState<string | null>(null);
@@ -173,6 +175,11 @@ export default function AutomationsScreen() {
() => summarizeAutomationOverview(schedules, runsBySchedule),
[runsBySchedule, schedules],
);
+ const formErrorText = typeof formError === 'string'
+ ? formError
+ : formError
+ ? localizeScheduleDraftValidation(formError, mobilePresentationLocalizer)
+ : null;
const bindableSessions = useMemo(
() => selectBindableSessions(remoteSessions, deviceId),
[deviceId, remoteSessions],
@@ -389,7 +396,7 @@ export default function AutomationsScreen() {
if (!formDraft || busyAction) return;
const validation = validateMobileScheduleDraft(formDraft, mobilePresentationLocalizer);
if (validation) {
- setFormError(validation.message);
+ setFormError(validation);
return;
}
if (formMode === 'create' && selectedTemplate && !templatePromptDirty) {
@@ -846,7 +853,7 @@ export default function AutomationsScreen() {
<ScheduleFormCard
busy={busyAction === 'create' || busyAction === `edit:${formScheduleId}`}
draft={formDraft}
- error={formError}
+ error={formErrorText}
mode={formMode ?? 'create'}
onCancel={closeScheduleForm}
onChange={setFormDraft}
diff --git a/apps/mobile/src/__tests__/mobilePresentationRefresh.test.ts b/apps/mobile/src/__tests__/mobilePresentationRefresh.test.ts
index 1f4d69e6da..b7e830b6f2 100644
--- a/apps/mobile/src/__tests__/mobilePresentationRefresh.test.ts
+++ b/apps/mobile/src/__tests__/mobilePresentationRefresh.test.ts
@@ -73,6 +73,14 @@ describe('mobile localized presentation refresh', () => {
expect(source).toContain('{describeAgentAuthError(error) ?? error}');
});
+ it('keeps schedule validation metadata until render time', () => {
+ const source = read('app/automations/[deviceId].tsx');
+
+ expect(source).toContain('setFormError(validation)');
+ expect(source).toContain('localizeScheduleDraftValidation(formError, mobilePresentationLocalizer)');
+ expect(source).toContain('error={formErrorText}');
+ });
+
it('rebuilds localized session sections when the language changes', () => {
const source = read('app/devices/[deviceId].tsx');
const sectionProjection = source.slice(
diff --git a/apps/mobile/src/__tests__/scheduleFormModel.test.ts b/apps/mobile/src/__tests__/scheduleFormModel.test.ts
index 030059d820..01fc4ee961 100644
--- a/apps/mobile/src/__tests__/scheduleFormModel.test.ts
+++ b/apps/mobile/src/__tests__/scheduleFormModel.test.ts
@@ -6,6 +6,7 @@ import {
createMobileScheduleDraft,
createTemplateParamDefaults,
deriveMobileScheduleSessionMode,
+ localizeScheduleDraftValidation,
MOBILE_SCHEDULE_PENDING_SESSION_ID,
updateDraftAgentKind,
updateDraftBoundSessionId,
@@ -114,6 +115,23 @@ describe('mobile schedule form model', () => {
})).toBeNull();
});
+ it('relocalizes a stored draft validation without rerunning validation rules', () => {
+ const draft = createMobileScheduleDraft(null);
+ const oldLocalizer = {
+ translate: () => '旧语言错误',
+ };
+ const validation = validateMobileScheduleDraft(draft, oldLocalizer);
+
+ expect(validation).toMatchObject({
+ field: 'name',
+ message: '旧语言错误',
+ messageKey: 'devices.automations.presentation.validation.name',
+ });
+ expect(validation && localizeScheduleDraftValidation(validation, {
+ translate: () => 'New locale error',
+ })).toBe('New locale error');
+ });
+
it('does not require a project path when editing a bound desktop schedule', () => {
const draft = createMobileScheduleDraft(schedule({
workingDir: '',
diff --git a/packages/maker-shared/src/scheduleForm.ts b/packages/maker-shared/src/scheduleForm.ts
index 60ddca075f..0840be4f0e 100644
--- a/packages/maker-shared/src/scheduleForm.ts
+++ b/packages/maker-shared/src/scheduleForm.ts
@@ -7,7 +7,11 @@ import type {
RemoteScheduleWriteInput,
RemoteTemplateParameter,
} from './scheduleTypes';
-import { presentationText, type PresentationLocalizer } from './presentationLocalization.js';
+import {
+ presentationText,
+ type PresentationInterpolationValue,
+ type PresentationLocalizer,
+} from './presentationLocalization.js';
export const MOBILE_SCHEDULE_EFFORT_VALUES = [
'minimal',
@@ -67,6 +71,37 @@ export interface MobileScheduleDraft {
export interface ScheduleDraftValidation {
field: keyof MobileScheduleDraft;
message: string;
+ messageFallback: string;
+ messageKey: string;
+ messageValues?: Readonly<Record<string, PresentationInterpolationValue>>;
+}
+
+function scheduleDraftValidation(
+ field: keyof MobileScheduleDraft,
+ localizer: PresentationLocalizer | undefined,
+ messageKey: string,
+ messageFallback: string,
+ messageValues?: Readonly<Record<string, PresentationInterpolationValue>>,
+): ScheduleDraftValidation {
+ return {
+ field,
+ message: presentationText(localizer, messageKey, messageFallback, messageValues),
+ messageFallback,
+ messageKey,
+ messageValues,
+ };
+}
+
+export function localizeScheduleDraftValidation(
+ validation: ScheduleDraftValidation,
+ localizer?: PresentationLocalizer,
+): string {
+ return presentationText(
+ localizer,
+ validation.messageKey,
+ validation.messageFallback,
+ validation.messageValues,
+ );
}
const DEFAULT_CRON = '0 9 * * *';
@@ -226,60 +261,75 @@ export function validateMobileScheduleDraft(
localizer?: PresentationLocalizer,
): ScheduleDraftValidation | null {
if (!draft.name.trim()) {
- return {
- field: 'name',
- message: presentationText(localizer, 'devices.automations.presentation.validation.name', '请输入任务名称'),
- };
+ return scheduleDraftValidation(
+ 'name',
+ localizer,
+ 'devices.automations.presentation.validation.name',
+ '请输入任务名称',
+ );
}
// "仅运行脚本"任务(桌面端高级功能)prompt 合法为空——mobile 没有编辑
// scriptConfig 的 UI,不该拿桌面端才有意义的字段挡住这类任务在移动端的其它
// 可编辑操作(改名、通知开关等),否则打开/保存一个桌面端创建的脚本任务在
// 移动端会先于任何改动就校验失败(codex review 发现)。
if (draft.executionMode !== 'script' && !draft.prompt.trim()) {
- return {
- field: 'prompt',
- message: presentationText(localizer, 'devices.automations.presentation.validation.prompt', '请输入任务提示词'),
- };
+ return scheduleDraftValidation(
+ 'prompt',
+ localizer,
+ 'devices.automations.presentation.validation.prompt',
+ '请输入任务提示词',
+ );
}
if (!draft.timezone.trim()) {
- return {
- field: 'timezone',
- message: presentationText(localizer, 'devices.automations.presentation.validation.timezone', '请输入时区'),
- };
+ return scheduleDraftValidation(
+ 'timezone',
+ localizer,
+ 'devices.automations.presentation.validation.timezone',
+ '请输入时区',
+ );
}
if (draft.targetSessionId.trim() === MOBILE_SCHEDULE_PENDING_SESSION_ID) {
- return {
- field: 'targetSessionId',
- message: presentationText(localizer, 'devices.automations.presentation.validation.boundSession', '请选择要绑定的任务'),
- };
+ return scheduleDraftValidation(
+ 'targetSessionId',
+ localizer,
+ 'devices.automations.presentation.validation.boundSession',
+ '请选择要绑定的任务',
+ );
}
if (draft.runMode === 'recurring') {
if (!draft.cronExpr.trim()) {
- return {
- field: 'cronExpr',
- message: presentationText(localizer, 'devices.automations.presentation.validation.cron', '请输入 cron 表达式'),
- };
+ return scheduleDraftValidation(
+ 'cronExpr',
+ localizer,
+ 'devices.automations.presentation.validation.cron',
+ '请输入 cron 表达式',
+ );
}
- const intervalError = validateIntervalMinutes(draft.intervalMinutes, localizer);
- if (intervalError) return { field: 'intervalMinutes', message: intervalError };
+ const intervalValidation = validateIntervalMinutes(draft.intervalMinutes, localizer);
+ if (intervalValidation) return intervalValidation;
}
if (
draft.workspaceKind === 'project' &&
!draft.targetSessionId.trim() &&
!draft.workingDir.trim()
) {
- return {
- field: 'workingDir',
- message: presentationText(localizer, 'devices.automations.presentation.validation.workingDir', '请输入项目目录'),
- };
+ return scheduleDraftValidation(
+ 'workingDir',
+ localizer,
+ 'devices.automations.presentation.validation.workingDir',
+ '请输入项目目录',
+ );
}
if (draft.effort.trim() && !isMobileScheduleEffort(draft.effort.trim())) {
- return {
- field: 'effort',
- message: presentationText(localizer, 'devices.automations.presentation.validation.effort', `推理强度只能是 ${MOBILE_SCHEDULE_EFFORT_VALUES.join(' / ')}`, {
+ return scheduleDraftValidation(
+ 'effort',
+ localizer,
+ 'devices.automations.presentation.validation.effort',
+ `推理强度只能是 ${MOBILE_SCHEDULE_EFFORT_VALUES.join(' / ')}`,
+ {
values: MOBILE_SCHEDULE_EFFORT_VALUES.join(' / '),
- }),
- };
+ },
+ );
}
return null;
}
@@ -548,14 +598,24 @@ function defaultModelFor(agentKind: RemoteScheduleAgentKind): string {
function validateIntervalMinutes(
value: string,
localizer?: PresentationLocalizer,
-): string | null {
+): ScheduleDraftValidation | null {
if (!value.trim()) return null;
const minutes = Number(value);
if (!Number.isInteger(minutes) || minutes <= 0) {
- return presentationText(localizer, 'devices.automations.presentation.validation.intervalPositiveInteger', '间隔分钟必须是正整数');
+ return scheduleDraftValidation(
+ 'intervalMinutes',
+ localizer,
+ 'devices.automations.presentation.validation.intervalPositiveInteger',
+ '间隔分钟必须是正整数',
+ );
}
if (intervalMinutesToCronExpr(minutes) === null) {
- return presentationText(localizer, 'devices.automations.presentation.validation.intervalUnsupported', '分钟间隔只支持 1-59 分钟,或 1-23 小时的整点间隔');
+ return scheduleDraftValidation(
+ 'intervalMinutes',
+ localizer,
+ 'devices.automations.presentation.validation.intervalUnsupported',
+ '分钟间隔只支持 1-59 分钟,或 1-23 小时的整点间隔',
+ );
}
return null;
}