codebee 0.1.101 → 0.1.103

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE +21 -21
  3. package/README.md +3 -6
  4. package/app/.playwright-cli/console-2026-09-23T08-26-03-635Z.log +88 -0
  5. package/app/.playwright-cli/page-2026-09-23T08-26-03-845Z.yml +126 -0
  6. package/app/.playwright-cli/page-2026-09-23T08-29-44-203Z.png +0 -0
  7. package/app/.playwright-cli/page-2026-09-23T08-35-42-046Z.yml +210 -0
  8. package/app/.playwright-cli/page-2026-09-23T08-36-52-858Z.png +0 -0
  9. package/app/.playwright-cli/page-2026-09-23T08-37-33-938Z.png +0 -0
  10. package/app/.playwright-cli/run-detail-after.png +0 -0
  11. package/app/core/acceptance.py +100 -100
  12. package/app/core/acp_client.py +457 -426
  13. package/app/core/aiflavor.py +197 -197
  14. package/app/core/analytics.py +38 -38
  15. package/app/core/artifacts.py +148 -148
  16. package/app/core/assets.py +110 -110
  17. package/app/core/attachments.py +623 -623
  18. package/app/core/audit.py +30 -30
  19. package/app/core/backend_profiles.py +188 -188
  20. package/app/core/backup.py +470 -470
  21. package/app/core/beekeeper.py +183 -183
  22. package/app/core/benchstore.py +102 -102
  23. package/app/core/board.py +21 -17
  24. package/app/core/bookmeta.py +1036 -1036
  25. package/app/core/bookmeta_catalog.py +180 -180
  26. package/app/core/branching.py +137 -137
  27. package/app/core/builtin_agent.py +13 -4
  28. package/app/core/capability.py +144 -144
  29. package/app/core/catalog.py +28 -6
  30. package/app/core/chaptersafe.py +32 -32
  31. package/app/core/checkpoints.py +117 -31
  32. package/app/core/cleanup.py +303 -303
  33. package/app/core/command_auth.py +302 -302
  34. package/app/core/compaction.py +201 -201
  35. package/app/core/contracts.py +255 -255
  36. package/app/core/covergen.py +356 -356
  37. package/app/core/defectretro.py +43 -43
  38. package/app/core/diagnostics.py +114 -114
  39. package/app/core/dispatch.py +1 -1
  40. package/app/core/dispatch_log.py +128 -128
  41. package/app/core/env_scrub.py +85 -85
  42. package/app/core/errorlog.py +137 -137
  43. package/app/core/eval_matrix.py +91 -82
  44. package/app/core/evaluation.py +174 -174
  45. package/app/core/failure_notify.py +290 -290
  46. package/app/core/flow_graph.py +33 -33
  47. package/app/core/goal_service.py +167 -167
  48. package/app/core/health.py +389 -389
  49. package/app/core/history.py +38 -38
  50. package/app/core/hooks.py +162 -162
  51. package/app/core/knowledge_pipeline.py +86 -86
  52. package/app/core/manager.py +2385 -2385
  53. package/app/core/mcp_client.py +7 -2
  54. package/app/core/mocks.py +64 -64
  55. package/app/core/notify.py +201 -201
  56. package/app/core/novel_quality.py +181 -181
  57. package/app/core/openapi.py +217 -209
  58. package/app/core/operations.py +228 -228
  59. package/app/core/paihang.py +145 -145
  60. package/app/core/pipeline.py +6 -3
  61. package/app/core/policy.py +9 -0
  62. package/app/core/portguard.py +111 -111
  63. package/app/core/presence.py +77 -77
  64. package/app/core/preview.py +293 -293
  65. package/app/core/project_memory.py +206 -206
  66. package/app/core/prompt_guard.py +78 -78
  67. package/app/core/publish/__init__.py +7 -7
  68. package/app/core/publish/auto.py +659 -562
  69. package/app/core/publish/chaprows.py +138 -138
  70. package/app/core/publish/fanqie.py +16 -10
  71. package/app/core/publish/flow.py +29 -1
  72. package/app/core/publish/flows-fanqie-calibrated.json +442 -442
  73. package/app/core/publish/manager.py +1185 -1177
  74. package/app/core/publish/qimao.py +193 -193
  75. package/app/core/publish/volumes.py +108 -108
  76. package/app/core/publish/ws.py +139 -139
  77. package/app/core/quality_gate.py +201 -201
  78. package/app/core/redact.py +38 -38
  79. package/app/core/repeat_guard.py +129 -129
  80. package/app/core/retrieval.py +137 -137
  81. package/app/core/sandbox_runtime.py +95 -0
  82. package/app/core/selfupdate.py +384 -384
  83. package/app/core/session_log.py +161 -161
  84. package/app/core/sessions.py +312 -312
  85. package/app/core/settings.py +215 -215
  86. package/app/core/settings_schema.py +282 -282
  87. package/app/core/share_page.py +114 -114
  88. package/app/core/skill_scan.py +139 -139
  89. package/app/core/skillpacks/market/character-bible.md +66 -66
  90. package/app/core/skillpacks/market/code-risk-checklist.md +58 -58
  91. package/app/core/skillpacks/market/git-workflow.md +57 -57
  92. package/app/core/skillpacks/market/release-notes.md +72 -72
  93. package/app/core/skillpacks/market/weekly-report.md +71 -71
  94. package/app/core/skillpacks/market/worldview-consistency.md +70 -70
  95. package/app/core/step_runner.py +132 -132
  96. package/app/core/store.py +13 -3
  97. package/app/core/story_tracking.py +275 -275
  98. package/app/core/task_compile.py +199 -199
  99. package/app/core/telemetry.py +292 -292
  100. package/app/core/tlsctx.py +64 -64
  101. package/app/core/token_meter.py +147 -147
  102. package/app/core/tool_outputs.py +107 -107
  103. package/app/core/tracing.py +207 -207
  104. package/app/core/upstream.py +41 -41
  105. package/app/core/usage.py +1038 -1038
  106. package/app/core/volumes.py +389 -389
  107. package/app/core/webhooks.py +89 -89
  108. package/app/core/wxdigest.py +865 -865
  109. package/app/core/zentao.py +2692 -2692
  110. package/app/main.py +1 -0
  111. package/app/mcp_server.py +244 -244
  112. package/app/output/_createbook_body.json +1 -0
  113. package/app/output/_probe_fq_click.py +50 -0
  114. package/app/output/_probe_fq_form.py +42 -0
  115. package/app/output/_probe_fq_modal.py +55 -0
  116. package/app/output/_probe_fq_points.py +71 -0
  117. package/app/output/_probe_fq_submit.py +34 -0
  118. package/app/output/_probe_fq_watch.py +62 -0
  119. package/app/output/workbench-clean-preview.jpg +0 -0
  120. package/app/output/workbench-reference-render.png +0 -0
  121. package/app/output/workbench-shell.png +0 -0
  122. package/app/pick_dialog.py +110 -110
  123. package/app/ui/app.js +165 -29
  124. package/app/ui/artdiff.js +74 -74
  125. package/app/ui/hive3d.js +491 -223
  126. package/app/ui/i18n.js +18 -1
  127. package/app/ui/icons/bee.svg +79 -79
  128. package/app/ui/index.html +24 -4
  129. package/app/ui/manifest.json +16 -16
  130. package/app/ui/qrcode.js +2297 -2297
  131. package/app/ui/style.css +548 -18
  132. package/bin/tutti.js +147 -147
  133. package/package.json +1 -1
  134. package/tools/wx_reader.py +261 -261
