dsh-world 0.6.6 → 0.7.0

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 (55) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/CONTRIBUTING.md +163 -161
  3. package/INSTALL.md +314 -314
  4. package/LICENSE +21 -21
  5. package/NOTES.md +173 -152
  6. package/README.md +236 -227
  7. package/client/index.js +849 -738
  8. package/cordis.patch.yml +6 -6
  9. package/lib/compaction.js +8 -0
  10. package/lib/compactionSwap.d.ts +29 -2
  11. package/lib/compactionSwap.js +62 -7
  12. package/lib/console/homeApply.d.ts +11 -2
  13. package/lib/console/homeApply.js +36 -7
  14. package/lib/console/routes.d.ts +8 -0
  15. package/lib/console/routes.js +32 -0
  16. package/lib/console/snapshot.d.ts +3 -0
  17. package/lib/console/snapshot.js +2 -1
  18. package/lib/ecosystem/bridge/route.d.ts +7 -0
  19. package/lib/ecosystem/bridge/route.js +30 -0
  20. package/lib/hostContract.d.ts +1 -0
  21. package/lib/index.js +37 -12
  22. package/lib/memory.d.ts +57 -0
  23. package/lib/memory.js +210 -0
  24. package/lib/memoryFiles.d.ts +53 -0
  25. package/lib/memoryFiles.js +193 -0
  26. package/lib/memoryStore.d.ts +114 -0
  27. package/lib/memoryStore.js +159 -0
  28. package/lib/memoryTools.d.ts +31 -0
  29. package/lib/memoryTools.js +106 -0
  30. package/lib/promptSection.d.ts +2 -0
  31. package/lib/runtime.d.ts +1 -1
  32. package/lib/runtime.js +61 -29
  33. package/lib/sourceArchive.d.ts +1 -0
  34. package/lib/sourceArchive.js +1 -1
  35. package/lib/store.d.ts +14 -0
  36. package/lib/store.js +11 -1
  37. package/lib/worldPrompt.d.ts +6 -0
  38. package/lib/worldPrompt.js +7 -1
  39. package/package.json +128 -117
  40. package/scripts/compact-home.mjs +29 -0
  41. package/scripts/enable-tool-free-compaction.ps1 +33 -33
  42. package/scripts/import-stickers.mjs +17 -17
  43. package/scripts/update-world.mjs +5 -3
  44. package/src/ecosystem/wechat/windows/README.md +6 -4
  45. package/src/ecosystem/wechat/windows/adapter.py +80 -31
  46. package/src/ecosystem/wechat/windows/engine/NOTICE.md +1 -1
  47. package/src/ecosystem/wechat/windows/engine/bridge.cpp +9 -1
  48. package/src/ecosystem/wechat/windows/engine/profiles.json +114 -58
  49. package/src/ecosystem/wechat/windows/native.py +4 -1
  50. package/src/ecosystem/wechat/windows/outgoing.py +21 -0
  51. package/src/ecosystem/wechat/windows/presence.py +109 -109
  52. package/src/ecosystem/wechat/windows/sender.py +9 -1
  53. package/templates/alarm.md +3 -4
  54. package/templates/world-prompt-memory.md +10 -0
  55. package/templates/world-prompt.md +7 -2
@@ -23,8 +23,10 @@ from reading import readHistory
23
23
  from presence import GroupPresence, seedPresence, speakerName, unreadAfter
24
24
  from datetime import datetime, timezone
25
25
 
26
- IMAGE_BUFFER_SECONDS = 3
27
- IMAGE_BUFFER_POLL_SECONDS = 0.1
26
+ BUFFER_SECONDS = 3
27
+ BUFFER_POLL_SECONDS = 0.1
28
+ MAX_BUFFER_SECONDS = 30
29
+ MAX_BATCH_ROWS = 16
28
30
  MAX_PRIVATE_IMAGES = 2
29
31
  CONTROL_TIMEOUT_SECONDS = 5
30
32
  REPLY_TIMEOUT_SECONDS = 190
@@ -38,6 +40,10 @@ class NeedsHuman(RuntimeError):
38
40
  pass
39
41
 
40
42
 
43
+ def pendingText(chatId, key):
44
+ return f'上次消息结果尚未确认:{chatId} / {key}。核对家和微信后使用 acknowledge,不自动重发。'
45
+
46
+
41
47
  class HomeRequestError(RuntimeError):
42
48
  pass
43
49
 
@@ -104,30 +110,63 @@ def newMessages(database, chatId, sinceSeq, pageSize):
104
110
  return sorted(found.values(), key=messageOrder)
105
111
 
106
112
 
