refactor(chat/shipping): 重构发货通知模块为统一UI State驱动模式
- 重构发货通知的状态与动作获取逻辑:移除从聊天文本解析HXOPS_SHIPPING_ACTIONS注释的旧逻辑,改为统一从后端shipping_ui_state获取任务状态、动作按钮与工作流步骤 - 新增SSE实时推送shipping_ui_state事件,支持前端即时更新聊天气泡与工作台的状态与按钮 - 优化路由与登录流程:调整根路径重定向逻辑,退出登录时跳转到登录页;移除chat路由不必要的props传参;将原路由守卫中的根路径判断迁移到路由配置中 - 完善库存RPA与本地导入流程:新增本地库存文件托管接口,优化RPA回调的文件落盘与任务关联逻辑 - 新增多组单元测试覆盖焦点切换、决策处理、SSE同步等核心场景 - 修复多语言显示逻辑,使用te方法判断翻译key是否存在,避免缺失翻译时的渲染异常 - 新增reparse_inventory菜单动作,支持静默重新解析库存且不追加额外聊天消息 - 重构工作流步骤渲染函数,优先使用后端返回的步骤数据, fallback到本地推导的旧逻辑 - 清理后端旧的chat-actions API接口,将相关逻辑迁移到统一的ui_state接口中
This commit is contained in:
commit
9c036c825b
39 files changed
+3114
-456
No files matched your search
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* features/chat/composables/useChat.spec.ts
|
||||
* 用途:验证聊天流对发货通知 UI State SSE 事件的消息级回写行为。
|
||||
* 依赖:vitest、useChat 导出的纯函数、chat.types。
|
||||
* 逻辑:覆盖 batch_ui_state 到 focus_task_ui_state 的投影、focus 切换时按钮跟随更新,以及已消费按钮消息的防回挂保护。
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { applyShippingUiStateToMessage } from './useChat';
|
||||
import type { ChatMessage } from '../types/chat.types';
|
||||
|
||||
const buildBatchUiStateEvent = (
|
||||
taskNo: string,
|
||||
batchNo: string,
|
||||
actionId: string,
|
||||
checkpoint = 'ready_export',
|
||||
) => ({
|
||||
event: 'shipping_ui_state',
|
||||
batch_no: batchNo,
|
||||
batch_ui_state: {
|
||||
batch_no: batchNo,
|
||||
current_focus_task_no: taskNo,
|
||||
tasks: [],
|
||||
focus_task_ui_state: {
|
||||
task_no: taskNo,
|
||||
batch_no: batchNo,
|
||||
checkpoint,
|
||||
process_status: 'matched',
|
||||
review_status: null,
|
||||
pending_human_reason: null,
|
||||
match_status: 'fulfillable',
|
||||
current_step: null,
|
||||
steps: [],
|
||||
summary: {
|
||||
title: '可生成发货文件',
|
||||
message: '库存匹配已满足发货条件,可以生成发货文件。',
|
||||
error_message: null,
|
||||
},
|
||||
action_buttons: [
|
||||
{
|
||||
id: actionId,
|
||||
label: `动作-${actionId}`,
|
||||
message: `执行 ${actionId}`,
|
||||
variant: 'primary' as const,
|
||||
},
|
||||
],
|
||||
updated_at: '2026-07-01T10:00:00+08:00',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
describe('useChat shipping_ui_state SSE', () => {
|
||||
it('hydrates focus task ui state from batch payload and does not reattach used actions', () => {
|
||||
const message: ChatMessage = {
|
||||
role: 'assistant',
|
||||
content: '当前正在处理批次任务',
|
||||
type: 'shipping-match-result',
|
||||
payload: { action_buttons: [] },
|
||||
};
|
||||
|
||||
const batchEvent = buildBatchUiStateEvent('SN-UNIT-001', 'SB-UNIT-001', 'generate_excel');
|
||||
|
||||
applyShippingUiStateToMessage(message, batchEvent);
|
||||
|
||||
expect(message.shipping_ui_state?.task_no).toBe('SN-UNIT-001');
|
||||
expect(message.shipping_actions?.map((item) => item.id)).toEqual(['generate_excel']);
|
||||
expect((message.payload as { action_buttons?: unknown[] }).action_buttons).toHaveLength(1);
|
||||
expect((message.payload as { shipping_batch_ui_state?: { batch_no?: string } }).shipping_batch_ui_state?.batch_no).toBe('SB-UNIT-001');
|
||||
|
||||
message.shipping_actions_used = true;
|
||||
message.shipping_actions = undefined;
|
||||
(message.payload as { action_buttons?: unknown[] }).action_buttons = [];
|
||||
|
||||
applyShippingUiStateToMessage(message, batchEvent);
|
||||
|
||||
expect(message.shipping_ui_state?.checkpoint).toBe('ready_export');
|
||||
expect(message.shipping_actions).toBeUndefined();
|
||||
expect((message.payload as { action_buttons?: unknown[] }).action_buttons).toEqual([]);
|
||||
});
|
||||
|
||||
it('switches buttons when batch focus task changes from A to B', () => {
|
||||
const message: ChatMessage = {
|
||||
role: 'assistant',
|
||||
content: '请继续处理当前批次',
|
||||
payload: { action_buttons: [] },
|
||||
};
|
||||
|
||||
applyShippingUiStateToMessage(message, buildBatchUiStateEvent('SN-FOCUS-A', 'SB-FOCUS-001', 'generate_excel'));
|
||||
|
||||
expect(message.shipping_ui_state?.task_no).toBe('SN-FOCUS-A');
|
||||
expect(message.shipping_actions?.map((item) => item.id)).toEqual(['generate_excel']);
|
||||
|
||||
applyShippingUiStateToMessage(message, buildBatchUiStateEvent('SN-FOCUS-B', 'SB-FOCUS-001', 'retry_upload'));
|
||||
|
||||
expect(message.shipping_ui_state?.task_no).toBe('SN-FOCUS-B');
|
||||
expect(message.shipping_actions?.map((item) => item.id)).toEqual(['retry_upload']);
|
||||
expect((message.payload as { shipping_batch_ui_state?: { current_focus_task_no?: string } }).shipping_batch_ui_state?.current_focus_task_no)
|
||||
.toBe('SN-FOCUS-B');
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@ import { useI18n } from 'vue-i18n';
|
||||
import { getAuthHeaders } from '@/shared/utils/auth';
|
||||
import { applyStreamErrorToMessage, type StreamErrorLabels } from '../utils/streamErrorMessage';
|
||||
import type { ChatMessage, ChatRun, RetrieverResource } from '../types/chat.types';
|
||||
import type { ShippingBatchUiState, ShippingTaskUiState } from '../types/shipping.api.types';
|
||||
|
||||
export interface SendMessageLifecycleHooks {
|
||||
onAssistantMessageFinished?: (message: ChatMessage, runMessages: ChatMessage[]) => void | Promise<void>;
|
||||
@@ -77,6 +78,63 @@ const addAttributionParts = (msg: ChatMessage, value: unknown) => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 解析发货通知 SSE 事件中的统一 UI State 载荷。
|
||||
*
|
||||
* 核心逻辑:优先读取单票 `ui_state`,批次事件则下钻 `batch_ui_state.focus_task_ui_state`;
|
||||
* 同时保留批次态原文,供右侧工作台和后续兼容逻辑继续消费。
|
||||
* @param data - SSE 事件 JSON
|
||||
* @returns 提炼后的单票 UI State 与原始批次 UI State
|
||||
*/
|
||||
export const resolveShippingUiStatePayload = (data: Record<string, unknown>): {
|
||||
uiState?: ShippingTaskUiState;
|
||||
batchUiState?: ShippingBatchUiState;
|
||||
} => {
|
||||
const rawUiState = data.ui_state;
|
||||
const uiState = rawUiState && typeof rawUiState === 'object'
|
||||
? rawUiState as ShippingTaskUiState
|
||||
: undefined;
|
||||
const rawBatchUiState = data.batch_ui_state;
|
||||
const batchUiState = rawBatchUiState && typeof rawBatchUiState === 'object'
|
||||
? rawBatchUiState as ShippingBatchUiState
|
||||
: undefined;
|
||||
const focusTaskUiState = batchUiState?.focus_task_ui_state && typeof batchUiState.focus_task_ui_state === 'object'
|
||||
? batchUiState.focus_task_ui_state
|
||||
: undefined;
|
||||
return {
|
||||
uiState: uiState || focusTaskUiState,
|
||||
batchUiState,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 将发货通知状态机事件同步到当前助手消息。
|
||||
*
|
||||
* 说明:shipping_ui_state 是 HXOPS 自有 SSE 事件,不依赖 Dify 正文;收到后立即挂按钮,
|
||||
* 让用户无需等待 `message_end` 才拿到状态机结果。若该消息按钮已被消费,则只更新状态快照,
|
||||
* 不重新挂出可点击按钮,避免首帧旧状态覆盖。
|
||||
*/
|
||||
export const applyShippingUiStateToMessage = (msg: ChatMessage, data: Record<string, unknown>) => {
|
||||
const { uiState, batchUiState } = resolveShippingUiStatePayload(data);
|
||||
if (!uiState) return;
|
||||
const nextActions = Array.isArray(uiState.action_buttons) && uiState.action_buttons.length
|
||||
? uiState.action_buttons
|
||||
: undefined;
|
||||
|
||||
msg.shipping_ui_state = uiState;
|
||||
if (!msg.shipping_actions_used) {
|
||||
msg.shipping_actions = nextActions;
|
||||
}
|
||||
if (msg.type === 'shipping-match-result' && msg.payload && typeof msg.payload === 'object' && !msg.shipping_actions_used) {
|
||||
(msg.payload as { action_buttons?: ChatMessage['shipping_actions'] }).action_buttons = nextActions;
|
||||
}
|
||||
msg.payload = {
|
||||
...(msg.payload && typeof msg.payload === 'object' ? msg.payload as Record<string, unknown> : {}),
|
||||
shipping_ui_state: uiState,
|
||||
...(batchUiState ? { shipping_batch_ui_state: batchUiState } : {}),
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 把 SSE 事件附带的元数据同步到当前消息对象。
|
||||
*
|
||||
@@ -360,9 +418,6 @@ export function useChat() {
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
if (ignoreFurtherStreamEvents) {
|
||||
continue;
|
||||
}
|
||||
clearLikelyCompletedStreamTimer();
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
@@ -400,7 +455,14 @@ export function useChat() {
|
||||
}
|
||||
|
||||
const eventName = String(data.event || '');
|
||||
if (eventName === 'message' || eventName === 'agent_message') {
|
||||
if (ignoreFurtherStreamEvents && eventName !== 'shipping_ui_state') {
|
||||
continue;
|
||||
}
|
||||
if (eventName === 'shipping_ui_state') {
|
||||
const botMsg = ensureBotMessage();
|
||||
if (!botMsg) continue;
|
||||
applyShippingUiStateToMessage(botMsg, data);
|
||||
} else if (eventName === 'message' || eventName === 'agent_message') {
|
||||
if (typeof data.answer === 'string' && data.answer.length > 0) {
|
||||
const botMsg = ensureBotMessage();
|
||||
if (!botMsg) continue;
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* features/chat/composables/useChatShipping.spec.ts
|
||||
* 用途:验证发货批次切换后的默认焦点解析,避免右侧工作台沿用旧批次的 focused task。
|
||||
* 依赖:vitest、useChatShipping 纯函数导出。
|
||||
* 逻辑:覆盖 current focus、process summary 回退、首票回退与无任务时清空焦点原因四种路径。
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { resolveShippingBatchFocusState } from './useChatShipping';
|
||||
|
||||
describe('useChatShipping batch focus sync', () => {
|
||||
it('prefers batch current focus task and keeps default reason', () => {
|
||||
expect(resolveShippingBatchFocusState(
|
||||
{
|
||||
current_focus_task_no: 'SN-BATCH-B2',
|
||||
process_summary: { current_task_no: 'SN-BATCH-B1' },
|
||||
},
|
||||
[{ task_no: 'SN-BATCH-B0' }],
|
||||
'当前聚焦任务',
|
||||
)).toEqual({
|
||||
taskNo: 'SN-BATCH-B2',
|
||||
focusReason: '当前聚焦任务',
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to process summary and first task when current focus is absent', () => {
|
||||
expect(resolveShippingBatchFocusState(
|
||||
{
|
||||
current_focus_task_no: '',
|
||||
process_summary: { current_task_no: 'SN-BATCH-C1' },
|
||||
},
|
||||
[{ task_no: 'SN-BATCH-C0' }],
|
||||
'当前聚焦任务',
|
||||
)).toEqual({
|
||||
taskNo: 'SN-BATCH-C1',
|
||||
focusReason: '当前聚焦任务',
|
||||
});
|
||||
|
||||
expect(resolveShippingBatchFocusState(
|
||||
{
|
||||
current_focus_task_no: '',
|
||||
process_summary: { current_task_no: '' },
|
||||
},
|
||||
[{ task_no: 'SN-BATCH-D0' }],
|
||||
'当前聚焦任务',
|
||||
)).toEqual({
|
||||
taskNo: 'SN-BATCH-D0',
|
||||
focusReason: '当前聚焦任务',
|
||||
});
|
||||
});
|
||||
|
||||
it('clears focus state when the new batch has no resolvable task', () => {
|
||||
expect(resolveShippingBatchFocusState(
|
||||
{
|
||||
current_focus_task_no: '',
|
||||
process_summary: { current_task_no: '' },
|
||||
},
|
||||
[],
|
||||
'当前聚焦任务',
|
||||
)).toEqual({
|
||||
taskNo: '',
|
||||
focusReason: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -25,7 +25,6 @@ import {
|
||||
createShippingParseReviewHandlers,
|
||||
createShippingSendHandlers,
|
||||
createShippingUploadHandlers,
|
||||
extractShippingActionsDirective,
|
||||
findHistorySessionForShippingTask,
|
||||
findLatestShippingTaskId,
|
||||
getShippingInventoryMappingPayload,
|
||||
@@ -42,6 +41,7 @@ import {
|
||||
getWorkspaceShippingBatch,
|
||||
getWorkspaceShippingTask,
|
||||
switchWorkspaceShippingBatchFocusTask,
|
||||
type ShippingBatchUiState,
|
||||
type ShippingBatchSummary,
|
||||
type ShippingConversationWorkspace,
|
||||
type ShippingTaskDetail,
|
||||
@@ -90,6 +90,34 @@ export interface UseChatShippingOptions {
|
||||
createNewChatView: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析批次切换后的默认焦点状态。
|
||||
* 核心逻辑:优先取状态机记录的 current focus,其次回退到 process summary 当前任务,再回退到任务列表首票;只有拿到 task_no 才回填默认聚焦原因。
|
||||
* @param batchSummary - 当前批次摘要
|
||||
* @param batchTasks - 当前批次任务列表
|
||||
* @param defaultReason - 批次切换后写入输入栏提示的默认焦点原因
|
||||
* @returns 供步骤条、按钮与右侧工作台共用的 focus 状态;取不到 task 时返回空焦点
|
||||
*/
|
||||
export const resolveShippingBatchFocusState = (
|
||||
batchSummary: {
|
||||
current_focus_task_no?: string | null;
|
||||
process_summary?: { current_task_no?: string | null } | null;
|
||||
} | null | undefined,
|
||||
batchTasks: Array<{ task_no?: string | null }> | null | undefined,
|
||||
defaultReason: string,
|
||||
) => {
|
||||
const taskNo = String(
|
||||
batchSummary?.current_focus_task_no
|
||||
|| batchSummary?.process_summary?.current_task_no
|
||||
|| batchTasks?.[0]?.task_no
|
||||
|| '',
|
||||
).trim();
|
||||
return {
|
||||
taskNo,
|
||||
focusReason: taskNo ? defaultReason : '',
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 组装发货通知聊天所需的状态、动作处理器与发送拦截逻辑。
|
||||
*
|
||||
@@ -142,8 +170,7 @@ export function useChatShipping(options: UseChatShippingOptions) {
|
||||
const shippingInventorySelectionTaskId = ref('');
|
||||
|
||||
const prepareMarkdownDisplayText = (text: string) => {
|
||||
const stripped = isShippingNoticeAgent.value ? extractShippingActionsDirective(text).cleanedText : text;
|
||||
return sanitizeShippingConversationText(stripReasoningPrelude(stripped), isShippingNoticeAgent.value);
|
||||
return sanitizeShippingConversationText(stripReasoningPrelude(text), isShippingNoticeAgent.value);
|
||||
};
|
||||
|
||||
// ── 任务编号状态 ──────────────────────────────────────────
|
||||
@@ -172,7 +199,7 @@ export function useChatShipping(options: UseChatShippingOptions) {
|
||||
let shippingStatusPollTimer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
const applyShippingBatchDetail = (
|
||||
detail: ShippingBatchSummary & { tasks?: ShippingTaskSummary[]; focus_task?: ShippingTaskSummary | null },
|
||||
detail: ShippingBatchUiState,
|
||||
fallbackBatchNo: string,
|
||||
) => {
|
||||
const normalizedBatchNo = detail.batch_no || fallbackBatchNo;
|
||||
@@ -200,6 +227,15 @@ export function useChatShipping(options: UseChatShippingOptions) {
|
||||
if (preferredTaskNo) {
|
||||
resolvedShippingTaskId.value = preferredTaskNo;
|
||||
}
|
||||
if (detail.focus_task && detail.focus_task_ui_state) {
|
||||
activeShippingTaskDetail.value = {
|
||||
...(activeShippingTaskDetail.value || {}),
|
||||
...detail.focus_task,
|
||||
lines: activeShippingTaskDetail.value?.lines || [],
|
||||
events: activeShippingTaskDetail.value?.events || [],
|
||||
ui_state: detail.focus_task_ui_state,
|
||||
} as ShippingTaskDetail;
|
||||
}
|
||||
};
|
||||
|
||||
const syncShippingBatchSummary = async (batchNo: string) => {
|
||||
@@ -282,12 +318,14 @@ export function useChatShipping(options: UseChatShippingOptions) {
|
||||
} else {
|
||||
await syncShippingBatchSummary(normalizedBatchNo);
|
||||
}
|
||||
const nextTaskNo = String(
|
||||
shippingBatchSummary.value?.current_focus_task_no
|
||||
|| shippingBatchSummary.value?.process_summary?.current_task_no
|
||||
|| shippingBatchTasks.value[0]?.task_no
|
||||
|| '',
|
||||
).trim();
|
||||
const nextFocusState = resolveShippingBatchFocusState(
|
||||
shippingBatchSummary.value,
|
||||
shippingBatchTasks.value,
|
||||
t('chat.shippingNoticeUi.currentFocusDefaultReason'),
|
||||
);
|
||||
const nextTaskNo = nextFocusState.taskNo;
|
||||
focusedShippingTaskNo.value = nextFocusState.taskNo;
|
||||
shippingFocusReason.value = nextFocusState.focusReason;
|
||||
if (nextTaskNo) {
|
||||
resolvedShippingTaskId.value = nextTaskNo;
|
||||
await syncActiveShippingTaskDetail(nextTaskNo);
|
||||
|
||||
Reference in new issue
Block a user