@wonderwhy-er/desktop-commander 0.3.1-alpha.1 → 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 +20 -20
- package/README.md +1152 -1152
- package/dist/bootstrap.js +0 -0
- package/dist/command-manager.js +0 -0
- package/dist/config-field-definitions.js +0 -0
- package/dist/config-manager.js +0 -0
- package/dist/config-recovery.js +0 -0
- package/dist/config.js +0 -0
- package/dist/custom-stdio.js +0 -0
- package/dist/data/onboarding-prompts.json +70 -70
- package/dist/error-handlers.js +0 -0
- package/dist/handlers/filesystem-handlers.js +2 -2
- package/dist/index.js +0 -0
- package/dist/npm-scripts/remote.js +25 -23
- package/dist/remote-device/desktop-commander-integration.d.ts +1 -0
- package/dist/remote-device/device.js +14 -2
- package/dist/remote-device/realtime-health-telemetry.d.ts +35 -0
- package/dist/remote-device/realtime-health-telemetry.js +110 -0
- package/dist/remote-device/remote-channel.d.ts +66 -55
- package/dist/remote-device/remote-channel.js +336 -389
- package/dist/remote-device/rollouts.d.ts +48 -0
- package/dist/remote-device/rollouts.js +105 -0
- package/dist/remote-device/scripts/blocking-offline-update.js +135 -133
- package/dist/search-manager.js +0 -0
- package/dist/server.js +581 -712
- package/dist/setup-claude-server.js +942 -942
- package/dist/terminal-manager.js +0 -0
- package/dist/tools/edit.js +2 -2
- package/dist/tools/fuzzySearch.js +9 -9
- package/dist/tools/improved-process-tools.js +10 -10
- package/dist/tools/pdf/extract-images.js +3 -7
- package/dist/tools/pdf/lib/pdf2md.js +28 -33
- package/dist/tools/pdf/utils.d.ts +10 -0
- package/dist/tools/pdf/utils.js +11 -0
- package/dist/tools/prompts.js +1 -1
- package/dist/tools/schemas.d.ts +0 -10
- package/dist/tools/schemas.js +0 -8
- package/dist/track-installation.js +387 -387
- package/dist/types.js +0 -0
- package/dist/ui/config-editor/config-editor-runtime.js +53 -53
- package/dist/ui/config-editor/index.html +13 -13
- package/dist/ui/config-editor/src/app.js +30 -30
- package/dist/ui/config-editor/src/array-modal.js +32 -32
- package/dist/ui/config-editor/styles.css +586 -586
- package/dist/ui/contracts.d.ts +27 -0
- package/dist/ui/contracts.js +36 -0
- package/dist/ui/file-preview/index.html +16 -16
- package/dist/ui/file-preview/preview-runtime.js +190 -182
- package/dist/ui/file-preview/src/app.js +8 -8
- package/dist/ui/file-preview/src/components/html-renderer.js +4 -2
- package/dist/ui/file-preview/src/document-layout.js +31 -31
- package/dist/ui/file-preview/src/document-outline.js +8 -8
- package/dist/ui/file-preview/src/markdown/conflict-dialog.js +33 -33
- package/dist/ui/file-preview/src/markdown/controller.js +9 -9
- package/dist/ui/file-preview/src/markdown/editor.js +31 -31
- package/dist/ui/file-preview/styles.css +1630 -1630
- package/dist/ui/resources.js +11 -4
- package/dist/uninstall-claude-server.js +745 -745
- package/dist/utils/files/binary.js +4 -4
- package/dist/utils/files/excel.js +3 -3
- package/dist/utils/files/text.d.ts +7 -0
- package/dist/utils/files/text.js +40 -21
- package/dist/utils/lazy-package.d.ts +5 -3
- package/dist/utils/lazy-package.js +6 -4
- package/dist/utils/system-info.d.ts +1 -0
- package/dist/utils/system-info.js +110 -97
- package/dist/utils/terminal-timestamps.d.ts +2 -0
- package/dist/utils/terminal-timestamps.js +18 -0
- package/dist/utils/usageTracker.d.ts +0 -39
- package/dist/utils/usageTracker.js +16 -176
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/node_modules/@desktop-commander/telemetry-contract/README.md +56 -56
- package/node_modules/@desktop-commander/telemetry-contract/package.json +32 -32
- package/node_modules/@desktop-commander/telemetry-contract/transport.d.ts +38 -38
- package/node_modules/@desktop-commander/telemetry-contract/transport.js +164 -164
- package/package.json +24 -15
- package/vendor/telemetry-contract/README.md +56 -56
- package/vendor/telemetry-contract/package.json +32 -32
- package/vendor/telemetry-contract/transport.d.ts +38 -38
- package/vendor/telemetry-contract/transport.js +164 -164
- package/vendor/telemetry-contract/transport.test.js +39 -39
package/dist/bootstrap.js
CHANGED
|
File without changes
|
package/dist/command-manager.js
CHANGED
|
File without changes
|
|
File without changes
|
package/dist/config-manager.js
CHANGED
|
File without changes
|
package/dist/config-recovery.js
CHANGED
|
File without changes
|
package/dist/config.js
CHANGED
|
File without changes
|
package/dist/custom-stdio.js
CHANGED
|
File without changes
|
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": "2.0.0",
|
|
3
|
-
"description": "Desktop Commander onboarding prompts - V2 simplified (5 prompts only)",
|
|
4
|
-
"prompts": [
|
|
5
|
-
{
|
|
6
|
-
"id": "onb2_01",
|
|
7
|
-
"title": "Organize my Downloads folder",
|
|
8
|
-
"description": "Clean up and organize your messy Downloads folder into relevant subfolders automatically.",
|
|
9
|
-
"prompt": "Let's organize your Downloads folder! \n\nFirst, let me check what we're working with. I'll look at your Downloads folder to see how many files are there and what types.\n\nShould I start by analyzing your Downloads folder?",
|
|
10
|
-
"categories": ["onboarding"],
|
|
11
|
-
"secondaryTag": "Quick Start",
|
|
12
|
-
"votes": 0,
|
|
13
|
-
"gaClicks": 0,
|
|
14
|
-
"icon": "FolderOpen",
|
|
15
|
-
"author": "DC team",
|
|
16
|
-
"verified": true
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
"id": "onb2_02",
|
|
20
|
-
"title": "Explain codebase or repository to me",
|
|
21
|
-
"description": "Analyze and explain any codebase - local project or GitHub repository - including architecture, dependencies, and how it works.",
|
|
22
|
-
"prompt": "I'll analyze and explain any codebase for you! 🔍\n\n**What should I analyze?**\n\n*Local project:* `~/work/my-project` (replace with your path)\n*GitHub repo:* `https://github.com/user/repo`\n\nI'll break down the architecture, dependencies, and how everything works together!",
|
|
23
|
-
"categories": ["onboarding"], "secondaryTag": "Code Analysis",
|
|
24
|
-
"votes": 0,
|
|
25
|
-
"gaClicks": 0,
|
|
26
|
-
"icon": "Code",
|
|
27
|
-
"author": "DC team",
|
|
28
|
-
"verified": true
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
"id": "onb2_03",
|
|
32
|
-
"title": "Create organized knowledge/documents folder",
|
|
33
|
-
"description": "Set up a well-structured knowledge base or document organization system with templates and suggested categories.",
|
|
34
|
-
"prompt": "Let's create an organized knowledge base! 📚\n\n**Where should I set it up?**\n\n*I suggest: `~/Documents/Knowledge-Base` (replace with your path) or give me a different location.*\n\nI'll create a clean folder structure with templates and organize any existing documents you have!",
|
|
35
|
-
"categories": ["onboarding"],
|
|
36
|
-
"secondaryTag": "Quick Start",
|
|
37
|
-
"votes": 0,
|
|
38
|
-
"gaClicks": 0,
|
|
39
|
-
"icon": "BookOpen",
|
|
40
|
-
"author": "DC team",
|
|
41
|
-
"verified": true
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
"id": "onb2_04",
|
|
45
|
-
"title": "Analyze my data file",
|
|
46
|
-
"description": "Upload or point to any data file (CSV, JSON, Excel, etc.) and get comprehensive analysis including patterns, insights, and summary reports.",
|
|
47
|
-
"prompt": "I'll help you analyze your data file! \n\nWhat's the path to your data file? (e.g., `/Users/yourname/data.csv`)\n\nOnce you give me the path, I'll start by checking what type of file it is and show you a quick preview, then we can dive deeper step by step.",
|
|
48
|
-
"categories": ["onboarding"],
|
|
49
|
-
"secondaryTag": "Quick Start",
|
|
50
|
-
"votes": 0,
|
|
51
|
-
"gaClicks": 0,
|
|
52
|
-
"icon": "BarChart3",
|
|
53
|
-
"author": "DC team",
|
|
54
|
-
"verified": true
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
"id": "onb2_05",
|
|
58
|
-
"title": "Check system health and resources",
|
|
59
|
-
"description": "Analyze your system's health, resource usage, running processes, and generate a comprehensive system status report.",
|
|
60
|
-
"prompt": "Let me check your system health and resources!\n\nI'll start by looking at your CPU, memory, and disk usage, then check for any performance issues.\n\nShould I begin the system analysis?",
|
|
61
|
-
"categories": ["onboarding"],
|
|
62
|
-
"secondaryTag": "Quick Start",
|
|
63
|
-
"votes": 0,
|
|
64
|
-
"gaClicks": 0,
|
|
65
|
-
"icon": "Activity",
|
|
66
|
-
"author": "DC team",
|
|
67
|
-
"verified": true
|
|
68
|
-
}
|
|
69
|
-
]
|
|
70
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"version": "2.0.0",
|
|
3
|
+
"description": "Desktop Commander onboarding prompts - V2 simplified (5 prompts only)",
|
|
4
|
+
"prompts": [
|
|
5
|
+
{
|
|
6
|
+
"id": "onb2_01",
|
|
7
|
+
"title": "Organize my Downloads folder",
|
|
8
|
+
"description": "Clean up and organize your messy Downloads folder into relevant subfolders automatically.",
|
|
9
|
+
"prompt": "Let's organize your Downloads folder! \n\nFirst, let me check what we're working with. I'll look at your Downloads folder to see how many files are there and what types.\n\nShould I start by analyzing your Downloads folder?",
|
|
10
|
+
"categories": ["onboarding"],
|
|
11
|
+
"secondaryTag": "Quick Start",
|
|
12
|
+
"votes": 0,
|
|
13
|
+
"gaClicks": 0,
|
|
14
|
+
"icon": "FolderOpen",
|
|
15
|
+
"author": "DC team",
|
|
16
|
+
"verified": true
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"id": "onb2_02",
|
|
20
|
+
"title": "Explain codebase or repository to me",
|
|
21
|
+
"description": "Analyze and explain any codebase - local project or GitHub repository - including architecture, dependencies, and how it works.",
|
|
22
|
+
"prompt": "I'll analyze and explain any codebase for you! 🔍\n\n**What should I analyze?**\n\n*Local project:* `~/work/my-project` (replace with your path)\n*GitHub repo:* `https://github.com/user/repo`\n\nI'll break down the architecture, dependencies, and how everything works together!",
|
|
23
|
+
"categories": ["onboarding"], "secondaryTag": "Code Analysis",
|
|
24
|
+
"votes": 0,
|
|
25
|
+
"gaClicks": 0,
|
|
26
|
+
"icon": "Code",
|
|
27
|
+
"author": "DC team",
|
|
28
|
+
"verified": true
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"id": "onb2_03",
|
|
32
|
+
"title": "Create organized knowledge/documents folder",
|
|
33
|
+
"description": "Set up a well-structured knowledge base or document organization system with templates and suggested categories.",
|
|
34
|
+
"prompt": "Let's create an organized knowledge base! 📚\n\n**Where should I set it up?**\n\n*I suggest: `~/Documents/Knowledge-Base` (replace with your path) or give me a different location.*\n\nI'll create a clean folder structure with templates and organize any existing documents you have!",
|
|
35
|
+
"categories": ["onboarding"],
|
|
36
|
+
"secondaryTag": "Quick Start",
|
|
37
|
+
"votes": 0,
|
|
38
|
+
"gaClicks": 0,
|
|
39
|
+
"icon": "BookOpen",
|
|
40
|
+
"author": "DC team",
|
|
41
|
+
"verified": true
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"id": "onb2_04",
|
|
45
|
+
"title": "Analyze my data file",
|
|
46
|
+
"description": "Upload or point to any data file (CSV, JSON, Excel, etc.) and get comprehensive analysis including patterns, insights, and summary reports.",
|
|
47
|
+
"prompt": "I'll help you analyze your data file! \n\nWhat's the path to your data file? (e.g., `/Users/yourname/data.csv`)\n\nOnce you give me the path, I'll start by checking what type of file it is and show you a quick preview, then we can dive deeper step by step.",
|
|
48
|
+
"categories": ["onboarding"],
|
|
49
|
+
"secondaryTag": "Quick Start",
|
|
50
|
+
"votes": 0,
|
|
51
|
+
"gaClicks": 0,
|
|
52
|
+
"icon": "BarChart3",
|
|
53
|
+
"author": "DC team",
|
|
54
|
+
"verified": true
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"id": "onb2_05",
|
|
58
|
+
"title": "Check system health and resources",
|
|
59
|
+
"description": "Analyze your system's health, resource usage, running processes, and generate a comprehensive system status report.",
|
|
60
|
+
"prompt": "Let me check your system health and resources!\n\nI'll start by looking at your CPU, memory, and disk usage, then check for any performance issues.\n\nShould I begin the system analysis?",
|
|
61
|
+
"categories": ["onboarding"],
|
|
62
|
+
"secondaryTag": "Quick Start",
|
|
63
|
+
"votes": 0,
|
|
64
|
+
"gaClicks": 0,
|
|
65
|
+
"icon": "Activity",
|
|
66
|
+
"author": "DC team",
|
|
67
|
+
"verified": true
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
}
|
package/dist/error-handlers.js
CHANGED
|
File without changes
|
|
@@ -297,8 +297,8 @@ export async function handleWriteFile(args) {
|
|
|
297
297
|
const lineCount = TextFileHandler.countLines(parsed.content);
|
|
298
298
|
let errorMessage = "";
|
|
299
299
|
if (lineCount > MAX_LINES) {
|
|
300
|
-
errorMessage = `✅ File written successfully! (${lineCount} lines)
|
|
301
|
-
|
|
300
|
+
errorMessage = `✅ File written successfully! (${lineCount} lines)
|
|
301
|
+
|
|
302
302
|
💡 Performance tip: For optimal speed, consider chunking files into ≤30 line pieces in future operations.`;
|
|
303
303
|
}
|
|
304
304
|
// Pass the mode parameter to writeFile
|
package/dist/index.js
CHANGED
|
File without changes
|
|
@@ -2,6 +2,7 @@ import { MCPDevice, getRemoteDeviceConfigPath, removeRemoteDeviceConfig } from '
|
|
|
2
2
|
import os from 'os';
|
|
3
3
|
import { captureRemote } from '../utils/capture.js';
|
|
4
4
|
import { startDeviceLog } from '../remote-device/diagnostics/device-log.js';
|
|
5
|
+
import { startTerminalTimestamps } from '../utils/terminal-timestamps.js';
|
|
5
6
|
const BLUE = '\x1b[34m';
|
|
6
7
|
const RESET = '\x1b[0m';
|
|
7
8
|
function printRemoteHeader() {
|
|
@@ -17,30 +18,31 @@ function printRemoteHeader() {
|
|
|
17
18
|
console.log();
|
|
18
19
|
}
|
|
19
20
|
export async function runRemote() {
|
|
21
|
+
startTerminalTimestamps();
|
|
20
22
|
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
|
21
|
-
console.log(`Desktop Commander Remote MCP device
|
|
22
|
-
|
|
23
|
-
Usage:
|
|
24
|
-
desktop-commander remote [options]
|
|
25
|
-
|
|
26
|
-
Options:
|
|
27
|
-
--logout Remove saved local Remote MCP credentials and exit
|
|
28
|
-
--report Save a diagnostics zip for support, send it, and exit
|
|
29
|
-
(the device doesn't start and nothing signs in)
|
|
30
|
-
--no-upload With --report: only save the zip, don't send it
|
|
31
|
-
--no-persist-session Do not reuse or save authentication for this run
|
|
32
|
-
--disable-no-sleep Do not prevent sleep while the remote device is running
|
|
33
|
-
--debug Enable verbose debug logging
|
|
34
|
-
-h, --help Show this help
|
|
35
|
-
|
|
36
|
-
Examples:
|
|
37
|
-
npx @wonderwhy-er/desktop-commander@latest remote
|
|
38
|
-
npx @wonderwhy-er/desktop-commander@latest remote --debug
|
|
39
|
-
npx @wonderwhy-er/desktop-commander@latest remote --logout
|
|
40
|
-
npx @wonderwhy-er/desktop-commander@latest remote --report
|
|
41
|
-
|
|
42
|
-
Note:
|
|
43
|
-
--logout removes local credentials only. Revoke the device in the Remote MCP
|
|
23
|
+
console.log(`Desktop Commander Remote MCP device
|
|
24
|
+
|
|
25
|
+
Usage:
|
|
26
|
+
desktop-commander remote [options]
|
|
27
|
+
|
|
28
|
+
Options:
|
|
29
|
+
--logout Remove saved local Remote MCP credentials and exit
|
|
30
|
+
--report Save a diagnostics zip for support, send it, and exit
|
|
31
|
+
(the device doesn't start and nothing signs in)
|
|
32
|
+
--no-upload With --report: only save the zip, don't send it
|
|
33
|
+
--no-persist-session Do not reuse or save authentication for this run
|
|
34
|
+
--disable-no-sleep Do not prevent sleep while the remote device is running
|
|
35
|
+
--debug Enable verbose debug logging
|
|
36
|
+
-h, --help Show this help
|
|
37
|
+
|
|
38
|
+
Examples:
|
|
39
|
+
npx @wonderwhy-er/desktop-commander@latest remote
|
|
40
|
+
npx @wonderwhy-er/desktop-commander@latest remote --debug
|
|
41
|
+
npx @wonderwhy-er/desktop-commander@latest remote --logout
|
|
42
|
+
npx @wonderwhy-er/desktop-commander@latest remote --report
|
|
43
|
+
|
|
44
|
+
Note:
|
|
45
|
+
--logout removes local credentials only. Revoke the device in the Remote MCP
|
|
44
46
|
dashboard if you also want to invalidate its server-side authorization.`);
|
|
45
47
|
return;
|
|
46
48
|
}
|
|
@@ -104,6 +104,7 @@ export declare class DesktopCommanderIntegration {
|
|
|
104
104
|
type: "resource_link";
|
|
105
105
|
description?: string | undefined;
|
|
106
106
|
mimeType?: string | undefined;
|
|
107
|
+
size?: number | undefined;
|
|
107
108
|
annotations?: {
|
|
108
109
|
audience?: ("user" | "assistant")[] | undefined;
|
|
109
110
|
priority?: number | undefined;
|
|
@@ -11,6 +11,9 @@ import { captureRemote } from '../utils/capture.js';
|
|
|
11
11
|
import { writeFileAtomic } from '../utils/atomic-write.js';
|
|
12
12
|
import { exitProcess } from '../utils/exit-process.js';
|
|
13
13
|
import { observeTransport } from './transport-telemetry.js';
|
|
14
|
+
import { startTerminalTimestamps } from '../utils/terminal-timestamps.js';
|
|
15
|
+
import { VERSION } from '../version.js';
|
|
16
|
+
import { rolloutManager } from './rollouts.js';
|
|
14
17
|
/**
|
|
15
18
|
* How many recently-handled call ids to remember for duplicate-delivery
|
|
16
19
|
* suppression. The two transports deliver a call within MILLISECONDS of each
|
|
@@ -222,9 +225,15 @@ export class MCPDevice {
|
|
|
222
225
|
}
|
|
223
226
|
// Force save the current session immediately to ensure it's persisted
|
|
224
227
|
await this.savePersistedConfig();
|
|
228
|
+
// Which rollouts are on for this user, known before it registers:
|
|
229
|
+
// asked now and again every 30 minutes, each time with the
|
|
230
|
+
// session's current token. setSession() may have refreshed the one
|
|
231
|
+
// device.json held, and the session keeps refreshing while it runs.
|
|
232
|
+
await rolloutManager.load(this.baseServerUrl, () => this.remoteChannel.getAccessToken());
|
|
233
|
+
rolloutManager.startRefresh();
|
|
225
234
|
const deviceName = os.hostname();
|
|
226
235
|
// Register as device. registerDevice() resolves only once the
|
|
227
|
-
// realtime channel is joined and
|
|
236
|
+
// realtime channel is joined and the device row records it, which is
|
|
228
237
|
// exactly what the hosted service needs to deliver a call — so a
|
|
229
238
|
// rejection means "running, but nothing can reach this device", and
|
|
230
239
|
// that has to be said instead of "Device ready".
|
|
@@ -256,8 +265,9 @@ export class MCPDevice {
|
|
|
256
265
|
console.log(` - User: ${this.remoteChannel.user.email}`);
|
|
257
266
|
console.log(` - Device ID: ${this.deviceId}`);
|
|
258
267
|
console.log(` - Device Name: ${deviceName}`);
|
|
268
|
+
console.log(` - Version: ${VERSION}`);
|
|
259
269
|
if (!reachable) {
|
|
260
|
-
console.log(' - Retrying in the background; commands start working once you see "✅
|
|
270
|
+
console.log(' - Retrying in the background; commands start working once you see "✅ Device ready".');
|
|
261
271
|
}
|
|
262
272
|
else {
|
|
263
273
|
console.log('');
|
|
@@ -646,6 +656,7 @@ export class MCPDevice {
|
|
|
646
656
|
console.debug('[DEBUG] Calling stopHeartbeat()');
|
|
647
657
|
this.remoteChannel.stopHeartbeat();
|
|
648
658
|
console.log(' ✓ Heartbeat stopped');
|
|
659
|
+
rolloutManager.stopRefresh();
|
|
649
660
|
// Unsubscribe from channel
|
|
650
661
|
console.log(' → Unsubscribing from channel...');
|
|
651
662
|
console.debug('[DEBUG] Calling channel.unsubscribe()');
|
|
@@ -683,6 +694,7 @@ import.meta.url === `file://${process.argv[1]}` ||
|
|
|
683
694
|
process.argv[1].endsWith('desktop-commander-device') ||
|
|
684
695
|
process.argv[1].endsWith('desktop-commander-device.js'));
|
|
685
696
|
if (isMainModule) {
|
|
697
|
+
startTerminalTimestamps();
|
|
686
698
|
// Parse command-line arguments
|
|
687
699
|
const args = process.argv.slice(2);
|
|
688
700
|
const options = {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type RealtimeCounter = 'explicit_subscribe_calls' | 'join_successes' | 'join_errors' | 'join_timeouts' | 'join_closures' | 'watchdog_recreates' | 'socket_disconnects' | 'presence_attempts' | 'presence_successes' | 'presence_failures' | 'readiness_write_failures';
|
|
2
|
+
export type RealtimeWindow = Record<RealtimeCounter, number> & {
|
|
3
|
+
event_version: number;
|
|
4
|
+
device_diag_id: string;
|
|
5
|
+
process_session_id: string;
|
|
6
|
+
channel_generation: number;
|
|
7
|
+
window_start: string;
|
|
8
|
+
window_seconds: number;
|
|
9
|
+
app_version: string;
|
|
10
|
+
platform: string;
|
|
11
|
+
};
|
|
12
|
+
export declare class RealtimeHealthTelemetry {
|
|
13
|
+
private readonly emit;
|
|
14
|
+
private readonly sample;
|
|
15
|
+
private readonly random;
|
|
16
|
+
readonly processSessionId: `${string}-${string}-${string}-${string}-${string}`;
|
|
17
|
+
private deviceDiagId;
|
|
18
|
+
private generation;
|
|
19
|
+
private counts;
|
|
20
|
+
private windowStart;
|
|
21
|
+
private windowStartMonotonic;
|
|
22
|
+
private socketState;
|
|
23
|
+
private timer;
|
|
24
|
+
constructor(emit: (window: RealtimeWindow) => void, sample?: (key: string) => number, random?: () => number);
|
|
25
|
+
private emptyCounts;
|
|
26
|
+
/** Stable for one registered device, unlinkable to its raw UUID without knowing the UUID. */
|
|
27
|
+
setDevice(deviceId: string): void;
|
|
28
|
+
newChannel(): void;
|
|
29
|
+
record(key: RealtimeCounter): void;
|
|
30
|
+
observeSocketState(state: string): void;
|
|
31
|
+
start(): void;
|
|
32
|
+
private schedule;
|
|
33
|
+
flush(now?: number, monotonicNow?: number): RealtimeWindow | null;
|
|
34
|
+
stop(): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Low-volume Realtime lifecycle counters. No per-retry HTTP requests.
|
|
3
|
+
* Counts are local observations, NOT exact Phoenix phx_join wire frames.
|
|
4
|
+
*/
|
|
5
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
6
|
+
import { VERSION } from '../version.js';
|
|
7
|
+
const COUNTERS = [
|
|
8
|
+
'explicit_subscribe_calls', 'join_successes', 'join_errors',
|
|
9
|
+
'join_timeouts', 'join_closures', 'watchdog_recreates',
|
|
10
|
+
'socket_disconnects', 'presence_attempts', 'presence_successes',
|
|
11
|
+
'presence_failures', 'readiness_write_failures',
|
|
12
|
+
];
|
|
13
|
+
const DEGRADED = [
|
|
14
|
+
'join_errors', 'join_timeouts', 'join_closures', 'watchdog_recreates',
|
|
15
|
+
'socket_disconnects', 'presence_failures', 'readiness_write_failures',
|
|
16
|
+
];
|
|
17
|
+
export class RealtimeHealthTelemetry {
|
|
18
|
+
constructor(emit, sample = (key) => parseInt(createHash('sha256').update(key).digest('hex').slice(0, 8), 16) / 0x100000000, random = Math.random) {
|
|
19
|
+
this.emit = emit;
|
|
20
|
+
this.sample = sample;
|
|
21
|
+
this.random = random;
|
|
22
|
+
this.processSessionId = randomUUID();
|
|
23
|
+
this.deviceDiagId = '';
|
|
24
|
+
this.generation = 0;
|
|
25
|
+
this.counts = this.emptyCounts();
|
|
26
|
+
this.windowStart = Date.now();
|
|
27
|
+
this.windowStartMonotonic = performance.now();
|
|
28
|
+
this.socketState = null;
|
|
29
|
+
this.timer = null;
|
|
30
|
+
}
|
|
31
|
+
emptyCounts() {
|
|
32
|
+
return Object.fromEntries(COUNTERS.map((key) => [key, 0]));
|
|
33
|
+
}
|
|
34
|
+
/** Stable for one registered device, unlinkable to its raw UUID without knowing the UUID. */
|
|
35
|
+
setDevice(deviceId) {
|
|
36
|
+
if (!deviceId)
|
|
37
|
+
return;
|
|
38
|
+
const id = createHash('sha256').update('dc-realtime-diagnostics-v1:').update(deviceId).digest('hex').slice(0, 32);
|
|
39
|
+
if (this.deviceDiagId === id)
|
|
40
|
+
return;
|
|
41
|
+
this.deviceDiagId = id;
|
|
42
|
+
this.windowStart = Date.now();
|
|
43
|
+
this.windowStartMonotonic = performance.now();
|
|
44
|
+
this.counts = this.emptyCounts();
|
|
45
|
+
}
|
|
46
|
+
newChannel() {
|
|
47
|
+
this.generation++;
|
|
48
|
+
this.record('explicit_subscribe_calls');
|
|
49
|
+
}
|
|
50
|
+
record(key) {
|
|
51
|
+
this.counts[key]++;
|
|
52
|
+
}
|
|
53
|
+
observeSocketState(state) {
|
|
54
|
+
if (this.socketState === 'open' && state !== 'open')
|
|
55
|
+
this.record('socket_disconnects');
|
|
56
|
+
this.socketState = state;
|
|
57
|
+
}
|
|
58
|
+
start() {
|
|
59
|
+
if (this.timer)
|
|
60
|
+
return;
|
|
61
|
+
this.schedule();
|
|
62
|
+
}
|
|
63
|
+
schedule() {
|
|
64
|
+
// Each device chooses a different reporting boundary; never a fleet-wide minute mark.
|
|
65
|
+
this.timer = setTimeout(() => {
|
|
66
|
+
this.timer = null;
|
|
67
|
+
this.flush();
|
|
68
|
+
this.schedule();
|
|
69
|
+
}, 60000 + Math.floor(this.random() * 30000));
|
|
70
|
+
this.timer.unref?.();
|
|
71
|
+
}
|
|
72
|
+
flush(now = Date.now(), monotonicNow = performance.now()) {
|
|
73
|
+
// Wall time may jump when observeServerDate() corrects clock skew.
|
|
74
|
+
// Use only monotonic time for durations; wall time still locates the window.
|
|
75
|
+
const elapsed = Math.max(1, Math.round((monotonicNow - this.windowStartMonotonic) / 1000));
|
|
76
|
+
const counts = this.counts;
|
|
77
|
+
const start = this.windowStart;
|
|
78
|
+
this.counts = this.emptyCounts();
|
|
79
|
+
this.windowStart = now;
|
|
80
|
+
this.windowStartMonotonic = monotonicNow;
|
|
81
|
+
if (!this.deviceDiagId)
|
|
82
|
+
return null;
|
|
83
|
+
const degraded = DEGRADED.some((key) => counts[key] > 0);
|
|
84
|
+
const sampleRate = degraded ? 0.05 : 0.01;
|
|
85
|
+
if (this.sample(this.deviceDiagId) >= sampleRate)
|
|
86
|
+
return null;
|
|
87
|
+
const window = {
|
|
88
|
+
...counts,
|
|
89
|
+
event_version: 1,
|
|
90
|
+
device_diag_id: this.deviceDiagId,
|
|
91
|
+
process_session_id: this.processSessionId,
|
|
92
|
+
channel_generation: this.generation,
|
|
93
|
+
window_start: new Date(start).toISOString(),
|
|
94
|
+
window_seconds: elapsed,
|
|
95
|
+
app_version: VERSION,
|
|
96
|
+
platform: process.platform,
|
|
97
|
+
};
|
|
98
|
+
try {
|
|
99
|
+
this.emit(window);
|
|
100
|
+
}
|
|
101
|
+
catch { /* diagnostics must not affect transport */ }
|
|
102
|
+
return window;
|
|
103
|
+
}
|
|
104
|
+
stop() {
|
|
105
|
+
if (this.timer)
|
|
106
|
+
clearTimeout(this.timer);
|
|
107
|
+
this.timer = null;
|
|
108
|
+
this.flush();
|
|
109
|
+
}
|
|
110
|
+
}
|