/** * features/chat/shipping/shippingUploadPrompt.ts * 用途:库存/通知上传后发给 Dify 的提示语构建与附件类型分类。 * 依赖:无;useChatShippingUploadHandler 在上传成功后调用。 * 逻辑:按 checkpoint 生成不同 follow-up 提示;通知建批后触发 Dify LLM 解析续办。 */ import i18n from '@/i18n'; /** * 根据上传 API 响应 checkpoint 生成发给 Agent 的 follow-up 提示语。 */ export function buildShippingInventoryUploadPrompt( result: Record | null | undefined, fileName?: string, ): string { const checkpoint = String(result?.checkpoint || '').trim(); const hasAlignment = Boolean(result?.data && typeof result.data === 'object' && (result.data as { field_alignment?: unknown }).field_alignment && typeof (result.data as { field_alignment?: unknown }).field_alignment === 'object'); const suggestOk = Boolean((result?.data as { suggest_ok?: boolean } | undefined)?.suggest_ok); const batchNo = String( (result?.batch_no as string | undefined) || ((result?.data as { batch_no?: string } | undefined)?.batch_no) || ((result?.data as { batch?: { batch_no?: string } } | undefined)?.batch?.batch_no) || '', ).trim(); const prefix = batchNo ? (fileName ? String(i18n.global.t('chat.shippingNoticePrompt.inventory.prefixBatchWithFile', { fileName, batchNo })) : String(i18n.global.t('chat.shippingNoticePrompt.inventory.prefixBatch', { batchNo }))) : (fileName ? String(i18n.global.t('chat.shippingNoticePrompt.inventory.prefixWithFile', { fileName })) : String(i18n.global.t('chat.shippingNoticePrompt.inventory.prefix'))); if (checkpoint === 'map_infer') { if (suggestOk) { return String(i18n.global.t('chat.shippingNoticePrompt.inventory.mapInferSuggested', { prefix })); } return String(i18n.global.t('chat.shippingNoticePrompt.inventory.mapInferPending', { prefix })); } if (checkpoint === 'align_review' || hasAlignment) { return String(i18n.global.t('chat.shippingNoticePrompt.inventory.alignReview', { prefix })); } return String(i18n.global.t('chat.shippingNoticePrompt.inventory.default', { prefix })); } /** * 根据通知建批 API 响应生成发给 Dify 的 follow-up 提示语(触发画布 LLM 抽取 + parse_notice)。 */ export function buildShippingNoticeUploadPrompt( result: Record | null | undefined, options?: { fileCount?: number; fileName?: string }, ): string { const batchNo = String( (result?.batch_no as string | undefined) || ((result?.data as { batch?: { batch_no?: string } } | undefined)?.batch?.batch_no) || '', ).trim(); const taskId = String( ((result?.data as { focus_task?: { task_no?: string } } | undefined)?.focus_task?.task_no) || (result?.task_id as string | undefined) || ((result?.data as { tasks?: Array<{ task_id?: string }> } | undefined)?.tasks?.[0]?.task_id) || '', ).trim(); const taskIds = Array.isArray((result?.data as { tasks?: unknown[] } | undefined)?.tasks) ? ((result?.data as { tasks?: Array<{ task_id?: string }> }).tasks || []) .map((task) => String(task?.task_id || '').trim()) .filter(Boolean) : []; const fileCount = Number(options?.fileCount || 0); const fileLabel = options?.fileName ? String(i18n.global.t('chat.shippingNoticePrompt.notice.fileLabelSingleFile', { fileName: options.fileName })) : (fileCount > 1 ? String(i18n.global.t('chat.shippingNoticePrompt.notice.fileLabelMulti', { fileCount })) : String(i18n.global.t('chat.shippingNoticePrompt.notice.fileLabelDefault'))); const parts = [String(i18n.global.t('chat.shippingNoticePrompt.notice.uploadedPrefix', { fileLabel }))]; if (batchNo) { parts.push(String(i18n.global.t('chat.shippingNoticePrompt.notice.batchLabel', { batchNo }))); } if (taskId) { parts.push(String(i18n.global.t('chat.shippingNoticePrompt.notice.taskLabel', { taskId }))); } if (fileCount > 1 || taskIds.length > 1) { const taskListText = taskIds.length ? String(i18n.global.t('chat.shippingNoticePrompt.notice.taskList', { taskIds: taskIds.join('、') })) : ''; return String(i18n.global.t('chat.shippingNoticePrompt.notice.multiTask', { summary: parts.join(','), taskListText, })); } return String(i18n.global.t('chat.shippingNoticePrompt.notice.singleTask', { summary: parts.join(','), })); } /** * 按扩展名判断附件是库存 Excel 还是发货通知文件。 */ export function classifyShippingUploadFile( file: File, extractExt: (name: string) => string, ): 'inventory' | 'notice' { const ext = extractExt(file.name || '').toLowerCase(); if (['xlsx', 'xls', 'csv'].includes(ext)) return 'inventory'; return 'notice'; }