claude-rpc 1.4.0 → 1.4.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-rpc",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "Discord Rich Presence for Claude Code — live model, project, tokens, and lifetime stats driven by Claude Code's hook system.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/daemon.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import './safe-stdio.js';
3
3
  import { existsSync, unlinkSync, watch, appendFileSync, mkdirSync, statSync, renameSync, readdirSync } from 'node:fs';
4
4
  import { basename, dirname, join } from 'node:path';
5
- import { Client } from './discord-ipc.js';
5
+ import { Client, isBridgeUser } from './discord-ipc.js';
6
6
  import { readState, sweepStaleStateTmp, listSessionStates, sweepStaleSessionStates } from './state.js';
7
7
  import { makeRotationCursor, pickFrames, selectFrame, resolveLargeImageKey, shouldShowGithubButton, pickActiveSession, throttleDecision } from './presence.js';
8
8
  import { buildVars, fillTemplate, framePasses, applyIdle, applyShipped, applyTrigger } from './format.js';
@@ -110,6 +110,11 @@ let claudeProcAlive = null;
110
110
  let client = null;
111
111
  let connected = false;
112
112
  let connecting = false; // login() in flight — see the watchdog note in connect()
113
+ // True when the READY handshake identified an arRPC-style bridge (Vesktop/
114
+ // Equibop/web client) rather than the real Discord desktop client — see
115
+ // isBridgeUser in discord-ipc.js. Bridges never replay the current activity
116
+ // after their web client blips, so the keepalive below runs tighter there.
117
+ let bridgeClient = false;
113
118
  // ── Outbound presence throttle ──────────────────────────────────────────────
114
119
  // Discord rate-limits SET_ACTIVITY hard (~5 writes per 20s); a burst past it
115
120
  // makes the client blank the presence until the writes stop — the actual
@@ -491,6 +496,25 @@ function maxActivityWrites() {
491
496
  return Math.max(2, config.maxActivityWrites || 4);
492
497
  }
493
498
 
499
+ // Presence keepalive — after this much wire silence, re-send the current frame
500
+ // even though it hasn't changed. The real Discord client holds an activity
501
+ // until told otherwise, so 60s there is pure insurance; arRPC-style bridges
502
+ // lose the activity whenever their web client blips and never replay it, so
503
+ // the interval tightens once the handshake identifies one (issue #37 — card
504
+ // gone on Vesktop while every write is acked). config.presenceKeepaliveSec
505
+ // overrides (floored at 15s); 0/false disables. Costs one SET_ACTIVITY per
506
+ // interval at most — far under the 4-per-20s window cap.
507
+ const KEEPALIVE_DESKTOP_SEC = 60;
508
+ const KEEPALIVE_BRIDGE_SEC = 20;
509
+ function presenceKeepaliveMs() {
510
+ const v = config.presenceKeepaliveSec;
511
+ if (v === 0 || v === false) return 0;
512
+ const sec = typeof v === 'number' && v > 0
513
+ ? Math.max(15, v)
514
+ : (bridgeClient ? KEEPALIVE_BRIDGE_SEC : KEEPALIVE_DESKTOP_SEC);
515
+ return sec * 1000;
516
+ }
517
+
494
518
  // Sentinel for a clear — can never collide with a real activity hash, since
495
519
  // JSON.stringify of an activity object always begins with '{'.
496
520
  const CLEAR_HASH = 'cleared';
@@ -500,12 +524,18 @@ const CLEAR_HASH = 'cleared';
500
524
  // timer; 'skip' means the wire already shows this exact frame.