@@ -1,261 +1,261 @@
1
- # -*- coding: utf-8 -*-
2
- """微信读取 sidecar:由 64 位 Python ≥3.9(配 wechatauto-replica)运行,
3
- 把本机微信 4.x 群消息增量拉成本仓库已有的「导出 txt」格式。
4
-
5
- 为什么是独立进程:wechatauto 依赖链(winsdk 等)需要 64 位 Python ≥3.9,
6
- 而 CodeBee 服务钉在 3.8-32。sidecar 通过文件与主程序解耦——拉到的新消息
7
- 按「2024-01-01 12:34:56 昵称」行格式追加进监控文件夹,wxdigest 现有的
8
- 解析/增量游标/摘要/桌宠提醒整条链路零改动复用。
9
-
10
- 用法(两个模式,互斥):
11
- 列群(供前端选择):
12
- python wx_reader.py --list-groups --result OUT.json
13
- 拉增量(每次扫描前调用):
14
- python wx_reader.py --pull --out DIR --state STATE.json \
15
- --groups-file GROUPS.json --result OUT.json
16
- GROUPS.json = [{"username": "...@chatroom", "name": "群名"}, ...]
17
-
18
- 只读保证:只调用 get_groups/get_new_messages/get_group_members/get_self_info,
19
- 不调用任何发送/删除接口。
20
- 结果 JSON 写 --result 文件(stdout 被库日志污染,不作为契约)。
21
- 语法保持 3.8 兼容(测试解释器要 import 本文件的纯函数)。
22
- """
23
- from __future__ import annotations
24
-
25
- import argparse
26
- import json
27
- import os
28
- import re
29
- import sys
30
- import time
31
-
32
- # 群消息 content 自带「<wxid>: 前缀」,剥掉后用成员表翻译成昵称
33
- _PREFIX_RE = re.compile(r"^([A-Za-z0-9_+@\-.]{3,}):\s*\n?")
34
- _ILLEGAL_FN = re.compile(r'[\\/:*?"<>|]')
35
-
36
- SELF_NAME = "我"
37
-
38
-
39
- def sanitize_filename(name):
40
- """群名 → 合法文件名(Windows 非法字符换下划线;空名回落 username)。"""
41
- out = _ILLEGAL_FN.sub("_", str(name or "").strip())
42
- return out[:80] or "unnamed"
43
-
44
-
45
- def strip_sender_prefix(content, sender_username):
46
- """群消息正文剥掉自带的「wxid: 」前缀(前缀与发送者一致才可信地剥)。"""
47
- text = str(content or "")
48
- m = _PREFIX_RE.match(text)
49
- if m and (not sender_username or m.group(1) == sender_username):
50
- return text[m.end():].strip()
51
- return text.strip()
52
-
53
-
54
- def member_map(members):
55
- """get_group_members 结果 → {username: 显示名}。备注优先(微信 UI 同款)。"""
56
- out = {}
57
- for m in members or []:
58
- if not isinstance(m, dict):
59
- continue
60
- uid = m.get("username") or ""
61
- if not uid:
62
- continue
63
- out[uid] = m.get("remark") or m.get("nick_name") or uid
64
- return out
65
-
66
-
67
- def format_line(ts_epoch, sender_name, text):
68
- """一条消息 → 导出格式行(时间戳行与正文之间不留空行,与现有解析器对齐)。"""
69
- stamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts_epoch or 0))
70
- return "%s %s\n%s\n" % (stamp, sender_name or "?", str(text or "").strip())
71
-
72
-
73
- def type_placeholder(mtype):
74
- """非文本消息 → 占位符(摘要时让模型知道有图/语音存在)。None=跳过不入正文。"""
75
- t = str(mtype or "")
76
- if "文本" in t:
77
- return None # 文本走正文,不是占位符
78
- for key, ph in (("图片", "[图片]"), ("语音", "[语音]"), ("视频", "[视频]"),
79
- ("文件", "[文件]"), ("链接", "[链接]"), ("名片", "[名片]"),
80
- ("位置", "[位置]"), ("动画表情", "[表情]"), ("系统", None)):
81
- if key in t:
82
- return ph
83
- return "[%s]" % t if t and len(t) <= 8 else None
84
-
85
-
86
- def _quiet_lib_logs():
87
- try:
88
- import logging
89
- logging.getLogger("wechatauto").setLevel(logging.WARNING)
90
- except Exception:
91
- pass
92
-
93
-
94
- def _write_result(path, obj):
95
- tmp = str(path) + ".tmp"
96
- with open(tmp, "w", encoding="utf-8") as f:
97
- json.dump(obj, f, ensure_ascii=False)
98
- os.replace(tmp, path)
99
-
100
-
101
- def _load_json(path, default):
102
- try:
103
- with open(path, "r", encoding="utf-8") as f:
104
- return json.load(f)
105
- except Exception:
106
- return default
107
-
108
-
109
- def load_state(path):
110
- return _load_json(path, {}) or {}
111
-
112
-
113
- def save_state(path, state):
114
- tmp = str(path) + ".tmp"
115
- with open(tmp, "w", encoding="utf-8") as f:
116
- json.dump(state, f, ensure_ascii=False)
117
- os.replace(tmp, path)
118
-
119
-
120
- def cmd_list_groups(result_path):
121
- _quiet_lib_logs()
122
- import wechatauto
123
- db = wechatauto.WeChatDB()
124
- groups, seen = [], set()
125
- for g in db.get_groups():
126
- if isinstance(g, dict) and g.get("username"):
127
- groups.append({"username": g["username"],
128
- "name": g.get("name") or g["username"],
129
- "member_count": str(g.get("member_count") or "")})
130
- seen.add(g["username"])
131
- # 新群兜底(2026-09-21 用户实测「缺一个新建的群」):刚建的群可能还没
132
- # 落进 chat_room/contact,但只要有过消息就在会话列表里——合并补进,
133
- # 昵称未同步时先显示 wxid,微信同步后「刷新群列表」即见真名。
134
- try:
135
- for s in db.get_sessions(200):
136
- u = str((s or {}).get("username") or "")
137
- if u.endswith("@chatroom") and u not in seen:
138
- groups.append({"username": u,
139
- "name": db.get_nickname(u) or u,
140
- "member_count": ""})
141
- seen.add(u)
142
- except Exception:
143
- pass # 会话库不可用不影响主清单
144
- _write_result(result_path, {"ok": True, "groups": groups, "count": len(groups)})
145
-
146
-
147
- def cmd_pull(out_dir, state_path, groups_file, result_path):
148
- _quiet_lib_logs()
149
- import wechatauto
150
- sel = _load_json(groups_file, [])
151
- sel = [g for g in (sel or []) if isinstance(g, dict) and g.get("username")]
152
- state = load_state(state_path)
153
- os.makedirs(out_dir, exist_ok=True)
154
- db = wechatauto.WeChatDB()
155
- try:
156
- self_wxid = db.wxid
157
- except Exception:
158
- self_wxid = ""
159
- pulled, errors = {}, []
160
- for g in sel:
161
- username, name = g["username"], g.get("name") or g["username"]
162
- since = 0
163
- try:
164
- since = int((state.get(username) or {}).get("since_seq") or 0)
165
- except (TypeError, ValueError):
166
- since = 0
167
- try:
168
- msgs = db.get_new_messages(username, since_seq=since, limit=500)
169
- except Exception as e:
170
- errors.append("%s: %s" % (name, e))
171
- continue
172
- if not msgs:
173
- pulled[username] = 0
174
- continue
175
- members = {}
176
- try:
177
- members = member_map(db.get_group_members(username))
178
- except Exception:
179
- members = {}
180
- # 昵称缓存:退群成员不在成员表里,靠 get_nickname 兜底一次后永久缓存
181
- gstate = state.get(username) or {}
182
- names = dict(gstate.get("names") or {})
183
-
184
- def resolve(uid):
185
- if not uid:
186
- return "?"
187
- if self_wxid and uid == self_wxid:
188
- return SELF_NAME
189
- if uid in members:
190
- return members[uid]
191
- if uid in names:
192
- return names[uid]
193
- try:
194
- nick = str(db.get_nickname(uid) or "").strip()
195
- except Exception:
196
- nick = ""
197
- name2 = nick or uid
198
- if len(names) < 5000: # 缓存上限防无限膨胀
199
- names[uid] = name2
200
- return name2
201
-
202
- fname = os.path.join(out_dir, sanitize_filename(name) + ".txt")
203
- header = "【%s】\n" % name
204
- need_header = not os.path.exists(fname)
205
- lines = []
206
- max_seq = since
207
- for m in msgs:
208
- seq = m.get("sort_seq") or 0
209
- if seq <= since:
210
- continue
211
- max_seq = max(max_seq, seq)
212
- uid = m.get("sender_username") or ""
213
- who = resolve(uid)
214
- mtype = m.get("type")
215
- text = None
216
- if "文本" in str(mtype or ""):
217
- text = strip_sender_prefix(m.get("content"), uid)
218
- else:
219
- text = type_placeholder(mtype)
220
- if not text:
221
- continue
222
- lines.append(format_line(m.get("create_time"), who, text))
223
- if lines:
224
- with open(fname, "a", encoding="utf-8") as f:
225
- if need_header:
226
- f.write(header)
227
- f.write("\n".join(lines) + "\n")
228
- state[username] = {"name": name, "since_seq": max_seq,
229
- "names": names, "t": time.time()}
230
- pulled[username] = len(lines)
231
- save_state(state_path, state)
232
- _write_result(result_path, {"ok": not errors, "pulled": pulled,
233
- "errors": errors, "count": sum(pulled.values())})
234
-
235
-
236
- def main():
237
- ap = argparse.ArgumentParser(description="CodeBee 微信群消息只读 sidecar")
238
- ap.add_argument("--list-groups", action="store_true")
239
- ap.add_argument("--pull", action="store_true")
240
- ap.add_argument("--out", default="")
241
- ap.add_argument("--state", default="")
242
- ap.add_argument("--groups-file", default="")
243
- ap.add_argument("--result", required=True, help="结果 JSON 落点(stdout 不可靠)")
244
- args = ap.parse_args()
245
- try:
246
- if args.list_groups:
247
- cmd_list_groups(args.result)
248
- elif args.pull:
249
- cmd_pull(args.out, args.state, args.groups_file, args.result)
250
- else:
251
- ap.error("需要 --list-groups 或 --pull")
252
- except Exception as e:
253
- # 顶层异常也落 result:微信未登录/DB 锁住等真实原因必须透传给主程序
254
- _write_result(args.result, {"ok": False, "errors": [str(e)],
255
- "groups": [], "pulled": {}, "count": 0})
256
- return 1
257
- return 0
258
-
259
-
260
- if __name__ == "__main__":
261
- sys.exit(main())
1
+ # -*- coding: utf-8 -*-
2
+ """微信读取 sidecar:由 64 位 Python ≥3.9(配 wechatauto-replica)运行,
3
+ 把本机微信 4.x 群消息增量拉成本仓库已有的「导出 txt」格式。
4
+
5
+ 为什么是独立进程:wechatauto 依赖链(winsdk 等)需要 64 位 Python ≥3.9,
6
+ 而 CodeBee 服务钉在 3.8-32。sidecar 通过文件与主程序解耦——拉到的新消息
7
+ 按「2024-01-01 12:34:56 昵称」行格式追加进监控文件夹,wxdigest 现有的
8
+ 解析/增量游标/摘要/桌宠提醒整条链路零改动复用。
9
+
10
+ 用法(两个模式,互斥):
11
+ 列群(供前端选择):
12
+ python wx_reader.py --list-groups --result OUT.json
13
+ 拉增量(每次扫描前调用):
14
+ python wx_reader.py --pull --out DIR --state STATE.json \
15
+ --groups-file GROUPS.json --result OUT.json
16
+ GROUPS.json = [{"username": "...@chatroom", "name": "群名"}, ...]
17
+
18
+ 只读保证:只调用 get_groups/get_new_messages/get_group_members/get_self_info,
19
+ 不调用任何发送/删除接口。
20
+ 结果 JSON 写 --result 文件(stdout 被库日志污染,不作为契约)。
21
+ 语法保持 3.8 兼容(测试解释器要 import 本文件的纯函数)。
22
+ """
23
+ from __future__ import annotations
24
+
25
+ import argparse
26
+ import json
27
+ import os
28
+ import re
29
+ import sys
30
+ import time
31
+
32
+ # 群消息 content 自带「<wxid>: 前缀」,剥掉后用成员表翻译成昵称
33
+ _PREFIX_RE = re.compile(r"^([A-Za-z0-9_+@\-.]{3,}):\s*\n?")
34
+ _ILLEGAL_FN = re.compile(r'[\\/:*?"<>|]')
35
+
36
+ SELF_NAME = "我"
37
+
38
+
39
+ def sanitize_filename(name):
40
+ """群名 → 合法文件名(Windows 非法字符换下划线;空名回落 username)。"""
41
+ out = _ILLEGAL_FN.sub("_", str(name or "").strip())
42
+ return out[:80] or "unnamed"
43
+
44
+
45
+ def strip_sender_prefix(content, sender_username):
46
+ """群消息正文剥掉自带的「wxid: 」前缀(前缀与发送者一致才可信地剥)。"""
47
+ text = str(content or "")
48
+ m = _PREFIX_RE.match(text)
49
+ if m and (not sender_username or m.group(1) == sender_username):
50
+ return text[m.end():].strip()
51
+ return text.strip()
52
+
53
+
54
+ def member_map(members):
55
+ """get_group_members 结果 → {username: 显示名}。备注优先(微信 UI 同款)。"""
56
+ out = {}
57
+ for m in members or []:
58
+ if not isinstance(m, dict):
59
+ continue
60
+ uid = m.get("username") or ""
61
+ if not uid:
62
+ continue
63
+ out[uid] = m.get("remark") or m.get("nick_name") or uid
64
+ return out
65
+
66
+
67
+ def format_line(ts_epoch, sender_name, text):
68
+ """一条消息 → 导出格式行(时间戳行与正文之间不留空行,与现有解析器对齐)。"""
69
+ stamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts_epoch or 0))
70
+ return "%s %s\n%s\n" % (stamp, sender_name or "?", str(text or "").strip())
71
+
72
+
73
+ def type_placeholder(mtype):
74
+ """非文本消息 → 占位符(摘要时让模型知道有图/语音存在)。None=跳过不入正文。"""
75
+ t = str(mtype or "")
76
+ if "文本" in t:
77
+ return None # 文本走正文,不是占位符
78
+ for key, ph in (("图片", "[图片]"), ("语音", "[语音]"), ("视频", "[视频]"),
79
+ ("文件", "[文件]"), ("链接", "[链接]"), ("名片", "[名片]"),
80
+ ("位置", "[位置]"), ("动画表情", "[表情]"), ("系统", None)):
81
+ if key in t:
82
+ return ph
83
+ return "[%s]" % t if t and len(t) <= 8 else None
84
+
85
+
86
+ def _quiet_lib_logs():
87
+ try:
88
+ import logging
89
+ logging.getLogger("wechatauto").setLevel(logging.WARNING)
90
+ except Exception:
91
+ pass
92
+
93
+
94
+ def _write_result(path, obj):
95
+ tmp = str(path) + ".tmp"
96
+ with open(tmp, "w", encoding="utf-8") as f:
97
+ json.dump(obj, f, ensure_ascii=False)
98
+ os.replace(tmp, path)
99
+
100
+
101
+ def _load_json(path, default):
102
+ try:
103
+ with open(path, "r", encoding="utf-8") as f:
104
+ return json.load(f)
105
+ except Exception:
106
+ return default
107
+
108
+
109
+ def load_state(path):
110
+ return _load_json(path, {}) or {}
111
+
112
+
113
+ def save_state(path, state):
114
+ tmp = str(path) + ".tmp"
115
+ with open(tmp, "w", encoding="utf-8") as f:
116
+ json.dump(state, f, ensure_ascii=False)
117
+ os.replace(tmp, path)
118
+
119
+
120
+ def cmd_list_groups(result_path):
121
+ _quiet_lib_logs()
122
+ import wechatauto
123
+ db = wechatauto.WeChatDB()
124
+ groups, seen = [], set()
125
+ for g in db.get_groups():
126
+ if isinstance(g, dict) and g.get("username"):
127
+ groups.append({"username": g["username"],
128
+ "name": g.get("name") or g["username"],
129
+ "member_count": str(g.get("member_count") or "")})
130
+ seen.add(g["username"])
131
+ # 新群兜底(2026-09-21 用户实测「缺一个新建的群」):刚建的群可能还没
132
+ # 落进 chat_room/contact,但只要有过消息就在会话列表里——合并补进,
133
+ # 昵称未同步时先显示 wxid,微信同步后「刷新群列表」即见真名。
134
+ try:
135
+ for s in db.get_sessions(200):
136
+ u = str((s or {}).get("username") or "")
137
+ if u.endswith("@chatroom") and u not in seen:
138
+ groups.append({"username": u,
139
+ "name": db.get_nickname(u) or u,
140
+ "member_count": ""})
141
+ seen.add(u)
142
+ except Exception:
143
+ pass # 会话库不可用不影响主清单
144
+ _write_result(result_path, {"ok": True, "groups": groups, "count": len(groups)})
145
+
146
+
147
+ def cmd_pull(out_dir, state_path, groups_file, result_path):
148
+ _quiet_lib_logs()
149
+ import wechatauto
150
+ sel = _load_json(groups_file, [])
151
+ sel = [g for g in (sel or []) if isinstance(g, dict) and g.get("username")]
152
+ state = load_state(state_path)
153
+ os.makedirs(out_dir, exist_ok=True)
154
+ db = wechatauto.WeChatDB()
155
+ try:
156
+ self_wxid = db.wxid
157
+ except Exception:
158
+ self_wxid = ""
159
+ pulled, errors = {}, []
160
+ for g in sel:
161
+ username, name = g["username"], g.get("name") or g["username"]
162
+ since = 0
163
+ try:
164
+ since = int((state.get(username) or {}).get("since_seq") or 0)
165
+ except (TypeError, ValueError):
166
+ since = 0
167
+ try:
168
+ msgs = db.get_new_messages(username, since_seq=since, limit=500)
169
+ except Exception as e:
170
+ errors.append("%s: %s" % (name, e))
171
+ continue
172
+ if not msgs:
173
+ pulled[username] = 0
174
+ continue
175
+ members = {}
176
+ try:
177
+ members = member_map(db.get_group_members(username))
178
+ except Exception:
179
+ members = {}
180
+ # 昵称缓存:退群成员不在成员表里,靠 get_nickname 兜底一次后永久缓存
181
+ gstate = state.get(username) or {}
182
+ names = dict(gstate.get("names") or {})
183
+
184
+ def resolve(uid):
185
+ if not uid:
186
+ return "?"
187
+ if self_wxid and uid == self_wxid:
188
+ return SELF_NAME
189
+ if uid in members:
190
+ return members[uid]
191
+ if uid in names:
192
+ return names[uid]
193
+ try:
194
+ nick = str(db.get_nickname(uid) or "").strip()
195
+ except Exception:
196
+ nick = ""
197
+ name2 = nick or uid
198
+ if len(names) < 5000: # 缓存上限防无限膨胀
199
+ names[uid] = name2
200
+ return name2
201
+
202
+ fname = os.path.join(out_dir, sanitize_filename(name) + ".txt")
203
+ header = "【%s】\n" % name
204
+ need_header = not os.path.exists(fname)
205
+ lines = []
206
+ max_seq = since
207
+ for m in msgs:
208
+ seq = m.get("sort_seq") or 0
209
+ if seq <= since:
210
+ continue
211
+ max_seq = max(max_seq, seq)
212
+ uid = m.get("sender_username") or ""
213
+ who = resolve(uid)
214
+ mtype = m.get("type")
215
+ text = None
216
+ if "文本" in str(mtype or ""):
217
+ text = strip_sender_prefix(m.get("content"), uid)
218
+ else:
219
+ text = type_placeholder(mtype)
220
+ if not text:
221
+ continue
222
+ lines.append(format_line(m.get("create_time"), who, text))
223
+ if lines:
224
+ with open(fname, "a", encoding="utf-8") as f:
225
+ if need_header:
226
+ f.write(header)
227
+ f.write("\n".join(lines) + "\n")
228
+ state[username] = {"name": name, "since_seq": max_seq,
229
+ "names": names, "t": time.time()}
230
+ pulled[username] = len(lines)
231
+ save_state(state_path, state)
232
+ _write_result(result_path, {"ok": not errors, "pulled": pulled,
233
+ "errors": errors, "count": sum(pulled.values())})
234
+
235
+
236
+ def main():
237
+ ap = argparse.ArgumentParser(description="CodeBee 微信群消息只读 sidecar")
238
+ ap.add_argument("--list-groups", action="store_true")
239
+ ap.add_argument("--pull", action="store_true")
240
+ ap.add_argument("--out", default="")
241
+ ap.add_argument("--state", default="")
242
+ ap.add_argument("--groups-file", default="")
243
+ ap.add_argument("--result", required=True, help="结果 JSON 落点(stdout 不可靠)")
244
+ args = ap.parse_args()
245
+ try:
246
+ if args.list_groups:
247
+ cmd_list_groups(args.result)
248
+ elif args.pull:
249
+ cmd_pull(args.out, args.state, args.groups_file, args.result)
250
+ else:
251
+ ap.error("需要 --list-groups 或 --pull")
252
+ except Exception as e:
253
+ # 顶层异常也落 result:微信未登录/DB 锁住等真实原因必须透传给主程序
254
+ _write_result(args.result, {"ok": False, "errors": [str(e)],
255
+ "groups": [], "pulled": {}, "count": 0})
256
+ return 1
257
+ return 0
258
+
259
+
260
+ if __name__ == "__main__":
261
+ sys.exit(main())