feat(shipping-notice): 增强订单抽取功能,添加进度事件和诊断运行管理,优化数据模型和访问控制

This commit is contained in:
hugo committed 2026-07-21 23:17:29 +08:00
1 parent ce26d6d0c4
commit c4095b08da
175 files changed
+13064 -1814

No files matched your search

@@ -14,12 +14,16 @@ const api = vi.hoisted(() => ({
createRun: vi.fn(),
getRun: vi.fn(),
listRuns: vi.fn(),
markSeen: vi.fn(),
retryRun: vi.fn(),
}));
vi.mock('../api/shipping.api', () => ({
createWorkspaceShippingOrderExtractionRun: api.createRun,
getWorkspaceShippingOrderExtractionRun: api.getRun,
listWorkspaceShippingOrderExtractionRuns: api.listRuns,
markWorkspaceShippingOrderExtractionRunSeen: api.markSeen,
retryWorkspaceShippingOrderExtractionRun: api.retryRun,
}));
const run = (patch: Partial<ShippingOrderExtractionRun>): ShippingOrderExtractionRun => ({
@@ -50,6 +54,7 @@ const createHarness = () => {
refreshShippingActions: vi.fn(async () => undefined),
loadHistory: vi.fn(async () => undefined),
onConversationResolved: vi.fn(async () => undefined),
onRetryError: vi.fn(),
t: (key: string) => key,
};
return { messages, conversationId, options, runs: useShippingOrderExtractionRuns(options) };
@@ -127,16 +132,110 @@ describe('useShippingOrderExtractionRuns lifecycle', () => {
expect(harness.messages.value[1]?.shipping_order_extraction_state?.status).toBe('extracting');
harness.runs.stopPolling();
});
it('retries a failed Run in place and resumes polling without creating a new user message', async () => {
const failed = run({ run_no: 'SNER-RETRY', status: 'failed', phase: 'failed', error: { message: 'bad workflow' } });
const queued = run({ run_no: 'SNER-RETRY', status: 'queued', phase: 'manual_retry_queued', error: null });
api.listRuns.mockResolvedValue({ items: [failed] });
api.retryRun.mockResolvedValue({ run: queued, reused: false });
api.getRun.mockResolvedValue(queued);
const harness = createHarness();
harness.conversationId.value = 'conv-1';
await harness.runs.restoreForConversation('conv-1');
await expect(harness.runs.retryRun('SNER-RETRY')).resolves.toBe(true);
expect(api.retryRun).toHaveBeenCalledWith('SNER-RETRY');
expect(harness.options.refreshNoticeIntakeStatuses).toHaveBeenCalledTimes(1);
expect(harness.messages.value).toHaveLength(1);
expect(harness.messages.value[0]?.shipping_order_extraction_state?.status).toBe('extracting');
expect(harness.runs.getActiveRunNo()).toBe('SNER-RETRY');
harness.runs.stopPolling();
});
it('reports a retry API error and leaves the failed state available for another retry', async () => {
api.retryRun.mockRejectedValue(new Error('retry unavailable'));
const harness = createHarness();
await expect(harness.runs.retryRun('SNER-FAILED')).resolves.toBe(false);
expect(harness.options.onRetryError).toHaveBeenCalledWith('retry unavailable');
expect(harness.runs.getActiveRunNo()).toBe('');
});
});
describe('mapOrderExtractionRunToMessageState', () => {
it('maps queued/running to indeterminate extracting state', () => {
expect(mapOrderExtractionRunToMessageState(run({ status: 'running', phase: 'calling_provider' }))).toMatchObject({
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',
},
}))).toMatchObject({
status: 'extracting', terminal: false, phase: 'llm_extraction',
current_stage_code: 'document_processing', health: 'normal',
last_activity_at: '2026-07-21T03:24:51Z',
});
});
it('persists a changed progress projection even when Run status and result are unchanged', async () => {
vi.useFakeTimers();
const first = run({
run_no: 'SNER-PROGRESS',
status: 'running',
progress: { version: 1, current_stage_code: 'workflow_accepted' },
});
const second = run({
run_no: 'SNER-PROGRESS',
status: 'running',
progress: { version: 2, current_stage_code: 'order_recognition' },
});
api.listRuns.mockResolvedValue({ items: [first] });
api.getRun.mockResolvedValueOnce(second);
const harness = createHarness();
harness.conversationId.value = 'conv-1';
await harness.runs.restoreForConversation('conv-1');
expect(harness.options.saveLocalMessageToBackend).toHaveBeenCalledTimes(1);
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');
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 failed = run({
run_no: 'SNER-DUPLICATE', status: 'failed', phase: 'failed', error: { message: 'provider unavailable' },
});
api.listRuns.mockResolvedValue({ items: [active] });
api.getRun.mockResolvedValue(failed);
const harness = createHarness();
harness.conversationId.value = 'conv-1';
harness.messages.value.push({
role: 'assistant', type: 'text', content: '', local_id: 'legacy-duplicate', local_only: true,
shipping_order_extraction_state: mapOrderExtractionRunToMessageState(active),
payload: { formId: 'legacy-state-card', order_extraction_run_no: 'SNER-DUPLICATE' },
});
await harness.runs.restoreForConversation('conv-1');
await vi.advanceTimersByTimeAsync(1000);
const cards = harness.messages.value.filter((item) => item.shipping_order_extraction_state);
expect(cards).toHaveLength(1);
expect(cards[0]?.shipping_order_extraction_state?.status).toBe('failed');
harness.runs.stopPolling();
vi.useRealTimers();
});
it('maps completed result counters without guessing from text', () => {
expect(mapOrderExtractionRunToMessageState(run({
status: 'completed',
@@ -152,6 +251,19 @@ describe('mapOrderExtractionRunToMessageState', () => {
}))).toMatchObject({ status: 'partially_completed', pending_count: 3, terminal: true });
expect(mapOrderExtractionRunToMessageState(run({
status: 'failed', error: { code: 'X', message: 'provider failed' },
}))).toMatchObject({ status: 'failed', message: 'provider failed', terminal: true });
}))).toMatchObject({ status: 'failed', message: '', terminal: true });
});
it('acknowledges a terminal Run only after it is restored into the active conversation', async () => {
const terminal = run({ run_no: 'SNER-SEEN', status: 'completed' });
api.listRuns.mockResolvedValue({ items: [terminal] });
api.markSeen.mockResolvedValue({ seen: true });
const harness = createHarness();
harness.conversationId.value = 'conv-1';
await harness.runs.restoreForConversation('conv-1');
expect(api.markSeen).toHaveBeenCalledWith('SNER-SEEN');
expect(harness.options.loadHistory).toHaveBeenCalledTimes(1);
});
});