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

@@ -37,6 +37,17 @@ const run = (patch: Partial<ShippingOrderExtractionRun>): ShippingOrderExtractio
intake_nos: ['SNI-1'],
schema_version_id: 1,
attempt_count: 0,
summary: {
total_documents: 1,
pending_documents: 1,
processing_documents: 0,
completed_documents: 0,
review_required_documents: 0,
failed_documents: 0,
candidate_count: 0,
},
current_activity: null,
available_actions: ['view_details'],
...patch,
});
@@ -86,7 +97,8 @@ describe('useShippingOrderExtractionRuns lifecycle', () => {
api.getRun.mockResolvedValue(queued);
const harness = createHarness();
await harness.runs.startRun('解析附件', ['SNI-1'], []);
await harness.runs.startRun('解析附件', ['SNI-1'], [], 'batch-1');
expect(api.createRun).toHaveBeenCalledWith(expect.objectContaining({ upload_batch_id: 'batch-1' }));
expect(harness.conversationId.value).toBe('conv-created');
expect(harness.options.onConversationResolved).toHaveBeenCalledWith('conv-created');
expect(harness.messages.value.map((item) => item.payload?.formId)).toEqual([
@@ -101,10 +113,42 @@ describe('useShippingOrderExtractionRuns lifecycle', () => {
harness.runs.stopPolling();
});
it('persists an attachment-only user message when the run starts without a text note', async () => {
const queued = run({ run_no: 'SNER-EMPTY-NOTE', conversation_id: 'conv-empty-note' });
api.createRun.mockResolvedValue({ run: queued, reused: false });
const harness = createHarness();
await harness.runs.startRun('', ['SNI-1'], [{
name: 'orders.eml',
size: 128,
extension: 'eml',
type: 'document',
source: 'shipping_notice',
}]);
expect(harness.messages.value[0]).toMatchObject({
role: 'user',
content: '',
attachments: [{
name: 'orders.eml',
size: 128,
extension: 'eml',
type: 'document',
source: 'shipping_notice',
}],
});
expect(harness.options.saveLocalMessageToBackend).toHaveBeenNthCalledWith(
1,
expect.objectContaining({ content: '' }),
undefined,
);
harness.runs.stopPolling();
});
it('absorbs restore failures and ignores an old conversation late response', async () => {
const pending = deferred<ShippingOrderExtractionRun>();
api.listRuns
.mockResolvedValueOnce({ items: [run({ run_no: 'SNER-OLD', conversation_id: 'conv-old', status: 'running' })] })
.mockResolvedValueOnce({ items: [run({ run_no: 'SNER-OLD', conversation_id: 'conv-old', status: 'processing' })] })
.mockRejectedValueOnce(new Error('offline'));
api.getRun.mockReturnValueOnce(pending.promise);
const harness = createHarness();
@@ -118,7 +162,7 @@ describe('useShippingOrderExtractionRuns lifecycle', () => {
await Promise.resolve();
expect(harness.runs.getActiveRunNo()).toBe('');
expect(harness.messages.value).toHaveLength(1);
expect(harness.messages.value[0]?.shipping_order_extraction_state?.status).toBe('extracting');
expect(harness.messages.value[0]?.shipping_order_extraction_state?.status).toBe('processing');
});
it('aborts a hanging Run query after ten seconds and keeps polling as a transport failure', async () => {
@@ -132,7 +176,7 @@ describe('useShippingOrderExtractionRuns lifecycle', () => {
await vi.advanceTimersByTimeAsync(11000);
expect(api.getRun).toHaveBeenCalledWith('SNER-TIMEOUT', expect.any(AbortSignal));
expect(harness.runs.getActiveRunNo()).toBe('SNER-TIMEOUT');
expect(harness.messages.value[1]?.shipping_order_extraction_state?.status).toBe('extracting');
expect(harness.messages.value[1]?.shipping_order_extraction_state?.status).toBe('queued');
harness.runs.stopPolling();
});
@@ -151,7 +195,7 @@ describe('useShippingOrderExtractionRuns lifecycle', () => {
expect(api.retryRun).toHaveBeenCalledWith('SNER-RETRY');
expect(harness.options.refreshNoticeIntakeStatuses).toHaveBeenCalledTimes(2);
expect(harness.messages.value).toHaveLength(1);
expect(harness.messages.value[0]?.shipping_order_extraction_state?.status).toBe('extracting');
expect(harness.messages.value[0]?.shipping_order_extraction_state?.status).toBe('queued');
expect(harness.runs.getActiveRunNo()).toBe('SNER-RETRY');
harness.runs.stopPolling();
});
@@ -167,7 +211,7 @@ describe('useShippingOrderExtractionRuns lifecycle', () => {
});
it('creates a new Run for reidentification and switches polling to the new version', async () => {
const source = run({ run_no: 'SNER-SOURCE', status: 'needs_review' });
const source = run({ run_no: 'SNER-SOURCE', status: 'review_required' });
const replacement = run({
run_no: 'SNER-REPLACEMENT', status: 'queued',
reparse: { source_run_no: 'SNER-SOURCE', reason: 'field_mapping_error' },
@@ -190,34 +234,75 @@ describe('useShippingOrderExtractionRuns lifecycle', () => {
describe('mapOrderExtractionRunToMessageState', () => {
it('maps queued/running to indeterminate extracting state', () => {
it('maps queued and processing to the new Run summary state', () => {
expect(mapOrderExtractionRunToMessageState(run({
status: 'running',
phase: 'calling_provider',
progress: {
version: 3,
current_stage_code: 'document_processing',
health: 'normal',
last_activity_at: '2026-07-21T03:24:51Z',
status: 'processing',
selection_plan: {
max_initial_attachments: 2,
max_initial_concurrency: 2,
expansion_batch_size: 2,
max_total_attachments: 6,
total_attachment_count: 5,
eligible_attachment_count: 4,
selected_attachment_count: 2,
planned_attachment_count: 4,
pending_expansion_attachment_count: 2,
pending_expansion_batches: { '2': 2 },
skipped_attachment_count: 1,
duplicate_attachment_count: 1,
budget_limited_attachment_count: 0,
group_count: 2,
},
current_activity: { unit_id: 7, file_name: 'PO-1.pdf', event_code: 'profile_matching' },
summary: {
total_documents: 3,
pending_documents: 0,
processing_documents: 1,
completed_documents: 2,
review_required_documents: 0,
failed_documents: 0,
candidate_count: 2,
},
}))).toMatchObject({
status: 'extracting', terminal: false, phase: 'llm_extraction',
current_stage_code: 'document_processing', health: 'normal',
last_activity_at: '2026-07-21T03:24:51Z',
status: 'processing',
terminal: false,
summary: {
totalDocuments: 3,
processingDocuments: 1,
completedDocuments: 2,
candidateCount: 2,
},
selectionPlan: {
expansionBatchSize: 2,
maxTotalAttachments: 6,
plannedAttachmentCount: 4,
pendingExpansionAttachmentCount: 2,
pendingExpansionBatches: { '2': 2 },
},
currentActivity: { unitId: 7, fileName: 'PO-1.pdf', eventCode: 'profile_matching' },
});
});
it('persists a changed progress projection even when Run status and result are unchanged', async () => {
it('persists a changed summary projection even when Run status is unchanged', async () => {
vi.useFakeTimers();
const first = run({
run_no: 'SNER-PROGRESS',
status: 'running',
progress: { version: 1, current_stage_code: 'workflow_accepted' },
status: 'processing',
current_activity: { unit_id: 1, file_name: 'PO-A.pdf', event_code: 'document_queued' },
});
const second = run({
run_no: 'SNER-PROGRESS',
status: 'running',
progress: { version: 2, current_stage_code: 'order_recognition' },
status: 'processing',
current_activity: { unit_id: 1, file_name: 'PO-A.pdf', event_code: 'profile_matching' },
summary: {
total_documents: 2,
pending_documents: 0,
processing_documents: 1,
completed_documents: 1,
review_required_documents: 0,
failed_documents: 0,
candidate_count: 1,
},
});
api.listRuns.mockResolvedValue({ items: [first] });
api.getRun.mockResolvedValueOnce(second);
@@ -229,14 +314,15 @@ describe('mapOrderExtractionRunToMessageState', () => {
await vi.advanceTimersByTimeAsync(1000);
expect(harness.options.saveLocalMessageToBackend).toHaveBeenCalledTimes(2);
expect(harness.messages.value[0]?.shipping_order_extraction_state?.current_stage_code).toBe('order_recognition');
expect(harness.messages.value[0]?.shipping_order_extraction_state?.currentActivity?.eventCode).toBe('profile_matching');
expect(harness.messages.value[0]?.shipping_order_extraction_state?.summary.completedDocuments).toBe(1);
harness.runs.stopPolling();
vi.useRealTimers();
});
it('merges duplicate local cards for one Run when a terminal update arrives', async () => {
vi.useFakeTimers();
const active = run({ run_no: 'SNER-DUPLICATE', status: 'running' });
const active = run({ run_no: 'SNER-DUPLICATE', status: 'processing' });
const failed = run({
run_no: 'SNER-DUPLICATE', status: 'failed', phase: 'failed', error: { code: 'SHIPPING_NOTICE.ORDER_EXTRACTION_PROVIDER_FAILED' },
});
@@ -262,8 +348,22 @@ describe('mapOrderExtractionRunToMessageState', () => {
it('refreshes Intake candidates and persists them on the terminal Run card without page reload', async () => {
vi.useFakeTimers();
const active = run({ run_no: 'SNER-REVIEW', status: 'running' });
const terminal = run({ run_no: 'SNER-REVIEW', status: 'needs_review', result: { pending_count: 1 } });
const active = run({ run_no: 'SNER-REVIEW', status: 'processing' });
const terminal = run({
run_no: 'SNER-REVIEW',
status: 'review_required',
summary: {
total_documents: 1,
pending_documents: 0,
processing_documents: 0,
completed_documents: 0,
review_required_documents: 1,
failed_documents: 0,
candidate_count: 0,
},
result: { pending_count: 1 },
available_actions: ['view_details', 'enter_manual_review'],
});
api.listRuns.mockResolvedValue({ items: [active] });
api.getRun.mockResolvedValue(terminal);
const harness = createHarness();
@@ -287,22 +387,49 @@ describe('mapOrderExtractionRunToMessageState', () => {
harness.runs.stopPolling();
});
it('maps completed result counters without guessing from text', () => {
it('maps completed summaries without guessing from text', () => {
expect(mapOrderExtractionRunToMessageState(run({
status: 'completed',
result: { batch_no: 'SB-1', created_count: 2, opened_existing_count: 1, pending_count: 0 },
summary: {
total_documents: 3,
pending_documents: 0,
processing_documents: 0,
completed_documents: 3,
review_required_documents: 0,
failed_documents: 0,
candidate_count: 3,
},
available_actions: ['view_details'],
}))).toMatchObject({
status: 'completed', terminal: true, batch_no: 'SB-1', created_count: 2, opened_existing_count: 1,
status: 'completed',
terminal: true,
summary: { completedDocuments: 3, candidateCount: 3 },
availableActions: ['view_details'],
});
});
it('maps needs_review and failed to distinct terminal states', () => {
it('maps review_required and failed to distinct terminal states', () => {
expect(mapOrderExtractionRunToMessageState(run({
status: 'needs_review', result: { pending_count: 3 },
}))).toMatchObject({ status: 'partially_completed', pending_count: 3, terminal: true });
status: 'review_required',
summary: {
total_documents: 3,
pending_documents: 0,
processing_documents: 0,
completed_documents: 0,
review_required_documents: 3,
failed_documents: 0,
candidate_count: 0,
},
available_actions: ['view_details', 'enter_manual_review'],
}))).toMatchObject({
status: 'review_required',
terminal: true,
summary: { reviewRequiredDocuments: 3 },
availableActions: ['view_details', 'enter_manual_review'],
});
expect(mapOrderExtractionRunToMessageState(run({
status: 'failed', error: { code: 'X' },
}))).toMatchObject({ status: 'failed', message: '', terminal: true });
}))).toMatchObject({ status: 'failed', errorCode: 'X', terminal: true });
});
it('acknowledges a terminal Run only after it is restored into the active conversation', async () => {