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.

3.6 KB
Raw Blame
import {
  DEVICE_TERMINAL_METADATA_KEY,
  type DeviceListItem,
  type DeviceMetricSeries,
} from '@lobechat/types';

import { getChannelKind } from '@/features/DeviceManager/channelKind';

import type { DeviceState } from './initialState';
import {
  DEVICE_LIST_KEY,
  deviceMetricSeriesKey,
  type DeviceMetricSeriesParams,
} from './projection';

const EMPTY_DEVICES: DeviceListItem[] = [];

const deviceList = (s: DeviceState): DeviceListItem[] =>
  s.deviceListMap[DEVICE_LIST_KEY] ?? EMPTY_DEVICES;

/** The list is on screen — from the persisted copy or the server. */
const isDeviceListLoaded = (s: DeviceState): boolean =>
  s.deviceListMap[DEVICE_LIST_KEY] !== undefined;

/**
 * The list was confirmed by the server this session. A persisted copy holds
 * stale `workingDirs` and unknown presence, so writes that merge into a device
 * row wait for this.
 */
const isDeviceListConfirmed = (s: DeviceState): boolean =>
  s.deviceListReplica.entries[DEVICE_LIST_KEY]?.source === 'server';

/**
 * Whether a shell opened from the browser can run on this device.
 *
 * The PTY lives in the machine's `lh connect` process or, on a desktop that
 * reports {@link DEVICE_TERMINAL_METADATA_KEY}, in the app's own gateway
 * connection. Terminal RPCs carry a `cli` channel hint the gateway only treats
 * as a *preference*, so a row whose live connections are all from an older
 * desktop answers them with a refusal. Requiring a capable live connection is
 * what keeps the picker from offering a target that cannot open a shell.
 *
 * Two edges: an offline row is a registry entry rather than a reachable host,
 * and a live row that reports no connections at all is a gateway that predates
 * the field — unknown, not unusable.
 */
export const canHostTerminal = (device: DeviceListItem): boolean => {
  if (!device.online) return false;
  const channels = device.channels ?? [];
  if (channels.length === 0) return true;
  const desktopServesTerminal = device.metadata?.[DEVICE_TERMINAL_METADATA_KEY] === '1';
  return channels.some((channel) => {
    const kind = getChannelKind(channel.channel);
    return kind === 'cli' || (kind === 'desktop' && desktopServesTerminal);
  });
};

/**
 * Whether any device can host a terminal.
 *
 * A boolean, not the filtered list: zustand compares selector results with
 * `Object.is`, and a fresh array on every call would re-render the panel on
 * every unrelated store write.
 */
const hasTerminalTarget = (s: DeviceState): boolean => deviceList(s).some(canHostTerminal);

const getDeviceById =
  (deviceId: string | undefined) =>
  (s: DeviceState): DeviceListItem | undefined =>
    deviceId ? deviceList(s).find((d) => d.deviceId === deviceId) : undefined;

/** A device's user-configured default working directory (per-device fallback cwd). */
const getDeviceDefaultCwd =
  (deviceId: string | undefined) =>
  (s: DeviceState): string | undefined =>
    getDeviceById(deviceId)(s)?.defaultCwd ?? undefined;

/** A device's recent working dirs (also the cache for workspace-init / repoType). */
const getDeviceWorkingDirs = (deviceId: string | undefined) => (s: DeviceState) =>
  getDeviceById(deviceId)(s)?.workingDirs ?? [];

/** A device's health series, or `undefined` until storage or the server answers. */
const getMetricSeries =
  (params: DeviceMetricSeriesParams) =>
  (s: DeviceState): DeviceMetricSeries | undefined =>
    s.deviceMetricSeriesMap[deviceMetricSeriesKey(params)];

export const deviceSelectors = {
  deviceList,
  getDeviceById,
  getDeviceDefaultCwd,
  getDeviceWorkingDirs,
  getMetricSeries,
  hasTerminalTarget,
  isDeviceListConfirmed,
  isDeviceListLoaded,
};