| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /**
- * 运行时 API:设备操作(adb)、日志、桩函数
- * config 由主脚本传入:{ projectRoot, adbInteractPath }
- */
- const path = require('path')
- const fs = require('fs')
- const { spawnSync } = require('child_process')
- function appendLog(folderPath, message) {
- if (!folderPath || typeof folderPath !== 'string') return Promise.resolve()
- const logDir = path.resolve(folderPath)
- const logFile = path.join(logDir, 'log.txt')
- fs.mkdirSync(logDir, { recursive: true })
- fs.appendFileSync(logFile, message + '\n')
- return Promise.resolve()
- }
- const _stub = (name) => ({ success: false, error: `${name} 需在主进程实现` })
- function createElectronAPI(overrides = {}, config = {}) {
- const { projectRoot, adbInteractPath } = config
- const runAdb = (action, args = [], deviceId = '') => {
- if (!adbInteractPath) return { success: false, error: '未配置 adbInteractPath' }
- const r = spawnSync('node', [adbInteractPath, action, ...args, deviceId], { encoding: 'utf-8', timeout: 10000 })
- return { success: r.status === 0, error: r.stderr }
- }
- const sendTap = (device, x, y) => runAdb('tap', [String(x), String(y)], device)
- const sendSwipe = (device, x1, y1, x2, y2, duration) =>
- runAdb('swipe-coords', [String(x1), String(y1), String(x2), String(y2), String(duration || 300)], device)
- const sendKeyEvent = (device, keyCode) => runAdb('keyevent', [String(keyCode)], device)
- const sendText = (device, text) => runAdb('text', [String(text)], device)
- let sendSystemKeyImpl = () => _stub('sendSystemKey')
- if (projectRoot) {
- try {
- const { sendSystemButton } = require(path.join(projectRoot, 'nodejs', 'adb', 'adb-sys-btn.js'))
- sendSystemKeyImpl = (device, keyCode) => sendSystemButton(String(keyCode), device)
- } catch (e) {}
- }
- return {
- sendTap,
- sendSwipe,
- sendKeyEvent,
- sendText,
- sendSystemKey: sendSystemKeyImpl,
- matchImageAndGetCoordinate: null,
- findTextAndGetCoordinate: () => _stub('findTextAndGetCoordinate'),
- appendLog,
- readTextFile: null,
- writeTextFile: null,
- saveChatHistory: () => _stub('saveChatHistory'),
- readChatHistory: () => _stub('readChatHistory'),
- readAllChatHistory: () => _stub('readAllChatHistory'),
- saveChatHistorySummary: () => _stub('saveChatHistorySummary'),
- getChatHistorySummary: () => _stub('getChatHistorySummary'),
- saveChatHistoryTxt: () => _stub('saveChatHistoryTxt'),
- extractChatHistory: () => _stub('extractChatHistory'),
- readLastMessage: () => _stub('readLastMessage'),
- ocrLastMessage: () => _stub('ocrLastMessage'),
- getCachedScreenshot: () => _stub('getCachedScreenshot'),
- captureScreenshot: () => _stub('captureScreenshot'),
- sendScroll: () => _stub('sendScroll'),
- matchImageRegionLocation: () => _stub('matchImageRegionLocation'),
- ...overrides,
- }
- }
- module.exports = {
- appendLog,
- createElectronAPI,
- }
|