desky-agent 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +56 -2
- package/assets/clawd.ico +0 -0
- package/assets/clawd.png +0 -0
- package/bin/cli.js +216 -0
- package/electron/icons.js +79 -0
- package/electron/main.js +244 -0
- package/firmware/boot_app0.bin +0 -0
- package/firmware/bootloader.bin +0 -0
- package/firmware/firmware.bin +0 -0
- package/firmware/manifest.json +33 -0
- package/firmware/partitions.bin +0 -0
- package/package.json +41 -6
- package/src/autostart.js +106 -0
- package/src/board.js +69 -0
- package/src/core.js +193 -0
- package/src/daemon.js +522 -0
- package/src/firmware.js +207 -0
- package/src/flasher.js +113 -0
- package/src/hook.js +92 -0
- package/src/install.js +418 -0
- package/src/link.js +207 -0
- package/src/paths.js +44 -0
- package/src/release.js +131 -0
- package/src/selfupdate.js +58 -0
- package/src/updater.js +138 -0
- package/src/webserial.js +130 -0
- package/ui/app.html +1519 -0
- package/ui/clawd3d.js +377 -0
- package/ui/models/A04_cap_J2.stl +0 -0
- package/ui/models/A04_cap_J3.stl +0 -0
- package/ui/models/A04_cap_J4.stl +0 -0
- package/ui/models/B0_base.stl +0 -0
- package/ui/models/B1_body.stl +0 -0
- package/ui/models/B2_face_plate.stl +0 -0
- package/ui/models/B3_side_left.stl +0 -0
- package/ui/models/B4_side_right.stl +0 -0
- package/ui/models/C1_column.stl +0 -0
- package/ui/models/C2_column_lid.stl +0 -0
- package/ui/models/D1_desk_frame.stl +0 -0
- package/ui/models/D2_desk_top.stl +0 -0
- package/ui/models/L1_laptop.stl +0 -0
- package/ui/models/P1_mug.stl +0 -0
- package/ui/models/P2_plant.stl +0 -0
- package/ui/models/R1_arm_left.stl +0 -0
- package/ui/models/R2_arm_right.stl +0 -0
- package/ui/models/S1_seat.stl +0 -0
- package/ui/models/S2_spacer.stl +0 -0
- package/ui/vendor/OrbitControls.js +1532 -0
- package/ui/vendor/RoomEnvironment.js +144 -0
- package/ui/vendor/STLLoader.js +410 -0
- package/ui/vendor/three.LICENSE +21 -0
- package/ui/vendor/three.module.min.js +6 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "clawd-desk",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"chip": "esp32s3",
|
|
5
|
+
"board": "waveshare-esp32-s3-zero",
|
|
6
|
+
"flash_size": "4MB",
|
|
7
|
+
"parts": [
|
|
8
|
+
{
|
|
9
|
+
"file": "bootloader.bin",
|
|
10
|
+
"address": "0x0",
|
|
11
|
+
"size": 15104,
|
|
12
|
+
"sha256": "e01a2300de23c8d601e9f2a37684b5dbc83ae37132d5a8833e23520afd895f92"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"file": "partitions.bin",
|
|
16
|
+
"address": "0x8000",
|
|
17
|
+
"size": 3072,
|
|
18
|
+
"sha256": "148b959cbff1c38aa8e1d5c0ba9d612c54997b945e56a63f41223eef650653a1"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"file": "boot_app0.bin",
|
|
22
|
+
"address": "0xE000",
|
|
23
|
+
"size": 8192,
|
|
24
|
+
"sha256": "f94c5d786a7a8fab06ac5d10e33bf37711a6697636dc037559ea19cc410a17f0"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"file": "firmware.bin",
|
|
28
|
+
"address": "0x10000",
|
|
29
|
+
"size": 338304,
|
|
30
|
+
"sha256": "a73638b97c94b2ac2885c544b8bac8b8c08b44a42c8016e03fe023548fa636a0"
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
}
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "desky-agent",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "desky-agent",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Desky Agent: Desky, a desk robot that acts out what Claude Code or Codex is doing. Tray app, hooks and installer in one npx command",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"claude-code",
|
|
7
|
+
"claude",
|
|
8
|
+
"codex",
|
|
9
|
+
"hooks",
|
|
10
|
+
"robot",
|
|
11
|
+
"esp32",
|
|
12
|
+
"tray",
|
|
13
|
+
"desk-toy"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bin": {
|
|
17
|
+
"desky-agent": "bin/cli.js"
|
|
18
|
+
},
|
|
19
|
+
"main": "electron/main.js",
|
|
20
|
+
"files": [
|
|
21
|
+
"bin",
|
|
22
|
+
"src",
|
|
23
|
+
"electron",
|
|
24
|
+
"ui",
|
|
25
|
+
"assets",
|
|
26
|
+
"firmware"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test": "node --test",
|
|
33
|
+
"prepack": "node scripts/pack-firmware.js",
|
|
34
|
+
"start": "electron . --open"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"electron": "44.9.3",
|
|
38
|
+
"esptool-js": "0.7.0",
|
|
39
|
+
"serialport": "13.0.0"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/autostart.js
ADDED
|
@@ -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, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
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,193 @@
|
|
|
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 };
|