107
- def bufferImageMessages(config, database, store, chat, first, *, clock=time.monotonic, sleep=time.sleep):
108
- deadline = clock() + config.get('bufferSeconds', IMAGE_BUFFER_SECONDS)
113
+ def followingRows(chat, first, candidates, store):
114
+ """What the sender of `first` added after it that can join its request, and whether nothing more can (someone else
115
+ spoke, a message that goes on its own, or a limit was reached)."""
116
+ group = chat['id'].endswith('@chatroom')
117
+ rows, images, length = [], int(first['type'] == '图片'), len(str(first['content'])) if first['type'] == '文本' else 0
118
+ for row in candidates:
119
+ if messageOrder(row) <= messageOrder(first) or store.seen(chat['id'], messageKey(row)):
120
+ continue
121
+ if row.get('sender_username') != first.get('sender_username') or row['type'] not in (('文本',) if group else ('文本', '图片')) or len(rows) + 1 == MAX_BATCH_ROWS:
122
+ return rows, True
123
+ if row['type'] == '图片':
124
+ if images == MAX_PRIVATE_IMAGES: return rows, True
125
+ images += 1
126
+ else:
127
+ text = str(row['content'])
128
+ # Merged, the request still has to fit the home's 2000-character limit.
129
+ if not text.strip() or length + len(text) + 1 > 2000: return rows, True
130
+ length += len(text) + 1
131
+ rows.append(row)
132
+ return rows, False
133
+
134
+
135
+ def mergeFollowing(database, chat, config, message, rows):
136
+ """Adds what `bufferMessages` gathered to the request of the first message: texts in order, pictures as media."""
137
+ group = chat['id'].endswith('@chatroom')
138
+ # A later group line does not call her itself, so it is read as plain text; the request keeps the identity that did.
139
+ normalize = messageNormalizer(database, chat, config) if group else None
140
+ for row in rows:
141
+ extra = normalize(row) if group else incomingMessage(database, chat, row, config)
142
+ if extra is None: raise RuntimeError('接收批次包含无法识别的消息')
143
+ if extra['text']:
144
+ message['text'] = '\n'.join(text for text in (message['text'], extra['text']) if text)
145
+ if not group: message['identity'] = extra['identity']
146
+ if extra.get('media'): message.setdefault('media', []).extend(extra['media'])
147
+
148
+
149
+ def bufferMessages(config, database, store, chat, first, *, clock=None, sleep=None):
150
+ """Waits until the sender of `first` has been quiet for the buffer time, each new message of theirs starting the wait
151
+ again, so a thought typed as several messages reaches her as one request. The whole wait is capped."""
152
+ clock, sleep = clock or time.monotonic, sleep or time.sleep
153
+ quiet = config.get('bufferSeconds', BUFFER_SECONDS)
154
+ latest = clock() + MAX_BUFFER_SECONDS
155
+ deadline = min(clock() + quiet, latest)
109
156
  batch = [first]
110
157
  while True:
111
158
  if clock() >= deadline: return batch
112
159
  candidates = newMessages(database, chat['id'], int(first['sort_seq']), config.get('pageSize', 50))
160
+ # A slow read must not take in a message that arrived after the window closed; it starts the next request.
113
161
  if clock() > deadline: return batch
114
- batch = [first]
115
- images = 1
116
- for row in candidates:
117
- if messageOrder(row) <= messageOrder(first) or store.seen(chat['id'], messageKey(row)):
118
- continue
119
- if row.get('sender_username') != first.get('sender_username') or row['type'] not in ('文本', '图片'):
120
- return batch
121
- if row['type'] == '图片':
122
- if images == MAX_PRIVATE_IMAGES: return batch
123
- images += 1
124
- batch.append(row)
125
- else:
126
- if str(row['content']).strip() and len(str(row['content'])) <= 2000: batch.append(row)
127
- return batch
162
+ following, closed = followingRows(chat, first, candidates, store)
163
+ if len(following) >= len(batch):
164
+ batch = [first, *following]
165
+ deadline = min(clock() + quiet, latest)
166
+ if closed: return batch
128
167
  remaining = deadline - clock()
129
168
  if remaining <= 0: return batch
130
- sleep(min(IMAGE_BUFFER_POLL_SECONDS, remaining))
169
+ sleep(min(BUFFER_POLL_SECONDS, remaining))
131
170
 
132
171
 
133
172
  def incomingMessage(database, chat, row, config):
@@ -241,7 +280,14 @@ class MessageStore:
241
280
  def checkPending(self):
242
281
  row = self.db.execute("SELECT chatId,messageId FROM messages WHERE status='pending' ORDER BY rowid LIMIT 1").fetchone()
243
282
  if row:
244
- raise NeedsHuman(f'上次消息结果尚未确认:{row[0]} / {row[1]}。核对家和微信后使用 acknowledge,不自动重发。')
283
+ raise NeedsHuman(pendingText(row[0], row[1]))
284
+
285
+ def pendingChats(self):
286
+ """The chats waiting for a person to check an unconfirmed message, each with its oldest such message."""
287
+ pending = {}
288
+ for chatId, key in self.db.execute("SELECT chatId,messageId FROM messages WHERE status='pending' ORDER BY rowid"):
289
+ pending.setdefault(chatId, key)
290
+ return pending
245
291
 
