| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- /** 语句:echo 打印信息(写入 log + UI);仅 inVars:字符串列表,支持 {{var}} / {var},由执行阶段替换 */
- const { assertStrictKeys } = require('../action-schema.js')
- const types = ['echo']
- /**
- * 统一打印结点报错:写入 log.txt 并可选通知 UI。所有结点失败时均由此处输出,无需在各结点内单独 logMessage。
- * @param {object} action - 当前执行的 action
- * @param {{ success: boolean, error?: string }} result - 执行结果(success 为 false 时应有 error)
- * @param {{ getActionName: function, logMessage: function, folderPath: string }} ctx - 上下文
- */
- async function logActionError(action, result, ctx) {
- if (result && result.success) return
- const { getActionName, logMessage, folderPath } = ctx
- const now = new Date()
- const timeStr = `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`
- const errDetail = result && result.error != null && String(result.error).trim() !== '' ? String(result.error) : 'unknown'
- const actionName = typeof getActionName === 'function' ? getActionName(action) : (action && action.type) || 'unknown'
- const errorMsg = `[sequence-runner] [ERROR] ${actionName} failed: ${errDetail} [time: ${timeStr}]`
- await logMessage(errorMsg, folderPath).catch(() => {})
- if (typeof window !== 'undefined') {
- try {
- window.dispatchEvent(new CustomEvent('log-message', { detail: { message: errorMsg, isError: true } }))
- } catch (_) {}
- }
- }
- function parse (action, parseContext) {
- const path = parseContext.actionPath || 'echo'
- assertStrictKeys(action, ['type', 'inVars'], path)
- if (!Array.isArray(action.inVars)) {
- throw new Error(`${path}: echo 须使用 inVars 数组(无内容写 [])`)
- }
- return {
- type: 'echo',
- inVars: action.inVars.map((v) => {
- if (v == null) return ''
- if (typeof v === 'object' && v !== null) return JSON.stringify(v)
- return String(v)
- }),
- }
- }
- async function execute(action, ctx) {
- const { folderPath, variableContext, replaceVariablesInString, logMessage } = ctx
- let message = ''
- if (action.inVars && action.inVars.length > 0) {
- message = action.inVars.map((v) => {
- if (v == null) return ''
- if (typeof v === 'object' && v !== null) return JSON.stringify(v)
- const s = String(v)
- return replaceVariablesInString ? replaceVariablesInString(s, variableContext) : s
- }).join(' ')
- }
- const now = new Date()
- const timeStr = `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`
- const messageWithTime = message ? `[echo] ${message} [time: ${timeStr}]` : `[echo] [empty message] [time: ${timeStr}]`
- await logMessage(messageWithTime, folderPath)
- if (typeof window !== 'undefined') {
- const logEvent = new CustomEvent('log-message', { detail: { message } })
- window.dispatchEvent(logEvent)
- }
- return { success: true }
- }
- module.exports = { types, parse, execute, logActionError }
|