501
525
  function transmit(kind, activity, logMsg) {
502
526
  const hash = kind === 'clear' ? CLEAR_HASH : JSON.stringify(activity);
527
+ // Keepalive only re-asserts a VISIBLE frame — re-writing an already-cleared
528
+ // presence would burn rate-limited writes on a no-op, all night, every night.
529
+ const keepaliveMs = kind === 'clear' ? 0 : presenceKeepaliveMs();
530
+ const reassert = hash === lastSentHash; // only possible via keepalive expiry
503
531
  const d = throttleDecision({
504
532
  hash, lastSentHash, lastSentAt,
505
533
  now: Date.now(), gapMs: activityGapMs(), flushPending: !!flushTimer,
506
534
  recentSends, windowMs: activityWindowMs(), maxPerWindow: maxActivityWrites(),
535
+ keepaliveMs,
507
536
  });
508
537
  if (d.action === 'skip') { pendingSend = null; return; }
538
+ if (reassert) logMsg = `${logMsg} · keepalive re-assert`;
509
539
  if (d.action === 'send') { doSend({ kind, activity, hash, logMsg }); return; }
510
540
  // defer — remember the LATEST payload and flush it once when the gap expires.
511
541
  pendingSend = { kind, activity, hash, logMsg };
@@ -593,7 +623,15 @@ async function connect() {
593
623
  connected = true;
594
624
  // Reset backoff so the next outage also starts at RECONNECT_BASE_MS.
595
625
  reconnectDelayMs = RECONNECT_BASE_MS;
596
- log('Discord RPC connected as', client.user?.username);
626
+ bridgeClient = isBridgeUser(client.user);
627
+ if (bridgeClient) {
628
+ // Doctor greps this line: "connected as" feeds ipcStateFromLog, "arrpc"
629
+ // feeds bridgeFromLog — keep both tokens if rewording.
630
+ log(`Discord RPC connected as ${client.user?.username} — arRPC-style bridge (Vesktop/Equibop/web client). ` +
631
+ `Bridges don't replay presence after their web client reconnects; keepalive re-asserts every ${Math.round(presenceKeepaliveMs() / 1000)}s.`);
632
+ } else {
633
+ log('Discord RPC connected as', client.user?.username);
634
+ }
597
635
  resetTransmit();
598
636
  pushPresence();
599
637
  });