246
292
  def acknowledge(self, chatId, key):
247
293
  row = self.db.execute("SELECT seq FROM messages WHERE chatId=? AND messageId=? AND status='pending' LIMIT 1", (chatId, key)).fetchone()
@@ -477,14 +523,21 @@ def askWhileSending(config, message, database, store, send, ask, report, poll):
477
523
 
478
524
  def watch(config, database, store, send, ask=askHome, report=reportReceipt, poll=pollOutgoing):
479
525
  pageSize = config.get('pageSize', 50)
480
- paused = None
526
+ paused, held = None, {}
481
527
  while True:
482
528
  try:
483
529
  flushReceipts(config['moduleFile'], store, report)
484
530
  flushOutgoing(config, database, store, send, poll=poll, report=report)
485
- store.checkPending()
531
+ # An unconfirmed message holds back only its own chat: a failed group request used to stop private chats too.
532
+ pending = store.pendingChats()
533
+ for chatId, key in pending.items():
534
+ if held.get(chatId) != key:
535
+ print(f'微信入站处理暂缓(仅此聊天,其他聊天照常):{pendingText(chatId, key)}', file=sys.stderr, flush=True)
536
+ held = pending
486
537
  paused = None
487
538
  for chat in config['chats']:
539
+ if chat['id'] in pending:
540
+ continue
488
541
  baselineChat(database, store, chat['id'], pageSize)
489
542
  seedPresence(database, store, config, chat)
490
543
  for row in newMessages(database, chat['id'], store.cursor(chat['id']), pageSize):
