98 lines
3.7 KiB
TypeScript
98 lines
3.7 KiB
TypeScript
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> = {
|
|
createProject: '初始化项目',
|
|
generateCharacter: '生成角色设定',
|
|
generateWorld: '生成世界观',
|
|
generateEpisode: '生成分集剧本',
|
|
reviewDrama: '审核剧本',
|
|
generateRewriteSuggestion: '生成改写建议',
|
|
rewrite: '改写剧本',
|
|
finishProject: '完成剧本创作',
|
|
breakdown: '拆解剧本',
|
|
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 }
|
|
}
|