lightman-agent 1.0.18 → 1.0.21

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 (52) hide show
  1. package/agent.config.json +22 -23
  2. package/agent.config.template.json +30 -31
  3. package/bin/cms-agent.js +269 -248
  4. package/package.json +1 -1
  5. package/public/assets/index-CcBNCz6h.css +1 -1
  6. package/public/assets/index-D9QHMG8k.js +1 -1
  7. package/public/assets/index-H-8HDl46.js +1 -1
  8. package/public/assets/index-YodeiCia.css +1 -1
  9. package/public/assets/index-legacy-DWtNM8y7.js +41 -41
  10. package/public/assets/polyfills-legacy-DyVYWHbW.js +4 -4
  11. package/scripts/guardian.ps1 +50 -124
  12. package/scripts/install-windows.ps1 +60 -116
  13. package/scripts/lightman-agent.logrotate +12 -12
  14. package/scripts/lightman-agent.service +38 -38
  15. package/scripts/reinstall-windows.ps1 +26 -26
  16. package/scripts/restore-desktop.ps1 +32 -32
  17. package/scripts/setup.ps1 +17 -22
  18. package/scripts/sync-display.mjs +20 -20
  19. package/scripts/uninstall-windows.ps1 +54 -54
  20. package/src/commands/display.ts +177 -177
  21. package/src/commands/kiosk.ts +113 -113
  22. package/src/commands/maintenance.ts +106 -106
  23. package/src/commands/network.ts +129 -129
  24. package/src/commands/power.ts +163 -163
  25. package/src/commands/rpi.ts +45 -45
  26. package/src/commands/screenshot.ts +166 -166
  27. package/src/commands/serial.ts +17 -17
  28. package/src/commands/update.ts +124 -124
  29. package/src/index.ts +173 -90
  30. package/src/lib/config.ts +2 -3
  31. package/src/lib/identity.ts +40 -40
  32. package/src/lib/logger.ts +137 -137
  33. package/src/lib/platform.ts +10 -10
  34. package/src/lib/rpi.ts +180 -180
  35. package/src/lib/screenMap.ts +135 -0
  36. package/src/lib/screens.ts +128 -128
  37. package/src/lib/types.ts +176 -177
  38. package/src/services/commands.ts +107 -107
  39. package/src/services/health.ts +161 -161
  40. package/src/services/localEvents.ts +60 -60
  41. package/src/services/logForwarder.ts +72 -72
  42. package/src/services/multiScreenKiosk.ts +116 -83
  43. package/src/services/oscBridge.ts +186 -186
  44. package/src/services/powerScheduler.ts +260 -260
  45. package/src/services/provisioning.ts +120 -122
  46. package/src/services/serialBridge.ts +230 -230
  47. package/src/services/serviceLauncher.ts +183 -183
  48. package/src/services/staticServer.ts +226 -226
  49. package/src/services/updater.ts +249 -249
  50. package/src/services/watchdog.ts +310 -310
  51. package/src/services/websocket.ts +152 -152
  52. package/tsconfig.json +28 -28
