Files
2026-10-22 17:07:08 +08:00

36 lines
2.3 KiB
TypeScript

import type {NodeHandler} from '../../../types/bot/workflow/runtime.ts';
import type {WorkflowNodeTemplate} from '../../../types/bot/workflow/metadata.ts';
import {describe, failure_port, output, success_port} from '../metadata.ts';
import {resolve_value} from '../runtime/value.ts';
export const config_template: WorkflowNodeTemplate = {
name: 'function', version: '1.0.0',
i18n: describe('调用函数', '解析参数值后调用机器人函数,返回 true 时成功,否则进入失败分支。'),
provider: 'function',
config: [
{name: 'path', type: 'string', required: true,
i18n: describe('函数路径', '函数所属目录,以 / 分隔;每段为 1 至 100 个字母、数字、下划线或连字符,禁止保留名称。')},
{name: 'name', type: 'string', required: true, max_length: 100,
i18n: describe('函数名称', '函数标识,由 1 至 100 个字母、数字、下划线或连字符组成,禁止保留名称。')},
{name: 'args', type: 'array', required: true, default: [], max: 100,
i18n: describe('函数参数', '按调用顺序提供参数,最多 100 项,每项可使用字面量或状态引用。'),
item: {name: 'argument', type: 'value_source', required: true,
i18n: describe('参数值', '解析字面量或状态引用,将得到的 JSON 值作为对应位置的函数参数。')}},
],
output: {...output('boolean', '函数返回值', '函数返回 true 时输出 true;返回其他值时输出 false,错误代码为 FUNCTION_RETURNED_FALSE;异常或超时输出 null。'), nullable: true},
ports: [success_port, failure_port],
};
export const execute: NodeHandler = async (node, state, signal, deps) => {
if (node.type !== 'function') throw new Error('Expected a function node');
signal.throwIfAborted();
const output = await deps.run_function(node.config.path, node.config.name,
node.config.args.map(source => resolve_value(source, state)), signal);
signal.throwIfAborted();
if (output !== true) {
return {port: 'failure', result: {status: 'failure', output: false,
error: {code: 'FUNCTION_RETURNED_FALSE', message: 'Function returned false', node_id: node.id}}};
}
return {port: 'success', result: {status: 'success', output}};
};