一个界面照着豆包抠的 AI 助手。主力是安卓端,完全离线自主:不依赖电脑、 不需要另外装 Termux。 安卓端 - 原生 Jetpack Compose,7 个页面,配色/圆角/间距对着真机截图量出来的 - 完整动效:抽屉滑入、页面切换、气泡淡入上移、面板底部升起、typing 三点、按钮回弹 - 内置 Termux:bash/apt/pkg 388 个命令、真 pty 终端; pty 不可用的 ROM 上自动降级成命令控制台 - APK 内嵌 CPython 3.13(Chaquopy),AI 内核跑在手机上 - 设备能力:定位 / 陀螺仪 / 加速度计 / 光线 / 电量 / 剪贴板 - 语音:朗读 + 语音输入,火山豆包音色 / OpenAI 兼容 / 设备自带三级引擎 AI 能力 - 10 家 OpenAI 兼容提供商:方舟 / OpenAI / DeepSeek / Kimi / 智谱 / 通义 / 硅基流动 / OpenRouter / 本地 Ollama / 自定义 - 思考强度按各家支持情况发不同字段(thinking / reasoning_effort / 不发), 认不出来的一律不发,避免给不支持的厂商发字段直接 400 - MCP 三种传输(http / stdio / inproc)、工具热加载、文件上传、权限模式 跨端 - 安卓走原生 Compose,Web / Windows 走 Lynx - Node 与 Python 两份内核接口逐字段一致,关键路径都有对拍脚本 自检脚本(都不需要真 Key) - verify-phone.mjs 设备端 24 项验收 - verify-speech.mjs 语音链路 31 项,两份内核各跑一遍 - parity-thinking.mjs 思考强度映射对拍 - probe-settings-wipe.mjs 设置防清空攻击测试 - mock-llm.mjs 假的 OpenAI + 火山语音服务 许可 MIT;第三方组件(Termux Apache-2.0、Chaquopy、Compose、LynxJS、Electron) 的授权见 LICENSE。
231 lines
7.7 KiB
JavaScript
231 lines
7.7 KiB
JavaScript
/**
|
||
* 用 CDP 驱动浏览器:加载页面、收集控制台/异常、模拟点击、截图。
|
||
*
|
||
* 用法: node screenshot.mjs <url|- > <out.png> [waitMs] [stepsJson|@stepsFile]
|
||
* stepsJson 形如 [{"click":[28,26]},{"wait":900},{"type":"你好"},{"key":"Enter"}]
|
||
* 坐标是 CSS px(截图是 2x 的,别看错)
|
||
*
|
||
* 环境变量:
|
||
* CDP_PORT=9555 连到「已经开着调试端口」的浏览器/Electron 窗口,不再自己起 Edge
|
||
* EDGE_PATH=... 指定浏览器可执行文件
|
||
*/
|
||
import { spawn } from 'node:child_process';
|
||
import fs from 'node:fs';
|
||
import os from 'node:os';
|
||
import path from 'node:path';
|
||
|
||
const EDGE = process.env.EDGE_PATH
|
||
|| 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe';
|
||
/** 有 CDP_PORT 就附着到已运行的浏览器(Electron 桌面窗口就是这种用法) */
|
||
const attachPort = process.env.CDP_PORT ? Number(process.env.CDP_PORT) : null;
|
||
const PORT = attachPort || 9333 + Math.floor(Math.random() * 200);
|
||
const [, , url, outFile, waitMsArg, stepsArg] = process.argv;
|
||
const waitMs = Number(waitMsArg || 9000);
|
||
// stepsArg 可以是 JSON 字面量,也可以是 @文件路径(PowerShell 5.1 传引号不可靠,推荐文件)
|
||
const steps = stepsArg
|
||
? JSON.parse(stepsArg.startsWith('@') ? fs.readFileSync(stepsArg.slice(1), 'utf8') : stepsArg)
|
||
: [];
|
||
// 浏览器 profile 放系统临时目录,别污染输出目录
|
||
const profile = path.join(os.tmpdir(), `doubao-shot-${PORT}`);
|
||
|
||
const child = attachPort ? null : spawn(
|
||
EDGE,
|
||
[
|
||
'--headless=new',
|
||
'--no-sandbox',
|
||
'--disable-gpu',
|
||
'--disable-crash-reporter',
|
||
'--no-first-run',
|
||
'--disable-extensions',
|
||
'--hide-scrollbars',
|
||
`--remote-debugging-port=${PORT}`,
|
||
`--user-data-dir=${profile}`,
|
||
'--window-size=420,900',
|
||
'about:blank',
|
||
],
|
||
{ stdio: 'ignore', windowsHide: true },
|
||
);
|
||
|
||
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
||
|
||
async function findPage() {
|
||
for (let i = 0; i < 60; i += 1) {
|
||
try {
|
||
const res = await fetch(`http://127.0.0.1:${PORT}/json/list`);
|
||
const list = await res.json();
|
||
const page = list.find((t) => t.type === 'page');
|
||
if (page?.webSocketDebuggerUrl) return page;
|
||
} catch {
|
||
/* 还没起来 */
|
||
}
|
||
await sleep(300);
|
||
}
|
||
throw new Error('连不上 CDP');
|
||
}
|
||
|
||
function connect(wsUrl) {
|
||
return new Promise((resolve, reject) => {
|
||
const ws = new WebSocket(wsUrl);
|
||
let id = 0;
|
||
const pending = new Map();
|
||
const listeners = [];
|
||
ws.addEventListener('open', () =>
|
||
resolve({
|
||
send(method, params = {}) {
|
||
const msgId = ++id;
|
||
ws.send(JSON.stringify({ id: msgId, method, params }));
|
||
return new Promise((res, rej) => pending.set(msgId, { res, rej }));
|
||
},
|
||
on(fn) {
|
||
listeners.push(fn);
|
||
},
|
||
close: () => ws.close(),
|
||
}));
|
||
ws.addEventListener('error', reject);
|
||
ws.addEventListener('message', (event) => {
|
||
const msg = JSON.parse(event.data);
|
||
if (msg.id && pending.has(msg.id)) {
|
||
const { res, rej } = pending.get(msg.id);
|
||
pending.delete(msg.id);
|
||
if (msg.error) rej(new Error(JSON.stringify(msg.error)));
|
||
else res(msg.result);
|
||
} else if (msg.method) {
|
||
for (const fn of listeners) fn(msg);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
function describe(msg) {
|
||
if (msg.method === 'Runtime.consoleAPICalled') {
|
||
const text = (msg.params.args || [])
|
||
.map((a) => a.value ?? a.description ?? a.unserializableValue ?? a.type)
|
||
.join(' ');
|
||
return `[console.${msg.params.type}] ${text}`;
|
||
}
|
||
if (msg.method === 'Runtime.exceptionThrown') {
|
||
const d = msg.params.exceptionDetails;
|
||
return `[exception] ${d.text} ${d.exception?.description || ''}`.slice(0, 1200);
|
||
}
|
||
if (msg.method === 'Log.entryAdded') {
|
||
const e = msg.params.entry;
|
||
return `[log.${e.level}] ${e.text}`.slice(0, 800);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
(async () => {
|
||
const page = await findPage();
|
||
const cdp = await connect(page.webSocketDebuggerUrl);
|
||
const logs = [];
|
||
cdp.on((msg) => {
|
||
const line = describe(msg);
|
||
if (line) logs.push(line);
|
||
});
|
||
|
||
await cdp.send('Runtime.enable');
|
||
await cdp.send('Log.enable');
|
||
await cdp.send('Page.enable');
|
||
if (!attachPort) {
|
||
// 自己起的 Edge 需要模拟成手机尺寸;附着到 Electron 窗口时用窗口真实尺寸
|
||
// VIEWPORT=宽x高 / SCALE=倍率 可以覆盖(栅格化图标时要用整页尺寸 + 1x)
|
||
const [vw, vh] = (process.env.VIEWPORT || '420x900').split('x').map(Number);
|
||
const scale = Number(process.env.SCALE || 2);
|
||
await cdp.send('Emulation.setDeviceMetricsOverride', {
|
||
width: vw,
|
||
height: vh,
|
||
deviceScaleFactor: scale,
|
||
mobile: scale > 1,
|
||
});
|
||
}
|
||
|
||
if (url && url !== '-') {
|
||
await cdp.send('Page.navigate', { url });
|
||
}
|
||
await sleep(waitMs);
|
||
|
||
for (const step of steps) {
|
||
if (step.wait) {
|
||
await sleep(step.wait);
|
||
} else if (step.click) {
|
||
const [x, y] = step.click;
|
||
await cdp.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x, y });
|
||
await cdp.send('Input.dispatchMouseEvent', { type: 'mousePressed', x, y, button: 'left', clickCount: 1 });
|
||
await sleep(60);
|
||
await cdp.send('Input.dispatchMouseEvent', { type: 'mouseReleased', x, y, button: 'left', clickCount: 1 });
|
||
await sleep(step.after || 600);
|
||
} else if (step.type !== undefined) {
|
||
for (const ch of step.type) {
|
||
await cdp.send('Input.insertText', { text: ch });
|
||
await sleep(25);
|
||
}
|
||
await sleep(step.after || 400);
|
||
} else if (step.key) {
|
||
await cdp.send('Input.dispatchKeyEvent', {
|
||
type: 'keyDown',
|
||
key: step.key,
|
||
code: step.key === 'Enter' ? 'Enter' : step.key,
|
||
windowsVirtualKeyCode: step.key === 'Enter' ? 13 : 0,
|
||
});
|
||
await cdp.send('Input.dispatchKeyEvent', {
|
||
type: 'keyUp',
|
||
key: step.key,
|
||
code: step.key === 'Enter' ? 'Enter' : step.key,
|
||
});
|
||
await sleep(step.after || 600);
|
||
} else if (step.wheel) {
|
||
// [x, y, deltaY]
|
||
const [x, y, deltaY] = step.wheel;
|
||
await cdp.send('Input.dispatchMouseEvent', {
|
||
type: 'mouseWheel',
|
||
x,
|
||
y,
|
||
deltaX: 0,
|
||
deltaY,
|
||
});
|
||
await sleep(step.after || 500);
|
||
} else if (step.eval) {
|
||
const res = await cdp.send('Runtime.evaluate', { expression: step.eval, awaitPromise: true, returnByValue: true });
|
||
console.log('[step-eval]', JSON.stringify(res.result?.value ?? res.result?.description ?? null).slice(0, 1200));
|
||
await sleep(step.after || 400);
|
||
}
|
||
}
|
||
|
||
const probe = await cdp.send('Runtime.evaluate', {
|
||
expression: `(() => {
|
||
const v = document.querySelector('lynx-view');
|
||
const sr = v && v.shadowRoot;
|
||
return JSON.stringify({
|
||
hasView: !!v,
|
||
viewRect: v ? [v.clientWidth, v.clientHeight] : null,
|
||
shadowChildren: sr ? sr.children.length : -1,
|
||
shadowHtmlLen: sr ? sr.innerHTML.length : -1,
|
||
bodyText: (document.body.innerText || '').slice(0, 200),
|
||
});
|
||
})()`,
|
||
returnByValue: true,
|
||
});
|
||
console.log('[probe]', probe.result?.value);
|
||
|
||
const shot = await cdp.send('Page.captureScreenshot', {
|
||
format: 'png',
|
||
captureBeyondViewport: false,
|
||
// OMIT_BG=1 时输出透明背景,用来把 SVG 栅格化成带透明通道的 PNG
|
||
omitBackground: process.env.OMIT_BG === '1',
|
||
});
|
||
fs.writeFileSync(outFile, Buffer.from(shot.data, 'base64'));
|
||
console.log('[shot]', outFile, fs.statSync(outFile).size, 'bytes');
|
||
|
||
console.log('--- console ---');
|
||
for (const line of logs.slice(0, 60)) console.log(line);
|
||
|
||
cdp.close();
|
||
child?.kill();
|
||
await sleep(500);
|
||
process.exit(0);
|
||
})().catch(async (err) => {
|
||
console.error('[fatal]', err.message);
|
||
child?.kill();
|
||
process.exit(1);
|
||
});
|