workflow-json-parser.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * 工作流 JSON 解析:只解析 type,按 type 分派给 actions 下各脚本自行解析并执行。
  3. * 依赖:actions/*(各 parser,含 set-parser 的 extractVarName/resolveValue、fun-parser 的 fun 类 type)
  4. */
  5. const setParser = require('./actions/set-parser.js')
  6. const extractVarName = setParser.extractVarName
  7. const resolveValue = setParser.resolveValue
  8. const actionModules = [
  9. require('./actions/delay-parser.js'),
  10. setParser,
  11. require('./actions/adb-parser.js'),
  12. require('./actions/echo-parser.js'),
  13. require('./actions/log-parser.js'),
  14. require('./actions/random-parser.js'),
  15. require('./actions/schedule-parser.js'),
  16. require('./actions/if-parser.js'),
  17. require('./actions/for-parser.js'),
  18. require('./actions/while-parser.js'),
  19. require('./actions/fun-parser.js'),
  20. ]
  21. const registry = {}
  22. actionModules.forEach(mod => {
  23. const types = mod.types ? (Array.isArray(mod.types) ? mod.types : [mod.types]) : []
  24. types.forEach(t => {
  25. registry[t] = { parse: mod.parse, execute: mod.execute }
  26. })
  27. })
  28. function parseAction(type, action, parseContext) {
  29. const entry = registry[type]
  30. if (!entry || !entry.parse) return { ...action, type }
  31. return entry.parse(action, parseContext)
  32. }
  33. async function executeAction(type, action, executeContext) {
  34. const entry = registry[type]
  35. if (!entry || !entry.execute) return { success: false, error: `未知操作类型: ${type}` }
  36. return entry.execute(action, executeContext)
  37. }
  38. /**
  39. * 根据 action 生成展示名称(用于 UI/日志)
  40. */
  41. function getActionName(action) {
  42. const typeNames = {
  43. 'schedule': '定时执行',
  44. 'adb': 'ADB操作',
  45. 'press': '点击图片',
  46. 'input': '输入文本',
  47. 'swipe': '滑动',
  48. 'string-press': '点击文字',
  49. 'scroll': '滚动',
  50. 'locate': '定位',
  51. 'click': '点击',
  52. 'ocr': '文字识别',
  53. 'extract-messages': '提取消息记录',
  54. 'save-messages': '保存消息记录',
  55. 'generate-summary': '生成总结',
  56. 'ocr-chat': 'OCR识别对话',
  57. 'ocr-chat-history': 'OCR提取消息记录',
  58. 'extract-chat-history': '提取消息记录',
  59. 'generate-history-summary': '生成总结',
  60. 'img-bounding-box-location': '图像区域定位',
  61. 'img-center-point-location': '图像中心点定位',
  62. 'img-cropping': '裁剪图片区域',
  63. 'read-last-message': '读取最后一条消息',
  64. 'read-txt': '读取文本文件',
  65. 'read-text': '读取文本文件',
  66. 'save-txt': '保存文本文件',
  67. 'save-text': '保存文本文件',
  68. 'smart-chat-append': '智能合并聊天记录',
  69. 'ai-generate': 'AI生成',
  70. 'if': '条件判断',
  71. 'for': '循环',
  72. 'while': '循环',
  73. 'delay': '延迟',
  74. 'set': '设置变量',
  75. 'random': '生成随机数',
  76. 'echo': '打印信息',
  77. 'log': '打印信息',
  78. }
  79. const typeName = action.type === 'fun' ? (typeNames[action.method] || action.method || 'fun') : (typeNames[action.type] || action.type)
  80. const value = action.value || action.target || ''
  81. const displayValue = typeof value === 'string' ? value : JSON.stringify(value)
  82. if (action.type === 'schedule') {
  83. const condition = action.condition || {}
  84. const interval = condition.interval || '0s'
  85. const repeat = condition.repeat !== undefined ? condition.repeat : 1
  86. const repeatText = repeat === -1 ? '无限循环' : `重复${repeat}次`
  87. return `${typeName}: ${interval}, ${repeatText}`
  88. }
  89. if (action.type === 'input') {
  90. return `${typeName}: ${displayValue.length > 20 ? displayValue.substring(0, 20) + '...' : displayValue}`
  91. }
  92. if (action.type === 'string-press' || action.type === 'click') {
  93. return `${typeName}: ${displayValue.length > 20 ? displayValue.substring(0, 20) + '...' : displayValue}`
  94. }
  95. if (action.type === 'if') return `${typeName}: ${action.condition || ''}`
  96. if (action.type === 'for') return `${typeName}: ${action.variable || ''}`
  97. if (action.type === 'set') return `${typeName}: ${action.variable || ''}`
  98. if (action.type === 'fun') return `${typeName}: ${displayValue}`
  99. return `${typeName}: ${displayValue}`
  100. }
  101. /**
  102. * 解析操作数组:只按 type 分派,由各 action 脚本自己解析
  103. */
  104. function parseActions(actions, state) {
  105. if (!Array.isArray(actions)) return []
  106. const parsedActions = []
  107. const parseActionsFn = (arr) => parseActions(arr, state)
  108. for (const action of actions) {
  109. if (typeof action !== 'object' || action === null) continue
  110. if (!action.type) continue
  111. const entry = registry[action.type]
  112. if (!entry || !entry.parse) {
  113. parsedActions.push(Object.assign({}, action, { type: action.type }))
  114. continue
  115. }
  116. const parseContext = {
  117. extractVarName,
  118. resolveValue,
  119. variableContext: state.variableContext,
  120. parseActions: parseActionsFn,
  121. }
  122. parsedActions.push(entry.parse(action, parseContext))
  123. }
  124. return parsedActions
  125. }
  126. /**
  127. * 解析工作流 JSON(支持 variables, execute)
  128. * state: { variableContext, getInitialized(), setInitialized(bool) }
  129. */
  130. function parseWorkflow(workflow, state) {
  131. if (!workflow || typeof workflow !== 'object') return null
  132. const { variableContext, getInitialized, setInitialized } = state
  133. if (!getInitialized || !getInitialized()) {
  134. for (const key in variableContext) delete variableContext[key]
  135. if (workflow.variables) {
  136. for (const key in workflow.variables) {
  137. const value = workflow.variables[key]
  138. if (value === null || value === undefined) variableContext[key] = ''
  139. else if (typeof value === 'boolean') variableContext[key] = value ? '1' : '0'
  140. else if (typeof value === 'number') variableContext[key] = value
  141. else if (typeof value === 'string') variableContext[key] = value
  142. else variableContext[key] = String(value)
  143. }
  144. }
  145. if (setInitialized) setInitialized(true)
  146. } else if (workflow.variables) {
  147. for (const key in workflow.variables) {
  148. const cur = variableContext[key]
  149. if (cur === undefined || cur === null || cur === '') {
  150. const value = workflow.variables[key]
  151. if (value === null || value === undefined) variableContext[key] = ''
  152. else if (typeof value === 'boolean') variableContext[key] = value ? '1' : '0'
  153. else if (typeof value === 'number') variableContext[key] = value
  154. else if (typeof value === 'string') variableContext[key] = value
  155. else variableContext[key] = String(value)
  156. }
  157. }
  158. }
  159. const actions = workflow.execute && Array.isArray(workflow.execute)
  160. ? parseActions(workflow.execute, state)
  161. : []
  162. let schedule = workflow.schedule || {}
  163. if (Array.isArray(schedule)) schedule = schedule.length > 0 ? schedule[0] : {}
  164. if (schedule && typeof schedule === 'object' && schedule.schedule) schedule = schedule.schedule
  165. if (schedule && typeof schedule === 'object' && (schedule.repeat === 'forever' || schedule.repeat === 'Forever')) schedule.repeat = -1
  166. return { schedule, variables: variableContext, actions }
  167. }
  168. module.exports = { parseWorkflow, parseActions, getActionName, registry, parseAction, executeAction }