refactor(agent, shipping): 简化代理模型并完善订单抽取业务

- 移除 Agent 的 exposure_mode 与 internal_executor 相关逻辑,清理关联的过滤、序列化代码
- 删除废弃的 AgentRuntimeBinding 模型及其依赖
- 新增订单抽取上传批次、请求快照等数据库迁移与业务逻辑
- 修复附件完整性检测、订单抽取状态映射问题
- 更新多语言错误文案与前端单元测试配置
- 清理平台配置中不再使用的暴露模式配置
This commit is contained in:
hugo committed 2026-07-28 20:25:48 +08:00
1 parent 1f3ecb87b7
commit 61ac6403c4
159 files changed
+12883 -5099

No files matched your search

@@ -1,17 +1,11 @@
/**
* features/chat/shipping/useChatShippingSendHandler.spec.ts
* 用途:验证上传预处理与点击发送完整抽取链路,以及建批提交中的并发拦截。
* 用途:验证阶段 2 发送语义,只冻结当前输入框这批 Intake,并允许空备注启动识别。
*/
import { computed, ref } from 'vue';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createShippingSendHandlers, type ShippingSendHandlerContext } from './useChatShippingSendHandler';
const isShippingNoticeBatchSubmitting = vi.hoisted(() => vi.fn(() => false));
vi.mock('./useChatShippingUploadHandler', () => ({
isShippingNoticeBatchSubmitting,
}));
const createContext = (
overrides: Partial<ShippingSendHandlerContext> = {},
): ShippingSendHandlerContext => ({
@@ -22,14 +16,7 @@ const createContext = (
conversationId: ref('conv-send'),
history: ref([]),
input: ref(''),
attachments: ref([{
id: 'local-notice',
kind: 'local',
file: new File(['notice'], 'notice.txt', { type: 'text/plain' }),
type: 'document',
uploadStatus: 'waiting',
uploadProgress: 0,
}]),
attachments: ref([]),
shippingAutoFlowRequested: ref(true),
resolvedShippingTaskId: ref(''),
getActiveShippingBatchNo: () => '',
@@ -39,7 +26,7 @@ const createContext = (
getActiveShippingTaskId: () => '',
lastAssistantMessageContent: () => '',
lastAssistantShippingActions: () => [],
sendMessage: vi.fn(async () => true),
sendMessage: vi.fn(async () => ({ ok: true } as never)),
buildFileSpecsForSend: vi.fn(async () => []),
loadHistory: vi.fn(async () => undefined),
sendShippingMenuMessage: vi.fn(async () => undefined),
@@ -50,9 +37,17 @@ const createContext = (
handleShippingMenuAction: vi.fn(async () => undefined),
handleShippingAutoUploadFile: vi.fn(async () => undefined),
handleShippingAutoUploadNoticeBatch: vi.fn(async () => undefined),
isShippingNoticeBatchSubmitting: () => isShippingNoticeBatchSubmitting(),
markOrderExtractionStarted: vi.fn(),
rollbackOrderExtractionStarted: vi.fn(),
isShippingNoticeBatchSubmitting: () => false,
hasBlockingNoticeIntake: () => false,
hasNoticeIntakeFailures: () => false,
hasBlockingNoticeDuplicates: () => false,
hasCandidateReviewIssues: () => false,
hasSelectedNoticeCandidates: () => false,
openShippingDuplicateDialog: vi.fn(),
getParsedIntakeNos: () => [],
startOrderExtractionRun: vi.fn(async () => true),
applyShippingMessageRecovery: vi.fn(async () => false),
applyShippingBatchSnapshot: vi.fn(() => false),
syncShippingConversationWorkspace: vi.fn(async () => true),
refreshShippingActions: vi.fn(async () => undefined),
shippingSuggestedPrompt: computed(() => ''),
@@ -65,211 +60,158 @@ const createContext = (
describe('useChatShippingSendHandler', () => {
beforeEach(() => {
isShippingNoticeBatchSubmitting.mockReset();
isShippingNoticeBatchSubmitting.mockReturnValue(false);
vi.clearAllMocks();
});
it('consumes send while notice batch is submitting and does not call sendMessage', async () => {
isShippingNoticeBatchSubmitting.mockReturnValue(true);
const ctx = createContext();
it('blocks send while current notice attachments are still previewing', async () => {
const ctx = createContext({
attachments: ref([{
id: 'notice-waiting',
kind: 'shipping_notice',
type: 'document',
uploadStatus: 'uploaded',
parseStatus: 'parsing',
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-WAITING',
}]),
hasBlockingNoticeIntake: () => true,
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(ctx.sendMessage).not.toHaveBeenCalled();
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
expect(ctx.msg.info).toHaveBeenCalledWith('chat.shippingNoticeBatchSubmitting');
expect(ctx.startOrderExtractionRun).not.toHaveBeenCalled();
expect(ctx.msg.info).toHaveBeenCalledWith('chat.shippingNoticeWaitingForParse');
});
it('sends parsed intakes to the existing Chatflow without requiring upload-time candidates', async () => {
const completionOrder: string[] = [];
const attachment = {
id: 'notice-1',
it('starts extraction only for the current parsed intake batch and removes only submitted notice attachments', async () => {
const currentNotice = {
id: 'notice-current',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
orderExtractionStatus: 'not_started' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
originalFilename: 'orders-current.eml',
fileSize: 100,
intakeNo: 'SNI-1',
intakeNo: 'SNI-CURRENT',
uploadBatchId: 'batch-current',
};
const ctx = createContext({
attachments: ref([attachment]),
hasCandidateReviewIssues: () => true,
hasSelectedNoticeCandidates: () => true,
getParsedIntakeNos: () => ['SNI-1'],
applyShippingBatchSnapshot: vi.fn(() => { completionOrder.push('snapshot'); 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);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
expect(ctx.sendMessage).toHaveBeenCalledWith(
'chat.shippingOrderExtractionDefaultPrompt',
1,
'',
[],
{
shipping_intake_nos: ['SNI-1'],
display_query: 'chat.shippingOrderExtractionDefaultPrompt',
},
expect.objectContaining({
userAttachments: [{
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.applyShippingBatchSnapshot).toHaveBeenCalledWith(assistantMessage);
expect(completionOrder).toEqual(['snapshot', 'intakes', 'workspace', 'clear', 'actions']);
expect(ctx.syncShippingConversationWorkspace).toHaveBeenCalledWith({ preferLatest: true });
});
it('uses the user query for full extraction even when no candidate exists before send', async () => {
const attachment = {
id: 'notice-1',
const laterNotice = {
id: 'notice-later',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
orderExtractionStatus: 'not_started' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-1',
originalFilename: 'orders-later.eml',
fileSize: 120,
intakeNo: 'SNI-LATER',
uploadBatchId: 'batch-later',
};
const otherAttachment = {
id: 'local-note',
kind: 'local' as const,
file: new File(['inventory'], 'inventory.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }),
type: 'document' as const,
uploadStatus: 'waiting' as const,
uploadProgress: 0,
};
const attachments = ref([currentNotice, laterNotice, otherAttachment]);
const startOrderExtractionRun = vi.fn(async () => true);
const ctx = createContext({
attachments: ref([attachment]),
hasCandidateReviewIssues: () => false,
hasSelectedNoticeCandidates: () => false,
getParsedIntakeNos: () => ['SNI-1'],
attachments,
getParsedIntakeNos: () => ['SNI-CURRENT'],
startOrderExtractionRun,
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '请解析这些附件', '请解析这些附件');
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '请优先处理当前批次', '请优先处理当前批次');
expect(consumed).toBe(true);
expect(ctx.handleShippingAutoUploadNoticeBatch).not.toHaveBeenCalled();
expect(ctx.sendMessage).toHaveBeenCalledWith(
'请解析这些附件',
1,
expect(startOrderExtractionRun).toHaveBeenCalledWith(
'请优先处理当前批次',
['SNI-CURRENT'],
[{
name: 'orders-current.eml',
size: 100,
extension: 'eml',
type: 'document',
source: 'shipping_notice',
}],
'batch-current',
);
expect(attachments.value).toEqual([laterNotice, otherAttachment]);
});
it('allows an empty note when the user only authorizes attachment analysis', async () => {
const startOrderExtractionRun = vi.fn(async () => true);
const ctx = createContext({
attachments: ref([{
id: 'notice-empty-note',
kind: 'shipping_notice',
type: 'document',
uploadStatus: 'uploaded',
parseStatus: 'parsed',
orderExtractionStatus: 'not_started',
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-EMPTY-NOTE',
uploadBatchId: 'batch-empty-note',
}]),
getParsedIntakeNos: () => ['SNI-EMPTY-NOTE'],
startOrderExtractionRun,
});
const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx);
const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', '');
expect(consumed).toBe(true);
expect(startOrderExtractionRun).toHaveBeenCalledWith(
'',
[],
{
shipping_intake_nos: ['SNI-1'],
display_query: '请解析这些附件',
},
expect.objectContaining({
userAttachments: [{
name: 'orders.eml',
size: 100,
extension: 'eml',
type: 'document',
source: 'shipping_notice',
}],
onAssistantMessageFinished: expect.any(Function),
}),
['SNI-EMPTY-NOTE'],
[{
name: 'orders.eml',
size: 100,
extension: 'eml',
type: 'document',
source: 'shipping_notice',
}],
'batch-empty-note',
);
});
it('retries the conversation workspace once when the first refresh fails', async () => {
const syncWorkspace = vi.fn()
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(true);
it('keeps attachments in the input when starting the run fails', async () => {
const attachment = {
id: 'notice-start-failed',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
orderExtractionStatus: 'not_started' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-START-FAILED',
};
const attachments = ref([attachment]);
const ctx = createContext({
attachments: ref([{
id: 'notice-retry',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-RETRY',
}]),
getParsedIntakeNos: () => ['SNI-RETRY'],
syncShippingConversationWorkspace: syncWorkspace,
});
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);
});
it('rolls back the local extracting state when the chat request fails', async () => {
const rollback = vi.fn();
const ctx = createContext({
attachments: ref([{
id: 'notice-failed-send',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-FAILED-SEND',
}]),
getParsedIntakeNos: () => ['SNI-FAILED-SEND'],
sendMessage: vi.fn(async () => { throw new Error('network failed'); }),
rollbackOrderExtractionStarted: rollback,
attachments,
getParsedIntakeNos: () => ['SNI-START-FAILED'],
startOrderExtractionRun: vi.fn(async () => { throw new Error('run create 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');
});
it('rolls back extraction state when sendMessage reports a handled stream failure', async () => {
const rollback = vi.fn();
const ctx = createContext({
attachments: ref([{
id: 'notice-handled-failure',
kind: 'shipping_notice' as const,
type: 'document' as const,
uploadStatus: 'uploaded' as const,
parseStatus: 'parsed' as const,
uploadProgress: 100,
originalFilename: 'orders.eml',
fileSize: 100,
intakeNo: 'SNI-HANDLED-FAILURE',
}]),
getParsedIntakeNos: () => ['SNI-HANDLED-FAILURE'],
sendMessage: vi.fn(async () => false),
rollbackOrderExtractionStarted: rollback,
});
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(attachments.value).toEqual([attachment]);
expect(ctx.msg.warning).toHaveBeenCalledWith('run create failed');
});
});