豆包复刻:原生安卓(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。
This commit is contained in:
l
2026-10-09 14:09:36 +08:00
commit f056bcf45a
308 changed files with 46874 additions and 0 deletions

141
server/src/uploads.mjs Normal file
View File

@@ -0,0 +1,141 @@
/**
* 附件:上传、Electron 原生选文件、以及"宿主页拖拽进来的文件"待取队列。
* 图片会以 data URL 形式喂给支持视觉的模型;文本类文件直接内联正文。
*/
import fs from 'node:fs';
import path from 'node:path';
import { DATA_DIR } from './config.mjs';
export const UPLOADS_DIR = path.join(DATA_DIR, 'uploads');
fs.mkdirSync(UPLOADS_DIR, { recursive: true });
const IMAGE_EXT = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif', '.bmp']);
const TEXT_EXT = new Set([
'.txt', '.md', '.markdown', '.json', '.csv', '.tsv', '.log', '.ini', '.toml', '.yml', '.yaml',
'.py', '.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.html', '.htm', '.css', '.scss',
'.xml', '.sql', '.sh', '.bat', '.ps1', '.java', '.c', '.h', '.cpp', '.cs', '.go', '.rs',
'.rb', '.php', '.swift', '.kt', '.lua', '.r', '.vue', '.svelte',
]);
/** 复制进 uploads 的上限;再大就只登记路径,不复制 */
const COPY_LIMIT = 25 * 1024 * 1024;
/** 喂给模型的单张图片上限 */
const VISION_LIMIT = 6 * 1024 * 1024;
let pending = [];
function rid() {
return `f_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 6)}`;
}
function safeName(name) {
return String(name || 'file').replace(/[\\/:*?"<>|]/g, '_').slice(-60);
}
export function isImage(name) {
return IMAGE_EXT.has(path.extname(String(name || '')).toLowerCase());
}
export function isText(name) {
return TEXT_EXT.has(path.extname(String(name || '')).toLowerCase());
}
function mimeOf(name) {
const ext = path.extname(String(name || '')).toLowerCase();
const map = {
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp',
'.gif': 'image/gif', '.bmp': 'image/bmp', '.pdf': 'application/pdf',
'.json': 'application/json', '.md': 'text/markdown',
};
return map[ext] || (isText(name) ? 'text/plain' : 'application/octet-stream');
}
function meta(filePath, name, { copy = true } = {}) {
const stat = fs.statSync(filePath);
let storedPath = filePath;
let url = '';
if (copy && stat.size <= COPY_LIMIT) {
const target = path.join(UPLOADS_DIR, `${Date.now().toString(36)}_${safeName(name)}`);
fs.copyFileSync(filePath, target);
storedPath = target;
url = `/uploads/${path.basename(target)}`;
}
return {
id: rid(),
name: String(name),
size: stat.size,
mime: mimeOf(name),
path: storedPath,
url,
at: new Date().toISOString(),
};
}
/** 浏览器上传:base64 → 落盘 */
export function saveUpload({ name, base64 }) {
if (!name || !base64) throw new Error('缺少 name 或 base64');
const buffer = Buffer.from(String(base64).replace(/^data:[^,]+,/, ''), 'base64');
if (buffer.length > COPY_LIMIT) throw new Error(`文件超过 ${COPY_LIMIT / 1024 / 1024}MB`);
const target = path.join(UPLOADS_DIR, `${Date.now().toString(36)}_${safeName(name)}`);
fs.writeFileSync(target, buffer);
return {
id: rid(),
name: String(name),
size: buffer.length,
mime: mimeOf(name),
path: target,
url: `/uploads/${path.basename(target)}`,
at: new Date().toISOString(),
};
}
/** 桌面版原生选文件:登记(必要时复制) */
export function registerLocalFile(filePath) {
if (!fs.existsSync(filePath)) throw new Error(`文件不存在:${filePath}`);
return meta(filePath, path.basename(filePath));
}
/** 宿主页拖拽进来的文件先放这里,等界面来取 */
export function addPending(items) {
pending.push(...items);
}
export function takePending() {
const items = pending;
pending = [];
return items;
}
/**
* 把附件转成模型能吃的形式。
* @returns {{text: string, images: {url: string}[]}}
*/
export function attachmentPayload(attachment) {
const out = { text: '', images: [] };
if (!attachment || !fs.existsSync(attachment.path)) {
out.text = `【附件 ${attachment?.name || '未知'} 已丢失】`;
return out;
}
const size = fs.statSync(attachment.path).size;
if (isImage(attachment.name) && size <= VISION_LIMIT) {
const b64 = fs.readFileSync(attachment.path).toString('base64');
out.images.push({ url: `data:${attachment.mime};base64,${b64}` });
out.text = `【图片 ${attachment.name}】`;
return out;
}
if (isText(attachment.name) || size < 64 * 1024) {
let body = '';
try {
body = fs.readFileSync(attachment.path, 'utf8');
} catch {
body = '';
}
if (body) {
const clipped = body.length > 20000 ? `${body.slice(0, 20000)}\n…(已截断,共 ${body.length} 字符)` : body;
out.text = `【文件 ${attachment.name}(${size} 字节)】\n\`\`\`\n${clipped}\n\`\`\``;
return out;
}
}
out.text = `【附件 ${attachment.name}(${size} 字节,二进制)本地路径:${attachment.path}】`;
return out;
}