const assetVersion = new URL(import.meta.url).searchParams.get('v'); function versionedModuleUrl(path) { const url = new URL(path, import.meta.url); if (assetVersion) url.searchParams.set('v', assetVersion); return url.href; } const taskRuntime = await import(versionedModuleUrl('./task-observer.js')); // Own the instance action contract: safe transport, receipt announcement, // terminal interpretation and fence messages. Polling remains in task-observer. export function createAdminInstanceActions({ runtimeFactory = globalThis.VenixAdminRequestRuntime?.create, tasks = taskRuntime, ...transportOptions } = {}) { if (typeof runtimeFactory !== 'function') { throw new TypeError('administrator request runtime is unavailable'); } const runtime = runtimeFactory({ ...transportOptions, taskReceiptValidator: tasks.taskUrl }); function failureMessage(task, fallback) { const recoveryNotice = tasks.recoveryTaskFailureNotice(task); if (recoveryNotice) return recoveryNotice; const message = task?.error || fallback; if (task?.effect_may_have_applied) { return `${message} 任务已失败,操作效果可能已经发生,相关资源已锁定。请勿再次提交,并将任务编号交由管理员核查。`; } return task?.safe_to_resubmit === false ? `${message} 任务已失败且相关资源已锁定,请勿再次提交,并联系管理员核查。` : message; } function blockMessage(task, action) { if (task?.blocks_mutation !== true) return ''; const id = task?.id || '—'; if (task.completed === false && task.status === 'active') { return `实例已有任务 #${id}(执行中),完成前不能${action}。`; } if (task.completed === true && task.recovery?.pending === true) { const code = String(task.recovery?.code || task.finalization_error_code || ''); if (code === 'recovery_dispatch_ledger_conflict' || task.dispatch_state === 'invalid') { return `任务 #${id} 的派发账本异常,资源保持安全锁定;修复账本并完成核验前不能${action}。`; } if (task.dispatch_state === 'not_dispatched') { return `任务 #${id} 已确认未派发到宿主机,但 PHP 本地账本收尾尚未完成;收尾成功前不能${action}。`; } return `任务 #${id} 尚在持续恢复,资源保持安全锁定;恢复收敛前不能${action}。`; } if (task.completed === true && task.status === 'failed') { return `任务 #${id} 已失败,资源安全锁定,需管理员人工处理后才能${action}。`; } return `任务 #${id} 状态协议无效,资源安全锁定,需管理员人工处理后才能${action}。`; } function watch(receipt, { title, onResult, ...options }) { tasks.announceTaskAccepted(receipt, { title }); return tasks.observeTask(receipt, { ...options, onTerminal(task) { const succeeded = tasks.taskSucceededWithoutRecovery(task); const label = `${title}任务 #${receipt.task_id || '—'}`; onResult?.({ task, succeeded, message: succeeded ? `${label} 已成功。` : failureMessage(task, `${label} 执行失败。`) }); }, }); } return Object.freeze({ read: (path, controller) => runtime.read(path, { signal: controller?.signal, timeoutMs: 30000 }), commit: (path, body, controller, method = 'POST') => runtime.mutate(path, { method, body, signal: controller?.signal, response: 'json', }), enqueue: (path, body, controller, method = 'PATCH') => runtime.mutate(path, { method, body, signal: controller?.signal, response: 'task', }), watch, blockMessage, }); }