desky-agent 0.0.0-stage → 0.3.0

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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +49 -2
  3. package/assets/clawd.ico +0 -0
  4. package/assets/clawd.png +0 -0
  5. package/bin/cli.js +206 -0
  6. package/electron/icons.js +79 -0
  7. package/electron/main.js +219 -0
  8. package/firmware/boot_app0.bin +0 -0
  9. package/firmware/bootloader.bin +0 -0
  10. package/firmware/firmware.bin +0 -0
  11. package/firmware/manifest.json +33 -0
  12. package/firmware/partitions.bin +0 -0
  13. package/package.json +38 -4
  14. package/src/autostart.js +106 -0
  15. package/src/board.js +69 -0
  16. package/src/core.js +172 -0
  17. package/src/daemon.js +446 -0
  18. package/src/firmware.js +207 -0
  19. package/src/flasher.js +113 -0
  20. package/src/hook.js +86 -0
  21. package/src/install.js +375 -0
  22. package/src/link.js +207 -0
  23. package/src/paths.js +44 -0
  24. package/src/updater.js +138 -0
  25. package/src/webserial.js +130 -0
  26. package/ui/app.html +1470 -0
  27. package/ui/clawd3d.js +375 -0
  28. package/ui/models/A04_cap_J2.stl +0 -0
  29. package/ui/models/A04_cap_J3.stl +0 -0
  30. package/ui/models/A04_cap_J4.stl +0 -0
  31. package/ui/models/B0_base.stl +0 -0
  32. package/ui/models/B1_body.stl +0 -0
  33. package/ui/models/B2_face_plate.stl +0 -0
  34. package/ui/models/B3_side_left.stl +0 -0
  35. package/ui/models/B4_side_right.stl +0 -0
  36. package/ui/models/C1_column.stl +0 -0
  37. package/ui/models/C2_column_lid.stl +0 -0
  38. package/ui/models/D1_desk_frame.stl +0 -0
  39. package/ui/models/D2_desk_top.stl +0 -0
  40. package/ui/models/L1_laptop.stl +0 -0
  41. package/ui/models/P1_mug.stl +0 -0
  42. package/ui/models/P2_plant.stl +0 -0
  43. package/ui/models/R1_arm_left.stl +0 -0
  44. package/ui/models/R2_arm_right.stl +0 -0
  45. package/ui/models/S1_seat.stl +0 -0
  46. package/ui/models/S2_spacer.stl +0 -0
  47. package/ui/vendor/OrbitControls.js +1532 -0
  48. package/ui/vendor/RoomEnvironment.js +144 -0
  49. package/ui/vendor/STLLoader.js +410 -0
  50. package/ui/vendor/three.LICENSE +21 -0
  51. package/ui/vendor/three.module.min.js +6 -0
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "desky-agent",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.0",
4
+ "description": "Desky Agent: Desky, a desk robot that acts out what Claude Code is doing. Tray app, hooks and installer in one npx command",
5
+ "keywords": [
6
+ "claude-code",
7
+ "claude",
8
+ "hooks",
9
+ "robot",
10
+ "esp32",
11
+ "tray",
12
+ "desk-toy"
13
+ ],
14
+ "license": "MIT",
15
+ "bin": {
16
+ "desky-agent": "bin/cli.js"
17
+ },
18
+ "main": "electron/main.js",
19
+ "files": [
20
+ "bin",
21
+ "src",
22
+ "electron",
23
+ "ui",
24
+ "assets",
25
+ "firmware"
26
+ ],
27
+ "engines": {
28
+ "node": ">=18"
29
+ },
30
+ "scripts": {
31
+ "test": "node --test",
32
+ "prepack": "node scripts/pack-firmware.js",
33
+ "start": "electron . --open"
34
+ },
35
+ "dependencies": {
36
+ "electron": "44.9.3",
37
+ "esptool-js": "0.7.0",
38
+ "serialport": "13.0.0"
39
+ }
40
+ }
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+ // Start Desky at login, on each system its own way. Used by the installer and by the tray's checkbox.
3
+ // Windows: HKCU\...\Run "Desky Agent" macOS: ~/Library/LaunchAgents/desk.clawd.app.plist
4
+ // Linux: ~/.config/autostart/clawd.desktop
5
+ // No KeepAlive anywhere: "Выйти" and "desky-agent stop" must stay stopped until the next login.
6
+
7
+ const fs = require('fs');
8
+ const os = require('os');
9
+ const path = require('path');
10
+ const { spawnSync } = require('child_process');
11
+ const paths = require('./paths');
12
+
13
+ const RUN_KEY = 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run';
14
+ const RUN_NAME = 'Desky Agent', OLD_RUN_NAME = 'Clawd'; // the Run value; "Clawd": 0.2.4 and older
15
+ const LABEL = 'desk.clawd.app';
16
+ const OLD_LABEL = 'desk.clawd.agent'; // the Python agent's LaunchAgent
17
+ const agents = () => path.join(os.homedir(), 'Library', 'LaunchAgents');
18
+ const PLIST = () => path.join(agents(), LABEL + '.plist');
19
+ const DESKTOP = () => path.join(process.env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config'), 'autostart',
20
+ 'clawd.desktop');
21
+
22
+ function launchInfo() {
23
+ return JSON.parse(fs.readFileSync(paths.LAUNCH, 'utf8'));
24
+ }
25
+
26
+ const xml = s => String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
27
+
28
+ function plist(command, args) {
29
+ const items = [command, ...args].map(a => `\t\t<string>${xml(a)}</string>`).join('\n');
30
+ return `<?xml version="1.0" encoding="UTF-8"?>
31
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
32
+ <plist version="1.0">
33
+ <dict>
34
+ \t<key>Label</key>
35
+ \t<string>${LABEL}</string>
36
+ \t<key>ProgramArguments</key>
37
+ \t<array>
38
+ ${items}
39
+ \t</array>
40
+ \t<key>RunAtLoad</key>
41
+ \t<true/>
42
+ \t<key>ProcessType</key>
43
+ \t<string>Interactive</string>
44
+ </dict>
45
+ </plist>
46
+ `;
47
+ }
48
+
49
+ // a .desktop Exec line: quote every argument, escape what the spec says to
50
+ const execLine = argv => argv.map(a => '"' + String(a).replace(/(["`$\\])/g, '\\$1') + '"').join(' ');
51
+
52
+ function desktopEntry(command, args, extra = '') {
53
+ return `[Desktop Entry]
54
+ Type=Application
55
+ Name=Desky Agent
56
+ Comment=Desky shows what Claude Code is doing
57
+ Exec=${execLine([command, ...args])}
58
+ Icon=${path.join(paths.HOME, 'clawd.png')}
59
+ Terminal=false
60
+ Categories=Utility;
61
+ ${extra}`;
62
+ }
63
+
64
+ function winQuote(a) {
65
+ return /[\s"]/.test(a) ? '"' + a.replace(/"/g, '\\"') + '"' : a;
66
+ }
67
+
68
+ function enable() {
69
+ const { command, args = [] } = launchInfo();
70
+ const argv = [...args, '--login'];
71
+ if (process.platform === 'win32') {
72
+ const value = [command, ...argv].map(winQuote).join(' ');
73
+ spawnSync('reg', ['delete', RUN_KEY, '/v', OLD_RUN_NAME, '/f'], { windowsHide: true, stdio: 'ignore' });
74
+ spawnSync('reg', ['add', RUN_KEY, '/v', RUN_NAME, '/t', 'REG_SZ', '/d', value, '/f'], { windowsHide: true });
75
+ } else if (process.platform === 'darwin') {
76
+ fs.mkdirSync(agents(), { recursive: true });
77
+ fs.writeFileSync(PLIST(), plist(command, argv));
78
+ } else {
79
+ fs.mkdirSync(path.dirname(DESKTOP()), { recursive: true });
80
+ fs.writeFileSync(DESKTOP(), desktopEntry(command, argv, 'X-GNOME-Autostart-enabled=true\n'));
81
+ }
82
+ }
83
+
84
+ function disable() {
85
+ if (process.platform === 'win32') {
86
+ for (const v of [RUN_NAME, OLD_RUN_NAME]) spawnSync('reg', ['delete', RUN_KEY, '/v', v, '/f'], { windowsHide: true, stdio: 'ignore' });
87
+ } else if (process.platform === 'darwin') {
88
+ fs.rmSync(PLIST(), { force: true });
89
+ } else {
90
+ fs.rmSync(DESKTOP(), { force: true });
91
+ }
92
+ }
93
+
94
+ function isEnabled() {
95
+ if (process.platform === 'win32') {
96
+ return spawnSync('reg', ['query', RUN_KEY, '/v', RUN_NAME], { windowsHide: true, stdio: 'ignore' }).status === 0;
97
+ }
98
+ return fs.existsSync(process.platform === 'darwin' ? PLIST() : DESKTOP());
99
+ }
100
+
101
+ // the tray asks every second; the registry or the disk once is enough until the checkbox changes it
102
+ let cached = null;
103
+ const isEnabledCached = () => (cached === null ? (cached = isEnabled()) : cached);
104
+ const forget = () => { cached = null; };
105
+
106
+ module.exports = { enable, disable, isEnabled, isEnabledCached, forget, plist, desktopEntry, execLine, LABEL, OLD_LABEL, RUN_KEY };
package/src/board.js ADDED
@@ -0,0 +1,69 @@
1
+ 'use strict';
2
+ // The board's "who" answer and what to do about its firmware: docs/firmware_contract.md, sections 2 and 3.
3
+ // Pure functions, no port: the link, the status, doctor and the firmware updater all use them.
4
+
5
+ const PKG_VERSION = require('../package.json').version;
6
+ const PROTO = 3; // the command set this package speaks
7
+
8
+ /** "clawd-desk 3 fw=0.2.0 chip=esp32s3 sn=CD-0007" -> { proto, fw, chip, sn }; null when the line is not Desky's.
9
+ * Protocol 2 boards say only "clawd-desk 2": fw, chip and sn come back null. Unknown fields are kept, order is free. */
10
+ function parseWho(line) {
11
+ const parts = String(line || '').trim().split(/\s+/);
12
+ if (parts[0] !== 'clawd-desk') return null;
13
+ const proto = Number.parseInt(parts[1], 10);
14
+ const info = { proto: Number.isFinite(proto) ? proto : null, fw: null, chip: null, sn: null };
15
+ for (const p of parts.slice(2)) {
16
+ const eq = p.indexOf('=');
17
+ if (eq > 0) info[p.slice(0, eq)] = p.slice(eq + 1);
18
+ }
19
+ if (info.sn === '-') info.sn = null;
20
+ return info;
21
+ }
22
+
23
+ /** semver order, prerelease below its release; -1, 0, 1. Not semver -> null. */
24
+ function compareVersions(a, b) {
25
+ const re = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+.*)?$/;
26
+ const ma = re.exec(String(a || ''));
27
+ const mb = re.exec(String(b || ''));
28
+ if (!ma || !mb) return null;
29
+ for (let i = 1; i <= 3; i++) {
30
+ const d = Number(ma[i]) - Number(mb[i]);
31
+ if (d) return d < 0 ? -1 : 1;
32
+ }
33
+ const pa = ma[4];
34
+ const pb = mb[4];
35
+ if (pa === pb) return 0;
36
+ if (!pa) return 1;
37
+ if (!pb) return -1;
38
+ const xa = pa.split('.');
39
+ const xb = pb.split('.');
40
+ for (let i = 0; i < Math.max(xa.length, xb.length); i++) {
41
+ if (xa[i] === undefined) return -1;
42
+ if (xb[i] === undefined) return 1;
43
+ const na = /^\d+$/.test(xa[i]);
44
+ const nb = /^\d+$/.test(xb[i]);
45
+ if (na && nb && Number(xa[i]) !== Number(xb[i])) return Number(xa[i]) < Number(xb[i]) ? -1 : 1;
46
+ if (na !== nb) return na ? -1 : 1;
47
+ if (xa[i] !== xb[i]) return xa[i] < xb[i] ? -1 : 1;
48
+ }
49
+ return 0;
50
+ }
51
+
52
+ /** The contract's table (section 3). bundled: the firmware version this package carries.
53
+ * -> { action: 'ok' | 'update' | 'newer' | 'upgrade-package' | 'mismatch', why } */
54
+ function verdict(info, bundled = PKG_VERSION, proto = PROTO) {
55
+ if (!info) return { action: 'mismatch', why: 'плата не відповіла як Desky' };
56
+ if (!info.fw) return { action: 'update', why: 'на платі прошивка без версії (протокол ' + info.proto + ')' };
57
+ const c = compareVersions(info.fw, bundled);
58
+ if (c === null) return { action: 'mismatch', why: 'незрозуміла версія прошивки: ' + info.fw };
59
+ if (c < 0) return { action: 'update', why: `прошивка ${info.fw} старша за ${bundled}` };
60
+ if (c > 0) {
61
+ return info.proto === proto
62
+ ? { action: 'newer', why: `прошивка ${info.fw} новіша за пакет ${bundled}, працює` }
63
+ : { action: 'upgrade-package', why: `прошивка ${info.fw} новіша за пакет ${bundled}: npx desky-agent@latest` };
64
+ }
65
+ if (info.proto !== proto) return { action: 'mismatch', why: `та сама версія ${bundled}, але протокол ${info.proto}, а не ${proto}` };
66
+ return { action: 'ok', why: 'прошивка ' + info.fw };
67
+ }
68
+
69
+ module.exports = { parseWho, compareVersions, verdict, PROTO, PKG_VERSION };
package/src/core.js ADDED
@@ -0,0 +1,172 @@
1
+ 'use strict';
2
+ // What Desky shows: Claude Code's hook events in, lines for the C3 out.
3
+ //
4
+ // No timers, no port, no wall clock: the daemon feeds events and ticks, the tests do the same with a fake clock.
5
+ // The firmware acts a state out until the next one; everything about time lives here (how long "done" lasts, when
6
+ // to sleep, the ask trill repeats, the way back after an error, quiet hours). Times are in seconds.
7
+
8
+ // the colours Desky is printed in; the app and the face on his screen follow the one chosen (ui/app.html, PALETTE)
9
+ const COLORS = ['orange', 'white', 'mint', 'blue', 'graphite'];
10
+
11
+ const DEFAULTS = {
12
+ color: 'orange', // which Desky this is: his body colour
13
+ quiet_hours: [23, 8], // no sound from 23:00 to 08:00; [0, 0] = never quiet
14
+ done_secs: 120, // "done" looks at you this long, then idle
15
+ sleep_secs: 600, // no events this long: sleep
16
+ ask_repeat_secs: 120, // the ask trill again after this long ...
17
+ ask_times: 3, // ... at most this many times in all
18
+ session_ttl_secs: 1800, // a done or idle session silent this long stops counting
19
+ // A session killed mid-task (PC or app restarted, terminal closed) never sends Stop or SessionEnd and would keep
20
+ // Desky "working". The longest tool call is a 10 min Bash, so a working session silent for 11 min is gone.
21
+ busy_ttl_secs: 660,
22
+ ask_ttl_secs: 3600, // nobody answered for an hour: that question is not coming back
23
+ listen_secs: 1.5, // the nod after your message, then work
24
+ error_secs: 1.2,
25
+ wake_secs: 2.5,
26
+ read_tools: ['Read', 'Grep', 'Glob', 'WebFetch', 'WebSearch', 'LS', 'NotebookRead', 'ToolSearch'],
27
+ ask_tools: ['AskUserQuestion'],
28
+ port: 47811,
29
+ };
30
+
31
+ // what wins when sessions disagree
32
+ const RANK = { ask: 5, work: 4, read: 3, done: 2, idle: 1 };
33
+ const SOUND_ON_ENTER = { wake: 'wake', listen: 'tick', error: 'error', done: 'done', sleep: 'sleep', ask: 'ask' };
34
+
35
+ const now = () => Date.now() / 1000;
36
+ const localHour = t => new Date(t * 1000).getHours();
37
+
38
+ class Core {
39
+ constructor(send, { clock = now, config = {}, hour = localHour } = {}) {
40
+ this.send = send;
41
+ this.clock = clock;
42
+ this.hour = hour;
43
+ this.cfg = { ...DEFAULTS, ...config };
44
+ this.sessions = new Map(); // id -> { state, t, doneT }
45
+ this.overlay = null; // [state, until]: wake / listen / error play over the computed state
46
+ this.lastEvent = clock();
47
+ this.shown = null; // the last state sent
48
+ this.asksPlayed = 0;
49
+ this.nextAsk = null;
50
+ }
51
+
52
+ // ------------------------------------------------------------ events
53
+ event(ev) {
54
+ const t = this.clock();
55
+ const name = ev.hook_event_name || '';
56
+ const sid = ev.session_id || '?';
57
+ const tool = ev.tool_name || '';
58
+ const before = this.display(t);
59
+ this.lastEvent = t;
60
+ if (name === 'SessionEnd') {
61
+ this.sessions.delete(sid);
62
+ this.tick();
63
+ return;
64
+ }
65
+ let s = this.sessions.get(sid);
66
+ if (!s) this.sessions.set(sid, (s = { state: 'idle', t, doneT: null }));
67
+ s.t = t;
68
+ const c = this.cfg;
69
+ if (name === 'SessionStart') {
70
+ set(s, 'idle'); // resumed after a restart: whatever it was doing then is over
71
+ if (before === 'idle' || before === 'sleep') this.overlay = ['wake', t + c.wake_secs];
72
+ } else if (name === 'UserPromptSubmit') {
73
+ set(s, 'work');
74
+ this.overlay = ['listen', t + c.listen_secs];
75
+ } else if (name === 'PreToolUse') {
76
+ if (c.ask_tools.includes(tool)) set(s, 'ask');
77
+ else set(s, c.read_tools.includes(tool) ? 'read' : 'work');
78
+ } else if (name === 'PermissionRequest') {
79
+ set(s, 'ask');
80
+ } else if (name === 'Notification') {
81
+ // idle_prompt comes a minute after Stop ("waiting for your input"): that is "done", not a call for you
82
+ if (ev.notification_type === 'permission_prompt' || ev.notification_type === 'elicitation_dialog') set(s, 'ask');
83
+ } else if (name === 'PostToolUse') {
84
+ if (['ask', 'done', 'idle'].includes(s.state)) set(s, 'work');
85
+ } else if (name === 'PostToolUseFailure') {
86
+ if (['ask', 'done', 'idle'].includes(s.state)) set(s, 'work');
87
+ this.overlay = ['error', t + c.error_secs];
88
+ } else if (['SubagentStop', 'SubagentStart', 'PreCompact'].includes(name)) {
89
+ if (s.state !== 'ask') set(s, 'work');
90
+ } else if (name === 'Stop') {
91
+ set(s, 'done');
92
+ s.doneT = t;
93
+ }
94
+ this.tick();
95
+ }
96
+
97
+ // ------------------------------------------------------------ time
98
+ computed(t) {
99
+ const c = this.cfg;
100
+ let best = null;
101
+ for (const [sid, s] of this.sessions) {
102
+ const ttl = s.state === 'ask' ? c.ask_ttl_secs
103
+ : s.state === 'work' || s.state === 'read' ? c.busy_ttl_secs : c.session_ttl_secs;
104
+ if (t - s.t >= ttl) {
105
+ this.sessions.delete(sid);
106
+ continue;
107
+ }
108
+ if (s.state === 'done' && s.doneT !== null && t - s.doneT >= c.done_secs) set(s, 'idle');
109
+ if (best === null || RANK[s.state] > RANK[best]) best = s.state;
110
+ }
111
+ if ((best === null || best === 'idle') && t - this.lastEvent >= c.sleep_secs) return 'sleep';
112
+ return best || 'idle';
113
+ }
114
+
115
+ display(t) {
116
+ const base = this.computed(t);
117
+ if (this.overlay && t >= this.overlay[1]) this.overlay = null;
118
+ if (this.overlay && base !== 'ask') return this.overlay[0]; // nothing hides a call for you
119
+ return base;
120
+ }
121
+
122
+ tick() {
123
+ const t = this.clock();
124
+ const st = this.display(t);
125
+ if (st !== this.shown) {
126
+ this.shown = st;
127
+ this.send('state ' + st);
128
+ if (st === 'ask') {
129
+ this.asksPlayed = 0;
130
+ this.nextAsk = t;
131
+ }
132
+ if (SOUND_ON_ENTER[st] && st !== 'ask') this.sound(SOUND_ON_ENTER[st], t);
133
+ }
134
+ if (st === 'ask' && this.nextAsk !== null && t >= this.nextAsk) {
135
+ this.sound('ask', t);
136
+ this.asksPlayed += 1;
137
+ this.nextAsk = this.asksPlayed < this.cfg.ask_times ? t + this.cfg.ask_repeat_secs : null;
138
+ }
139
+ }
140
+
141
+ sound(name, t) {
142
+ if (!this.quiet(t)) this.send('sound ' + name);
143
+ }
144
+
145
+ quiet(t) {
146
+ const [a, b] = this.cfg.quiet_hours;
147
+ const h = this.hour(t);
148
+ if (a === b) return false;
149
+ return a > b ? a <= h || h < b : a <= h && h < b;
150
+ }
151
+
152
+ // ------------------------------------------------------------ port
153
+ /** After a (re)connect: the C3 may have restarted, so say the current state again. */
154
+ resend() {
155
+ if (this.shown) this.send('state ' + this.shown);
156
+ }
157
+
158
+ status() {
159
+ const t = this.clock();
160
+ const bySession = {};
161
+ const state = this.display(t);
162
+ for (const [k, s] of this.sessions) bySession[k.slice(0, 8)] = s.state;
163
+ return { state, sessions: this.sessions.size, by_session: bySession };
164
+ }
165
+ }
166
+
167
+ function set(s, state) {
168
+ s.state = state;
169
+ if (state !== 'done') s.doneT = null;
170
+ }
171
+
172
+ module.exports = { Core, DEFAULTS, COLORS };