desky-agent 0.3.0 → 0.3.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/src/hook.js CHANGED
@@ -1,86 +1,92 @@
1
- #!/usr/bin/env node
2
- 'use strict';
3
- // Claude Code hook: hand the event to Desky and get out of the way.
4
- //
5
- // Wired for every event by the installer. Reads the hook's JSON from stdin and POSTs it to the app on localhost.
6
- // If the app does not answer, starts it in the tray (no window) and tries again for 3 s. Always exits 0 and prints
7
- // nothing: a hook that fails or talks would get in the way of the session it decorates.
8
- //
9
- // After "desky-agent stop" or "Выйти" in the tray a marker file says so and the hook does not start the app;
10
- // opening Desky or the next login clears it. CLAWD_PORT overrides the port, CLAWD_NO_SPAWN=1 skips starting (tests).
11
-
12
- const fs = require('fs');
13
- const http = require('http');
14
- const { spawn } = require('child_process');
15
- const paths = require('./paths');
16
-
17
- function post(body, timeout = 500) {
18
- return new Promise((resolve, reject) => {
19
- const req = http.request({ host: '127.0.0.1', port: paths.port(), path: '/event', method: 'POST', timeout,
20
- headers: { 'Content-Type': 'application/json', 'Content-Length': body.length } }, res => {
21
- res.resume();
22
- res.on('end', () => (res.statusCode === 200 ? resolve() : reject(new Error(String(res.statusCode)))));
23
- });
24
- req.on('timeout', () => req.destroy(new Error('timeout')));
25
- req.on('error', reject);
26
- req.end(body);
27
- });
28
- }
29
-
30
- /** Start the app detached, in the tray, so it outlives the hook and the session that ran it. */
31
- function launch(extra = ['--background']) {
32
- const how = JSON.parse(fs.readFileSync(paths.LAUNCH, 'utf8'));
33
- const env = { ...process.env };
34
- delete env.ELECTRON_RUN_AS_NODE; // set when this runs inside an Electron app such as an editor
35
- // no windowsHide: Desky is a GUI program with no console, and SW_HIDE would hide its first window for good
36
- const child = spawn(how.command, [...(how.args || []), ...extra], { detached: true, stdio: 'ignore', env });
37
- child.unref();
38
- }
39
-
40
- function readStdin() {
41
- return new Promise(resolve => {
42
- if (process.stdin.isTTY) return resolve(Buffer.from('{}'));
43
- const chunks = [];
44
- process.stdin.on('data', c => chunks.push(c));
45
- process.stdin.on('end', () => resolve(chunks.length ? Buffer.concat(chunks) : Buffer.from('{}')));
46
- process.stdin.on('error', () => resolve(Buffer.from('{}')));
47
- });
48
- }
49
-
50
- // what the Core reads; a PostToolUse can carry megabytes of tool output that Desky never looks at
51
- const KEEP = ['hook_event_name', 'session_id', 'tool_name', 'notification_type', 'source'];
52
-
53
- function slim(raw) {
54
- try {
55
- const ev = JSON.parse(raw.toString('utf8').replace(/^/, ''));
56
- const out = {};
57
- for (const k of KEEP) if (ev[k] !== undefined) out[k] = ev[k];
58
- return Buffer.from(JSON.stringify(out));
59
- } catch (e) {
60
- return raw.length <= 1 << 20 ? raw : Buffer.from('{}');
61
- }
62
- }
63
-
64
- async function main() {
65
- const body = slim(await readStdin());
66
- try {
67
- await post(body);
68
- return;
69
- } catch (e) { /* not running */ }
70
- if (process.env.CLAWD_NO_SPAWN || fs.existsSync(paths.STOPPED)) return;
71
- launch();
72
- const end = Date.now() + 3000;
73
- while (Date.now() < end) {
74
- await new Promise(r => setTimeout(r, 300));
75
- try {
76
- await post(body);
77
- return;
78
- } catch (e) { /* still starting */ }
79
- }
80
- }
81
-
82
- if (require.main === module) {
83
- main().catch(() => {}).finally(() => process.exit(0));
84
- }
85
-
86
- module.exports = { launch, post };
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+ // Claude Code and Codex hook: hand the event to Desky and get out of the way.
4
+ //
5
+ // Wired for every event by the installer ("hook.js codex" in Codex). Reads the hook's JSON from stdin and POSTs it
6
+ // to the app on localhost. If the app does not answer, starts it in the tray (no window) and tries again for 3 s.
7
+ // Always exits 0 and prints nothing: a hook that fails or talks would get in the way of the session it decorates.
8
+ //
9
+ // After "desky-agent stop" or "Выйти" in the tray a marker file says so and the hook does not start the app;
10
+ // opening Desky or the next login clears it. CLAWD_PORT overrides the port, CLAWD_NO_SPAWN=1 skips starting (tests).
11
+
12
+ const fs = require('fs');
13
+ const http = require('http');
14
+ const { spawn } = require('child_process');
15
+ const paths = require('./paths');
16
+
17
+ function post(body, timeout = 500) {
18
+ return new Promise((resolve, reject) => {
19
+ const req = http.request({ host: '127.0.0.1', port: paths.port(), path: '/event', method: 'POST', timeout,
20
+ headers: { 'Content-Type': 'application/json', 'Content-Length': body.length } }, res => {
21
+ res.resume();
22
+ res.on('end', () => (res.statusCode === 200 ? resolve() : reject(new Error(String(res.statusCode)))));
23
+ });
24
+ req.on('timeout', () => req.destroy(new Error('timeout')));
25
+ req.on('error', reject);
26
+ req.end(body);
27
+ });
28
+ }
29
+
30
+ /** Start the app detached, in the tray, so it outlives the hook and the session that ran it. */
31
+ function launch(extra = ['--background']) {
32
+ const how = JSON.parse(fs.readFileSync(paths.LAUNCH, 'utf8'));
33
+ const env = { ...process.env };
34
+ delete env.ELECTRON_RUN_AS_NODE; // set when this runs inside an Electron app such as an editor
35
+ // no windowsHide: Desky is a GUI program with no console, and SW_HIDE would hide its first window for good
36
+ const child = spawn(how.command, [...(how.args || []), ...extra], { detached: true, stdio: 'ignore', env });
37
+ child.unref();
38
+ }
39
+
40
+ function readStdin() {
41
+ return new Promise(resolve => {
42
+ if (process.stdin.isTTY) return resolve(Buffer.from('{}'));
43
+ const chunks = [];
44
+ process.stdin.on('data', c => chunks.push(c));
45
+ process.stdin.on('end', () => resolve(chunks.length ? Buffer.concat(chunks) : Buffer.from('{}')));
46
+ process.stdin.on('error', () => resolve(Buffer.from('{}')));
47
+ });
48
+ }
49
+
50
+ // what the Core reads; a PostToolUse can carry megabytes of tool output that Desky never looks at
51
+ const KEEP = ['hook_event_name', 'session_id', 'tool_name', 'notification_type', 'source'];
52
+
53
+ /** agent: "codex" when Codex ran us (its hooks.json passes the word), else Claude Code. Codex reads files with
54
+ * shell commands, so for it the start of the command goes along: the Core tells reading from working by it. */
55
+ function slim(raw, agent = null) {
56
+ try {
57
+ const ev = JSON.parse(raw.toString('utf8').replace(/^/, ''));
58
+ const out = {};
59
+ for (const k of KEEP) if (ev[k] !== undefined) out[k] = ev[k];
60
+ if (agent) out.agent = agent;
61
+ if (agent === 'codex' && ev.tool_input && typeof ev.tool_input.command === 'string') {
62
+ out.command = ev.tool_input.command.slice(0, 500);
63
+ }
64
+ return Buffer.from(JSON.stringify(out));
65
+ } catch (e) {
66
+ return raw.length <= 1 << 20 ? raw : Buffer.from('{}');
67
+ }
68
+ }
69
+
70
+ async function main() {
71
+ const body = slim(await readStdin(), process.argv[2] === 'codex' ? 'codex' : null);
72
+ try {
73
+ await post(body);
74
+ return;
75
+ } catch (e) { /* not running */ }
76
+ if (process.env.CLAWD_NO_SPAWN || fs.existsSync(paths.STOPPED)) return;
77
+ launch();
78
+ const end = Date.now() + 3000;
79
+ while (Date.now() < end) {
80
+ await new Promise(r => setTimeout(r, 300));
81
+ try {
82
+ await post(body);
83
+ return;
84
+ } catch (e) { /* still starting */ }
85
+ }
86
+ }
87
+
88
+ if (require.main === module) {
89
+ main().catch(() => {}).finally(() => process.exit(0));
90
+ }
91
+
92
+ module.exports = { launch, post, slim };