desky-agent 0.3.0 → 0.3.2
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 +57 -50
- package/bin/cli.js +217 -206
- package/electron/icons.js +79 -79
- package/electron/main.js +245 -219
- package/firmware/firmware.bin +0 -0
- package/firmware/manifest.json +3 -3
- package/package.json +41 -40
- package/src/autostart.js +106 -106
- package/src/board.js +69 -69
- package/src/core.js +244 -172
- package/src/daemon.js +522 -446
- package/src/firmware.js +207 -207
- package/src/flasher.js +113 -113
- package/src/hook.js +95 -86
- package/src/install.js +418 -375
- package/src/link.js +207 -207
- package/src/paths.js +44 -44
- package/src/release.js +131 -0
- package/src/selfupdate.js +58 -0
- package/src/updater.js +138 -138
- package/ui/app.html +1522 -1470
- package/ui/clawd3d.js +377 -375
- package/ui/vendor/OrbitControls.js +1532 -1532
- package/ui/vendor/RoomEnvironment.js +144 -144
- package/ui/vendor/STLLoader.js +410 -410
- package/ui/vendor/three.LICENSE +21 -21
- package/ui/vendor/three.module.min.js +6 -6
package/src/link.js
CHANGED
|
@@ -1,207 +1,207 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
// Desky's board (ESP32-S3-Zero) on USB: find it, open it without resetting it, keep it. docs/firmware_contract.md.
|
|
3
|
-
//
|
|
4
|
-
// The chip's USB-Serial/JTAG resets the chip on a DTR/RTS toggle (RTS up with DTR down is the reset pulse).
|
|
5
|
-
// Windows: serialport opens with hupcl:false as DTR and RTS both disabled, so nothing moves; leave the lines alone.
|
|
6
|
-
// macOS and Linux: open() itself raises both lines. Dropping DTR first would leave RTS up with DTR down for a
|
|
7
|
-
// moment, so RTS goes down first and only then DTR: up/up, up/down, down/down, never the reset state.
|
|
8
|
-
|
|
9
|
-
const VID = '303a'; // Espressif USB JTAG/serial: any S3/C3/C6, so "who" tells Desky from other boards
|
|
10
|
-
const { parseWho } = require('./board');
|
|
11
|
-
const PID = '1001';
|
|
12
|
-
const BAUD = 115200;
|
|
13
|
-
|
|
14
|
-
let serialport = null;
|
|
15
|
-
function lib() {
|
|
16
|
-
if (!serialport) serialport = require('serialport');
|
|
17
|
-
return serialport;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** Every 303A:1001 port: { path, serialNumber }. serialNumber is the chip's MAC on the USB-Serial/JTAG, so it
|
|
21
|
-
* stays the same when the port is renamed after a reset. */
|
|
22
|
-
async function ports() {
|
|
23
|
-
try {
|
|
24
|
-
const list = await lib().SerialPort.list();
|
|
25
|
-
return list.filter(p => String(p.vendorId || '').toLowerCase() === VID && String(p.productId || '').toLowerCase() === PID)
|
|
26
|
-
.map(p => ({ path: process.platform === 'darwin' ? p.path.replace(/^\/dev\/tty\./, '/dev/cu.') : p.path,
|
|
27
|
-
serialNumber: p.serialNumber ? String(p.serialNumber) : null }));
|
|
28
|
-
} catch (e) {
|
|
29
|
-
return [];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async function candidates() {
|
|
34
|
-
return (await ports()).map(p => p.path);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
38
|
-
const call = (port, fn, ...args) => new Promise((res, rej) => port[fn](...args, e => (e ? rej(e) : res())));
|
|
39
|
-
|
|
40
|
-
async function openPort(dev) {
|
|
41
|
-
const { SerialPort } = lib();
|
|
42
|
-
const port = new SerialPort({ path: dev, baudRate: BAUD, autoOpen: false, hupcl: false });
|
|
43
|
-
port.on('error', () => {}); // an unplug mid-handshake must not throw; attach() listens for real
|
|
44
|
-
await call(port, 'open');
|
|
45
|
-
if (process.platform !== 'win32') {
|
|
46
|
-
await call(port, 'set', { dtr: true, rts: false });
|
|
47
|
-
await call(port, 'set', { dtr: false, rts: false });
|
|
48
|
-
}
|
|
49
|
-
return port;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Ask "who" once a second until a Desky line comes back (contract, section 2): the parsed answer, or null after ms.
|
|
53
|
-
* stop() lets the caller give up early. Leaves the port open and unpiped. */
|
|
54
|
-
function askWho(port, ms = 8000, stop = () => false) {
|
|
55
|
-
const { ReadlineParser } = lib();
|
|
56
|
-
const parser = port.pipe(new ReadlineParser({ delimiter: '\n' }));
|
|
57
|
-
return new Promise(resolve => {
|
|
58
|
-
let ask = null;
|
|
59
|
-
let give = null;
|
|
60
|
-
const done = ok => {
|
|
61
|
-
clearInterval(ask);
|
|
62
|
-
clearTimeout(give);
|
|
63
|
-
parser.removeAllListeners('data');
|
|
64
|
-
port.unpipe(parser);
|
|
65
|
-
resolve(ok);
|
|
66
|
-
};
|
|
67
|
-
parser.on('data', ln => {
|
|
68
|
-
const info = parseWho(ln);
|
|
69
|
-
if (info) done(info);
|
|
70
|
-
});
|
|
71
|
-
const who = () => {
|
|
72
|
-
if (stop()) return done(null);
|
|
73
|
-
try { port.write('who\n'); } catch (e) { done(null); }
|
|
74
|
-
};
|
|
75
|
-
who();
|
|
76
|
-
ask = setInterval(who, 1000);
|
|
77
|
-
give = setTimeout(() => done(null), ms);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
class Link {
|
|
82
|
-
/** Keeps the port open. onConnect() runs after every (re)connect, once "who" has answered; lines from the C3
|
|
83
|
-
* go to onLine(). send() drops the line when nothing is connected. */
|
|
84
|
-
constructor({ onConnect = () => {}, onLine = () => {}, onSilent = () => {}, retry = 3000 } = {}) {
|
|
85
|
-
this.onConnect = onConnect;
|
|
86
|
-
this.onSilent = onSilent; // a 303A:1001 port that did not answer "who": maybe an interrupted update
|
|
87
|
-
this.onLine = onLine;
|
|
88
|
-
this.retry = retry;
|
|
89
|
-
this.port = null;
|
|
90
|
-
this.dev = null;
|
|
91
|
-
this.usb = null; // { path, serialNumber } of the connected port
|
|
92
|
-
this.info = null; // the board's "who": { proto, fw, chip, sn }
|
|
93
|
-
this.stopping = false;
|
|
94
|
-
this.timer = null;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
get connected() {
|
|
98
|
-
return this.port !== null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
start() {
|
|
102
|
-
this.stopping = false;
|
|
103
|
-
this.scan();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async stop() {
|
|
107
|
-
this.stopping = true;
|
|
108
|
-
clearTimeout(this.timer);
|
|
109
|
-
await this.drop();
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
send(line) {
|
|
113
|
-
if (!this.port) return false;
|
|
114
|
-
try {
|
|
115
|
-
this.port.write(line + '\n');
|
|
116
|
-
return true;
|
|
117
|
-
} catch (e) {
|
|
118
|
-
this.drop();
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/** Restart the C3: an RTS pulse with DTR (BOOT) low. The chip drops off USB for a moment; the port closes, the
|
|
124
|
-
* scan finds it again and onConnect() says the state again. */
|
|
125
|
-
async reset() {
|
|
126
|
-
const port = this.port;
|
|
127
|
-
if (!port) return false;
|
|
128
|
-
try {
|
|
129
|
-
await call(port, 'set', { dtr: false, rts: true });
|
|
130
|
-
await sleep(120);
|
|
131
|
-
await call(port, 'set', { dtr: false, rts: false });
|
|
132
|
-
return true;
|
|
133
|
-
} catch (e) {
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
async drop() {
|
|
139
|
-
const port = this.port;
|
|
140
|
-
this.port = null;
|
|
141
|
-
this.dev = null;
|
|
142
|
-
this.usb = null;
|
|
143
|
-
this.info = null;
|
|
144
|
-
if (port && port.isOpen) await call(port, 'close').catch(() => {});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
later() {
|
|
148
|
-
if (!this.stopping) this.timer = setTimeout(() => this.scan(), this.retry);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async scan() {
|
|
152
|
-
if (this.stopping || this.port) return;
|
|
153
|
-
for (const usb of await ports()) {
|
|
154
|
-
const dev = usb.path;
|
|
155
|
-
let port;
|
|
156
|
-
try {
|
|
157
|
-
port = await openPort(dev);
|
|
158
|
-
} catch (e) {
|
|
159
|
-
continue; // held by the remote or a serial monitor
|
|
160
|
-
}
|
|
161
|
-
const info = await this.handshake(port);
|
|
162
|
-
if (info && !this.stopping) {
|
|
163
|
-
this.info = info;
|
|
164
|
-
this.usb = usb;
|
|
165
|
-
this.attach(port, dev);
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
await call(port, 'close').catch(() => {});
|
|
169
|
-
if (this.stopping) return; // stop() came while asking: the port is someone else's now
|
|
170
|
-
try { this.onSilent(usb); } catch (e) { /* the daemon's problem */ }
|
|
171
|
-
}
|
|
172
|
-
this.later();
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
handshake(port) {
|
|
176
|
-
// a freshly plugged board is still booting (about 4 s to the first prompt): askWho repeats until it answers
|
|
177
|
-
return askWho(port, 8000, () => this.stopping);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
attach(port, dev) {
|
|
181
|
-
const { ReadlineParser } = lib();
|
|
182
|
-
this.port = port;
|
|
183
|
-
this.dev = dev;
|
|
184
|
-
// read everything: unread lines once filled the C3's USB buffer and its watchdog reset it
|
|
185
|
-
const parser = port.pipe(new ReadlineParser({ delimiter: '\n' }));
|
|
186
|
-
parser.on('data', ln => {
|
|
187
|
-
ln = String(ln).trim();
|
|
188
|
-
if (ln) {
|
|
189
|
-
try { this.onLine(ln); } catch (e) { /* the daemon's problem, not the port's */ }
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
const gone = () => {
|
|
193
|
-
if (this.port !== port) return;
|
|
194
|
-
this.port = null;
|
|
195
|
-
this.dev = null;
|
|
196
|
-
this.usb = null;
|
|
197
|
-
this.info = null;
|
|
198
|
-
if (port.isOpen) port.close(() => {});
|
|
199
|
-
this.later();
|
|
200
|
-
};
|
|
201
|
-
port.on('close', gone);
|
|
202
|
-
port.on('error', gone);
|
|
203
|
-
try { this.onConnect(); } catch (e) { /* as above */ }
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
module.exports = { Link, candidates, ports, openPort, askWho, VID, PID };
|
|
1
|
+
'use strict';
|
|
2
|
+
// Desky's board (ESP32-S3-Zero) on USB: find it, open it without resetting it, keep it. docs/firmware_contract.md.
|
|
3
|
+
//
|
|
4
|
+
// The chip's USB-Serial/JTAG resets the chip on a DTR/RTS toggle (RTS up with DTR down is the reset pulse).
|
|
5
|
+
// Windows: serialport opens with hupcl:false as DTR and RTS both disabled, so nothing moves; leave the lines alone.
|
|
6
|
+
// macOS and Linux: open() itself raises both lines. Dropping DTR first would leave RTS up with DTR down for a
|
|
7
|
+
// moment, so RTS goes down first and only then DTR: up/up, up/down, down/down, never the reset state.
|
|
8
|
+
|
|
9
|
+
const VID = '303a'; // Espressif USB JTAG/serial: any S3/C3/C6, so "who" tells Desky from other boards
|
|
10
|
+
const { parseWho } = require('./board');
|
|
11
|
+
const PID = '1001';
|
|
12
|
+
const BAUD = 115200;
|
|
13
|
+
|
|
14
|
+
let serialport = null;
|
|
15
|
+
function lib() {
|
|
16
|
+
if (!serialport) serialport = require('serialport');
|
|
17
|
+
return serialport;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Every 303A:1001 port: { path, serialNumber }. serialNumber is the chip's MAC on the USB-Serial/JTAG, so it
|
|
21
|
+
* stays the same when the port is renamed after a reset. */
|
|
22
|
+
async function ports() {
|
|
23
|
+
try {
|
|
24
|
+
const list = await lib().SerialPort.list();
|
|
25
|
+
return list.filter(p => String(p.vendorId || '').toLowerCase() === VID && String(p.productId || '').toLowerCase() === PID)
|
|
26
|
+
.map(p => ({ path: process.platform === 'darwin' ? p.path.replace(/^\/dev\/tty\./, '/dev/cu.') : p.path,
|
|
27
|
+
serialNumber: p.serialNumber ? String(p.serialNumber) : null }));
|
|
28
|
+
} catch (e) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function candidates() {
|
|
34
|
+
return (await ports()).map(p => p.path);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
38
|
+
const call = (port, fn, ...args) => new Promise((res, rej) => port[fn](...args, e => (e ? rej(e) : res())));
|
|
39
|
+
|
|
40
|
+
async function openPort(dev) {
|
|
41
|
+
const { SerialPort } = lib();
|
|
42
|
+
const port = new SerialPort({ path: dev, baudRate: BAUD, autoOpen: false, hupcl: false });
|
|
43
|
+
port.on('error', () => {}); // an unplug mid-handshake must not throw; attach() listens for real
|
|
44
|
+
await call(port, 'open');
|
|
45
|
+
if (process.platform !== 'win32') {
|
|
46
|
+
await call(port, 'set', { dtr: true, rts: false });
|
|
47
|
+
await call(port, 'set', { dtr: false, rts: false });
|
|
48
|
+
}
|
|
49
|
+
return port;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Ask "who" once a second until a Desky line comes back (contract, section 2): the parsed answer, or null after ms.
|
|
53
|
+
* stop() lets the caller give up early. Leaves the port open and unpiped. */
|
|
54
|
+
function askWho(port, ms = 8000, stop = () => false) {
|
|
55
|
+
const { ReadlineParser } = lib();
|
|
56
|
+
const parser = port.pipe(new ReadlineParser({ delimiter: '\n' }));
|
|
57
|
+
return new Promise(resolve => {
|
|
58
|
+
let ask = null;
|
|
59
|
+
let give = null;
|
|
60
|
+
const done = ok => {
|
|
61
|
+
clearInterval(ask);
|
|
62
|
+
clearTimeout(give);
|
|
63
|
+
parser.removeAllListeners('data');
|
|
64
|
+
port.unpipe(parser);
|
|
65
|
+
resolve(ok);
|
|
66
|
+
};
|
|
67
|
+
parser.on('data', ln => {
|
|
68
|
+
const info = parseWho(ln);
|
|
69
|
+
if (info) done(info);
|
|
70
|
+
});
|
|
71
|
+
const who = () => {
|
|
72
|
+
if (stop()) return done(null);
|
|
73
|
+
try { port.write('who\n'); } catch (e) { done(null); }
|
|
74
|
+
};
|
|
75
|
+
who();
|
|
76
|
+
ask = setInterval(who, 1000);
|
|
77
|
+
give = setTimeout(() => done(null), ms);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class Link {
|
|
82
|
+
/** Keeps the port open. onConnect() runs after every (re)connect, once "who" has answered; lines from the C3
|
|
83
|
+
* go to onLine(). send() drops the line when nothing is connected. */
|
|
84
|
+
constructor({ onConnect = () => {}, onLine = () => {}, onSilent = () => {}, retry = 3000 } = {}) {
|
|
85
|
+
this.onConnect = onConnect;
|
|
86
|
+
this.onSilent = onSilent; // a 303A:1001 port that did not answer "who": maybe an interrupted update
|
|
87
|
+
this.onLine = onLine;
|
|
88
|
+
this.retry = retry;
|
|
89
|
+
this.port = null;
|
|
90
|
+
this.dev = null;
|
|
91
|
+
this.usb = null; // { path, serialNumber } of the connected port
|
|
92
|
+
this.info = null; // the board's "who": { proto, fw, chip, sn }
|
|
93
|
+
this.stopping = false;
|
|
94
|
+
this.timer = null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
get connected() {
|
|
98
|
+
return this.port !== null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
start() {
|
|
102
|
+
this.stopping = false;
|
|
103
|
+
this.scan();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async stop() {
|
|
107
|
+
this.stopping = true;
|
|
108
|
+
clearTimeout(this.timer);
|
|
109
|
+
await this.drop();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
send(line) {
|
|
113
|
+
if (!this.port) return false;
|
|
114
|
+
try {
|
|
115
|
+
this.port.write(line + '\n');
|
|
116
|
+
return true;
|
|
117
|
+
} catch (e) {
|
|
118
|
+
this.drop();
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Restart the C3: an RTS pulse with DTR (BOOT) low. The chip drops off USB for a moment; the port closes, the
|
|
124
|
+
* scan finds it again and onConnect() says the state again. */
|
|
125
|
+
async reset() {
|
|
126
|
+
const port = this.port;
|
|
127
|
+
if (!port) return false;
|
|
128
|
+
try {
|
|
129
|
+
await call(port, 'set', { dtr: false, rts: true });
|
|
130
|
+
await sleep(120);
|
|
131
|
+
await call(port, 'set', { dtr: false, rts: false });
|
|
132
|
+
return true;
|
|
133
|
+
} catch (e) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async drop() {
|
|
139
|
+
const port = this.port;
|
|
140
|
+
this.port = null;
|
|
141
|
+
this.dev = null;
|
|
142
|
+
this.usb = null;
|
|
143
|
+
this.info = null;
|
|
144
|
+
if (port && port.isOpen) await call(port, 'close').catch(() => {});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
later() {
|
|
148
|
+
if (!this.stopping) this.timer = setTimeout(() => this.scan(), this.retry);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async scan() {
|
|
152
|
+
if (this.stopping || this.port) return;
|
|
153
|
+
for (const usb of await ports()) {
|
|
154
|
+
const dev = usb.path;
|
|
155
|
+
let port;
|
|
156
|
+
try {
|
|
157
|
+
port = await openPort(dev);
|
|
158
|
+
} catch (e) {
|
|
159
|
+
continue; // held by the remote or a serial monitor
|
|
160
|
+
}
|
|
161
|
+
const info = await this.handshake(port);
|
|
162
|
+
if (info && !this.stopping) {
|
|
163
|
+
this.info = info;
|
|
164
|
+
this.usb = usb;
|
|
165
|
+
this.attach(port, dev);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
await call(port, 'close').catch(() => {});
|
|
169
|
+
if (this.stopping) return; // stop() came while asking: the port is someone else's now
|
|
170
|
+
try { this.onSilent(usb); } catch (e) { /* the daemon's problem */ }
|
|
171
|
+
}
|
|
172
|
+
this.later();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
handshake(port) {
|
|
176
|
+
// a freshly plugged board is still booting (about 4 s to the first prompt): askWho repeats until it answers
|
|
177
|
+
return askWho(port, 8000, () => this.stopping);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
attach(port, dev) {
|
|
181
|
+
const { ReadlineParser } = lib();
|
|
182
|
+
this.port = port;
|
|
183
|
+
this.dev = dev;
|
|
184
|
+
// read everything: unread lines once filled the C3's USB buffer and its watchdog reset it
|
|
185
|
+
const parser = port.pipe(new ReadlineParser({ delimiter: '\n' }));
|
|
186
|
+
parser.on('data', ln => {
|
|
187
|
+
ln = String(ln).trim();
|
|
188
|
+
if (ln) {
|
|
189
|
+
try { this.onLine(ln); } catch (e) { /* the daemon's problem, not the port's */ }
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
const gone = () => {
|
|
193
|
+
if (this.port !== port) return;
|
|
194
|
+
this.port = null;
|
|
195
|
+
this.dev = null;
|
|
196
|
+
this.usb = null;
|
|
197
|
+
this.info = null;
|
|
198
|
+
if (port.isOpen) port.close(() => {});
|
|
199
|
+
this.later();
|
|
200
|
+
};
|
|
201
|
+
port.on('close', gone);
|
|
202
|
+
port.on('error', gone);
|
|
203
|
+
try { this.onConnect(); } catch (e) { /* as above */ }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
module.exports = { Link, candidates, ports, openPort, askWho, VID, PID };
|
package/src/paths.js
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
// Where Desky keeps things. Everything lives in one folder in the home directory (~/.clawd-desk), not in
|
|
3
|
-
// %LOCALAPPDATA%: hooks started by the Claude desktop app on Windows (an MSIX package) see a virtualised AppData.
|
|
4
|
-
// CLAWD_HOME moves it (tests), CLAWD_PORT overrides the port.
|
|
5
|
-
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const os = require('os');
|
|
8
|
-
const path = require('path');
|
|
9
|
-
|
|
10
|
-
const HOME = process.env.CLAWD_HOME || path.join(os.homedir(), '.clawd-desk');
|
|
11
|
-
const STATE = path.join(HOME, 'state');
|
|
12
|
-
|
|
13
|
-
const paths = {
|
|
14
|
-
HOME,
|
|
15
|
-
STATE,
|
|
16
|
-
CONFIG: path.join(HOME, 'config.json'),
|
|
17
|
-
LAUNCH: path.join(HOME, 'launch.json'), // how to start the app: written by the installer, read by the hook
|
|
18
|
-
STOPPED: path.join(STATE, 'stopped'), // "clawd stop" or "Вийти": hooks must not start the app again
|
|
19
|
-
LOG: path.join(STATE, 'clawd.log'),
|
|
20
|
-
WINDOW: path.join(STATE, 'window.json'),
|
|
21
|
-
DEFAULT_PORT: 47811,
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
paths.port = function port() {
|
|
25
|
-
if (process.env.CLAWD_PORT) return Number(process.env.CLAWD_PORT);
|
|
26
|
-
try {
|
|
27
|
-
const p = Number(JSON.parse(fs.readFileSync(paths.CONFIG, 'utf8')).port);
|
|
28
|
-
if (p) return p;
|
|
29
|
-
} catch (e) { /* no config: the default */ }
|
|
30
|
-
return paths.DEFAULT_PORT;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
paths.markStopped = function markStopped(on) {
|
|
34
|
-
try {
|
|
35
|
-
if (on) {
|
|
36
|
-
fs.mkdirSync(STATE, { recursive: true });
|
|
37
|
-
fs.writeFileSync(paths.STOPPED, '');
|
|
38
|
-
} else {
|
|
39
|
-
fs.rmSync(paths.STOPPED, { force: true });
|
|
40
|
-
}
|
|
41
|
-
} catch (e) { /* best effort */ }
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
module.exports = paths;
|
|
1
|
+
'use strict';
|
|
2
|
+
// Where Desky keeps things. Everything lives in one folder in the home directory (~/.clawd-desk), not in
|
|
3
|
+
// %LOCALAPPDATA%: hooks started by the Claude desktop app on Windows (an MSIX package) see a virtualised AppData.
|
|
4
|
+
// CLAWD_HOME moves it (tests), CLAWD_PORT overrides the port.
|
|
5
|
+
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const os = require('os');
|
|
8
|
+
const path = require('path');
|
|
9
|
+
|
|
10
|
+
const HOME = process.env.CLAWD_HOME || path.join(os.homedir(), '.clawd-desk');
|
|
11
|
+
const STATE = path.join(HOME, 'state');
|
|
12
|
+
|
|
13
|
+
const paths = {
|
|
14
|
+
HOME,
|
|
15
|
+
STATE,
|
|
16
|
+
CONFIG: path.join(HOME, 'config.json'),
|
|
17
|
+
LAUNCH: path.join(HOME, 'launch.json'), // how to start the app: written by the installer, read by the hook
|
|
18
|
+
STOPPED: path.join(STATE, 'stopped'), // "clawd stop" or "Вийти": hooks must not start the app again
|
|
19
|
+
LOG: path.join(STATE, 'clawd.log'),
|
|
20
|
+
WINDOW: path.join(STATE, 'window.json'),
|
|
21
|
+
DEFAULT_PORT: 47811,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
paths.port = function port() {
|
|
25
|
+
if (process.env.CLAWD_PORT) return Number(process.env.CLAWD_PORT);
|
|
26
|
+
try {
|
|
27
|
+
const p = Number(JSON.parse(fs.readFileSync(paths.CONFIG, 'utf8')).port);
|
|
28
|
+
if (p) return p;
|
|
29
|
+
} catch (e) { /* no config: the default */ }
|
|
30
|
+
return paths.DEFAULT_PORT;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
paths.markStopped = function markStopped(on) {
|
|
34
|
+
try {
|
|
35
|
+
if (on) {
|
|
36
|
+
fs.mkdirSync(STATE, { recursive: true });
|
|
37
|
+
fs.writeFileSync(paths.STOPPED, '');
|
|
38
|
+
} else {
|
|
39
|
+
fs.rmSync(paths.STOPPED, { force: true });
|
|
40
|
+
}
|
|
41
|
+
} catch (e) { /* best effort */ }
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
module.exports = paths;
|
package/src/release.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// New versions of the app: npm's "latest" for this package, and the update the control app starts with one button.
|
|
3
|
+
//
|
|
4
|
+
// latest() the version npm calls latest, or throws (offline, the registry is down)
|
|
5
|
+
// newer(a, b) a is a later release than b
|
|
6
|
+
// canUpdate() the Node that installed Desky is still there: the app can update itself
|
|
7
|
+
// start(v) run the installer of version v in a process of its own (src/selfupdate.js): it stops this app,
|
|
8
|
+
// installs, starts the app again with its window open
|
|
9
|
+
// takeResult() what the last update did, once: { state: done | failed, text } or null
|
|
10
|
+
//
|
|
11
|
+
// The update is the very same "npx desky-agent@<v>" a person types: the installer of the new version does the work,
|
|
12
|
+
// this app only starts it and gets out of the way.
|
|
13
|
+
|
|
14
|
+
const fs = require('fs');
|
|
15
|
+
const os = require('os');
|
|
16
|
+
const path = require('path');
|
|
17
|
+
const https = require('https');
|
|
18
|
+
const { spawn } = require('child_process');
|
|
19
|
+
const paths = require('./paths');
|
|
20
|
+
|
|
21
|
+
const PKG = require('../package.json');
|
|
22
|
+
const RESULT = path.join(paths.STATE, 'app-update.json');
|
|
23
|
+
const UPDATE_LOG = path.join(paths.STATE, 'update.log');
|
|
24
|
+
const PLAIN = /^\d+\.\d+\.\d+$/; // a release; anything else never reaches a command line
|
|
25
|
+
|
|
26
|
+
function registry() {
|
|
27
|
+
const r = process.env.npm_config_registry || 'https://registry.npmjs.org/';
|
|
28
|
+
return r.endsWith('/') ? r : r + '/';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function latest({ name = PKG.name, timeout = 10000 } = {}) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const req = https.get(registry() + name + '/latest', {
|
|
34
|
+
timeout, headers: { accept: 'application/json', 'user-agent': `${PKG.name}/${PKG.version}` },
|
|
35
|
+
}, res => {
|
|
36
|
+
if (res.statusCode !== 200) {
|
|
37
|
+
res.resume();
|
|
38
|
+
return reject(new Error('реєстр npm відповів ' + res.statusCode));
|
|
39
|
+
}
|
|
40
|
+
let data = '';
|
|
41
|
+
res.setEncoding('utf8');
|
|
42
|
+
res.on('data', c => { if (data.length < 1 << 20) data += c; });
|
|
43
|
+
res.on('end', () => {
|
|
44
|
+
try {
|
|
45
|
+
const v = JSON.parse(data).version;
|
|
46
|
+
if (!PLAIN.test(v)) throw new Error('дивна версія ' + v);
|
|
47
|
+
resolve(v);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
reject(e);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return undefined;
|
|
53
|
+
});
|
|
54
|
+
req.on('timeout', () => req.destroy(new Error('реєстр npm не відповів')));
|
|
55
|
+
req.on('error', reject);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function newer(a, b) {
|
|
60
|
+
if (!PLAIN.test(String(a)) || !PLAIN.test(String(b))) return false;
|
|
61
|
+
const x = a.split('.').map(Number);
|
|
62
|
+
const y = b.split('.').map(Number);
|
|
63
|
+
for (let i = 0; i < 3; i++) if (x[i] !== y[i]) return x[i] > y[i];
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** npm's own script next to a Node: the update runs npm with that Node, not with whatever is on PATH. */
|
|
68
|
+
function npmCliFor(node) {
|
|
69
|
+
const bin = path.dirname(node);
|
|
70
|
+
return [path.join(bin, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
|
|
71
|
+
path.join(bin, '..', 'lib', 'node_modules', 'npm', 'bin', 'npm-cli.js')].find(f => fs.existsSync(f)) || null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The Node that installed Desky: launch.json says so since 0.3; before that, the hooks in settings.json ran with it.
|
|
75
|
+
* -> { node, npm } or null */
|
|
76
|
+
function runner({ launchFile = paths.LAUNCH, settingsFile = path.join(os.homedir(), '.claude', 'settings.json') } = {}) {
|
|
77
|
+
const cands = [];
|
|
78
|
+
try {
|
|
79
|
+
cands.push(JSON.parse(fs.readFileSync(launchFile, 'utf8')).node);
|
|
80
|
+
} catch (e) { /* not installed */ }
|
|
81
|
+
try {
|
|
82
|
+
const hooks = JSON.parse(fs.readFileSync(settingsFile, 'utf8')).hooks || {};
|
|
83
|
+
for (const h of Object.values(hooks).flat().flatMap(g => (g && g.hooks) || [])) {
|
|
84
|
+
if (/(clawdydesk|desky-agent)[\\/]src[\\/]hook\.js/i.test((h.args || []).join(' '))) cands.push(h.command);
|
|
85
|
+
}
|
|
86
|
+
} catch (e) { /* no settings */ }
|
|
87
|
+
for (const node of cands) {
|
|
88
|
+
if (typeof node !== 'string' || !path.isAbsolute(node) || !fs.existsSync(node)) continue;
|
|
89
|
+
const npm = npmCliFor(node);
|
|
90
|
+
if (npm) return { node, npm };
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function canUpdate() {
|
|
96
|
+
return fs.existsSync(paths.LAUNCH) && Boolean(runner());
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function start(version) {
|
|
100
|
+
if (!PLAIN.test(String(version))) throw new Error('не версія: ' + version);
|
|
101
|
+
const how = runner();
|
|
102
|
+
if (!how) throw new Error('не знайшов Node, яким ставили Desky. Онови з термінала: npx desky-agent@latest');
|
|
103
|
+
fs.mkdirSync(paths.STATE, { recursive: true });
|
|
104
|
+
fs.rmSync(RESULT, { force: true });
|
|
105
|
+
const env = { ...process.env };
|
|
106
|
+
delete env.ELECTRON_RUN_AS_NODE;
|
|
107
|
+
// a console program: hidden, or Windows shows an empty terminal for the whole minute
|
|
108
|
+
const child = spawn(how.node, [path.join(__dirname, 'selfupdate.js'), version, how.npm],
|
|
109
|
+
{ detached: true, stdio: 'ignore', windowsHide: true, cwd: os.homedir(), env });
|
|
110
|
+
child.unref();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function writeResult(r) {
|
|
114
|
+
try {
|
|
115
|
+
fs.mkdirSync(paths.STATE, { recursive: true });
|
|
116
|
+
fs.writeFileSync(RESULT, JSON.stringify({ ...r, at: new Date().toISOString() }));
|
|
117
|
+
} catch (e) { /* the log still says it */ }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function takeResult() {
|
|
121
|
+
try {
|
|
122
|
+
const r = JSON.parse(fs.readFileSync(RESULT, 'utf8'));
|
|
123
|
+
fs.rmSync(RESULT, { force: true });
|
|
124
|
+
return { state: r.ok ? 'done' : 'failed', text: String(r.text || '') };
|
|
125
|
+
} catch (e) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = { latest, newer, runner, npmCliFor, canUpdate, start, writeResult, takeResult, RESULT, UPDATE_LOG,
|
|
131
|
+
PLAIN };
|