- 添加单票与多票发货需求的处理方式 - 说明库存 Excel 上传后的保存与关联机制 - 解释库存匹配与 Excel 生成的独立执行方式 - 明确批次库存的真相来源 feat(chat): 优化发货库存上传提示语 - 根据上传结果生成更详细的提示信息 - 增加批次号的提示逻辑 - 使用国际化支持替换硬编码字符串 refactor(chat): 重构发货库存上传处理逻辑 - 移除多余的任务选择逻辑 - 简化上传处理流程 - 增强代码可读性与维护性 feat(locales): 更新多语言支持的提示信息 - 添加库存上传与任务处理的多语言提示 - 更新库存缺口与任务状态的描述
107 lines
4.7 KiB
TypeScript
107 lines
4.7 KiB
TypeScript
/**
|
||
* 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<string, unknown> | 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<string, unknown> | 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';
|
||
}
|