| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- /**
- * 工作流 JSON 解析:只解析 type,按 type 分派给 actions 下各脚本自行解析并执行。
- * 依赖:actions/*(各 parser,含 set-parser 的 extractVarName/resolveValue、fun-parser 的 fun 类 type)
- */
- const setParser = require('./actions/set-parser.js')
- const extractVarName = setParser.extractVarName
- const resolveValue = setParser.resolveValue
- const funNodeRegistry = require('./actions/fun/fun-node-registry.js')
- const actionModules = [
- require('./actions/delay-parser.js'),
- setParser,
- require('./actions/fun/adb/adb-parser.js'),
- require('./actions/echo-parser.js'),
- require('./actions/random-parser.js'),
- require('./actions/schedule-parser.js'),
- require('./actions/if-parser.js'),
- require('./actions/for-parser.js'),
- require('./actions/while-parser.js'),
- require('./actions/try-parser.js'),
- require('./actions/fun/fun-parser.js'),
- ]
- const registry = {}
- actionModules.forEach(mod => {
- const types = mod.types ? (Array.isArray(mod.types) ? mod.types : [mod.types]) : []
- types.forEach(t => {
- registry[t] = { parse: mod.parse, execute: mod.execute }
- })
- })
- function parseAction(type, action, parseContext) {
- const entry = registry[type]
- if (!entry || !entry.parse) return { ...action, type }
- return entry.parse(action, parseContext)
- }
- async function executeAction(type, action, executeContext) {
- const entry = registry[type]
- if (!entry || !entry.execute) return { success: false, error: `Unknown action type: ${type}` }
- return entry.execute(action, executeContext)
- }
- /**
- * Get display name for action (used in UI / log; English only for log.txt)
- */
- function getActionName(action) {
- const typeNames = {
- 'schedule': 'schedule',
- 'adb': 'adb',
- 'press': 'press image',
- 'input': 'input text',
- 'swipe': 'swipe',
- 'string-press': 'press text',
- 'scroll': 'scroll',
- 'locate': 'locate',
- 'click': 'click',
- 'ocr': 'ocr',
- 'extract-messages': 'extract messages',
- 'save-messages': 'save messages',
- 'generate-summary': 'generate summary',
- 'ocr-chat': 'ocr chat',
- 'ocr-chat-history': 'ocr chat history',
- 'extract-chat-history': 'extract chat history',
- 'generate-history-summary': 'generate history summary',
- 'img-bounding-box-location': 'img bounding box',
- 'img-center-point-location': 'img center point',
- 'img-cropping': 'img cropping',
- 'img-scale': 'img scale',
- 'read-last-message': 'read last message',
- 'read-txt': 'read text file',
- 'read-text': 'read text file',
- 'save-txt': 'save text file',
- 'save-text': 'save text file',
- 'smart-chat-append': 'smart chat append',
- 'ai-generate': 'ai generate',
- 'if': 'if',
- 'for': 'for',
- 'while': 'while',
- 'try': 'try',
- 'delay': 'delay',
- 'set': 'set variable',
- 'random': 'random',
- 'echo': 'echo',
- }
- ;(funNodeRegistry || []).forEach((def) => { typeNames[def.type] = def.displayName || def.type })
- const typeName = (action.type === 'fun' || action.type === 'ai' || action.type === 'io')
- ? (typeNames[action.method] || action.method || action.type)
- : (typeNames[action.type] || action.type)
- const value = action.value || action.target || ''
- const displayValue = typeof value === 'string' ? value : JSON.stringify(value)
- if (action.type === 'schedule') {
- const condition = action.condition || {}
- const interval = condition.interval || '0s'
- const repeat = condition.repeat !== undefined ? condition.repeat : 1
- const repeatText = repeat === -1 ? 'infinite' : `repeat ${repeat}`
- return `${typeName}: ${interval}, ${repeatText}`
- }
- if (action.type === 'echo') {
- const parts = (action.inVars && Array.isArray(action.inVars)) ? action.inVars.map((v) => (v == null ? '' : String(v))) : []
- const joined = parts.join(' ').trim()
- const d = joined.length > 40 ? joined.substring(0, 40) + '...' : joined
- return `${typeName}: ${d || '(empty)'}`
- }
- if (action.type === 'input') {
- return `${typeName}: ${displayValue.length > 20 ? displayValue.substring(0, 20) + '...' : displayValue}`
- }
- if (action.type === 'string-press' || action.type === 'click') {
- return `${typeName}: ${displayValue.length > 20 ? displayValue.substring(0, 20) + '...' : displayValue}`
- }
- if (action.type === 'if') return `${typeName}: ${action.condition || ''}`
- if (action.type === 'for') return `${typeName}: ${action.variable || ''}`
- if (action.type === 'try') return typeName
- if (action.type === 'set') return `${typeName}: ${action.variable || ''}`
- if (action.type === 'fun' || action.type === 'ai' || action.type === 'io') {
- const m = action.method && String(action.method)
- if (m && m.startsWith('adb-')) return `adb ${m.slice(4)}: ${displayValue}`
- if (m === 'json-to-arr' || m === 'json-json-to-arr') return `json to arr: ${displayValue}`
- return `${typeName}: ${displayValue}`
- }
- return `${typeName}: ${displayValue}`
- }
- /**
- * 解析操作数组:只按 type 分派,由各 action 脚本自己解析。
- * 非法结点、未知 type、缺 type 均抛错并中止解析(不静默跳过、不原样透传)。
- */
- function parseActions (actions, state, pathPrefix = 'execute') {
- if (!Array.isArray(actions)) {
- throw new Error(`[${pathPrefix}] 必须为数组`)
- }
- const parsedActions = []
- for (let i = 0; i < actions.length; i++) {
- const itemPath = `${pathPrefix}[${i}]`
- const action = actions[i]
- if (action === null || typeof action !== 'object' || Array.isArray(action)) {
- throw new Error(`[${itemPath}] 结点必须为对象`)
- }
- if (!action.type || typeof action.type !== 'string') {
- throw new Error(`[${itemPath}] 缺少字符串字段 type`)
- }
- const entry = registry[action.type]
- if (!entry || !entry.parse) {
- throw new Error(`[${itemPath}] 未知的 type: "${action.type}"`)
- }
- const parseActionsChild = (childArr, segment) => {
- if (!Array.isArray(childArr)) {
- throw new Error(`[${itemPath}.${segment}] 必须为数组`)
- }
- return parseActions(childArr, state, `${itemPath}.${segment}`)
- }
- const parseContext = {
- extractVarName,
- resolveValue,
- variableContext: state.variableContext,
- state,
- parseActions: parseActionsChild,
- actionPath: itemPath,
- }
- try {
- parsedActions.push(entry.parse(action, parseContext))
- } catch (e) {
- const msg = e && e.message ? String(e.message) : String(e)
- if (msg.includes(itemPath)) throw e
- throw new Error(`[${itemPath}] ${msg}`)
- }
- }
- return parsedActions
- }
- /**
- * 解析工作流 JSON(支持 variables, execute)
- * state: { variableContext, getInitialized(), setInitialized(bool) }
- */
- function parseWorkflow(workflow, state) {
- if (!workflow || typeof workflow !== 'object') return null
- const { variableContext, getInitialized, setInitialized } = state
- if (!getInitialized || !getInitialized()) {
- for (const key in variableContext) delete variableContext[key]
- if (workflow.variables) {
- for (const key in workflow.variables) {
- const value = workflow.variables[key]
- if (value === null || value === undefined) variableContext[key] = ''
- else if (typeof value === 'boolean') variableContext[key] = value ? '1' : '0'
- else if (typeof value === 'number') variableContext[key] = value
- else if (typeof value === 'string') variableContext[key] = value
- else if (Array.isArray(value) || (typeof value === 'object' && value !== null)) variableContext[key] = value
- else variableContext[key] = String(value)
- }
- }
- if (setInitialized) setInitialized(true)
- } else if (workflow.variables) {
- for (const key in workflow.variables) {
- const cur = variableContext[key]
- if (cur === undefined || cur === null || cur === '') {
- const value = workflow.variables[key]
- if (value === null || value === undefined) variableContext[key] = ''
- else if (typeof value === 'boolean') variableContext[key] = value ? '1' : '0'
- else if (typeof value === 'number') variableContext[key] = value
- else if (typeof value === 'string') variableContext[key] = value
- else if (Array.isArray(value) || (typeof value === 'object' && value !== null)) variableContext[key] = value
- else variableContext[key] = String(value)
- }
- }
- }
- const actions = workflow.execute && Array.isArray(workflow.execute)
- ? parseActions(workflow.execute, state)
- : []
- let schedule = workflow.schedule || {}
- if (Array.isArray(schedule)) schedule = schedule.length > 0 ? schedule[0] : {}
- if (schedule && typeof schedule === 'object' && schedule.schedule) schedule = schedule.schedule
- if (schedule && typeof schedule === 'object' && (schedule.repeat === 'forever' || schedule.repeat === 'Forever')) schedule.repeat = -1
- return { schedule, variables: variableContext, actions }
- }
- module.exports = { parseWorkflow, parseActions, getActionName, registry, parseAction, executeAction }
|