From 2ea3f22349d385e7d58f2fb8f6581f366fa38545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E5=B1=85?= Date: Thu, 29 Oct 2026 14:35:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(freight):=20=E6=94=AF=E6=8C=81=E8=BF=9E?= =?UTF-8?q?=E7=BB=AD=E5=AF=B9=E8=AF=9D=E4=BF=AE=E6=AD=A3=E4=B8=8E=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=8A=A5=E4=BB=B7=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../freight-ai-comparison/export/route.ts | 24 ++++ .../freight-ai-comparison/route.ts | 22 ++++ .../custom-table/CustomTableView.test.tsx | 11 +- .../FreightAiComparisonTab.tsx | 32 ++++- .../FreightCalculationTab.tsx | 27 +++- .../FreightMaintenanceView.test.tsx | 80 +++++++++++- .../lib/freight-ai-chat-instructions.test.ts | 33 +++++ .../src/lib/freight-ai-chat-instructions.ts | 121 ++++++++++++++---- .../src/lib/freight-calculation.test.ts | 63 ++++++++- .../src/lib/freight-calculation.ts | 96 +++++++++++--- .../freight-ai-comparison.service.test.ts | 15 +++ ...freight-calculation-export.service.test.ts | 8 +- .../services/freight-ai-comparison.service.ts | 29 +++-- .../freight-calculation-export.service.ts | 9 +- 14 files changed, 489 insertions(+), 81 deletions(-) diff --git a/furniture-3d-system/src/app/api/supply-chain/limit-management/freight-ai-comparison/export/route.ts b/furniture-3d-system/src/app/api/supply-chain/limit-management/freight-ai-comparison/export/route.ts index 9f04c87e..7fcaa6e3 100644 --- a/furniture-3d-system/src/app/api/supply-chain/limit-management/freight-ai-comparison/export/route.ts +++ b/furniture-3d-system/src/app/api/supply-chain/limit-management/freight-ai-comparison/export/route.ts @@ -13,6 +13,8 @@ export const runtime = 'nodejs' export const dynamic = 'force-dynamic' export const maxDuration = 300 +const MAX_MESSAGE_LENGTH = 2_000 + async function readUpload( form: FormData, key: string, @@ -78,6 +80,27 @@ export async function POST(request: NextRequest) { if (!message) { return jsonError(ErrorCode.VALIDATION_REQUIRED_FIELD, '缺少原测算需求', 400, ctx.requestId) } + let history: string[] = [] + const historyValue = String(form.get('history') ?? '').trim() + if (historyValue) { + try { + const parsedHistory = JSON.parse(historyValue) as unknown + if ( + !Array.isArray(parsedHistory) + || parsedHistory.length > 50 + || parsedHistory.some((item) => ( + typeof item !== 'string' + || item.trim().length === 0 + || item.length > MAX_MESSAGE_LENGTH + )) + ) { + throw new Error('invalid history') + } + history = parsedHistory.map((item) => item.trim()) + } catch { + return jsonError(ErrorCode.VALIDATION_INVALID_FORMAT, '对话历史格式不正确', 400, ctx.requestId) + } + } const quoteFile = form.get('quote') const requestedMode = String(form.get('mode') ?? '') const mode = requestedMode === 'EXISTING_ONLY' @@ -117,6 +140,7 @@ export async function POST(request: NextRequest) { message, orderDetailLimit: null, mode, + history, }) const { result } = analysis const workbook = XLSX.utils.book_new() diff --git a/furniture-3d-system/src/app/api/supply-chain/limit-management/freight-ai-comparison/route.ts b/furniture-3d-system/src/app/api/supply-chain/limit-management/freight-ai-comparison/route.ts index a5dd693a..d85679be 100644 --- a/furniture-3d-system/src/app/api/supply-chain/limit-management/freight-ai-comparison/route.ts +++ b/furniture-3d-system/src/app/api/supply-chain/limit-management/freight-ai-comparison/route.ts @@ -68,6 +68,27 @@ export async function POST(request: NextRequest) { if (message.length > MAX_MESSAGE_LENGTH) { return jsonError(ErrorCode.VALIDATION_OUT_OF_RANGE, '测算需求不能超过 2000 字', 400, ctx.requestId) } + let history: string[] = [] + const historyValue = String(form.get('history') ?? '').trim() + if (historyValue) { + try { + const parsedHistory = JSON.parse(historyValue) as unknown + if ( + !Array.isArray(parsedHistory) + || parsedHistory.length > 50 + || parsedHistory.some((item) => ( + typeof item !== 'string' + || item.trim().length === 0 + || item.length > MAX_MESSAGE_LENGTH + )) + ) { + throw new Error('invalid history') + } + history = parsedHistory.map((item) => item.trim()) + } catch { + return jsonError(ErrorCode.VALIDATION_INVALID_FORMAT, '对话历史格式不正确', 400, ctx.requestId) + } + } const quoteFile = form.get('quote') const requestedMode = String(form.get('mode') ?? '') const mode = requestedMode === 'EXISTING_ONLY' @@ -106,6 +127,7 @@ export async function POST(request: NextRequest) { orderBuffer: orders.buffer, message, mode, + history, }) return jsonSuccess(result, 200, ctx.requestId) }, { requireAuth: true }) diff --git a/furniture-3d-system/src/app/supply-chain/management/custom-table/CustomTableView.test.tsx b/furniture-3d-system/src/app/supply-chain/management/custom-table/CustomTableView.test.tsx index 908f854f..080ace0d 100644 --- a/furniture-3d-system/src/app/supply-chain/management/custom-table/CustomTableView.test.tsx +++ b/furniture-3d-system/src/app/supply-chain/management/custom-table/CustomTableView.test.tsx @@ -157,10 +157,10 @@ function mockLimitManagementFetch(rows: SupplyChainProductArchiveRow[]) { courierCount: 1, calculatedCourierCount: 1, regionAverages: [ - { region: '一区', regionShare: 0.214, courierCount: 1, averageCost: 10.379 }, - { region: '二区', regionShare: 0.485, courierCount: 1, averageCost: 20 }, - { region: '三区', regionShare: 0.251, courierCount: 1, averageCost: 30 }, - { region: '四区', regionShare: 0.05, courierCount: 1, averageCost: 5 }, + { region: '一区', regionShare: 0.214, coveredShare: 0.214, provinceCount: 4, courierCount: 1, averageCost: 10.379 }, + { region: '二区', regionShare: 0.485, coveredShare: 0.485, provinceCount: 10, courierCount: 1, averageCost: 20 }, + { region: '三区', regionShare: 0.251, coveredShare: 0.251, provinceCount: 10, courierCount: 1, averageCost: 30 }, + { region: '四区', regionShare: 0.05, coveredShare: 0.05, provinceCount: 5, courierCount: 1, averageCost: 5 }, ], warnings: [], couriers: [{ @@ -173,6 +173,7 @@ function mockLimitManagementFetch(rows: SupplyChainProductArchiveRow[]) { provinceCosts: [{ province: '上海', region: '一区', + provinceShare: 0.029642, fee: 12.5, chargeableWeight: 15, roundedWeight: 15, @@ -586,7 +587,7 @@ describe('限价管理', () => { expect(screen.getByText('36')).toBeInTheDocument() }) - it('大货和品牌商品展示按快递维护逻辑计算的区域及全国运费', async () => { + it('大货和品牌商品展示按快递维护逻辑计算的四区运费', async () => { mockLimitManagementFetch([ makeRow({ freightZone1: '8.5600', diff --git a/furniture-3d-system/src/app/supply-chain/management/freight-maintenance/FreightAiComparisonTab.tsx b/furniture-3d-system/src/app/supply-chain/management/freight-maintenance/FreightAiComparisonTab.tsx index b87d0275..de1efa9a 100644 --- a/furniture-3d-system/src/app/supply-chain/management/freight-maintenance/FreightAiComparisonTab.tsx +++ b/furniture-3d-system/src/app/supply-chain/management/freight-maintenance/FreightAiComparisonTab.tsx @@ -51,6 +51,7 @@ type AnalysisRequestSnapshot = { quoteFile: File | null orderFile: File message: string + history: string[] mode: FreightAiComparisonResult['mode'] } @@ -464,12 +465,14 @@ export function FreightAiComparisonTab({ const [result, setResult] = useState(null) const [loading, setLoading] = useState(false) const [exporting, setExporting] = useState(false) + const [revision, setRevision] = useState(0) const [error, setError] = useState('') const quoteInputRef = useRef(null) const orderInputRef = useRef(null) const messageScrollRef = useRef(null) const nextMessageId = useRef(2) const analysisSnapshotRef = useRef(null) + const conversationInputsRef = useRef([]) useEffect(() => { const container = messageScrollRef.current @@ -511,6 +514,8 @@ export function FreightAiComparisonTab({ setError('') setResult(null) analysisSnapshotRef.current = null + conversationInputsRef.current = [] + setRevision(0) if (kind === 'quote') { setQuoteFile(file) appendAssistantMessage(`已加入新快递报价:${file.name}。${orderFile ? '两份资料都齐了,直接告诉我仓库和新快递名称即可。' : '还需要上传待测算的订单明细。'}`) @@ -542,6 +547,7 @@ export function FreightAiComparisonTab({ const mode: FreightAiComparisonResult['mode'] = quoteFile ? 'COMPARE_NEW' : 'EXISTING_ONLY' + const history = conversationInputsRef.current.slice(-50) setLoading(true) try { @@ -550,6 +556,7 @@ export function FreightAiComparisonTab({ form.append('orders', orderFile) form.append('message', normalizedMessage) form.append('mode', mode) + if (history.length > 0) form.append('history', JSON.stringify(history)) const response = await fetch('/api/supply-chain/limit-management/freight-ai-comparison', { method: 'POST', body: form, @@ -559,10 +566,13 @@ export function FreightAiComparisonTab({ throw new Error(payload.error?.message || 'AI 比价测算失败') } setResult(payload.data.result) + conversationInputsRef.current = [...history, normalizedMessage] + setRevision(history.length + 1) analysisSnapshotRef.current = { quoteFile, orderFile, message: normalizedMessage, + history, mode, } appendAssistantMessage(payload.data.assistantReply) @@ -590,8 +600,10 @@ export function FreightAiComparisonTab({ setResult(null) setError('') setExporting(false) + setRevision(0) setMessage('') analysisSnapshotRef.current = null + conversationInputsRef.current = [] nextMessageId.current = 2 setMessages([{ id: 1, role: 'assistant', text: INITIAL_MESSAGE }]) } @@ -614,6 +626,9 @@ export function FreightAiComparisonTab({ form.append('orders', snapshot.orderFile) form.append('message', snapshot.message) form.append('mode', snapshot.mode) + if (snapshot.history.length > 0) { + form.append('history', JSON.stringify(snapshot.history)) + } const response = await fetch( '/api/supply-chain/limit-management/freight-ai-comparison/export', { method: 'POST', body: form }, @@ -649,6 +664,8 @@ export function FreightAiComparisonTab({ else setOrderFile(null) setResult(null) analysisSnapshotRef.current = null + conversationInputsRef.current = [] + setRevision(0) } return ( @@ -659,7 +676,14 @@ export function FreightAiComparisonTab({
快递策略助手 - {canOperate ? '规则引擎已连接' : '当前为只读权限'} + + + {canOperate + ? revision > 0 + ? `规则引擎已连接 · 第 ${revision} 版` + : '规则引擎已连接' + : '当前为只读权限'} +