chore: initial commit
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import type {base_define} from '../../../types/bot/plugins/plugin.ts';
|
||||
|
||||
export const define: base_define = {
|
||||
path: 'workflow',
|
||||
i18n: [{lang: 'zh-CN', name: '工作流控制', description: '运行、停止、管理工作流触发器和重新加载工作流'}],
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
import {WorkflowControlFunction} from './shared.ts';
|
||||
|
||||
export class init extends WorkflowControlFunction {
|
||||
readonly name = 'reload';
|
||||
readonly i18n = {lang: 'zh-CN', name: '重新加载工作流', description: '提交重新加载指定工作流的请求,接受后立即返回,不等待加载完成'};
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import {WorkflowControlFunction} from './shared.ts';
|
||||
|
||||
export class init extends WorkflowControlFunction {
|
||||
readonly name = 'run_once';
|
||||
readonly i18n = {lang: 'zh-CN', name: '运行工作流一次', description: '提交工作流单次运行请求,接受后立即返回,不等待运行完成'};
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
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<boolean> {
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import {WorkflowControlFunction} from './shared.ts';
|
||||
|
||||
export class init extends WorkflowControlFunction {
|
||||
readonly name = 'stop';
|
||||
readonly i18n = {lang: 'zh-CN', name: '停止工作流', description: '提交停止指定工作流的请求,接受后立即返回,不等待运行结束'};
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import {WorkflowControlFunction} from './shared.ts';
|
||||
|
||||
export class init extends WorkflowControlFunction {
|
||||
readonly name = 'stop_run';
|
||||
readonly i18n = {lang: 'zh-CN', name: '停止工作流运行实例', description: '提交停止指定运行实例的请求,接受后立即返回,不等待实例结束'};
|
||||
readonly args = [{name: 'run_id', type: 'string' as const,
|
||||
i18n: {lang: 'zh-CN', name: '运行实例 ID', description: '目标工作流运行实例的唯一标识'}}];
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import {WorkflowControlFunction} from './shared.ts';
|
||||
|
||||
export class init extends WorkflowControlFunction {
|
||||
readonly name = 'trigger_start';
|
||||
readonly i18n = {lang: 'zh-CN', name: '启动工作流触发器', description: '提交启动指定工作流触发器的请求,接受后立即返回'};
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import {WorkflowControlFunction} from './shared.ts';
|
||||
|
||||
export class init extends WorkflowControlFunction {
|
||||
readonly name = 'trigger_stop';
|
||||
readonly i18n = {lang: 'zh-CN', name: '停止工作流触发器', description: '提交停止指定工作流触发器的请求,接受后立即返回'};
|
||||
}
|
||||
Reference in New Issue
Block a user