opencode-work-context 0.4.3 → 0.4.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/README.md +37 -30
- package/package.json +1 -1
- package/plugin/attention.js +38 -2
- package/plugin/global-attention.js +301 -0
- package/plugin/tui-focus.js +12 -2
package/README.md
CHANGED
|
@@ -229,10 +229,25 @@ Questions, permission requests, errors, and a background session becoming idle
|
|
|
229
229
|
after observed work remain marked with `[ATTN]` until that native session is
|
|
230
230
|
opened. Attention does not change workspace or stage lifecycle state.
|
|
231
231
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
232
|
+
Every loaded TUI instance registers a short-lived user-level activity lease.
|
|
233
|
+
Focus and keyboard activity select the active OpenCode instance independently
|
|
234
|
+
of its project directory. When a session in project A needs attention while the
|
|
235
|
+
user works in project B, A publishes a bounded event under
|
|
236
|
+
`$XDG_RUNTIME_DIR/opencode-work-context/attention-v1` and active B displays a
|
|
237
|
+
native `Work Context` toast. The persistent `[ATTN]` marker remains in project A;
|
|
238
|
+
the toast does not import A's state or enable cross-server navigation.
|
|
239
|
+
|
|
240
|
+
The channel uses private directories, atomic immutable event files, strict
|
|
241
|
+
schemas, ownership/type/size checks, short event expiry, stale-lease cleanup,
|
|
242
|
+
and bounded retention. It contains only sanitized project, workspace, and stage
|
|
243
|
+
titles plus a controlled reason. Prompts, messages, tool output, secrets, raw
|
|
244
|
+
provider errors, and full project paths are never published. No daemon, socket,
|
|
245
|
+
terminal multiplexer integration, or arbitrary command is required.
|
|
246
|
+
|
|
247
|
+
If the runtime channel or in-TUI toast API is unavailable, Linux falls back to
|
|
248
|
+
one best-effort desktop notification when `notify-send` and a desktop DBus
|
|
249
|
+
session are available. Desktop fallback is enabled by default. Disable it while
|
|
250
|
+
retaining project-local attention indicators with:
|
|
236
251
|
|
|
237
252
|
```yaml
|
|
238
253
|
notifications:
|
|
@@ -240,32 +255,24 @@ notifications:
|
|
|
240
255
|
```
|
|
241
256
|
|
|
242
257
|
The package invokes the fixed `notify-send` executable directly without a shell
|
|
243
|
-
or configurable command.
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
To verify an installed project manually, install the distribution package that
|
|
262
|
-
provides `notify-send` (commonly `libnotify-bin`), ensure `notify-send "Test"`
|
|
263
|
-
appears in the current desktop session, restart OpenCode, start work in a linked
|
|
264
|
-
session, and switch to another native session before the first one finishes or
|
|
265
|
-
asks a question. Expect one desktop notification and a persistent `[ATTN]`
|
|
266
|
-
marker. Open the affected native session to clear the marker. Set
|
|
267
|
-
`notifications.desktop` to `false`, restart OpenCode, and repeat to verify that
|
|
268
|
-
only the marker remains.
|
|
258
|
+
or configurable command. Missing commands, unsupported platforms, unavailable
|
|
259
|
+
desktop sessions, timeouts, and delivery errors are ignored so they cannot
|
|
260
|
+
interrupt TUI startup or event processing. Clicking a notification does not
|
|
261
|
+
navigate to its session in this release.
|
|
262
|
+
|
|
263
|
+
Background includes a linked native session that is not selected and any session
|
|
264
|
+
owned by an OpenCode instance other than the most recently active live instance.
|
|
265
|
+
Each instance refreshes its lease independently, so a crashed process expires
|
|
266
|
+
without holding ownership. If the global channel is unavailable, focus falls
|
|
267
|
+
back to OpenTUI's renderer events and unknown focus is conservatively treated as
|
|
268
|
+
foreground.
|
|
269
|
+
|
|
270
|
+
To verify cross-project delivery, install and load the TUI integration in two
|
|
271
|
+
projects, start work in linked session A, then interact with OpenCode B before A
|
|
272
|
+
finishes or asks a question. Expect a `Work Context` toast inside B and a
|
|
273
|
+
persistent `[ATTN]` marker in A. Stop B or use an environment without a valid
|
|
274
|
+
private `XDG_RUNTIME_DIR` to test the Linux desktop fallback. Open A's affected
|
|
275
|
+
native session to clear its marker.
|
|
269
276
|
|
|
270
277
|
Tools are registered by the installed server export and are not copied into the
|
|
271
278
|
project. Core and storage remain in the installed package; projects should not
|
package/package.json
CHANGED
package/plugin/attention.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
2
3
|
import { loadConfig } from "../src/storage.js";
|
|
3
4
|
import { readWorkContextSnapshot } from "../src/work-context-snapshot.js";
|
|
5
|
+
import { createGlobalAttentionChannel } from "./global-attention.js";
|
|
4
6
|
import { createDesktopNotifier } from "./notifier.js";
|
|
5
7
|
import { createTuiFocusTracker } from "./tui-focus.js";
|
|
6
8
|
import { readTuiState, updateTuiState } from "./tui-state.js";
|
|
@@ -67,6 +69,15 @@ export const normalizeAttentionEvent = (event, cycle = 0) => {
|
|
|
67
69
|
};
|
|
68
70
|
|
|
69
71
|
export const attentionReasonText = (reason) => REASON_TEXT[reason] || "attention";
|
|
72
|
+
export const globalAttentionToast = (event) => {
|
|
73
|
+
const context = [event?.projectTitle, event?.workspaceTitle, event?.stageTitle].filter(Boolean).join(" / ");
|
|
74
|
+
return {
|
|
75
|
+
title: "Work Context",
|
|
76
|
+
message: `${context || "Background session"}: ${attentionReasonText(event?.reason)}`,
|
|
77
|
+
variant: event?.reason === "error" ? "error" : ["permission", "question"].includes(event?.reason) ? "warning" : "info",
|
|
78
|
+
duration: 5000,
|
|
79
|
+
};
|
|
80
|
+
};
|
|
70
81
|
export const attentionForSession = (api, sessionId) => api?.workContextAttention?.attentionFor?.(sessionId) || null;
|
|
71
82
|
export const acknowledgeNavigation = (api, sessionId, observedEventKey) => {
|
|
72
83
|
if (!sessionId || !observedEventKey) return false;
|
|
@@ -327,19 +338,43 @@ export const tui = async (api, options = {}) => {
|
|
|
327
338
|
if (!projectRoot) return;
|
|
328
339
|
const desktopNotifier = options.desktopNotifier || createDesktopNotifier({ enabled: desktopNotificationsEnabled(projectRoot), ...options.notifierOptions });
|
|
329
340
|
let controller;
|
|
341
|
+
const globalChannel = options.globalAttentionChannel === false
|
|
342
|
+
? null
|
|
343
|
+
: options.globalAttentionChannel || (typeof api.ui?.toast === "function" ? createGlobalAttentionChannel(options.globalAttentionOptions) : null);
|
|
344
|
+
const globalDelivery = globalChannel?.available === true && globalChannel.start((event) => {
|
|
345
|
+
api.ui.toast(globalAttentionToast(event));
|
|
346
|
+
try { options.onGlobalAttention?.(event); } catch {}
|
|
347
|
+
return true;
|
|
348
|
+
});
|
|
330
349
|
const focus = options.tuiFocus || createTuiFocusTracker({
|
|
331
350
|
renderer: api.renderer,
|
|
332
351
|
onFocus: () => { void controller?.refresh(); },
|
|
352
|
+
onActivity: () => {
|
|
353
|
+
globalChannel?.activity?.();
|
|
354
|
+
const sessionId = currentSessionId(api);
|
|
355
|
+
if (sessionId) controller?.acknowledge(sessionId);
|
|
356
|
+
},
|
|
357
|
+
onBlur: () => { globalChannel?.deactivate?.(); },
|
|
333
358
|
});
|
|
334
359
|
controller = createAttentionTuiController({
|
|
335
360
|
api,
|
|
336
361
|
projectRoot,
|
|
337
362
|
read: api.attentionSnapshotProvider || readWorkContextSnapshot,
|
|
338
|
-
isFocused: options.isTuiFocused || (
|
|
363
|
+
isFocused: options.isTuiFocused || (globalDelivery
|
|
364
|
+
? () => globalChannel.hasActiveInstance() ? globalChannel.isActive() && focus.isFocused() : focus.isFocused()
|
|
365
|
+
: () => focus.isFocused()),
|
|
339
366
|
onAttention: (change) => {
|
|
340
367
|
try { options.onAttention?.(change); } catch {}
|
|
341
368
|
const context = change.contexts?.[0];
|
|
342
|
-
if (context)
|
|
369
|
+
if (!context) return;
|
|
370
|
+
const published = globalDelivery && globalChannel.publish({
|
|
371
|
+
dedupeKey: `${projectRoot}\0${change.sessionId}\0${change.attention.eventKey}`,
|
|
372
|
+
reason: context.reason,
|
|
373
|
+
projectTitle: path.basename(projectRoot),
|
|
374
|
+
workspaceTitle: context.workspaceTitle,
|
|
375
|
+
stageTitle: context.stageTitle,
|
|
376
|
+
});
|
|
377
|
+
if (!published) void Promise.resolve(desktopNotifier.notify(context)).catch(() => {});
|
|
343
378
|
},
|
|
344
379
|
now: options.now,
|
|
345
380
|
});
|
|
@@ -357,6 +392,7 @@ export const tui = async (api, options = {}) => {
|
|
|
357
392
|
cleaned = true;
|
|
358
393
|
controller.dispose();
|
|
359
394
|
focus.dispose?.();
|
|
395
|
+
globalChannel?.dispose?.();
|
|
360
396
|
for (const registration of registrations) disposeRegistration(registration);
|
|
361
397
|
if (activeControllers.get(api) === cleanup) activeControllers.delete(api);
|
|
362
398
|
if (api[ACTIVE_ATTENTION_KEY] === cleanup) delete api[ACTIVE_ATTENTION_KEY];
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
const SCHEMA = 1;
|
|
6
|
+
const EVENT_TTL_MS = 5 * 60 * 1000;
|
|
7
|
+
const LEASE_STALE_MS = 30 * 1000;
|
|
8
|
+
const CLAIM_STALE_MS = 10 * 1000;
|
|
9
|
+
const MAX_FILE_SIZE = 8192;
|
|
10
|
+
const MAX_EVENTS = 100;
|
|
11
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
12
|
+
const REASONS = new Set(["idle", "question", "permission", "error"]);
|
|
13
|
+
|
|
14
|
+
const safeText = (value, limit = 80) => String(value ?? "")
|
|
15
|
+
.replace(/[\u0000-\u001f\u007f]/g, " ")
|
|
16
|
+
.replace(/\s+/g, " ")
|
|
17
|
+
.trim()
|
|
18
|
+
.slice(0, limit);
|
|
19
|
+
|
|
20
|
+
const ownedDirectory = (directory, uid) => {
|
|
21
|
+
try {
|
|
22
|
+
const stat = fs.lstatSync(directory);
|
|
23
|
+
return stat.isDirectory() && !stat.isSymbolicLink() && (uid === undefined || stat.uid === uid);
|
|
24
|
+
} catch { return false; }
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const ensurePrivateDirectory = (directory, uid) => {
|
|
28
|
+
try {
|
|
29
|
+
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
30
|
+
if (!ownedDirectory(directory, uid)) return false;
|
|
31
|
+
fs.chmodSync(directory, 0o700);
|
|
32
|
+
return true;
|
|
33
|
+
} catch { return false; }
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const defaultGlobalAttentionRoot = ({ env = process.env, uid = process.getuid?.() } = {}) => {
|
|
37
|
+
const runtime = env.XDG_RUNTIME_DIR;
|
|
38
|
+
if (!runtime || !path.isAbsolute(runtime) || !ownedDirectory(runtime, uid)) return null;
|
|
39
|
+
try {
|
|
40
|
+
const mode = fs.statSync(runtime).mode & 0o777;
|
|
41
|
+
if ((mode & 0o077) !== 0) return null;
|
|
42
|
+
} catch { return null; }
|
|
43
|
+
return path.join(runtime, "opencode-work-context", "attention-v1");
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const atomicWrite = (file, value) => {
|
|
47
|
+
const temporary = path.join(path.dirname(file), `.${path.basename(file)}.${randomUUID()}.tmp`);
|
|
48
|
+
try {
|
|
49
|
+
fs.writeFileSync(temporary, `${JSON.stringify(value)}\n`, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
50
|
+
fs.renameSync(temporary, file);
|
|
51
|
+
return true;
|
|
52
|
+
} catch {
|
|
53
|
+
try { fs.rmSync(temporary, { force: true }); } catch {}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const readRecord = (file, uid) => {
|
|
59
|
+
try {
|
|
60
|
+
const stat = fs.lstatSync(file);
|
|
61
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > MAX_FILE_SIZE || (uid !== undefined && stat.uid !== uid)) return null;
|
|
62
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
63
|
+
} catch { return null; }
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const validLease = (value) => value
|
|
67
|
+
&& value.schema === SCHEMA
|
|
68
|
+
&& Object.keys(value).every((key) => ["schema", "instanceId", "heartbeatAt", "activeAt"].includes(key))
|
|
69
|
+
&& UUID.test(value.instanceId)
|
|
70
|
+
&& Number.isFinite(value.heartbeatAt)
|
|
71
|
+
&& Number.isFinite(value.activeAt);
|
|
72
|
+
|
|
73
|
+
const validEvent = (value) => value
|
|
74
|
+
&& value.schema === SCHEMA
|
|
75
|
+
&& Object.keys(value).every((key) => ["schema", "id", "sourceInstanceId", "dedupeKey", "createdAt", "expiresAt", "reason", "projectTitle", "workspaceTitle", "stageTitle"].includes(key))
|
|
76
|
+
&& UUID.test(value.id)
|
|
77
|
+
&& UUID.test(value.sourceInstanceId)
|
|
78
|
+
&& /^[0-9a-f]{24}$/.test(value.dedupeKey)
|
|
79
|
+
&& Number.isFinite(value.createdAt)
|
|
80
|
+
&& Number.isFinite(value.expiresAt)
|
|
81
|
+
&& value.expiresAt >= value.createdAt
|
|
82
|
+
&& REASONS.has(value.reason)
|
|
83
|
+
&& [value.projectTitle, value.workspaceTitle, value.stageTitle].every((item) => typeof item === "string" && item === safeText(item));
|
|
84
|
+
|
|
85
|
+
export const createGlobalAttentionChannel = ({
|
|
86
|
+
root = defaultGlobalAttentionRoot(),
|
|
87
|
+
instanceId = randomUUID(),
|
|
88
|
+
now = () => Date.now(),
|
|
89
|
+
pollMs = 750,
|
|
90
|
+
heartbeatMs = 5000,
|
|
91
|
+
activityThrottleMs = 250,
|
|
92
|
+
leaseStaleMs = LEASE_STALE_MS,
|
|
93
|
+
claimStaleMs = CLAIM_STALE_MS,
|
|
94
|
+
eventTtlMs = EVENT_TTL_MS,
|
|
95
|
+
uid = process.getuid?.(),
|
|
96
|
+
} = {}) => {
|
|
97
|
+
const leasesDirectory = root && path.join(root, "leases");
|
|
98
|
+
const eventsDirectory = root && path.join(root, "events");
|
|
99
|
+
const leaseFile = leasesDirectory && path.join(leasesDirectory, `${instanceId}.json`);
|
|
100
|
+
const seen = new Set();
|
|
101
|
+
let activeAt = 0;
|
|
102
|
+
let onEvent;
|
|
103
|
+
let pollTimer;
|
|
104
|
+
let heartbeatTimer;
|
|
105
|
+
let activityTimer;
|
|
106
|
+
let lastActivityWrite = 0;
|
|
107
|
+
let started = false;
|
|
108
|
+
|
|
109
|
+
const available = Boolean(root && UUID.test(instanceId)
|
|
110
|
+
&& ensurePrivateDirectory(root, uid)
|
|
111
|
+
&& ensurePrivateDirectory(leasesDirectory, uid)
|
|
112
|
+
&& ensurePrivateDirectory(eventsDirectory, uid));
|
|
113
|
+
|
|
114
|
+
const writeLease = (activity = false) => {
|
|
115
|
+
if (!available) return false;
|
|
116
|
+
const timestamp = now();
|
|
117
|
+
if (activity) {
|
|
118
|
+
activeAt = Math.max(timestamp, activeAt + 1);
|
|
119
|
+
lastActivityWrite = timestamp;
|
|
120
|
+
}
|
|
121
|
+
return atomicWrite(leaseFile, { schema: SCHEMA, instanceId, heartbeatAt: timestamp, activeAt });
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const leases = () => {
|
|
125
|
+
if (!available) return [];
|
|
126
|
+
const timestamp = now();
|
|
127
|
+
const result = [];
|
|
128
|
+
let names = [];
|
|
129
|
+
try { names = fs.readdirSync(leasesDirectory); } catch { return result; }
|
|
130
|
+
for (const name of names) {
|
|
131
|
+
if (!UUID.test(name.replace(/\.json$/, "")) || !name.endsWith(".json")) continue;
|
|
132
|
+
const file = path.join(leasesDirectory, name);
|
|
133
|
+
const lease = readRecord(file, uid);
|
|
134
|
+
if (!validLease(lease) || lease.heartbeatAt > timestamp + leaseStaleMs || timestamp - lease.heartbeatAt > leaseStaleMs) {
|
|
135
|
+
try { fs.rmSync(file, { force: true }); } catch {}
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
result.push(lease);
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const activeOwner = () => {
|
|
144
|
+
const active = leases().filter((lease) => lease.activeAt > 0)
|
|
145
|
+
.sort((left, right) => right.activeAt - left.activeAt || right.instanceId.localeCompare(left.instanceId))[0];
|
|
146
|
+
return active?.instanceId || null;
|
|
147
|
+
};
|
|
148
|
+
const hasActiveInstance = () => activeOwner() !== null;
|
|
149
|
+
const isActive = () => available && activeOwner() === instanceId;
|
|
150
|
+
const remember = (key) => {
|
|
151
|
+
seen.add(key);
|
|
152
|
+
while (seen.size > MAX_EVENTS * 2) seen.delete(seen.values().next().value);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const activity = () => {
|
|
156
|
+
if (!available) return false;
|
|
157
|
+
const timestamp = now();
|
|
158
|
+
const remaining = activityThrottleMs - (timestamp - lastActivityWrite);
|
|
159
|
+
if (activityThrottleMs <= 0 || remaining <= 0) return writeLease(true);
|
|
160
|
+
if (!activityTimer) {
|
|
161
|
+
activityTimer = setTimeout(() => {
|
|
162
|
+
activityTimer = undefined;
|
|
163
|
+
writeLease(true);
|
|
164
|
+
}, Math.max(1, Math.min(activityThrottleMs, remaining)));
|
|
165
|
+
activityTimer.unref?.();
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const deactivate = () => {
|
|
171
|
+
if (!available) return false;
|
|
172
|
+
if (activityTimer) clearTimeout(activityTimer);
|
|
173
|
+
activityTimer = undefined;
|
|
174
|
+
activeAt = 0;
|
|
175
|
+
return writeLease(false);
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const recoverClaims = (timestamp) => {
|
|
179
|
+
let claims = [];
|
|
180
|
+
try { claims = fs.readdirSync(eventsDirectory).filter((name) => /^claim-[0-9a-f-]+-[0-9a-f-]+\.json$/i.test(name)); } catch { return; }
|
|
181
|
+
for (const name of claims) {
|
|
182
|
+
const claimed = path.join(eventsDirectory, name);
|
|
183
|
+
try {
|
|
184
|
+
const stat = fs.lstatSync(claimed);
|
|
185
|
+
if (!stat.isFile() || stat.isSymbolicLink() || timestamp - stat.mtimeMs < claimStaleMs) continue;
|
|
186
|
+
const event = readRecord(claimed, uid);
|
|
187
|
+
if (!validEvent(event) || event.expiresAt <= timestamp) {
|
|
188
|
+
fs.rmSync(claimed, { force: true });
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const target = path.join(eventsDirectory, `event-${event.id}.json`);
|
|
192
|
+
if (fs.existsSync(target)) fs.rmSync(claimed, { force: true });
|
|
193
|
+
else fs.renameSync(claimed, target);
|
|
194
|
+
} catch {}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const scan = () => {
|
|
199
|
+
if (!available || !isActive()) return 0;
|
|
200
|
+
const timestamp = now();
|
|
201
|
+
recoverClaims(timestamp);
|
|
202
|
+
let files = [];
|
|
203
|
+
try { files = fs.readdirSync(eventsDirectory).filter((name) => /^event-[0-9a-f-]+\.json$/i.test(name)); } catch { return 0; }
|
|
204
|
+
const events = [];
|
|
205
|
+
for (const name of files) {
|
|
206
|
+
const file = path.join(eventsDirectory, name);
|
|
207
|
+
const event = readRecord(file, uid);
|
|
208
|
+
if (!validEvent(event) || event.expiresAt <= timestamp) {
|
|
209
|
+
try { fs.rmSync(file, { force: true }); } catch {}
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
events.push({ event, file });
|
|
213
|
+
}
|
|
214
|
+
events.sort((left, right) => left.event.createdAt - right.event.createdAt || left.event.id.localeCompare(right.event.id));
|
|
215
|
+
const overflow = events.splice(0, Math.max(0, events.length - MAX_EVENTS));
|
|
216
|
+
for (const item of overflow) try { fs.rmSync(item.file, { force: true }); } catch {}
|
|
217
|
+
let delivered = 0;
|
|
218
|
+
for (const { event, file } of events) {
|
|
219
|
+
if (seen.has(event.id) || seen.has(event.dedupeKey)) {
|
|
220
|
+
try { fs.rmSync(file, { force: true }); } catch {}
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const claim = path.join(eventsDirectory, `claim-${event.id}-${instanceId}.json`);
|
|
224
|
+
try { fs.renameSync(file, claim); } catch { continue; }
|
|
225
|
+
if (!isActive()) {
|
|
226
|
+
try { fs.renameSync(claim, file); } catch {}
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
if (typeof onEvent !== "function" || onEvent(event) === false) {
|
|
231
|
+
try { fs.renameSync(claim, file); } catch {}
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
remember(event.id);
|
|
235
|
+
remember(event.dedupeKey);
|
|
236
|
+
try { fs.rmSync(claim, { force: true }); } catch {}
|
|
237
|
+
delivered += 1;
|
|
238
|
+
} catch {
|
|
239
|
+
try { fs.renameSync(claim, file); } catch {}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return delivered;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const publish = ({ dedupeKey, reason, projectTitle, workspaceTitle, stageTitle } = {}) => {
|
|
246
|
+
if (!available || !REASONS.has(reason)) return false;
|
|
247
|
+
const timestamp = now();
|
|
248
|
+
const id = randomUUID();
|
|
249
|
+
const normalizedKey = createHash("sha256").update(String(dedupeKey ?? "")).digest("hex").slice(0, 24);
|
|
250
|
+
return atomicWrite(path.join(eventsDirectory, `event-${id}.json`), {
|
|
251
|
+
schema: SCHEMA,
|
|
252
|
+
id,
|
|
253
|
+
sourceInstanceId: instanceId,
|
|
254
|
+
dedupeKey: normalizedKey,
|
|
255
|
+
createdAt: timestamp,
|
|
256
|
+
expiresAt: timestamp + eventTtlMs,
|
|
257
|
+
reason,
|
|
258
|
+
projectTitle: safeText(projectTitle),
|
|
259
|
+
workspaceTitle: safeText(workspaceTitle),
|
|
260
|
+
stageTitle: safeText(stageTitle),
|
|
261
|
+
});
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const start = (handler) => {
|
|
265
|
+
if (!available || started) return available;
|
|
266
|
+
started = true;
|
|
267
|
+
onEvent = handler;
|
|
268
|
+
writeLease(false);
|
|
269
|
+
scan();
|
|
270
|
+
if (pollMs > 0) { pollTimer = setInterval(scan, pollMs); pollTimer.unref?.(); }
|
|
271
|
+
if (heartbeatMs > 0) { heartbeatTimer = setInterval(() => writeLease(false), heartbeatMs); heartbeatTimer.unref?.(); }
|
|
272
|
+
return true;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const dispose = () => {
|
|
276
|
+
if (pollTimer) clearInterval(pollTimer);
|
|
277
|
+
if (heartbeatTimer) clearInterval(heartbeatTimer);
|
|
278
|
+
if (activityTimer) clearTimeout(activityTimer);
|
|
279
|
+
pollTimer = undefined;
|
|
280
|
+
heartbeatTimer = undefined;
|
|
281
|
+
activityTimer = undefined;
|
|
282
|
+
started = false;
|
|
283
|
+
if (available) {
|
|
284
|
+
const current = readRecord(leaseFile, uid);
|
|
285
|
+
if (current?.instanceId === instanceId) try { fs.rmSync(leaseFile, { force: true }); } catch {}
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
return {
|
|
290
|
+
available,
|
|
291
|
+
instanceId,
|
|
292
|
+
start,
|
|
293
|
+
activity,
|
|
294
|
+
deactivate,
|
|
295
|
+
hasActiveInstance,
|
|
296
|
+
isActive,
|
|
297
|
+
publish,
|
|
298
|
+
scan,
|
|
299
|
+
dispose,
|
|
300
|
+
};
|
|
301
|
+
};
|
package/plugin/tui-focus.js
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
|
-
export const createTuiFocusTracker = ({ renderer, onFocus } = {}) => {
|
|
1
|
+
export const createTuiFocusTracker = ({ renderer, onFocus, onActivity, onBlur } = {}) => {
|
|
2
2
|
let state = "unknown";
|
|
3
3
|
const handleFocus = () => {
|
|
4
4
|
state = "focused";
|
|
5
|
+
try { onActivity?.(); } catch {}
|
|
5
6
|
try { onFocus?.(); } catch {}
|
|
6
7
|
};
|
|
7
|
-
const
|
|
8
|
+
const handleKeypress = () => {
|
|
9
|
+
state = "focused";
|
|
10
|
+
try { onActivity?.(); } catch {}
|
|
11
|
+
};
|
|
12
|
+
const handleBlur = () => {
|
|
13
|
+
state = "blurred";
|
|
14
|
+
try { onBlur?.(); } catch {}
|
|
15
|
+
};
|
|
8
16
|
renderer?.on?.("focus", handleFocus);
|
|
9
17
|
renderer?.on?.("blur", handleBlur);
|
|
18
|
+
renderer?.keyInput?.on?.("keypress", handleKeypress);
|
|
10
19
|
return {
|
|
11
20
|
state: () => state,
|
|
12
21
|
isFocused: () => state !== "blurred",
|
|
13
22
|
dispose: () => {
|
|
14
23
|
renderer?.off?.("focus", handleFocus);
|
|
15
24
|
renderer?.off?.("blur", handleBlur);
|
|
25
|
+
renderer?.keyInput?.off?.("keypress", handleKeypress);
|
|
16
26
|
},
|
|
17
27
|
};
|
|
18
28
|
};
|