Files
doubao-clone/desktop/main.cjs
l f056bcf45a 豆包复刻:原生安卓(Compose + 内嵌 Termux)+ Web / Windows
一个界面照着豆包抠的 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。
2026-10-09 14:09:36 +08:00

169 lines
5.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 豆包 · LynxJS 复刻 —— Electron 桌面壳(CommonJS)
*
* 为什么用 CJS:Electron 主进程在 ESM 下解析 'electron' 会命中 npm 包
* (那个包的 index.js 只导出二进制路径),拿不到 app/BrowserWindow。
* 内核本身是 ESM,用动态 import() 加载即可。
*
* 主进程做三件事:
* 1. 进程内直接启动 AI 内核(不额外起 node 子进程)
* 2. 开一个手机比例窗口,加载 ?frame=0 的界面
* 3. 数据目录指到 %APPDATA%,避免装到 Program Files 后写不进去
*/
const { app, BrowserWindow, Menu, shell, dialog, clipboard } = require('electron');
const fs = require('node:fs');
const path = require('node:path');
const { pathToFileURL } = require('node:url');
/**
* 可选:开一个 CDP 调试端口,方便用脚本截图/验证桌面窗口。
*
* 为什么需要:Electron 的界面只有真窗口才渲染得出来,光跑 curl 验不了;
* 有了这个端口就能用 scripts/screenshot.mjs 附着上去截图(设 CDP_PORT 即可)。
* 默认不开,避免平时多一个监听端口。
*/
if (process.env.DOUBAO_CDP_PORT) {
app.commandLine.appendSwitch('remote-debugging-port', String(process.env.DOUBAO_CDP_PORT));
app.commandLine.appendSwitch('remote-allow-origins', '*');
}
// 打包后应用根目录是 resources/app(若打成 asar 则是 resources/app.asar);开发时是仓库根
const appRoot = app.isPackaged
? [
path.join(process.resourcesPath, 'app'),
path.join(process.resourcesPath, 'app.asar'),
].find((p) => fs.existsSync(p)) || path.join(process.resourcesPath, 'app')
: path.resolve(__dirname, '..');
// 打包后的自有资源放在 <appRoot>/runtime,开发时直接用仓库根
const bundledRuntime = path.join(appRoot, 'runtime');
const runtimeRoot = fs.existsSync(bundledRuntime) ? bundledRuntime : appRoot;
process.env.DOUBAO_RESOURCES = runtimeRoot;
process.env.DOUBAO_TOOLS = path.join(runtimeRoot, 'tools');
process.env.DOUBAO_DATA = path.join(app.getPath('userData'), 'data');
const serverEntry = path.join(appRoot, 'server', 'src', 'index.mjs');
// 给内核挂几个桌面能力钩子:定位配置文件、用系统浏览器打开链接、原生选文件、写剪贴板。
// 内核自己不认识 Electron,有钩子就用,没有就降级。
globalThis.__doubaoReveal = (target) => shell.showItemInFolder(target);
globalThis.__doubaoOpenExternal = (url) => shell.openExternal(url);
globalThis.__doubaoClipboard = (text) => clipboard.writeText(String(text));
globalThis.__doubaoPickFile = async () => {
const result = await dialog.showOpenDialog({
title: '选择要发给豆包的文件',
buttonLabel: '发送',
properties: ['openFile', 'multiSelections'],
filters: [
{
name: '常用文件',
extensions: ['png', 'jpg', 'jpeg', 'webp', 'gif', 'bmp', 'txt', 'md', 'json', 'csv',
'log', 'yml', 'yaml', 'py', 'js', 'ts', 'tsx', 'html', 'css', 'pdf'],
},
{ name: '所有文件', extensions: ['*'] },
],
});
return result.canceled ? [] : result.filePaths;
};
let kernel = null;
let win = null;
/** 启动内核:默认 5199,被占用就换随机端口 */
async function startKernelWithFallback() {
const { startKernel } = await import(pathToFileURL(serverEntry).href);
const preferred = Number(process.env.DOUBAO_PORT || 5199);
try {
return await startKernel({ port: preferred, quiet: true });
} catch (err) {
if (err && err.code === 'EADDRINUSE') {
console.warn(`[desktop] 端口 ${preferred} 被占用,改用随机端口`);
return startKernel({ port: 0, quiet: true });
}
throw err;
}
}
function createWindow(url) {
win = new BrowserWindow({
width: 420,
height: 900,
minWidth: 360,
minHeight: 620,
title: '豆包',
backgroundColor: '#ffffff',
autoHideMenuBar: true,
show: false,
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
spellcheck: false,
},
});
// 界面是移动端比例,菜单栏只会碍事:去掉,改用快捷键
Menu.setApplicationMenu(null);
win.once('ready-to-show', () => win.show());
win.webContents.on('before-input-event', (event, input) => {
const key = (input.key || '').toLowerCase();
if (key === 'f12' || (input.control && input.shift && key === 'i')) {
win.webContents.toggleDevTools();
event.preventDefault();
} else if (input.control && key === 'r') {
win.webContents.reload();
event.preventDefault();
}
});
// 外链交给系统浏览器
win.webContents.setWindowOpenHandler(({ url: target }) => {
if (/^https?:/i.test(target)) shell.openExternal(target);
return { action: 'deny' };
});
win.webContents.on('will-navigate', (event, target) => {
if (!target.startsWith(url)) {
event.preventDefault();
if (/^https?:/i.test(target)) shell.openExternal(target);
}
});
win.loadURL(`${url}?frame=0`);
return win;
}
// 只允许开一个实例,第二次启动就把已有窗口拉到前面
if (!app.requestSingleInstanceLock()) {
app.quit();
} else {
app.on('second-instance', () => {
if (win) {
if (win.isMinimized()) win.restore();
win.focus();
}
});
app.whenReady().then(async () => {
app.setAppUserModelId('com.doubao.lynxclone');
try {
kernel = await startKernelWithFallback();
createWindow(kernel.url);
console.log(`[desktop] 内核就绪:${kernel.url} 资源:${runtimeRoot}`);
console.log(`[desktop] 数据目录:${process.env.DOUBAO_DATA}`);
} catch (err) {
dialog.showErrorBox('豆包启动失败', String((err && err.stack) || (err && err.message) || err));
app.quit();
}
});
app.on('window-all-closed', async () => {
try {
if (kernel) await kernel.close();
} catch {
/* ignore */
}
app.quit();
});
}