"""读手机上的通知。 ## 这是三个"动手机"的工具之一(另两个:开 App、调音量) ## ★ 隐私:读到的内容可能很敏感 通知里有验证码、银行短信、私信 —— 这是**整个 App 里最隐私的一类数据**。 所以: · 只在**内存**里留最近 200 条,不落盘(进程一死就没了)。 写磁盘意味着卸载前一直躺着,也可能被别的工具读到。 · 用户要在系统设置里**手动开"通知使用权"**才能读到。 Android 没有这个权限的运行时申请弹窗 —— 这是唯一合法的路。 · 读之前一定先查权限,没开就**明确报错**(见下)。 ## ★ 权限没开时返回错误,不是空列表 返回空列表的话模型会说"你最近没有通知" —— 那是**错的** (明明有好几条,只是我们读不到)。用户会被误导, 而且他不会想到"原来是要去开个权限"。 所以这里返回一个带 `hint` 的错误,模型就能照着告诉用户怎么开。 """ import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from _protocol import serve # noqa: E402 from _ui_bridge import call_device # noqa: E402 TOOL = { "name": "notifications", "description": ( "读手机上的通知,或者**主动发一条通知**给用户。" "\n\n读:" "\n· action=list(默认)—— 列出最近的通知。" "query 可以按关键词过滤(标题/正文/App 名),limit 默认 20、最多 200。" "\n· action=alive —— 只看**还挂在通知栏上**的(已经被用户划掉的不算)" "\n· action=access —— 查有没有「通知使用权」" "\n· action=open_settings —— 跳到系统的通知使用权设置页,让用户去开" "\n· action=clear —— 把已经读到的记录清掉(内存里那份)" "\n\n发:" "\n· action=notify —— 发一条通知给用户。传 title(标题)和 text(正文)。" "\n ★ 这是**会打扰用户**的动作(他手机会响/震)——只在真的值得提醒时用:" "\n 定时提醒、长任务跑完了、用户让你过一会儿告诉他。" "\n 别用它来重复你已经在对话里说过的话。" "\n 点了通知会回到本 App。" "\n\n★ 读通知之前用户必须先手动开「通知使用权」(安卓不给运行时申请)。" "\n 没开的时候本工具会返回错误 + 怎么开的提示 —— 请照着念给用户," "\n 别自己编步骤。**发通知不需要这个权限。**" ), "parameters": { "type": "object", "properties": { "action": { "type": "string", "enum": ["list", "alive", "access", "open_settings", "clear", "notify"], "description": "要做的事,默认 list", }, "query": {"type": "string", "description": "按关键词过滤(可选)"}, "limit": {"type": "number", "description": "最多几条,默认 20,最多 200"}, "title": {"type": "string", "description": "action=notify 时的通知标题"}, "text": {"type": "string", "description": "action=notify 时的通知正文"}, }, }, } def run(args: dict) -> dict: action = str(args.get("action") or "list").strip().lower() # ★ 发通知放在最前面:它**不需要**通知使用权(那权限只管读), # 所以不能被下面读路径的权限检查拦住。 if action == "notify": title = str(args.get("title") or "").strip() text = str(args.get("text") or "").strip() if not title and not text: raise ValueError("notify 至少要给 title 或 text") res = call_device({ "action": "notification_post", "title": title, "text": text, "autoCancel": True, }) if res.get("unavailable"): raise RuntimeError(res.get("error") or "当前环境没有通知能力") if res.get("error"): raise RuntimeError(str(res["error"])) return { "sent": True, "title": res.get("title") or title, "chars": res.get("chars", len(text)), "note": ( "通知已经发出去了,用户手机会响/震一下。" "点它会回到本 App。" "**别为了同一件事再发一条** —— 用户会烦。" ), } if action == "open_settings": res = call_device({"action": "notification_settings"}) if res.get("unavailable"): raise RuntimeError(res.get("error") or "当前环境没有界面能力") if res.get("error"): raise RuntimeError(str(res["error"])) return { "opened": True, "note": ( "已经跳到系统的「通知使用权」设置页。" "请让用户在那里找到本 App 并打开开关 —— 开了之后我才能读到通知。" ), } if action == "access": res = call_device({"action": "notification_access"}) if res.get("unavailable"): raise RuntimeError(res.get("error") or "当前环境没有界面能力") granted = bool(res.get("granted")) out = {"granted": granted, "kept": res.get("kept", 0)} if not granted: out["note"] = ( "还没开「通知使用权」,读不到任何通知。" "让用户去 设置 → 通知 → 通知使用权 里打开本 App(我也可以调 " "action=open_settings 直接跳过去)。" ) return out if action == "clear": res = call_device({"action": "notification_clear"}) if res.get("error"): raise RuntimeError(str(res["error"])) return {"cleared": True} # list / alive payload = { "action": "notifications", "query": str(args.get("query") or ""), "onlyAlive": action == "alive", } try: limit = int(args.get("limit") or 20) except (TypeError, ValueError): limit = 20 payload["limit"] = max(1, min(200, limit)) res = call_device(payload) if res.get("unavailable"): raise RuntimeError(res.get("error") or "当前环境没有界面能力") # ★ 权限没开:把错误和"怎么开"一起抛回去。 # 抛异常而不是返回空列表 —— 空列表会让模型说"你没有通知",那是错的。 if res.get("error"): hint = res.get("hint") or "" raise RuntimeError(f"{res['error']}。{hint}".strip()) items = res.get("notifications") or [] # 顺手整理成模型好读的形状:把时间戳变成"几分钟前" simplified = [] import time now_ms = int(time.time() * 1000) for n in items: at = int(n.get("at") or 0) ago = "" if at: secs = max(0, (now_ms - at) // 1000) if secs < 60: ago = "刚刚" elif secs < 3600: ago = f"{secs // 60} 分钟前" elif secs < 86400: ago = f"{secs // 3600} 小时前" else: ago = f"{secs // 86400} 天前" simplified.append({ "app": n.get("app") or n.get("package") or "", "title": n.get("title") or "", "text": n.get("text") or "", "ago": ago, # 已经被划掉的也留着 —— 用户问"刚才那条验证码呢"时它早就没了 "gone": bool(n.get("removed")), }) return { "count": len(simplified), "only_alive": action == "alive", "notifications": simplified, } if __name__ == "__main__": serve(TOOL, run)