| 1 | export type AppLocale = 'zh' | 'en' |
| 2 | |
| 3 | export type ProgressStatusKey = |
| 4 | | 'understanding' |
| 5 | | 'planning' |
| 6 | | 'preparing' |
| 7 | | 'generating' |
| 8 | | 'checking' |
| 9 | | 'retrying' |
| 10 | | 'finalizing' |
| 11 | | 'completed' |
| 12 | | 'failed' |
| 13 | | 'canceled' |
| 14 | |
| 15 | const PROGRESS_TEXT: Record<ProgressStatusKey, Record<AppLocale, string>> = { |
| 16 | understanding: { |
| 17 | zh: '理解需求', |
| 18 | en: 'Understanding request' |
| 19 | }, |
| 20 | planning: { |
| 21 | zh: '规划结构', |
| 22 | en: 'Planning structure' |
| 23 | }, |
| 24 | preparing: { |
| 25 | zh: '准备画布', |
| 26 | en: 'Preparing canvas' |
| 27 | }, |
| 28 | generating: { |
| 29 | zh: '生成页面', |
| 30 | en: 'Generating pages' |
| 31 | }, |
| 32 | checking: { |
| 33 | zh: '检查页面', |
| 34 | en: 'Checking pages' |
| 35 | }, |
| 36 | retrying: { |
| 37 | zh: '正在重试', |
| 38 | en: 'Retrying' |
| 39 | }, |
| 40 | finalizing: { |
| 41 | zh: '正在收尾', |
| 42 | en: 'Finalizing' |
| 43 | }, |
| 44 | completed: { |
| 45 | zh: '已完成', |
| 46 | en: 'Completed' |
| 47 | }, |
| 48 | failed: { |
| 49 | zh: '已失败', |
| 50 | en: 'Failed' |
| 51 | }, |
| 52 | canceled: { |
| 53 | zh: '已取消', |
| 54 | en: 'Canceled' |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | const LABEL_MAP: Array<[RegExp, ProgressStatusKey]> = [ |
| 59 | [/取消|cancel/i, 'canceled'], |
| 60 | [/失败|错误|fail|error/i, 'failed'], |
| 61 | [/重试|retry/i, 'retrying'], |
| 62 | [/完成|已生成|已更新|已创建|complete|completed|done|generated|updated|created/i, 'completed'], |
| 63 | [/检查|验证|校验|check|verif|validation/i, 'checking'], |
| 64 | [/规划|结构|大纲|整理.*大纲|plan|structur|outline/i, 'planning'], |
| 65 | [/画布|准备|本地.*就绪|canvas|prepar|ready/i, 'preparing'], |
| 66 | [/理解|分析|understand|analyz/i, 'understanding'], |
| 67 | [/生成|写入|更新|填充|generate|generating|writing|updating|filling/i, 'generating'], |
| 68 | [/收尾|finaliz/i, 'finalizing'] |
| 69 | ] |
| 70 | |
| 71 | export const normalizeLocale = (locale: AppLocale | undefined): AppLocale => |
| 72 | locale === 'en' ? 'en' : 'zh' |
| 73 | |
| 74 | export const progressText = (locale: AppLocale | undefined, key: ProgressStatusKey): string => |
| 75 | PROGRESS_TEXT[key][normalizeLocale(locale)] |
| 76 | |
| 77 | export const normalizeProgressLabel = (rawLabel: string | undefined): ProgressStatusKey => { |
| 78 | const label = (rawLabel || '').trim() |
| 79 | if (!label) return 'generating' |
| 80 | for (const [pattern, key] of LABEL_MAP) { |
| 81 | if (pattern.test(label)) return key |
| 82 | } |
| 83 | return 'generating' |
| 84 | } |
| 85 | |
| 86 | export const progressLabel = ( |
| 87 | locale: AppLocale | undefined, |
| 88 | rawLabel: string | undefined |
| 89 | ): string => progressText(locale, normalizeProgressLabel(rawLabel)) |
| 90 | |
| 91 | export const progressDisplayLabel = ( |
| 92 | locale: AppLocale | undefined, |
| 93 | rawLabel: string | undefined |
| 94 | ): string => { |
| 95 | const label = (rawLabel || '').trim() |
| 96 | if (/^(?:P\d+\b|第\s*\d+\s*页)/i.test(label)) return label |
| 97 | return progressLabel(locale, label) |
| 98 | } |
| 99 |