82 lines
3.5 KiB
TypeScript
82 lines
3.5 KiB
TypeScript
/**
|
||
* features/chat/shipping/shippingUploadPrompt.ts
|
||
* 用途:通知上传后提示语构建、文件格式识别与临时库存声明判断。
|
||
* 依赖:无;useChatShippingUploadHandler 在上传成功后调用。
|
||
* 逻辑:按提交结果生成 follow-up 提示;通知进入 Intake 后触发完整动态订单抽取。
|
||
*/
|
||
import i18n from '@/i18n';
|
||
|
||
/**
|
||
* 根据上传 API 响应 checkpoint 生成发给 Agent 的 follow-up 提示语。
|
||
*/
|
||
/**
|
||
* 根据通知提交 API 响应生成发给 Dify 的 follow-up 提示语(触发完整订单抽取链)。
|
||
*/
|
||
export function buildShippingNoticeUploadPrompt(
|
||
result: Record<string, unknown> | null | undefined,
|
||
options?: { fileCount?: number; fileName?: string },
|
||
): string {
|
||
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 (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(','),
|
||
}));
|
||
}
|
||
|
||
/**
|
||
* 判断文件是否为后端当前可受控分析的表格格式。
|
||
* 格式只是技术能力,不能据此推断文件一定是库存或订单。
|
||
*/
|
||
export function isShippingSpreadsheetFile(
|
||
file: File,
|
||
extractExt: (name: string) => string,
|
||
): boolean {
|
||
const ext = extractExt(file.name || '').toLowerCase();
|
||
return ['xlsx', 'xls', 'csv'].includes(ext);
|
||
}
|
||
|
||
/**
|
||
* 判断用户是否明确声明当前附件是库存文件。
|
||
* 仅用于临时库存分支门禁;不能从文件名、表格后缀或模型猜测推导。
|
||
*/
|
||
export function isTemporaryInventoryDeclaration(text: string): boolean {
|
||
const value = String(text || '').trim();
|
||
if (!value) return false;
|
||
return [
|
||
/这是.*库存(?:文件|表|excel|csv)?/i,
|
||
/(?:请|帮我|麻烦).*(?:分析|解析|识别).*(?:库存|stock)/i,
|
||
/(?:上传|附件|这个|该).*(?:库存文件|库存表|库存excel|库存csv)/i,
|
||
/(?:库存文件|库存表|库存excel|库存csv).*(?:分析|解析|识别|使用)/i,
|
||
/(?:this|the).*(?:is|are).*(?:inventory|stock)/i,
|
||
/(?:analyse|analyze|parse).*(?:inventory|stock)/i,
|
||
].some((pattern) => pattern.test(value));
|
||
}
|