echo-parser.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /** 语句:echo 打印信息(写入 log + UI);仅 inVars:字符串列表,支持 {{var}} / {var},由执行阶段替换 */
  2. const { assertStrictKeys } = require('../action-schema.js')
  3. const types = ['echo']
  4. /**
  5. * 统一打印结点报错:写入 log.txt 并可选通知 UI。所有结点失败时均由此处输出,无需在各结点内单独 logMessage。
  6. * @param {object} action - 当前执行的 action
  7. * @param {{ success: boolean, error?: string }} result - 执行结果(success 为 false 时应有 error)
  8. * @param {{ getActionName: function, logMessage: function, folderPath: string }} ctx - 上下文
  9. */
  10. async function logActionError(action, result, ctx) {
  11. if (result && result.success) return
  12. const { getActionName, logMessage, folderPath } = ctx
  13. const now = new Date()
  14. 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')}`
  15. const errDetail = result && result.error != null && String(result.error).trim() !== '' ? String(result.error) : 'unknown'
  16. const actionName = typeof getActionName === 'function' ? getActionName(action) : (action && action.type) || 'unknown'
  17. const errorMsg = `[sequence-runner] [ERROR] ${actionName} failed: ${errDetail} [time: ${timeStr}]`
  18. await logMessage(errorMsg, folderPath).catch(() => {})
  19. if (typeof window !== 'undefined') {
  20. try {
  21. window.dispatchEvent(new CustomEvent('log-message', { detail: { message: errorMsg, isError: true } }))
  22. } catch (_) {}
  23. }
  24. }
  25. function parse (action, parseContext) {
  26. const path = parseContext.actionPath || 'echo'
  27. assertStrictKeys(action, ['type', 'inVars'], path)
  28. if (!Array.isArray(action.inVars)) {
  29. throw new Error(`${path}: echo 须使用 inVars 数组(无内容写 [])`)
  30. }
  31. return {
  32. type: 'echo',
  33. inVars: action.inVars.map((v) => {
  34. if (v == null) return ''
  35. if (typeof v === 'object' && v !== null) return JSON.stringify(v)
  36. return String(v)
  37. }),
  38. }
  39. }
  40. async function execute(action, ctx) {
  41. const { folderPath, variableContext, replaceVariablesInString, logMessage } = ctx
  42. let message = ''
  43. if (action.inVars && action.inVars.length > 0) {
  44. message = action.inVars.map((v) => {
  45. if (v == null) return ''
  46. if (typeof v === 'object' && v !== null) return JSON.stringify(v)
  47. const s = String(v)
  48. return replaceVariablesInString ? replaceVariablesInString(s, variableContext) : s
  49. }).join(' ')
  50. }
  51. const now = new Date()
  52. 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')}`
  53. const messageWithTime = message ? `[echo] ${message} [time: ${timeStr}]` : `[echo] [empty message] [time: ${timeStr}]`
  54. await logMessage(messageWithTime, folderPath)
  55. if (typeof window !== 'undefined') {
  56. const logEvent = new CustomEvent('log-message', { detail: { message } })
  57. window.dispatchEvent(logEvent)
  58. }
  59. return { success: true }
  60. }
  61. module.exports = { types, parse, execute, logActionError }