@wonderwhy-er/desktop-commander 0.2.51 → 0.3.1-alpha.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.
- package/README.md +14 -24
- package/dist/command-manager.d.ts +7 -2
- package/dist/command-manager.js +16 -11
- package/dist/config-manager.d.ts +49 -3
- package/dist/config-manager.js +329 -87
- package/dist/config-recovery.d.ts +28 -0
- package/dist/config-recovery.js +81 -0
- package/dist/config.d.ts +1 -0
- package/dist/config.js +9 -0
- package/dist/handlers/filesystem-handlers.d.ts +1 -1
- package/dist/handlers/filesystem-handlers.js +26 -16
- package/dist/handlers/search-answers.d.ts +58 -0
- package/dist/handlers/search-answers.js +172 -0
- package/dist/handlers/search-handlers.js +4 -92
- package/dist/index.js +14 -7
- package/dist/npm-scripts/remote.js +32 -4
- package/dist/remote-device/broadcast-analytics.d.ts +28 -0
- package/dist/remote-device/broadcast-analytics.js +41 -0
- package/dist/remote-device/desktop-commander-integration.d.ts +20 -0
- package/dist/remote-device/desktop-commander-integration.js +121 -40
- package/dist/remote-device/device-authenticator.d.ts +13 -1
- package/dist/remote-device/device-authenticator.js +87 -47
- package/dist/remote-device/device.d.ts +14 -0
- package/dist/remote-device/device.js +244 -51
- package/dist/remote-device/mqtt-enrollment.d.ts +16 -0
- package/dist/remote-device/mqtt-enrollment.js +162 -0
- package/dist/remote-device/mqtt-transport.d.ts +44 -0
- package/dist/remote-device/mqtt-transport.js +197 -0
- package/dist/remote-device/remote-channel.d.ts +64 -3
- package/dist/remote-device/remote-channel.js +317 -48
- package/dist/remote-device/scripts/blocking-offline-update.js +106 -69
- package/dist/remote-device/transport-analytics.d.ts +29 -0
- package/dist/remote-device/transport-analytics.js +44 -0
- package/dist/remote-device/transport-telemetry.d.ts +32 -0
- package/dist/remote-device/transport-telemetry.js +48 -0
- package/dist/search-manager.d.ts +239 -27
- package/dist/search-manager.js +746 -312
- package/dist/server.js +77 -92
- package/dist/terminal-manager.d.ts +138 -21
- package/dist/terminal-manager.js +300 -215
- package/dist/tools/config.d.ts +1 -1
- package/dist/tools/config.js +21 -70
- package/dist/tools/edit.js +12 -4
- package/dist/tools/filesystem.d.ts +30 -6
- package/dist/tools/filesystem.js +246 -140
- package/dist/tools/improved-process-tools.d.ts +9 -3
- package/dist/tools/improved-process-tools.js +171 -68
- package/dist/tools/pdf/extract-images.d.ts +2 -0
- package/dist/tools/pdf/extract-images.js +4 -1
- package/dist/tools/pdf/index.d.ts +5 -3
- package/dist/tools/pdf/index.js +4 -3
- package/dist/tools/pdf/lib/pdf2md.d.ts +8 -1
- package/dist/tools/pdf/lib/pdf2md.js +12 -5
- package/dist/tools/pdf/manipulations.d.ts +12 -1
- package/dist/tools/pdf/manipulations.js +17 -4
- package/dist/tools/pdf/markdown.d.ts +96 -0
- package/dist/tools/pdf/markdown.js +485 -20
- package/dist/tools/pdf/utils.d.ts +2 -2
- package/dist/tools/pdf/utils.js +4 -4
- package/dist/tools/process.js +8 -5
- package/dist/tools/schemas.d.ts +1 -1
- package/dist/tools/schemas.js +15 -6
- package/dist/tools/search.d.ts +32 -0
- package/dist/tools/search.js +202 -0
- package/dist/types.d.ts +1 -1
- package/dist/ui/config-editor/config-editor-runtime.js +6 -6
- package/dist/ui/config-editor/src/app.js +2 -15
- package/dist/utils/ab-test.js +27 -9
- package/dist/utils/atomic-write.d.ts +14 -0
- package/dist/utils/atomic-write.js +80 -0
- package/dist/utils/capture.d.ts +17 -1
- package/dist/utils/capture.js +177 -68
- package/dist/utils/exit-process.d.ts +24 -0
- package/dist/utils/exit-process.js +31 -0
- package/dist/utils/feature-flags.js +2 -1
- package/dist/utils/files/base.d.ts +3 -1
- package/dist/utils/files/docx.d.ts +3 -0
- package/dist/utils/files/docx.js +24 -9
- package/dist/utils/files/excel.d.ts +10 -0
- package/dist/utils/files/excel.js +93 -33
- package/dist/utils/files/factory.d.ts +22 -1
- package/dist/utils/files/factory.js +59 -4
- package/dist/utils/files/image.d.ts +12 -4
- package/dist/utils/files/image.js +19 -8
- package/dist/utils/files/index.d.ts +5 -3
- package/dist/utils/files/index.js +4 -3
- package/dist/utils/files/pdf.js +18 -36
- package/dist/utils/files/text.d.ts +21 -5
- package/dist/utils/files/text.js +90 -86
- package/dist/utils/internal-facts.d.ts +3 -0
- package/dist/utils/internal-facts.js +23 -0
- package/dist/utils/lazy-package.d.ts +23 -0
- package/dist/utils/lazy-package.js +42 -0
- package/dist/utils/open-browser.js +2 -1
- package/dist/utils/process-detection.d.ts +24 -5
- package/dist/utils/process-detection.js +199 -89
- package/dist/utils/process-tree.d.ts +14 -0
- package/dist/utils/process-tree.js +223 -0
- package/dist/utils/rename.d.ts +9 -0
- package/dist/utils/rename.js +74 -0
- package/dist/utils/ripgrep-resolver.js +9 -14
- package/dist/utils/shell.d.ts +42 -0
- package/dist/utils/shell.js +187 -0
- package/dist/utils/system-info.js +9 -7
- package/dist/utils/trackTools.js +2 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/node_modules/@desktop-commander/telemetry-contract/README.md +56 -0
- package/node_modules/@desktop-commander/telemetry-contract/package.json +32 -0
- package/node_modules/@desktop-commander/telemetry-contract/transport.d.ts +38 -0
- package/node_modules/@desktop-commander/telemetry-contract/transport.js +164 -0
- package/package.json +8 -2
- package/vendor/telemetry-contract/README.md +56 -0
- package/vendor/telemetry-contract/package.json +32 -0
- package/vendor/telemetry-contract/transport.d.ts +38 -0
- package/vendor/telemetry-contract/transport.js +164 -0
- package/vendor/telemetry-contract/transport.test.js +39 -0
|
@@ -3,94 +3,131 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Blocking script to update device status to offline
|
|
5
5
|
* Runs synchronously during shutdown to ensure DB update completes
|
|
6
|
-
*
|
|
6
|
+
*
|
|
7
7
|
* Usage: node blocking-offline-update.js <deviceId> <supabaseUrl> <supabaseKey> <accessToken> [refreshToken]
|
|
8
8
|
* Without a refreshToken only a still-valid accessToken can be used.
|
|
9
|
+
*
|
|
10
|
+
* Ends through exitProcess(), never process.exit(): process.exit() right after
|
|
11
|
+
* the update's fetch() can abort Node on Windows with exit code 0xC0000409
|
|
12
|
+
* (see src/utils/exit-process.ts), and the parent then reports a failed update.
|
|
9
13
|
*/
|
|
10
14
|
|
|
11
15
|
import { createClient } from '@supabase/supabase-js';
|
|
12
16
|
|
|
17
|
+
// The build copies this script to dist/remote-device/scripts/, next to the
|
|
18
|
+
// compiled dist/utils/exit-process.js. Run from a source checkout (npm run
|
|
19
|
+
// device:start starts it with plain node, without tsx) it loads the checkout's
|
|
20
|
+
// build, dist/utils/exit-process.js (npm install builds it), which works on
|
|
21
|
+
// every Node version. Without a build it falls back to
|
|
22
|
+
// src/utils/exit-process.ts, which only Node 22.18+ loads (by stripping types).
|
|
23
|
+
const ifNotFound = (importNext) => (error) => {
|
|
24
|
+
if (error.code !== 'ERR_MODULE_NOT_FOUND') throw error;
|
|
25
|
+
return importNext();
|
|
26
|
+
};
|
|
27
|
+
const { exitProcess, EXIT_GRACE_MS } = await import('../../utils/exit-process.js')
|
|
28
|
+
.catch(ifNotFound(() => import('../../../dist/utils/exit-process.js')))
|
|
29
|
+
.catch(ifNotFound(() => import('../../utils/exit-process.ts')));
|
|
30
|
+
|
|
13
31
|
// Parse command line arguments
|
|
14
32
|
const [deviceId, supabaseUrl, supabaseKey, accessToken, refreshToken] = process.argv.slice(2);
|
|
15
33
|
|
|
16
34
|
if (!deviceId || !supabaseUrl || !supabaseKey || !accessToken) {
|
|
17
35
|
console.error('❌ Missing required arguments');
|
|
18
36
|
console.error('Usage: node blocking-offline-update.js <deviceId> <supabaseUrl> <supabaseKey> <accessToken> [refreshToken]');
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Set timeout for entire operation
|
|
23
|
-
const TIMEOUT_MS = 3000;
|
|
24
|
-
const timeoutHandle = setTimeout(() => {
|
|
25
|
-
console.error('⏱️ Timeout: Update took too long');
|
|
26
|
-
process.exit(2); // Exit code 2 for timeout
|
|
27
|
-
}, TIMEOUT_MS);
|
|
28
|
-
|
|
29
|
-
const auth = { persistSession: false, autoRefreshToken: false };
|
|
30
|
-
|
|
31
|
-
// Update device status to offline, stamping the exact shutdown moment so
|
|
32
|
-
// "last seen X ago" is precise for clean shutdowns (the periodic
|
|
33
|
-
// bookkeeping write only runs on the slow capable cadence).
|
|
34
|
-
// The token goes straight to PostgREST as a Bearer header: setSession()
|
|
35
|
-
// would first spend a GoTrue round trip of the budget re-validating it.
|
|
36
|
-
const markOffline = (token) => createClient(supabaseUrl, supabaseKey, {
|
|
37
|
-
auth,
|
|
38
|
-
global: { headers: { Authorization: `Bearer ${token}` } }
|
|
39
|
-
})
|
|
40
|
-
.from('mcp_devices')
|
|
41
|
-
.update({ status: 'offline', last_seen: new Date().toISOString() })
|
|
42
|
-
.eq('id', deviceId);
|
|
43
|
-
|
|
44
|
-
function tokenLooksLive(token) {
|
|
45
|
-
try {
|
|
46
|
-
const { exp } = JSON.parse(Buffer.from(token.split('.')[1], 'base64url').toString('utf8'));
|
|
47
|
-
return typeof exp === 'number' && exp * 1000 > Date.now() + 5000;
|
|
48
|
-
} catch {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
37
|
+
exitProcess(1);
|
|
38
|
+
} else {
|
|
39
|
+
await markDeviceOffline();
|
|
51
40
|
}
|
|
52
41
|
|
|
53
|
-
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
42
|
+
async function markDeviceOffline() {
|
|
43
|
+
// The parent (setOffline() in remote-channel.ts) kills this script 3000ms
|
|
44
|
+
// after starting it. The update gets that time, counted from the start of
|
|
45
|
+
// the process, minus what the process may need to end once it gives up.
|
|
46
|
+
const PARENT_TIMEOUT_MS = 3000;
|
|
47
|
+
const deadline = new AbortController();
|
|
48
|
+
const timeoutHandle = setTimeout(() => {
|
|
49
|
+
console.error('⏱️ Timeout: Update took too long');
|
|
50
|
+
exitProcess(2); // Exit code 2 for timeout
|
|
51
|
+
// Ends the requests still in flight, which would keep the process alive
|
|
52
|
+
deadline.abort();
|
|
53
|
+
}, PARENT_TIMEOUT_MS - EXIT_GRACE_MS - performance.now());
|
|
54
|
+
|
|
55
|
+
const auth = { persistSession: false, autoRefreshToken: false };
|
|
56
|
+
const fetchBeforeDeadline = (input, init) => fetch(input, { ...init, signal: deadline.signal });
|
|
57
|
+
|
|
58
|
+
// Update device status to offline, stamping the exact shutdown moment so
|
|
59
|
+
// "last seen X ago" is precise for clean shutdowns (the periodic
|
|
60
|
+
// bookkeeping write only runs on the slow capable cadence).
|
|
61
|
+
// The token goes straight to PostgREST as a Bearer header: setSession()
|
|
62
|
+
// would first spend a GoTrue round trip of the budget re-validating it.
|
|
63
|
+
const markOffline = (token) => createClient(supabaseUrl, supabaseKey, {
|
|
64
|
+
auth,
|
|
65
|
+
global: { headers: { Authorization: `Bearer ${token}` }, fetch: fetchBeforeDeadline }
|
|
66
|
+
})
|
|
67
|
+
.from('mcp_devices')
|
|
68
|
+
.update({ status: 'offline', last_seen: new Date().toISOString() })
|
|
69
|
+
.eq('id', deviceId);
|
|
70
|
+
|
|
71
|
+
function tokenLooksLive(token) {
|
|
72
|
+
try {
|
|
73
|
+
const { exp } = JSON.parse(Buffer.from(token.split('.')[1], 'base64url').toString('utf8'));
|
|
74
|
+
return typeof exp === 'number' && exp * 1000 > Date.now() + 5000;
|
|
75
|
+
} catch {
|
|
76
|
+
// Not a JWT we can read: treat it as expired and refresh the session
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
60
79
|
}
|
|
61
|
-
const client = createClient(supabaseUrl, supabaseKey, { auth });
|
|
62
|
-
const { data, error: authError } = await client.auth.refreshSession({ refresh_token: refreshToken });
|
|
63
80
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
81
|
+
// refreshSession(), not setSession(): setSession() validates a token that
|
|
82
|
+
// looks live on this machine's clock via /user instead of refreshing it.
|
|
83
|
+
// Resolves with the new access token, or undefined after reporting why there is none.
|
|
84
|
+
async function refreshedToken() {
|
|
85
|
+
if (!refreshToken) {
|
|
86
|
+
console.error('❌ Auth error: access token not usable and no refresh token');
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
const client = createClient(supabaseUrl, supabaseKey, { auth, global: { fetch: fetchBeforeDeadline } });
|
|
90
|
+
const { data, error: authError } = await client.auth.refreshSession({ refresh_token: refreshToken });
|
|
91
|
+
|
|
92
|
+
if (authError || !data.session) {
|
|
93
|
+
console.error('❌ Auth error:', authError?.message ?? 'no session returned');
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
return data.session.access_token;
|
|
68
97
|
}
|
|
69
|
-
return data.session.access_token;
|
|
70
|
-
}
|
|
71
98
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
99
|
+
// Resolves with the exit code
|
|
100
|
+
async function update() {
|
|
101
|
+
try {
|
|
102
|
+
// An expired token (e.g. the machine just woke) is refreshed first. A 401
|
|
103
|
+
// means it only looked live on this machine's clock: refresh and retry once.
|
|
104
|
+
let result = tokenLooksLive(accessToken) ? await markOffline(accessToken) : null;
|
|
105
|
+
if (!result || result.status === 401) {
|
|
106
|
+
const token = await refreshedToken();
|
|
107
|
+
if (!token) {
|
|
108
|
+
return 3; // Exit code 3 for auth error
|
|
109
|
+
}
|
|
110
|
+
result = await markOffline(token);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const { error } = result;
|
|
114
|
+
|
|
115
|
+
if (error) {
|
|
116
|
+
console.error('❌ DB update error:', error.message);
|
|
117
|
+
return 4; // Exit code 4 for DB error
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
console.log('✓ Device marked as offline');
|
|
121
|
+
return 0; // Success
|
|
122
|
+
|
|
123
|
+
} catch (error) {
|
|
124
|
+
console.error('❌ Unexpected error:', error.message);
|
|
125
|
+
return 5; // Exit code 5 for unexpected error
|
|
126
|
+
}
|
|
78
127
|
}
|
|
79
128
|
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
clearTimeout(timeoutHandle);
|
|
83
|
-
|
|
84
|
-
if (error) {
|
|
85
|
-
console.error('❌ DB update error:', error.message);
|
|
86
|
-
process.exit(4); // Exit code 4 for DB error
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
console.log('✓ Device marked as offline');
|
|
90
|
-
process.exit(0); // Success
|
|
91
|
-
|
|
92
|
-
} catch (error) {
|
|
129
|
+
const code = await update();
|
|
93
130
|
clearTimeout(timeoutHandle);
|
|
94
|
-
|
|
95
|
-
|
|
131
|
+
// After a timeout the exit code stays 2 (only exitProcess()'s first call counts)
|
|
132
|
+
exitProcess(code);
|
|
96
133
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
type TransportObservation = {
|
|
2
|
+
stage: 'received';
|
|
3
|
+
payload: any;
|
|
4
|
+
deviceId: string | null;
|
|
5
|
+
userId: string | undefined;
|
|
6
|
+
} | {
|
|
7
|
+
stage: string;
|
|
8
|
+
callId: string;
|
|
9
|
+
deviceId: string | null | undefined;
|
|
10
|
+
operation?: string;
|
|
11
|
+
operationId?: string;
|
|
12
|
+
};
|
|
13
|
+
/** Record a device transport checkpoint without affecting command handling. */
|
|
14
|
+
export declare function observeTransport(input: TransportObservation): {
|
|
15
|
+
observation_id: `${string}-${string}-${string}-${string}-${string}`;
|
|
16
|
+
source_process_id: `${string}-${string}-${string}-${string}-${string}`;
|
|
17
|
+
schema_version: number;
|
|
18
|
+
timestamp_utc: string;
|
|
19
|
+
monotonic_ms: number;
|
|
20
|
+
operation_id?: string | undefined;
|
|
21
|
+
operation?: string | undefined;
|
|
22
|
+
call_id: string;
|
|
23
|
+
device_id: string;
|
|
24
|
+
transport: string;
|
|
25
|
+
stage: string;
|
|
26
|
+
notification_id: string;
|
|
27
|
+
attempt_number: number;
|
|
28
|
+
} | null;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { capture } from '../utils/capture.js';
|
|
3
|
+
const ID = /^[A-Za-z0-9_-]{1,128}$/;
|
|
4
|
+
const PROCESS_ID = randomUUID();
|
|
5
|
+
/** Record a device transport checkpoint without affecting command handling. */
|
|
6
|
+
export function observeTransport(input) {
|
|
7
|
+
try {
|
|
8
|
+
const timestamp_utc = new Date().toISOString();
|
|
9
|
+
const monotonic_ms = performance.now();
|
|
10
|
+
const receipt = 'payload' in input;
|
|
11
|
+
const callId = receipt ? input.payload?.call_id : input.callId;
|
|
12
|
+
const deviceId = input.deviceId;
|
|
13
|
+
if (typeof callId !== 'string' || !ID.test(callId) ||
|
|
14
|
+
typeof deviceId !== 'string' || !ID.test(deviceId))
|
|
15
|
+
return null;
|
|
16
|
+
if (receipt && (input.payload.device_id !== deviceId || !input.userId ||
|
|
17
|
+
(input.payload.user_id !== undefined && input.payload.user_id !== input.userId)))
|
|
18
|
+
return null;
|
|
19
|
+
const observationId = randomUUID();
|
|
20
|
+
const operationId = receipt ? undefined : input.operationId ??
|
|
21
|
+
(input.stage === 'operation_start' ? observationId : undefined);
|
|
22
|
+
const event = {
|
|
23
|
+
call_id: callId,
|
|
24
|
+
device_id: deviceId,
|
|
25
|
+
transport: 'broadcast',
|
|
26
|
+
stage: input.stage,
|
|
27
|
+
notification_id: `${callId}:broadcast:1`,
|
|
28
|
+
attempt_number: 1,
|
|
29
|
+
...(!receipt && input.operation ? { operation: input.operation } : {}),
|
|
30
|
+
...(operationId ? { operation_id: operationId } : {}),
|
|
31
|
+
observation_id: observationId,
|
|
32
|
+
source_process_id: PROCESS_ID,
|
|
33
|
+
schema_version: 1,
|
|
34
|
+
timestamp_utc,
|
|
35
|
+
monotonic_ms,
|
|
36
|
+
};
|
|
37
|
+
void capture(input.stage, event);
|
|
38
|
+
return event;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Telemetry must never affect command handling.
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
type TransportObservation = ({
|
|
2
|
+
stage: 'received';
|
|
3
|
+
payload: any;
|
|
4
|
+
deviceId: string | null;
|
|
5
|
+
userId: string | undefined;
|
|
6
|
+
} | {
|
|
7
|
+
stage: string;
|
|
8
|
+
callId: string;
|
|
9
|
+
deviceId: string | null | undefined;
|
|
10
|
+
toolName?: string;
|
|
11
|
+
operation?: string;
|
|
12
|
+
operationId?: string;
|
|
13
|
+
}) & {
|
|
14
|
+
transport?: 'broadcast' | 'mqtt';
|
|
15
|
+
};
|
|
16
|
+
/** Record a device transport checkpoint without affecting command handling. */
|
|
17
|
+
export declare function observeTransport(input: TransportObservation): {
|
|
18
|
+
observation_id: `${string}-${string}-${string}-${string}-${string}`;
|
|
19
|
+
source_process_id: `${string}-${string}-${string}-${string}-${string}`;
|
|
20
|
+
schema_version: number;
|
|
21
|
+
timestamp_utc: string;
|
|
22
|
+
operation_id?: string | undefined;
|
|
23
|
+
operation?: string | undefined;
|
|
24
|
+
tool_name?: string | undefined;
|
|
25
|
+
notification_id?: string | undefined;
|
|
26
|
+
attempt_number?: number | undefined;
|
|
27
|
+
transport?: "broadcast" | "mqtt" | undefined;
|
|
28
|
+
call_id: string;
|
|
29
|
+
device_id: string;
|
|
30
|
+
stage: string;
|
|
31
|
+
} | null;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { sanitizeTransportEvent } from '@desktop-commander/telemetry-contract/transport';
|
|
3
|
+
import { captureTransport } from '../utils/capture.js';
|
|
4
|
+
const ID = /^[A-Za-z0-9_-]{1,128}$/;
|
|
5
|
+
const PROCESS_ID = randomUUID();
|
|
6
|
+
/** Record a device transport checkpoint without affecting command handling. */
|
|
7
|
+
export function observeTransport(input) {
|
|
8
|
+
try {
|
|
9
|
+
const timestamp_utc = new Date().toISOString();
|
|
10
|
+
const receipt = 'payload' in input;
|
|
11
|
+
const callId = receipt ? input.payload?.call_id : input.callId;
|
|
12
|
+
const deviceId = input.deviceId;
|
|
13
|
+
if (typeof callId !== 'string' || !ID.test(callId) ||
|
|
14
|
+
typeof deviceId !== 'string' || !ID.test(deviceId))
|
|
15
|
+
return null;
|
|
16
|
+
if (receipt && (input.payload.device_id !== deviceId || !input.userId ||
|
|
17
|
+
(input.payload.user_id !== undefined && input.payload.user_id !== input.userId)))
|
|
18
|
+
return null;
|
|
19
|
+
const observationId = randomUUID();
|
|
20
|
+
let operationId;
|
|
21
|
+
if (!receipt) {
|
|
22
|
+
operationId = input.stage === 'operation_start' ? observationId : input.operationId;
|
|
23
|
+
}
|
|
24
|
+
const event = {
|
|
25
|
+
call_id: callId,
|
|
26
|
+
device_id: deviceId,
|
|
27
|
+
stage: input.stage,
|
|
28
|
+
...(input.transport === 'broadcast' || input.transport === 'mqtt' ? { transport: input.transport } : {}),
|
|
29
|
+
...(input.transport === 'broadcast' ? {
|
|
30
|
+
notification_id: `${callId}:broadcast:1`, attempt_number: 1,
|
|
31
|
+
} : {}),
|
|
32
|
+
...(!receipt && typeof input.toolName === 'string' && input.toolName.length > 0 &&
|
|
33
|
+
input.toolName.length <= 128 ? { tool_name: input.toolName } : {}),
|
|
34
|
+
...(!receipt && input.operation ? { operation: input.operation } : {}),
|
|
35
|
+
...(operationId ? { operation_id: operationId } : {}),
|
|
36
|
+
observation_id: observationId,
|
|
37
|
+
source_process_id: PROCESS_ID,
|
|
38
|
+
schema_version: 1,
|
|
39
|
+
timestamp_utc,
|
|
40
|
+
};
|
|
41
|
+
void captureTransport(input.stage, sanitizeTransportEvent(input.stage, event));
|
|
42
|
+
return event;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// Telemetry must never affect command handling.
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|