@lethevimlet/sshift 1.7.2 → 1.7.4
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 +1 -1
- package/plugins/claude-attention/index.js +389 -187
- package/plugins/claude-attention/package.json +3 -3
- package/plugins/opencode-attention/index.js +373 -184
- package/plugins/opencode-attention/package.json +3 -3
- package/src/server/endpoints/ws/ssh.js +14 -0
- package/src/server/plugins/plugin-manager.js +33 -1
- package/src/server/services/ssh-manager.js +31 -0
- package/src/webapp/css/style.css +16 -12
- package/src/webapp/js/app.js +47 -0
|
@@ -1,263 +1,472 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* claude-attention Plugin
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Flashes the tab when Claude Code (https://claude.ai/claude-code) finishes
|
|
5
|
+
* working and is waiting for the user (reply complete, or a permission
|
|
6
|
+
* dialog like "Do you want to make this edit?").
|
|
6
7
|
*
|
|
7
|
-
* Detection strategy:
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
8
|
+
* Detection strategy (state-transition based, not text-pattern based):
|
|
9
|
+
*
|
|
10
|
+
* 1. App detection is NON-STICKY: Claude Code is considered present only
|
|
11
|
+
* while one of its UI signatures is visible in the CURRENT viewport of
|
|
12
|
+
* the headless terminal ("Claude", the "? for shortcuts" footer hint,
|
|
13
|
+
* "esc to interrupt", the ⎿ tool-result marker, edit-mode hints...).
|
|
14
|
+
* When the user exits back to a shell the signatures disappear and,
|
|
15
|
+
* after a few misses, no NEW working episode can start — so a shell
|
|
16
|
+
* prompt can never arm the engine.
|
|
17
|
+
*
|
|
18
|
+
* 2. "Working" is tracked from two signals:
|
|
19
|
+
* - working indicators visible in the viewport footer (braille
|
|
20
|
+
* spinner glyphs, the ✢✳✶✻✽ sparkle spinner frames, and the
|
|
21
|
+
* "esc to interrupt" hint Claude Code shows while running), and
|
|
22
|
+
* - sustained agent-driven output: bytes that arrive while the user
|
|
23
|
+
* has NOT typed recently (echo/UI responses to keystrokes are
|
|
24
|
+
* ignored via userEchoWindowMs).
|
|
25
|
+
* NOTE: "·" is deliberately NOT a spinner char — it appears in every
|
|
26
|
+
* status line ("3.2k tokens · esc to interrupt").
|
|
27
|
+
*
|
|
28
|
+
* 3. The tab flashes ONLY on the working → idle TRANSITION: a working
|
|
29
|
+
* episode was observed, the working indicator is gone, and no work
|
|
30
|
+
* signal has been seen for idleThreshold ms. A finished reply and a
|
|
31
|
+
* permission dialog both look like this transition, so no brittle
|
|
32
|
+
* prompt regexes ("❯", "Do you want", "allow", ...) are needed.
|
|
33
|
+
*
|
|
34
|
+
* 4. User input (keystrokes) ends the current working episode, stops an
|
|
35
|
+
* active flash and suppresses flashing for cooldownMs — the user is
|
|
36
|
+
* already there. A genuinely running agent re-arms the episode with
|
|
37
|
+
* its very next spinner frame, so nothing is lost.
|
|
38
|
+
*
|
|
39
|
+
* Things that made v1.7.3 miss most "waiting for input" events, and how
|
|
40
|
+
* they are handled now:
|
|
41
|
+
*
|
|
42
|
+
* - The viewport was read through getTerminalState(), which serializes
|
|
43
|
+
* the whole 10k-line scrollback and returns NULL past its 1MB guard.
|
|
44
|
+
* Busy agent sessions — exactly the ones worth watching — went blind.
|
|
45
|
+
* Now it reads getTerminalViewport() (visible screen only) and follows
|
|
46
|
+
* the alternate-screen marker when a full-screen TUI is active.
|
|
47
|
+
* - A transition that landed inside the post-keystroke cooldown was
|
|
48
|
+
* dropped and never retried. The engine now keeps a pendingAttention
|
|
49
|
+
* flag and retries on every check until the flash actually goes out.
|
|
50
|
+
* - Losing the app signature (a permission dialog can cover the footer
|
|
51
|
+
* hints) wiped the pending episode. App misses now only stop NEW
|
|
52
|
+
* episodes from starting; an armed one still gets to flash.
|
|
53
|
+
* - A flash the user dismissed by simply looking at the tab left the
|
|
54
|
+
* server stuck in "already flashing", swallowing every later event.
|
|
55
|
+
* Clients now report onSessionFocus, and a fresh transition re-emits
|
|
56
|
+
* the flash anyway.
|
|
57
|
+
*
|
|
58
|
+
* There are NO default attention text patterns: strings like "(y/n)",
|
|
59
|
+
* "allow" or "❯ " routinely appear inside chat prose, code and shell
|
|
60
|
+
* prompts and caused constant false flashes. Custom `patterns` from the
|
|
61
|
+
* config are still honored, but they are only evaluated while Claude is on
|
|
62
|
+
* screen and not working.
|
|
16
63
|
*
|
|
17
64
|
* Configuration (in config.json under plugins[].config):
|
|
18
|
-
* -
|
|
19
|
-
* -
|
|
20
|
-
* -
|
|
21
|
-
*
|
|
22
|
-
* -
|
|
23
|
-
*
|
|
24
|
-
* -
|
|
65
|
+
* - flashDuration: number - Flash duration in ms, 0 = flash until focused (default: 0)
|
|
66
|
+
* - checkInterval: number - Milliseconds between periodic viewport checks (default: 2000)
|
|
67
|
+
* - idleThreshold: number - Milliseconds without a work signal before Claude
|
|
68
|
+
* is considered idle/finished (default: 2500)
|
|
69
|
+
* - cooldownMs: number - Milliseconds to suppress flashing after user input
|
|
70
|
+
* or after a flash stops (default: 3000)
|
|
71
|
+
* - debounceMs: number - Debounce for output-driven evaluations (default: 300)
|
|
72
|
+
* - footerLines: number - Viewport bottom lines scanned for working
|
|
73
|
+
* indicators / custom patterns (default: 8)
|
|
74
|
+
* - userEchoWindowMs: number - Output arriving within this window after a
|
|
75
|
+
* keystroke counts as echo, not agent work (default: 1200)
|
|
76
|
+
* - minWorkBytes: number - Agent-driven bytes within workWindowMs that
|
|
77
|
+
* mark the session as working (default: 600)
|
|
78
|
+
* - workWindowMs: number - Rolling window for minWorkBytes (default: 2000)
|
|
79
|
+
* - appMissLimit: number - Consecutive viewport checks without an app
|
|
80
|
+
* signature before new episodes are blocked (default: 3)
|
|
81
|
+
* - appPatterns: string[] - Regexes replacing the default app signatures
|
|
82
|
+
* - spinnerStaleMs: number - A bare spinner glyph only counts as "working" if
|
|
83
|
+
* output arrived within this window (default: 2000)
|
|
84
|
+
* - workStaleMs: number - A screen with no output at all for this long is
|
|
85
|
+
* never considered working (default: 60000)
|
|
86
|
+
* - workingPatterns: string[] - Regexes replacing the default working indicators
|
|
87
|
+
* - workingHintPatterns: string[] - Regexes for run hints that count as working
|
|
88
|
+
* even on a quiet screen (default: "esc to interrupt")
|
|
89
|
+
* - patterns: string[] - Extra attention regexes (footer lines, idle only)
|
|
90
|
+
* - excludePatterns: string[] - Remove patterns (by source) from all lists
|
|
25
91
|
*/
|
|
26
92
|
|
|
27
93
|
class ClaudeAttentionPlugin {
|
|
28
94
|
constructor(ctx, config = {}) {
|
|
29
95
|
this.ctx = ctx;
|
|
30
96
|
this.config = {
|
|
31
|
-
debounceMs: config.debounceMs || 300,
|
|
32
97
|
flashDuration: config.flashDuration || 0,
|
|
33
98
|
checkInterval: config.checkInterval || 2000,
|
|
34
|
-
idleThreshold: config.idleThreshold ||
|
|
35
|
-
cooldownMs: config.cooldownMs ||
|
|
36
|
-
|
|
99
|
+
idleThreshold: config.idleThreshold || 2500,
|
|
100
|
+
cooldownMs: config.cooldownMs || 3000,
|
|
101
|
+
debounceMs: config.debounceMs || 300,
|
|
102
|
+
footerLines: config.footerLines || 8,
|
|
103
|
+
userEchoWindowMs: config.userEchoWindowMs || 1200,
|
|
104
|
+
minWorkBytes: config.minWorkBytes || 600,
|
|
105
|
+
workWindowMs: config.workWindowMs || 2000,
|
|
106
|
+
appMissLimit: config.appMissLimit || 3,
|
|
107
|
+
spinnerStaleMs: config.spinnerStaleMs || 2000,
|
|
108
|
+
workStaleMs: config.workStaleMs || 60000,
|
|
37
109
|
};
|
|
38
110
|
|
|
39
|
-
|
|
111
|
+
// App signatures. Claude Code's welcome box scrolls away in long
|
|
112
|
+
// sessions, so alongside the case-SENSITIVE "Claude" wordmark (keeps
|
|
113
|
+
// lowercase shell text like `cd claude-project` from arming the
|
|
114
|
+
// engine) we accept the markers that survive on screen: the footer
|
|
115
|
+
// hints, the mode indicators, the ⎿ tool-result glyph and the
|
|
116
|
+
// permission-dialog headline/choices (a dialog can push every other
|
|
117
|
+
// hint off the viewport, which is precisely when we must flash).
|
|
118
|
+
// A false positive here only ARMS the engine; a flash still requires
|
|
119
|
+
// a real working → idle transition.
|
|
120
|
+
this._appPatterns = this._compileList(
|
|
121
|
+
config.appPatterns || [
|
|
122
|
+
'Claude',
|
|
123
|
+
'\\? for shortcuts',
|
|
124
|
+
'esc to interrupt',
|
|
125
|
+
'esc to cancel',
|
|
126
|
+
'accept edits',
|
|
127
|
+
'plan mode',
|
|
128
|
+
'bypass permissions',
|
|
129
|
+
'shift\\+tab to cycle',
|
|
130
|
+
'auto-compact',
|
|
131
|
+
'⎿',
|
|
132
|
+
'⏵⏵',
|
|
133
|
+
'Do you want',
|
|
134
|
+
'❯ \\d+\\.',
|
|
135
|
+
],
|
|
136
|
+
'appPatterns',
|
|
137
|
+
'' // case-sensitive
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
// Working indicators, scanned over the viewport footer only:
|
|
141
|
+
// - the full braille block (U+2800–U+28FF) covers braille spinners,
|
|
142
|
+
// - ✢✳✶✻✽∗ are Claude Code's sparkle spinner frames,
|
|
143
|
+
// - ◐◓◑◒◜◝◞◟ cover the arc/circle spinners used by some tools,
|
|
144
|
+
// - "esc to interrupt" is shown for the whole duration of a run.
|
|
145
|
+
this._workingPatterns = this._compileList(
|
|
146
|
+
config.workingPatterns || [
|
|
147
|
+
'[\\u2800-\\u28FF]',
|
|
148
|
+
'[✢✳✶✻✽∗]',
|
|
149
|
+
'[◐◓◑◒◜◝◞◟]',
|
|
150
|
+
'esc to interrupt',
|
|
151
|
+
],
|
|
152
|
+
'workingPatterns'
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
// Text hints that stay on screen for the WHOLE run ("esc to
|
|
156
|
+
// interrupt"). Unlike a spinner glyph these are unambiguous, so they
|
|
157
|
+
// count as "working" even when the screen is completely quiet.
|
|
158
|
+
this._workingHintPatterns = this._compileList(
|
|
159
|
+
config.workingHintPatterns || [
|
|
160
|
+
'esc to interrupt',
|
|
161
|
+
],
|
|
162
|
+
'workingHintPatterns'
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
// Optional user-supplied attention patterns (none by default).
|
|
166
|
+
this._attentionPatterns = this._compileList(config.patterns || [], 'patterns');
|
|
167
|
+
|
|
168
|
+
// excludePatterns removes entries (matched by regex source) from all lists.
|
|
169
|
+
for (const p of (config.excludePatterns || [])) {
|
|
170
|
+
try {
|
|
171
|
+
const source = new RegExp(p).source;
|
|
172
|
+
this._appPatterns = this._appPatterns.filter(r => r.source !== source);
|
|
173
|
+
this._workingPatterns = this._workingPatterns.filter(r => r.source !== source);
|
|
174
|
+
this._workingHintPatterns = this._workingHintPatterns.filter(r => r.source !== source);
|
|
175
|
+
this._attentionPatterns = this._attentionPatterns.filter(r => r.source !== source);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
console.error(`[claude-attention] Invalid exclude pattern "${p}":`, e.message);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
40
180
|
|
|
41
|
-
this.
|
|
181
|
+
this._sessionState = new Map();
|
|
42
182
|
this._checkTimers = new Map();
|
|
183
|
+
this._dataTimers = new Map();
|
|
184
|
+
this._idleTimers = new Map();
|
|
43
185
|
this._flashing = new Map();
|
|
44
|
-
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
_compileList(list, label, flags = 'i') {
|
|
189
|
+
const out = [];
|
|
190
|
+
for (const p of list) {
|
|
191
|
+
try { out.push(new RegExp(p, flags)); } catch (e) {
|
|
192
|
+
console.error(`[claude-attention] Invalid ${label} regex "${p}":`, e.message);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return out;
|
|
45
196
|
}
|
|
46
197
|
|
|
47
198
|
_getSessionState(sessionId) {
|
|
48
199
|
if (!this._sessionState.has(sessionId)) {
|
|
49
200
|
this._sessionState.set(sessionId, {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
201
|
+
appVisible: false, // signature currently on screen
|
|
202
|
+
appMissStreak: 0, // consecutive checks without a signature
|
|
203
|
+
working: false, // working indicator seen recently
|
|
204
|
+
workEpisode: false, // a working episode is armed (not yet consumed)
|
|
205
|
+
pendingAttention: false, // wants to flash, hasn't managed to yet
|
|
206
|
+
lastWorkSignalAt: 0, // last time any work signal was seen
|
|
207
|
+
lastUserInputAt: 0, // last user keystroke
|
|
208
|
+
lastOutputAt: 0, // last output of any kind
|
|
209
|
+
agentBytes: 0, // agent-driven bytes in the rolling window
|
|
210
|
+
agentWindowStart: 0,
|
|
53
211
|
cooldownUntil: 0,
|
|
54
|
-
idleCheckScheduled: false,
|
|
55
212
|
});
|
|
56
213
|
}
|
|
57
214
|
return this._sessionState.get(sessionId);
|
|
58
215
|
}
|
|
59
216
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
217
|
+
_stripAnsi(str) {
|
|
218
|
+
return str
|
|
219
|
+
.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, '')
|
|
220
|
+
.replace(/\x1b\][^\x07]*\x07/g, '')
|
|
221
|
+
.replace(/\x1b\[\?[0-9]+[hl]/g, '')
|
|
222
|
+
.replace(/\x1b[\(\)]B/g, '')
|
|
223
|
+
.replace(/\x1b[0-9;]*[a-zA-Z]/g, '')
|
|
224
|
+
.replace(/\x1b[^[\]()?P]/g, '')
|
|
225
|
+
.replace(/[\x00-\x08\x0b\x0c\x0d\x0e-\x1f\x7f]/g, '');
|
|
63
226
|
}
|
|
64
227
|
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
/\> \x1b/,
|
|
69
|
-
/❯ /,
|
|
70
|
-
/waiting for input/i,
|
|
71
|
-
/press enter/i,
|
|
72
|
-
/press any key/i,
|
|
73
|
-
/confirm\s*\?/i,
|
|
74
|
-
/\(y\/n\)/i,
|
|
75
|
-
/\(yes\/no\)/i,
|
|
76
|
-
/\[Y\/n\]/i,
|
|
77
|
-
/\[y\/N\]/i,
|
|
78
|
-
/do you want/i,
|
|
79
|
-
/allow/i,
|
|
80
|
-
/Esc to cancel/i,
|
|
81
|
-
];
|
|
82
|
-
|
|
83
|
-
let patterns = [...defaultPatterns];
|
|
84
|
-
|
|
85
|
-
for (const p of extraPatterns) {
|
|
86
|
-
try { patterns.push(new RegExp(p)); } catch (e) {
|
|
87
|
-
console.error(`[claude-attention] Invalid pattern "${p}":`, e.message);
|
|
88
|
-
}
|
|
228
|
+
_matchesAny(patterns, text) {
|
|
229
|
+
for (const p of patterns) {
|
|
230
|
+
if (p.test(text)) return true;
|
|
89
231
|
}
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
90
234
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
235
|
+
// Extract the current viewport, ANSI-stripped, one entry per screen line.
|
|
236
|
+
//
|
|
237
|
+
// Prefers the cheap viewport-only serialization; getTerminalState() is a
|
|
238
|
+
// fallback for older hosts (it serializes the entire scrollback and
|
|
239
|
+
// returns null once that exceeds its size guard).
|
|
240
|
+
_getViewportLines(sessionId) {
|
|
241
|
+
let state = null;
|
|
242
|
+
if (typeof this.ctx.getTerminalViewport === 'function') {
|
|
243
|
+
state = this.ctx.getTerminalViewport(sessionId);
|
|
244
|
+
}
|
|
245
|
+
if (!state || !state.state) {
|
|
246
|
+
state = this.ctx.getTerminalState(sessionId);
|
|
98
247
|
}
|
|
248
|
+
if (!state || !state.state) return null;
|
|
99
249
|
|
|
100
|
-
|
|
101
|
-
|
|
250
|
+
const rows = state.rows || 24;
|
|
251
|
+
let text = state.state;
|
|
102
252
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
253
|
+
// Full-screen TUIs run on the alternate buffer, which the serializer
|
|
254
|
+
// appends after a `\x1b[?1049h` marker — everything before it is the
|
|
255
|
+
// scrolled-away normal buffer, not what the user is looking at.
|
|
256
|
+
const altMarker = text.lastIndexOf('\x1b[?1049h');
|
|
257
|
+
if (altMarker !== -1) {
|
|
258
|
+
text = text.slice(altMarker);
|
|
106
259
|
}
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
260
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
.replace(/\x1b\][^\x07]*\x07/g, '')
|
|
114
|
-
.replace(/\x1b\[\?[0-9]+[hl]/g, '')
|
|
115
|
-
.replace(/\x1b[\(\)]B/g, '')
|
|
116
|
-
.replace(/\x1b[0-9;]*[a-zA-Z]/g, '')
|
|
117
|
-
.replace(/\x1b[^[\]()?P]/g, '')
|
|
118
|
-
.replace(/[\x00-\x08\x0b\x0c\x0d\x0e-\x1f\x7f]/g, '')
|
|
119
|
-
.trim();
|
|
261
|
+
const lines = text.split('\n');
|
|
262
|
+
const viewport = lines.slice(-rows);
|
|
263
|
+
return viewport.map(l => this._stripAnsi(l));
|
|
120
264
|
}
|
|
121
265
|
|
|
122
|
-
_startFlash(sessionId) {
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
266
|
+
_startFlash(sessionId, { force = false } = {}) {
|
|
267
|
+
const s = this._getSessionState(sessionId);
|
|
268
|
+
const now = Date.now();
|
|
269
|
+
// Suppressed for now — pendingAttention keeps it queued for a retry
|
|
270
|
+
// instead of dropping the event on the floor.
|
|
271
|
+
if (now < s.cooldownUntil) return;
|
|
272
|
+
if (now - s.lastUserInputAt < this.config.userEchoWindowMs) return;
|
|
273
|
+
// `force` is used for a fresh transition: re-emitting is harmless and
|
|
274
|
+
// recovers from a flash the client cleared without telling us.
|
|
275
|
+
if (!force && this._flashing.get(sessionId)) return;
|
|
127
276
|
this._flashing.set(sessionId, true);
|
|
277
|
+
s.pendingAttention = false;
|
|
128
278
|
this.ctx.flashTab(sessionId, { duration: this.config.flashDuration || 0 });
|
|
129
279
|
}
|
|
130
280
|
|
|
131
|
-
_stopFlash(sessionId) {
|
|
281
|
+
_stopFlash(sessionId, { cooldown = false } = {}) {
|
|
282
|
+
const s = this._getSessionState(sessionId);
|
|
283
|
+
if (cooldown) {
|
|
284
|
+
s.cooldownUntil = Date.now() + this.config.cooldownMs;
|
|
285
|
+
}
|
|
286
|
+
s.pendingAttention = false;
|
|
132
287
|
if (!this._flashing.get(sessionId)) return;
|
|
133
288
|
this._flashing.delete(sessionId);
|
|
134
289
|
this.ctx.stopFlashTab(sessionId);
|
|
135
290
|
}
|
|
136
291
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
this._checkAttention(sessionId);
|
|
143
|
-
}, this.config.debounceMs);
|
|
144
|
-
|
|
145
|
-
this._dataTimers.set(sessionId, timer);
|
|
292
|
+
_resetSession(sessionId) {
|
|
293
|
+
this._stopFlash(sessionId);
|
|
294
|
+
this._sessionState.delete(sessionId);
|
|
295
|
+
this._clearTimer(this._dataTimers, sessionId);
|
|
296
|
+
this._clearTimer(this._idleTimers, sessionId);
|
|
146
297
|
}
|
|
147
298
|
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
if (
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
setTimeout(() => {
|
|
156
|
-
sessionState.idleCheckScheduled = false;
|
|
157
|
-
if (!sessionState.wasWorking) return;
|
|
158
|
-
if (this._isWorking(sessionId)) return;
|
|
159
|
-
this._checkAttention(sessionId);
|
|
160
|
-
}, delay);
|
|
299
|
+
_clearTimer(map, sessionId) {
|
|
300
|
+
const timer = map.get(sessionId);
|
|
301
|
+
if (timer) {
|
|
302
|
+
clearTimeout(timer);
|
|
303
|
+
map.delete(sessionId);
|
|
304
|
+
}
|
|
161
305
|
}
|
|
162
306
|
|
|
163
|
-
|
|
164
|
-
|
|
307
|
+
_markWorking(s, sessionId) {
|
|
308
|
+
s.working = true;
|
|
309
|
+
s.workEpisode = true;
|
|
310
|
+
s.pendingAttention = false;
|
|
311
|
+
s.lastWorkSignalAt = Date.now();
|
|
312
|
+
// Re-evaluate as soon as the idle threshold could be met, so the flash
|
|
313
|
+
// doesn't wait for the next periodic check (up to checkInterval later).
|
|
314
|
+
this._clearTimer(this._idleTimers, sessionId);
|
|
315
|
+
this._idleTimers.set(sessionId, setTimeout(() => {
|
|
316
|
+
this._idleTimers.delete(sessionId);
|
|
317
|
+
this._evaluate(sessionId);
|
|
318
|
+
}, this.config.idleThreshold + 100));
|
|
165
319
|
}
|
|
166
320
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (!state || !state.state) return;
|
|
172
|
-
|
|
173
|
-
const lines = state.state.split('\n');
|
|
174
|
-
const recentLines = lines.slice(-30);
|
|
175
|
-
|
|
176
|
-
let foundClaude = false;
|
|
177
|
-
let matchedPrompt = false;
|
|
178
|
-
|
|
179
|
-
for (const line of recentLines) {
|
|
180
|
-
const stripped = this._stripAnsi(line);
|
|
181
|
-
if (!stripped) continue;
|
|
321
|
+
// Core evaluation: reads the live viewport and drives the state machine.
|
|
322
|
+
_evaluate(sessionId) {
|
|
323
|
+
const viewport = this._getViewportLines(sessionId);
|
|
324
|
+
if (!viewport) return;
|
|
182
325
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
326
|
+
const s = this._getSessionState(sessionId);
|
|
327
|
+
const now = Date.now();
|
|
186
328
|
|
|
187
|
-
|
|
188
|
-
|
|
329
|
+
// 1) App presence (non-sticky, with a small miss streak so a single
|
|
330
|
+
// mid-redraw check can't reset state).
|
|
331
|
+
const appOnScreen = viewport.some(line => this._matchesAny(this._appPatterns, line));
|
|
332
|
+
if (appOnScreen) {
|
|
333
|
+
s.appVisible = true;
|
|
334
|
+
s.appMissStreak = 0;
|
|
335
|
+
} else {
|
|
336
|
+
s.appMissStreak++;
|
|
337
|
+
if (s.appMissStreak >= this.config.appMissLimit) {
|
|
338
|
+
// Claude is off screen: no NEW working episode may start. An
|
|
339
|
+
// already armed episode is kept on purpose — a permission dialog
|
|
340
|
+
// can hide every signature, and an agent that exited on its own
|
|
341
|
+
// still deserves its one flash. Anything the user did to leave
|
|
342
|
+
// the app went through onUserInput, which clears the episode.
|
|
343
|
+
s.appVisible = false;
|
|
344
|
+
s.working = false;
|
|
189
345
|
}
|
|
190
346
|
}
|
|
191
347
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
348
|
+
// 2) Working indicator in the footer region, in two tiers:
|
|
349
|
+
// - a run hint ("esc to interrupt") is unambiguous and counts even
|
|
350
|
+
// on a completely quiet screen;
|
|
351
|
+
// - a bare spinner glyph only counts while output is actually
|
|
352
|
+
// flowing. A spinner that animates always produces output, so a
|
|
353
|
+
// glyph with a quiet stream is a LEFTOVER (the finished status
|
|
354
|
+
// line pushed up by a dialog, a braille graph, a progress bar).
|
|
355
|
+
// Treating those as "still working" is what kept sessions
|
|
356
|
+
// pinned in the working state and swallowed the flash.
|
|
357
|
+
const footer = viewport.slice(-this.config.footerLines);
|
|
358
|
+
const hintVisible = footer.some(line => this._matchesAny(this._workingHintPatterns, line));
|
|
359
|
+
const glyphVisible = !hintVisible &&
|
|
360
|
+
footer.some(line => this._matchesAny(this._workingPatterns, line));
|
|
361
|
+
const spinnerLive = (now - s.lastOutputAt) < this.config.spinnerStaleMs;
|
|
362
|
+
// Last resort against a permanently stuck "working" state: a run that
|
|
363
|
+
// is really in progress keeps repainting its elapsed-time counter, so
|
|
364
|
+
// a screen that has produced NOTHING for workStaleMs is showing a
|
|
365
|
+
// leftover, not a live run.
|
|
366
|
+
const outputStale = (now - s.lastOutputAt) > this.config.workStaleMs;
|
|
367
|
+
const workingVisible = s.appVisible && !outputStale &&
|
|
368
|
+
(hintVisible || (glyphVisible && spinnerLive));
|
|
369
|
+
|
|
370
|
+
if (workingVisible) {
|
|
371
|
+
const wasFlashing = this._flashing.get(sessionId) || s.pendingAttention;
|
|
372
|
+
this._markWorking(s, sessionId);
|
|
373
|
+
// Working resumed — anything currently flashing is stale.
|
|
374
|
+
if (wasFlashing) this._stopFlash(sessionId, { cooldown: true });
|
|
375
|
+
return;
|
|
195
376
|
}
|
|
196
377
|
|
|
197
|
-
|
|
378
|
+
// 3) Idle transition: a working episode ended (indicator gone AND no
|
|
379
|
+
// work signal for idleThreshold) → the reply finished or a
|
|
380
|
+
// permission dialog is waiting → flash.
|
|
381
|
+
if (s.workEpisode && (now - s.lastWorkSignalAt) >= this.config.idleThreshold) {
|
|
382
|
+
s.workEpisode = false;
|
|
383
|
+
s.working = false;
|
|
384
|
+
s.pendingAttention = true;
|
|
385
|
+
this._startFlash(sessionId, { force: true });
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
198
388
|
|
|
199
|
-
|
|
389
|
+
// 4) A transition that was suppressed (cooldown after a keystroke)
|
|
390
|
+
// stays queued and is retried until it goes out or the user shows up.
|
|
391
|
+
if (s.pendingAttention) {
|
|
200
392
|
this._startFlash(sessionId);
|
|
201
393
|
return;
|
|
202
394
|
}
|
|
203
395
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
396
|
+
// 5) Optional custom attention patterns (config-supplied only),
|
|
397
|
+
// evaluated against the footer while idle.
|
|
398
|
+
if (s.appVisible && !s.working && this._attentionPatterns.length > 0) {
|
|
399
|
+
// Level-triggered (the pattern stays on screen), so the dedupe in
|
|
400
|
+
// _startFlash must apply — no force here.
|
|
401
|
+
if (footer.some(line => this._matchesAny(this._attentionPatterns, line))) {
|
|
402
|
+
this._startFlash(sessionId);
|
|
403
|
+
}
|
|
210
404
|
}
|
|
211
405
|
}
|
|
212
406
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const
|
|
407
|
+
_scheduleEvaluate(sessionId) {
|
|
408
|
+
if (this._dataTimers.has(sessionId)) return;
|
|
409
|
+
const timer = setTimeout(() => {
|
|
410
|
+
this._dataTimers.delete(sessionId);
|
|
411
|
+
this._evaluate(sessionId);
|
|
412
|
+
}, this.config.debounceMs);
|
|
413
|
+
this._dataTimers.set(sessionId, timer);
|
|
414
|
+
}
|
|
216
415
|
|
|
217
|
-
|
|
218
|
-
|
|
416
|
+
onData(sessionId, data) {
|
|
417
|
+
const s = this._getSessionState(sessionId);
|
|
418
|
+
const now = Date.now();
|
|
419
|
+
s.lastOutputAt = now;
|
|
420
|
+
|
|
421
|
+
// Cheap inline work signal: spinner/indicator glyphs in the stream
|
|
422
|
+
// keep the working state fresh without waiting for a viewport check.
|
|
423
|
+
if (s.appVisible && this._matchesAny(this._workingPatterns, data)) {
|
|
424
|
+
const wasFlashing = this._flashing.get(sessionId) || s.pendingAttention;
|
|
425
|
+
this._markWorking(s, sessionId);
|
|
426
|
+
if (wasFlashing) this._stopFlash(sessionId, { cooldown: true });
|
|
427
|
+
return;
|
|
219
428
|
}
|
|
220
429
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
if (
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
430
|
+
// Agent-driven output heuristic: bytes arriving while the user hasn't
|
|
431
|
+
// typed recently. Echo/UI reactions to keystrokes don't count.
|
|
432
|
+
if (now - s.lastUserInputAt > this.config.userEchoWindowMs) {
|
|
433
|
+
if (now - s.agentWindowStart > this.config.workWindowMs) {
|
|
434
|
+
s.agentWindowStart = now;
|
|
435
|
+
s.agentBytes = 0;
|
|
436
|
+
}
|
|
437
|
+
s.agentBytes += data.length;
|
|
438
|
+
if (s.appVisible && s.agentBytes >= this.config.minWorkBytes) {
|
|
439
|
+
const wasFlashing = this._flashing.get(sessionId) || s.pendingAttention;
|
|
440
|
+
this._markWorking(s, sessionId);
|
|
441
|
+
if (wasFlashing) this._stopFlash(sessionId, { cooldown: true });
|
|
230
442
|
}
|
|
231
|
-
return;
|
|
232
443
|
}
|
|
233
444
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if (this._matchesPattern(stripped) || this._matchesPattern(data)) {
|
|
238
|
-
this._startFlash(sessionId);
|
|
239
|
-
} else {
|
|
240
|
-
this._scheduleCheck(sessionId);
|
|
241
|
-
}
|
|
445
|
+
// Debounced evaluation so a burst that ends (stream complete, dialog
|
|
446
|
+
// shown) is noticed quickly instead of waiting for the next interval.
|
|
447
|
+
this._scheduleEvaluate(sessionId);
|
|
242
448
|
}
|
|
243
449
|
|
|
244
|
-
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
450
|
+
onUserInput(sessionId /*, data */) {
|
|
451
|
+
const s = this._getSessionState(sessionId);
|
|
452
|
+
s.lastUserInputAt = Date.now();
|
|
453
|
+
// The user is here — stop flashing and hold off for a while. The
|
|
454
|
+
// current episode is dropped as well: if the agent really is working,
|
|
455
|
+
// its next spinner frame re-arms it within milliseconds, and if the
|
|
456
|
+
// keystroke was the user quitting to a shell there is nothing left to
|
|
457
|
+
// announce.
|
|
458
|
+
s.workEpisode = false;
|
|
459
|
+
s.working = false;
|
|
460
|
+
this._stopFlash(sessionId, { cooldown: true });
|
|
461
|
+
}
|
|
253
462
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
463
|
+
// A client reported the user is looking at this tab.
|
|
464
|
+
onSessionFocus(sessionId) {
|
|
465
|
+
this._stopFlash(sessionId, { cooldown: true });
|
|
257
466
|
}
|
|
258
467
|
|
|
259
468
|
onSessionConnect(sessionId) {
|
|
260
|
-
this.
|
|
469
|
+
this._resetSession(sessionId);
|
|
261
470
|
this._flashing.delete(sessionId);
|
|
262
471
|
|
|
263
472
|
const existingTimer = this._checkTimers.get(sessionId);
|
|
@@ -269,28 +478,21 @@ class ClaudeAttentionPlugin {
|
|
|
269
478
|
this._checkTimers.delete(sessionId);
|
|
270
479
|
return;
|
|
271
480
|
}
|
|
272
|
-
this.
|
|
481
|
+
this._evaluate(sessionId);
|
|
273
482
|
}, this.config.checkInterval);
|
|
274
483
|
|
|
275
484
|
this._checkTimers.set(sessionId, timer);
|
|
276
485
|
}
|
|
277
486
|
|
|
278
487
|
onSessionDisconnect(sessionId) {
|
|
279
|
-
this.
|
|
280
|
-
this._sessionState.delete(sessionId);
|
|
488
|
+
this._resetSession(sessionId);
|
|
281
489
|
|
|
282
490
|
const checkTimer = this._checkTimers.get(sessionId);
|
|
283
491
|
if (checkTimer) {
|
|
284
492
|
clearInterval(checkTimer);
|
|
285
493
|
this._checkTimers.delete(sessionId);
|
|
286
494
|
}
|
|
287
|
-
|
|
288
|
-
const dataTimer = this._dataTimers.get(sessionId);
|
|
289
|
-
if (dataTimer) {
|
|
290
|
-
clearTimeout(dataTimer);
|
|
291
|
-
this._dataTimers.delete(sessionId);
|
|
292
|
-
}
|
|
293
495
|
}
|
|
294
496
|
}
|
|
295
497
|
|
|
296
|
-
module.exports = ClaudeAttentionPlugin;
|
|
498
|
+
module.exports = ClaudeAttentionPlugin;
|