feat(shipping-notice): 落地字段资产治理与唯一 Schema,自动建单随版本冻结
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
b7307fc573
commit
3a1dbf7ce5
144 files changed
+15167
-4480
No files matched your search
@@ -1,127 +0,0 @@
|
||||
/**
|
||||
* features/chat/shipping/shippingWorkspaceActions.spec.ts
|
||||
* 用途:锁定聊天页右侧批次工作台的业务按钮解析契约。
|
||||
* 依赖:vitest、shippingWorkspaceActions、shipping.api.types。
|
||||
* 逻辑:验证终态任务不渲染业务按钮,且非终态任务只消费后端 `ui_state.action_buttons`。
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { ShippingTaskSummary } from '../types/shipping.api.types';
|
||||
import {
|
||||
isShippingWorkspaceTerminalTask,
|
||||
resolveShippingWorkspaceActionButtons,
|
||||
} from './shippingWorkspaceActions';
|
||||
|
||||
const createTask = (overrides: Partial<ShippingTaskSummary> = {}): ShippingTaskSummary => ({
|
||||
id: 1,
|
||||
task_no: 'SN-UNIT-001',
|
||||
source_type: 'manual',
|
||||
process_status: 'matched',
|
||||
ui_state: {
|
||||
task_no: 'SN-UNIT-001',
|
||||
process_status: 'matched',
|
||||
steps: [],
|
||||
summary: {},
|
||||
action_buttons: [],
|
||||
},
|
||||
...overrides,
|
||||
});
|
||||
|
||||
describe('shippingWorkspaceActions', () => {
|
||||
it('treats uploaded/completed/closed/rejected tasks as terminal workspace tasks', () => {
|
||||
expect(isShippingWorkspaceTerminalTask(createTask({ process_status: 'uploaded' }))).toBe(true);
|
||||
expect(isShippingWorkspaceTerminalTask(createTask({ process_status: 'completed' }))).toBe(true);
|
||||
expect(isShippingWorkspaceTerminalTask(createTask({ process_status: 'closed' }))).toBe(true);
|
||||
expect(isShippingWorkspaceTerminalTask(createTask({ process_status: 'rejected' }))).toBe(true);
|
||||
expect(isShippingWorkspaceTerminalTask(createTask({ process_status: 'matched' }))).toBe(false);
|
||||
});
|
||||
|
||||
it('hides business buttons for completed tasks even if ui_state.action_buttons exists', () => {
|
||||
const task = createTask({
|
||||
process_status: 'completed',
|
||||
ui_state: {
|
||||
task_no: 'SN-UNIT-001',
|
||||
process_status: 'completed',
|
||||
steps: [],
|
||||
summary: {},
|
||||
action_buttons: [
|
||||
{
|
||||
id: 'retry_rpa',
|
||||
label: '重新获取库存',
|
||||
message: '重新获取库存',
|
||||
variant: 'primary',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
expect(resolveShippingWorkspaceActionButtons(task)).toEqual([]);
|
||||
});
|
||||
|
||||
it('hides business buttons for rejected tasks even if ui_state.action_buttons exists', () => {
|
||||
const task = createTask({
|
||||
process_status: 'rejected',
|
||||
ui_state: {
|
||||
task_no: 'SN-UNIT-001',
|
||||
process_status: 'rejected',
|
||||
steps: [],
|
||||
summary: {},
|
||||
action_buttons: [
|
||||
{
|
||||
id: 'retry_upload',
|
||||
label: '重试上传',
|
||||
message: '重试上传',
|
||||
variant: 'danger',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
expect(resolveShippingWorkspaceActionButtons(task)).toEqual([]);
|
||||
});
|
||||
|
||||
it('uses backend ui_state.action_buttons as the only business button source for non-completed tasks', () => {
|
||||
const task = createTask({
|
||||
process_status: 'waiting_inventory',
|
||||
pending_human_reason: 'rpa_exhausted',
|
||||
ui_state: {
|
||||
task_no: 'SN-UNIT-001',
|
||||
process_status: 'waiting_inventory',
|
||||
steps: [],
|
||||
summary: {},
|
||||
action_buttons: [
|
||||
{
|
||||
id: 'upload_inventory_file',
|
||||
label: '上传库存文件',
|
||||
message: '请上传库存文件',
|
||||
variant: 'primary',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
expect(resolveShippingWorkspaceActionButtons(task)).toEqual([
|
||||
{
|
||||
id: 'upload_inventory_file',
|
||||
label: '上传库存文件',
|
||||
message: '请上传库存文件',
|
||||
variant: 'primary',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('returns empty array when non-completed task has no backend action_buttons', () => {
|
||||
const task = createTask({
|
||||
process_status: 'waiting_inventory',
|
||||
pending_human_reason: 'rpa_exhausted',
|
||||
ui_state: {
|
||||
task_no: 'SN-UNIT-001',
|
||||
process_status: 'waiting_inventory',
|
||||
steps: [],
|
||||
summary: {},
|
||||
action_buttons: [],
|
||||
},
|
||||
});
|
||||
|
||||
expect(resolveShippingWorkspaceActionButtons(task)).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -1,32 +0,0 @@
|
||||
/**
|
||||
* features/chat/shipping/shippingWorkspaceActions.ts
|
||||
* 用途:集中封装聊天页右侧批次工作台的业务按钮解析规则。
|
||||
* 依赖:shipping.api.types 任务 DTO 类型。
|
||||
* 逻辑:由 ShippingBatchWorkspaceDrawer 调用;统一约束终态任务不渲染业务按钮,且非终态任务只消费后端 `ui_state.action_buttons`。
|
||||
*/
|
||||
import type { ShippingChatAction, ShippingTaskDetail, ShippingTaskSummary } from '../api/shipping.api';
|
||||
|
||||
const TERMINAL_PROCESS_STATUSES = new Set(['uploaded', 'completed', 'closed', 'rejected']);
|
||||
|
||||
/**
|
||||
* 判断工作台任务是否已进入终态。
|
||||
* 核心逻辑:终态任务只允许查看/切换,不再暴露任何后端业务动作按钮。
|
||||
* @param task - 工作台任务摘要或详情
|
||||
* @returns 是否属于终态任务
|
||||
*/
|
||||
export const isShippingWorkspaceTerminalTask = (task: ShippingTaskSummary | ShippingTaskDetail) =>
|
||||
TERMINAL_PROCESS_STATUSES.has(String(task.process_status || '').trim());
|
||||
|
||||
/**
|
||||
* 解析工作台应渲染的业务按钮列表。
|
||||
* 核心逻辑:终态任务直接返回空数组;其他任务只信任后端 `ui_state.action_buttons`,
|
||||
* 不再根据 `process_status / pending_human_reason` 在前端二次推导。
|
||||
* @param task - 工作台任务摘要或详情
|
||||
* @returns 可渲染的业务动作按钮列表
|
||||
*/
|
||||
export const resolveShippingWorkspaceActionButtons = (
|
||||
task: ShippingTaskSummary | ShippingTaskDetail,
|
||||
): ShippingChatAction[] => {
|
||||
if (isShippingWorkspaceTerminalTask(task)) return [];
|
||||
return Array.isArray(task.ui_state?.action_buttons) ? task.ui_state.action_buttons : [];
|
||||
};
|
||||
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* useChatShippingRefresh.spec.ts
|
||||
* 用途:验证发货动作按钮刷新时的任务定位优先级与 focus 切换刷新保护。
|
||||
* 用途:验证发货 UI State 刷新只同步通知态,不再把 action_buttons 映射为消息按钮。
|
||||
* 依赖:vitest、vue refs、useChatShippingRefresh 纯函数导出。
|
||||
* 逻辑:覆盖任务编号解析优先级,以及批次 focus 变化后应先锁定新 task 再拉取按钮。
|
||||
* 逻辑:覆盖任务编号解析优先级,以及刷新后 shipping_actions 始终清空。
|
||||
*/
|
||||
import { computed, ref } from 'vue';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
@@ -39,7 +39,7 @@ describe('useChatShippingRefresh', () => {
|
||||
})).toBe('');
|
||||
});
|
||||
|
||||
it('locks onto the latest focus task before refreshing chat buttons', async () => {
|
||||
it('syncs ui_state for notifications without attaching chat action buttons', async () => {
|
||||
getWorkspaceShippingTaskUiState.mockResolvedValue({
|
||||
task_no: 'SN-FOCUS-B',
|
||||
batch_no: 'SB-FOCUS-001',
|
||||
@@ -54,6 +54,7 @@ describe('useChatShippingRefresh', () => {
|
||||
title: '可生成发货文件',
|
||||
message: '库存匹配已满足发货条件,可以生成发货文件。',
|
||||
error_message: null,
|
||||
entry_surface: 'task_workbench',
|
||||
},
|
||||
action_buttons: [
|
||||
{
|
||||
@@ -94,13 +95,12 @@ describe('useChatShippingRefresh', () => {
|
||||
await refreshShippingActions();
|
||||
|
||||
expect(resolvedShippingTaskId.value).toBe('SN-FOCUS-B');
|
||||
expect(getWorkspaceShippingTaskUiState).toHaveBeenCalledTimes(1);
|
||||
expect(getWorkspaceShippingTaskUiState).toHaveBeenCalledWith('SN-FOCUS-B');
|
||||
expect(messages.value[0].shipping_ui_state?.task_no).toBe('SN-FOCUS-B');
|
||||
expect(messages.value[0].shipping_actions?.map((item) => item.id)).toEqual(['generate_excel']);
|
||||
expect(messages.value[0].shipping_actions).toBeUndefined();
|
||||
});
|
||||
|
||||
it('treats backend ui_state.action_buttons as the only button source when api refresh succeeds', async () => {
|
||||
it('clears legacy shipping_actions even when api returns action_buttons', async () => {
|
||||
getWorkspaceShippingTaskUiState.mockResolvedValue({
|
||||
task_no: 'SN-SOURCE-001',
|
||||
batch_no: 'SB-SOURCE-001',
|
||||
@@ -163,8 +163,8 @@ describe('useChatShippingRefresh', () => {
|
||||
|
||||
await refreshShippingActions();
|
||||
|
||||
expect(messages.value[0].shipping_actions?.map((item) => item.id)).toEqual(['download_excel']);
|
||||
expect(messages.value[0].shipping_ui_state?.action_buttons?.map((item) => item.id)).toEqual(['download_excel']);
|
||||
expect(messages.value[0].shipping_actions).toBeUndefined();
|
||||
expect(messages.value[0].shipping_ui_state?.summary?.title).toBe('可生成发货文件');
|
||||
});
|
||||
|
||||
it('does not guess a task id from history metadata when no explicit anchor exists', async () => {
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
/**
|
||||
* features/chat/shipping/useChatShippingRefresh.ts
|
||||
* 用途:刷新聊天区发货通知 UI State,把后端状态机 action_buttons 同步到消息气泡。
|
||||
* 用途:刷新聊天区发货通知 UI State,同步后端 summary/checkpoint 到消息,供通知展示。
|
||||
* 依赖:shipping.api、shippingTaskId、shippingNoticeActions 常量、chat.types。
|
||||
* 逻辑:由 useChatShipping 调用本文件;本文件只读取后端 /ui-state,不再从会话标题猜任务号或从 Dify 正文解析按钮。
|
||||
* 逻辑:由 useChatShipping 调用本文件;只读取后端 /ui-state 并净化正文中的动作菜单文案,不再把 action_buttons 映射为消息按钮。
|
||||
*/
|
||||
import { getWorkspaceShippingTaskUiState } from '@/features/chat/api/shipping.api';
|
||||
import { SHIPPING_TERMINAL_CHECKPOINTS } from '@/constants/shippingNoticeActions';
|
||||
import { findLatestShippingTaskId } from './shippingTaskId';
|
||||
import { stripShippingActionListByButtons, stripShippingPlainActionMenu } from './shippingText';
|
||||
import type { ChatMessage } from '../types/chat.types';
|
||||
import type { ShippingActionButton } from '../types/shipping.types';
|
||||
import type { ShippingTaskUiState } from '../types/shipping.api.types';
|
||||
import type { ComputedRef, Ref } from 'vue';
|
||||
|
||||
@@ -27,7 +26,7 @@ export interface ShippingRefreshContext {
|
||||
* 解析当前聊天页最可靠的发货任务编号。
|
||||
*
|
||||
* 核心逻辑:优先使用批次当前焦点任务,其次才回退到消息内嵌任务号、已解析状态和路由参数;
|
||||
* 禁止再按 conversation 标题“猜”一票,避免多票会话把动作按钮挂错任务。
|
||||
* 禁止再按 conversation 标题“猜”一票,避免多票会话把状态挂错任务。
|
||||
* @param params - 当前聊天页中可用的任务定位线索
|
||||
* @returns 可信的任务编号;无法明确定位时返回空字符串
|
||||
*/
|
||||
@@ -57,11 +56,12 @@ export function assistantMessageHasLaterUserReply(msgs: Array<{ role?: string }>
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建发货通知动作按钮刷新器。
|
||||
* 创建发货通知 UI State 刷新器。
|
||||
*
|
||||
* 核心逻辑:按 taskId 拉取后端 ui_state/action_buttons,并在终态 checkpoint 清空旧按钮。
|
||||
* 核心逻辑:按 taskId 拉取后端 ui_state,写回 `shipping_ui_state` 供摘要/通知展示,
|
||||
* 并清空消息上的 `shipping_actions`(Chat 不再渲染履约写按钮)。
|
||||
* @param ctx - 发货通知聊天上下文
|
||||
* @returns 动作刷新函数
|
||||
* @returns 状态刷新函数
|
||||
*/
|
||||
export function createShippingActionsRefresher(ctx: ShippingRefreshContext) {
|
||||
let shippingActionsRequestSeq = 0;
|
||||
@@ -128,43 +128,33 @@ export function createShippingActionsRefresher(ctx: ShippingRefreshContext) {
|
||||
return;
|
||||
}
|
||||
msgs.forEach((rawMsg, index) => {
|
||||
const chatMsg = rawMsg as ChatMessage & {
|
||||
shipping_actions_used?: boolean;
|
||||
shipping_actions?: ShippingActionButton[];
|
||||
};
|
||||
if (chatMsg.role !== 'assistant' || chatMsg.shipping_actions_used) {
|
||||
const chatMsg = rawMsg as ChatMessage;
|
||||
if (chatMsg.role !== 'assistant') {
|
||||
return;
|
||||
}
|
||||
if (chatMsg.type && !['text', undefined].includes(chatMsg.type)) {
|
||||
chatMsg.shipping_actions = undefined;
|
||||
// Chat 只保留通知态:始终清空履约按钮,仅同步最新 ui_state summary。
|
||||
chatMsg.shipping_actions = undefined;
|
||||
if (chatMsg.type && !['text', undefined, 'shipping-match-result'].includes(chatMsg.type)) {
|
||||
return;
|
||||
}
|
||||
if (assistantMessageHasLaterUserReply(msgs, index) && index !== targetAssistantIndex) {
|
||||
chatMsg.shipping_actions = undefined;
|
||||
return;
|
||||
}
|
||||
let buttons: ShippingActionButton[] = [];
|
||||
let cleanedText = stripShippingPlainActionMenu(String(chatMsg.content || ''));
|
||||
if (index === targetAssistantIndex) {
|
||||
if (apiState?.action_buttons?.length) {
|
||||
buttons = apiState.action_buttons;
|
||||
chatMsg.shipping_ui_state = apiState;
|
||||
} else if (!apiResolved && chatMsg.shipping_ui_state?.action_buttons?.length) {
|
||||
buttons = chatMsg.shipping_ui_state.action_buttons;
|
||||
if (index === targetAssistantIndex && apiState) {
|
||||
chatMsg.shipping_ui_state = apiState;
|
||||
if (apiState.action_buttons?.length) {
|
||||
cleanedText = stripShippingActionListByButtons(cleanedText, apiState.action_buttons);
|
||||
}
|
||||
if (buttons.length) {
|
||||
cleanedText = stripShippingActionListByButtons(cleanedText, buttons);
|
||||
}
|
||||
} else {
|
||||
cleanedText = stripShippingPlainActionMenu(String(chatMsg.content || ''));
|
||||
} else if (!apiResolved && chatMsg.shipping_ui_state?.action_buttons?.length) {
|
||||
cleanedText = stripShippingActionListByButtons(
|
||||
cleanedText,
|
||||
chatMsg.shipping_ui_state.action_buttons,
|
||||
);
|
||||
}
|
||||
if (cleanedText !== String(chatMsg.content || '').trim()) {
|
||||
chatMsg.content = cleanedText;
|
||||
}
|
||||
if (chatMsg.type === 'shipping-match-result' && chatMsg.payload && typeof chatMsg.payload === 'object') {
|
||||
(chatMsg.payload as { action_buttons?: ShippingActionButton[] }).action_buttons = buttons;
|
||||
(chatMsg.payload as { action_buttons?: unknown[] }).action_buttons = [];
|
||||
}
|
||||
chatMsg.shipping_actions = buttons.length ? buttons : undefined;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* features/chat/shipping/useShippingOrderExtractionRuns.ts
|
||||
* 用途:把货代订单抽取作为独立 Run 启动、轮询并投影到聊天状态消息。
|
||||
* 依赖:shipping.api Run 接口、chat.types、Vue refs。
|
||||
* 逻辑:发送处理器调用 startRun;本模块持久化本地消息并轮询 Run,终态后刷新 Intake、批次工作台与动作。
|
||||
* 逻辑:发送处理器调用 startRun;本模块持久化本地消息并轮询 Run;终态后仍保留 run_no 供右侧「本次处理」面板。
|
||||
*/
|
||||
import { getCurrentInstance, onBeforeUnmount, type ComputedRef, type Ref } from 'vue';
|
||||
import {
|
||||
@@ -48,6 +48,11 @@ export function mapOrderExtractionRunToMessageState(run: ShippingOrderExtraction
|
||||
reviewRequiredDocuments: Number(run.summary?.review_required_documents || 0),
|
||||
failedDocuments: Number(run.summary?.failed_documents || 0),
|
||||
candidateCount: Number(run.summary?.candidate_count || 0),
|
||||
openedExistingCount: Number(
|
||||
run.summary?.opened_existing_documents
|
||||
|| run.result?.opened_existing_count
|
||||
|| 0,
|
||||
),
|
||||
},
|
||||
currentActivity: run.current_activity
|
||||
? {
|
||||
@@ -119,15 +124,21 @@ export function useShippingOrderExtractionRuns(options: UseShippingOrderExtracti
|
||||
let pollGeneration = 0;
|
||||
const persistedSignatures = new Map<string, string>();
|
||||
|
||||
/** 停止当前 Run 短轮询;终态、切会话和组件卸载都会调用。 */
|
||||
/** 停止当前 Run 短轮询;切会话和组件卸载时再清空 activeRunNo。 */
|
||||
const stopPolling = () => {
|
||||
if (pollTimer) clearTimeout(pollTimer);
|
||||
requestController?.abort();
|
||||
requestController = null;
|
||||
pollTimer = null;
|
||||
activeRunNo = '';
|
||||
pollFailureCount = 0;
|
||||
pollGeneration += 1;
|
||||
// 注意:终态(含需复核)后仍保留 activeRunNo,供右侧「本次处理」面板继续展示。
|
||||
};
|
||||
|
||||
/** 切会话时清空活动 Run,避免串会话。 */
|
||||
const clearActiveRun = () => {
|
||||
stopPolling();
|
||||
activeRunNo = '';
|
||||
};
|
||||
|
||||
/** 网络轮询失败只更新本地提示,不得把后台业务 Run 误判为失败。 */
|
||||
@@ -421,9 +432,9 @@ export function useShippingOrderExtractionRuns(options: UseShippingOrderExtracti
|
||||
}));
|
||||
};
|
||||
|
||||
/** 刷新或进入既有会话时恢复最新 Run;活动 Run 继续轮询,终态只恢复状态卡。 */
|
||||
/** 刷新或进入既有会话时恢复最新 Run;活动 Run 继续轮询,终态只恢复状态卡并保留 run_no 供右侧面板。 */
|
||||
const restoreForConversation = async (conversationId: string): Promise<void> => {
|
||||
stopPolling();
|
||||
clearActiveRun();
|
||||
try {
|
||||
const generation = pollGeneration;
|
||||
const request = createRequestController(generation);
|
||||
@@ -431,6 +442,7 @@ export function useShippingOrderExtractionRuns(options: UseShippingOrderExtracti
|
||||
if (options.conversationId.value !== conversationId) return;
|
||||
const latest = items[0];
|
||||
if (!latest) return;
|
||||
activeRunNo = latest.run_no;
|
||||
const reviewAttachments = ACTIVE_RUN_STATUSES.has(latest.status)
|
||||
? undefined
|
||||
: await refreshTerminalFacts(latest);
|
||||
@@ -446,6 +458,15 @@ export function useShippingOrderExtractionRuns(options: UseShippingOrderExtracti
|
||||
}
|
||||
};
|
||||
|
||||
if (getCurrentInstance()) onBeforeUnmount(stopPolling);
|
||||
return { startRun, retryRun, reparseRun, restoreForConversation, stopPolling, updateCandidateReviewSnapshot, getActiveRunNo: () => activeRunNo };
|
||||
if (getCurrentInstance()) onBeforeUnmount(clearActiveRun);
|
||||
return {
|
||||
startRun,
|
||||
retryRun,
|
||||
reparseRun,
|
||||
restoreForConversation,
|
||||
stopPolling,
|
||||
clearActiveRun,
|
||||
updateCandidateReviewSnapshot,
|
||||
getActiveRunNo: () => activeRunNo,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user