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

@@ -0,0 +1,422 @@
/**
* features/chat/components/shipping/ShippingOrderExtractionCard.spec.ts
* 用途:覆盖订单解析摘要卡、详情抽屉和单 PDF 结果项的关键交互。
* 依赖:Vitest、Vue Test Utils、vue-i18n、本域组件与 DTO 类型。
* 逻辑:验证按钮由后端 available_actions 驱动,详情抽屉按需加载并支持键盘关闭。
*/
import { nextTick } from 'vue';
import { mount } from '@vue/test-utils';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createI18n } from 'vue-i18n';
import chatZhCn from '@/locales/zh-CN/chat.json';
import ShippingOrderDocumentResultItem from './ShippingOrderDocumentResultItem.vue';
import ShippingOrderExtractionCard from './ShippingOrderExtractionCard.vue';
import ShippingOrderExtractionDetailsDrawer from './ShippingOrderExtractionDetailsDrawer.vue';
import type { ShippingOrderExtractionState } from '../../types/chat.types';
import type {
ShippingOrderExtractionDocumentResult,
ShippingOrderExtractionProgressEvent,
} from '../../types/shipping.api.types';
const api = vi.hoisted(() => ({
listDocuments: vi.fn(),
listProgressEvents: vi.fn(),
}));
vi.mock('../../api/shipping.api', () => ({
listWorkspaceShippingOrderExtractionDocuments: api.listDocuments,
listWorkspaceShippingOrderExtractionProgressEvents: api.listProgressEvents,
}));
const i18n = createI18n({
legacy: false,
locale: 'zh-CN',
messages: {
'zh-CN': {
chat: chatZhCn,
},
},
});
const flushUi = async () => {
await Promise.resolve();
await nextTick();
};
const buildState = (patch: Partial<ShippingOrderExtractionState> = {}): ShippingOrderExtractionState => ({
status: 'processing',
summary: {
totalDocuments: 3,
pendingDocuments: 0,
processingDocuments: 1,
completedDocuments: 2,
reviewRequiredDocuments: 0,
failedDocuments: 0,
candidateCount: 2,
},
currentActivity: {
unitId: 12,
fileName: 'PO-450003.pdf',
eventCode: 'profile_matching',
},
availableActions: ['view_details'],
terminal: false,
refreshFailed: false,
errorCode: null,
startedAt: null,
finishedAt: null,
...patch,
});
const buildDocument = (
patch: Partial<ShippingOrderExtractionDocumentResult> = {},
): ShippingOrderExtractionDocumentResult => ({
unit_id: 11,
document_id: 85,
file_name: 'PO-450001.pdf',
status: 'candidate_ready',
po_number: '450001',
line_count: 4,
profile: {
code: 'vendor_a_po',
name: 'Vendor A Purchase Order',
version: 2,
},
email_supplement: {
status: 'merged',
relation_codes: ['po_mpn'],
conflict_count: 0,
reservation_id_status: 'merged',
source_type_status: 'unavailable',
},
reason_code: null,
available_actions: ['view_evidence', 'review_candidate'],
...patch,
});
const buildProgressEvent = (
patch: Partial<ShippingOrderExtractionProgressEvent> = {},
): ShippingOrderExtractionProgressEvent => ({
event_code: 'expansion_wave_started',
status: 'completed',
unit_id: null,
document_id: null,
reason_code: null,
summary: {
selection_batch: 2,
reason_codes: ['required_field_missing'],
},
occurred_at: '2026-07-28T10:30:00Z',
...patch,
});
describe('ShippingOrderExtractionCard', () => {
beforeEach(() => {
vi.clearAllMocks();
document.body.innerHTML = '';
});
it('loads documents once when opening details from server actions', async () => {
api.listDocuments.mockResolvedValue({ items: [buildDocument()] });
api.listProgressEvents.mockResolvedValue({ items: [buildProgressEvent()] });
const wrapper = mount(ShippingOrderExtractionCard, {
props: {
state: buildState({
selectionPlan: {
maxInitialAttachments: 2,
maxInitialConcurrency: 2,
expansionBatchSize: 2,
maxTotalAttachments: 6,
totalAttachmentCount: 5,
eligibleAttachmentCount: 3,
selectedAttachmentCount: 2,
plannedAttachmentCount: 3,
pendingExpansionAttachmentCount: 1,
pendingExpansionBatches: { '2': 1 },
skippedAttachmentCount: 3,
duplicateAttachmentCount: 1,
budgetLimitedAttachmentCount: 1,
groupCount: 2,
},
unitSummary: {
totalUnits: 2,
selectedUnits: 2,
retryingUnits: 1,
pendingRetryUnits: 0,
failedUnits: 0,
reviewRequiredUnits: 0,
succeededUnits: 0,
selectionBatches: { '1': 2 },
reviewRequiredReasonCodes: ['required_field_missing'],
expansionTriggerReasonCodes: ['missing_reservation_id'],
technicalFailureReasonCodes: [],
},
}),
runNo: 'SNER-001',
},
global: {
plugins: [i18n],
stubs: {
teleport: true,
},
},
});
const detailButton = wrapper.findAll('button').find((item) => item.text().includes('查看处理详情'));
expect(detailButton).toBeTruthy();
await detailButton!.trigger('click');
await flushUi();
expect(api.listDocuments).toHaveBeenCalledTimes(1);
expect(api.listProgressEvents).toHaveBeenCalledTimes(1);
expect(api.listDocuments).toHaveBeenCalledWith('SNER-001');
expect(api.listProgressEvents).toHaveBeenCalledWith('SNER-001');
expect(wrapper.text()).toContain('处理详情');
expect(wrapper.text()).toContain('运行时间线');
expect(wrapper.text()).toContain('系统已开始分析下一轮候选附件。');
expect(wrapper.text()).toContain('当前批次:第 2 轮');
expect(wrapper.text()).toContain('当前原因:订单关键字段不完整。');
expect(wrapper.text()).toContain('PO-450001.pdf');
expect(wrapper.text()).toContain('本轮从 5 个附件中规划 2 个订单组');
expect(wrapper.text()).toContain('已选 2 个附件');
expect(wrapper.text()).toContain('待扩展 1 个附件');
expect(wrapper.text()).toContain('预算受限 1 个');
expect(wrapper.text()).toContain('技术重试 1 个');
expect(wrapper.text()).toContain('待复核原因:订单关键字段不完整。');
expect(wrapper.text()).toContain('扩批触发:仍缺少 Reservation 字段,需要后续附件补证。');
await detailButton!.trigger('click');
await flushUi();
expect(api.listDocuments).toHaveBeenCalledTimes(1);
expect(api.listProgressEvents).toHaveBeenCalledTimes(1);
});
it('emits manual review directly when attachments are already available', async () => {
const wrapper = mount(ShippingOrderExtractionCard, {
props: {
state: buildState({
availableActions: ['enter_manual_review'],
}),
runNo: 'SNER-002',
hasReviewAttachments: true,
},
global: {
plugins: [i18n],
stubs: {
teleport: true,
},
},
});
const reviewButton = wrapper.findAll('button').find((item) => item.text().includes('进入人工复核'));
expect(reviewButton).toBeTruthy();
await reviewButton!.trigger('click');
expect(wrapper.emitted('enterManualReview')).toHaveLength(1);
expect(api.listDocuments).not.toHaveBeenCalled();
});
it('emits retry for failed attachments without mutating state locally', async () => {
const wrapper = mount(ShippingOrderExtractionCard, {
props: {
state: buildState({
status: 'failed',
terminal: true,
summary: {
totalDocuments: 3,
pendingDocuments: 0,
processingDocuments: 0,
completedDocuments: 2,
reviewRequiredDocuments: 0,
failedDocuments: 1,
candidateCount: 2,
},
currentActivity: null,
availableActions: ['retry_failed_attachments'],
}),
runNo: 'SNER-003',
},
global: {
plugins: [i18n],
stubs: {
teleport: true,
},
},
});
const retryButton = wrapper.findAll('button').find((item) => item.text().includes('重试失败附件'));
expect(retryButton).toBeTruthy();
await retryButton!.trigger('click');
expect(wrapper.emitted('retry')).toHaveLength(1);
});
it('renders run-level activity text without forcing an unknown file label', async () => {
const wrapper = mount(ShippingOrderExtractionCard, {
props: {
state: buildState({
currentActivity: {
unitId: 0,
fileName: null,
eventCode: 'plan_created',
selectionBatch: 2,
reasonCodes: ['required_field_missing'],
},
}),
runNo: 'SNER-004',
},
global: {
plugins: [i18n],
stubs: {
teleport: true,
},
},
});
expect(wrapper.text()).toContain('识别计划已创建,正在准备首轮附件。');
expect(wrapper.text()).toContain('当前批次:第 2 轮');
expect(wrapper.text()).toContain('当前原因:订单关键字段不完整。');
expect(wrapper.text()).not.toContain('未命名 PDF');
});
});
describe('ShippingOrderExtractionDetailsDrawer', () => {
beforeEach(() => {
document.body.innerHTML = '';
});
it('closes on Escape when the drawer is open', async () => {
const wrapper = mount(ShippingOrderExtractionDetailsDrawer, {
attachTo: document.body,
props: {
open: false,
state: buildState({
status: 'partially_completed',
terminal: true,
availableActions: ['view_details', 'enter_manual_review'],
}),
documents: [buildDocument()],
progressEvents: [buildProgressEvent()],
loading: false,
loadFailed: false,
progressLoading: false,
progressLoadFailed: false,
hasReviewAttachments: true,
},
global: {
plugins: [i18n],
},
});
await wrapper.setProps({ open: true });
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }));
await nextTick();
expect(wrapper.emitted('close')).toHaveLength(1);
});
it('renders run timeline entries with batch and reason context', async () => {
const wrapper = mount(ShippingOrderExtractionDetailsDrawer, {
props: {
open: true,
state: buildState(),
documents: [buildDocument()],
progressEvents: [buildProgressEvent()],
loading: false,
loadFailed: false,
progressLoading: false,
progressLoadFailed: false,
},
global: {
plugins: [i18n],
stubs: {
teleport: true,
},
},
});
expect(wrapper.text()).toContain('运行时间线');
expect(wrapper.text()).toContain('业务复核');
expect(wrapper.text()).toContain('系统已开始分析下一轮候选附件。');
expect(wrapper.text()).toContain('当前批次:第 2 轮');
expect(wrapper.text()).toContain('当前原因:订单关键字段不完整。');
});
});
describe('ShippingOrderDocumentResultItem', () => {
beforeEach(() => {
document.body.innerHTML = '';
});
it('renders business reason and server-declared actions for a review-required PDF', async () => {
const wrapper = mount(ShippingOrderDocumentResultItem, {
props: {
document: buildDocument({
status: 'review_required',
reason_code: 'multiple_po_detected',
po_number: null,
line_count: 0,
email_supplement: {
status: 'conflict',
relation_codes: ['po_mpn'],
conflict_count: 2,
reservation_id_status: 'conflict',
source_type_status: 'none',
},
available_actions: ['view_evidence', 'manual_review'],
}),
hasReviewAttachments: true,
},
global: {
plugins: [i18n],
},
});
expect(wrapper.text()).toContain('需要人工复核');
expect(wrapper.text()).toContain('一份 PDF 中检测到多个不同 PO');
expect(wrapper.text()).toContain('正文冲突 2 项');
const actionButtons = wrapper.findAll('button');
expect(actionButtons.map((item) => item.text())).toEqual(
expect.arrayContaining(['查看证据', '人工复核']),
);
await actionButtons[0].trigger('click');
expect(wrapper.emitted('enterManualReview')).toHaveLength(1);
});
it('renders a user-safe retry hint rather than a technical failure code for a failed attachment', async () => {
const wrapper = mount(ShippingOrderDocumentResultItem, {
props: {
document: buildDocument({
status: 'failed',
reason_code: 'technical_failure',
selection_batch: 2,
group_key: 'PO-GRP-02',
retry_status: 'failed',
technical_failure_reason_code: 'SHIPPING_NOTICE.ORDER_EXTRACTION_TIMEOUT',
budget_cost: {
initial_attachment_budget: 2,
initial_concurrency_budget: 2,
selected_in_batch: 1,
},
available_actions: [],
}),
},
global: {
plugins: [i18n],
},
});
expect(wrapper.text()).toContain('选择批次:第 2 轮');
expect(wrapper.text()).toContain('订单组:PO-GRP-02');
expect(wrapper.text()).toContain('系统暂未完成处理,您可以稍后重试。');
expect(wrapper.text()).toContain('首轮预算:附件 2 个,并发 2 个,本轮已选 1 个');
expect(wrapper.text()).not.toContain('SHIPPING_NOTICE.ORDER_EXTRACTION_TIMEOUT');
});
});