index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * 语句注册表:按类型分发解析与执行到各语句脚本
  3. * 除 fun 外,每种语句对应一个 *-parser.js(types + parse + execute)
  4. */
  5. const actionModules = [
  6. require('./delay-parser.js'),
  7. require('./set-parser.js'),
  8. require('./keyevent-parser.js'),
  9. require('./echo-parser.js'),
  10. require('./log-parser.js'),
  11. require('./random-parser.js'),
  12. require('./scroll-parser.js'),
  13. require('./swipe-parser.js'),
  14. require('./string-press-parser.js'),
  15. // 待补充:adb-parser, locate-parser, click-parser, input-parser, ocr-parser, extract-messages-parser,
  16. // save-messages-parser, generate-summary-parser, ai-generate-parser, schedule-parser, if-parser, for-parser,
  17. // while-parser, read-last-message-parser, read-txt-parser, save-txt-parser, smart-chat-append-parser,
  18. // img-bounding-box-location-parser, img-center-point-location-parser, img-cropping-parser, press-parser
  19. ]
  20. const registry = {}
  21. actionModules.forEach(mod => {
  22. const types = mod.types ? (Array.isArray(mod.types) ? mod.types : [mod.types]) : []
  23. types.forEach(t => {
  24. registry[t] = { parse: mod.parse, execute: mod.execute }
  25. })
  26. })
  27. function parseAction(type, action, parseContext) {
  28. const entry = registry[type]
  29. if (!entry || !entry.parse) return { ...action, type }
  30. return entry.parse(action, parseContext)
  31. }
  32. async function executeAction(type, action, executeContext) {
  33. const entry = registry[type]
  34. if (!entry || !entry.execute) return { success: false, error: `未知操作类型: ${type}` }
  35. return entry.execute(action, executeContext)
  36. }
  37. module.exports = { registry, parseAction, executeAction }