feat: 接入持久化工作流执行状态
This commit is contained in:
1 parent
7d2900c705
commit
3caea26924
14 files changed
+532
-44
No files matched your search
@@ -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 }
|
||||
}
|
||||
Reference in new issue
Block a user