豆包复刻:原生安卓(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:
144
server/src/store.mjs
Normal file
144
server/src/store.mjs
Normal file
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* 会话持久化:<数据目录>/conversations.json
|
||||
* 结构:{ conversations: [{ id, title, agentId, createdAt, updatedAt, pinned, messages: [...] }] }
|
||||
*
|
||||
* 这里不放假数据:列表里的每一条都是真聊出来的,
|
||||
* 列表上显示的就是最后一条消息的预览。
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { DATA_DIR } from './config.mjs';
|
||||
|
||||
const FILE = path.join(DATA_DIR, 'conversations.json');
|
||||
|
||||
function nowIso() {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function rid() {
|
||||
return `c_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 7)}`;
|
||||
}
|
||||
|
||||
function freshState() {
|
||||
return {
|
||||
conversations: [
|
||||
{
|
||||
id: 'c_main',
|
||||
title: '主对话',
|
||||
agentId: 'a_doubao',
|
||||
pinned: true,
|
||||
createdAt: nowIso(),
|
||||
updatedAt: nowIso(),
|
||||
messages: [],
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
let state = null;
|
||||
|
||||
function load() {
|
||||
if (state) return state;
|
||||
if (fs.existsSync(FILE)) {
|
||||
try {
|
||||
state = JSON.parse(fs.readFileSync(FILE, 'utf8'));
|
||||
} catch (err) {
|
||||
console.error('[store] conversations.json 解析失败,重建:', err.message);
|
||||
state = freshState();
|
||||
}
|
||||
} else {
|
||||
state = freshState();
|
||||
}
|
||||
if (!Array.isArray(state.conversations) || state.conversations.length === 0) state = freshState();
|
||||
return state;
|
||||
}
|
||||
|
||||
function persist() {
|
||||
fs.writeFileSync(FILE, JSON.stringify(load(), null, 2), 'utf8');
|
||||
}
|
||||
|
||||
function previewOf(messages) {
|
||||
for (let i = messages.length - 1; i >= 0; i -= 1) {
|
||||
const msg = messages[i];
|
||||
if (!msg.content) continue;
|
||||
const text = String(msg.content).replace(/\s+/g, ' ').trim();
|
||||
if (!text) continue;
|
||||
const tag = msg.role === 'user' ? '我:' : '';
|
||||
return `${tag}${text.length > 30 ? `${text.slice(0, 30)}…` : text}`;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/** 列表:带最后一条消息预览,按置顶 + 更新时间排 */
|
||||
export function listConversations() {
|
||||
return load().conversations
|
||||
.slice()
|
||||
.sort((a, b) => {
|
||||
if (Boolean(b.pinned) !== Boolean(a.pinned)) return Number(b.pinned) - Number(a.pinned);
|
||||
return String(b.updatedAt).localeCompare(String(a.updatedAt));
|
||||
})
|
||||
.map(({ messages, ...meta }) => ({
|
||||
...meta,
|
||||
messageCount: messages?.length || 0,
|
||||
preview: previewOf(messages || []),
|
||||
}));
|
||||
}
|
||||
|
||||
export function getConversation(id) {
|
||||
return load().conversations.find((c) => c.id === id) || null;
|
||||
}
|
||||
|
||||
export function createConversation(title = '新对话', agentId) {
|
||||
const item = {
|
||||
id: rid(),
|
||||
title,
|
||||
agentId: agentId || '',
|
||||
pinned: false,
|
||||
createdAt: nowIso(),
|
||||
updatedAt: nowIso(),
|
||||
messages: [],
|
||||
};
|
||||
load().conversations.unshift(item);
|
||||
persist();
|
||||
return item;
|
||||
}
|
||||
|
||||
export function updateConversation(id, patch) {
|
||||
const item = getConversation(id);
|
||||
if (!item) return null;
|
||||
Object.assign(item, patch, { updatedAt: nowIso() });
|
||||
persist();
|
||||
return item;
|
||||
}
|
||||
|
||||
export function deleteConversation(id) {
|
||||
const s = load();
|
||||
const i = s.conversations.findIndex((c) => c.id === id);
|
||||
if (i < 0) return false;
|
||||
s.conversations.splice(i, 1);
|
||||
if (s.conversations.length === 0) s.conversations.push(...freshState().conversations);
|
||||
persist();
|
||||
return true;
|
||||
}
|
||||
|
||||
export function appendMessages(id, messages) {
|
||||
const item = getConversation(id);
|
||||
if (!item) return null;
|
||||
item.messages.push(...messages);
|
||||
item.updatedAt = nowIso();
|
||||
persist();
|
||||
return item;
|
||||
}
|
||||
|
||||
/** 用首条用户消息自动起标题 */
|
||||
export function autoTitle(id, text) {
|
||||
const item = getConversation(id);
|
||||
if (!item || (item.title !== '新对话' && item.title !== '主对话')) return;
|
||||
const title = String(text).replace(/\s+/g, ' ').trim().slice(0, 18);
|
||||
if (title) updateConversation(id, { title });
|
||||
}
|
||||
|
||||
/** 会话数据文件位置(界面上显示用) */
|
||||
export function storeFilePath() {
|
||||
return FILE;
|
||||
}
|
||||
Reference in New Issue
Block a user