|
| 1 | +import { z } from 'zod' |
| 2 | +import useTranslate from '../useTranslate' |
| 3 | +import { createOutputSchema, createSuccessResponse, createErrorResponse } from './commonSchema' |
| 4 | + |
| 5 | +// 定义为普通对象,用于传递给 inputSchema 字段 |
| 6 | +const inputSchema = z.object({ |
| 7 | + key: z.string().describe('The unique key for the i18n entry to update'), |
| 8 | + zh_CN: z.string().optional().describe('The Chinese translation text (optional, only update if provided)'), |
| 9 | + en_US: z.string().optional().describe('The English translation text (optional, only update if provided)') |
| 10 | +}) |
| 11 | + |
| 12 | +// 定义 data 部分的 Schema(用于更新的 i18n 条目数据) |
| 13 | +const updateI18nDataSchema = z.object({ |
| 14 | + key: z.string().describe('The unique key of the updated entry'), |
| 15 | + zh_CN: z.string().describe('The updated Chinese translation text'), |
| 16 | + en_US: z.string().describe('The updated English translation text'), |
| 17 | + type: z.string().describe('The type of the entry'), |
| 18 | + originalEntry: z.record(z.any()).describe('The original i18n entry before update') |
| 19 | +}) |
| 20 | + |
| 21 | +// 输出schema定义 - 使用 Zod 版本的统一输出结构 |
| 22 | +const outputSchema = createOutputSchema(updateI18nDataSchema) |
| 23 | + |
| 24 | +export const updateI18n = { |
| 25 | + name: 'update_i18n', |
| 26 | + title: '更新 I18n 词条', |
| 27 | + description: |
| 28 | + 'Update an existing i18n entry in the current TinyEngine low-code application. Use this when you need to modify internationalization translations.', |
| 29 | + inputSchema: inputSchema.shape, |
| 30 | + outputSchema: outputSchema.shape, |
| 31 | + annotations: { |
| 32 | + title: 'Update I18n Entry', |
| 33 | + readOnlyHint: false, |
| 34 | + destructiveHint: false, |
| 35 | + idempotentHint: true, |
| 36 | + openWorldHint: false |
| 37 | + }, |
| 38 | + callback: async (args: z.infer<typeof inputSchema>) => { |
| 39 | + const { key, zh_CN, en_US } = args |
| 40 | + // 验证至少有一个翻译字段 |
| 41 | + const translationValidation = z |
| 42 | + .object({ |
| 43 | + zh_CN: z.string().optional(), |
| 44 | + en_US: z.string().optional() |
| 45 | + }) |
| 46 | + .safeParse(args) |
| 47 | + |
| 48 | + if (!translationValidation.success) { |
| 49 | + // 直接返回验证错误,已经符合新的结构化格式 |
| 50 | + return createErrorResponse( |
| 51 | + 'Invalid translation fields', |
| 52 | + 'At least one translation (zh_CN or en_US) must be provided' |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + const { getLangs, ensureI18n } = useTranslate() |
| 57 | + const langs = getLangs() as Record<string, any> |
| 58 | + |
| 59 | + if (!langs[key]) { |
| 60 | + // 错误情况:key不存在 - 使用通用错误响应 |
| 61 | + return createErrorResponse('I18n key not found', `Key "${key}" does not exist in the i18n dictionary`) |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + // Get existing translations |
| 66 | + const existingEntry = langs[key] |
| 67 | + |
| 68 | + // Update with new translations, keeping existing values for ones not provided |
| 69 | + const updatedEntry = { |
| 70 | + key, |
| 71 | + zh_CN: zh_CN || existingEntry.zh_CN, |
| 72 | + en_US: en_US || existingEntry.en_US, |
| 73 | + type: existingEntry.type |
| 74 | + } |
| 75 | + |
| 76 | + await ensureI18n(updatedEntry, true) |
| 77 | + |
| 78 | + // 成功情况 - 使用通用成功响应 |
| 79 | + return createSuccessResponse('I18n entry updated successfully', { |
| 80 | + ...updatedEntry, |
| 81 | + originalEntry: existingEntry |
| 82 | + }) |
| 83 | + } catch (error) { |
| 84 | + // 处理执行过程中的异常 - 使用通用错误响应 |
| 85 | + const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred' |
| 86 | + return createErrorResponse('Failed to update i18n entry', errorMessage) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments