UNPKG

@wonderwhy-er/desktop-commander

Version:

MCP server for terminal operations and file editing

126 lines (125 loc) • 5.16 kB
import { withTimeout } from '../utils/withTimeout.js'; /** How long a request for the rollouts waits for the server's answer */ const REQUEST_TIMEOUT_MS = 3000; /** How often a running device asks again, as often as feature flags are refreshed */ const REFRESH_INTERVAL_MS = 30 * 60 * 1000; /** * Which rollouts are on for this device's user. A rollout turns something on * for a share of users, and the Remote MCP server decides who is in. The * device asks at start, takes the map each call row carries, and asks again * every 30 minutes: * * - a start answer that fails (a refused token, a server error, a broken * answer, no answer within 3 s) leaves every rollout off * - a call row without rollouts (an older server) keeps what the device has * - a 30-minute answer that fails keeps what the device has * - a 30-minute answer that arrives after a call row it was waiting behind is * dropped: the call row's rollouts are newer * * With no session token the device sends no request: that counts as a * failed answer. * * Each change logs a debug line, "Rollouts: …". */ class RolloutManager { constructor() { this.rollouts = {}; this.listeners = new Set(); this.serverUrl = ''; this.getToken = async () => null; this.refreshInterval = null; /** How many call rows brought rollouts; a 30-minute answer compares it to tell whether one came meanwhile */ this.callRowUpdates = 0; } /** * GET {serverUrl}/device/rollouts with the session's access token, which * getToken() is asked for on every request (null: no session, no request). * A failed answer leaves every rollout off. */ async load(serverUrl, getToken) { this.serverUrl = serverUrl; this.getToken = getToken; this.replace((await this.request()) ?? {}); } /** A call row's rollouts (metadata.rollouts); a row without them keeps what the device has */ update(rollouts) { if (!isRolloutMap(rollouts)) return; this.callRowUpdates++; this.replace(rollouts); } /** Asks again every 30 minutes, after load(); a failed answer keeps what the device has */ startRefresh() { // The callback hands back the refresh's promise, which never rejects this.refreshInterval = setInterval(() => this.refresh(), REFRESH_INTERVAL_MS); // A pending refresh must not keep a process that is otherwise done running this.refreshInterval.unref(); } stopRefresh() { clearInterval(this.refreshInterval ?? undefined); this.refreshInterval = null; } /** Whether a rollout is on; `defaultValue` for one the server did not name */ get(name, defaultValue = false) { return this.rollouts[name] ?? defaultValue; } getAll() { return { ...this.rollouts }; } /** Listen for a named rollout changing; a missing rollout is off. Returns a cleanup function. */ onChange(name, callback) { const listener = { name, callback }; this.listeners.add(listener); return () => { this.listeners.delete(listener); }; } async refresh() { const callRowUpdatesBefore = this.callRowUpdates; const rollouts = await this.request(); // A call row that came while this request waited is newer than its answer if (rollouts && this.callRowUpdates === callRowUpdatesBefore) this.replace(rollouts); } /** The server's answer, or undefined when it failed or took longer than 3 s */ request() { return withTimeout(this.fetchRollouts(), REQUEST_TIMEOUT_MS, 'Rollouts fetch', undefined); } async fetchRollouts() { const token = await this.getToken(); if (!token) throw new Error('No session token'); const response = await fetch(`${this.serverUrl}/device/rollouts`, { headers: { Authorization: `Bearer ${token}` }, redirect: 'error', }); if (!response.ok) throw new Error(`HTTP ${response.status}`); const { rollouts } = await response.json(); if (!isRolloutMap(rollouts)) throw new Error('No rollouts in the answer'); return rollouts; } replace(rollouts) { if (JSON.stringify(rollouts) === JSON.stringify(this.rollouts)) return; const previous = this.rollouts; this.rollouts = { ...rollouts }; console.debug('Rollouts:', this.getAll()); for (const { name, callback } of this.listeners) { const enabled = this.rollouts[name] ?? false; if (enabled === (previous[name] ?? false)) continue; try { callback(enabled); } catch { console.debug('[Rollouts] Change listener failed:', name); } } } } /** A name -> on/off object */ function isRolloutMap(value) { return !!value && typeof value === 'object' && !Array.isArray(value) && Object.values(value).every((on) => typeof on === 'boolean'); } export const rolloutManager = new RolloutManager();