@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
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
type Rollouts = Record<string, boolean>;
|
|
2
|
+
/**
|
|
3
|
+
* Which rollouts are on for this device's user. A rollout turns something on
|
|
4
|
+
* for a share of users, and the Remote MCP server decides who is in. The
|
|
5
|
+
* device asks at start, takes the map each call row carries, and asks again
|
|
6
|
+
* every 30 minutes:
|
|
7
|
+
*
|
|
8
|
+
* - a start answer that fails (a refused token, a server error, a broken
|
|
9
|
+
* answer, no answer within 3 s) leaves every rollout off
|
|
10
|
+
* - a call row without rollouts (an older server) keeps what the device has
|
|
11
|
+
* - a 30-minute answer that fails keeps what the device has
|
|
12
|
+
* - a 30-minute answer that arrives after a call row it was waiting behind is
|
|
13
|
+
* dropped: the call row's rollouts are newer
|
|
14
|
+
*
|
|
15
|
+
* With no session token the device sends no request: that counts as a
|
|
16
|
+
* failed answer.
|
|
17
|
+
*
|
|
18
|
+
* Each change logs a debug line, "Rollouts: …".
|
|
19
|
+
*/
|
|
20
|
+
declare class RolloutManager {
|
|
21
|
+
private rollouts;
|
|
22
|
+
private serverUrl;
|
|
23
|
+
private getToken;
|
|
24
|
+
private refreshInterval;
|
|
25
|
+
/** How many call rows brought rollouts; a 30-minute answer compares it to tell whether one came meanwhile */
|
|
26
|
+
private callRowUpdates;
|
|
27
|
+
/**
|
|
28
|
+
* GET {serverUrl}/device/rollouts with the session's access token, which
|
|
29
|
+
* getToken() is asked for on every request (null: no session, no request).
|
|
30
|
+
* A failed answer leaves every rollout off.
|
|
31
|
+
*/
|
|
32
|
+
load(serverUrl: string, getToken: () => Promise<string | null>): Promise<void>;
|
|
33
|
+
/** A call row's rollouts (metadata.rollouts); a row without them keeps what the device has */
|
|
34
|
+
update(rollouts: unknown): void;
|
|
35
|
+
/** Asks again every 30 minutes, after load(); a failed answer keeps what the device has */
|
|
36
|
+
startRefresh(): void;
|
|
37
|
+
stopRefresh(): void;
|
|
38
|
+
/** Whether a rollout is on; `defaultValue` for one the server did not name */
|
|
39
|
+
get(name: string, defaultValue?: boolean): boolean;
|
|
40
|
+
getAll(): Rollouts;
|
|
41
|
+
private refresh;
|
|
42
|
+
/** The server's answer, or undefined when it failed or took longer than 3 s */
|
|
43
|
+
private request;
|
|
44
|
+
private fetchRollouts;
|
|
45
|
+
private replace;
|
|
46
|
+
}
|
|
47
|
+
export declare const rolloutManager: RolloutManager;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { withTimeout } from '../utils/withTimeout.js';
|
|
2
|
+
/** How long a request for the rollouts waits for the server's answer */
|
|
3
|
+
const REQUEST_TIMEOUT_MS = 3000;
|
|
4
|
+
/** How often a running device asks again, as often as feature flags are refreshed */
|
|
5
|
+
const REFRESH_INTERVAL_MS = 30 * 60 * 1000;
|
|
6
|
+
/**
|
|
7
|
+
* Which rollouts are on for this device's user. A rollout turns something on
|
|
8
|
+
* for a share of users, and the Remote MCP server decides who is in. The
|
|
9
|
+
* device asks at start, takes the map each call row carries, and asks again
|
|
10
|
+
* every 30 minutes:
|
|
11
|
+
*
|
|
12
|
+
* - a start answer that fails (a refused token, a server error, a broken
|
|
13
|
+
* answer, no answer within 3 s) leaves every rollout off
|
|
14
|
+
* - a call row without rollouts (an older server) keeps what the device has
|
|
15
|
+
* - a 30-minute answer that fails keeps what the device has
|
|
16
|
+
* - a 30-minute answer that arrives after a call row it was waiting behind is
|
|
17
|
+
* dropped: the call row's rollouts are newer
|
|
18
|
+
*
|
|
19
|
+
* With no session token the device sends no request: that counts as a
|
|
20
|
+
* failed answer.
|
|
21
|
+
*
|
|
22
|
+
* Each change logs a debug line, "Rollouts: …".
|
|
23
|
+
*/
|
|
24
|
+
class RolloutManager {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.rollouts = {};
|
|
27
|
+
this.serverUrl = '';
|
|
28
|
+
this.getToken = async () => null;
|
|
29
|
+
this.refreshInterval = null;
|
|
30
|
+
/** How many call rows brought rollouts; a 30-minute answer compares it to tell whether one came meanwhile */
|
|
31
|
+
this.callRowUpdates = 0;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* GET {serverUrl}/device/rollouts with the session's access token, which
|
|
35
|
+
* getToken() is asked for on every request (null: no session, no request).
|
|
36
|
+
* A failed answer leaves every rollout off.
|
|
37
|
+
*/
|
|
38
|
+
async load(serverUrl, getToken) {
|
|
39
|
+
this.serverUrl = serverUrl;
|
|
40
|
+
this.getToken = getToken;
|
|
41
|
+
this.replace((await this.request()) ?? {});
|
|
42
|
+
}
|
|
43
|
+
/** A call row's rollouts (metadata.rollouts); a row without them keeps what the device has */
|
|
44
|
+
update(rollouts) {
|
|
45
|
+
if (!isRolloutMap(rollouts))
|
|
46
|
+
return;
|
|
47
|
+
this.callRowUpdates++;
|
|
48
|
+
this.replace(rollouts);
|
|
49
|
+
}
|
|
50
|
+
/** Asks again every 30 minutes, after load(); a failed answer keeps what the device has */
|
|
51
|
+
startRefresh() {
|
|
52
|
+
// The callback hands back the refresh's promise, which never rejects
|
|
53
|
+
this.refreshInterval = setInterval(() => this.refresh(), REFRESH_INTERVAL_MS);
|
|
54
|
+
// A pending refresh must not keep a process that is otherwise done running
|
|
55
|
+
this.refreshInterval.unref();
|
|
56
|
+
}
|
|
57
|
+
stopRefresh() {
|
|
58
|
+
clearInterval(this.refreshInterval ?? undefined);
|
|
59
|
+
this.refreshInterval = null;
|
|
60
|
+
}
|
|
61
|
+
/** Whether a rollout is on; `defaultValue` for one the server did not name */
|
|
62
|
+
get(name, defaultValue = false) {
|
|
63
|
+
return this.rollouts[name] ?? defaultValue;
|
|
64
|
+
}
|
|
65
|
+
getAll() {
|
|
66
|
+
return { ...this.rollouts };
|
|
67
|
+
}
|
|
68
|
+
async refresh() {
|
|
69
|
+
const callRowUpdatesBefore = this.callRowUpdates;
|
|
70
|
+
const rollouts = await this.request();
|
|
71
|
+
// A call row that came while this request waited is newer than its answer
|
|
72
|
+
if (rollouts && this.callRowUpdates === callRowUpdatesBefore)
|
|
73
|
+
this.replace(rollouts);
|
|
74
|
+
}
|
|
75
|
+
/** The server's answer, or undefined when it failed or took longer than 3 s */
|
|
76
|
+
request() {
|
|
77
|
+
return withTimeout(this.fetchRollouts(), REQUEST_TIMEOUT_MS, 'Rollouts fetch', undefined);
|
|
78
|
+
}
|
|
79
|
+
async fetchRollouts() {
|
|
80
|
+
const token = await this.getToken();
|
|
81
|
+
if (!token)
|
|
82
|
+
throw new Error('No session token');
|
|
83
|
+
const response = await fetch(`${this.serverUrl}/device/rollouts`, {
|
|
84
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
85
|
+
});
|
|
86
|
+
if (!response.ok)
|
|
87
|
+
throw new Error(`HTTP ${response.status}`);
|
|
88
|
+
const { rollouts } = await response.json();
|
|
89
|
+
if (!isRolloutMap(rollouts))
|
|
90
|
+
throw new Error('No rollouts in the answer');
|
|
91
|
+
return rollouts;
|
|
92
|
+
}
|
|
93
|
+
replace(rollouts) {
|
|
94
|
+
if (JSON.stringify(rollouts) === JSON.stringify(this.rollouts))
|
|
95
|
+
return;
|
|
96
|
+
this.rollouts = { ...rollouts };
|
|
97
|
+
console.debug('Rollouts:', this.getAll());
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** A name -> on/off object */
|
|
101
|
+
function isRolloutMap(value) {
|
|
102
|
+
return !!value && typeof value === 'object' && !Array.isArray(value)
|
|
103
|
+
&& Object.values(value).every((on) => typeof on === 'boolean');
|
|
104
|
+
}
|
|
105
|
+
export const rolloutManager = new RolloutManager();
|
|
@@ -1,133 +1,135 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Blocking script to update device status to offline
|
|
5
|
-
* Runs synchronously during shutdown to ensure DB update completes
|
|
6
|
-
*
|
|
7
|
-
* Usage: node blocking-offline-update.js <deviceId> <supabaseUrl> <supabaseKey> <accessToken> [refreshToken]
|
|
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.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { createClient } from '@supabase/supabase-js';
|
|
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
|
-
|
|
31
|
-
// Parse command line arguments
|
|
32
|
-
const [deviceId, supabaseUrl, supabaseKey, accessToken, refreshToken] = process.argv.slice(2);
|
|
33
|
-
|
|
34
|
-
if (!deviceId || !supabaseUrl || !supabaseKey || !accessToken) {
|
|
35
|
-
console.error('❌ Missing required arguments');
|
|
36
|
-
console.error('Usage: node blocking-offline-update.js <deviceId> <supabaseUrl> <supabaseKey> <accessToken> [refreshToken]');
|
|
37
|
-
exitProcess(1);
|
|
38
|
-
} else {
|
|
39
|
-
await markDeviceOffline();
|
|
40
|
-
}
|
|
41
|
-
|
|
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
|
-
//
|
|
59
|
-
//
|
|
60
|
-
//
|
|
61
|
-
//
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Blocking script to update device status to offline
|
|
5
|
+
* Runs synchronously during shutdown to ensure DB update completes
|
|
6
|
+
*
|
|
7
|
+
* Usage: node blocking-offline-update.js <deviceId> <supabaseUrl> <supabaseKey> <accessToken> [refreshToken]
|
|
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.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { createClient } from '@supabase/supabase-js';
|
|
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
|
+
|
|
31
|
+
// Parse command line arguments
|
|
32
|
+
const [deviceId, supabaseUrl, supabaseKey, accessToken, refreshToken] = process.argv.slice(2);
|
|
33
|
+
|
|
34
|
+
if (!deviceId || !supabaseUrl || !supabaseKey || !accessToken) {
|
|
35
|
+
console.error('❌ Missing required arguments');
|
|
36
|
+
console.error('Usage: node blocking-offline-update.js <deviceId> <supabaseUrl> <supabaseKey> <accessToken> [refreshToken]');
|
|
37
|
+
exitProcess(1);
|
|
38
|
+
} else {
|
|
39
|
+
await markDeviceOffline();
|
|
40
|
+
}
|
|
41
|
+
|
|
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
|
+
// Let Postgres stamp last_seen at update time, rather than trusting the
|
|
59
|
+
// device's potentially skewed wall clock. This records shutdown when the
|
|
60
|
+
// DB processes the request, not the precise local shutdown instant.
|
|
61
|
+
// A successful write keeps "last seen X ago" accurate even though the
|
|
62
|
+
// periodic bookkeeping write only runs on the slow capable cadence.
|
|
63
|
+
// The token goes straight to PostgREST as a Bearer header: setSession()
|
|
64
|
+
// would first spend a GoTrue round trip of the budget re-validating it.
|
|
65
|
+
const markOffline = (token) => createClient(supabaseUrl, supabaseKey, {
|
|
66
|
+
auth,
|
|
67
|
+
global: { headers: { Authorization: `Bearer ${token}` }, fetch: fetchBeforeDeadline }
|
|
68
|
+
})
|
|
69
|
+
.from('mcp_devices')
|
|
70
|
+
.update({ status: 'offline', last_seen: 'now' })
|
|
71
|
+
.eq('id', deviceId);
|
|
72
|
+
|
|
73
|
+
function tokenLooksLive(token) {
|
|
74
|
+
try {
|
|
75
|
+
const { exp } = JSON.parse(Buffer.from(token.split('.')[1], 'base64url').toString('utf8'));
|
|
76
|
+
return typeof exp === 'number' && exp * 1000 > Date.now() + 5000;
|
|
77
|
+
} catch {
|
|
78
|
+
// Not a JWT we can read: treat it as expired and refresh the session
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// refreshSession(), not setSession(): setSession() validates a token that
|
|
84
|
+
// looks live on this machine's clock via /user instead of refreshing it.
|
|
85
|
+
// Resolves with the new access token, or undefined after reporting why there is none.
|
|
86
|
+
async function refreshedToken() {
|
|
87
|
+
if (!refreshToken) {
|
|
88
|
+
console.error('❌ Auth error: access token not usable and no refresh token');
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const client = createClient(supabaseUrl, supabaseKey, { auth, global: { fetch: fetchBeforeDeadline } });
|
|
92
|
+
const { data, error: authError } = await client.auth.refreshSession({ refresh_token: refreshToken });
|
|
93
|
+
|
|
94
|
+
if (authError || !data.session) {
|
|
95
|
+
console.error('❌ Auth error:', authError?.message ?? 'no session returned');
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
return data.session.access_token;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Resolves with the exit code
|
|
102
|
+
async function update() {
|
|
103
|
+
try {
|
|
104
|
+
// An expired token (e.g. the machine just woke) is refreshed first. A 401
|
|
105
|
+
// means it only looked live on this machine's clock: refresh and retry once.
|
|
106
|
+
let result = tokenLooksLive(accessToken) ? await markOffline(accessToken) : null;
|
|
107
|
+
if (!result || result.status === 401) {
|
|
108
|
+
const token = await refreshedToken();
|
|
109
|
+
if (!token) {
|
|
110
|
+
return 3; // Exit code 3 for auth error
|
|
111
|
+
}
|
|
112
|
+
result = await markOffline(token);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const { error } = result;
|
|
116
|
+
|
|
117
|
+
if (error) {
|
|
118
|
+
console.error('❌ DB update error:', error.message);
|
|
119
|
+
return 4; // Exit code 4 for DB error
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log('✓ Device marked as offline');
|
|
123
|
+
return 0; // Success
|
|
124
|
+
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error('❌ Unexpected error:', error.message);
|
|
127
|
+
return 5; // Exit code 5 for unexpected error
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const code = await update();
|
|
132
|
+
clearTimeout(timeoutHandle);
|
|
133
|
+
// After a timeout the exit code stays 2 (only exitProcess()'s first call counts)
|
|
134
|
+
exitProcess(code);
|
|
135
|
+
}
|
package/dist/search-manager.js
CHANGED
|
File without changes
|