32 lines
1.9 KiB
TypeScript
32 lines
1.9 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: 'log', version: '1.0.0',
|
|
i18n: describe('记录日志', '解析消息值并按指定级别记录日志,同时输出解析后的原始值。'),
|
|
config: [
|
|
{name: 'level', type: 'select', required: true, default: 'info',
|
|
i18n: describe('日志级别', '选择日志的记录级别;错误级别本身不会使节点执行失败。'),
|
|
options: [
|
|
{value: 'info', i18n: describe('信息', '记录一般运行信息。')},
|
|
{value: 'warning', i18n: describe('警告', '记录需要关注的情况。')},
|
|
{value: 'error', i18n: describe('错误', '记录错误信息,仍按正常日志节点继续执行。')},
|
|
]},
|
|
{name: 'message', type: 'value_source', required: true,
|
|
i18n: describe('日志消息', '使用字面量或状态引用;字符串直接记录,其他 JSON 值序列化后记录。')},
|
|
],
|
|
output: output('json', '消息原值', '解析 message 得到的原始 JSON 值,非字符串值不会因日志序列化而改变输出类型。'),
|
|
ports: [success_port, failure_port],
|
|
};
|
|
|
|
export const execute: NodeHandler = async (node, state, signal, deps) => {
|
|
if (node.type !== 'log') throw new Error('Expected a log node');
|
|
signal.throwIfAborted();
|
|
const output = resolve_value(node.config.message, state);
|
|
deps.log(node.config.level, typeof output === 'string' ? output : JSON.stringify(output));
|
|
signal.throwIfAborted();
|
|
return {port: 'success', result: {status: 'success', output}};
|
|
};
|