"""让 AI 主动往**当前这次会话**里发一条消息(像用户说的一样进历史)。 ## 和"正常回答"的区别 正常情况下模型说的话就是它这一轮的回答。但有些场景它需要**单独推一条**: · 角色扮演:它想主动找用户说话("在干嘛呢"),而不是等用户先开口 · 长任务:它跑完一段想先把中间结果贴进对话,再继续 · 用户在它写的界面上点了东西 → 那个动作该在对话里留下一条记录 ## ★ 必须防死循环(这个工具最危险的地方) 模型发一条消息 → 内核顺手跑一轮回复 → 那一轮里模型又调这个工具 → 无限套娃。 一个下午就能把 API 额度烧光,而且用户看到满屏自己没发过的消息。 所以有三道闸: 1. **深度上限**:回复里再发消息,最多套 [MAX_DEPTH] 层 (用 `tools_runtime` 的调用上下文传深度,跨线程也能带过去) 2. **每会话条数上限**:一次会话里最多 [MAX_PER_CONV] 条 3. **默认不触发回复**:`reply` 默认 false。模型想接着说就显式写 `reply=true` ★ 为什么默认不触发回复:大多数用法是"贴一条就完了"(记录、通知)。 默认触发的话,一句 `Doubao.sendToAi` 就会连锁跑起模型 —— 用户会觉得"它怎么自己说起来了",而且很容易撞上额度。 """ 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, conversation_id # noqa: E402 # 回复里再发消息,最多套几层 MAX_DEPTH = 2 # 一次会话最多让 AI 主动发几条(超了就拒绝并说明) MAX_PER_CONV = 20 TOOL = { "name": "send_message", "description": ( "往**当前这次会话**里发一条消息(会像用户说的话一样进历史、显示在对话里)。" "\n\n什么时候用:" "\n· 角色扮演里你想**主动**找用户说话(不用等他先开口)" "\n· 你跑完一段想先把结果贴进对话再继续" "\n· 用户在界面上做了个动作,这件事该在对话里留个记录" "\n\n什么时候别用:" "\n· 你只是想回答用户 —— **直接回答就行**,这工具是给'额外推一条'用的" "\n· 别用它在一次回答里刷好几条消息(那会把对话刷屏)" "\n\nreply=true 会让内核**顺手替你跑一轮回复**(默认 false)。" "\n★ 回复里不能再调这个工具(最多套两层),所以别指望用它递归。" ), "parameters": { "type": "object", "properties": { "text": { "type": "string", "description": "要发的内容", }, "reply": { "type": "boolean", "description": "true 表示发完之后顺手跑一轮你的回复;默认 false(只发这一条)", }, "role": { "type": "string", "enum": ["user", "assistant"], "description": ( "这条消息算谁的。默认 user(当作用户说的 —— 适合'用户点了某个按钮'这类);" "想当成你自己说过的就传 assistant" ), }, }, "required": ["text"], }, } def run(args: dict) -> dict: text = str(args.get("text") or "").strip() if not text: raise ValueError("text 不能为空") conv = conversation_id() conv_note = "" if not conv: # 拿不到对话 id 时**不能假装成功** —— 那样消息会写到默认会话里去, # 用户在一个会话里操作、消息却出现在另一个会话。 raise RuntimeError("拿不到当前对话 id,无法发送消息") role = str(args.get("role") or "user").strip().lower() if role not in ("user", "assistant"): role = "user" res = call_device( { "action": "session_message", "conversationId": conv, "text": text, "role": role, "reply": bool(args.get("reply")), "depth": _depth() + 1, } ) if res.get("unavailable"): raise RuntimeError(res.get("error") or "当前环境没有界面能力") if res.get("error"): raise RuntimeError(str(res["error"])) out = { "sent": True, "conversationId": conv, "role": role, "chars": len(text), } if res.get("replied"): out["replied"] = True out["note"] = "消息已发进对话,并且你已经顺手回了一轮。别重复同一件事。" else: out["note"] = ( "消息已发进对话,显示在聊天里了。" "你没有触发回复 —— 如果还想说什么,直接在**你这一轮的回答里**说就行," "别再调一次这个工具。" ) if res.get("replyStarted"): out["replyStarted"] = True out["note"] = "消息已发进对话,回复正在后台生成(用户会看到它自己冒出来)。" if conv_note: out["note"] = out["note"] + conv_note return out def _depth() -> int: """ 当前嵌套深度。 ★ 从 `tools_runtime` 的调用上下文里读 —— 它在 `run_python_tool` 里被 显式搬到工作线程(那个跨线程的坑踩过:threading.local 不跨线程继承)。 所以这里读得到。 """ try: import tools_runtime # noqa: PLC0415 ctx = tools_runtime.current_context() or {} return int(ctx.get("sendDepth") or 0) except Exception: # noqa: BLE001 return 0 if __name__ == "__main__": serve(TOOL, run)