Files
doubao-clone/tools/shell.py
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

105 lines
3.3 KiB
Python
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.

"""终端工具:在系统 shell 里执行命令,拿回 stdout/stderr。
- Windows 走 cmd /c,Linux/macOS/Android 走 /bin/sh -c
- 有超时和一份最小黑名单(防止一条命令把机器搞没)
- 想放开限制:把环境变量 DOUBAO_SHELL_UNSAFE=1 打开(或在设置里关掉限制模式)
"""
import os
import platform
import subprocess
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _protocol import serve # noqa: E402
TOOL = {
"name": "shell",
"description": (
"在用户电脑/手机的终端里执行一条 shell 命令,返回退出码、标准输出和标准错误。"
"适合查看文件、跑脚本、装依赖、查系统信息。危险命令会被拦截。"
),
"parameters": {
"type": "object",
"properties": {
"command": {"type": "string", "description": "要执行的命令,例如 ls -la 或 python --version"},
"cwd": {"type": "string", "description": "工作目录,可留空"},
"timeout": {"type": "number", "description": "超时秒数,默认 30,最大 300"},
},
"required": ["command"],
},
}
# 明显会搞坏机器的模式(可用 DOUBAO_SHELL_UNSAFE=1 关闭)
BLOCKED = [
"rm -rf /",
"rm -rf /*",
"mkfs",
"dd if=/dev/zero",
":(){:|:&};:",
"shutdown",
"reboot",
"format c:",
"del /f /s /q c:\\",
"rmdir /s /q c:\\",
]
def run(args):
command = str(args.get("command", "")).strip()
if not command:
raise ValueError("command 不能为空")
if len(command) > 4000:
raise ValueError("命令过长")
unsafe = os.environ.get("DOUBAO_SHELL_UNSAFE") == "1"
if not unsafe:
lowered = command.lower()
for bad in BLOCKED:
if bad in lowered:
raise ValueError(f"命令命中黑名单({bad}),已拒绝执行。确实需要请设置 DOUBAO_SHELL_UNSAFE=1")
timeout = max(1, min(300, int(args.get("timeout", 30) or 30)))
cwd = args.get("cwd") or None
if cwd and not os.path.isdir(cwd):
raise ValueError(f"工作目录不存在:{cwd}")
system = platform.system().lower()
if system == "windows":
# ★ Windows 的 cmd 默认用 GBK 输出,直接按 UTF-8 解会变乱码、按 GBK 解又和后端不一致。
# 这里强制切到 65001(UTF-8) 再执行,后面统一按 UTF-8 解码。
argv = ["cmd", "/c", f"chcp 65001 >nul && {command}"]
encoding = "utf-8"
via = "cmd"
else:
argv = ["/bin/sh", "-c", command]
encoding = "utf-8"
via = "sh"
if os.path.exists("/system/bin/sh") or "ANDROID_ROOT" in os.environ:
via = "toybox"
try:
proc = subprocess.run(
argv,
cwd=cwd,
capture_output=True,
timeout=timeout,
text=True,
encoding=encoding,
errors="replace",
)
except subprocess.TimeoutExpired:
return {"error": f"命令超时({timeout}s)", "timeout": True}
return {
"exit_code": proc.returncode,
"stdout": (proc.stdout or "")[-8000:],
"stderr": (proc.stderr or "")[-4000:],
"platform": f"{platform.system()} {platform.release()}",
"via": via,
}
if __name__ == "__main__":
serve(TOOL, run)