豆包复刻:原生安卓(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:
74
tools/web_fetch.py
Normal file
74
tools/web_fetch.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""网页抓取工具:拉取 URL 正文并转成纯文本(不依赖第三方库)。"""
|
||||
|
||||
import html
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from _protocol import serve # noqa: E402
|
||||
|
||||
TOOL = {
|
||||
"name": "web_fetch",
|
||||
"description": "抓取一个网页或接口返回的文本内容(自动去 HTML 标签)。需要看某个链接里的内容时使用。",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"url": {"type": "string", "description": "要抓取的 http/https 地址"},
|
||||
"max_chars": {"type": "number", "description": "最多返回多少字符,默认 4000,上限 20000"},
|
||||
},
|
||||
"required": ["url"],
|
||||
},
|
||||
}
|
||||
|
||||
SCRIPT_RE = re.compile(r"<(script|style|noscript)[^>]*>.*?</\1>", re.S | re.I)
|
||||
TAG_RE = re.compile(r"<[^>]+>")
|
||||
SPACE_RE = re.compile(r"[ \t\r\f\v]+")
|
||||
BLANK_RE = re.compile(r"\n{3,}")
|
||||
|
||||
|
||||
def strip_html(raw):
|
||||
text = SCRIPT_RE.sub(" ", raw)
|
||||
text = re.sub(r"<br\s*/?>", "\n", text, flags=re.I)
|
||||
text = re.sub(r"</(p|div|li|h[1-6]|tr)>", "\n", text, flags=re.I)
|
||||
text = TAG_RE.sub(" ", text)
|
||||
text = html.unescape(text)
|
||||
text = SPACE_RE.sub(" ", text)
|
||||
text = "\n".join(line.strip() for line in text.split("\n"))
|
||||
return BLANK_RE.sub("\n\n", text).strip()
|
||||
|
||||
|
||||
def run(args):
|
||||
url = str(args.get("url", "")).strip()
|
||||
if not url.startswith(("http://", "https://")):
|
||||
raise ValueError("url 必须是 http/https 开头")
|
||||
limit = max(200, min(20000, int(args.get("max_chars", 4000) or 4000)))
|
||||
|
||||
req = urllib.request.Request(
|
||||
url,
|
||||
headers={
|
||||
"User-Agent": "Mozilla/5.0 (compatible; doubao-kernel/0.1)",
|
||||
"Accept": "text/html,application/json;q=0.9,*/*;q=0.8",
|
||||
},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=25) as resp:
|
||||
charset = resp.headers.get_content_charset() or "utf-8"
|
||||
raw = resp.read(2_000_000).decode(charset, errors="replace")
|
||||
content_type = resp.headers.get("Content-Type", "")
|
||||
|
||||
if "json" in content_type.lower():
|
||||
body = raw[:limit]
|
||||
else:
|
||||
body = strip_html(raw)[:limit]
|
||||
|
||||
return {
|
||||
"url": url,
|
||||
"content_type": content_type,
|
||||
"truncated": len(body) >= limit,
|
||||
"content": body,
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
serve(TOOL, run)
|
||||
Reference in New Issue
Block a user