Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

14.0 KB
Raw Blame
import { action, computed, makeObservable, observable, runInAction } from 'mobx';
import { toast } from 'sonner';
import { createUpdateToastActionLabel } from '@renderer/lib/components/update-toast-action-label';
import { events, rpc } from '@renderer/lib/ipc';
import { reportOpenAttempt } from '@renderer/lib/open-external';
import { appState } from '@renderer/lib/stores/app-state';
import { menuCheckForUpdatesChannel } from '@shared/events/appEvents';
import {
  updateAvailableEvent,
  updateCheckingEvent,
  updateDownloadedEvent,
  updateDownloadingEvent,
  updateErrorEvent,
  updateInstallFailedEvent,
  updateInstallingEvent,
  updateNotAvailableEvent,
  updateProgressEvent,
} from '@shared/events/updateEvents';

const LAST_NOTIFIED_KEY = 'switchdash:update:lastNotified';
const SNOOZE_HOURS = 6;
/** One "Update Available" toast at a time: a repeat replaces it, and it goes once the update moves on. */
export const UPDATE_AVAILABLE_TOAST_ID = 'console-update-available';
/** One "didn't install" toast at a time, however many times main repeats it. */
export const UPDATE_INSTALL_FAILED_TOAST_ID = 'console-update-install-failed';
/** Shared by the update toasts' action buttons, so they look and move alike. */
const UPDATE_TOAST_CLASS_NAMES = {
  actionButton:
    'group/action cursor-pointer transition-all duration-150 hover:bg-primary/85 active:scale-[0.97]',
};

type DownloadProgress = {
  percent?: number;
  transferred?: number;
  total?: number;
  bytesPerSecond?: number;
};

export type UpdateReleaseInfo = {
  version: string;
  releaseDate?: string;
  releaseName?: string | null;
};

export type UpdateState =
  | { status: 'idle' }
  | { status: 'checking' }
  | { status: 'available'; info?: UpdateReleaseInfo }
  | { status: 'not-available' }
  | { status: 'downloading'; progress?: DownloadProgress }
  | { status: 'downloaded' }
  | { status: 'installing' }
  /** The update was downloaded but cannot or did not install; it has to be downloaded by hand. */
  | { status: 'install-failed'; message: string }
  | { status: 'error'; message: string };

/** Statuses where main is mid-flight and a fresh check would interrupt it. */
const IN_FLIGHT_STATUSES: ReadonlySet<UpdateState['status']> = new Set([
  'downloading',
  'downloaded',
  'installing',
]);

/**
 * Shape of the main-process update state, described structurally so the
 * renderer does not import from @main. Keep in sync with UpdateState in
 * src/main/core/updates/update-service.ts.
 */
type MainUpdateState = {
  status:
    | 'idle'
    | 'checking'
    | 'available'
    | 'downloading'
    | 'downloaded'
    | 'installing'
    | 'install-failed'
    | 'error';
  currentVersion: string;
  availableVersion?: string;
  updateInfo?: { version: string; releaseDate?: string; releaseName?: string | null };
  downloadProgress?: DownloadProgress;
  error?: string;
  installFailedAtLaunch?: boolean;
};

export function mainStateToRendererState(main: MainUpdateState): UpdateState {
  switch (main.status) {
    case 'checking':
      return { status: 'checking' };
    case 'available':
      return {
        status: 'available',
        info: main.availableVersion
          ? {
              version: main.availableVersion,
              releaseDate: main.updateInfo?.releaseDate,
              releaseName: main.updateInfo?.releaseName,
            }
          : undefined,
      };
    case 'downloading':
      return { status: 'downloading', progress: main.downloadProgress };
    case 'downloaded':
      return { status: 'downloaded' };
    case 'installing':
      return { status: 'installing' };
    case 'install-failed':
      return {
        status: 'install-failed',
        message: main.error || "The update didn't install.",
      };
    case 'error':
      return { status: 'error', message: main.error || 'The update could not be completed.' };
    default:
      return { status: 'idle' };
  }
}

export class UpdateStore {
  state: UpdateState = { status: 'idle' };
  currentVersion = '';
  availableVersion: string | undefined = undefined;

  constructor() {
    makeObservable(this, {
      state: observable,
      currentVersion: observable,
      availableVersion: observable,
      setState: action,
      hasUpdate: computed,
      progressLabel: computed,
      latestVersion: computed,
    });
  }

  get hasUpdate(): boolean {
    const { status } = this.state;
    return status === 'available' || status === 'downloading' || status === 'downloaded';
  }

