Selaa lähdekoodia

内容: 评论通知相关收尾 - opencode_client 过滤纯空白回复; listener 转发消息到 opencode 会话生成回复; vbs 注释编码修正

Sisyphus Agent 2 viikkoa sitten
vanhempi
sitoutus
d01181d28a

+ 2 - 2
运营文案/opencode_client.py

@@ -174,7 +174,7 @@ def wait_for_reply(session_id: str, timeout: float = 180,
         last = msgs[-1]
         if last.get("info", {}).get("role") == "assistant":
             lt = [p.get("text", "") for p in last.get("parts", [])
-                  if p.get("type") == "text" and p.get("text")]
+                  if p.get("type") == "text" and (p.get("text") or "").strip()]
             if lt:
                 return lt[-1]
 
@@ -184,7 +184,7 @@ def wait_for_reply(session_id: str, timeout: float = 180,
                 role = m.get("info", {}).get("role")
                 if role == "assistant":
                     txts = [p.get("text", "") for p in m.get("parts", [])
-                            if p.get("type") == "text" and p.get("text")]
+                            if p.get("type") == "text" and (p.get("text") or "").strip()]
                     if txts:
                         return txts[-1]
     raise TimeoutError(f"等待 opencode 回复超时({timeout}s)")

+ 24 - 5
运营文案/xhs_bot_listener.py

@@ -35,6 +35,9 @@ sys.stderr = sys.stdout
 
 import requests
 
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+import opencode_client  # opencode 4090 HTTP 会话交互
+
 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
 OPERATION_DIR = SCRIPT_DIR  # 本脚本位于 运营文案/ 目录,配置文件也在这
 CFC_ROOT = os.path.dirname(OPERATION_DIR)  # C:\code\cfc
@@ -266,17 +269,33 @@ def on_message_receive(data):
             try:
                 feishu = FeishuChat(_load_config())
                 cmd = _mention_text(msg).lower()
-                # 去掉可能的 at 前缀后解析指令
+
+                # 需要执行采集脚本的指令 -> 本地路由
+                # (opencode 会话为只读,无法代为运行 CDP 采集,故保留本地执行)
                 if any(k in cmd for k in ("看数据", "日报", "数据", "stats", "status")):
                     _handle_data_command(msg.message_id, feishu)
-                elif any(k in cmd for k in ("help", "帮助", "?")):
+                    return
+                if any(k in cmd for k in ("help", "帮助", "?")):
                     feishu.reply_text(msg.message_id, HELP_TEXT)
-                elif cmd:
-                    feishu.reply_text(msg.message_id, f"未识别指令: {cmd}\n发送 help 查看可用命令。")
+                    return
+
+                # 其余消息 -> 转发到该群的 opencode 会话处理(只读分析 + 生成回复)
+                user_text = _mention_text(msg)
+                _log(f"[opencode] chat={chat_id} 转发到 opencode 会话处理")
+                reply_text = opencode_client.ask(chat_id, user_text)
+                _log(f"[opencode] 得到回复: {reply_text[:120]!r}")
+                if reply_text and reply_text.strip():
+                    feishu.reply_text(msg.message_id, reply_text)
                 else:
-                    feishu.reply_text(msg.message_id, "你好!@我并发送「看数据」即可查看最新小红书数据。")
+                    feishu.reply_text(msg.message_id, "(opencode 未返回有效回复,请稍后再试)")
             except Exception as e:
                 _log(f"[worker] {e}")
+                try:
+                    feishu.reply_text(
+                        msg.message_id, f"❌ 处理出错:{e}\n请稍后再试,或发送「help」查看可用命令。"
+                    )
+                except Exception:
+                    pass
 
         threading.Thread(target=_worker, daemon=True).start()
     except Exception as e:

+ 3 - 3
运营文案/xhs_bot_listener.vbs

@@ -1,4 +1,4 @@
-' 灏忕孩涔︽暟鎹�満鍣ㄤ汉鐩戝惉鍚�姩鍣�紙闅愯棌绐楀彛锛�
-' 鐢� Windows 璁″垝浠诲姟鍦ㄧ櫥褰曟椂璋冪敤锛岄伩鍏嶉粦绐楀彛甯搁┗銆�
+' 小红书数据机器人监听启动器(隐藏窗口)
+' 由 Windows 计划任务在登录时调用,避免黑窗口常驻。
 Set WshShell = CreateObject("WScript.Shell")
-WshShell.Run """C:\Users\Administrator\AppData\Local\Programs\Python\Python312\python.exe"" ""C:\code\cfc\杩愯惀鏂囨�\xhs_bot_listener.py""", 0, False
+WshShell.Run """C:\Users\Administrator\AppData\Local\Programs\Python\Python312\python.exe"" ""C:\code\cfc\运营文案\xhs_bot_listener.py""", 0, False