@@ -492,25 +545,21 @@ def watch(config, database, store, send, ask=askHome, report=reportReceipt, poll
492
545
  continue
493
546
  if chat['id'].endswith('@chatroom') and row['type'] != '系统消息':
494
547
  store.presence.observe(config['selfId'], chat['id'], row, speakerName(database, config, row))
495
- rows = bufferImageMessages(config, database, store, chat, row) if not chat['id'].endswith('@chatroom') and row['type'] == '图片' and row.get('sender_username') != config['selfId'] else [row]
496
548
  try: message = incomingMessage(database, chat, row, config)
497
549
  except MessageContentError as error:
498
550
  store.fault(chat['id'], row, error, rejected=True)
499
551
  continue
500
- if chat['id'].endswith('@chatroom') and message is None:
552
+ group = chat['id'].endswith('@chatroom')
553
+ if group and message is None:
501
554
  # No delivery to track: retain only the group's counter and receive position.
502
555
  store.finish(chat['id'], messageKey(row), int(row['sort_seq']))
503
556
  flushOutgoing(config, database, store, send, poll=poll, report=report)
504
557
  continue
505
558
  if message and message.get('reference', {}).get('error'):
506
559
  store.fault(chat['id'], row, message['reference']['error'])
507
- for following in rows[1:]:
508
- extra = incomingMessage(database, chat, following, config)
509
- if extra is None: raise RuntimeError('接收批次包含无法识别的消息')
510
- if extra['text']:
511
- message['text'] = extra['text']
512
- message['identity'] = extra['identity']
513
- if extra.get('media'): message.setdefault('media', []).extend(extra['media'])
560
+ # Only a message she is asked about waits for what its sender adds; in a group, that is one that calls her.
561
+ rows = bufferMessages(config, database, store, chat, row) if message and row['type'] in ('文本', '图片') else [row]
562
+ mergeFollowing(database, chat, config, message, rows[1:])
514
563
  store.beginBatch(chat['id'], rows)
515
564
  if message:
516
565
  askWhileSending(config, message, database, store, send, ask, report, poll)
@@ -9,5 +9,5 @@ This is a tailored send-only bridge. It has its own request ABI and lifecycle;
9
9
  it does not include helper's bot framework, model, user interface, receive
10
10
  hooks, recall handling, account discovery or group management.
11
11
 
12
- profiles.json contains the fingerprint-locked 4.1.15.13 and 4.1.15.56
12
+ profiles.json contains the fingerprint-locked 4.1.15.13, 4.1.15.56, 4.2.0.11 and 4.2.0.12
13
13
  address mappings from https://github.com/Prometheus-first/helper/blob/main/native/core_version.h.
@@ -189,6 +189,11 @@ static uint32_t enqueue(Job* job, std::function<void()>* task) {
189
189
  __try { release(future); release(scheduler); } __except (EXCEPTION_EXECUTE_HANDLER) { status = GetExceptionCode(); }
190
190
  return status;
191
191
  }
192
+ // Used only to send a GIF sticker (kind 3). A build whose sticker path is not mapped (4.2) leaves them 0 and sends
193
+ // text and images only.
194
+ static bool stickerCall(int call) {
195
+ return call == Emoticon || call == ValidateGif || call == ProbeGif || call == ReleaseProbe || call == EmoticonVtable;
196
+ }
192
197
  static bool valid(const Request& request, uintptr_t base) {
193
198
  if (request.size != sizeof(Request) || request.pid != GetCurrentProcessId() || request.kind > 3 ||
194
199
  !memchr(request.account, 0, sizeof(request.account)) || !request.account[0] ||
@@ -200,7 +205,10 @@ static bool valid(const Request& request, uintptr_t base) {
200
205
  auto header = reinterpret_cast<const IMAGE_NT_HEADERS64*>(base + dos->e_lfanew);
201
206
  if (header->Signature != IMAGE_NT_SIGNATURE || header->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64 ||
202
207
  header->FileHeader.TimeDateStamp != request.timestamp || header->OptionalHeader.SizeOfImage != request.imageSize) return false;
203
- for (auto rva: request.rva) if (!rva || rva >= request.imageSize) return false;
208
+ for (int call = 0; call < CallCount; ++call) {
209
+ const auto rva = request.rva[call];
210
+ if (rva >= request.imageSize || (!rva && (!stickerCall(call) || request.kind == 3))) return false;
211
+ }
204
212
  return true;
205
213
  } __except (EXCEPTION_EXECUTE_HANDLER) { return false; }
206
214
  }
@@ -1,58 +1,114 @@
1
- [
2
- {
3
- "version": "4.1.15.13",
4
- "size": 201552944,
5
- "sha256": "10f8e995453e2da46d4f2b5080cd6da1f13cc5147746adc119ceae38cb039de5",
6
- "timestamp": 1789961080,
7
- "imageSize": 202039296,
8
- "addresses": {
9
- "application": 285968,
10
- "scheduler": 291600,
11
- "schedule": 3125264,
12
- "context": 291360,
13
- "provider": 3589952,
14
- "manager": 7294352,
15
- "text": 88802032,
16
- "image": 64608656,
17
- "emoticon": 29340096,
18
- "copyWide": 636368,
19
- "validateGif": 15182048,
20
- "probeGif": 5441984,
21
- "releaseProbe": 5137248,
22
- "send": 24703232,
23
- "allocate": 122337436,
24
- "free": 122337504,
25
- "accountRoot": 194061968,
26
- "contextVtable": 148890360,
27
- "emoticonVtable": 151891960
28
- }
29
- },
30
- {
31
- "version": "4.1.15.56",
32
- "size": 204305968,
33
- "sha256": "e0428a7b691853de496532523c2c09aa095f90941c37c9f4cfb3f137f3c66556",
34
- "timestamp": 1792050120,
35
- "imageSize": 204795904,
36
- "addresses": {
37
- "application": 285968,
38
- "scheduler": 291600,
39
- "schedule": 3208272,
40
- "context": 291360,
41
- "provider": 3679184,
42
- "manager": 7377536,
43
- "text": 90112896,
44
- "image": 65154416,
45
- "emoticon": 29436416,
46
- "copyWide": 636384,
47
- "validateGif": 15343584,
48
- "probeGif": 5528896,
49
- "releaseProbe": 5224160,
50
- "send": 24788000,
51
- "allocate": 122993884,
52
- "free": 122993952,
53
- "accountRoot": 196685728,
54
- "contextVtable": 150803560,
55
- "emoticonVtable": 153824520
56
- }
57
- }
58
- ]
1
+ [
2
+ {
3
+ "version": "4.1.15.13",
4
+ "size": 201552944,
5
+ "sha256": "10f8e995453e2da46d4f2b5080cd6da1f13cc5147746adc119ceae38cb039de5",
6
+ "timestamp": 1789961080,
7
+ "imageSize": 202039296,
8
+ "addresses": {
9
+ "application": 285968,
10
+ "scheduler": 291600,
11
+ "schedule": 3125264,
12
+ "context": 291360,
13
+ "provider": 3589952,
14
+ "manager": 7294352,
15
+ "text": 88802032,
16
+ "image": 64608656,
17
+ "emoticon": 29340096,
18
+ "copyWide": 636368,
19
+ "validateGif": 15182048,
20
+ "probeGif": 5441984,
21
+ "releaseProbe": 5137248,
22
+ "send": 24703232,
23
+ "allocate": 122337436,
24
+ "free": 122337504,
25
+ "accountRoot": 194061968,
26
+ "contextVtable": 148890360,
27
+ "emoticonVtable": 151891960
28
+ }
29
+ },
30
+ {
31
+ "version": "4.1.15.56",
32
+ "size": 204305968,
33
+ "sha256": "e0428a7b691853de496532523c2c09aa095f90941c37c9f4cfb3f137f3c66556",
34
+ "timestamp": 1792050120,
35
+ "imageSize": 204795904,
36
+ "addresses": {
37
+ "application": 285968,
38
+ "scheduler": 291600,
39
+ "schedule": 3208272,
40
+ "context": 291360,
41
+ "provider": 3679184,
42
+ "manager": 7377536,
43
+ "text": 90112896,
44
+ "image": 65154416,
45
+ "emoticon": 29436416,
46
+ "copyWide": 636384,
47
+ "validateGif": 15343584,
48
+ "probeGif": 5528896,
49
+ "releaseProbe": 5224160,
50
+ "send": 24788000,
51
+ "allocate": 122993884,
52
+ "free": 122993952,
53
+ "accountRoot": 196685728,
54
+ "contextVtable": 150803560,
55
+ "emoticonVtable": 153824520
56
+ }
57
+ },
58
+ {
59
+ "version": "4.2.0.11",
60
+ "size": 205929008,
61
+ "sha256": "745cdf2955fa5128cdb00c056c97571156a5f107406a9bb15ac0754f3246b201",
62
+ "timestamp": 1793108177,
63
+ "imageSize": 206422016,
64
+ "addresses": {
65
+ "application": 285968,
66
+ "scheduler": 291600,
67
+ "schedule": 3217808,
68
+ "context": 291360,
69
+ "provider": 3688240,
70
+ "manager": 7456256,
71
+ "text": 91356448,
72
+ "image": 65819968,
73
+ "emoticon": null,
74
+ "copyWide": 636384,
75
+ "validateGif": null,
76
+ "probeGif": null,
77
+ "releaseProbe": null,
78
+ "send": 24965648,
79
+ "allocate": 124404668,
80
+ "free": 124404736,
81
+ "accountRoot": 198233648,
82
+ "contextVtable": 151867272,
83
+ "emoticonVtable": null
84
+ }
85
+ },
86
+ {
87
+ "version": "4.2.0.12",
88
+ "size": 205932072,
89
+ "sha256": "2b3677c3f932daaa4ef84c8b2acd0aee12cbe5a779ff9e740c370761e5c291ce",
90
+ "timestamp": 1793275655,
91
+ "imageSize": 206417920,
92
+ "addresses": {
93
+ "application": 285968,
94
+ "scheduler": 291600,
95
+ "schedule": 3217808,
96
+ "context": 291360,
97
+ "provider": 3688240,
98
+ "manager": 7451520,
99
+ "text": 91361344,
100
+ "image": 65824848,
101
+ "emoticon": null,
102
+ "copyWide": 636384,
103
+ "validateGif": null,
104
+ "probeGif": null,
105
+ "releaseProbe": null,
106
+ "send": 24962240,
107
+ "allocate": 124408764,
108
+ "free": 124408832,
109
+ "accountRoot": 198233648,
110
+ "contextVtable": 151871336,
111
+ "emoticonVtable": null
112
+ }
113
+ }
114
+ ]
@@ -134,9 +134,12 @@ def connect(config, stateDir):
134
134
 
135
135
  def send(chat, item):
136
136
  import hashlib
137
- from outgoing import mediaDigestMatches, serverConfirmed
137
+ from outgoing import mediaDigestMatches, serverConfirmed, stillSticker
138
+ from sender import sendsStickers
138
139
  checkProcess()
139
140
  item = dict(type='text', text=item) if isinstance(item, str) else item
141
+ if item['type'] == 'sticker' and not sendsStickers(getSender().profile):
142
+ item = dict(type='image', path=stillSticker(item['path']))
140
143
  kind = {'text': 1, 'image': 2, 'sticker': 3}[item['type']]
141
144
  value = item['text'] if kind == 1 else item['path']
142
145
  def stableRows(deadline):
@@ -87,6 +87,27 @@ def prepareMessages(messages, config, stateDir):
87
87
  return prepared
88
88
 
89
89
 
90
+ def blankFrame(frame):
91
+ """Nothing to see: fully transparent, or a single flat colour (many stickers fade in from one)."""
92
+ if frame.getchannel('A').point(lambda alpha: 255 if alpha > 16 else 0).getbbox() is None:
93
+ return True
94
+ return frame.convert('RGB').getcolors(maxcolors=1) is not None
95
+
96
+
97
+ def stillSticker(path):
98
+ """A GIF sticker as one still PNG beside it, for a WeChat build that cannot send stickers: its first frame, or the
99
+ last when the first is blank."""
100
+ from PIL import Image
101
+ still = Path(path).with_suffix('.still.png')
102
+ with Image.open(path) as image:
103
+ frame = image.convert('RGBA')
104
+ if blankFrame(frame) and getattr(image, 'n_frames', 1) > 1:
105
+ image.seek(image.n_frames - 1)
106
+ frame = image.convert('RGBA')
107
+ frame.save(still, format='PNG')
108
+ return str(still)
109
+
110
+
90
111
  def deliverMessages(messages, send, confirmed=None):
91
112
  for index, message in enumerate(messages):
92
113
  try:
@@ -1,109 +1,109 @@
1
- """One durable metadata row per account/group; no message body, wake or model call."""
2
- from datetime import datetime, timezone
3
- import json
4
- from messages import messageKey, messageOrder, recentEntries
5
- from snapshot import SnapshotBusy
6
-
7
-
8
- class GroupPresence:
9
- def __init__(self, db):
10
- self.db = db
11
- db.execute('''CREATE TABLE IF NOT EXISTS groupPresence(
12
- accountId TEXT, chatId TEXT, count INTEGER NOT NULL,
13
- seq INTEGER, shard TEXT, localId INTEGER, lastAt TEXT, speaker TEXT, self INTEGER,
14
- readSeq INTEGER NOT NULL DEFAULT 0, readShard TEXT NOT NULL DEFAULT '', readId INTEGER NOT NULL DEFAULT 0,
15
- seen TEXT NOT NULL DEFAULT '[]', readSeen TEXT NOT NULL DEFAULT '[]',
16
- PRIMARY KEY(accountId,chatId))''')
17
-
18
- def _get(self, account, chat):
19
- return self.db.execute('SELECT count,seq,shard,localId,lastAt,speaker,self,readSeq,readShard,readId,seen,readSeen '
20
- 'FROM groupPresence WHERE accountId=? AND chatId=? LIMIT 1', (account, chat)).fetchone()
21
-
22
- def observe(self, account, chat, row, speaker, *, unread=True):
23
- if row['type'] == '系统消息': return
24
- if not row.get('sender_username'): raise ValueError('群动态缺少真实发言人')
25
- order = messageOrder(row)
26
- previous = self._get(account, chat)
27
- if previous and order[0] < previous[1]: return
28
- sameSeq = bool(previous and order[0] == previous[1])
29
- seen = set(json.loads(previous[10])) if sameSeq else set()
30
- key = messageKey(row)
31
- if key in seen: return
32
- seen.add(key)
33
- read = tuple(previous[7:10]) if previous else (0, '', 0)
34
- own = row['sender_username'] == account
35
- count = (previous[0] if previous else 0) + int(unread and not own and (order > read or (sameSeq and order[0] == read[0])))
36
- at = datetime.fromtimestamp(int(row['create_time']), timezone.utc).isoformat()
37
- metadata = (at, str(speaker)[:80], int(own))
38
- if sameSeq and order < tuple(previous[1:4]):
39
- order, metadata = tuple(previous[1:4]), tuple(previous[4:7])
40
- with self.db:
41
- self.db.execute('INSERT INTO groupPresence VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?) '
42
- 'ON CONFLICT(accountId,chatId) DO UPDATE SET count=excluded.count,seq=excluded.seq,'
43
- 'shard=excluded.shard,localId=excluded.localId,lastAt=excluded.lastAt,speaker=excluded.speaker,self=excluded.self,seen=excluded.seen',
44
- (account, chat, count, *order, *metadata, *read, json.dumps(sorted(seen)), previous[11] if previous else '[]'))
45
-
46
- def markRead(self, account, chat, through, countRemaining=None, seenKeys=()):
47
- previous = self._get(account, chat)
48
- boundary = tuple(through)
49
- if not previous:
50
- with self.db:
51
- self.db.execute('INSERT INTO groupPresence(accountId,chatId,count,seq,shard,localId,readSeq,readShard,readId,readSeen) VALUES (?,?,0,0,\'\',0,?,?,?,?)', (account, chat, *boundary, json.dumps(sorted(seenKeys))))
52
- return
53
- read = max(tuple(previous[7:10]), boundary)
54
- seen = set(json.loads(previous[10]))
55
- if boundary[0] == previous[1]: seen.update(seenKeys)
56
- readSeen = set(json.loads(previous[11])) if boundary[0] == previous[7] else set()
57
- if boundary < tuple(previous[7:10]) or (boundary == tuple(previous[7:10]) and not set(seenKeys) - readSeen): return
58
- readSeen.update(seenKeys)
59
- remaining = 0
60
- if boundary < tuple(previous[1:4]):
61
- if countRemaining is None: raise ValueError('旧读取结果需要核对尚未读到的群消息')
62
- remaining = min(previous[0], countRemaining(boundary, tuple(previous[1:4])))
63
- with self.db:
64
- self.db.execute('UPDATE groupPresence SET count=?,readSeq=?,readShard=?,readId=?,seen=?,readSeen=? WHERE accountId=? AND chatId=?',
65
- (remaining, *read, json.dumps(sorted(seen)), json.dumps(sorted(readSeen)), account, chat))
66
-
67
- def snapshot(self, config):
68
- groups = []
69
- for chat in config['chats']:
70
- if not chat['id'].endswith('@chatroom'): continue
71
- row = self._get(config['selfId'], chat['id'])
72
- if row and row[4]:
73
- groups.append(dict(chatId=chat['id'], count=min(100, row[0]), lastAt=row[4], lastSpeaker=row[5], lastSelf=bool(row[6])))
74
- # Keep the existing control poll bounded even with many configured groups.
75
- return sorted(groups, key=lambda item: (item['count'] > 0, item['lastAt']), reverse=True)[:8]
76
-
77
-
78
- def speakerName(database, config, row):
79
- sender = row.get('sender_username')
80
- if not sender: raise ValueError('群动态缺少真实发言人')
81
- return config.get('people', {}).get(sender) or database.get_nickname(sender) or sender
82
-
83
-
84
- def seedPresence(database, store, config, chat):
85
- presence = store.presence
86
- previous = presence._get(config['selfId'], chat['id'])
87
- if not chat['id'].endswith('@chatroom') or (previous and previous[4]): return
88
- cursor = store.cursor(chat['id'])
89
- if cursor is None: return
90
- found = recentEntries(database, chat['id'], lambda row: row if int(row['sort_seq']) <= cursor and row['type'] != '系统消息' else None,
91
- config.get('pageSize', 50), 1)
92
- if found:
93
- latest = next(reversed(found.values()))
94
- presence.observe(config['selfId'], chat['id'], latest, speakerName(database, config, latest), unread=False)
95
-
96
-
97
- def unreadAfter(database, config, chat, after, until):
98
- offset, size, found = 0, config.get('pageSize', 50), set()
99
- while True:
100
- rows = database.get_messages(chat, limit=size, offset=offset)
101
- if offset == 0 and (not rows or max(int(row['sort_seq']) for row in rows) < until[0]):
102
- raise SnapshotBusy('群消息快照落后于已观察位置,保留未读等待重读')
103
- for row in rows:
104
- order = messageOrder(row)
105
- if after < order <= until and row['type'] != '系统消息':
106
- if not row.get('sender_username'): raise ValueError('群动态缺少真实发言人')
107
- if row['sender_username'] != config['selfId']: found.add(order)
108
- if len(rows) < size or any(int(row['sort_seq']) < after[0] for row in rows): return len(found)
109
- offset += len(rows)
1
+ """One durable metadata row per account/group; no message body, wake or model call."""
2
+ from datetime import datetime, timezone
3
+ import json
4
+ from messages import messageKey, messageOrder, recentEntries
5
+ from snapshot import SnapshotBusy
6
+
7
+
8
+ class GroupPresence:
9
+ def __init__(self, db):
10
+ self.db = db
11
+ db.execute('''CREATE TABLE IF NOT EXISTS groupPresence(
12
+ accountId TEXT, chatId TEXT, count INTEGER NOT NULL,
13
+ seq INTEGER, shard TEXT, localId INTEGER, lastAt TEXT, speaker TEXT, self INTEGER,
14
+ readSeq INTEGER NOT NULL DEFAULT 0, readShard TEXT NOT NULL DEFAULT '', readId INTEGER NOT NULL DEFAULT 0,
15
+ seen TEXT NOT NULL DEFAULT '[]', readSeen TEXT NOT NULL DEFAULT '[]',
16
+ PRIMARY KEY(accountId,chatId))''')
17
+
18
+ def _get(self, account, chat):
19
+ return self.db.execute('SELECT count,seq,shard,localId,lastAt,speaker,self,readSeq,readShard,readId,seen,readSeen '
20
+ 'FROM groupPresence WHERE accountId=? AND chatId=? LIMIT 1', (account, chat)).fetchone()
21
+
22
+ def observe(self, account, chat, row, speaker, *, unread=True):
23
+ if row['type'] == '系统消息': return
24
+ if not row.get('sender_username'): raise ValueError('群动态缺少真实发言人')
25
+ order = messageOrder(row)
26
+ previous = self._get(account, chat)
27
+ if previous and order[0] < previous[1]: return
28
+ sameSeq = bool(previous and order[0] == previous[1])
29
+ seen = set(json.loads(previous[10])) if sameSeq else set()
30
+ key = messageKey(row)
31
+ if key in seen: return
32
+ seen.add(key)
33
+ read = tuple(previous[7:10]) if previous else (0, '', 0)
34
+ own = row['sender_username'] == account
35
+ count = (previous[0] if previous else 0) + int(unread and not own and (order > read or (sameSeq and order[0] == read[0])))
36
+ at = datetime.fromtimestamp(int(row['create_time']), timezone.utc).isoformat()
37
+ metadata = (at, str(speaker)[:80], int(own))
38
+ if sameSeq and order < tuple(previous[1:4]):
39
+ order, metadata = tuple(previous[1:4]), tuple(previous[4:7])
40
+ with self.db:
41
+ self.db.execute('INSERT INTO groupPresence VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?) '
42
+ 'ON CONFLICT(accountId,chatId) DO UPDATE SET count=excluded.count,seq=excluded.seq,'
43
+ 'shard=excluded.shard,localId=excluded.localId,lastAt=excluded.lastAt,speaker=excluded.speaker,self=excluded.self,seen=excluded.seen',
44
+ (account, chat, count, *order, *metadata, *read, json.dumps(sorted(seen)), previous[11] if previous else '[]'))
45
+
46
+ def markRead(self, account, chat, through, countRemaining=None, seenKeys=()):
47
+ previous = self._get(account, chat)
48
+ boundary = tuple(through)
49
+ if not previous:
50
+ with self.db:
51
+ self.db.execute('INSERT INTO groupPresence(accountId,chatId,count,seq,shard,localId,readSeq,readShard,readId,readSeen) VALUES (?,?,0,0,\'\',0,?,?,?,?)', (account, chat, *boundary, json.dumps(sorted(seenKeys))))
52
+ return
53
+ read = max(tuple(previous[7:10]), boundary)
54
+ seen = set(json.loads(previous[10]))
55
+ if boundary[0] == previous[1]: seen.update(seenKeys)
56
+ readSeen = set(json.loads(previous[11])) if boundary[0] == previous[7] else set()
57
+ if boundary < tuple(previous[7:10]) or (boundary == tuple(previous[7:10]) and not set(seenKeys) - readSeen): return
58
+ readSeen.update(seenKeys)
59
+ remaining = 0
60
+ if boundary < tuple(previous[1:4]):
61
+ if countRemaining is None: raise ValueError('旧读取结果需要核对尚未读到的群消息')
62
+ remaining = min(previous[0], countRemaining(boundary, tuple(previous[1:4])))
63
+ with self.db:
64
+ self.db.execute('UPDATE groupPresence SET count=?,readSeq=?,readShard=?,readId=?,seen=?,readSeen=? WHERE accountId=? AND chatId=?',
65
+ (remaining, *read, json.dumps(sorted(seen)), json.dumps(sorted(readSeen)), account, chat))
66
+
67
+ def snapshot(self, config):
68
+ groups = []
69
+ for chat in config['chats']:
70
+ if not chat['id'].endswith('@chatroom'): continue
71
+ row = self._get(config['selfId'], chat['id'])
72
+ if row and row[4]:
73
+ groups.append(dict(chatId=chat['id'], count=min(100, row[0]), lastAt=row[4], lastSpeaker=row[5], lastSelf=bool(row[6])))
74
+ # Keep the existing control poll bounded even with many configured groups.
75
+ return sorted(groups, key=lambda item: (item['count'] > 0, item['lastAt']), reverse=True)[:8]
76
+
77
+
78
+ def speakerName(database, config, row):
79
+ sender = row.get('sender_username')
80
+ if not sender: raise ValueError('群动态缺少真实发言人')
81
+ return config.get('people', {}).get(sender) or database.get_nickname(sender) or sender
82
+
83
+
84
+ def seedPresence(database, store, config, chat):
85
+ presence = store.presence
86
+ previous = presence._get(config['selfId'], chat['id'])
87
+ if not chat['id'].endswith('@chatroom') or (previous and previous[4]): return
88
+ cursor = store.cursor(chat['id'])
89
+ if cursor is None: return
90
+ found = recentEntries(database, chat['id'], lambda row: row if int(row['sort_seq']) <= cursor and row['type'] != '系统消息' else None,
91
+ config.get('pageSize', 50), 1)
92
+ if found:
93
+ latest = next(reversed(found.values()))
94
+ presence.observe(config['selfId'], chat['id'], latest, speakerName(database, config, latest), unread=False)
95
+
96
+
97
+ def unreadAfter(database, config, chat, after, until):
98
+ offset, size, found = 0, config.get('pageSize', 50), set()
99
+ while True:
100
+ rows = database.get_messages(chat, limit=size, offset=offset)
101
+ if offset == 0 and (not rows or max(int(row['sort_seq']) for row in rows) < until[0]):
102
+ raise SnapshotBusy('群消息快照落后于已观察位置,保留未读等待重读')
103
+ for row in rows:
104
+ order = messageOrder(row)
105
+ if after < order <= until and row['type'] != '系统消息':
106
+ if not row.get('sender_username'): raise ValueError('群动态缺少真实发言人')
107
+ if row['sender_username'] != config['selfId']: found.add(order)
108
+ if len(rows) < size or any(int(row['sort_seq']) < after[0] for row in rows): return len(found)
109
+ offset += len(rows)