WIP: 打开网页按钮 + 全屏网页界面 + 网页点选(待改成 AI 可写的数据回流接口)
This commit is contained in:
34
scripts/probe-check-conv.py
Normal file
34
scripts/probe-check-conv.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import json
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
URL = "http://127.0.0.1:15199"
|
||||
|
||||
|
||||
def call(path, payload=None, method="POST", timeout=60):
|
||||
data = json.dumps(payload, ensure_ascii=False).encode("utf-8") if payload is not None else None
|
||||
req = urllib.request.Request(
|
||||
URL + path, data=data, method=method,
|
||||
headers={"Content-Type": "application/json; charset=utf-8"},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
return json.loads(r.read().decode("utf-8"))
|
||||
|
||||
|
||||
convs = call("/api/conversations", method="GET").get("conversations") or []
|
||||
print("会话:")
|
||||
for c in convs:
|
||||
print(" ", c.get("id"), "|", c.get("title"), "| 消息", c.get("messageCount"),
|
||||
"| updated", c.get("updatedAt"))
|
||||
|
||||
# 直接看 c_main 里到底有没有消息
|
||||
full = call("/api/conversations/c_main", method="GET").get("conversation") or {}
|
||||
msgs = full.get("messages") or []
|
||||
print(f"\nc_main 里 {len(msgs)} 条消息:")
|
||||
for m in msgs:
|
||||
print(" ", m.get("role"), "|", repr((m.get("content") or "")[:60]))
|
||||
62
scripts/probe-seed-link-msg.py
Normal file
62
scripts/probe-seed-link-msg.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""给会话塞一条带链接的助手回复,用来测「打开网页」按钮。
|
||||
|
||||
★ 为什么要造数据而不是真让模型回一条:
|
||||
真回一条要配 API key、要等流式、还有网络不确定性 —— 而我们这里要测的是
|
||||
**界面上那个按钮在不在、点了对不对**,跟回复怎么来的无关。
|
||||
直接写一条一样的消息,测起来快且稳定。
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
URL = "http://127.0.0.1:15199"
|
||||
|
||||
|
||||
def call(path, payload=None, method="POST", timeout=60):
|
||||
data = json.dumps(payload, ensure_ascii=False).encode("utf-8") if payload is not None else None
|
||||
req = urllib.request.Request(
|
||||
URL + path, data=data, method=method,
|
||||
headers={"Content-Type": "application/json; charset=utf-8"},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
return json.loads(r.read().decode("utf-8"))
|
||||
|
||||
|
||||
convs = call("/api/conversations", method="GET").get("conversations") or []
|
||||
print("会话数:", len(convs))
|
||||
for c in convs[:5]:
|
||||
print(" ", c.get("id"), "|", c.get("title"), "| 消息", c.get("messageCount"))
|
||||
|
||||
if not convs:
|
||||
print("!! 没有会话")
|
||||
sys.exit(1)
|
||||
|
||||
cid = convs[0]["id"]
|
||||
print(f"\n往 {cid} 里塞一条带链接的回复…")
|
||||
|
||||
content = (
|
||||
"我查到了几个可能有用的页面:\n\n"
|
||||
"- [Example 官网](https://example.com)\n"
|
||||
"- [Example 中文](https://example.com/index-zh)\n\n"
|
||||
"**说明**:这两个是示例域名,用来验证「打开网页」按钮。\n"
|
||||
"还有一个没有链接的说法:这是个普通段落。"
|
||||
)
|
||||
|
||||
res = call(f"/api/conversations/{cid}", {
|
||||
"messages": [
|
||||
{"role": "user", "content": "帮我找几个网页"},
|
||||
{"role": "assistant", "content": content},
|
||||
],
|
||||
}, method="PATCH")
|
||||
print("写入结果:", json.dumps(res, ensure_ascii=False)[:200])
|
||||
|
||||
full = call(f"/api/conversations/{cid}", method="GET").get("conversation") or {}
|
||||
msgs = full.get("messages") or []
|
||||
print("现在有", len(msgs), "条消息")
|
||||
for m in msgs:
|
||||
print(" ", m.get("role"), "|", (m.get("content") or "")[:50].replace("\n", " "))
|
||||
264
scripts/verify-web-pick.py
Normal file
264
scripts/verify-web-pick.py
Normal file
@@ -0,0 +1,264 @@
|
||||
"""守「网页点选回传给 AI」这条链路的硬性质。
|
||||
|
||||
## 为什么这些性质值得钉住
|
||||
|
||||
这条链路最容易被"顺手写成不安全"的地方,就是**给外部网页挂 JS 桥**。
|
||||
看起来最省事:`addJavascriptInterface` 一下,页面里 `Doubao.pick(...)` 就完事。
|
||||
但这里浏览的是**互联网上任意一个站点** —— 挂桥等于把 App 的内部接口交出去。
|
||||
|
||||
所以这里守的第一条就是:**没有桥**,改用"页面写数组 + Kotlin 主动轮询取走",
|
||||
数据只从页面流向 App,页面**没有任何办法**主动调 App。
|
||||
|
||||
用法:
|
||||
python scripts/verify-web-pick.py
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
WS = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
UI = os.path.join(WS, "android", "app", "src", "main", "java", "com", "doubao", "lynxclone", "ui")
|
||||
|
||||
fails = []
|
||||
|
||||
|
||||
def fail(m):
|
||||
fails.append(m)
|
||||
print(f" ✗ {m}")
|
||||
|
||||
|
||||
def ok(m):
|
||||
print(f" ✓ {m}")
|
||||
|
||||
|
||||
def read(name):
|
||||
p = os.path.join(UI, name)
|
||||
if not os.path.isfile(p):
|
||||
print(f"!! 找不到 {p}")
|
||||
sys.exit(1)
|
||||
# ★ 只去块注释,不去行注释 —— 行注释里常有 "http://" 之类的字符串,
|
||||
# 第一版验证脚本就是这么把自己坑了的(见 verify-ai-webview.py)。
|
||||
return re.sub(r"/\*.*?\*/", "", open(p, encoding="utf-8").read(), flags=re.DOTALL)
|
||||
|
||||
|
||||
web = read("WebPageScreen.kt")
|
||||
pick = read("WebPick.kt")
|
||||
root = read("AppRoot.kt")
|
||||
chat = read("ChatScreen.kt")
|
||||
vm = read("AppViewModel.kt")
|
||||
md = read("MarkdownText.kt")
|
||||
|
||||
print("=== 一、安全性(最重要)===")
|
||||
|
||||
# 1. 浏览外部网站的 WebView 绝不能挂桥
|
||||
#
|
||||
# ★ 判据要**排除注释**:源码里有一句"这个 WebView 里不挂任何 JS 桥",
|
||||
# 第一版直接在这份去块注释的文本里找 `addJavascriptInterface`,被那句话
|
||||
# 绊了个跟头(同一类误报在 verify-ai-webview.py 里也出过一次)。
|
||||
# 所以只看**去掉所有注释行**之后的代码。
|
||||
code_only = "\n".join(
|
||||
ln for ln in web.splitlines() if not ln.strip().startswith("//")
|
||||
)
|
||||
|
||||
if "addJavascriptInterface" in code_only:
|
||||
fail("WebPageScreen 里挂了 JavascriptInterface —— 浏览外部网站时绝不能这么干")
|
||||
else:
|
||||
ok("WebPageScreen 的代码里没有 addJavascriptInterface(外部页面碰不到 App)")
|
||||
|
||||
# ★ 判据要收窄:`Doubao` 这个名字在这份文件里到处都有
|
||||
# (`Doubao.Blue`、`doubaoText()`、`DoubaoClone` 的 UA),
|
||||
# 光找 "Doubao" 永远会红。真正该防的是**桥方法的调用**。
|
||||
bridge_call = re.search(r"Doubao\s*\.\s*(pick|close)\s*\(", code_only)
|
||||
if bridge_call:
|
||||
fail(f"外部浏览这条路上出现了桥方法调用:{bridge_call.group(0)}")
|
||||
else:
|
||||
ok("外部浏览这条路上没有桥方法调用(pick/close 都不在)")
|
||||
|
||||
# 2. 通信必须是"页面写队列 + App 主动取",而不是页面主动调
|
||||
if "evaluateJavascript" in web:
|
||||
ok("用 evaluateJavascript 主动取数据(单向)")
|
||||
else:
|
||||
fail("没有 evaluateJavascript,取不到页面数据")
|
||||
|
||||
if "DRAIN_JS" in web and "INSTALL_JS" in web:
|
||||
ok("注入脚本 + 取数脚本都在")
|
||||
else:
|
||||
fail("缺少注入或取数脚本")
|
||||
|
||||
# 3. 取数必须"取走"而不是"只读"(否则每轮询一次重复报一次)
|
||||
if "window.$QUEUE = [];" in pick or "window.__doubaoPicks = [];" in pick:
|
||||
ok("取数时清空队列(不会重复上报)")
|
||||
else:
|
||||
fail("取数没清空队列 —— 用户点一下会被重复上报十几次")
|
||||
|
||||
# 4. 危险开关仍然关着
|
||||
for name, pat in (
|
||||
("allowFileAccess", r"allowFileAccess\s*=\s*false"),
|
||||
("allowContentAccess", r"allowContentAccess\s*=\s*false"),
|
||||
("混合内容", r"MIXED_CONTENT_NEVER_ALLOW"),
|
||||
):
|
||||
if re.search(pat, web):
|
||||
ok(f"{name} 关着")
|
||||
else:
|
||||
fail(f"{name} 没关")
|
||||
|
||||
print("\n=== 二、点选脚本本身的正确性 ===")
|
||||
|
||||
# 点选要拦掉默认行为(用户是"选中"不是"跳转")
|
||||
if "preventDefault" in pick and "stopPropagation" in pick:
|
||||
ok("拦掉默认行为(点链接不会跳走)")
|
||||
else:
|
||||
fail("没拦默认行为 —— 点个链接页面就跳走了,用户来不及看选中提示")
|
||||
|
||||
# 捕获阶段
|
||||
if re.search(r"addEventListener\('click',\s*onClick,\s*true\)", pick):
|
||||
ok("用捕获阶段监听(抢在页面自己的 handler 前面)")
|
||||
else:
|
||||
fail("不是捕获阶段 —— 页面自己的点击脚本会先跑")
|
||||
|
||||
# 防重注入
|
||||
if "__doubaoPickInstalled" in pick:
|
||||
ok("防重注入(每次加载完补注不会叠监听)")
|
||||
else:
|
||||
fail("没有防重 —— 多次注入会叠出好几层监听,一次点击写多条重复记录")
|
||||
|
||||
# 字段要够模型用
|
||||
for field in ("text", "href", "tag", "path", "title", "url"):
|
||||
if f"{field}:" in pick:
|
||||
ok(f"记录了 {field}")
|
||||
break
|
||||
else:
|
||||
fail("记录里没有可用字段")
|
||||
|
||||
print("\n=== 三、用户体验 ===")
|
||||
|
||||
# 点错了要能反悔
|
||||
if "PickedElement" in web and "发给 AI" in web:
|
||||
ok("有「发给 AI / 取消」确认条(点错了不会白跑一轮模型)")
|
||||
else:
|
||||
fail("点了就直接发 —— 点错会触发一整轮模型调用,又慢又费 token")
|
||||
|
||||
# 发给 AI 要走普通发消息那条路
|
||||
if "sendExternal" in vm and "runChat" in vm:
|
||||
ok("走普通发消息那条路(会显示在对话里、能重新生成)")
|
||||
else:
|
||||
fail("不是我预想的路径")
|
||||
|
||||
if "sendExternal" in root:
|
||||
ok("AppRoot 把点选结果接到 ViewModel")
|
||||
else:
|
||||
fail("点选结果没接上")
|
||||
|
||||
print("\n=== 四、链接能被打开(用户之前问过的)===")
|
||||
|
||||
if "extractUrls" in md:
|
||||
ok("有 extractUrls(从 markdown 抽链接)")
|
||||
else:
|
||||
fail("没有 extractUrls")
|
||||
|
||||
if "打开网页" in chat:
|
||||
ok("消息下方有「打开网页」按钮(用户要求的『单独开个按钮』)")
|
||||
else:
|
||||
fail("没有「打开网页」按钮")
|
||||
|
||||
if "withLink" in md and "LinkAnnotation" in md:
|
||||
ok("正文里的链接也绑了点击(不只是按钮)")
|
||||
else:
|
||||
fail("正文链接没绑点击")
|
||||
|
||||
if "ExperimentalTextApi" in md:
|
||||
ok("withLink 做了 @OptIn(它是实验 API,不 opt-in 报的是 Unresolved reference)")
|
||||
else:
|
||||
fail("withLink 没有 @OptIn(ExperimentalTextApi) —— 编译会报 Unresolved reference")
|
||||
|
||||
print("\n=== 五、按项目约定:列表用全屏界面而不是弹窗 ===")
|
||||
|
||||
if "UrlListScreen" in web:
|
||||
ok("链接列表是独立全屏界面")
|
||||
else:
|
||||
fail("没有 UrlListScreen")
|
||||
|
||||
if "UrlChooser" in web or "UrlChooser" in root:
|
||||
fail("还在用弹窗版的 UrlChooser —— 项目约定是列表要全屏")
|
||||
else:
|
||||
ok("没有残留的弹窗版 UrlChooser")
|
||||
|
||||
print("\n=== 六、接口覆盖「选了 / 写了 / 关闭」三类 ===")
|
||||
|
||||
# 用户明确要求:接口要让 AI 知道用户"选择了什么、写了什么",还能"关闭这个网页"。
|
||||
|
||||
# 1. 写了什么(表单输入)
|
||||
if "addEventListener('input'" in pick or 'addEventListener("input"' in pick:
|
||||
ok("监听 input(能知道用户**写了什么**)")
|
||||
else:
|
||||
fail("没监听 input —— AI 拿不到用户填的内容")
|
||||
|
||||
if "kind: 'input'" in pick and "kind: 'click'" in pick:
|
||||
ok("两类记录分开了(click 是'选了'、input 是'写了')")
|
||||
else:
|
||||
fail("记录没有区分类型")
|
||||
|
||||
if "checkbox" in pick or "radio" in pick:
|
||||
ok("勾选框/单选框也认(值是'已勾选/未勾选')")
|
||||
else:
|
||||
fail("勾选框没处理")
|
||||
|
||||
# 输入事件不能拦(拦了会破坏页面的自动补全、字数统计)
|
||||
if re.search(r"addEventListener\('input',\s*onInput,\s*false\)", pick):
|
||||
ok("input 用冒泡阶段旁听,不拦页面自己的处理")
|
||||
else:
|
||||
fail("input 的处理方式不对(可能是捕获+拦截,会破坏页面功能)")
|
||||
|
||||
# 防抖:连续打字不能塞一堆重复记录
|
||||
if "setTimeout" in pick and "600" in pick:
|
||||
ok("输入做了防抖(打十个字不会塞十条记录)")
|
||||
else:
|
||||
fail("输入没防抖 —— 报告会变得没法看")
|
||||
|
||||
# 2. 多条累积
|
||||
if "buildReport" in pick:
|
||||
ok("有 buildReport(多条汇总成一段给模型的话)")
|
||||
else:
|
||||
fail("没有汇总函数 —— 用户选五处就得发五次")
|
||||
|
||||
if "mutableStateListOf<PickedElement>" in web:
|
||||
ok("界面侧用列表累积多条")
|
||||
else:
|
||||
fail("界面侧只存了一条")
|
||||
|
||||
# 3. 关闭这个网页
|
||||
if "closeWebPage" in vm:
|
||||
ok("有 closeWebPage(接口能关闭这个网页)")
|
||||
else:
|
||||
fail("没有关闭网页的入口")
|
||||
|
||||
if "onClose = { vm.closeWebPage() }" in root:
|
||||
ok("网页界面接上了关闭")
|
||||
else:
|
||||
fail("关闭没接上")
|
||||
|
||||
# 4. 我们自己的 HTML 页面(show_ui)那边能关闭 + 回传
|
||||
#
|
||||
# ★ 只查 "Doubao.close"(不带括号):那个字符串在 py 源码里是
|
||||
# `"\n 也可以用 Doubao.close() 关掉界面。"` 的形式,
|
||||
# `)` 后面紧跟的是 `\`,查带括号的完整串会因为转义而对不上。
|
||||
# (这是这个验证脚本第三次自己绊自己了,记一笔。)
|
||||
show_ui_src = open(os.path.join(WS, "tools", "show_ui.py"), encoding="utf-8").read()
|
||||
if "Doubao.close" in show_ui_src and "Doubao.pick" in show_ui_src:
|
||||
ok("show_ui 的说明里告诉模型能关界面、能回传(Doubao.close / Doubao.pick)")
|
||||
else:
|
||||
fail("show_ui 的说明里没告诉模型能关界面或回传")
|
||||
|
||||
print()
|
||||
if fails:
|
||||
print(f"❌ {len(fails)} 项不合格:")
|
||||
for m in fails:
|
||||
print(" -", m)
|
||||
sys.exit(1)
|
||||
print("✅ 全部通过")
|
||||
Reference in New Issue
Block a user