@@ -39,6 +39,16 @@ export const DEFAULT_CONFIG = {
39
39
  // headroom under Discord's ~5-per-20s ceiling.
40
40
  maxActivityWrites: 4,
41
41
  activityWindowMs: 20000,
42
+ // Presence keepalive (seconds). A frame that hasn't changed is normally
43
+ // never re-sent — correct against Discord desktop, which holds an activity
44
+ // until told otherwise. arRPC-style bridges (Vesktop/Equibop/web clients)
45
+ // instead LOSE the activity whenever their renderer blips (background
46
+ // throttling of an unfocused window, a reload) and never replay it, leaving
47
+ // the card blank while the daemon's writes are still acked. After this much
48
+ // write silence the daemon re-asserts the current frame. null = auto: 60s
49
+ // against Discord desktop, 20s when the handshake identifies a bridge.
50
+ // Floored at 15; set 0 to disable.
51
+ presenceKeepaliveSec: null,
42
52
  rescanIntervalSec: 300,
43
53
  idleThresholdSec: 60,
44
54
  // Time (minutes) of no hook activity AND no live transcripts on disk before
@@ -73,6 +73,21 @@ export function candidatePaths(platform = process.platform) {
73
73
  return out;
74
74
  }
75
75
 
76
+ // arRPC — the open-source RPC bridge that Vesktop/Equibop bundle and web-client
77
+ // setups run standalone — answers the handshake READY with a mock user instead
78
+ // of the real account: id 1045800378228281345, username "arrpc". Detecting it
79
+ // matters because bridges do NOT replay the current activity when their web
80
+ // client reconnects (arRPC keeps no last-activity state), so presence silently
81
+ // vanishes on any renderer blip — background throttling of an unfocused window,
82
+ // a reload — until the daemon happens to write a *different* frame. The daemon
83
+ // tightens its keepalive re-assert when this returns true.
84
+ export const ARRPC_USER_ID = '1045800378228281345';
85
+ export function isBridgeUser(user) {
86
+ if (!user) return false;
87
+ if (String(user.id || '') === ARRPC_USER_ID) return true;
88
+ return /^arrpc$/i.test(String(user.username || ''));
89
+ }
90
+
76
91
  // Encode one IPC frame: 8-byte little-endian header + JSON body.
77
92
  export function encodeFrame(op, data) {
78
93
  const body = data === undefined ? Buffer.alloc(0) : Buffer.from(JSON.stringify(data));
package/src/doctor.js CHANGED
@@ -134,6 +134,21 @@ export function ipcStateFromLog(logText) {
134
134
  return ipc;
135
135
  }
136
136
 
137
+ // Which client answered the RPC handshake, from the most recent "connected as"
138
+ // line: 'arrpc' for an arRPC-style bridge (Vesktop/Equibop/web client — the
139
+ // bridge's mock user is literally named arrpc), 'discord' for the real desktop
140
+ // client, null when the log has no connect line. Most-recent-wins, like
141
+ // ipcStateFromLog — a user who switches from Vesktop to stock Discord mid-run
142
+ // should not keep the bridge caveat.
143
+ export function bridgeFromLog(logText) {
144
+ let kind = null;
145
+ for (const line of String(logText || '').split('\n').slice(-200)) {
146
+ const m = line.match(/Discord RPC connected as\s+(\S+)/i);
147
+ if (m) kind = /arrpc/i.test(m[1]) ? 'arrpc' : 'discord';
148
+ }
149
+ return kind;
150
+ }
151
+
137
152
  function checkConfig() {
138
153
  if (!existsSync(CONFIG_PATH)) {
139
154
  check('config.json present', 'fail', CONFIG_PATH,
@@ -310,11 +325,25 @@ function checkDaemonLog() {
310
325
  // Ns" / "login failed" / "disconnected" line means it dropped. Whichever
311
326
  // happened most recently wins.
312
327
  let ipc = 'unknown';
313
- try { ipc = ipcStateFromLog(readFileSync(LOG_PATH, 'utf8')); }
314
- catch { /* log unreadable — ipc stays 'unknown', warn renders */ }
328
+ let bridge = null;
329
+ try {
330
+ const logText = readFileSync(LOG_PATH, 'utf8');
331
+ ipc = ipcStateFromLog(logText);
332
+ bridge = bridgeFromLog(logText);
333
+ } catch { /* log unreadable — ipc stays 'unknown', warn renders */ }
315
334
  if (ipc === 'up') {
316
335
  check('discord IPC connection', 'pass',
317
336
  `connected · ${sizeKB} KB log · last write ${ageMin.toFixed(1)} min ago`);
337
+ if (bridge === 'arrpc') {
338
+ // The single most-reported "presence vanished but doctor is green" cause:
339
+ // an arRPC bridge acks every write on the socket, but loses the activity
340
+ // whenever its web client blips (unfocused window throttled, reload) and
341
+ // never replays it. The daemon's keepalive re-asserts within ~20s of the
342
+ // client being able to display again — but while the renderer is down,
343
+ // nothing any RPC app writes will show.
344
+ check('discord client', 'info', 'arRPC bridge (Vesktop/Equibop/web) — not Discord desktop',
345
+ 'bridges drop presence while their window is unfocused/reloading; the daemon re-asserts it every ~20s. if the card stays gone, focus the client window — or use Discord desktop');
346
+ }
318
347
  } else if (ipc === 'down') {
319
348
  check('discord IPC connection', 'warn', 'daemon is reconnecting to Discord',
320
349
  'is the discord desktop client running? rpc only works via desktop, not browser', 'discord');
package/src/presence.js CHANGED
@@ -130,8 +130,21 @@ export function shouldShowGithubButton(p, state) {
130
130
  // in the window expires, guaranteeing no more than `maxPerWindow` writes in any
131
131
  // `windowMs` no matter how many triggers fire. Omitting them keeps the old
132
132
  // gap-only behavior (used by tests that predate the cap).
133
- export function throttleDecision({ hash, lastSentHash, lastSentAt, now, gapMs, flushPending, recentSends, windowMs, maxPerWindow }) {
134
- if (hash === lastSentHash) return { action: 'skip', waitMs: 0 };
133
+ //
134
+ // `keepaliveMs` (optional) re-opens the identical-hash skip after that much
135
+ // wire silence. Against the real Discord client an unchanged frame never needs
136
+ // re-sending — the client holds an activity until told otherwise — but arRPC-
137
+ // style bridges (Vesktop/Equibop/web) lose the activity whenever their web
138
+ // client blips and never replay it (arRPC keeps no last-activity state), so a
139
+ // quiet single-frame status would stay invisible forever while every doctor
140
+ // check reads green (#37). Once `now - lastSentAt >= keepaliveMs` the frame is
141
+ // treated as changed and flows through the same gap/window limiting as any
142
+ // other write. Omitted/0 keeps the pure skip.
143
+ export function throttleDecision({ hash, lastSentHash, lastSentAt, now, gapMs, flushPending, recentSends, windowMs, maxPerWindow, keepaliveMs }) {
144
+ if (hash === lastSentHash) {
145
+ const due = keepaliveMs > 0 && lastSentAt > 0 && now - lastSentAt >= keepaliveMs;
146
+ if (!due) return { action: 'skip', waitMs: 0 };
147
+ }
135
148
  let waitMs = Math.max(0, (lastSentAt || 0) + gapMs - now);
136
149
  if (maxPerWindow && windowMs && Array.isArray(recentSends)) {
137
150
  // Strict window (age < windowMs) so a write exactly windowMs old has already
package/src/version.js CHANGED
@@ -11,7 +11,7 @@ import { readFileSync } from 'node:fs';
11
11
  import { join } from 'node:path';
12
12
  import { ROOT } from './paths.js';
13
13
 
14
- const BAKED = '1.4.0';
14
+ const BAKED = '1.4.1';
15
15
 
16
16
  function readPkgVersion() {
17
17
  try {