  setState(state: UpdateState): void {
    this.state = state;
  }

  get progressLabel(): string {
    if (this.state.status !== 'downloading') return '';
    const p = this.state.progress?.percent ?? 0;
    return `${p.toFixed(0)}%`;
  }

  /** Version being offered/downloaded, independent of the current status. */
  get latestVersion(): string | undefined {
    if (this.state.status === 'available' && this.state.info?.version) {
      return this.state.info.version;
    }
    return this.availableVersion;
  }

  start(): void {
    void rpc.app.getAppVersion().then((v) => {
      runInAction(() => {
        this.currentVersion = v;
      });
    });

    events.on(updateCheckingEvent, () => {
      // Keep the failure and its "Download manually" button while a check runs.
      if (this.state.status === 'install-failed') return;
      runInAction(() => {
        this.state = { status: 'checking' };
      });
    });

    events.on(updateAvailableEvent, (d) => {
      runInAction(() => {
        this.availableVersion = d.version;
        this.state = {
          status: 'available',
          info: {
            version: d.version,
            releaseDate: d.updateInfo?.releaseDate,
            releaseName: d.updateInfo?.releaseName,
          },
        };
      });
      this._maybeToastAvailable(d.version);
    });

    events.on(updateNotAvailableEvent, () => {
      this._dismissAvailableToast();
      runInAction(() => {
        this.state = { status: 'not-available' };
      });
    });

    events.on(updateDownloadingEvent, (_d) => {
      this._dismissAvailableToast();
      runInAction(() => {
        this.state = { status: 'downloading', progress: { percent: 0 } };
      });
    });

    events.on(updateProgressEvent, (d) => {
      runInAction(() => {
        this.state = {
          status: 'downloading',
          progress: {
            percent: d.percent,
            transferred: d.transferred,
            total: d.total,
            bytesPerSecond: d.bytesPerSecond,
          },
        };
      });
    });

    events.on(updateDownloadedEvent, (d) => {
      this._dismissAvailableToast();
      runInAction(() => {
        if (d?.version) this.availableVersion = d.version;
        this.state = { status: 'downloaded' };
      });
    });

    events.on(updateInstallingEvent, () => {
      this._dismissAvailableToast();
      runInAction(() => {
        this.state = { status: 'installing' };
      });
    });

    events.on(updateInstallFailedEvent, (d) => {
      this._dismissAvailableToast();
      runInAction(() => {
        this.availableVersion = d.version;
        this.state = { status: 'install-failed', message: d.message };
      });
      // A failure after a click is already on the panel or the card that was
      // clicked. Only one found at launch needs telling.
      if (d.foundAtLaunch) this._toastInstallFailed(d.message);
    });

    events.on(updateErrorEvent, (d) => {
      runInAction(() => {
        this.state = { status: 'error', message: d.message };
      });
    });

    events.on(menuCheckForUpdatesChannel, () => {
      void this.check();
    });

    // Adopt whatever main already knows before kicking off a new check: a
    // renderer reload during a download would otherwise show idle while the
    // download continues in the background.
    void this._resyncFromMain().then(() => {
      if (IN_FLIGHT_STATUSES.has(this.state.status)) return;
      rpc.update.check('startup').catch(() => {});
    });
  }

  private async _resyncFromMain(): Promise<void> {
    try {
      const res = await rpc.update.getState();
      if (!res?.success || !res.data) return;
      const main = res.data;

      runInAction(() => {
        if (main.currentVersion && main.currentVersion !== 'unknown') {
          this.currentVersion = main.currentVersion;
        }
        this.availableVersion = main.availableVersion;
        this.state = mainStateToRendererState(main);
      });
      // Main usually finds this out at launch before the window listens: the
      // update the user restarted for did not take, and they should hear so.
      if (this.state.status === 'install-failed' && main.installFailedAtLaunch) {
        this._toastInstallFailed(this.state.message);
      }
    } catch {
      // A failed resync is not itself an update failure — the check that
      // follows will establish real state.
    }
  }

  async check(): Promise<void> {
    // Main refuses a check during an install, and that refusal reads as "nothing to install".
    if (this.state.status === 'installing') return;
    if (this.state.status !== 'install-failed') {
      runInAction(() => {
        this.state = { status: 'checking' };
      });
    }
    try {
      const res = await rpc.update.check();
      if (!res) {
        this._setCheckResult({ status: 'error', message: 'Update API unavailable' });
        return;
      }
      if (!res.success) {
        this._setCheckResult({
          status: 'error',
          message: res.error ?? 'Failed to check for updates',
        });
      } else if (res.result === null) {
        this._setCheckResult({ status: 'idle' });
      }
    } catch {
      this._setCheckResult({ status: 'error', message: 'Failed to check for updates' });
    }
  }

