36 lines
2.0 KiB
TypeScript
36 lines
2.0 KiB
TypeScript
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);
|
|
}
|