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): 动态翻译模板参数错误

davida8e372621c51
diff --git a/apps/mobile/app/automations/[deviceId].tsx b/apps/mobile/app/automations/[deviceId].tsx
index ed13b085e9..fad784d2e8 100644
--- a/apps/mobile/app/automations/[deviceId].tsx
+++ b/apps/mobile/app/automations/[deviceId].tsx
@@ -68,6 +68,7 @@ import {
deriveMobileScheduleSessionMode,
hasMobileScheduleRealBinding,
localizeScheduleDraftValidation,
+ localizeTemplateParamValidation,
MOBILE_SCHEDULE_PENDING_SESSION_ID,
updateDraftAgentKind,
updateDraftBoundSessionId,
@@ -81,6 +82,7 @@ import {
validateMobileScheduleDraft,
type MobileScheduleDraft,
type ScheduleDraftValidation,
+ type TemplateParamValidation,
} from '@/scheduler/scheduleFormModel';
import { useRemoteScheduleEventSnapshot } from '@/scheduler/remoteScheduleEvents';
import {
@@ -153,7 +155,9 @@ 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 | ScheduleDraftValidation | null>(null);
+ const [formError, setFormError] = useState<
+ string | ScheduleDraftValidation | TemplateParamValidation | null
+ >(null);
const [templates, setTemplates] = useState<RemoteScheduleTemplate[]>([]);
const [templatesLoading, setTemplatesLoading] = useState(false);
const [templateError, setTemplateError] = useState<string | null>(null);
@@ -175,10 +179,25 @@ export default function AutomationsScreen() {
() => summarizeAutomationOverview(schedules, runsBySchedule),
[runsBySchedule, schedules],
);
+ const selectedTemplatePresentation = useMemo(
+ () => selectedTemplate
+ ? localizeBuiltinTemplate(
+ templates.find((template) => template.id === selectedTemplate.id) ?? selectedTemplate,
+ t,
+ )
+ : null,
+ [selectedTemplate, t, templates],
+ );
const formErrorText = typeof formError === 'string'
? formError
: formError
- ? localizeScheduleDraftValidation(formError, mobilePresentationLocalizer)
+ ? 'parameterKey' in formError && selectedTemplatePresentation
+ ? localizeTemplateParamValidation(
+ formError,
+ selectedTemplatePresentation,
+ mobilePresentationLocalizer,
+ )
+ : localizeScheduleDraftValidation(formError, mobilePresentationLocalizer)
: null;
const bindableSessions = useMemo(
() => selectBindableSessions(remoteSessions, deviceId),
diff --git a/apps/mobile/src/__tests__/mobilePresentationRefresh.test.ts b/apps/mobile/src/__tests__/mobilePresentationRefresh.test.ts
index b7e830b6f2..529ed222a5 100644
--- a/apps/mobile/src/__tests__/mobilePresentationRefresh.test.ts
+++ b/apps/mobile/src/__tests__/mobilePresentationRefresh.test.ts
@@ -81,6 +81,14 @@ describe('mobile localized presentation refresh', () => {
expect(source).toContain('error={formErrorText}');
});
+ it('keeps template-parameter validation metadata until render time', () => {
+ const source = read('app/automations/[deviceId].tsx');
+
+ expect(source).toContain('setFormError(paramError)');
+ expect(source).toContain('selectedTemplatePresentation,');
+ expect(source).toContain('localizeTemplateParamValidation(');
+ });
+
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 01fc4ee961..4fb1bff633 100644
--- a/apps/mobile/src/__tests__/scheduleFormModel.test.ts
+++ b/apps/mobile/src/__tests__/scheduleFormModel.test.ts
@@ -7,6 +7,7 @@ import {
createTemplateParamDefaults,
deriveMobileScheduleSessionMode,
localizeScheduleDraftValidation,
+ localizeTemplateParamValidation,
MOBILE_SCHEDULE_PENDING_SESSION_ID,
updateDraftAgentKind,
updateDraftBoundSessionId,
@@ -132,6 +133,41 @@ describe('mobile schedule form model', () => {
})).toBe('New locale error');
});
+ it('relocalizes a stored template-parameter validation', () => {
+ const template: RemoteScheduleTemplate = {
+ id: 'custom',
+ name: 'Custom',
+ description: 'Custom',
+ category: 'status-reports',
+ source: 'builtin',
+ parameters: [
+ { key: 'project', label: 'Project', type: 'string', required: true },
+ ],
+ };
+ const validation = validateTemplateParamValues(template, {}, {
+ translate: () => '旧语言参数错误',
+ });
+
+ expect(validation).toMatchObject({
+ field: 'prompt',
+ message: '旧语言参数错误',
+ messageKey: 'devices.automations.presentation.validation.templateParameter',
+ messageValues: { label: 'Project' },
+ parameterKey: 'project',
+ });
+ const relocalizedTemplate = {
+ ...template,
+ parameters: [
+ { key: 'project', label: '项目', type: 'string' as const, required: true },
+ ],
+ };
+ expect(validation && localizeTemplateParamValidation(
+ validation,
+ relocalizedTemplate,
+ { translate: (_key, _fallback, values) => `新语言参数:${values?.label}` },
+ )).toBe('新语言参数:项目');
+ });
+
it('does not require a project path when editing a bound desktop schedule', () => {
const draft = createMobileScheduleDraft(schedule({
workingDir: '',
@@ -233,7 +269,13 @@ describe('mobile schedule form model', () => {
],
};
- expect(validateTemplateParamValues(template, {})).toBe('请输入模板参数:Project');
+ expect(validateTemplateParamValues(template, {})).toMatchObject({
+ field: 'prompt',
+ message: '请输入模板参数:Project',
+ messageKey: 'devices.automations.presentation.validation.templateParameter',
+ messageValues: { label: 'Project' },
+ parameterKey: 'project',
+ });
expect(() => applyMobileTemplateParams('Run {{project}}', {}, template.parameters))
.toThrow('Missing required template parameter');
});
diff --git a/packages/maker-shared/src/__tests__/scheduleForm.test.ts b/packages/maker-shared/src/__tests__/scheduleForm.test.ts
index 4438680bad..e9f1c696e3 100644
--- a/packages/maker-shared/src/__tests__/scheduleForm.test.ts
+++ b/packages/maker-shared/src/__tests__/scheduleForm.test.ts
@@ -443,7 +443,13 @@ describe('mobile schedule form model', () => {
],
};
- expect(validateTemplateParamValues(template, {})).toBe('请输入模板参数:Project');
+ expect(validateTemplateParamValues(template, {})).toMatchObject({
+ field: 'prompt',
+ message: '请输入模板参数:Project',
+ messageKey: 'devices.automations.presentation.validation.templateParameter',
+ messageValues: { label: 'Project' },
+ parameterKey: 'project',
+ });
expect(() => applyMobileTemplateParams('Run {{project}}', {}, template.parameters))
.toThrow('Missing required template parameter');
});
diff --git a/packages/maker-shared/src/scheduleForm.ts b/packages/maker-shared/src/scheduleForm.ts
index 0840be4f0e..0232b5ebff 100644
--- a/packages/maker-shared/src/scheduleForm.ts
+++ b/packages/maker-shared/src/scheduleForm.ts
@@ -76,6 +76,10 @@ export interface ScheduleDraftValidation {
messageValues?: Readonly<Record<string, PresentationInterpolationValue>>;
}
+export interface TemplateParamValidation extends ScheduleDraftValidation {
+ parameterKey: string;
+}
+
function scheduleDraftValidation(
field: keyof MobileScheduleDraft,
localizer: PresentationLocalizer | undefined,
@@ -104,6 +108,21 @@ export function localizeScheduleDraftValidation(
);
}
+export function localizeTemplateParamValidation(
+ validation: TemplateParamValidation,
+ template: Pick<RemoteScheduleTemplate, 'parameters'>,
+ localizer?: PresentationLocalizer,
+): string {
+ const parameter = template.parameters?.find((item) => item.key === validation.parameterKey);
+ const label = parameter?.label || validation.parameterKey;
+ return presentationText(
+ localizer,
+ validation.messageKey,
+ `请输入模板参数:${label}`,
+ { label },
+ );
+}
+
const DEFAULT_CRON = '0 9 * * *';
const DEFAULT_TIMEZONE = 'Asia/Shanghai';
const DEFAULT_CLAUDE_MODEL = 'claude-sonnet-4-6';
@@ -219,14 +238,21 @@ export function validateTemplateParamValues(
template: Pick<RemoteScheduleTemplate, 'parameters'>,
values: Record<string, string>,
localizer?: PresentationLocalizer,
-): string | null {
+): TemplateParamValidation | null {
for (const parameter of template.parameters ?? []) {
if (!parameter.required) continue;
if ((values[parameter.key] ?? parameter.default ?? '').trim()) continue;
const label = parameter.label || parameter.key;
- return presentationText(localizer, 'devices.automations.presentation.validation.templateParameter', `请输入模板参数:${label}`, {
- label,
- });
+ return {
+ ...scheduleDraftValidation(
+ 'prompt',
+ localizer,
+ 'devices.automations.presentation.validation.templateParameter',
+ `请输入模板参数:${label}`,
+ { label },
+ ),
+ parameterKey: parameter.key,
+ };
}
return null;
}