337 lines
12 KiB
Vue
337 lines
12 KiB
Vue
/**
|
||
* features/workspace/shipping-task-center/ShippingRunProgressTable.vue
|
||
* 用途:任务中心「我的识别」续办表——展示用户自有抽取 Run 的阶段、耗时、取消态与续办动作。
|
||
* 依赖:shipping.api 分页列表/取消/重试、element-plus、vue-i18n、vue-router、orderExtractionProgressDisplay。
|
||
* 逻辑:事实来自后端投影;打开会话/复核深链到 Chat;取消/重试复用工作台 API。样式由任务中心页引入。
|
||
*/
|
||
<script setup lang="ts">
|
||
import { onMounted, ref, watch } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { useRouter } from 'vue-router';
|
||
import {
|
||
ElButton,
|
||
ElInput,
|
||
ElLoading,
|
||
ElOption,
|
||
ElPagination,
|
||
ElSelect,
|
||
ElTable,
|
||
ElTableColumn,
|
||
ElTag,
|
||
ElText,
|
||
} from 'element-plus';
|
||
import {
|
||
cancelWorkspaceShippingOrderExtractionRun,
|
||
listShippingChatAgents,
|
||
listWorkspaceShippingOrderExtractionRunsPage,
|
||
retryWorkspaceShippingOrderExtractionRun,
|
||
SHIPPING_NOTICE_AGENT_CODE,
|
||
} from '@/features/chat/api/shipping.api';
|
||
import type { ShippingOrderExtractionRun } from '@/features/chat/types/shipping.api.types';
|
||
import { formatOrderExtractionElapsed, resolveOrderExtractionRunStageKey } from '@/features/chat/shipping/orderExtractionProgressDisplay';
|
||
|
||
const vLoading = ElLoading.directive;
|
||
const { t, te } = useI18n();
|
||
const router = useRouter();
|
||
|
||
const loading = ref(false);
|
||
const actingRunNo = ref('');
|
||
const pendingCancelRunNo = ref('');
|
||
const cancelReason = ref('');
|
||
const status = ref('');
|
||
const page = ref(1);
|
||
const pageSize = 20;
|
||
const total = ref(0);
|
||
const rows = ref<ShippingOrderExtractionRun[]>([]);
|
||
const chatAgentId = ref<number | null>(null);
|
||
const error = ref('');
|
||
const statusOptions = ['queued', 'processing', 'completed', 'partially_completed', 'review_required', 'failed', 'cancelled'];
|
||
|
||
/**
|
||
* 解析货代 Chat Agent,供会话深链。
|
||
*/
|
||
async function resolveChatAgent() {
|
||
try {
|
||
const agents = await listShippingChatAgents();
|
||
const found = agents.find((a) => String(a.agent_code || '').trim() === SHIPPING_NOTICE_AGENT_CODE);
|
||
if (found) chatAgentId.value = found.id;
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 分页加载当前用户识别 Run。
|
||
*/
|
||
async function load() {
|
||
loading.value = true;
|
||
error.value = '';
|
||
try {
|
||
const response = await listWorkspaceShippingOrderExtractionRunsPage({
|
||
status: status.value || undefined,
|
||
page: page.value,
|
||
page_size: pageSize,
|
||
});
|
||
rows.value = response.items || [];
|
||
total.value = Number(response.total || 0);
|
||
} catch {
|
||
error.value = t('workspace.shippingNotice.myRuns.loadFailed');
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 用户可见阶段文案;无投影时回退状态文案。
|
||
* v2:key 构造改走共享 resolveOrderExtractionRunStageKey(输出不变,仅事实解析同源)。
|
||
* @param run - 识别 Run
|
||
*/
|
||
function stageLabel(run: ShippingOrderExtractionRun): string {
|
||
const stage = String(run.current_activity?.user_visible_stage || '').trim();
|
||
if (stage) {
|
||
const key = resolveOrderExtractionRunStageKey(stage, run.status);
|
||
if (te(key)) return t(key);
|
||
}
|
||
const statusKey = resolveOrderExtractionRunStageKey(null, run.status);
|
||
return te(statusKey) ? t(statusKey) : run.status;
|
||
}
|
||
|
||
/**
|
||
* 打开所属会话;活动 Run 进入聊天进度面。
|
||
* @param run - 识别 Run
|
||
*/
|
||
async function openInChat(run: ShippingOrderExtractionRun) {
|
||
const agentId = Number(run.agent_id || chatAgentId.value || 0);
|
||
if (!agentId) return;
|
||
const query: Record<string, string> = {};
|
||
const cid = String(run.conversation_id || '').trim();
|
||
if (cid) query.conversation_id = cid;
|
||
if (['review_required', 'partially_completed'].includes(run.status)) {
|
||
query.mode = 'order-review';
|
||
query.run_no = run.run_no;
|
||
}
|
||
await router.push({ path: `/chat/${agentId}`, query });
|
||
}
|
||
|
||
/**
|
||
* 请求取消整批;排队立即取消,执行中进入正在取消。
|
||
* @param run - 识别 Run
|
||
* @param reason - 可选取消原因
|
||
*/
|
||
async function submitCancelRun(run: ShippingOrderExtractionRun, reason?: string | null) {
|
||
if (actingRunNo.value) return;
|
||
actingRunNo.value = run.run_no;
|
||
try {
|
||
await cancelWorkspaceShippingOrderExtractionRun(run.run_no, {
|
||
reason: String(reason || '').trim() || null,
|
||
});
|
||
pendingCancelRunNo.value = '';
|
||
cancelReason.value = '';
|
||
await load();
|
||
} catch {
|
||
error.value = t('chat.shippingOrderExtractionCancel.failed');
|
||
} finally {
|
||
actingRunNo.value = '';
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 展开/收起单行取消表单。
|
||
* @param run - 识别 Run
|
||
*/
|
||
function beginCancelRun(run: ShippingOrderExtractionRun) {
|
||
if (pendingCancelRunNo.value === run.run_no) {
|
||
pendingCancelRunNo.value = '';
|
||
cancelReason.value = '';
|
||
return;
|
||
}
|
||
pendingCancelRunNo.value = run.run_no;
|
||
cancelReason.value = '';
|
||
}
|
||
|
||
/**
|
||
* 重试失败附件。
|
||
* @param run - 识别 Run
|
||
*/
|
||
async function retryRun(run: ShippingOrderExtractionRun) {
|
||
if (actingRunNo.value) return;
|
||
actingRunNo.value = run.run_no;
|
||
try {
|
||
await retryWorkspaceShippingOrderExtractionRun(run.run_no);
|
||
await load();
|
||
} catch {
|
||
error.value = t('chat.shippingOrderExtractionRetryFailed');
|
||
} finally {
|
||
actingRunNo.value = '';
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 状态筛选。清空写回空字符串,接口仍查全部状态。
|
||
* @param value - 状态码;清空为 undefined
|
||
*/
|
||
function onStatusChange(value: string | undefined): void {
|
||
status.value = value ?? '';
|
||
}
|
||
|
||
/**
|
||
* 翻页并重新加载。
|
||
* @param nextPage - 1-based 页码
|
||
*/
|
||
function onPageChange(nextPage: number): void {
|
||
page.value = nextPage;
|
||
void load();
|
||
}
|
||
|
||
watch(status, () => {
|
||
page.value = 1;
|
||
void load();
|
||
});
|
||
|
||
onMounted(async () => {
|
||
await resolveChatAgent();
|
||
await load();
|
||
});
|
||
|
||
defineExpose({ reload: load });
|
||
</script>
|
||
|
||
<template>
|
||
<section class="space-y-3" data-testid="shipping-run-progress-table">
|
||
<div class="flex flex-wrap items-center justify-between gap-3">
|
||
<div>
|
||
<h3>{{ t('workspace.shippingNotice.myRuns.title') }}</h3>
|
||
<ElText type="info" size="small">{{ t('workspace.shippingNotice.myRuns.subtitle') }}</ElText>
|
||
</div>
|
||
<ElSelect
|
||
class="run-status-select"
|
||
:model-value="status || undefined"
|
||
clearable
|
||
:placeholder="t('workspace.shippingNotice.myRuns.allStatuses')"
|
||
:aria-label="t('workspace.shippingNotice.myRuns.status')"
|
||
@update:model-value="onStatusChange"
|
||
>
|
||
<ElOption
|
||
v-for="value in statusOptions"
|
||
:key="value"
|
||
:value="value"
|
||
:label="t(`chat.shippingOrderExtractionCardTitle.${value}`)"
|
||
/>
|
||
</ElSelect>
|
||
</div>
|
||
|
||
<ElText v-if="error" type="danger">{{ error }}</ElText>
|
||
|
||
<ElTable
|
||
v-loading="loading"
|
||
:data="rows"
|
||
row-key="run_no"
|
||
:empty-text="t('workspace.shippingNotice.myRuns.empty')"
|
||
>
|
||
<ElTableColumn prop="run_no" :label="t('workspace.shippingNotice.myRuns.runNo')" min-width="160" />
|
||
<ElTableColumn :label="t('workspace.shippingNotice.myRuns.status')" min-width="160">
|
||
<template #default="{ row }">
|
||
{{ t(`chat.shippingOrderExtractionCardTitle.${(row as ShippingOrderExtractionRun).status}`) }}
|
||
<ElTag
|
||
v-if="(row as ShippingOrderExtractionRun).cancellation?.requested && !(row as ShippingOrderExtractionRun).cancellation?.completed_at"
|
||
type="warning"
|
||
effect="light"
|
||
>
|
||
{{ t('chat.shippingOrderExtractionUserStage.cancellation_requested') }}
|
||
</ElTag>
|
||
</template>
|
||
</ElTableColumn>
|
||
<ElTableColumn :label="t('workspace.shippingNotice.myRuns.stage')" min-width="160">
|
||
<template #default="{ row }">
|
||
<div>{{ stageLabel(row as ShippingOrderExtractionRun) }}</div>
|
||
<ElText v-if="(row as ShippingOrderExtractionRun).current_activity?.file_name" type="info" size="small">
|
||
{{ (row as ShippingOrderExtractionRun).current_activity?.file_name }}
|
||
</ElText>
|
||
</template>
|
||
</ElTableColumn>
|
||
<ElTableColumn :label="t('workspace.shippingNotice.myRuns.summary')" min-width="220">
|
||
<template #default="{ row }">
|
||
{{ t('workspace.shippingNotice.myRuns.summaryCounts', {
|
||
total: (row as ShippingOrderExtractionRun).summary?.total_documents || 0,
|
||
done: (row as ShippingOrderExtractionRun).summary?.completed_documents || 0,
|
||
review: (row as ShippingOrderExtractionRun).summary?.review_required_documents || 0,
|
||
failed: (row as ShippingOrderExtractionRun).summary?.failed_documents || 0,
|
||
cancelled: (row as ShippingOrderExtractionRun).summary?.cancelled_documents || 0,
|
||
}) }}
|
||
</template>
|
||
</ElTableColumn>
|
||
<ElTableColumn :label="t('workspace.shippingNotice.myRuns.elapsed')" min-width="100">
|
||
<template #default="{ row }">
|
||
{{ formatOrderExtractionElapsed((row as ShippingOrderExtractionRun).current_activity?.elapsed_seconds) || '—' }}
|
||
</template>
|
||
</ElTableColumn>
|
||
<ElTableColumn :label="t('workspace.shippingNotice.myRuns.actions')" min-width="220" align="right">
|
||
<template #default="{ row }">
|
||
<ElButton link type="primary" @click="openInChat(row as ShippingOrderExtractionRun)">
|
||
{{ t('workspace.shippingNotice.myRuns.openChat') }}
|
||
</ElButton>
|
||
<template v-if="((row as ShippingOrderExtractionRun).available_actions || []).includes('cancel_run')">
|
||
<div v-if="pendingCancelRunNo === (row as ShippingOrderExtractionRun).run_no">
|
||
<div>{{ t('chat.shippingOrderExtractionCancel.reasonOptional') }}</div>
|
||
<ElInput v-model="cancelReason" type="textarea" :rows="2" />
|
||
<ElButton
|
||
link
|
||
type="danger"
|
||
:loading="actingRunNo === (row as ShippingOrderExtractionRun).run_no"
|
||
@click="submitCancelRun(row as ShippingOrderExtractionRun, cancelReason)"
|
||
>
|
||
{{ actingRunNo === (row as ShippingOrderExtractionRun).run_no
|
||
? t('chat.shippingOrderExtractionCancel.submitting')
|
||
: t('chat.shippingOrderExtractionAction.cancel_run') }}
|
||
</ElButton>
|
||
<ElButton
|
||
link
|
||
:disabled="actingRunNo === (row as ShippingOrderExtractionRun).run_no"
|
||
@click="pendingCancelRunNo = ''; cancelReason = ''"
|
||
>
|
||
{{ t('chat.shippingOrderExtractionCancel.dismiss') }}
|
||
</ElButton>
|
||
</div>
|
||
<ElButton
|
||
v-else
|
||
link
|
||
type="danger"
|
||
:disabled="actingRunNo === (row as ShippingOrderExtractionRun).run_no"
|
||
@click="beginCancelRun(row as ShippingOrderExtractionRun)"
|
||
>
|
||
{{ t('chat.shippingOrderExtractionAction.cancel_run') }}
|
||
</ElButton>
|
||
</template>
|
||
<ElButton
|
||
v-if="((row as ShippingOrderExtractionRun).available_actions || []).includes('retry_failed_attachments')"
|
||
link
|
||
:disabled="actingRunNo === (row as ShippingOrderExtractionRun).run_no"
|
||
@click="retryRun(row as ShippingOrderExtractionRun)"
|
||
>
|
||
{{ t('chat.shippingOrderExtractionAction.retry_failed_attachments') }}
|
||
</ElButton>
|
||
</template>
|
||
</ElTableColumn>
|
||
</ElTable>
|
||
|
||
<div class="flex items-center justify-end gap-3">
|
||
<ElText type="info" size="small">{{ t('workspace.shippingNotice.myRuns.total', { count: total }) }}</ElText>
|
||
<ElPagination
|
||
:current-page="page"
|
||
:page-size="pageSize"
|
||
:total="total"
|
||
layout="prev, pager, next"
|
||
background
|
||
:disabled="loading"
|
||
@current-change="onPageChange"
|
||
/>
|
||
</div>
|
||
</section>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.run-status-select {
|
||
width: 12rem;
|
||
}
|
||
</style>
|