polygram 0.23.2 → 0.24.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.
@@ -108,7 +108,19 @@ function createStreamer({
108
108
  // to instrumentation for the visible bubble-shape change.
109
109
  onRichUpgrade = () => {},
110
110
  } = {}) {
111
- throttleMs = Math.max(THROTTLE_FLOOR_MS, throttleMs);
111
+ // throttleMs may be a number OR a function evaluated per edit. Telegram's
112
+ // rate limits are per CHAT, but a streamer is per turn — several forum topics
113
+ // in one chat each edit at their own cadence and share one budget. A caller
114
+ // that knows how many previews are live in the chat right now passes a
115
+ // function so the cadence scales as topics come and go mid-turn.
116
+ const fixedThrottleMs = typeof throttleMs === 'function'
117
+ ? null
118
+ : Math.max(THROTTLE_FLOOR_MS, throttleMs);
119
+ function currentThrottleMs() {
120
+ if (fixedThrottleMs != null) return fixedThrottleMs;
121
+ const n = Number(throttleMs());
122
+ return Math.max(THROTTLE_FLOOR_MS, Number.isFinite(n) && n > 0 ? n : DEFAULT_THROTTLE_MS);
123
+ }
112
124
  let state = 'idle'; // 'idle' | 'live' | 'finalized'
113
125
  let msgId = null;
114
126
  let hadReplyAnchor = false;
@@ -225,7 +237,7 @@ function createStreamer({
225
237
  const now = clock();
226
238
  const elapsed = now - lastEditTs;
227
239
  if (pendingEdit) return; // already queued
228
- const delay = Math.max(0, throttleMs - elapsed);
240
+ const delay = Math.max(0, currentThrottleMs() - elapsed);
229
241
  pendingEdit = schedule(flush, delay);
230
242
  }
231
243
 
@@ -343,6 +355,23 @@ function createStreamer({
343
355
  return sealChain;
344
356
  }
345
357
 
358
+ // The initial send is what turns the bubble from an intention into a
359
+ // message_id. onChunk flips to 'live' synchronously but assigns msgId only
360
+ // once that send resolves, so anything acting on the bubble in between —
361
+ // a final edit, a drain, a delete — would target msgId=null and the message
362
+ // that lands a moment later becomes an unreachable orphan in the chat.
363
+ // Everything that touches the bubble waits here first.
364
+ async function awaitInitialSend() {
365
+ while (initialSendPromise) {
366
+ const pending = initialSendPromise;
367
+ try { await pending; } catch { /* onChunk owns the failure path */ }
368
+ // onChunk's own continuation clears the field (it was registered on the
369
+ // promise first, so it has already committed msgId by now). If it somehow
370
+ // hasn't, stop rather than spin.
371
+ if (initialSendPromise === pending) return;
372
+ }
373
+ }
374
+
346
375
  async function drainSeals() {
347
376
  while (true) {
348
377
  const pending = sealChain;
@@ -395,6 +424,7 @@ function createStreamer({
395
424
  // visual state to be accurate (no stale half-rendered text under a
396
425
  // pending timer).
397
426
  async function flushDraft() {
427
+ await awaitInitialSend();
398
428
  if (pendingEdit) { cancel(pendingEdit); pendingEdit = null; await flush(); }
399
429
  if (flushPromise) { try { await flushPromise; } catch {} }
400
430
  await drainSeals();
@@ -479,6 +509,7 @@ function createStreamer({
479
509
  // state without touching Telegram — the bubble stays at its last
480
510
  // edited content, becoming a vestigial "head" of the conversation.
481
511
  async function discard() {
512
+ await awaitInitialSend();
482
513
  if (pendingEdit) { cancel(pendingEdit); pendingEdit = null; }
483
514
  if (flushPromise) { try { await flushPromise; } catch {} }
484
515
  await drainSeals();
@@ -513,6 +544,11 @@ function createStreamer({
513
544
  // discard + deliverReplies (false). `overflow` is the one specific
514
545
  // reason: body wouldn't fit in a single Telegram message.
515
546
  async function finalize(finalText, { errorSuffix = null } = {}) {
547
+ if (state === 'finalized') return { streamed: false, msgId, finalEditOk: false, overflow: false };
548
+ // An in-flight send can still resolve the bubble into existence (or fail
549
+ // and drop us back to 'idle'), and another caller can finalize while we
550
+ // wait — hence the re-check on the far side.
551
+ await awaitInitialSend();
516
552
  if (state === 'finalized') return { streamed: false, msgId, finalEditOk: false, overflow: false };
517
553
  if (pendingEdit) { cancel(pendingEdit); pendingEdit = null; }
518
554
  if (flushPromise) { try { await flushPromise; } catch {} }
@@ -628,6 +664,11 @@ function createStreamer({
628
664
  // Introspection for tests:
629
665
  get state() { return state; },
630
666
  get msgId() { return msgId; },
667
+ // The most recent text the streamer was told about, which is NOT always
668
+ // what's on screen (a throttled edit may still be pending) and NOT always
669
+ // what a reply delivered. A caller reconciling a live preview at turn end
670
+ // needs it to decide whether the draft still holds undelivered content.
671
+ get latestText() { return latestText; },
631
672
  get currentText() { return currentText; },
632
673
  get isRichMode() { return currentRichJson !== null; },
633
674
  get currentRichBlocks() { return currentRichJson ? JSON.parse(currentRichJson) : null; },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polygram",
3
- "version": "0.23.2",
3
+ "version": "0.24.0",
4
4
  "description": "Telegram daemon for Claude Code that preserves the OpenClaw per-chat session model. Migration path for OpenClaw users moving to Claude Code.",
5
5
  "main": "lib/ipc/client.js",
6
6
  "bin": {
@@ -63,7 +63,7 @@
63
63
  "dependencies": {
64
64
  "@anthropic-ai/claude-agent-sdk": "0.2.123",
65
65
  "@modelcontextprotocol/sdk": "^1.29.0",
66
- "@shumkov/orchestra": "0.7.3",
66
+ "@shumkov/orchestra": "0.8.0",
67
67
  "better-sqlite3": "^12.9.0",
68
68
  "grammy": "^1.42.0",
69
69
  "markdown-it": "^14.1.1",