357 lines
14 KiB
Vue
357 lines
14 KiB
Vue
<!--
|
||
features/chat/components/shipping-review/ReviewLlmSuggestionPanel.vue
|
||
用途:AI 建议列表与裁决内容(布局重构后作为抽屉内容组件,非常驻主区大卡片)。
|
||
依赖:vue-i18n、ReviewLlmSuggestionDialog、ShippingOrderReview* 类型。
|
||
逻辑:由 ReviewLlmSuggestionDrawer 挂载;请求须用户主动触发;仅 validated 可接受;
|
||
validation_failed 仅可拒绝;provider_unavailable 明示原因;证据按 field_id + line_key/line_index 绑定。
|
||
-->
|
||
<script setup lang="ts">
|
||
import { computed, ref } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import type {
|
||
ShippingOrderReviewCase,
|
||
ShippingOrderReviewEvidence,
|
||
ShippingOrderReviewLlmAdjudicationPayload,
|
||
ShippingOrderReviewLlmSuggestion,
|
||
} from '../../types/shipping.api.types';
|
||
import ReviewLlmSuggestionDialog from './ReviewLlmSuggestionDialog.vue';
|
||
|
||
const props = defineProps<{
|
||
activeCase?: ShippingOrderReviewCase | null;
|
||
evidence?: ShippingOrderReviewEvidence[] | null;
|
||
editable?: boolean;
|
||
saving?: boolean;
|
||
suggestions?: ShippingOrderReviewLlmSuggestion[];
|
||
suggestionLoading?: boolean;
|
||
focusedFieldId?: string | null;
|
||
focusedLineKey?: string | null;
|
||
/** 父级传入的聚焦字段草稿;弹窗优先用建议字段自身解析值。 */
|
||
draftValue?: unknown;
|
||
}>();
|
||
|
||
const emit = defineEmits<{
|
||
requestSuggestion: [];
|
||
adjudicate: [payload: {
|
||
suggestion: ShippingOrderReviewLlmSuggestion;
|
||
body: ShippingOrderReviewLlmAdjudicationPayload;
|
||
}];
|
||
}>();
|
||
|
||
const { t, te } = useI18n();
|
||
const dialogOpen = ref(false);
|
||
const selected = ref<ShippingOrderReviewLlmSuggestion | null>(null);
|
||
|
||
/** 仅已通过逐字校验的建议可进入 accept/modify 待裁决列表。 */
|
||
const pendingSuggestions = computed(() =>
|
||
(props.suggestions || []).filter((item) => String(item.status || '') === 'validated' && !item.decision),
|
||
);
|
||
|
||
/** 验证失败建议仅可查看/拒绝,不进入可接受列表。 */
|
||
const failedSuggestions = computed(() =>
|
||
(props.suggestions || []).filter((item) => String(item.status || '') === 'validation_failed' && !item.decision),
|
||
);
|
||
|
||
/** Provider 未调用或调用失败的记录必须明确展示,避免用户把静默空态误认为 AI 已执行。 */
|
||
const unavailableSuggestions = computed(() =>
|
||
(props.suggestions || []).filter((item) => String(item.status || '') === 'provider_unavailable'),
|
||
);
|
||
|
||
/** 前门禁失败(未调用 Dify)的记录必须明确展示,错误码指向 i18n errors.json;避免用户误认为 AI 已跑过。 */
|
||
const gatedFailedSuggestions = computed(() =>
|
||
(props.suggestions || []).filter((item) => String(item.status || '') === 'gated_failed'),
|
||
);
|
||
|
||
/** 将 Provider 失败码转成可执行的业务提示,不向用户暴露底层地址或异常正文。 */
|
||
function unavailableMessage(item?: ShippingOrderReviewLlmSuggestion): string {
|
||
if (String(item?.error_code || '') === 'SHIPPING.REVIEW_LLM_DISABLED') {
|
||
return t('chat.shippingOrderReview.llmSuggestion.unavailableDisabled');
|
||
}
|
||
return t('chat.shippingOrderReview.llmSuggestion.unavailableProvider');
|
||
}
|
||
|
||
/**
|
||
* 门禁失败 short 提示(一层标题):用 errors.json 本地化;缺失时给通用提示避免裸 code。
|
||
* 完全消费后端投影的 item.error_code,前端绝对不推导「为什么失败」。
|
||
*/
|
||
function gatedShortMessage(item: ShippingOrderReviewLlmSuggestion): string {
|
||
const code = String(item.error_code || '');
|
||
const key = `errors.${code}`;
|
||
if (code && te(key)) return t(key);
|
||
return t('chat.shippingOrderReview.llmSuggestion.status.gated_failed');
|
||
}
|
||
|
||
/**
|
||
* 门禁失败 detail 提示(二层详情):查 errors.json 的 `${code}Detail` 或通用 hint。
|
||
* 同样不推导原因,纯 i18n 查表。
|
||
*/
|
||
function gatedDetailMessage(item: ShippingOrderReviewLlmSuggestion): string | null {
|
||
const code = String(item.error_code || '');
|
||
if (!code) return null;
|
||
const detailKey = `errors.${code}Detail`;
|
||
if (te(detailKey)) return t(detailKey);
|
||
const hintKey = `chat.shippingOrderReview.llmSuggestion.gateDetail.${code.replace(/^SHIPPING\./, '')}`;
|
||
if (te(hintKey)) return t(hintKey);
|
||
return null;
|
||
}
|
||
|
||
const statusLabel = computed(() => {
|
||
if (props.suggestionLoading) return t('chat.shippingOrderReview.llmSuggestion.status.pending');
|
||
const latest = (props.suggestions || [])[0];
|
||
const code = String(latest?.status || latest?.decision || 'idle');
|
||
const key = `chat.shippingOrderReview.llmSuggestion.status.${code}`;
|
||
return te(key) ? t(key) : t('chat.shippingOrderReview.llmSuggestion.status.unknown');
|
||
});
|
||
|
||
const canRequest = computed(() =>
|
||
Boolean(props.editable && props.activeCase && props.focusedFieldId && !props.suggestionLoading && !props.saving),
|
||
);
|
||
|
||
/**
|
||
* 证据须同时匹配 field_id 与明细行(machine-N → line_index)。
|
||
*/
|
||
function evidenceMatchesFieldLine(
|
||
item: ShippingOrderReviewEvidence,
|
||
fieldId: string,
|
||
lineKey: string | null | undefined,
|
||
): boolean {
|
||
if (String(item.field_id || '') !== fieldId) return false;
|
||
const key = String(lineKey || '').trim();
|
||
if (!key) {
|
||
return item.line_index == null || item.line_index === undefined;
|
||
}
|
||
const matched = /^machine-(\d+)$/i.exec(key);
|
||
if (matched) {
|
||
const idx = Number(matched[1]) - 1;
|
||
return Number(item.line_index) === idx;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
const selectedEvidence = computed(() => {
|
||
const items = props.evidence || [];
|
||
const fieldId = String(selected.value?.field_id || props.focusedFieldId || '');
|
||
const lineKey = selected.value?.line_key ?? props.focusedLineKey;
|
||
return items.find((item) => evidenceMatchesFieldLine(item, fieldId, lineKey)) || null;
|
||
});
|
||
|
||
/**
|
||
* 从草稿容器读取字段当前值(人工值优先,否则机器投影)。
|
||
*/
|
||
function readContainerDraftValue(container: Record<string, unknown> | null | undefined, fieldId: string): unknown {
|
||
if (!container || !fieldId) return null;
|
||
const values = container.values as Record<string, { value?: unknown }> | undefined;
|
||
if (values?.[fieldId] && 'value' in values[fieldId]) return values[fieldId].value ?? null;
|
||
const machine = Array.isArray(container.machine_fields)
|
||
? (container.machine_fields as Array<Record<string, unknown>>)
|
||
: [];
|
||
const hit = machine.find((item) => String(item.field_id || '') === fieldId);
|
||
return hit?.value ?? null;
|
||
}
|
||
|
||
/**
|
||
* 按建议(或聚焦字段)解析当前草稿值,供三方对比。
|
||
*/
|
||
const dialogDraftValue = computed(() => {
|
||
const fieldId = String(selected.value?.field_id || props.focusedFieldId || '');
|
||
const lineKey = selected.value?.line_key ?? props.focusedLineKey;
|
||
const draft = props.activeCase?.draft;
|
||
if (!draft || !fieldId) return props.draftValue ?? null;
|
||
if (lineKey) {
|
||
const lines = Array.isArray(draft.lines) ? draft.lines : [];
|
||
const line = lines.find((item) => String((item as { line_key?: string }).line_key || '') === String(lineKey));
|
||
return readContainerDraftValue(line as Record<string, unknown> | undefined, fieldId);
|
||
}
|
||
return readContainerDraftValue(draft.header_values as Record<string, unknown> | undefined, fieldId);
|
||
});
|
||
|
||
/**
|
||
* 打开指定建议的对比裁决对话框。
|
||
*/
|
||
function openSuggestion(item: ShippingOrderReviewLlmSuggestion) {
|
||
selected.value = item;
|
||
dialogOpen.value = true;
|
||
}
|
||
|
||
/**
|
||
* 将建议字段解析为订单字段名称;缺失时使用通用名称,避免显示内部字段 ID。
|
||
*/
|
||
function suggestionFieldLabel(item: ShippingOrderReviewLlmSuggestion): string {
|
||
const fieldId = String(item.field_id || '').trim();
|
||
const fields = props.activeCase?.schema?.fields || [];
|
||
const hit = fields.find((field) => String(field.field_id || '') === fieldId);
|
||
const name = String(hit?.display_name || hit?.field_code || '').trim();
|
||
if (name && item.line_key) {
|
||
return `${name} · ${item.line_key}`;
|
||
}
|
||
return name || t('chat.shippingOrderReview.unknownField');
|
||
}
|
||
|
||
/**
|
||
* 将证据等级转为业务文案,避免直接展示 L1_field。
|
||
*/
|
||
function evidenceLevelLabel(level?: string | null): string {
|
||
const code = String(level || 'L1_field').trim() || 'L1_field';
|
||
const key = `chat.shippingOrderReview.llmSuggestion.evidenceLevel.${code}`;
|
||
return te(key) ? t(key) : t('chat.shippingOrderReview.llmSuggestion.evidenceLevel.unknown');
|
||
}
|
||
|
||
/**
|
||
* 将对话框裁决转发给父级。
|
||
*/
|
||
function onDecide(payload: { decision: 'accept' | 'modify' | 'reject'; reason: string; value?: unknown }) {
|
||
if (!selected.value) return;
|
||
emit('adjudicate', {
|
||
suggestion: selected.value,
|
||
body: {
|
||
decision: payload.decision,
|
||
reason: payload.reason || undefined,
|
||
value: payload.value,
|
||
expected_draft_version: props.activeCase?.draft?.draft_version,
|
||
},
|
||
});
|
||
dialogOpen.value = false;
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<section class="rounded-xl border border-slate-200 bg-white p-3 text-sm shadow-sm dark:border-slate-800 dark:bg-slate-900">
|
||
<div class="flex flex-wrap items-center justify-between gap-2">
|
||
<div>
|
||
<div class="font-semibold">{{ t('chat.shippingOrderReview.llmSuggestion.panelTitle') }}</div>
|
||
<div class="mt-1 text-xs text-slate-500">
|
||
{{ t('chat.shippingOrderReview.llmSuggestion.panelHint') }}
|
||
· {{ evidenceLevelLabel(selected?.evidence_level || 'L1_field') }}
|
||
· {{ statusLabel }}
|
||
<span v-if="suggestions?.length">· {{ suggestions.length }}</span>
|
||
</div>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
class="rounded border px-2 py-1 text-xs disabled:opacity-50"
|
||
:disabled="!canRequest"
|
||
data-testid="review-llm-request"
|
||
@click="emit('requestSuggestion')"
|
||
>
|
||
{{
|
||
suggestionLoading
|
||
? t('chat.shippingOrderReview.llmSuggestion.requesting')
|
||
: t('chat.shippingOrderReview.llmSuggestion.request')
|
||
}}
|
||
</button>
|
||
</div>
|
||
|
||
<div
|
||
v-if="unavailableSuggestions.length"
|
||
class="mt-3 rounded border border-rose-200 bg-rose-50 px-2 py-2 text-xs text-rose-700 dark:border-rose-900 dark:bg-rose-950/30 dark:text-rose-300"
|
||
data-testid="review-llm-provider-unavailable"
|
||
role="alert"
|
||
>
|
||
{{ unavailableMessage(unavailableSuggestions[0]) }}
|
||
</div>
|
||
<ul
|
||
v-if="gatedFailedSuggestions.length"
|
||
class="mt-3 space-y-2"
|
||
data-testid="review-llm-gated-failed-list"
|
||
>
|
||
<li
|
||
v-for="item in gatedFailedSuggestions"
|
||
:key="item.suggestion_no"
|
||
class="rounded border border-sky-200 bg-sky-50/60 px-2 py-2 text-xs text-sky-800 dark:border-sky-900 dark:bg-sky-950/30 dark:text-sky-200"
|
||
role="note"
|
||
>
|
||
<div class="flex items-start justify-between gap-2">
|
||
<div class="min-w-0 flex-1">
|
||
<div class="font-medium leading-snug">{{ suggestionFieldLabel(item) }}</div>
|
||
<p class="mt-1 leading-snug">{{ gatedShortMessage(item) }}</p>
|
||
<p
|
||
v-if="gatedDetailMessage(item)"
|
||
class="mt-1 text-[11px] leading-relaxed opacity-80"
|
||
>
|
||
{{ gatedDetailMessage(item) }}
|
||
</p>
|
||
<p
|
||
v-if="item.evidence_summary"
|
||
class="mt-1 flex flex-wrap gap-x-3 gap-y-0.5 text-[11px] opacity-80"
|
||
>
|
||
<span v-if="item.evidence_summary.level">
|
||
{{ t('chat.shippingOrderReview.llmSuggestion.evidenceLevelLabel') }}:{{ evidenceLevelLabel(item.evidence_summary.level) }}
|
||
</span>
|
||
<span v-if="item.evidence_summary.auto_upgraded">
|
||
{{ t('chat.shippingOrderReview.llmSuggestion.autoUpgradedLabel') }}
|
||
</span>
|
||
<span v-if="typeof item.evidence_summary.field_count === 'number'">
|
||
{{ t('chat.shippingOrderReview.llmSuggestion.fieldCountLabel') }}:{{ item.evidence_summary.field_count }}
|
||
</span>
|
||
<span v-if="item.evidence_summary.context_truncated">
|
||
{{ t('chat.shippingOrderReview.llmSuggestion.contextTruncatedLabel') }}
|
||
</span>
|
||
</p>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
class="shrink-0 rounded border border-sky-300 bg-white px-2 py-0.5 text-xs text-sky-700 dark:border-sky-700 dark:bg-slate-900 dark:text-sky-200"
|
||
:disabled="!canRequest"
|
||
:aria-label="t('chat.shippingOrderReview.llmSuggestion.request')"
|
||
@click="emit('requestSuggestion')"
|
||
>
|
||
{{ t('chat.shippingOrderReview.llmSuggestion.request') }}
|
||
</button>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
<p
|
||
v-if="!pendingSuggestions.length && !failedSuggestions.length && !unavailableSuggestions.length && !gatedFailedSuggestions.length"
|
||
class="mt-3 text-xs text-slate-500"
|
||
>
|
||
{{ t('chat.shippingOrderReview.llmSuggestion.empty') }}
|
||
</p>
|
||
<ul v-if="pendingSuggestions.length" class="mt-3 space-y-2">
|
||
<li
|
||
v-for="item in pendingSuggestions"
|
||
:key="item.suggestion_no"
|
||
class="flex items-center justify-between gap-2 rounded border border-slate-200 px-2 py-1.5 dark:border-slate-700"
|
||
>
|
||
<div class="min-w-0 truncate text-xs">
|
||
<span class="font-medium">{{ suggestionFieldLabel(item) }}</span>
|
||
<span class="ml-2 opacity-70">{{ item.suggested_value ?? '—' }}</span>
|
||
</div>
|
||
<button type="button" class="shrink-0 rounded border px-2 py-0.5 text-xs" @click="openSuggestion(item)">
|
||
{{ t('chat.shippingOrderReview.llmSuggestion.review') }}
|
||
</button>
|
||
</li>
|
||
</ul>
|
||
<ul v-if="failedSuggestions.length" class="mt-3 space-y-2" data-testid="review-llm-failed-list">
|
||
<li
|
||
v-for="item in failedSuggestions"
|
||
:key="item.suggestion_no"
|
||
class="flex items-center justify-between gap-2 rounded border border-amber-200 bg-amber-50/50 px-2 py-1.5 dark:border-amber-900 dark:bg-amber-950/30"
|
||
>
|
||
<div class="min-w-0 truncate text-xs">
|
||
<span class="font-medium">{{ suggestionFieldLabel(item) }}</span>
|
||
<span class="ml-2 text-amber-700 dark:text-amber-300">
|
||
{{ t('chat.shippingOrderReview.llmSuggestion.verbatimFailed') }}
|
||
</span>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
class="shrink-0 rounded border px-2 py-0.5 text-xs"
|
||
data-testid="review-llm-open-failed"
|
||
@click="openSuggestion(item)"
|
||
>
|
||
{{ t('chat.shippingOrderReview.llmSuggestion.review') }}
|
||
</button>
|
||
</li>
|
||
</ul>
|
||
|
||
<ReviewLlmSuggestionDialog
|
||
:open="dialogOpen"
|
||
:suggestion="selected"
|
||
:evidence="selectedEvidence"
|
||
:draft-value="dialogDraftValue"
|
||
:field-label="selected ? suggestionFieldLabel(selected) : ''"
|
||
@close="dialogOpen = false"
|
||
@decide="onDecide"
|
||
/>
|
||
</section>
|
||
</template>
|