import {randomUUID} from 'node:crypto'; import type {Bot} from '@wp2508/mineflayer'; import type {BaseFunction, FunctionExecutionContext, FunctionI18n} from '../../../types/bot/function/function.ts'; import type {WorkflowAction} from '../../../types/bot/workflow/runtime.ts'; export abstract class WorkflowControlFunction implements BaseFunction<[string]> { abstract readonly name: WorkflowAction; abstract readonly i18n: FunctionI18n; readonly execution = 'workflow_control'; readonly args = [{name: 'workflow_id', type: 'string' as const, i18n: {lang: 'zh-CN', name: '工作流 ID', description: '目标工作流的唯一标识'}}]; async run(_bot: Bot, args: [string], executionContext?: FunctionExecutionContext): Promise { if (!Array.isArray(args) || args.length !== 1 || typeof args[0] !== 'string' || args[0].length < 1 || args[0].length > 100 || /[^a-zA-Z0-9_-]/.test(args[0]) || ['__proto__', 'prototype', 'constructor'].includes(args[0])) return false; const workflow = executionContext?.workflow; if (!workflow) return false; try { const command = workflow.control({command_id: randomUUID(), action: this.name, ...(this.name === 'stop_run' ? {run_id: args[0]} : {workflow_id: args[0]})}); // Acknowledgement is enough; waiting for completion would deadlock a self-stop. return command.state === 'accepted' || command.state === 'completed'; } catch { return false; } } }