runtime-api.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * 运行时 API:设备操作(adb)、日志、桩函数
  3. * config 由主脚本传入:{ projectRoot, adbInteractPath }
  4. */
  5. const path = require('path')
  6. const fs = require('fs')
  7. const { spawnSync } = require('child_process')
  8. function appendLog(folderPath, message) {
  9. if (!folderPath || typeof folderPath !== 'string') return Promise.resolve()
  10. const logDir = path.resolve(folderPath)
  11. const logFile = path.join(logDir, 'log.txt')
  12. fs.mkdirSync(logDir, { recursive: true })
  13. fs.appendFileSync(logFile, message + '\n')
  14. return Promise.resolve()
  15. }
  16. const _stub = (name) => ({ success: false, error: `${name} 需在主进程实现` })
  17. function createElectronAPI(overrides = {}, config = {}) {
  18. const { projectRoot, adbInteractPath } = config
  19. const runAdb = (action, args = [], deviceId = '') => {
  20. if (!adbInteractPath) return { success: false, error: '未配置 adbInteractPath' }
  21. const r = spawnSync('node', [adbInteractPath, action, ...args, deviceId], { encoding: 'utf-8', timeout: 10000 })
  22. return { success: r.status === 0, error: r.stderr }
  23. }
  24. const sendTap = (device, x, y) => runAdb('tap', [String(x), String(y)], device)
  25. const sendSwipe = (device, x1, y1, x2, y2, duration) =>
  26. runAdb('swipe-coords', [String(x1), String(y1), String(x2), String(y2), String(duration || 300)], device)
  27. const sendKeyEvent = (device, keyCode) => runAdb('keyevent', [String(keyCode)], device)
  28. const sendText = (device, text) => runAdb('text', [String(text)], device)
  29. let sendSystemKeyImpl = () => _stub('sendSystemKey')
  30. if (projectRoot) {
  31. try {
  32. const { sendSystemButton } = require(path.join(projectRoot, 'nodejs', 'adb', 'adb-sys-btn.js'))
  33. sendSystemKeyImpl = (device, keyCode) => sendSystemButton(String(keyCode), device)
  34. } catch (e) {}
  35. }
  36. return {
  37. sendTap,
  38. sendSwipe,
  39. sendKeyEvent,
  40. sendText,
  41. sendSystemKey: sendSystemKeyImpl,
  42. matchImageAndGetCoordinate: null,
  43. findTextAndGetCoordinate: () => _stub('findTextAndGetCoordinate'),
  44. appendLog,
  45. readTextFile: null,
  46. writeTextFile: null,
  47. saveChatHistory: () => _stub('saveChatHistory'),
  48. readChatHistory: () => _stub('readChatHistory'),
  49. readAllChatHistory: () => _stub('readAllChatHistory'),
  50. saveChatHistorySummary: () => _stub('saveChatHistorySummary'),
  51. getChatHistorySummary: () => _stub('getChatHistorySummary'),
  52. saveChatHistoryTxt: () => _stub('saveChatHistoryTxt'),
  53. extractChatHistory: () => _stub('extractChatHistory'),
  54. readLastMessage: () => _stub('readLastMessage'),
  55. ocrLastMessage: () => _stub('ocrLastMessage'),
  56. getCachedScreenshot: () => _stub('getCachedScreenshot'),
  57. captureScreenshot: () => _stub('captureScreenshot'),
  58. sendScroll: () => _stub('sendScroll'),
  59. matchImageRegionLocation: () => _stub('matchImageRegionLocation'),
  60. ...overrides,
  61. }
  62. }
  63. module.exports = {
  64. appendLog,
  65. createElectronAPI,
  66. }