| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /**
- * 工作流 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/try-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: `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',
- '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',
- 'log': '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 ? 'infinite' : `repeat ${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 === 'try') return typeName
- 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 }
|