Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

5.2 KB
Raw Blame
import { isDesktop } from '@lobechat/const';
import type { GatewayConnectionState } from '@lobechat/electron-client-ipc';
import useSWR from 'swr';
import { type SWRResponse } from 'swr';

import { createReplicaSlice, recordLens, type ReplicaSyncResult } from '@/libs/replica';
import { electronKeys } from '@/libs/swr/keys';
import { gatewayConnectionService } from '@/services/electron/gatewayConnection';
import { type StoreSetter } from '@/store/types';

import {
  GATEWAY_STABLE_DEVICE_ID_KEY,
  type GatewayStableDeviceId,
  gatewayStableDeviceIdResource,
} from '../projection';
import { type ElectronStore } from '../store';

type Setter = StoreSetter<ElectronStore>;
export const gatewaySlice = (set: Setter, get: () => ElectronStore, _api?: unknown) =>
  new ElectronGatewayActionImpl(set, get, _api);

export interface GatewayDeviceInfo {
  deviceId: string;
  hostname: string;
  platform: string;
}

export class ElectronGatewayActionImpl {
  readonly #get: () => ElectronStore;
  readonly #set: Setter;
  readonly #stableDeviceId;

  constructor(set: Setter, get: () => ElectronStore, _api?: unknown) {
    void _api;
    this.#get = get;
    this.#set = set;
    this.#stableDeviceId = createReplicaSlice(gatewayStableDeviceIdResource, {
      actionPrefix: 'gatewayStableDeviceId',
      fetcher: async (): Promise<GatewayStableDeviceId> => ({
        deviceId: (await this.resolveGatewayDeviceId()) ?? null,
      }),
      get,
      set,
      stateKey: 'gatewayStableDeviceIdReplica',
      view: recordLens<ElectronStore, GatewayStableDeviceId>('gatewayStableDeviceIdMap'),
    });
  }

  connectGateway = async (): Promise<void> => {
    this.#set({ gatewayConnectionError: undefined, gatewayConnectionStatus: 'connecting' });
    try {
      const result = await gatewayConnectionService.connect();
      if (!result.success) {
        // The main process owns the outcome (and its reason); a superseded
        // attempt may already be connecting again, so take its state as is.
        this.setGatewayConnectionState(await gatewayConnectionService.getConnectionStatus());
      }
    } catch (error) {
      console.error('Gateway connect failed:', error);
      this.#set({ gatewayConnectionStatus: 'disconnected' });
    }
  };

  disconnectGateway = async (): Promise<void> => {
    try {
      await gatewayConnectionService.disconnect();
      this.#set({ gatewayConnectionError: undefined, gatewayConnectionStatus: 'disconnected' });
    } catch (error) {
      console.error('Gateway disconnect failed:', error);
    }
  };

  setGatewayConnectionState = ({ error, status }: GatewayConnectionState): void => {
    this.#set(
      { gatewayConnectionError: error, gatewayConnectionStatus: status },
      false,
      'setGatewayConnectionState',
    );
  };

  /**
   * This desktop's stable device id, for writes that pin a topic's cwd to it.
   *
   * Asked over IPC every time rather than read from `gatewayDeviceInfo`: before
   * the gateway connects, that holds the install's fallback UUID, which stops
   * naming this machine once the user-scoped identity is derived. The IPC
   * derives that identity locally β€” no network on the send path β€” and returns
   * nothing while signed out, so no pin is written rather than a stale one.
   * A differing cached id is corrected too, so "this device" comparisons agree
   * with the pins written here.
   */
  resolveGatewayDeviceId = async (): Promise<string | undefined> => {
    if (!isDesktop) return;
    try {
      const deviceId = (await gatewayConnectionService.getStableDeviceId()) ?? undefined;
      const cached = this.#get().gatewayDeviceInfo;
      if (deviceId && cached && cached.deviceId !== deviceId) {
        this.#set({ gatewayDeviceInfo: { ...cached, deviceId } }, false, 'setGatewayDeviceInfo');
      }
      return deviceId;
    } catch (error) {
      console.error('Gateway device id lookup failed:', error);
      return;
    }
  };

  /**
   * Keep this machine's stable device id in sync for a mounted view that must
   * not act before it is known. Data comes from
   * `electronSelectors.gatewayStableDeviceId`; resolving also corrects a cached
   * fallback id in `gatewayDeviceInfo`.
   */
  useFetchGatewayStableDeviceId = (enabled = true): ReplicaSyncResult =>
    this.#stableDeviceId.useSync(isDesktop && enabled ? GATEWAY_STABLE_DEVICE_ID_KEY : null);

  useFetchGatewayDeviceInfo = (): SWRResponse<GatewayDeviceInfo> => {
    return useSWR<GatewayDeviceInfo>(
      // Desktop-only IPC: on web there is no electronAPI, so never fetch off-desktop.
      isDesktop ? electronKeys.gatewayDeviceInfo() : null,
      async () => gatewayConnectionService.getDeviceInfo() as Promise<GatewayDeviceInfo>,
      {
        onSuccess: (data) => {
          this.#set({ gatewayDeviceInfo: data }, false, 'setGatewayDeviceInfo');
        },
      },
    );
  };

  useFetchGatewayStatus = (): SWRResponse<GatewayConnectionState> => {
    return useSWR<GatewayConnectionState>(
      isDesktop ? 'electron:getGatewayConnectionStatus' : null,
      async () => gatewayConnectionService.getConnectionStatus(),
      { onSuccess: (data) => this.setGatewayConnectionState(data) },
    );
  };
}

export type ElectronGatewayAction = Pick<
  ElectronGatewayActionImpl,
  keyof ElectronGatewayActionImpl
>;