chore: initial commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import type {Json} from '../../../../types/bot/workflow/workflow.ts';
|
||||
import type {WorkflowEventTemplate} from '../../../../types/bot/workflow/metadata.ts';
|
||||
import {describe, output, event_args_output} from '../../metadata.ts';
|
||||
import {normalize_custom_event} from './custom.ts';
|
||||
|
||||
export const config_template: WorkflowEventTemplate = {
|
||||
name: 'chat', version: '1.0.0', readiness: 'spawned',
|
||||
i18n: describe('聊天事件', '接收玩家公开聊天,提供用户名、消息内容及安全的原始参数。'),
|
||||
config: [],
|
||||
output: {
|
||||
...output('object', '聊天数据', '从聊天事件前两个字符串参数提取的数据。'),
|
||||
properties: {
|
||||
args: event_args_output,
|
||||
event: output('string', '事件名称', '固定为 chat。'),
|
||||
username: output('string', '用户名', '发送公开聊天消息的玩家名称。'),
|
||||
message: output('string', '消息内容', '玩家发送的聊天文本。'),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export function normalize(args: unknown[]): Record<string, Json> | undefined {
|
||||
if (args.length > 32 || typeof args[0] !== 'string' || typeof args[1] !== 'string') return;
|
||||
const original = normalize_custom_event('chat', args);
|
||||
if (!original) return;
|
||||
const trigger = {...original, username: args[0], message: args[1]};
|
||||
if (Buffer.byteLength(JSON.stringify(trigger)) <= 65_536) return trigger;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import type {Json} from '../../../../types/bot/workflow/workflow.ts';
|
||||
import type {WorkflowEventTemplate} from '../../../../types/bot/workflow/metadata.ts';
|
||||
import {assert_json} from '../../runtime/value.ts';
|
||||
import {describe, output} from '../../metadata.ts';
|
||||
|
||||
export const config_template: WorkflowEventTemplate = {
|
||||
name: 'custom', version: '1.0.0', readiness: 'spawned', custom: true,
|
||||
i18n: describe('自定义事件', '监听自定义名称的事件,以有界 JSON 参数数组输出。any 事件保留原始事件名称作为首个参数。'),
|
||||
config: [{name: 'event', type: 'string', required: true,
|
||||
i18n: describe('事件名称', '填写要监听的事件名称;workflow_update 及 any 转发的 workflow_update 不触发工作流。')}],
|
||||
output: {
|
||||
...output('object', '自定义事件数据', '最多 32 个参数,输出 JSON 最大 64 KiB;无法安全转换的参数替换为 null。'),
|
||||
properties: {
|
||||
event: output('string', '事件名称', '订阅的事件名称;订阅 any 时为 any。'),
|
||||
args: {...output('array', '事件参数', '按原始顺序复制的 JSON 参数;非 JSON 对象或不安全值为 null。'),
|
||||
items: {...output('json', '参数值', '事件参数的 JSON 副本,无法安全转换时为 null。'), nullable: true}},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export function normalize_custom_event(event: string, args: unknown[]): Record<string, Json> | undefined {
|
||||
try {
|
||||
if (args.length > 32) return;
|
||||
const trigger = {event, args: args.map(value => {
|
||||
try { assert_json(value); return structuredClone(value); }
|
||||
catch { return null; }
|
||||
})};
|
||||
assert_json(trigger);
|
||||
if (Buffer.byteLength(JSON.stringify(trigger)) <= 65_536) return trigger;
|
||||
} catch { /* Unsupported or oversized payloads skip this occurrence. */ }
|
||||
}
|
||||
|
||||
export function normalize(args: unknown[]): Record<string, Json> | undefined {
|
||||
return normalize_custom_event('custom', args);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type {Json} from '../../../../types/bot/workflow/workflow.ts';
|
||||
import type {WorkflowEventTemplate} from '../../../../types/bot/workflow/metadata.ts';
|
||||
import {describe, output, event_args_output} from '../../metadata.ts';
|
||||
import {normalize_custom_event} from './custom.ts';
|
||||
|
||||
export const config_template: WorkflowEventTemplate = {
|
||||
name: 'health', version: '1.0.0', readiness: 'spawned',
|
||||
i18n: describe('生命状态事件', '生命状态更新时发出通知。生命值和饥饿值需通过 info 查询单独读取。'),
|
||||
config: [],
|
||||
output: {
|
||||
...output('object', '生命状态通知', '包含事件名称和原始参数,不包含生命值或饥饿值快照。'),
|
||||
properties: {event: output('string', '事件名称', '固定为 health。'), args: event_args_output},
|
||||
},
|
||||
};
|
||||
|
||||
export function normalize(args: unknown[] = []): Record<string, Json> | undefined {
|
||||
return normalize_custom_event('health', args);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import {MinecraftJsonParser} from '../../../../utils/message_json_parser.ts';
|
||||
import type {Json} from '../../../../types/bot/workflow/workflow.ts';
|
||||
import type {WorkflowEventTemplate} from '../../../../types/bot/workflow/metadata.ts';
|
||||
import {assert_json} from '../../runtime/value.ts';
|
||||
import {describe, output} from '../../metadata.ts';
|
||||
|
||||
export const config_template: WorkflowEventTemplate = {
|
||||
name: 'message', version: '1.0.0', readiness: 'spawned',
|
||||
i18n: describe('消息事件', '接收聊天或系统消息,将原始文本组件解析为用户名、消息和完整纯文本。'),
|
||||
config: [],
|
||||
output: {
|
||||
...output('object', '消息数据', '解析后的消息内容和协议附加信息。'),
|
||||
properties: {
|
||||
event: output('string', '事件名称', '固定为 message。'),
|
||||
username: output('string', '用户名', '从消息文本提取的用户名;无法识别时为空字符串。'),
|
||||
message: output('string', '消息内容', '去除已识别用户名后的消息;无法识别时为裁剪首尾空白的纯文本。'),
|
||||
plainText: output('string', '完整纯文本', '展开翻译和文本组件后得到的完整消息文本。'),
|
||||
position: output('string', '显示位置', '协议提供的字符串位置;缺失或类型不符时为空字符串。'),
|
||||
sender: {...output('string', '发送者', '协议提供的发送者标识;缺失或类型不符时为 null。'), nullable: true},
|
||||
verified: {...output('boolean', '签名验证', '协议提供的验证结果;缺失或类型不符时为 null。'), nullable: true},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export function parse_message_trigger(args: unknown[]): Record<string, Json> | undefined {
|
||||
try {
|
||||
if (args.length > 32) return;
|
||||
let component = args[0];
|
||||
if (component && typeof component === 'object' && !Array.isArray(component)) {
|
||||
const json = Object.getOwnPropertyDescriptor(component, 'json');
|
||||
if (json && 'value' in json) component = json.value;
|
||||
}
|
||||
assert_json(component);
|
||||
if (Buffer.byteLength(JSON.stringify(component)) > 65_536) return;
|
||||
const parsed = MinecraftJsonParser.parse(component);
|
||||
const trigger: Record<string, Json> = {
|
||||
event: 'message', username: parsed.username, message: parsed.message, plainText: parsed.plainText,
|
||||
position: typeof args[1] === 'string' ? args[1] : '',
|
||||
sender: typeof args[2] === 'string' ? args[2] : null,
|
||||
verified: typeof args[3] === 'boolean' ? args[3] : null,
|
||||
};
|
||||
assert_json(trigger);
|
||||
if (Buffer.byteLength(JSON.stringify(trigger)) <= 65_536) return trigger;
|
||||
} catch { /* Malformed or non-JSON message payloads are not workflow inputs. */ }
|
||||
}
|
||||
|
||||
export const normalize = parse_message_trigger;
|
||||
@@ -0,0 +1,19 @@
|
||||
import type {Json} from '../../../../types/bot/workflow/workflow.ts';
|
||||
import type {WorkflowEventDefinition} from '../../../../types/bot/workflow/metadata.ts';
|
||||
import * as message from './message.ts';
|
||||
import * as chat from './chat.ts';
|
||||
import * as whisper from './whisper.ts';
|
||||
import * as spawn from './spawn.ts';
|
||||
import * as health from './health.ts';
|
||||
import * as custom from './custom.ts';
|
||||
|
||||
export const event_registry = {message, chat, whisper, spawn, health, custom} satisfies Record<string, WorkflowEventDefinition>;
|
||||
|
||||
export function normalize_event(event: string, args: unknown[]): Record<string, Json> | undefined {
|
||||
if (args.length > 32) return;
|
||||
if (Object.hasOwn(event_registry, event)) {
|
||||
const definition: WorkflowEventDefinition = event_registry[event as keyof typeof event_registry];
|
||||
return definition.normalize(args);
|
||||
}
|
||||
return custom.normalize_custom_event(event, args);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type {Json} from '../../../../types/bot/workflow/workflow.ts';
|
||||
import type {WorkflowEventTemplate} from '../../../../types/bot/workflow/metadata.ts';
|
||||
import {describe, output, event_args_output} from '../../metadata.ts';
|
||||
import {normalize_custom_event} from './custom.ts';
|
||||
|
||||
export const config_template: WorkflowEventTemplate = {
|
||||
name: 'spawn', version: '1.0.0', readiness: 'spawned',
|
||||
i18n: describe('出生事件', '通知机器人已出生;输出不包含机器人实例。'),
|
||||
config: [],
|
||||
output: {
|
||||
...output('object', '出生通知', '机器人出生事件的通知数据。'),
|
||||
properties: {event: output('string', '事件名称', '固定为 spawn。'), args: event_args_output},
|
||||
},
|
||||
};
|
||||
|
||||
export function normalize(args: unknown[] = []): Record<string, Json> | undefined {
|
||||
return normalize_custom_event('spawn', args);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import type {Json} from '../../../../types/bot/workflow/workflow.ts';
|
||||
import type {WorkflowEventTemplate} from '../../../../types/bot/workflow/metadata.ts';
|
||||
import {describe, output, event_args_output} from '../../metadata.ts';
|
||||
import {normalize_custom_event} from './custom.ts';
|
||||
|
||||
export const config_template: WorkflowEventTemplate = {
|
||||
name: 'whisper', version: '1.0.0', readiness: 'spawned',
|
||||
i18n: describe('私聊事件', '接收玩家私聊,提供用户名、消息内容及安全的原始参数。'),
|
||||
config: [],
|
||||
output: {
|
||||
...output('object', '私聊数据', '从私聊事件前两个字符串参数提取的数据。'),
|
||||
properties: {
|
||||
args: event_args_output,
|
||||
event: output('string', '事件名称', '固定为 whisper。'),
|
||||
username: output('string', '用户名', '发送私聊消息的玩家名称。'),
|
||||
message: output('string', '消息内容', '玩家发送的私聊文本。'),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export function normalize(args: unknown[]): Record<string, Json> | undefined {
|
||||
if (args.length > 32 || typeof args[0] !== 'string' || typeof args[1] !== 'string') return;
|
||||
const original = normalize_custom_event('whisper', args);
|
||||
if (!original) return;
|
||||
const trigger = {...original, username: args[0], message: args[1]};
|
||||
if (Buffer.byteLength(JSON.stringify(trigger)) <= 65_536) return trigger;
|
||||
}
|
||||
Reference in New Issue
Block a user