/** * features/chat/shipping/shippingSubmissionFeedback.ts * 用途:根据统一提交决策生成聊天区非阻塞系统文案与右侧短时标签。 * 依赖:OrderExtractionSubmissionResponse;不含敏感数据。 * 逻辑:由 useShippingOrderExtractionRuns / ChatMessageBubble 消费;不弹窗、不作为唯一 Toast。 */ import type { OrderExtractionSubmissionResponse } from '@/features/chat/types/shipping.api.types'; export interface ShippingSubmissionFeedback { /** 聊天区可回看系统消息正文(调用方再走 i18n 模板时也可只用 decision) */ decision: 'appended' | 'created'; fileCount: number; userTaskNo: string; runNo: string; createdNew: boolean; reasonCode?: string | null; /** 右侧任务卡短时标签码 */ badgeCode: 'just_appended' | 'just_created'; recentAppendCount?: number; } /** * 从提交响应生成用户可见反馈结构。 * @param response - 统一提交 API 响应 * @param fileCount - 本次发送附件数 */ export function buildShippingSubmissionFeedback( response: OrderExtractionSubmissionResponse, fileCount: number, ): ShippingSubmissionFeedback { const decision = response.decision === 'appended' ? 'appended' : 'created'; return { decision, fileCount: Math.max(0, Number(fileCount) || 0), userTaskNo: String(response.user_task_no || '').trim(), runNo: String(response.run_no || '').trim(), createdNew: Boolean(response.created_new), reasonCode: response.reason_code ?? null, badgeCode: decision === 'appended' ? 'just_appended' : 'just_created', recentAppendCount: decision === 'appended' ? Math.max(0, Number(fileCount) || 0) : undefined, }; } /** * 生成稳定 submission_id(同一次发送重试用同一 id)。 */ export function createShippingSubmissionId(): string { if (typeof globalThis.crypto?.randomUUID === 'function') { return `SUB-${globalThis.crypto.randomUUID()}`; } return `SUB-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`; } /** * 生成输入区短生命周期 composer_context_id。 */ export function createComposerContextId(): string { if (typeof globalThis.crypto?.randomUUID === 'function') { return `CMP-${globalThis.crypto.randomUUID()}`; } return `CMP-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`; }