Files
doubao-clone/app/src/pages/Terminal.tsx
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

182 lines
6.8 KiB
TypeScript
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.

import { useCallback, useRef, useState } from '@lynx-js/react';
import { api } from '../api.js';
import { Icon } from '../Icon.js';
import { cv } from '../lynx-input.js';
interface TerminalProps {
onBack: () => void;
/** 当前是走 Termux 还是系统自带 shell(从 device 工具查询) */
shellChannel: string;
onRefreshChannel: () => void;
/** win32 / linux / android —— 决定预设命令用哪套 */
platform?: string;
}
/** 按平台给几条常用命令,点一下就跑,省得在手机上打字 */
const PRESETS_ANDROID = [
{ label: '我是谁', command: 'getprop ro.product.model; uname -a' },
{ label: 'Python', command: 'python -V' },
{ label: '电池', command: 'dumpsys battery | head -8' },
{ label: '存储', command: 'df -h /data /sdcard' },
];
const PRESETS_DESKTOP = [
{ label: '系统', command: 'ver' },
{ label: 'Python', command: 'python -V' },
{ label: '目录', command: 'dir' },
{ label: '网络', command: 'ping -n 2 127.0.0.1' },
];
const PRESETS_POSIX = [
{ label: '我是谁', command: 'uname -a; whoami' },
{ label: 'Python', command: 'python3 -V' },
{ label: '目录', command: 'pwd; ls -la' },
{ label: '网络', command: 'ping -c 2 127.0.0.1' },
];
interface Line {
kind: 'cmd' | 'out' | 'err' | 'note';
text: string;
}
/** 内置几条常用命令,点一下就跑,省得在手机上打字 */
const PRESETS = [
{ label: '我是谁', command: 'getprop ro.product.model; uname -a' },
{ label: 'Python', command: 'python -V; pip list 2>/dev/null | head -20' },
{ label: '文件', command: 'ls -la /sdcard/Android/data/com.doubao.lynxclone/files/tools' },
{ label: '网络', command: 'ping -c 2 8.8.8.8' },
];
/**
* 应用内终端。
*
* 命令不是直接本地执行的,而是走内核的 shell 工具 —— 这样电脑/手机走的是同一条路:
* 手机:内核优先交给 Termux(装了且开了 allow-external-apps),否则退回系统 toybox
* 电脑:cmd /c 或 /bin/sh -c
* 所以在手机上它其实就是"Termux 的另一个入口",还省得在两个 App 之间来回切。
*/
export function TerminalPage(props: TerminalProps) {
const { onBack, shellChannel, onRefreshChannel, platform } = props;
const presets = platform === 'android' ? PRESETS_ANDROID
: platform === 'win32' ? PRESETS_DESKTOP
: PRESETS_POSIX;
const [lines, setLines] = useState<Line[]>([
{ kind: 'note', text: '输入命令回车执行。装了 Termux 的话会自动走 Termux 的 bash。' },
]);
const [draft, setDraft] = useState('');
const [busy, setBusy] = useState(false);
const [signal, setSignal] = useState(0);
const history = useRef<string[]>([]);
const push = useCallback((line: Line) => {
setLines((prev) => [...prev.slice(-200), line]);
setSignal((n) => n + 1);
}, []);
const runCommand = useCallback(async (command: string) => {
const cmd = command.trim();
if (!cmd || busy) return;
history.current = [cmd, ...history.current.filter((c) => c !== cmd)].slice(0, 30);
push({ kind: 'cmd', text: `$ ${cmd}` });
setDraft('');
setBusy(true);
try {
const res = await api.runTool('shell', { command: cmd, timeout: 60 });
if (!res.ok) {
push({ kind: 'err', text: res.error || '执行失败' });
} else {
const r = (res.result || {}) as { stdout?: string; stderr?: string; exit_code?: number; via?: string; termux_note?: string };
if (r.stdout) push({ kind: 'out', text: r.stdout.replace(/\s+$/, '') });
if (r.stderr) push({ kind: 'err', text: r.stderr.replace(/\s+$/, '') });
push({
kind: 'note',
text: `[退出码 ${r.exit_code ?? '?'}|通道 ${
r.via === 'termux' ? 'Termux'
: r.via === 'toybox' ? '系统 shell (toybox)'
: r.via === 'cmd' ? 'Windows cmd'
: r.via === 'sh' ? '/bin/sh'
: r.via || '未知'
}]`,
});
if (r.termux_note) push({ kind: 'note', text: r.termux_note });
if (r.via && !termuxReady) onRefreshChannel();
}
} catch (err) {
push({ kind: 'err', text: `连不上内核:${(err as Error).message}` });
} finally {
setBusy(false);
onRefreshChannel();
}
}, [busy, push, onRefreshChannel]);
const handleInput = useCallback((e: { detail: { value: string } }) => setDraft(e.detail.value), []);
const handleConfirm = useCallback(() => { runCommand(draft); }, [draft, runCommand]);
const termuxReady = shellChannel === 'termux';
const channelText = termuxReady
? '通道:Termux(完整 Linux:bash / apt / pip / node)'
: shellChannel === 'cmd' ? '通道:Windows cmd'
: shellChannel === 'sh' ? '通道:/bin/sh'
: shellChannel === 'toybox' || shellChannel === 'android'
? '通道:系统自带 shell(toybox)。装 Termux 并在其中执行一次 echo "allow-external-apps=true" >> ~/.termux/termux.properties 即可升级'
: '通道:未知(内核可能没连上)';
return (
<view className="page terminal-page">
<view className="term-head">
<view className="hdr-btn" bindtap={onBack}>
<Icon name="chevron-right" className="icon back-flip" />
</view>
<text className="term-title">终端</text>
<view className="hdr-btn" bindtap={() => setLines([])}>
<Icon name="close" className="icon-sm" />
</view>
</view>
<view className={termuxReady ? 'term-chip term-chip-ok' : 'term-chip'}>
<text className="term-chip-text">{channelText}</text>
</view>
<view className="term-presets">
{presets.map((preset) => (
<view className="chip" key={preset.label} bindtap={() => runCommand(preset.command)}>
<text className="chip-text">{preset.label}</text>
</view>
))}
</view>
<scroll-view className="term-body" scroll-orientation="vertical" scroll-y scroll-top={900000 + signal}>
<view>
{lines.map((line, index) => (
<text
key={`l${index}`}
className={
line.kind === 'cmd' ? 'term-line term-cmd'
: line.kind === 'err' ? 'term-line term-err'
: line.kind === 'note' ? 'term-line term-note'
: 'term-line'
}
>
{line.text}
</text>
))}
{busy ? <text className="term-line term-note">执行中…</text> : null}
</view>
</scroll-view>
<view className="term-input-bar">
<input
className="term-input"
placeholder="输入命令,回车执行"
{...cv(draft)}
bindinput={handleInput}
bindconfirm={handleConfirm}
/>
<view className="send-btn" bindtap={handleConfirm}>
<Icon name="send" className="icon-sm" />
</view>
</view>
</view>
);
}