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/core.js CHANGED
@@ -1,172 +1,193 @@
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 };
1
+ 'use strict';
2
+ // What Desky shows: Claude Code's and Codex's hook events in, lines for the C3 out. Codex sends the same events
3
+ // under the same names (hook.js marks them agent: "codex"), less Notification and PostToolUseFailure, plus Interrupt.
4
+ //
5
+ // No timers, no port, no wall clock: the daemon feeds events and ticks, the tests do the same with a fake clock.
6
+ // The firmware acts a state out until the next one; everything about time lives here (how long "done" lasts, when
7
+ // to sleep, the ask trill repeats, the way back after an error, quiet hours). Times are in seconds.
8
+
9
+ // the colours Desky is printed in; the app and the face on his screen follow the one chosen (ui/app.html, PALETTE)
10
+ const COLORS = ['orange', 'white', 'mint', 'blue', 'graphite'];
11
+
12
+ const DEFAULTS = {
13
+ color: 'orange', // which Desky this is: his body colour
14
+ quiet_hours: [23, 8], // no sound from 23:00 to 08:00; [0, 0] = never quiet
15
+ done_secs: 120, // "done" looks at you this long, then idle
16
+ sleep_secs: 600, // no events this long: sleep
17
+ ask_repeat_secs: 120, // the ask trill again after this long ...
18
+ ask_times: 3, // ... at most this many times in all
19
+ session_ttl_secs: 1800, // a done or idle session silent this long stops counting
20
+ // A session killed mid-task (PC or app restarted, terminal closed) never sends Stop or SessionEnd and would keep
21
+ // Desky "working". The longest tool call is a 10 min Bash, so a working session silent for 11 min is gone.
22
+ busy_ttl_secs: 660,
23
+ ask_ttl_secs: 3600, // nobody answered for an hour: that question is not coming back
24
+ listen_secs: 1.5, // the nod after your message, then work
25
+ error_secs: 1.2,
26
+ wake_secs: 2.5,
27
+ read_tools: ['Read', 'Grep', 'Glob', 'WebFetch', 'WebSearch', 'LS', 'NotebookRead', 'ToolSearch'],
28
+ ask_tools: ['AskUserQuestion', 'request_user_input'],
29
+ // Codex reads with shell commands (tool "Bash"): a command made only of these reads, anything else works.
30
+ // Matched at the start of each part of a command line split at && || ; |, ignoring case.
31
+ read_commands: ['rg', 'grep', 'egrep', 'cat', 'head', 'tail', 'less', 'more', 'ls', 'dir', 'find', 'fd', 'tree',
32
+ 'wc', 'pwd', 'stat', 'file', 'which', 'where', 'type', 'findstr', 'nl', 'sort', 'uniq', 'cut', 'jq', 'sed -n',
33
+ 'cd', 'git status', 'git log', 'git diff', 'git show', 'git blame', 'git grep', 'git ls-files', 'git branch',
34
+ 'git rev-parse', 'get-content', 'gc', 'get-childitem', 'gci', 'select-string', 'sls', 'get-item', 'test-path',
35
+ 'resolve-path', 'select-object', 'sort-object', 'measure-object', 'where-object', 'format-table', 'format-list',
36
+ 'out-string'],
37
+ port: 47811,
38
+ };
39
+
40
+ // what wins when sessions disagree
41
+ const RANK = { ask: 5, work: 4, read: 3, done: 2, idle: 1 };
42
+ const SOUND_ON_ENTER = { wake: 'wake', listen: 'tick', error: 'error', done: 'done', sleep: 'sleep', ask: 'ask' };
43
+
44
+ const now = () => Date.now() / 1000;
45
+ const localHour = t => new Date(t * 1000).getHours();
46
+
47
+ class Core {
48
+ constructor(send, { clock = now, config = {}, hour = localHour } = {}) {
49
+ this.send = send;
50
+ this.clock = clock;
51
+ this.hour = hour;
52
+ this.cfg = { ...DEFAULTS, ...config };
53
+ this.sessions = new Map(); // id -> { state, t, doneT }
54
+ this.overlay = null; // [state, until]: wake / listen / error play over the computed state
55
+ this.lastEvent = clock();
56
+ this.shown = null; // the last state sent
57
+ this.asksPlayed = 0;
58
+ this.nextAsk = null;
59
+ }
60
+
61
+ // ------------------------------------------------------------ events
62
+ event(ev) {
63
+ const t = this.clock();
64
+ const name = ev.hook_event_name || '';
65
+ const sid = ev.session_id || '?';
66
+ const tool = ev.tool_name || '';
67
+ const before = this.display(t);
68
+ this.lastEvent = t;
69
+ if (name === 'SessionEnd') {
70
+ this.sessions.delete(sid);
71
+ this.tick();
72
+ return;
73
+ }
74
+ let s = this.sessions.get(sid);
75
+ if (!s) this.sessions.set(sid, (s = { state: 'idle', t, doneT: null }));
76
+ s.t = t;
77
+ const c = this.cfg;
78
+ if (name === 'SessionStart') {
79
+ set(s, 'idle'); // resumed after a restart: whatever it was doing then is over
80
+ if (before === 'idle' || before === 'sleep') this.overlay = ['wake', t + c.wake_secs];
81
+ } else if (name === 'UserPromptSubmit') {
82
+ set(s, 'work');
83
+ this.overlay = ['listen', t + c.listen_secs];
84
+ } else if (name === 'PreToolUse') {
85
+ if (c.ask_tools.includes(tool)) set(s, 'ask');
86
+ else if (c.read_tools.includes(tool)) set(s, 'read');
87
+ else if (ev.agent === 'codex' && tool === 'Bash' && readsOnly(ev.command, c.read_commands)) set(s, 'read');
88
+ else set(s, 'work');
89
+ } else if (name === 'PermissionRequest') {
90
+ set(s, 'ask');
91
+ } else if (name === 'Notification') {
92
+ // idle_prompt comes a minute after Stop ("waiting for your input"): that is "done", not a call for you
93
+ if (ev.notification_type === 'permission_prompt' || ev.notification_type === 'elicitation_dialog') set(s, 'ask');
94
+ } else if (name === 'PostToolUse') {
95
+ if (['ask', 'done', 'idle'].includes(s.state)) set(s, 'work');
96
+ } else if (name === 'PostToolUseFailure') {
97
+ if (['ask', 'done', 'idle'].includes(s.state)) set(s, 'work');
98
+ this.overlay = ['error', t + c.error_secs];
99
+ } else if (['SubagentStop', 'SubagentStart', 'PreCompact'].includes(name)) {
100
+ if (s.state !== 'ask') set(s, 'work');
101
+ } else if (name === 'Stop') {
102
+ set(s, 'done');
103
+ s.doneT = t;
104
+ } else if (name === 'Interrupt') {
105
+ set(s, 'idle'); // you stopped it yourself: no "done" fanfare
106
+ }
107
+ this.tick();
108
+ }
109
+
110
+ // ------------------------------------------------------------ time
111
+ computed(t) {
112
+ const c = this.cfg;
113
+ let best = null;
114
+ for (const [sid, s] of this.sessions) {
115
+ const ttl = s.state === 'ask' ? c.ask_ttl_secs
116
+ : s.state === 'work' || s.state === 'read' ? c.busy_ttl_secs : c.session_ttl_secs;
117
+ if (t - s.t >= ttl) {
118
+ this.sessions.delete(sid);
119
+ continue;
120
+ }
121
+ if (s.state === 'done' && s.doneT !== null && t - s.doneT >= c.done_secs) set(s, 'idle');
122
+ if (best === null || RANK[s.state] > RANK[best]) best = s.state;
123
+ }
124
+ if ((best === null || best === 'idle') && t - this.lastEvent >= c.sleep_secs) return 'sleep';
125
+ return best || 'idle';
126
+ }
127
+
128
+ display(t) {
129
+ const base = this.computed(t);
130
+ if (this.overlay && t >= this.overlay[1]) this.overlay = null;
131
+ if (this.overlay && base !== 'ask') return this.overlay[0]; // nothing hides a call for you
132
+ return base;
133
+ }
134
+
135
+ tick() {
136
+ const t = this.clock();
137
+ const st = this.display(t);
138
+ if (st !== this.shown) {
139
+ this.shown = st;
140
+ this.send('state ' + st);
141
+ if (st === 'ask') {
142
+ this.asksPlayed = 0;
143
+ this.nextAsk = t;
144
+ }
145
+ if (SOUND_ON_ENTER[st] && st !== 'ask') this.sound(SOUND_ON_ENTER[st], t);
146
+ }
147
+ if (st === 'ask' && this.nextAsk !== null && t >= this.nextAsk) {
148
+ this.sound('ask', t);
149
+ this.asksPlayed += 1;
150
+ this.nextAsk = this.asksPlayed < this.cfg.ask_times ? t + this.cfg.ask_repeat_secs : null;
151
+ }
152
+ }
153
+
154
+ sound(name, t) {
155
+ if (!this.quiet(t)) this.send('sound ' + name);
156
+ }
157
+
158
+ quiet(t) {
159
+ const [a, b] = this.cfg.quiet_hours;
160
+ const h = this.hour(t);
161
+ if (a === b) return false;
162
+ return a > b ? a <= h || h < b : a <= h && h < b;
163
+ }
164
+
165
+ // ------------------------------------------------------------ port
166
+ /** After a (re)connect: the C3 may have restarted, so say the current state again. */
167
+ resend() {
168
+ if (this.shown) this.send('state ' + this.shown);
169
+ }
170
+
171
+ status() {
172
+ const t = this.clock();
173
+ const bySession = {};
174
+ const state = this.display(t);
175
+ for (const [k, s] of this.sessions) bySession[k.slice(0, 8)] = s.state;
176
+ return { state, sessions: this.sessions.size, by_session: bySession };
177
+ }
178
+ }
179
+
180
+ /** True when every part of a shell command line only reads (see read_commands). */
181
+ function readsOnly(command, words) {
182
+ if (typeof command !== 'string' || !command.trim()) return false;
183
+ if (/(^|[^0-9])>/.test(command)) return false; // "> file" writes; "2>/dev/null" does not
184
+ const parts = command.split(/&&|\|\||;|\|/).map(p => p.trim().toLowerCase()).filter(Boolean);
185
+ return parts.length > 0 && parts.every(p => words.some(w => p === w || p.startsWith(w + ' ')));
186
+ }
187
+
188
+ function set(s, state) {
189
+ s.state = state;
190
+ if (state !== 'done') s.doneT = null;
191
+ }
192
+
193
+ module.exports = { Core, DEFAULTS, COLORS, readsOnly };