Files
image-workbench-desktop/src/runtime-record.js
T

83 lines
4.3 KiB
JavaScript

;(function (global) {
'use strict'
const states = ['queued', 'running', 'awaiting-result', 'succeeded', 'failed', 'cancelled', 'interrupted', 'recoverable']
const transitions = {
queued: ['running', 'cancelled', 'interrupted', 'recoverable'],
running: ['queued', 'awaiting-result', 'succeeded', 'failed', 'cancelled', 'interrupted'],
'awaiting-result': ['succeeded', 'failed', 'cancelled', 'interrupted'],
interrupted: ['recoverable', 'failed', 'cancelled'],
recoverable: ['queued', 'failed', 'cancelled'],
failed: ['queued', 'cancelled', 'recoverable'],
succeeded: [],
cancelled: [],
}
const terminal = { succeeded: true, failed: true, cancelled: true }
function now() { return Date.now() }
function text(value, fallback) { return value === undefined || value === null ? fallback : String(value) }
function safeError(error) {
if (!error) return null
if (typeof error === 'string') return error.slice(0, 240)
return text(error.message || error.code, '任务失败').slice(0, 240)
}
function create(input) {
const source = input || {}
const state = states.indexOf(source.state) >= 0 ? source.state : 'queued'
const createdAt = Number(source.createdAt) || now()
return {
id: text(source.id, 'run-' + createdAt.toString(36)),
kind: text(source.kind, 'generation'),
taskId: text(source.taskId, ''),
nodeId: text(source.nodeId, ''),
state,
createdAt,
startedAt: Number(source.startedAt) || 0,
finishedAt: Number(source.finishedAt) || 0,
attempt: Math.max(0, Number(source.attempt) || 0),
retryCount: Math.max(0, Number(source.retryCount) || 0),
errorClass: text(source.errorClass, ''),
retryable: !!source.retryable,
cancelReason: text(source.cancelReason, ''),
durationMs: Math.max(0, Number(source.durationMs) || 0),
error: safeError(source.error),
}
}
function canTransition(from, to) { return !!(transitions[from] && transitions[from].indexOf(to) >= 0) }
function transition(record, next, patch) {
const current = create(record)
if (!canTransition(current.state, next)) return { ok: false, error: 'invalid-transition', from: current.state, to: next, value: current }
const value = Object.assign({}, current, patch || {}, { state: next })
if (next === 'running') { value.attempt = Math.max(1, value.attempt + 1); value.startedAt = value.startedAt || now() }
if (next === 'queued' && current.state === 'running') value.retryCount += 1
if (terminal[next] || next === 'interrupted') {
value.finishedAt = value.finishedAt || now()
value.durationMs = value.startedAt ? Math.max(0, value.finishedAt - value.startedAt) : value.durationMs
}
return { ok: true, value: create(value) }
}
function fromLegacy(input) {
const source = input || {}
let state = source.state || source.status || 'queued'
if (state === 'completed') state = 'succeeded'
if (state === 'partial-failed') state = 'failed'
if (states.indexOf(state) < 0) state = 'queued'
return create(Object.assign({}, source, { state, taskId: source.taskId || source.promptId || '' }))
}
function markInterrupted(record) {
const current = fromLegacy(record)
if (current.state === 'running') return transition(current, 'interrupted', { errorClass: 'lifecycle', error: '页面或运行上下文中断' })
return { ok: true, value: current }
}
function toDiagnostic(record) {
const value = fromLegacy(record)
return { id: value.id, kind: value.kind, taskId: value.taskId, nodeId: value.nodeId, state: value.state, attempt: value.attempt, retryCount: value.retryCount, errorClass: value.errorClass || undefined, retryable: value.retryable, durationMs: value.durationMs || undefined }
}
function summary(records) {
const result = { total: 0, queued: 0, running: 0, 'awaiting-result': 0, succeeded: 0, failed: 0, cancelled: 0, interrupted: 0, recoverable: 0 }
;(Array.isArray(records) ? records : []).forEach((record) => { const state = fromLegacy(record).state; result.total++; if (result[state] !== undefined) result[state]++ })
return result
}
global.IWBRuntimeRecord = { states, transitions, terminal, create, fromLegacy, canTransition, transition, markInterrupted, toDiagnostic, summary }
})(typeof window !== 'undefined' ? window : globalThis)