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.

2.1 KB
Raw Blame
import { isManagedCloudDevice, isWakeableManagedDevice } from '@lobechat/types';

export interface DeviceOptionState {
  /** Locale key (namespace `chat`) for an offline row's status. */
  offlineLabelKey:
    'heteroAgent.executionTarget.offline' | 'heteroAgent.executionTarget.wakesWhenUsed';
  /** Locale key (namespace `chat`) for the row's reconnect or wake control. */
  reconnectLabelKey: 'heteroAgent.executionTarget.reconnect' | 'heteroAgent.executionTarget.wake';
  /** Whether the user may pick the row. */
  selectable: boolean;
  /** Whether the row shows its reconnect or wake control. */
  showReconnect: boolean;
}

/**
 * How one device row of the execution environment picker behaves.
 *
 * - An online device is selectable.
 * - A managed cloud device that sleeps is selectable too: a run bound to it
 *   wakes it before dispatch. It reads "Wakes when used", and its control
 *   wakes it through the server.
 * - Any other offline device is not selectable, and its control reconnects
 *   it through the desktop app.
 * - A managed device that cannot be woken (failed, deleting) has no control:
 *   the desktop app cannot reach it.
 * - A device the current agent cannot bind (`blocked`, e.g. a private device on
 *   a published fixed Agent) is not selectable, whatever its presence: the
 *   save would be rejected. Its reconnect control stays, as reconnecting is
 *   still useful elsewhere.
 */
export const getDeviceOptionState = (
  // Structural, so a `managedPhase` on the list row narrows it when present.
  device: Parameters<typeof isWakeableManagedDevice>[0],
  { blocked = false, presenceUnknown }: { blocked?: boolean; presenceUnknown: boolean },
): DeviceOptionState => {
  const wakeable = isWakeableManagedDevice(device);

  return {
    offlineLabelKey: wakeable
      ? 'heteroAgent.executionTarget.wakesWhenUsed'
      : 'heteroAgent.executionTarget.offline',
    reconnectLabelKey: wakeable
      ? 'heteroAgent.executionTarget.wake'
      : 'heteroAgent.executionTarget.reconnect',
    selectable: !blocked && (device.online || wakeable),
    showReconnect:
      !device.online && !presenceUnknown && (wakeable || !isManagedCloudDevice(device)),
  };
};