package/src/lib/types.ts CHANGED
@@ -1,177 +1,176 @@
1
- // --- Agent Configuration ---
2
- export interface AgentConfig {
3
- serverUrl: string;
4
- deviceSlug: string;
5
- healthIntervalMs: number;
6
- logLevel: 'debug' | 'info' | 'warn' | 'error';
7
- logFile: string;
8
- identityFile: string;
9
- pairingTimeoutSeconds?: number;
10
- /** When false, agent runs in kiosk-only mode — no local server/display processes. Default: true */
11
- localServices: boolean;
12
- kiosk?: KioskConfig;
13
- screenshot?: ScreenshotConfig;
14
- powerSchedule?: PowerScheduleConfig;
15
- /** Port for the local hardware event WebSocket server (default: 3402) */
16
- localEventsPort?: number;
17
- }
18
-
19
- // --- Device Identity (persisted locally) ---
20
- export interface Identity {
21
- deviceId: string;
22
- apiKey: string;
23
- }
24
-
25
- // --- WebSocket Messages ---
26
- export interface WsMessage {
27
- type: string;
28
- payload?: Record<string, unknown>;
29
- timestamp: number;
30
- }
31
-
32
- // --- Health Report ---
33
- export interface HealthReport {
34
- cpuUsage: number;
35
- memTotal: number;
36
- memUsed: number;
37
- memPercent: number;
38
- diskTotal: number;
39
- diskUsed: number;
40
- diskPercent: number;
41
- cpuTemp: number | null;
42
- uptime: number;
43
- agentVersion: string;
44
- // RPi-specific fields (optional, only present on Raspberry Pi)
45
- gpuTemp?: number | null;
46
- throttled?: number | null;
47
- sdCardReadOnly?: boolean;
48
- // Network info (optional)
49
- network?: {
50
- interface: string;
51
- ip: string;
52
- mac: string;
53
- serverLatencyMs: number | null;
54
- };
55
- }
56
-
57
- // --- Command Execution ---
58
- export interface CommandRequest {
59
- id: string;
60
- command: string;
61
- args?: Record<string, unknown>;
62
- timeout?: number;
63
- }
64
-
65
- export interface CommandResult {
66
- id: string;
67
- command: string;
68
- success: boolean;
69
- data?: Record<string, unknown>;
70
- error?: string;
71
- durationMs: number;
72
- }
73
-
74
- // --- Kiosk Configuration ---
75
- export interface KioskConfig {
76
- browserPath: string;
77
- defaultUrl: string;
78
- extraArgs: string[];
79
- pollIntervalMs: number;
80
- maxCrashesInWindow: number;
81
- crashWindowMs: number;
82
- /** Shell replacement mode: Chrome is launched by the Windows shell (lightman-shell.bat),
83
- * not by the agent. Agent only manages URL changes and monitors Chrome via process list. */
84
- shellMode?: boolean;
85
- }
86
-
87
- // --- Multi-Screen Configuration ---
88
-
89
- /** Mapping of a physical screen to a URL (stored in device config, pushed from admin) */
90
- export interface ScreenMapping {
91
- /** Hardware display ID, e.g. "\\\\.\\DISPLAY1" or "HDMI-1" */
92
- hardwareId: string;
93
- /** URL to open on this screen */
94
- url: string;
95
- /** Optional label for admin display */
96
- label?: string;
97
- }
98
-
99
- // --- Kiosk Status ---
100
- export interface KioskStatus {
101
- running: boolean;
102
- pid: number | null;
103
- url: string | null;
104
- crashCount: number;
105
- crashLoopDetected: boolean;
106
- uptimeMs: number | null;
107
- }
108
-
109
- /** Status for multi-screen kiosk */
110
- export interface MultiScreenKioskStatus {
111
- screens: SingleScreenStatus[];
112
- }
113
-
114
- export interface SingleScreenStatus {
115
- hardwareId: string;
116
- url: string | null;
117
- running: boolean;
118
- pid: number | null;
119
- uptimeMs: number | null;
120
- }
121
-
122
- // --- Screenshot Configuration ---
123
- export interface ScreenshotConfig {
124
- captureCommand: string;
125
- quality: number;
126
- uploadEndpoint: string;
127
- }
128
-
129
- // --- Command Handler Function ---
130
- export type CommandHandler = (
131
- args?: Record<string, unknown>
132
- ) => Promise<Record<string, unknown> | void>;
133
-
134
- // --- Log Forwarding ---
135
- export interface LogEntry {
136
- timestamp: string;
137
- level: 'debug' | 'info' | 'warn' | 'error';
138
- message: string;
139
- source: string;
140
- }
141
-
142
- // --- Power Schedule ---
143
- export interface PowerScheduleConfig {
144
- /** Cron expression for shutdown (e.g., "0 19 * * *" = 7 PM daily) */
145
- shutdownCron?: string;
146
- /** Cron expression for startup prep — agent uses this only for logging; actual wake is via WOL */
147
- startupCron?: string;
148
- /** Timezone for cron expressions (e.g., "Asia/Kolkata"). Defaults to system timezone. */
149
- timezone?: string;
150
- /** Seconds before shutdown to warn via WebSocket (default: 60) */
151
- shutdownWarningSeconds?: number;
152
- }
153
-
154
- // --- Watchdog & Self-Healing ---
155
- export interface WatchdogConfig {
156
- checkIntervalMs: number;
157
- kioskCrashCooldownMs: number;
158
- highMemoryThresholdMb: number;
159
- highMemoryCooldownMs: number;
160
- highDiskThresholdPercent: number;
161
- highDiskCooldownMs: number;
162
- wsDisconnectedThresholdMs: number;
163
- wsDisconnectedCooldownMs: number;
164
- }
165
-
166
- export interface CrashReport {
167
- process: string;
168
- exitCode: number | null;
169
- signal: string | null;
170
- timestamp: string;
171
- system: {
172
- memPercent: number;
173
- diskPercent: number;
174
- cpuUsage: number;
175
- uptime: number;
176
- };
177
- }
1
+ // --- Agent Configuration ---
2
+ export interface AgentConfig {
3
+ serverUrl: string;
4
+ deviceSlug: string;
5
+ healthIntervalMs: number;
6
+ logLevel: 'debug' | 'info' | 'warn' | 'error';
7
+ logFile: string;
8
+ identityFile: string;
9
+ /** When false, agent runs in kiosk-only mode — no local server/display processes. Default: true */
10
+ localServices: boolean;
11
+ kiosk?: KioskConfig;
12
+ screenshot?: ScreenshotConfig;
13
+ powerSchedule?: PowerScheduleConfig;
14
+ /** Port for the local hardware event WebSocket server (default: 3402) */
15
+ localEventsPort?: number;
16
+ }
17
+
18
+ // --- Device Identity (persisted locally) ---
19
+ export interface Identity {
20
+ deviceId: string;
21
+ apiKey: string;
22
+ }
23
+
24
+ // --- WebSocket Messages ---
25
+ export interface WsMessage {
26
+ type: string;
27
+ payload?: Record<string, unknown>;
28
+ timestamp: number;
29
+ }
30
+
31
+ // --- Health Report ---
32
+ export interface HealthReport {
33
+ cpuUsage: number;
34
+ memTotal: number;
35
+ memUsed: number;
36
+ memPercent: number;
37
+ diskTotal: number;
38
+ diskUsed: number;
39
+ diskPercent: number;
40
+ cpuTemp: number | null;
41
+ uptime: number;
42
+ agentVersion: string;
43
+ // RPi-specific fields (optional, only present on Raspberry Pi)
44
+ gpuTemp?: number | null;
45
+ throttled?: number | null;
46
+ sdCardReadOnly?: boolean;
47
+ // Network info (optional)
48
+ network?: {
49
+ interface: string;
50
+ ip: string;
51
+ mac: string;
52
+ serverLatencyMs: number | null;
53
+ };
54
+ }
55
+
56
+ // --- Command Execution ---
57
+ export interface CommandRequest {
58
+ id: string;
59
+ command: string;
60
+ args?: Record<string, unknown>;
61
+ timeout?: number;
62
+ }
63
+
64
+ export interface CommandResult {
65
+ id: string;
66
+ command: string;
67
+ success: boolean;
68
+ data?: Record<string, unknown>;
69
+ error?: string;
70
+ durationMs: number;
71
+ }
72
+
73
+ // --- Kiosk Configuration ---
74
+ export interface KioskConfig {
75
+ browserPath: string;
76
+ defaultUrl: string;
77
+ extraArgs: string[];
78
+ pollIntervalMs: number;
79
+ maxCrashesInWindow: number;
80
+ crashWindowMs: number;
81
+ /** Shell replacement mode: Chrome is launched by the Windows shell (lightman-shell.bat),
82
+ * not by the agent. Agent only manages URL changes and monitors Chrome via process list. */
83
+ shellMode?: boolean;
84
+ }
85
+
86
+ // --- Multi-Screen Configuration ---
87
+
88
+ /** Mapping of a physical screen to a URL (stored in device config, pushed from admin) */
89
+ export interface ScreenMapping {
90
+ /** Hardware display ID, e.g. "\\\\.\\DISPLAY1" or "HDMI-1" */
91
+ hardwareId: string;
92
+ /** URL to open on this screen */
93
+ url: string;
94
+ /** Optional label for admin display */
95
+ label?: string;
96
+ }
97
+
98
+ // --- Kiosk Status ---
99
+ export interface KioskStatus {
100
+ running: boolean;
101
+ pid: number | null;
102
+ url: string | null;
103
+ crashCount: number;
104
+ crashLoopDetected: boolean;
105
+ uptimeMs: number | null;
106
+ }
107
+
108
+ /** Status for multi-screen kiosk */
109
+ export interface MultiScreenKioskStatus {
110
+ screens: SingleScreenStatus[];
111
+ }
112
+
113
+ export interface SingleScreenStatus {
114
+ hardwareId: string;
115
+ url: string | null;
116
+ running: boolean;
117
+ pid: number | null;
118
+ uptimeMs: number | null;
119
+ }
120
+
121
+ // --- Screenshot Configuration ---
122
+ export interface ScreenshotConfig {
123
+ captureCommand: string;
124
+ quality: number;
125
+ uploadEndpoint: string;
126
+ }
127
+
128
+ // --- Command Handler Function ---
129
+ export type CommandHandler = (
130
+ args?: Record<string, unknown>
131
+ ) => Promise<Record<string, unknown> | void>;
132
+
133
+ // --- Log Forwarding ---
134
+ export interface LogEntry {
135
+ timestamp: string;
136
+ level: 'debug' | 'info' | 'warn' | 'error';
137
+ message: string;
138
+ source: string;
139
+ }
140
+
141
+ // --- Power Schedule ---
142
+ export interface PowerScheduleConfig {
143
+ /** Cron expression for shutdown (e.g., "0 19 * * *" = 7 PM daily) */
144
+ shutdownCron?: string;
145
+ /** Cron expression for startup prep — agent uses this only for logging; actual wake is via WOL */
146
+ startupCron?: string;
147
+ /** Timezone for cron expressions (e.g., "Asia/Kolkata"). Defaults to system timezone. */
148
+ timezone?: string;
149
+ /** Seconds before shutdown to warn via WebSocket (default: 60) */
150
+ shutdownWarningSeconds?: number;
151
+ }
152
+
153
+ // --- Watchdog & Self-Healing ---
154
+ export interface WatchdogConfig {
155
+ checkIntervalMs: number;
156
+ kioskCrashCooldownMs: number;
157
+ highMemoryThresholdMb: number;
158
+ highMemoryCooldownMs: number;
159
+ highDiskThresholdPercent: number;
160
+ highDiskCooldownMs: number;
161
+ wsDisconnectedThresholdMs: number;
162
+ wsDisconnectedCooldownMs: number;
163
+ }
164
+
165
+ export interface CrashReport {
166
+ process: string;
167
+ exitCode: number | null;
168
+ signal: string | null;
169
+ timestamp: string;
170
+ system: {
171
+ memPercent: number;
172
+ diskPercent: number;
173
+ cpuUsage: number;
174
+ uptime: number;
175
+ };
176
+ }
@@ -1,107 +1,107 @@
1
- import type {
2
- CommandRequest,
3
- CommandResult,
4
- CommandHandler,
5
- WsMessage,
6
- } from '../lib/types.js';
7
- import type { WsClient } from './websocket.js';
8
- import type { Logger } from '../lib/logger.js';
9
-
10
- export class CommandExecutor {
11
- private registry = new Map<string, CommandHandler>();
12
- private wsClient: WsClient;
13
- private logger: Logger;
14
-
15
- constructor(wsClient: WsClient, logger: Logger) {
16
- this.wsClient = wsClient;
17
- this.logger = logger;
18
- }
19
-
20
- register(command: string, handler: CommandHandler): void {
21
- this.registry.set(command, handler);
22
- this.logger.debug(`Command registered: ${command}`);
23
- }
24
-
25
- getRegisteredCommands(): string[] {
26
- return Array.from(this.registry.keys());
27
- }
28
-
29
- /**
30
- * Handle an incoming command message from the server.
31
- * Lifecycle: validate → ack → execute → result
32
- */
33
- async handleCommand(msg: WsMessage): Promise<void> {
34
- const request = msg.payload as unknown as CommandRequest;
35
-
36
- if (!request || !request.id || !request.command) {
37
- this.logger.warn('Invalid command request:', msg);
38
- return;
39
- }
40
-
41
- this.logger.info(`Command received: ${request.command} (${request.id})`);
42
-
43
- // Check if command is registered
44
- const handler = this.registry.get(request.command);
45
- if (!handler) {
46
- this.sendResult({
47
- id: request.id,
48
- command: request.command,
49
- success: false,
50
- error: `Unknown command: ${request.command}`,
51
- durationMs: 0,
52
- });
53
- return;
54
- }
55
-
56
- // Send ack
57
- this.wsClient.send({
58
- type: 'agent:command_ack',
59
- payload: { id: request.id, command: request.command },
60
- timestamp: Date.now(),
61
- });
62
-
63
- // Execute with timeout
64
- const start = Date.now();
65
- const timeout = request.timeout || 30_000;
66
- let timeoutId: NodeJS.Timeout | undefined;
67
-
68
- try {
69
- const data = await Promise.race([
70
- handler(request.args),
71
- new Promise<never>((_, reject) => {
72
- timeoutId = setTimeout(() => reject(new Error('Command timed out')), timeout);
73
- }),
74
- ]);
75
- clearTimeout(timeoutId);
76
-
77
- this.sendResult({
78
- id: request.id,
79
- command: request.command,
80
- success: true,
81
- data: (data as Record<string, unknown>) || {},
82
- durationMs: Date.now() - start,
83
- });
84
- } catch (err) {
85
- clearTimeout(timeoutId);
86
- this.sendResult({
87
- id: request.id,
88
- command: request.command,
89
- success: false,
90
- error: err instanceof Error ? err.message : String(err),
91
- durationMs: Date.now() - start,
92
- });
93
- }
94
- }
95
-
96
- private sendResult(result: CommandResult): void {
97
- this.logger.info(
98
- `Command result: ${result.command} (${result.id}) → ${result.success ? 'OK' : 'FAIL'} in ${result.durationMs}ms`
99
- );
100
-
101
- this.wsClient.send({
102
- type: 'agent:command_result',
103
- payload: result as unknown as Record<string, unknown>,
104
- timestamp: Date.now(),
105
- });
106
- }
107
- }
1
+ import type {
2
+ CommandRequest,
3
+ CommandResult,
4
+ CommandHandler,
5
+ WsMessage,
6
+ } from '../lib/types.js';
7
+ import type { WsClient } from './websocket.js';
8
+ import type { Logger } from '../lib/logger.js';
9
+
10
+ export class CommandExecutor {
11
+ private registry = new Map<string, CommandHandler>();
12
+ private wsClient: WsClient;
13
+ private logger: Logger;
14
+
15
+ constructor(wsClient: WsClient, logger: Logger) {
16
+ this.wsClient = wsClient;
17
+ this.logger = logger;
18
+ }
19
+
20
+ register(command: string, handler: CommandHandler): void {
21
+ this.registry.set(command, handler);
22
+ this.logger.debug(`Command registered: ${command}`);
23
+ }
24
+
25
+ getRegisteredCommands(): string[] {
26
+ return Array.from(this.registry.keys());
27
+ }
28
+
29
+ /**
30
+ * Handle an incoming command message from the server.
31
+ * Lifecycle: validate → ack → execute → result
32
+ */
33
+ async handleCommand(msg: WsMessage): Promise<void> {
34
+ const request = msg.payload as unknown as CommandRequest;
35
+
36
+ if (!request || !request.id || !request.command) {
37
+ this.logger.warn('Invalid command request:', msg);
38
+ return;
39
+ }
40
+
41
+ this.logger.info(`Command received: ${request.command} (${request.id})`);
42
+
43
+ // Check if command is registered
44
+ const handler = this.registry.get(request.command);
45
+ if (!handler) {
46
+ this.sendResult({
47
+ id: request.id,
48
+ command: request.command,
49
+ success: false,
50
+ error: `Unknown command: ${request.command}`,
51
+ durationMs: 0,
52
+ });
53
+ return;
54
+ }
55
+
56
+ // Send ack
57
+ this.wsClient.send({
58
+ type: 'agent:command_ack',
59
+ payload: { id: request.id, command: request.command },
60
+ timestamp: Date.now(),
61
+ });
62
+
63
+ // Execute with timeout
64
+ const start = Date.now();
65
+ const timeout = request.timeout || 30_000;
66
+ let timeoutId: NodeJS.Timeout | undefined;
67
+
68
+ try {
69
+ const data = await Promise.race([
70
+ handler(request.args),
71
+ new Promise<never>((_, reject) => {
72
+ timeoutId = setTimeout(() => reject(new Error('Command timed out')), timeout);
73
+ }),
74
+ ]);
75
+ clearTimeout(timeoutId);
76
+
77
+ this.sendResult({
78
+ id: request.id,
79
+ command: request.command,
80
+ success: true,
81
+ data: (data as Record<string, unknown>) || {},
82
+ durationMs: Date.now() - start,
83
+ });
84
+ } catch (err) {
85
+ clearTimeout(timeoutId);
86
+ this.sendResult({
87
+ id: request.id,
88
+ command: request.command,
89
+ success: false,
90
+ error: err instanceof Error ? err.message : String(err),
91
+ durationMs: Date.now() - start,
92
+ });
93
+ }
94
+ }
95
+
96
+ private sendResult(result: CommandResult): void {
97
+ this.logger.info(
98
+ `Command result: ${result.command} (${result.id}) → ${result.success ? 'OK' : 'FAIL'} in ${result.durationMs}ms`
99
+ );
100
+
101
+ this.wsClient.send({
102
+ type: 'agent:command_result',
103
+ payload: result as unknown as Record<string, unknown>,
104
+ timestamp: Date.now(),
105
+ });
106
+ }
107
+ }