  /**
   * Main sends `installing` and `install-failed` itself, and it keeps them
   * through a failed check. A check result must not replace them: "Try again" on
   * an `error` state would download the version that already failed.
   */
  private _setCheckResult(state: UpdateState): void {
    if (this.state.status === 'installing' || this.state.status === 'install-failed') return;
    runInAction(() => {
      this.state = state;
    });
  }

  async download(): Promise<void> {
    try {
      const res = await rpc.update.download();
      if (!res) {
        this._failUserAction('Update API unavailable');
        return;
      }
      if (!res.success) {
        this._failUserAction(res.error ?? 'Failed to download update');
      }
    } catch {
      this._failUserAction('Failed to download update');
    }
  }

  async install(): Promise<void> {
    runInAction(() => {
      this.state = { status: 'installing' };
    });
    try {
      const res = await rpc.update.quitAndInstall();
      if (!res) {
        this._failUserAction('Update API unavailable');
        return;
      }
      if (!res.success) {
        this._failUserAction(res.error ?? 'Failed to install update');
      }
    } catch {
      this._failUserAction('Failed to install update');
    }
  }

  /**
   * Open the release page to download the update by hand. Main quits the app
   * after. If the page does not open, main does not quit, so say why.
   */
  async openLatest(): Promise<void> {
    await reportOpenAttempt(
      rpc.update.openLatest(),
      'Could not open the download page',
      "Switch Console couldn't open the download page."
    );
  }

  /**
   * Open this update's GitHub release page in the user's browser. Main picks the
   * page, because only main knows when the version is the dev harness's own.
   */
  async openReleasePage(): Promise<void> {
    await reportOpenAttempt(
      rpc.update.openReleaseNotes(),
      'Could not open the release page',
      'The release page could not be opened.'
    );
  }

  /**
   * Record a failure the user personally triggered. Background check failures
   * only tint the sidebar indicator; a click that fails gets a toast, because
   * the user is waiting on an answer.
   */
  private _failUserAction(message: string): void {
    runInAction(() => {
      this.state = { status: 'error', message };
    });
    toast.error('Update failed', { description: message, duration: 8_000 });
  }

  /**
   * Point at the update card rather than repeat its button: "Download manually"
   * quits the app, so it lives where the reason and the button are read together.
   */
  private _toastInstallFailed(message: string): void {
    toast.error("Update didn't install", {
      id: UPDATE_INSTALL_FAILED_TOAST_ID,
      description: message,
      duration: 10_000,
      classNames: UPDATE_TOAST_CLASS_NAMES,
      action: {
        label: createUpdateToastActionLabel('Details'),
        onClick: () => appState.navigation.navigate('settings', { tab: 'status' }),
      },
    });
  }

  private _maybeToastAvailable(version: string): void {
    if (!this._shouldNotify(version)) return;
    this._showAvailableToast(version);
    this._rememberNotified(version);
  }

  private _showAvailableToast(version: string): void {
    toast('Update Available', {
      id: UPDATE_AVAILABLE_TOAST_ID,
      description: `Version ${version} is available to download and install.`,
      duration: 10_000,
      classNames: UPDATE_TOAST_CLASS_NAMES,
      action: {
        label: createUpdateToastActionLabel('Update'),
        onClick: () => {
          appState.navigation.navigate('settings', { tab: 'status' });
          if (this.state.status === 'available') {
            void this.download();
          }
        },
      },
    });
  }

  private _dismissAvailableToast(): void {
    toast.dismiss(UPDATE_AVAILABLE_TOAST_ID);
  }

  private _shouldNotify(version: string): boolean {
    try {
      const raw = localStorage.getItem(LAST_NOTIFIED_KEY);
      if (!raw) return true;
      const parsed = JSON.parse(raw) as { version?: string; at?: number };
      if (parsed.version === version) {
        const at = parsed.at ?? 0;
        if (Date.now() - at < Math.max(1, SNOOZE_HOURS) * 3_600_000) return false;
      }
      return true;
    } catch {
      return true;
    }
  }

  private _rememberNotified(version: string): void {
    try {
      localStorage.setItem(LAST_NOTIFIED_KEY, JSON.stringify({ version, at: Date.now() }));
    } catch {
      // localStorage may be unavailable
    }
  }
}