Files
syops/frontend/src/features/workspace/shipping-task-center/ShippingRunProgressTable.vue
T

327 lines
12 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* features/workspace/shipping-task-center/ShippingRunProgressTable.vue
* 用途:任务中心「我的识别」续办表——展示用户自有抽取 Run 的阶段、耗时、取消态与续办动作。
* 依赖:shipping.api 分页列表/取消/重试、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 {
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 { 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('');
/**
* 解析货代 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(输出不变,仅事实解析同源)。
*/
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 进入聊天进度面。
*/
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 });
}
/** 请求取消整批;排队立即取消,执行中进入正在取消。 */
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 = '';
}
}
/**
* 展开/收起单行取消表单。
*/
function beginCancelRun(run: ShippingOrderExtractionRun) {
if (pendingCancelRunNo.value === run.run_no) {
pendingCancelRunNo.value = '';
cancelReason.value = '';
return;
}
pendingCancelRunNo.value = run.run_no;
cancelReason.value = '';
}
/**
* 重试失败附件。
*/
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 = '';
}
}
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 class="text-base font-semibold text-primary">{{ t('workspace.shippingNotice.myRuns.title') }}</h3>
<p class="text-xs text-muted">{{ t('workspace.shippingNotice.myRuns.subtitle') }}</p>
</div>
<label class="flex items-center gap-2 text-sm text-secondary">
{{ t('workspace.shippingNotice.myRuns.status') }}
<select v-model="status" class="input-field py-1.5 text-sm">
<option value="">{{ t('workspace.shippingNotice.myRuns.allStatuses') }}</option>
<option
v-for="value in ['queued', 'processing', 'completed', 'partially_completed', 'review_required', 'failed', 'cancelled']"
:key="value"
:value="value"
>
{{ t(`chat.shippingOrderExtractionCardTitle.${value}`) }}
</option>
</select>
</label>
</div>
<p v-if="error" class="rounded-lg border border-rose-200 bg-rose-50 p-3 text-sm text-rose-700">{{ error }}</p>
<div class="overflow-x-auto rounded-xl border border-default bg-surface">
<table class="min-w-[860px] w-full text-left text-sm">
<thead class="border-b border-default bg-surface-soft text-xs text-muted">
<tr>
<th class="px-3 py-2">{{ t('workspace.shippingNotice.myRuns.runNo') }}</th>
<th class="px-3 py-2">{{ t('workspace.shippingNotice.myRuns.status') }}</th>
<th class="px-3 py-2">{{ t('workspace.shippingNotice.myRuns.stage') }}</th>
<th class="px-3 py-2">{{ t('workspace.shippingNotice.myRuns.summary') }}</th>
<th class="px-3 py-2">{{ t('workspace.shippingNotice.myRuns.elapsed') }}</th>
<th class="px-3 py-2">{{ t('workspace.shippingNotice.myRuns.actions') }}</th>
</tr>
</thead>
<tbody>
<tr v-if="loading">
<td colspan="6" class="px-3 py-8 text-center text-muted">{{ t('workspace.shippingNotice.myRuns.loading') }}</td>
</tr>
<tr v-else-if="!rows.length">
<td colspan="6" class="px-3 py-8 text-center text-muted">{{ t('workspace.shippingNotice.myRuns.empty') }}</td>
</tr>
<tr
v-for="run in rows"
:key="run.run_no"
class="border-b border-default last:border-0"
>
<td class="px-3 py-2 font-mono text-xs">{{ run.run_no }}</td>
<td class="px-3 py-2">
{{ t(`chat.shippingOrderExtractionCardTitle.${run.status}`) }}
<span
v-if="run.cancellation?.requested && !run.cancellation?.completed_at"
class="ml-1 rounded-full bg-amber-100 px-1.5 py-0.5 text-[10px] text-amber-800"
>
{{ t('chat.shippingOrderExtractionUserStage.cancellation_requested') }}
</span>
</td>
<td class="px-3 py-2">
<div>{{ stageLabel(run) }}</div>
<div v-if="run.current_activity?.file_name" class="text-[11px] text-muted">
{{ run.current_activity.file_name }}
</div>
</td>
<td class="px-3 py-2 text-xs text-secondary">
{{ t('workspace.shippingNotice.myRuns.summaryCounts', {
total: run.summary?.total_documents || 0,
done: run.summary?.completed_documents || 0,
review: run.summary?.review_required_documents || 0,
failed: run.summary?.failed_documents || 0,
cancelled: run.summary?.cancelled_documents || 0,
}) }}
</td>
<td class="px-3 py-2 text-xs">
{{ formatOrderExtractionElapsed(run.current_activity?.elapsed_seconds) || '—' }}
</td>
<td class="px-3 py-2">
<div class="flex flex-wrap gap-2">
<button
type="button"
class="text-xs font-medium text-brand-700 hover:underline"
@click="openInChat(run)"
>
{{ t('workspace.shippingNotice.myRuns.openChat') }}
</button>
<template v-if="(run.available_actions || []).includes('cancel_run')">
<div
v-if="pendingCancelRunNo === run.run_no"
class="w-full min-w-[220px] space-y-1.5 rounded-md border border-rose-200 bg-rose-50/60 p-2 dark:border-rose-900/40 dark:bg-rose-950/20"
>
<label class="block text-[11px] text-secondary">
{{ t('chat.shippingOrderExtractionCancel.reasonOptional') }}
</label>
<textarea
v-model="cancelReason"
rows="2"
class="input-field w-full py-1 text-xs"
/>
<div class="flex flex-wrap gap-2">
<button
type="button"
class="text-xs font-medium text-rose-700 hover:underline"
:disabled="actingRunNo === run.run_no"
@click="submitCancelRun(run, cancelReason)"
>
{{ actingRunNo === run.run_no
? t('chat.shippingOrderExtractionCancel.submitting')
: t('chat.shippingOrderExtractionAction.cancel_run') }}
</button>
<button
type="button"
class="text-xs text-muted hover:underline"
:disabled="actingRunNo === run.run_no"
@click="pendingCancelRunNo = ''; cancelReason = ''"
>
{{ t('chat.shippingOrderExtractionCancel.dismiss') }}
</button>
</div>
</div>
<button
v-else
type="button"
class="text-xs font-medium text-rose-700 hover:underline"
:disabled="actingRunNo === run.run_no"
@click="beginCancelRun(run)"
>
{{ t('chat.shippingOrderExtractionAction.cancel_run') }}
</button>
</template>
<button
v-if="(run.available_actions || []).includes('retry_failed_attachments')"
type="button"
class="text-xs font-medium text-secondary hover:underline"
:disabled="actingRunNo === run.run_no"
@click="retryRun(run)"
>
{{ t('chat.shippingOrderExtractionAction.retry_failed_attachments') }}
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="flex items-center justify-end gap-3 text-sm">
<span class="text-xs text-muted">{{ t('workspace.shippingNotice.myRuns.total', { count: total }) }}</span>
<button
type="button"
class="rounded border border-default px-3 py-1.5 disabled:opacity-50"
:disabled="page <= 1 || loading"
@click="page -= 1; load()"
>
{{ t('workspace.shippingNotice.myRuns.previous') }}
</button>
<span>{{ page }}</span>
<button
type="button"
class="rounded border border-default px-3 py-1.5 disabled:opacity-50"
:disabled="page * pageSize >= total || loading"
@click="page += 1; load()"
>
{{ t('workspace.shippingNotice.myRuns.next') }}
</button>
</div>
</section>
</template>