feat(shipping-notice): 添加订单抽取功能,支持异步运行和管理,优化相关数据模型和处理逻辑

This commit is contained in:
hugo committed 2026-07-21 09:02:42 +08:00
1 parent 34d489a2d1
commit ce26d6d0c4
144 files changed
+12241 -4233

No files matched your search

@@ -51,8 +51,7 @@ const createContext = (
handleShippingAutoUploadFile: vi.fn(async () => undefined),
handleShippingAutoUploadNoticeBatch: vi.fn(async () => undefined),
isShippingNoticeBatchSubmitting: () => isShippingNoticeBatchSubmitting(),
markOrderExtractionStarted: vi.fn(),
rollbackOrderExtractionStarted: vi.fn(),
startOrderExtractionRun: vi.fn(async () => true),
syncShippingConversationWorkspace: vi.fn(async () => true),
refreshShippingActions: vi.fn(async () => undefined),
shippingSuggestedPrompt: computed(() => ''),
@@ -116,8 +115,7 @@ describe('useChatShippingSendHandler', () => {
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
});
it('sends parsed intakes to the existing Chatflow without requiring upload-time candidates', async () => {
const completionOrder: string[] = [];
it('creates a background Run for parsed intakes without calling the Chatflow', async () => {
const attachment = {
id: 'notice-1',
kind: 'shipping_notice' as const,
@@ -134,11 +132,6 @@ describe('useChatShippingSendHandler', () => {
hasCandidateReviewIssues: () => true,
hasSelectedNoticeCandidates: () => true,
getParsedIntakeNos: () => ['SNI-1'],
applyShippingMessageRecovery: vi.fn(async () => { completionOrder.push('recovery'); return true; }),
refreshNoticeIntakeStatuses: vi.fn(async () => { completionOrder.push('intakes'); }),
syncShippingConversationWorkspace: vi.fn(async () => { completionOrder.push('workspace'); return true; }),
clearConsumedNoticeAttachments: vi.fn(() => { completionOrder.push('clear'); }),
refreshShippingActions: vi.fn(async () => { completionOrder.push('actions'); }),
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
@@ -146,35 +139,20 @@ describe('useChatShippingSendHandler', () => {
expect(consumed).toBe(true);
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
expect(ctx.sendMessage).toHaveBeenCalledWith(
expect(ctx.startOrderExtractionRun).toHaveBeenCalledWith(
'chat.shippingOrderExtractionDefaultPrompt',
1,
'',
[],
{
shipping_intake_nos: ['SNI-1'],
display_query: 'chat.shippingOrderExtractionDefaultPrompt',
},
expect.objectContaining({
userAttachments: [{
['SNI-1'],
[{
id: 'notice-1',
name: 'orders.eml',
size: 100,
extension: 'eml',
type: 'document',
source: 'shipping_notice',
}],
onAssistantMessageFinished: expect.any(Function),
}),
}],
);
expect(ctx.markOrderExtractionStarted).toHaveBeenCalledWith(['SNI-1']);
const hooks = vi.mocked(ctx.sendMessage).mock.calls[0]?.[5];
const assistantMessage = { role: 'assistant', content: '已打开已有任务' } as const;
await hooks?.onAssistantMessageFinished?.(assistantMessage, []);
expect(ctx.applyShippingMessageRecovery).toHaveBeenCalledWith(assistantMessage);
expect(completionOrder).toEqual(['recovery', 'intakes', 'workspace', 'clear', 'actions']);
expect(ctx.syncShippingConversationWorkspace).toHaveBeenCalledWith({ preferLatest: true });
expect(ctx.sendMessage).not.toHaveBeenCalled();
expect(ctx.attachments.value).toEqual([]);
});
it('uses the user query for full extraction even when no candidate exists before send', async () => {
@@ -201,33 +179,21 @@ describe('useChatShippingSendHandler', () => {
expect(consumed).toBe(true);
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
expect(ctx.sendMessage).toHaveBeenCalledWith(
expect(ctx.startOrderExtractionRun).toHaveBeenCalledWith(
'请解析这些附件',
1,
'',
[],
{
shipping_intake_nos: ['SNI-1'],
display_query: '请解析这些附件',
},
expect.objectContaining({
userAttachments: [{
['SNI-1'],
[{
id: 'notice-1',
name: 'orders.eml',
size: 100,
extension: 'eml',
type: 'document',
source: 'shipping_notice',
}],
onAssistantMessageFinished: expect.any(Function),
}),
}],
);
});
it('retries the conversation workspace once when the first refresh fails', async () => {
const syncWorkspace = vi.fn()
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(true);
it('keeps attachments when Run creation reports not started', async () => {
const ctx = createContext({
attachments: ref([{
id: 'notice-retry',
@@ -241,22 +207,15 @@ describe('useChatShippingSendHandler', () => {
intakeNo: 'SNI-RETRY',
}]),
getParsedIntakeNos: () => ['SNI-RETRY'],
syncShippingConversationWorkspace: syncWorkspace,
startOrderExtractionRun: vi.fn(async () => false),
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
const hooks = vi.mocked(ctx.sendMessage).mock.calls[0]?.[5];
await hooks?.onAssistantMessageFinished?.({} as never, []);
expect(syncWorkspace).toHaveBeenCalledTimes(2);
expect(syncWorkspace).toHaveBeenNthCalledWith(1, { preferLatest: true });
expect(syncWorkspace).toHaveBeenNthCalledWith(2, { preferLatest: true });
expect(ctx.refreshShippingActions).toHaveBeenCalledTimes(1);
expect(ctx.attachments.value).toHaveLength(1);
});
it('rolls back the local extracting state when the chat request fails', async () => {
const rollback = vi.fn();
it('keeps attachments and reports the error when Run creation fails', async () => {
const ctx = createContext({
attachments: ref([{
id: 'notice-failed-send',
@@ -270,20 +229,18 @@ describe('useChatShippingSendHandler', () => {
intakeNo: 'SNI-FAILED-SEND',
}]),
getParsedIntakeNos: () => ['SNI-FAILED-SEND'],
sendMessage: vi.fn(async () => { throw new Error('network failed'); }),
rollbackOrderExtractionStarted: rollback,
startOrderExtractionRun: vi.fn(async () => { throw new Error('network failed'); }),
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(rollback).toHaveBeenCalledWith(['SNI-FAILED-SEND']);
expect(ctx.msg.warning).toHaveBeenCalledWith('network failed');
expect(ctx.attachments.value).toHaveLength(1);
});
it('rolls back extraction state when sendMessage reports a handled stream failure', async () => {
const rollback = vi.fn();
it('does not call the Chatflow when Run creation is handled as unavailable', async () => {
const ctx = createContext({
attachments: ref([{
id: 'notice-handled-failure',
@@ -297,19 +254,18 @@ describe('useChatShippingSendHandler', () => {
intakeNo: 'SNI-HANDLED-FAILURE',
}]),
getParsedIntakeNos: () => ['SNI-HANDLED-FAILURE'],
sendMessage: vi.fn(async () => ({ accepted: false, completed: false })),
rollbackOrderExtractionStarted: rollback,
startOrderExtractionRun: vi.fn(async () => false),
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(rollback).toHaveBeenCalledWith(['SNI-HANDLED-FAILURE']);
expect(ctx.loadHistory).not.toHaveBeenCalled();
expect(ctx.sendMessage).not.toHaveBeenCalled();
expect(ctx.attachments.value).toHaveLength(1);
});
it('removes submitted notice attachments after acceptance even when the assistant stream fails', async () => {
it('removes only the Intake set accepted by the background Run', async () => {
const submitted = {
id: 'notice-accepted-error',
kind: 'shipping_notice' as const,
@@ -330,15 +286,14 @@ describe('useChatShippingSendHandler', () => {
const ctx = createContext({
attachments: ref([submitted, retained]),
getParsedIntakeNos: () => ['SNI-ACCEPTED-ERROR'],
sendMessage: vi.fn(async () => ({ accepted: true, completed: false, chatLogId: 42 })),
startOrderExtractionRun: vi.fn(async () => true),
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(ctx.rollbackOrderExtractionStarted).not.toHaveBeenCalled();
expect(ctx.attachments.value).toEqual([retained]);
expect(ctx.loadHistory).toHaveBeenCalledOnce();
expect(ctx.sendMessage).not.toHaveBeenCalled();
});
});