refactor(shipping-notice): 重构货代发货通知状态管理,移除废弃的process_status字段
统一货代发货通知的业务阶段为四个标准阶段:接收解析、订单复核、发货准备、上传完成 设置 ui_state 为前端唯一状态数据源,不再依赖 process_status 驱动业务流程 删除 shipping_notices 表的废弃 process_status 列及对应复合索引,新增数据库迁移脚本 替换全栈代码中所有对 process_status 的引用为生命周期、上传状态等领域字段 更新相关文档、测试用例与前端展示逻辑适配新的状态管理规则
This commit is contained in:
1 parent
6684e7181a
commit
5ef8329bd8
178 files changed
+5091
-1734
No files matched your search
@@ -24,7 +24,7 @@ vi.mock('../api/chat.api', () => ({
|
||||
|
||||
const createConversationContext = () => {
|
||||
const conversationId = ref<string | null>('conv-a');
|
||||
const messages = ref([]);
|
||||
const messages = ref<any[]>([{ role: 'assistant', content: 'old-a' }]);
|
||||
const createNewChatView = vi.fn(() => {
|
||||
conversationId.value = null;
|
||||
messages.value = [];
|
||||
@@ -44,7 +44,10 @@ const createConversationContext = () => {
|
||||
activeRunId: ref(null),
|
||||
messages,
|
||||
isThinking: ref(false),
|
||||
loadConversationState: vi.fn(),
|
||||
loadConversationState: (id, loaded) => {
|
||||
conversationId.value = id;
|
||||
messages.value = loaded;
|
||||
},
|
||||
createNewChatView,
|
||||
clearPendingNoticeBatchRetry,
|
||||
onConversationIdChanging: vi.fn(),
|
||||
@@ -152,4 +155,62 @@ describe('useChatPageConversation', () => {
|
||||
await handleShippingAutoUploadNoticeBatch([file]);
|
||||
expect(enqueueNoticeFiles).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('clears the previous conversation messages before an uncached history request resolves', async () => {
|
||||
let resolveMessages!: (value: any[]) => void;
|
||||
listChatMessages.mockImplementation(() => new Promise((resolve) => { resolveMessages = resolve; }));
|
||||
const { api, messages, conversationId } = createConversationContext();
|
||||
|
||||
const pending = api.loadConversation('conv-b', { force: true });
|
||||
|
||||
// 会话已切换但 B 尚无本地快照时,绝不能继续显示 A 的消息/订单卡。
|
||||
expect(conversationId.value).toBe('conv-b');
|
||||
expect(messages.value).toEqual([]);
|
||||
|
||||
resolveMessages([{ role: 'assistant', content: 'loaded-b' }]);
|
||||
await pending;
|
||||
expect(messages.value[0]?.content).toBe('loaded-b');
|
||||
});
|
||||
|
||||
it('restores cached messages immediately and discards a late response from an older selection', async () => {
|
||||
const pending = new Map<string, { resolve: (value: any[]) => void }>();
|
||||
listChatMessages.mockImplementation((id: string) => new Promise((resolve) => {
|
||||
pending.set(id, { resolve });
|
||||
}));
|
||||
const { api, messages, conversationId } = createConversationContext();
|
||||
|
||||
const firstB = api.loadConversation('conv-b', { force: true });
|
||||
pending.get('conv-b')!.resolve([{ role: 'assistant', content: 'cached-b' }]);
|
||||
await firstB;
|
||||
|
||||
const selectA = api.loadConversation('conv-a', { force: true });
|
||||
const returnB = api.loadConversation('conv-b', { force: true });
|
||||
expect(messages.value[0]?.content).toBe('cached-b');
|
||||
|
||||
pending.get('conv-a')!.resolve([{ role: 'assistant', content: 'late-a' }]);
|
||||
pending.get('conv-b')!.resolve([{ role: 'assistant', content: 'cached-b' }]);
|
||||
await Promise.all([selectA, returnB]);
|
||||
|
||||
expect(conversationId.value).toBe('conv-b');
|
||||
expect(messages.value[0]?.content).toBe('cached-b');
|
||||
});
|
||||
|
||||
it('keeps the rendered cached message array when revalidation is semantically unchanged', async () => {
|
||||
const pending = new Map<string, { resolve: (value: any[]) => void }>();
|
||||
listChatMessages.mockImplementation((id: string) => new Promise((resolve) => {
|
||||
pending.set(id, { resolve });
|
||||
}));
|
||||
const { api, messages } = createConversationContext();
|
||||
|
||||
const initial = api.loadConversation('conv-b', { force: true });
|
||||
pending.get('conv-b')!.resolve([{ chat_log_id: 91, role: 'assistant', content: 'stable-b' }]);
|
||||
await initial;
|
||||
|
||||
const revalidate = api.loadConversation('conv-b', { force: true });
|
||||
const renderedCache = messages.value;
|
||||
pending.get('conv-b')!.resolve([{ chat_log_id: 91, role: 'assistant', content: 'stable-b' }]);
|
||||
await revalidate;
|
||||
|
||||
expect(messages.value).toBe(renderedCache);
|
||||
});
|
||||
});
|
||||
@@ -37,6 +37,30 @@ export interface ChatPageConversationContext {
|
||||
}
|
||||
|
||||
export function createChatPageConversationApi(ctx: ChatPageConversationContext) {
|
||||
// 会话消息与发货 Projection 使用相同的 SWR 原则:仅在内存保留最近成功快照;
|
||||
// 路由切换先原子显示目标会话快照(或空白),再后台读取,不能让旧会话占据新会话首帧。
|
||||
const messageSnapshots = new Map<string, ChatMessage[]>();
|
||||
let messageRequestGeneration = 0;
|
||||
|
||||
const touchMessageSnapshot = (id: string, messages: ChatMessage[]) => {
|
||||
messageSnapshots.delete(id);
|
||||
messageSnapshots.set(id, messages);
|
||||
while (messageSnapshots.size > 20) {
|
||||
const oldestId = messageSnapshots.keys().next().value as string | undefined;
|
||||
if (!oldestId) break;
|
||||
messageSnapshots.delete(oldestId);
|
||||
}
|
||||
};
|
||||
|
||||
const messageSemanticKey = (items: ChatMessage[]) => JSON.stringify(items.map((item) => ({
|
||||
id: item.chat_log_id ?? item.local_id ?? null,
|
||||
role: item.role,
|
||||
type: item.type,
|
||||
content: item.content,
|
||||
payload: item.payload ?? null,
|
||||
attachments: item.attachments ?? [],
|
||||
})));
|
||||
|
||||
const loadConversation = async (id: string, options?: { force?: boolean }) => {
|
||||
const force = options?.force ?? false;
|
||||
if (!force && ctx.conversationId.value === id && !ctx.activeRunId.value && ctx.messages.value.length > 0) {
|
||||
@@ -44,6 +68,8 @@ export function createChatPageConversationApi(ctx: ChatPageConversationContext)
|
||||
}
|
||||
|
||||
const prevConversationId = ctx.conversationId.value;
|
||||
const generation = ++messageRequestGeneration;
|
||||
const cachedMessages = messageSnapshots.get(id);
|
||||
try {
|
||||
/**
|
||||
* 切换会话前先清空输入框草稿,避免 textarea DOM 里残留上一会话的可见文本,
|
||||
@@ -51,9 +77,19 @@ export function createChatPageConversationApi(ctx: ChatPageConversationContext)
|
||||
*/
|
||||
ctx.input.value = '';
|
||||
ctx.onConversationIdChanging(id, prevConversationId);
|
||||
// [STEP 1] 先切换消息容器:未访问会话渲染空白而非上一会话;
|
||||
// 已访问会话复用自身快照,避免“缓存卡先出现后整页闪一下”。
|
||||
ctx.loadConversationState(id, cachedMessages ? [...cachedMessages] : []);
|
||||
const loaded = await listChatMessages(id, ctx.currentAgentId.value);
|
||||
ctx.loadConversationState(id, Array.isArray(loaded) ? loaded : []);
|
||||
if (!loaded?.length) {
|
||||
if (generation !== messageRequestGeneration || ctx.conversationId.value !== id) return;
|
||||
const nextMessages = Array.isArray(loaded) ? loaded : [];
|
||||
const currentMessages = ctx.messages.value;
|
||||
touchMessageSnapshot(id, nextMessages);
|
||||
// 同一会话后台重验没有业务变化时,保留消息数组和子卡实例,避免缓存首帧后闪一下。
|
||||
if (messageSemanticKey(currentMessages) !== messageSemanticKey(nextMessages)) {
|
||||
ctx.loadConversationState(id, nextMessages);
|
||||
}
|
||||
if (!nextMessages.length) {
|
||||
await ctx.onConversationLoadedEmpty();
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user