feat: 接入持久化工作流执行状态

This commit is contained in:
GouJ committed 2026-09-23 19:53:55 +08:00
1 parent 7d2900c705
commit 3caea26924
14 files changed
+532 -44

No files matched your search

@@ -0,0 +1,107 @@
<script setup lang="ts">
import { computed } from 'vue'
import { NAlert, NButton, NProgress, NTag } from 'naive-ui'
import { RefreshCw, X } from '@lucide/vue'
import { formatDate } from '../../lib/format'
import { executionPercentage, executionStageLabel } from './execution'
import type { WorkflowExecution } from './execution.types'
const props = withDefaults(
defineProps<{ title?: string; execution: WorkflowExecution | null; loading: boolean; error: string }>(),
{ title: '后台任务' }
)
const emit = defineEmits<{ refresh: []; dismiss: [] }>()
const percentage = computed(() => executionPercentage(props.execution?.current ?? 0, props.execution?.total ?? 0))
const result = computed(() => props.execution?.result ?? {})
const statusLabel = computed(() => {
const labels = { pending: '等待开始', running: '执行中', completed: '已完成', failed: '失败' }
return props.execution ? labels[props.execution.status] : ''
})
const statusType = computed(() =>
props.execution?.status === 'completed' ? 'success' : props.execution?.status === 'failed' ? 'error' : 'warning'
)
const finished = computed(() => ['completed', 'failed'].includes(props.execution?.status ?? ''))
const videoQuality = computed(() => props.execution?.workflowName === 'video-quality')
</script>
<template>
<section v-if="execution || error" class="panel mt-4 p-5" :aria-label="title">
<div class="flex flex-wrap items-start justify-between gap-3">
<div>
<div class="flex flex-wrap items-center gap-2">
<h3 class="text-sm font-semibold">{{ title }}</h3>
<NTag v-if="execution" size="small" :type="statusType" :bordered="false">{{ statusLabel }}</NTag>
</div>
<p v-if="execution" class="mt-2 text-xs text-muted">
{{ executionStageLabel(execution.stage) }} · 更新于 {{ formatDate(execution.updatedAt) }}
</p>
</div>
<div class="flex items-center gap-2">
<NButton
class="icon-button"
quaternary
:loading="loading"
aria-label="刷新后台任务状态"
title="刷新后台任务状态"
@click="emit('refresh')"
><RefreshCw :size="15" /></NButton
><NButton
v-if="finished || error"
class="icon-button"
quaternary
aria-label="隐藏后台任务记录"
title="隐藏后台任务记录"
@click="emit('dismiss')"
><X :size="15" /></NButton
>
</div>
</div>
<template v-if="execution">
<NProgress
v-if="execution.total > 0"
type="line"
:percentage="percentage"
:height="5"
:show-indicator="false"
class="mt-4"
/>
<p class="mt-3 text-xs">
<template v-if="execution.total > 0">已处理 {{ execution.current }} / {{ execution.total }}</template>
<template v-else>后端正在确定本次任务范围</template>
<template v-if="execution.currentResourceId">
· 当前镜头 <code>{{ execution.currentResourceId }}</code></template
>
</p>
<NAlert v-if="execution.status === 'running'" type="info" :show-icon="false" class="mt-3">
{{
videoQuality
? '后端会继续生成、校验并按需修复。'
: '后端会继续处理剩余镜头并保存已完成的提示词。'
}}可以离开或刷新页面,返回后会继续显示本次任务进度,请勿重复提交。
</NAlert>
<NAlert v-else-if="execution.status === 'completed'" type="success" :show-icon="false" class="mt-3">
<p>本轮任务已结束,页面正在重新读取镜头资产状态。</p>
<p v-if="videoQuality" class="mt-2 text-xs">
目标 {{ result.targetCount ?? execution.total }} · 通过 {{ result.passed ?? 0 }} · 修复耗尽
{{ result.exhausted ?? 0 }} · 模型失败 {{ result.providerFailed ?? 0 }} · 提交失败
{{ result.submissionFailed ?? 0 }}
</p>
<p v-else class="mt-2 text-xs">
共 {{ result.total ?? execution.total }} · 生成 {{ result.generated ?? execution.current }} · 跳过
{{ result.skipped ?? 0 }} · 阻塞 {{ result.blocked ?? 0 }} · 失败 {{ result.failed ?? 0 }}
</p>
</NAlert>
<NAlert v-else-if="execution.status === 'failed'" type="error" :show-icon="false" class="mt-3">
<p>{{ execution.error || '后台任务执行失败。' }}</p>
<p class="mt-2 text-xs">
请先刷新下方视频状态,确认已成功的镜头;处理报错原因后,只对仍处于“可执行”的镜头重新提交。
</p>
</NAlert>
<p class="mt-3 break-all font-mono text-[10px] text-muted">执行 ID · {{ execution.id }}</p>
</template>
<NAlert v-if="error" type="error" :show-icon="false" class="mt-3">
{{ error }}。任务可能仍在后端运行,请先点击刷新;不要直接重复提交。
</NAlert>
</section>
</template>
+8
View File
@@ -0,0 +1,8 @@
import { request } from '../../lib/http'
import type { WorkflowExecution } from './execution.types'
/** WorkflowExecution 查询接口;数据库状态是任务进度的权威来源。 */
export const workflowExecutionApi = {
get: (executionId: string, signal?: AbortSignal) =>
request<WorkflowExecution>(`/workflow-executions/${encodeURIComponent(executionId)}`, { signal })
}
+88
View File
@@ -0,0 +1,88 @@
import { computed, ref, watch, type Ref } from 'vue'
import { usePolling } from '../../composables/usePolling'
import { workflowExecutionApi } from './execution-api'
const storagePrefix = 'short-drama:workflow-execution'
/** 任务阶段短标签;未知后端阶段保留原值,避免静默隐藏新能力。 */
export function executionStageLabel(stage: string | null) {
if (!stage) return '等待后端开始'
const labels: Record<string, string> = {
video_prompt: '生成视频提示词',
submitting: '提交视频候选',
generating: '生成视频',
validating: '质量校验',
repairing: '自动修复',
passed: '校验通过',
exhausted: '修复次数已用完',
provider_failed: '模型生成失败',
submission_failed: '任务提交失败',
completed: '执行完成',
failed: '执行失败'
}
return labels[stage] ?? stage
}
/** 有总量时按真实计数展示进度;未知总量保持不确定进度。 */
export function executionPercentage(current: number, total: number) {
return total > 0 ? Math.min(100, Math.max(0, Math.round((current / total) * 100))) : 0
}
function storageKey(projectId: string, workflowName: string) {
return `${storagePrefix}:${projectId}:${workflowName}`
}
function savedExecutionId(projectId: string, workflowName: string) {
if (!projectId || typeof localStorage === 'undefined') return ''
return localStorage.getItem(storageKey(projectId, workflowName)) ?? ''
}
/** 跟踪一个项目最近的持久化任务;刷新页面后从本地句柄恢复,状态仍从后端读取。 */
export function useWorkflowExecution(projectId: Ref<string>, workflowName: string) {
const executionId = ref('')
const terminal = ref(false)
watch(
projectId,
value => {
executionId.value = savedExecutionId(value, workflowName)
terminal.value = false
},
{ immediate: true }
)
const key = computed(() => executionId.value)
const query = usePolling(
key,
(id, signal) => (id ? workflowExecutionApi.get(id, signal) : Promise.resolve(null)),
computed(() => (executionId.value && !terminal.value ? 1_500 : false))
)
watch(
() => query.data.value?.status,
status => {
terminal.value = status === 'completed' || status === 'failed'
}
)
const active = computed(
() =>
!!executionId.value &&
(!query.data.value || ['pending', 'running'].includes(query.data.value.status))
)
/** 保存后端返回的真实 executionId,随后立即读取一次权威状态。 */
async function track(id: string) {
if (!id || !projectId.value) return
if (typeof localStorage !== 'undefined') localStorage.setItem(storageKey(projectId.value, workflowName), id)
terminal.value = false
executionId.value = id
await query.refresh()
}
/** 只隐藏当前观察记录,不取消后端任务。 */
function dismiss() {
if (projectId.value && typeof localStorage !== 'undefined')
localStorage.removeItem(storageKey(projectId.value, workflowName))
executionId.value = ''
terminal.value = false
}
return { executionId, query, active, track, dismiss }
}
+28
View File
@@ -0,0 +1,28 @@
/** 后端持久化长任务状态;字段以 WorkflowExecution 查询接口为准。 */
export type WorkflowExecutionStatus = 'pending' | 'running' | 'completed' | 'failed'
/** 可持久化、可在刷新页面后继续观察的工作流执行记录。 */
export interface WorkflowExecution {
id: string
projectId: string
workflowName: string
status: WorkflowExecutionStatus
stage: string | null
current: number
total: number
currentResourceId: string | null
metadata: Record<string, unknown> | null
result: Record<string, unknown> | null
error: string | null
startedAt: string | null
completedAt: string | null
createdAt: string
updatedAt: string
}
/** 后台任务启动接口只返回观察句柄,不把它误认为最终生产结果。 */
export interface WorkflowExecutionStart {
executionId: string
status: 'pending' | 'running'
total?: number
}