opencode-work-context 0.4.6 → 0.4.7
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 +11 -2
- package/package.json +1 -1
- package/plugin/attention-activation.js +159 -0
- package/plugin/attention.js +13 -2
- package/plugin/notifier.js +70 -32
package/README.md
CHANGED
|
@@ -260,8 +260,17 @@ The package invokes the fixed `notify-send` executable directly without a shell
|
|
|
260
260
|
or configurable command. Missing commands, unsupported platforms, unavailable
|
|
261
261
|
desktop sessions, timeouts, and delivery errors are ignored so they cannot
|
|
262
262
|
interrupt TUI startup or event processing. If the runtime channel is unavailable,
|
|
263
|
-
desktop delivery remains a best-effort fallback.
|
|
264
|
-
|
|
263
|
+
desktop delivery remains a best-effort fallback.
|
|
264
|
+
|
|
265
|
+
On Linux notification servers that support actions, `Open session` routes the
|
|
266
|
+
originating OpenCode process to the affected native session. If that process
|
|
267
|
+
runs in tmux, its validated `TMUX_PANE` is selected with fixed `tmux` arguments.
|
|
268
|
+
On X11, the loader records the active window with `xprop` and raises it with
|
|
269
|
+
`wmctrl` when those optional tools are available. Each step is independent and
|
|
270
|
+
best-effort: navigation can still succeed if pane or window activation fails.
|
|
271
|
+
Wayland focus restrictions and terminal tabs without a public activation API
|
|
272
|
+
may require the user to foreground the terminal manually. Session and window
|
|
273
|
+
identifiers never appear in notification text or configurable commands.
|
|
265
274
|
|
|
266
275
|
Background includes a linked native session that is not selected and any session
|
|
267
276
|
owned by an OpenCode instance other than the most recently active live instance.
|
package/package.json
CHANGED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
const WINDOW_ID = /^0x[0-9a-f]+$/i;
|
|
4
|
+
const MAX_OUTPUT = 128;
|
|
5
|
+
|
|
6
|
+
const run = ({ executable, args, spawn, timeoutMs, capture = false, children }) => new Promise((resolve) => {
|
|
7
|
+
let child;
|
|
8
|
+
let output = "";
|
|
9
|
+
let timer;
|
|
10
|
+
let settled = false;
|
|
11
|
+
const finish = (result) => {
|
|
12
|
+
if (settled) return;
|
|
13
|
+
settled = true;
|
|
14
|
+
if (timer) clearTimeout(timer);
|
|
15
|
+
if (child) children?.delete(child);
|
|
16
|
+
resolve(result);
|
|
17
|
+
};
|
|
18
|
+
try {
|
|
19
|
+
child = spawn(executable, args, {
|
|
20
|
+
shell: false,
|
|
21
|
+
stdio: capture ? ["ignore", "pipe", "ignore"] : "ignore",
|
|
22
|
+
windowsHide: true,
|
|
23
|
+
});
|
|
24
|
+
children?.set(child, finish);
|
|
25
|
+
child.stdout?.on("data", (chunk) => {
|
|
26
|
+
const text = String(chunk);
|
|
27
|
+
const remaining = MAX_OUTPUT - output.length;
|
|
28
|
+
output += text.slice(0, Math.max(0, remaining));
|
|
29
|
+
if (text.length > remaining) {
|
|
30
|
+
try { child.kill(); } catch {}
|
|
31
|
+
finish({ ok: false, output: "" });
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
child.stdout?.once("error", () => finish({ ok: false, output: "" }));
|
|
35
|
+
child.once("error", () => finish({ ok: false, output: "" }));
|
|
36
|
+
child.once("exit", (code) => finish({ ok: code === 0, output }));
|
|
37
|
+
timer = setTimeout(() => {
|
|
38
|
+
try { child.kill(); } catch {}
|
|
39
|
+
finish({ ok: false, output: "" });
|
|
40
|
+
}, timeoutMs);
|
|
41
|
+
timer.unref?.();
|
|
42
|
+
} catch {
|
|
43
|
+
finish({ ok: false, output: "" });
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const normalizeWindowId = (value) => {
|
|
48
|
+
const text = String(value ?? "").trim();
|
|
49
|
+
if (WINDOW_ID.test(text)) return text === "0x0" ? null : text;
|
|
50
|
+
if (/^\d+$/.test(text)) {
|
|
51
|
+
try {
|
|
52
|
+
const normalized = `0x${BigInt(text).toString(16)}`;
|
|
53
|
+
return normalized === "0x0" ? null : normalized;
|
|
54
|
+
} catch {}
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const parseActiveWindowId = (output) => {
|
|
60
|
+
const match = String(output ?? "").match(/0x[0-9a-f]+/i);
|
|
61
|
+
return normalizeWindowId(match?.[0]);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const createAttentionActivator = ({
|
|
65
|
+
api,
|
|
66
|
+
env = process.env,
|
|
67
|
+
platform = process.platform,
|
|
68
|
+
spawn = nodeSpawn,
|
|
69
|
+
timeoutMs = 1000,
|
|
70
|
+
captureThrottleMs = 1000,
|
|
71
|
+
navigationTimeoutMs = 2000,
|
|
72
|
+
now = () => Date.now(),
|
|
73
|
+
} = {}) => {
|
|
74
|
+
const paneId = env.TMUX && /^%\d+$/.test(env.TMUX_PANE || "") ? env.TMUX_PANE : null;
|
|
75
|
+
const canCaptureWindow = platform === "linux" && Boolean(env.DISPLAY);
|
|
76
|
+
let windowId = null;
|
|
77
|
+
let lastCapture = 0;
|
|
78
|
+
let capturePending;
|
|
79
|
+
let disposed = false;
|
|
80
|
+
let focused = false;
|
|
81
|
+
let captureGeneration = 0;
|
|
82
|
+
const children = new Map();
|
|
83
|
+
|
|
84
|
+
const capture = () => {
|
|
85
|
+
if (disposed || !canCaptureWindow) return Promise.resolve(windowId);
|
|
86
|
+
focused = true;
|
|
87
|
+
const timestamp = now();
|
|
88
|
+
if (capturePending) return capturePending;
|
|
89
|
+
if (lastCapture && timestamp - lastCapture < captureThrottleMs) return Promise.resolve(windowId);
|
|
90
|
+
lastCapture = timestamp;
|
|
91
|
+
const generation = captureGeneration;
|
|
92
|
+
capturePending = run({ executable: "xprop", args: ["-root", "_NET_ACTIVE_WINDOW"], spawn, timeoutMs, capture: true, children })
|
|
93
|
+
.then((result) => {
|
|
94
|
+
const captured = result.ok ? parseActiveWindowId(result.output) : null;
|
|
95
|
+
if (!disposed && focused && generation === captureGeneration && captured) windowId = captured;
|
|
96
|
+
return windowId;
|
|
97
|
+
})
|
|
98
|
+
.finally(() => { capturePending = undefined; });
|
|
99
|
+
return capturePending;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const blur = () => {
|
|
103
|
+
focused = false;
|
|
104
|
+
captureGeneration += 1;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const navigate = async (sessionId) => {
|
|
108
|
+
if (typeof api?.route?.navigate !== "function") return false;
|
|
109
|
+
let navigation;
|
|
110
|
+
try { navigation = Promise.resolve(api.route.navigate("session", { sessionID: sessionId })); } catch { return false; }
|
|
111
|
+
let timer;
|
|
112
|
+
try {
|
|
113
|
+
return await Promise.race([
|
|
114
|
+
navigation.then(() => true, () => false),
|
|
115
|
+
new Promise((resolve) => {
|
|
116
|
+
timer = setTimeout(() => resolve(false), navigationTimeoutMs);
|
|
117
|
+
timer.unref?.();
|
|
118
|
+
}),
|
|
119
|
+
]);
|
|
120
|
+
} finally {
|
|
121
|
+
if (timer) clearTimeout(timer);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const activate = async (sessionId) => {
|
|
126
|
+
if (disposed || typeof sessionId !== "string" || !sessionId || sessionId.length > 256) return false;
|
|
127
|
+
let activated = await navigate(sessionId);
|
|
128
|
+
if (disposed) return activated;
|
|
129
|
+
if (paneId) {
|
|
130
|
+
const selectedWindow = await run({ executable: "tmux", args: ["select-window", "-t", paneId], spawn, timeoutMs, children });
|
|
131
|
+
if (disposed) return activated;
|
|
132
|
+
const selectedPane = await run({ executable: "tmux", args: ["select-pane", "-t", paneId], spawn, timeoutMs, children });
|
|
133
|
+
activated = selectedWindow.ok && selectedPane.ok || activated;
|
|
134
|
+
}
|
|
135
|
+
if (disposed) return activated;
|
|
136
|
+
if (windowId && canCaptureWindow) {
|
|
137
|
+
const raised = await run({ executable: "wmctrl", args: ["-ia", windowId], spawn, timeoutMs, children });
|
|
138
|
+
activated = raised.ok || activated;
|
|
139
|
+
}
|
|
140
|
+
return activated;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
available: typeof api?.route?.navigate === "function" || Boolean(paneId) || Boolean(windowId),
|
|
145
|
+
capture,
|
|
146
|
+
blur,
|
|
147
|
+
activate,
|
|
148
|
+
target: () => ({ paneId, windowId }),
|
|
149
|
+
dispose: () => {
|
|
150
|
+
disposed = true;
|
|
151
|
+
blur();
|
|
152
|
+
for (const [child, finish] of children) {
|
|
153
|
+
try { child.kill(); } catch {}
|
|
154
|
+
finish({ ok: false, output: "" });
|
|
155
|
+
}
|
|
156
|
+
children.clear();
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
};
|
package/plugin/attention.js
CHANGED
|
@@ -3,6 +3,7 @@ import path from "node:path";
|
|
|
3
3
|
import { loadConfig } from "../src/storage.js";
|
|
4
4
|
import { readWorkContextSnapshot } from "../src/work-context-snapshot.js";
|
|
5
5
|
import { createGlobalAttentionChannel } from "./global-attention.js";
|
|
6
|
+
import { createAttentionActivator } from "./attention-activation.js";
|
|
6
7
|
import { createDesktopNotifier } from "./notifier.js";
|
|
7
8
|
import { createTuiFocusTracker } from "./tui-focus.js";
|
|
8
9
|
import { readTuiState, updateTuiState } from "./tui-state.js";
|
|
@@ -340,7 +341,10 @@ export const tui = async (api, options = {}) => {
|
|
|
340
341
|
if (activeControllers.has(api) || api[ACTIVE_ATTENTION_KEY]) return activeControllers.get(api) || api[ACTIVE_ATTENTION_KEY];
|
|
341
342
|
const projectRoot = api.state?.path?.worktree || api.state?.path?.directory;
|
|
342
343
|
if (!projectRoot) return;
|
|
344
|
+
const ownsDesktopNotifier = !options.desktopNotifier;
|
|
343
345
|
const desktopNotifier = options.desktopNotifier || createDesktopNotifier({ enabled: desktopNotificationsEnabled(projectRoot), ...options.notifierOptions });
|
|
346
|
+
const activator = options.attentionActivator
|
|
347
|
+
|| (typeof api.route?.navigate === "function" ? createAttentionActivator({ api, ...options.activatorOptions }) : null);
|
|
344
348
|
let controller;
|
|
345
349
|
const globalChannel = options.globalAttentionChannel === false
|
|
346
350
|
? null
|
|
@@ -355,10 +359,14 @@ export const tui = async (api, options = {}) => {
|
|
|
355
359
|
onFocus: () => { void controller?.refresh(); },
|
|
356
360
|
onActivity: () => {
|
|
357
361
|
globalChannel?.activity?.();
|
|
362
|
+
void activator?.capture?.();
|
|
358
363
|
const sessionId = currentSessionId(api);
|
|
359
364
|
if (sessionId) controller?.acknowledge(sessionId);
|
|
360
365
|
},
|
|
361
|
-
onBlur: () => {
|
|
366
|
+
onBlur: () => {
|
|
367
|
+
globalChannel?.deactivate?.();
|
|
368
|
+
activator?.blur?.();
|
|
369
|
+
},
|
|
362
370
|
});
|
|
363
371
|
controller = createAttentionTuiController({
|
|
364
372
|
api,
|
|
@@ -378,7 +386,8 @@ export const tui = async (api, options = {}) => {
|
|
|
378
386
|
workspaceTitle: context.workspaceTitle,
|
|
379
387
|
stageTitle: context.stageTitle,
|
|
380
388
|
});
|
|
381
|
-
|
|
389
|
+
const onOpen = activator?.available ? () => activator.activate(change.sessionId) : undefined;
|
|
390
|
+
void Promise.resolve(desktopNotifier.notify(context, { onOpen })).catch(() => {});
|
|
382
391
|
},
|
|
383
392
|
now: options.now,
|
|
384
393
|
});
|
|
@@ -396,6 +405,8 @@ export const tui = async (api, options = {}) => {
|
|
|
396
405
|
cleaned = true;
|
|
397
406
|
controller.dispose();
|
|
398
407
|
focus.dispose?.();
|
|
408
|
+
activator?.dispose?.();
|
|
409
|
+
if (ownsDesktopNotifier) desktopNotifier.dispose?.();
|
|
399
410
|
globalChannel?.dispose?.();
|
|
400
411
|
for (const registration of registrations) disposeRegistration(registration);
|
|
401
412
|
if (activeControllers.get(api) === cleanup) activeControllers.delete(api);
|
package/plugin/notifier.js
CHANGED
|
@@ -35,9 +35,10 @@ export const buildNotification = ({ workspaceId, workspaceTitle, stageId, stageT
|
|
|
35
35
|
};
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
export const notifySendArguments = (notification) => [
|
|
38
|
+
export const notifySendArguments = (notification, actionable = false, expireTimeMs = 0) => [
|
|
39
39
|
"--app-name=OpenCode Work Context",
|
|
40
40
|
"--urgency=normal",
|
|
41
|
+
...(actionable ? ["--action=default=Open session", `--expire-time=${expireTimeMs}`] : []),
|
|
41
42
|
"--",
|
|
42
43
|
notification.title,
|
|
43
44
|
notification.body,
|
|
@@ -48,36 +49,73 @@ export const createDesktopNotifier = ({
|
|
|
48
49
|
platform = process.platform,
|
|
49
50
|
spawn = nodeSpawn,
|
|
50
51
|
timeoutMs = 3000,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
52
|
+
actionTimeoutMs = 5 * 60 * 1000,
|
|
53
|
+
} = {}) => {
|
|
54
|
+
const children = new Map();
|
|
55
|
+
let disposed = false;
|
|
56
|
+
const send = (notification, { onOpen, actionable }) => new Promise((resolve) => {
|
|
57
|
+
let child;
|
|
58
|
+
let output = "";
|
|
59
|
+
let timer;
|
|
60
|
+
let settled = false;
|
|
61
|
+
const finish = (result) => {
|
|
62
|
+
if (settled) return;
|
|
63
|
+
settled = true;
|
|
64
|
+
if (timer) clearTimeout(timer);
|
|
65
|
+
if (child) children.delete(child);
|
|
66
|
+
resolve(result);
|
|
67
|
+
};
|
|
68
|
+
try {
|
|
69
|
+
child = spawn("notify-send", notifySendArguments(notification, actionable, actionTimeoutMs), {
|
|
70
|
+
shell: false,
|
|
71
|
+
stdio: actionable ? ["ignore", "pipe", "ignore"] : "ignore",
|
|
72
|
+
windowsHide: true,
|
|
73
|
+
});
|
|
74
|
+
children.set(child, finish);
|
|
75
|
+
child.stdout?.on("data", (chunk) => {
|
|
76
|
+
const text = String(chunk);
|
|
77
|
+
const remaining = 32 - output.length;
|
|
78
|
+
output += text.slice(0, Math.max(0, remaining));
|
|
79
|
+
if (text.length > remaining) {
|
|
74
80
|
try { child.kill(); } catch {}
|
|
75
|
-
finish(false);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
finish({ delivered: false, retry: false });
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
child.stdout?.once("error", () => finish({ delivered: false, retry: false }));
|
|
85
|
+
child.once("error", () => finish({ delivered: false, retry: false }));
|
|
86
|
+
child.once("exit", (code) => {
|
|
87
|
+
if (settled) return;
|
|
88
|
+
if (code === 0 && actionable && output.trim() === "default") {
|
|
89
|
+
try { void Promise.resolve(onOpen()).catch(() => {}); } catch {}
|
|
90
|
+
}
|
|
91
|
+
finish({ delivered: code === 0, retry: actionable && code !== 0 });
|
|
92
|
+
});
|
|
93
|
+
timer = setTimeout(() => {
|
|
94
|
+
try { child.kill(); } catch {}
|
|
95
|
+
finish({ delivered: false, retry: false });
|
|
96
|
+
}, actionable ? actionTimeoutMs + 1000 : timeoutMs);
|
|
97
|
+
timer.unref?.();
|
|
98
|
+
} catch {
|
|
99
|
+
finish({ delivered: false, retry: false });
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
const notify = async (context, { onOpen } = {}) => {
|
|
103
|
+
const notification = buildNotification(context);
|
|
104
|
+
if (disposed || !enabled || platform !== "linux" || !notification) return false;
|
|
105
|
+
const actionable = typeof onOpen === "function";
|
|
106
|
+
const result = await send(notification, { onOpen, actionable });
|
|
107
|
+
if (result.retry && !disposed) return (await send(notification, { actionable: false })).delivered;
|
|
108
|
+
return result.delivered;
|
|
109
|
+
};
|
|
110
|
+
return {
|
|
111
|
+
notify,
|
|
112
|
+
dispose: () => {
|
|
113
|
+
disposed = true;
|
|
114
|
+
for (const [child, finish] of children) {
|
|
115
|
+
try { child.kill(); } catch {}
|
|
116
|
+
finish({ delivered: false, retry: false });
|
|
80
117
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
118
|
+
children.clear();
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
};
|