workflow-json-parser.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 funNodeRegistry = require('./actions/fun/fun-node-registry.js')
  9. const actionModules = [
  10. require('./actions/delay-parser.js'),
  11. setParser,
  12. require('./actions/fun/adb/adb-parser.js'),
  13. require('./actions/echo-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/try-parser.js'),
  20. require('./actions/fun/fun-parser.js'),
  21. ]
  22. const registry = {}
  23. actionModules.forEach(mod => {
  24. const types = mod.types ? (Array.isArray(mod.types) ? mod.types : [mod.types]) : []
  25. types.forEach(t => {
  26. registry[t] = { parse: mod.parse, execute: mod.execute }
  27. })
  28. })
  29. function parseAction(type, action, parseContext) {
  30. const entry = registry[type]
  31. if (!entry || !entry.parse) return { ...action, type }
  32. return entry.parse(action, parseContext)
  33. }
  34. async function executeAction(type, action, executeContext) {
  35. const entry = registry[type]
  36. if (!entry || !entry.execute) return { success: false, error: `Unknown action type: ${type}` }
  37. return entry.execute(action, executeContext)
  38. }
  39. /**
  40. * Get display name for action (used in UI / log; English only for log.txt)
  41. */
  42. function getActionName(action) {
  43. const typeNames = {
  44. 'schedule': 'schedule',
  45. 'adb': 'adb',
  46. 'press': 'press image',
  47. 'input': 'input text',
  48. 'swipe': 'swipe',
  49. 'string-press': 'press text',
  50. 'scroll': 'scroll',
  51. 'locate': 'locate',
  52. 'click': 'click',
  53. 'ocr': 'ocr',
  54. 'extract-messages': 'extract messages',
  55. 'save-messages': 'save messages',
  56. 'generate-summary': 'generate summary',
  57. 'ocr-chat': 'ocr chat',
  58. 'ocr-chat-history': 'ocr chat history',
  59. 'extract-chat-history': 'extract chat history',
  60. 'generate-history-summary': 'generate history summary',
  61. 'img-bounding-box-location': 'img bounding box',
  62. 'img-center-point-location': 'img center point',
  63. 'img-cropping': 'img cropping',
  64. 'img-scale': 'img scale',
  65. 'read-last-message': 'read last message',
  66. 'read-txt': 'read text file',
  67. 'read-text': 'read text file',
  68. 'save-txt': 'save text file',
  69. 'save-text': 'save text file',
  70. 'smart-chat-append': 'smart chat append',
  71. 'ai-generate': 'ai generate',
  72. 'if': 'if',
  73. 'for': 'for',
  74. 'while': 'while',
  75. 'try': 'try',
  76. 'delay': 'delay',
  77. 'set': 'set variable',
  78. 'random': 'random',
  79. 'echo': 'echo',
  80. }
  81. ;(funNodeRegistry || []).forEach((def) => { typeNames[def.type] = def.displayName || def.type })
  82. const typeName = (action.type === 'fun' || action.type === 'ai' || action.type === 'io')
  83. ? (typeNames[action.method] || action.method || action.type)
  84. : (typeNames[action.type] || action.type)
  85. const value = action.value || action.target || ''
  86. const displayValue = typeof value === 'string' ? value : JSON.stringify(value)
  87. if (action.type === 'schedule') {
  88. const condition = action.condition || {}
  89. const interval = condition.interval || '0s'
  90. const repeat = condition.repeat !== undefined ? condition.repeat : 1
  91. const repeatText = repeat === -1 ? 'infinite' : `repeat ${repeat}`
  92. return `${typeName}: ${interval}, ${repeatText}`
  93. }
  94. if (action.type === 'input') {
  95. return `${typeName}: ${displayValue.length > 20 ? displayValue.substring(0, 20) + '...' : displayValue}`
  96. }
  97. if (action.type === 'string-press' || action.type === 'click') {
  98. return `${typeName}: ${displayValue.length > 20 ? displayValue.substring(0, 20) + '...' : displayValue}`
  99. }
  100. if (action.type === 'if') return `${typeName}: ${action.condition || ''}`
  101. if (action.type === 'for') return `${typeName}: ${action.variable || ''}`
  102. if (action.type === 'try') return typeName
  103. if (action.type === 'set') return `${typeName}: ${action.variable || ''}`
  104. if (action.type === 'fun' || action.type === 'ai' || action.type === 'io') {
  105. const m = action.method && String(action.method)
  106. if (m && m.startsWith('adb-')) return `adb ${m.slice(4)}: ${displayValue}`
  107. if (m === 'json-to-arr' || m === 'json-json-to-arr') return `json to arr: ${displayValue}`
  108. return `${typeName}: ${displayValue}`
  109. }
  110. return `${typeName}: ${displayValue}`
  111. }
  112. /**
  113. * 解析操作数组:只按 type 分派,由各 action 脚本自己解析
  114. */
  115. function parseActions(actions, state) {
  116. if (!Array.isArray(actions)) return []
  117. const parsedActions = []
  118. const parseActionsFn = (arr) => parseActions(arr, state)
  119. for (const action of actions) {
  120. if (typeof action !== 'object' || action === null) continue
  121. if (!action.type) continue
  122. const entry = registry[action.type]
  123. if (!entry || !entry.parse) {
  124. parsedActions.push(Object.assign({}, action, { type: action.type }))
  125. continue
  126. }
  127. const parseContext = {
  128. extractVarName,
  129. resolveValue,
  130. variableContext: state.variableContext,
  131. parseActions: parseActionsFn,
  132. }
  133. parsedActions.push(entry.parse(action, parseContext))
  134. }
  135. return parsedActions
  136. }
  137. /**
  138. * 解析工作流 JSON(支持 variables, execute)
  139. * state: { variableContext, getInitialized(), setInitialized(bool) }
  140. */
  141. function parseWorkflow(workflow, state) {
  142. if (!workflow || typeof workflow !== 'object') return null
  143. const { variableContext, getInitialized, setInitialized } = state
  144. if (!getInitialized || !getInitialized()) {
  145. for (const key in variableContext) delete variableContext[key]
  146. if (workflow.variables) {
  147. for (const key in workflow.variables) {
  148. const value = workflow.variables[key]
  149. if (value === null || value === undefined) variableContext[key] = ''
  150. else if (typeof value === 'boolean') variableContext[key] = value ? '1' : '0'
  151. else if (typeof value === 'number') variableContext[key] = value
  152. else if (typeof value === 'string') variableContext[key] = value
  153. else if (Array.isArray(value) || (typeof value === 'object' && value !== null)) variableContext[key] = value
  154. else variableContext[key] = String(value)
  155. }
  156. }
  157. if (setInitialized) setInitialized(true)
  158. } else if (workflow.variables) {
  159. for (const key in workflow.variables) {
  160. const cur = variableContext[key]
  161. if (cur === undefined || cur === null || cur === '') {
  162. const value = workflow.variables[key]
  163. if (value === null || value === undefined) variableContext[key] = ''
  164. else if (typeof value === 'boolean') variableContext[key] = value ? '1' : '0'
  165. else if (typeof value === 'number') variableContext[key] = value
  166. else if (typeof value === 'string') variableContext[key] = value
  167. else if (Array.isArray(value) || (typeof value === 'object' && value !== null)) variableContext[key] = value
  168. else variableContext[key] = String(value)
  169. }
  170. }
  171. }
  172. const actions = workflow.execute && Array.isArray(workflow.execute)
  173. ? parseActions(workflow.execute, state)
  174. : []
  175. let schedule = workflow.schedule || {}
  176. if (Array.isArray(schedule)) schedule = schedule.length > 0 ? schedule[0] : {}
  177. if (schedule && typeof schedule === 'object' && schedule.schedule) schedule = schedule.schedule
  178. if (schedule && typeof schedule === 'object' && (schedule.repeat === 'forever' || schedule.repeat === 'Forever')) schedule.repeat = -1
  179. return { schedule, variables: variableContext, actions }
  180. }
  181. module.exports = { parseWorkflow, parseActions, getActionName, registry, parseAction, executeAction }