codebee 0.1.70 → 0.1.71

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 (107) hide show
  1. package/CHANGELOG.md +589 -580
  2. package/LICENSE +21 -21
  3. package/README.md +509 -509
  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/aiflavor.py +197 -197
  13. package/app/core/artifacts.py +148 -148
  14. package/app/core/assets.py +110 -110
  15. package/app/core/attachments.py +623 -623
  16. package/app/core/audit.py +30 -30
  17. package/app/core/backup.py +470 -470
  18. package/app/core/beekeeper.py +183 -183
  19. package/app/core/benchstore.py +95 -95
  20. package/app/core/board.py +209 -209
  21. package/app/core/bookmeta.py +935 -935
  22. package/app/core/bookmeta_catalog.py +180 -180
  23. package/app/core/branching.py +137 -137
  24. package/app/core/builtin_agent.py +1673 -1673
  25. package/app/core/capability.py +144 -144
  26. package/app/core/chaptersafe.py +32 -32
  27. package/app/core/cleanup.py +303 -303
  28. package/app/core/compaction.py +201 -201
  29. package/app/core/covergen.py +356 -356
  30. package/app/core/defectretro.py +43 -43
  31. package/app/core/diagnostics.py +114 -114
  32. package/app/core/dispatch.py +190 -190
  33. package/app/core/dispatch_log.py +128 -128
  34. package/app/core/env_scrub.py +85 -85
  35. package/app/core/errorlog.py +137 -137
  36. package/app/core/evalbench.py +720 -720
  37. package/app/core/evaluation.py +174 -174
  38. package/app/core/failure_notify.py +290 -290
  39. package/app/core/flows.py +7 -7
  40. package/app/core/goal_service.py +167 -167
  41. package/app/core/health.py +374 -374
  42. package/app/core/history.py +38 -38
  43. package/app/core/hooks.py +162 -162
  44. package/app/core/manager.py +2143 -2143
  45. package/app/core/mocks.py +64 -64
  46. package/app/core/notify.py +201 -201
  47. package/app/core/novel_quality.py +189 -189
  48. package/app/core/operations.py +228 -228
  49. package/app/core/paihang.py +138 -138
  50. package/app/core/portguard.py +111 -111
  51. package/app/core/preview.py +293 -293
  52. package/app/core/publish/__init__.py +7 -7
  53. package/app/core/publish/auto.py +367 -367
  54. package/app/core/publish/browser.py +655 -655
  55. package/app/core/publish/fanqie.py +165 -165
  56. package/app/core/publish/flow.py +589 -589
  57. package/app/core/publish/flows-fanqie-calibrated.json +336 -336
  58. package/app/core/publish/ledger.py +229 -229
  59. package/app/core/publish/manager.py +824 -824
  60. package/app/core/publish/qimao.py +146 -146
  61. package/app/core/publish/ws.py +139 -139
  62. package/app/core/redact.py +38 -38
  63. package/app/core/repeat_guard.py +129 -129
  64. package/app/core/router.py +398 -398
  65. package/app/core/runner.py +2142 -2142
  66. package/app/core/selfupdate.py +384 -384
  67. package/app/core/session_log.py +161 -161
  68. package/app/core/sessions.py +312 -312
  69. package/app/core/settings.py +3 -2
  70. package/app/core/settings_schema.py +270 -270
  71. package/app/core/share_page.py +114 -114
  72. package/app/core/skill_scan.py +122 -122
  73. package/app/core/skillpacks/fanqie-novel.md +80 -80
  74. package/app/core/skillpacks/market/character-bible.md +66 -66
  75. package/app/core/skillpacks/market/code-risk-checklist.md +58 -58
  76. package/app/core/skillpacks/market/git-workflow.md +57 -57
  77. package/app/core/skillpacks/market/release-notes.md +72 -72
  78. package/app/core/skillpacks/market/weekly-report.md +71 -71
  79. package/app/core/skillpacks/market/worldview-consistency.md +70 -70
  80. package/app/core/step_runner.py +132 -132
  81. package/app/core/task_compile.py +199 -199
  82. package/app/core/telemetry.py +292 -292
  83. package/app/core/tlsctx.py +64 -64
  84. package/app/core/token_meter.py +147 -147
  85. package/app/core/tracing.py +144 -144
  86. package/app/core/upstream.py +41 -41
  87. package/app/core/usage.py +1035 -1035
  88. package/app/core/volumes.py +389 -389
  89. package/app/core/wxdigest.py +858 -858
  90. package/app/core/zentao.py +2614 -2614
  91. package/app/main.py +38 -3
  92. package/app/mcp_server.py +175 -175
  93. package/app/pet.py +1 -1
  94. package/app/pick_dialog.py +110 -110
  95. package/app/ui/app.js +11 -4
  96. package/app/ui/artdiff.js +74 -74
  97. package/app/ui/board.html +162 -162
  98. package/app/ui/board.js +305 -305
  99. package/app/ui/i18n.js +1 -1
  100. package/app/ui/icons/bee.svg +79 -79
  101. package/app/ui/manifest.json +16 -16
  102. package/app/ui/qrcode.js +2297 -2297
  103. package/app/ui/style.css +65 -18
  104. package/app//344/275/234/345/223/201/344/277/241/346/201/257-/344/270/203/347/214/253.md +18 -0
  105. package/bin/tutti.js +147 -147
  106. package/package.json +47 -47
  107. 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())