workflow-json-parser.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/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. 'read-last-message': 'read last message',
  65. 'read-txt': 'read text file',
  66. 'read-text': 'read text file',
  67. 'save-txt': 'save text file',
  68. 'save-text': 'save text file',
  69. 'smart-chat-append': 'smart chat append',
  70. 'ai-generate': 'ai generate',
  71. 'if': 'if',
  72. 'for': 'for',
  73. 'while': 'while',
  74. 'try': 'try',
  75. 'delay': 'delay',
  76. 'set': 'set variable',
  77. 'random': 'random',
  78. 'echo': 'echo',
  79. }
  80. ;(funNodeRegistry || []).forEach((def) => { typeNames[def.type] = def.displayName || def.type })
  81. const typeName = (action.type === 'fun' || action.type === 'ai') ? (typeNames[action.method] || action.method || action.type) : (typeNames[action.type] || action.type)
  82. const value = action.value || action.target || ''
  83. const displayValue = typeof value === 'string' ? value : JSON.stringify(value)
  84. if (action.type === 'schedule') {
  85. const condition = action.condition || {}
  86. const interval = condition.interval || '0s'
  87. const repeat = condition.repeat !== undefined ? condition.repeat : 1
  88. const repeatText = repeat === -1 ? 'infinite' : `repeat ${repeat}`
  89. return `${typeName}: ${interval}, ${repeatText}`
  90. }
  91. if (action.type === 'input') {
  92. return `${typeName}: ${displayValue.length > 20 ? displayValue.substring(0, 20) + '...' : displayValue}`
  93. }
  94. if (action.type === 'string-press' || action.type === 'click') {
  95. return `${typeName}: ${displayValue.length > 20 ? displayValue.substring(0, 20) + '...' : displayValue}`
  96. }
  97. if (action.type === 'if') return `${typeName}: ${action.condition || ''}`
  98. if (action.type === 'for') return `${typeName}: ${action.variable || ''}`
  99. if (action.type === 'try') return typeName
  100. if (action.type === 'set') return `${typeName}: ${action.variable || ''}`
  101. if (action.type === 'fun' || action.type === 'ai') return `${typeName}: ${displayValue}`
  102. return `${typeName}: ${displayValue}`
  103. }
  104. /**
  105. * 解析操作数组:只按 type 分派,由各 action 脚本自己解析
  106. */
  107. function parseActions(actions, state) {
  108. if (!Array.isArray(actions)) return []
  109. const parsedActions = []
  110. const parseActionsFn = (arr) => parseActions(arr, state)
  111. for (const action of actions) {
  112. if (typeof action !== 'object' || action === null) continue
  113. if (!action.type) continue
  114. const entry = registry[action.type]
  115. if (!entry || !entry.parse) {
  116. parsedActions.push(Object.assign({}, action, { type: action.type }))
  117. continue
  118. }
  119. const parseContext = {
  120. extractVarName,
  121. resolveValue,
  122. variableContext: state.variableContext,
  123. parseActions: parseActionsFn,
  124. }
  125. parsedActions.push(entry.parse(action, parseContext))
  126. }
  127. return parsedActions
  128. }
  129. /**
  130. * 解析工作流 JSON(支持 variables, execute)
  131. * state: { variableContext, getInitialized(), setInitialized(bool) }
  132. */
  133. function parseWorkflow(workflow, state) {
  134. if (!workflow || typeof workflow !== 'object') return null
  135. const { variableContext, getInitialized, setInitialized } = state
  136. if (!getInitialized || !getInitialized()) {
  137. for (const key in variableContext) delete variableContext[key]
  138. if (workflow.variables) {
  139. for (const key in workflow.variables) {
  140. const value = workflow.variables[key]
  141. if (value === null || value === undefined) variableContext[key] = ''
  142. else if (typeof value === 'boolean') variableContext[key] = value ? '1' : '0'
  143. else if (typeof value === 'number') variableContext[key] = value
  144. else if (typeof value === 'string') variableContext[key] = value
  145. else if (Array.isArray(value) || (typeof value === 'object' && value !== null)) variableContext[key] = value
  146. else variableContext[key] = String(value)
  147. }
  148. }
  149. if (setInitialized) setInitialized(true)
  150. } else if (workflow.variables) {
  151. for (const key in workflow.variables) {
  152. const cur = variableContext[key]
  153. if (cur === undefined || cur === null || cur === '') {
  154. const value = workflow.variables[key]
  155. if (value === null || value === undefined) variableContext[key] = ''
  156. else if (typeof value === 'boolean') variableContext[key] = value ? '1' : '0'
  157. else if (typeof value === 'number') variableContext[key] = value
  158. else if (typeof value === 'string') variableContext[key] = value
  159. else if (Array.isArray(value) || (typeof value === 'object' && value !== null)) variableContext[key] = value
  160. else variableContext[key] = String(value)
  161. }
  162. }
  163. }
  164. const actions = workflow.execute && Array.isArray(workflow.execute)
  165. ? parseActions(workflow.execute, state)
  166. : []
  167. let schedule = workflow.schedule || {}
  168. if (Array.isArray(schedule)) schedule = schedule.length > 0 ? schedule[0] : {}
  169. if (schedule && typeof schedule === 'object' && schedule.schedule) schedule = schedule.schedule
  170. if (schedule && typeof schedule === 'object' && (schedule.repeat === 'forever' || schedule.repeat === 'Forever')) schedule.repeat = -1
  171. return { schedule, variables: variableContext, actions }
  172. }
  173. module.exports = { parseWorkflow, parseActions, getActionName, registry, parseAction, executeAction }