feat(shipping-notice): 新增发货通知重复检测与处理接口,支持重复决策保存与重新执行检测

This commit is contained in:
hugo committed 2026-07-14 21:12:36 +08:00
1 parent 729546a7b7
commit ceb49461e8
72 files changed
+14270 -460

No files matched your search

@@ -20,7 +20,11 @@ import {
Square,
X,
} from 'lucide-vue-next';
import type { PendingAttachment } from '../types/shipping.types';
import type { PendingAttachment, ShippingNoticePendingAttachment } from '../types/shipping.types';
import {
isBlockingWarning,
isShippingNoticeAttachmentParseIncomplete,
} from '../shipping/shippingNoticeAttachmentIntegrity';
export interface ChatInputAttachmentPolicy {
anyEnabled: boolean;
@@ -60,6 +64,7 @@ const emit = defineEmits<{
'voice-click': [];
'remove-attachment': [id: string];
'retry-shipping-notice-attachment': [id: string];
'resolve-shipping-notice-duplicate': [id: string];
'local-files-selected': [event: Event];
'add-remote-url': [url: string];
'shipping-inventory-selected': [event: Event];
@@ -79,6 +84,73 @@ const showAttachmentMenu = ref(false);
const showAttachmentUrlPanel = ref(false);
const showKbDropdown = ref(false);
const attachmentUrlInput = ref('');
/**
* 汇总 EML 附件解析进度,供输入栏徽标展示。
* 核心逻辑:failed/empty/skipped/超限/文本预算耗尽计为问题;duplicate 单独提示,不记为失败。
*/
function shippingNoticeAttachmentHint(attachment: ShippingNoticePendingAttachment): string {
const summary = attachment.parseSummary;
const total = Number(summary?.attachment_count || 0);
const rows = summary?.attachments || [];
const warnings = summary?.warnings || [];
const hasBlockingWarning = warnings.some((w) => isBlockingWarning(String(w || '')));
if (!total && !hasBlockingWarning && !rows.length) return '';
const effectiveTotal = total || rows.length;
const ok = rows.filter((item) => item.parse_status === 'parsed').length;
const problematic = rows.filter((item) =>
['failed', 'empty', 'skipped'].includes(String(item.parse_status || '')),
).length;
const duplicates = rows.filter((item) => item.parse_status === 'duplicate').length;
const overflow = Math.max(0, effectiveTotal - rows.length);
const issues = problematic + overflow + (hasBlockingWarning && problematic === 0 && overflow === 0 ? 1 : 0);
if (issues > 0) {
return t('chat.shippingNoticeAttachmentHintPartial', {
ok,
total: effectiveTotal || Math.max(ok + issues, 1),
failed: issues,
});
}
if (duplicates > 0) {
return t('chat.shippingNoticeAttachmentHintWithDup', { ok, total: effectiveTotal, duplicates });
}
if (!effectiveTotal) return '';
return t('chat.shippingNoticeAttachmentHint', { ok, total: effectiveTotal });
}
/**
* 是否展示「重试解析」:顶层失败,或已 parsed 但附件不完整。
*/
function canRetryShippingNoticeParse(attachment: ShippingNoticePendingAttachment): boolean {
if (attachment.kind !== 'shipping_notice') return false;
if (attachment.parseStatus === 'failed') return true;
return (
attachment.parseStatus === 'parsed'
&& isShippingNoticeAttachmentParseIncomplete(attachment.parseSummary)
);
}
/**
* 生成附件明细 title(文件名 + 状态 + 关键警告),便于悬停查看完整性。
*/
function shippingNoticeAttachmentTitle(attachment: ShippingNoticePendingAttachment): string {
const rows = attachment.parseSummary?.attachments || [];
const warnings = attachment.parseSummary?.warnings || [];
const lines: string[] = [];
for (const item of rows) {
const name = item.filename || item.attachment_key || 'attachment';
const status = item.parse_status || 'unknown';
lines.push(`${name}: ${status}`);
}
for (const warning of warnings) {
const text = String(warning || '').trim();
if (text) lines.push(`warning: ${text}`);
}
return lines.join('\n');
}
const closeAttachmentMenu = () => {
showAttachmentMenu.value = false;
showAttachmentUrlPanel.value = false;
@@ -246,7 +318,36 @@ watch(
{{ a.originalFilename }}
<span v-if="a.uploadStatus === 'uploading'" class="text-muted"> · {{ t('chat.shippingNoticeUploading') }}</span>
<span v-else-if="a.parseStatus === 'queued' || a.parseStatus === 'parsing'" class="text-muted"> · {{ t('chat.shippingNoticeParsing') }}</span>
<span v-else-if="a.parseStatus === 'parsed'" class="text-muted"> · {{ t('chat.shippingNoticeParsed') }}</span>
<span
v-else-if="a.parseStatus === 'parsed' && a.duplicate?.status === 'possible_duplicate' && !a.duplicateResolution?.action"
class="font-medium text-amber-700"
> · {{ t('chat.shippingNoticeDuplicatePossibleBadge') }}</span>
<span
v-else-if="a.parseStatus === 'parsed' && a.duplicate?.blocking && !a.duplicateResolution?.action"
class="font-medium text-amber-700"
> · {{ t('chat.shippingNoticeDuplicateBlockingBadge') }}</span>
<span
v-else-if="a.parseStatus === 'parsed' && a.duplicateResolution?.action === 'create_revision'"
class="text-brand-700"
> · {{ t('chat.shippingNoticeDuplicateRevisionChosen') }}</span>
<span
v-else-if="a.parseStatus === 'parsed' && a.duplicateResolution?.action === 'force_create'"
class="text-amber-700"
> · {{ t('chat.shippingNoticeDuplicateForceChosen') }}</span>
<span
v-else-if="a.parseStatus === 'parsed' && a.duplicateResolution?.action === 'open_existing'"
class="text-muted"
> · {{ t('chat.shippingNoticeDuplicateOpenChosen') }}</span>
<span
v-else-if="a.parseStatus === 'parsed'"
class="text-muted"
:title="shippingNoticeAttachmentTitle(a)"
>
· {{ t('chat.shippingNoticeParsed') }}
<template v-if="shippingNoticeAttachmentHint(a)">
· {{ shippingNoticeAttachmentHint(a) }}
</template>
</span>
<span v-else-if="a.parseStatus === 'failed'" class="text-muted"> · {{ t('chat.shippingNoticeParseFailed') }}</span>
</template>
<template v-else>
@@ -254,7 +355,16 @@ watch(
</template>
</span>
<button
v-if="a.kind === 'shipping_notice' && a.parseStatus === 'failed'"
v-if="a.kind === 'shipping_notice' && a.duplicate?.blocking && !a.duplicateResolution?.action"
type="button"
class="text-amber-700 hover:text-amber-800"
:title="t('chat.shippingNoticeDuplicateHandle')"
@click="emit('resolve-shipping-notice-duplicate', a.id)"
>
{{ t('chat.shippingNoticeDuplicateHandle') }}
</button>
<button
v-if="a.kind === 'shipping_notice' && canRetryShippingNoticeParse(a)"
type="button"
class="text-muted hover:text-primary"
:title="t('chat.shippingNoticeRetryParse')"
@@ -287,6 +287,7 @@ watch(() => page.currentAgentId, () => {
@voice-click="page.handleVoiceFeatureClick"
@remove-attachment="page.removeAttachment"
@retry-shipping-notice-attachment="page.retryShippingNoticeAttachment"
@resolve-shipping-notice-duplicate="page.openShippingDuplicateDialog"
@local-files-selected="page.handleLocalFilesSelected"
@add-remote-url="page.addRemoteUrlAttachment"
@shipping-inventory-selected="page.handleShippingInventorySelected"
@@ -10,6 +10,7 @@ import ChatQueryPreviewDialog from '@/features/chat/components/ChatQueryPreviewD
import ChatLeaveAgentDialog from '@/features/chat/components/ChatLeaveAgentDialog.vue';
import ChatOALoginDialog from '@/features/chat/components/ChatOALoginDialog.vue';
import ChatGuideModal from '@/features/chat/components/ChatGuideModal.vue';
import ShippingNoticeDuplicateDialog from '@/features/chat/components/ShippingNoticeDuplicateDialog.vue';
import { Check, Loader2, X } from 'lucide-vue-next';
import { nextTick } from 'vue';
import { useChatPageContext } from '@/features/chat/composables/chat.context';
@@ -145,4 +146,13 @@ async function handleQueryPreviewAssetImage(assetId: string, assetName: string)
:guides="page.agentGuides"
:active-guide="page.activeGuide"
/>
<ShippingNoticeDuplicateDialog
:open="!!page.showShippingDuplicateDialog"
:attachment="page.shippingDuplicateAttachment || null"
:can-force-create="!!page.canForceCreateShippingDuplicate"
@update:open="(v) => { if (!v) page.closeShippingDuplicateDialog?.(); }"
@resolve="(payload) => void page.resolveShippingDuplicateDialog?.(payload)"
@remove="() => void page.removeShippingDuplicateDialogAttachment?.()"
@cancel="page.closeShippingDuplicateDialog?.()"
/>
</template>
@@ -4,7 +4,7 @@
* 用途:发货通知 Agent 的右侧批次工作台抽屉,承载多批次切换、任务筛选、步骤诊断与统一动作入口。
* 依赖:shippingStepper、shippingMenu、shipping.api.types、vue-i18n、lucide-vue-next。
* 逻辑:上游传入会话级 workspace 与当前批次任务;本组件只渲染后端 `ui_state.action_buttons` 和查看类辅助入口。
* 「重试所有失败」仅 emit `retry-pending`,由父级打开确认弹窗后再调用 useChatShipping 执行。
* 解析阶段诊断优先展示 `ui_state.summary.issues` 完整问题列表;「重试所有失败」仅 emit `retry-pending`,由父级打开确认弹窗后再调用 useChatShipping 执行。
*/
import { computed, ref, watch } from 'vue';
import { RefreshCw, Target, X } from 'lucide-vue-next';
@@ -247,12 +247,42 @@ const currentStepProgress = (task: ShippingTaskSummary | ShippingTaskDetail) =>
const sanitizeDiagnosisCopy = (text: string | null | undefined) =>
sanitizeShippingRuntimeStatusText(String(text || '').trim());
/**
* 读取任务诊断卡要展示的全部问题列表。
* 核心逻辑:优先消费后端 `ui_state.summary.issues`(解析阶段已汇总库存源+warnings);
* 若缺失则回退到单一 error_message,保证旧数据仍有可读文案。
*/
const resolveTaskDiagnosisIssues = (task: ShippingTaskSummary | ShippingTaskDetail): string[] => {
const uiIssues = task.ui_state?.summary?.issues;
if (Array.isArray(uiIssues) && uiIssues.length) {
return uiIssues
.map((item) => sanitizeDiagnosisCopy(item))
.filter(Boolean);
}
const fallback = sanitizeDiagnosisCopy(task.ui_state?.summary?.error_message || task.error_message);
return fallback ? [fallback] : [];
};
/**
* 解析右侧工作台诊断标题/建议/问题列表。
* 核心逻辑:解析阶段若 issues 多于 1 条,使用「发现问题」标题并列出全部条目,避免只显示库存源一条。
*/
const resolveTaskDiagnosis = (task: ShippingTaskSummary | ShippingTaskDetail) => {
const uiSummary = task.ui_state?.summary;
if (uiSummary?.title || uiSummary?.message || uiSummary?.error_message) {
const issues = resolveTaskDiagnosisIssues(task);
if (uiSummary?.title || uiSummary?.message || uiSummary?.error_message || issues.length) {
const multiIssue = issues.length > 1;
return {
title: sanitizeDiagnosisCopy(uiSummary.error_message || uiSummary.title) || t('chat.shippingNoticeUi.diagnosisStepWaitingTitle', { step: taskStepNumber(task) }),
advice: sanitizeDiagnosisCopy(uiSummary.message) || t('chat.shippingNoticeUi.diagnosisStepWaitingAdvice'),
title: multiIssue
? t('chat.shippingNoticeUi.diagnosisIssuesTitle', { count: issues.length })
: (
sanitizeDiagnosisCopy(issues[0] || uiSummary?.error_message || uiSummary?.title)
|| t('chat.shippingNoticeUi.diagnosisStepWaitingTitle', { step: taskStepNumber(task) })
),
advice: multiIssue
? t('chat.shippingNoticeUi.diagnosisIssuesAdvice')
: (sanitizeDiagnosisCopy(uiSummary?.message) || t('chat.shippingNoticeUi.diagnosisStepWaitingAdvice')),
issues: multiIssue ? issues : [],
};
}
const reason = String(task.pending_human_reason || '').trim();
@@ -577,11 +607,24 @@ const handleSwitchBatch = (batchNo: string) => {
</div>
<div
v-for="diagnosis in [resolveTaskDiagnosis(task)]"
:key="`${task.task_no}-diagnosis`"
class="mb-2 rounded-lg border p-2.5 text-xs font-medium leading-5"
:class="isFailedTask(task) ? 'border-rose-100 bg-rose-50 text-rose-700' : isAbnormalTask(task) ? 'border-amber-100 bg-amber-50 text-amber-700' : 'border-emerald-100 bg-emerald-50 text-emerald-700'"
>
<div class="font-semibold">{{ resolveTaskDiagnosis(task).title }}</div>
<div class="mt-1 font-normal text-slate-600">{{ resolveTaskDiagnosis(task).advice }}</div>
<div class="font-semibold">{{ diagnosis.title }}</div>
<ul
v-if="(diagnosis.issues || []).length"
class="mt-1.5 list-disc space-y-1 pl-4 font-normal text-slate-700"
>
<li
v-for="(issue, issueIndex) in diagnosis.issues"
:key="`${task.task_no}-issue-${issueIndex}`"
>
{{ issue }}
</li>
</ul>
<div class="mt-1 font-normal text-slate-600">{{ diagnosis.advice }}</div>
</div>
<div v-if="!resolveWorkspaceActionButtons(task).length" class="mb-3 text-xs leading-5 text-muted">
<span class="font-semibold text-secondary">{{ t('chat.shippingNoticeUi.suggestedAction') }}</span>
@@ -0,0 +1,201 @@
/**
* features/chat/components/ShippingNoticeDuplicateDialog.vue
* 用途:发货通知阻断型重复处理弹窗(打开已有 / 修订 / 强制新建)。
* 依赖:shipping.types Duplicate 类型、vue-i18n。
* 逻辑:ChatViewOverlays 展示;确认后回写 resolveDuplicateAttachment,再允许确认建单。
*/
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import type {
ShippingDuplicateAction,
ShippingNoticePendingAttachment,
} from '../types/shipping.types';
const props = defineProps<{
open: boolean;
attachment: ShippingNoticePendingAttachment | null;
canForceCreate: boolean;
}>();
const emit = defineEmits<{
'update:open': [value: boolean];
resolve: [payload: { action: ShippingDuplicateAction; targetTaskNo?: string; reason?: string }];
remove: [];
cancel: [];
}>();
const { t } = useI18n();
const reason = ref('');
const selectedAction = ref<ShippingDuplicateAction>('open_existing');
const submitting = ref(false);
const localError = ref('');
const match = computed(() => props.attachment?.duplicate?.matches?.[0] || null);
const allowed = computed(() => new Set(props.attachment?.duplicate?.allowedActions || []));
watch(
() => props.open,
(open) => {
if (!open) return;
reason.value = '';
localError.value = '';
submitting.value = false;
const actions = props.attachment?.duplicate?.allowedActions || [];
selectedAction.value = (actions[0] as ShippingDuplicateAction) || 'open_existing';
},
);
/**
* 关闭弹窗。
*/
function close() {
emit('update:open', false);
emit('cancel');
}
/**
* 提交所选重复处理动作。
*/
async function confirm() {
if (!props.attachment?.duplicate || !match.value) return;
const action = selectedAction.value;
if (!allowed.value.has(action)) {
localError.value = t('chat.shippingNoticeDuplicateActionUnavailable');
return;
}
if (action === 'force_create') {
if (!props.canForceCreate) {
localError.value = t('chat.shippingNoticeDuplicateForceForbidden');
return;
}
if (!reason.value.trim()) {
localError.value = t('chat.shippingNoticeDuplicateReasonRequired');
return;
}
}
submitting.value = true;
localError.value = '';
try {
emit('resolve', {
action,
targetTaskNo: match.value.taskNo,
reason: action === 'force_create' ? reason.value.trim() : undefined,
});
emit('update:open', false);
} finally {
submitting.value = false;
}
}
const statusLabel = computed(() => {
const status = props.attachment?.duplicate?.status;
if (status === 'exact_file') return t('chat.shippingNoticeDuplicateExactFile');
if (status === 'same_active_task') return t('chat.shippingNoticeDuplicateSameActive');
if (status === 'same_completed_task') return t('chat.shippingNoticeDuplicateSameCompleted');
if (status === 'possible_duplicate') return t('chat.shippingNoticeDuplicatePossible');
return t('chat.shippingNoticeDuplicateNeedsResolution');
});
</script>
<template>
<div
v-if="open && attachment"
class="fixed inset-0 z-[160] flex items-center justify-center bg-black/40 px-4"
role="dialog"
aria-modal="true"
>
<div class="w-full max-w-lg rounded-xl border border-default bg-surface p-5 shadow-xl">
<h3 class="text-base font-semibold text-primary">
{{ t('chat.shippingNoticeDuplicateTitle') }}
</h3>
<p class="mt-2 text-sm text-secondary">{{ statusLabel }}</p>
<p class="mt-1 text-xs text-muted">
{{ attachment.originalFilename }}
</p>
<div
v-if="match"
class="mt-4 rounded-lg border border-default bg-surface-soft px-3 py-2 text-sm text-secondary"
>
<div>{{ t('chat.shippingNoticeDuplicateMatchTask', { taskNo: match.taskNo }) }}</div>
<div class="mt-1 text-xs text-muted">
{{ t('chat.shippingNoticeDuplicateMatchStatus', { status: match.processStatus }) }}
· v{{ match.documentVersion }}
</div>
<div v-if="match.matchedFields?.length" class="mt-1 text-xs text-muted">
{{ t('chat.shippingNoticeDuplicateMatchFields', { fields: match.matchedFields.join(', ') }) }}
</div>
</div>
<div class="mt-4 space-y-2">
<label
v-if="allowed.has('open_existing')"
class="flex cursor-pointer items-start gap-2 rounded-lg border border-default px-3 py-2 text-sm"
:class="selectedAction === 'open_existing' ? 'border-brand-500 bg-brand-50/40' : ''"
>
<input v-model="selectedAction" type="radio" value="open_existing" class="mt-1" />
<span>
<span class="font-medium text-primary">{{ t('chat.shippingNoticeDuplicateOpenExisting') }}</span>
<span class="mt-0.5 block text-xs text-muted">{{ t('chat.shippingNoticeDuplicateOpenExistingHint') }}</span>
</span>
</label>
<label
v-if="allowed.has('create_revision')"
class="flex cursor-pointer items-start gap-2 rounded-lg border border-default px-3 py-2 text-sm"
:class="selectedAction === 'create_revision' ? 'border-brand-500 bg-brand-50/40' : ''"
>
<input v-model="selectedAction" type="radio" value="create_revision" class="mt-1" />
<span>
<span class="font-medium text-primary">{{ t('chat.shippingNoticeDuplicateCreateRevision') }}</span>
<span class="mt-0.5 block text-xs text-muted">{{ t('chat.shippingNoticeDuplicateCreateRevisionHint') }}</span>
</span>
</label>
<label
v-if="allowed.has('force_create') && canForceCreate"
class="flex cursor-pointer items-start gap-2 rounded-lg border border-default px-3 py-2 text-sm"
:class="selectedAction === 'force_create' ? 'border-amber-500 bg-amber-50/50' : ''"
>
<input v-model="selectedAction" type="radio" value="force_create" class="mt-1" />
<span>
<span class="font-medium text-primary">{{ t('chat.shippingNoticeDuplicateForceCreate') }}</span>
<span class="mt-0.5 block text-xs text-muted">{{ t('chat.shippingNoticeDuplicateForceCreateHint') }}</span>
</span>
</label>
</div>
<div v-if="selectedAction === 'force_create'" class="mt-3">
<label class="text-xs font-medium text-secondary">{{ t('chat.shippingNoticeDuplicateReasonLabel') }}</label>
<textarea
v-model="reason"
rows="2"
class="mt-1 w-full rounded-lg border border-default bg-surface px-3 py-2 text-sm"
:placeholder="t('chat.shippingNoticeDuplicateReasonPlaceholder')"
/>
</div>
<p v-if="localError" class="mt-2 text-xs text-red-600">{{ localError }}</p>
<div class="mt-5 flex flex-wrap justify-end gap-2">
<button type="button" class="rounded-lg px-3 py-1.5 text-sm text-muted hover:text-primary" @click="close">
{{ t('common.cancel') }}
</button>
<button
type="button"
class="rounded-lg px-3 py-1.5 text-sm text-red-600 hover:bg-red-50"
@click="emit('remove'); emit('update:open', false)"
>
{{ t('chat.shippingNoticeDuplicateRemoveFile') }}
</button>
<button
type="button"
class="rounded-lg bg-brand-600 px-3 py-1.5 text-sm font-medium text-white disabled:opacity-50"
:disabled="submitting || !match"
@click="confirm"
>
{{ t('chat.shippingNoticeDuplicateConfirm') }}
</button>
</div>
</div>
</div>
</template>