/** * 工作流 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 actionModules = [ require('./actions/delay-parser.js'), setParser, require('./actions/adb-parser.js'), require('./actions/echo-parser.js'), require('./actions/log-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/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: `未知操作类型: ${type}` } return entry.execute(action, executeContext) } /** * 根据 action 生成展示名称(用于 UI/日志) */ function getActionName(action) { const typeNames = { 'schedule': '定时执行', 'adb': 'ADB操作', 'press': '点击图片', 'input': '输入文本', 'swipe': '滑动', 'string-press': '点击文字', 'scroll': '滚动', 'locate': '定位', 'click': '点击', 'ocr': '文字识别', 'extract-messages': '提取消息记录', 'save-messages': '保存消息记录', 'generate-summary': '生成总结', 'ocr-chat': 'OCR识别对话', 'ocr-chat-history': 'OCR提取消息记录', 'extract-chat-history': '提取消息记录', 'generate-history-summary': '生成总结', 'img-bounding-box-location': '图像区域定位', 'img-center-point-location': '图像中心点定位', 'img-cropping': '裁剪图片区域', 'read-last-message': '读取最后一条消息', 'read-txt': '读取文本文件', 'read-text': '读取文本文件', 'save-txt': '保存文本文件', 'save-text': '保存文本文件', 'smart-chat-append': '智能合并聊天记录', 'ai-generate': 'AI生成', 'if': '条件判断', 'for': '循环', 'while': '循环', 'delay': '延迟', 'set': '设置变量', 'random': '生成随机数', 'echo': '打印信息', 'log': '打印信息', } const typeName = action.type === 'fun' ? (typeNames[action.method] || action.method || 'fun') : (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 ? '无限循环' : `重复${repeat}次` return `${typeName}: ${interval}, ${repeatText}` } 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 === 'set') return `${typeName}: ${action.variable || ''}` if (action.type === 'fun') return `${typeName}: ${displayValue}` return `${typeName}: ${displayValue}` } /** * 解析操作数组:只按 type 分派,由各 action 脚本自己解析 */ function parseActions(actions, state) { if (!Array.isArray(actions)) return [] const parsedActions = [] const parseActionsFn = (arr) => parseActions(arr, state) for (const action of actions) { if (typeof action !== 'object' || action === null) continue if (!action.type) continue const entry = registry[action.type] if (!entry || !entry.parse) { parsedActions.push(Object.assign({}, action, { type: action.type })) continue } const parseContext = { extractVarName, resolveValue, variableContext: state.variableContext, parseActions: parseActionsFn, } parsedActions.push(entry.parse(action, parseContext)) } 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 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 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 }