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.

6.3 KB
Raw Blame
import { isOfficialCloudServer } from '@lobechat/const';
import {
  type DeviceGatewayDiscovery,
  type DeviceGatewayEndpoint,
  type DeviceGatewayResolution,
  type DeviceGatewayUrlSource,
  parseDeviceGatewayDiscovery,
  resolveDeviceGatewayEndpoint,
} from '@lobechat/device-gateway-client';

import { createPublicLambdaClient } from '../api/client';
import { CLI_PRIMARY_BIN } from '../constants/identity';
import { OFFICIAL_GATEWAY_URL } from '../constants/urls';
import { redactUrlCredentials, scrubText } from '../doctor/redact';
import { loadDeviceGatewayUrl, saveDeviceGatewayUrl } from '../settings';
import { formatError, isTransientNetworkError } from '../utils/error';

/** Total discovery budget, including retries, leaving room in the 30s daemon startup window. */
const DISCOVERY_TIMEOUT_MS = 10_000;
/** Retry quick transient failures against the same server within the total budget. */
const DISCOVERY_RETRY_DELAYS_MS = [250, 1000, 2500];

export type DeviceGatewayFailure = Extract<DeviceGatewayResolution, { ok: false }>;

/**
 * Ask `serverUrl` which Device Gateway its clients should use. Anonymous on
 * purpose β€” the address is public β€” and never throws: an unreachable server,
 * an error status or a body that is not a global config is `failed`, distinct
 * from a server that simply advertises nothing.
 */
export async function discoverDeviceGateway(serverUrl: string): Promise<DeviceGatewayDiscovery> {
  const controller = new AbortController();
  const deadline = Date.now() + DISCOVERY_TIMEOUT_MS;
  const timeout = setTimeout(() => {
    controller.abort(new Error('Device gateway discovery timed out'));
  }, DISCOVERY_TIMEOUT_MS);

  try {
    const client = createPublicLambdaClient(serverUrl);
    for (let attempt = 0; ; attempt += 1) {
      try {
        const config = await client.config.getGlobalConfig.query(undefined, {
          signal: controller.signal,
        });
        return parseDeviceGatewayDiscovery(config);
      } catch (error) {
        // Retry the same server only; never start a delay beyond the shared deadline.
        const delay = DISCOVERY_RETRY_DELAYS_MS[attempt];
        if (
          delay !== undefined &&
          !controller.signal.aborted &&
          delay < deadline - Date.now() &&
          isTransientNetworkError(error)
        ) {
          await new Promise((resolve) => setTimeout(resolve, delay));
          continue;
        }
        // Preserve the cause chain while removing URL credentials before any caller logs it.
        return {
          reason: scrubText(
            formatError(controller.signal.aborted ? controller.signal.reason : error),
          ),
          status: 'failed',
        };
      }
    }
  } finally {
    clearTimeout(timeout);
  }
}

/**
 * The Device Gateway for a credential's server β€” `auth.serverUrl` as resolved
 * with the token, so the address, the server and the token always belong to
 * one login. `connect`, `status` and `doctor` all resolve through here.
 *
 * Order: `--gateway`, then the address saved for this server (both without
 * asking it), then what the server advertises, then β€” when it advertises
 * nothing β€” the official gateway for official cloud. A self-hosted server
 * keeps needing an address, as before discovery existed. A failed lookup is an
 * error, never a reason to try another deployment's gateway.
 */
export async function resolveCliDeviceGateway({
  override,
  serverUrl,
}: {
  override?: string;
  serverUrl: string;
}): Promise<DeviceGatewayResolution> {
  return resolveDeviceGatewayEndpoint({
    discover: () => discoverDeviceGateway(serverUrl),
    manualUrl: loadDeviceGatewayUrl(serverUrl),
    officialGatewayUrl: isOfficialCloudServer(serverUrl) ? OFFICIAL_GATEWAY_URL : undefined,
    override,
  });
}

/**
 * Keep an explicit `--gateway` as this server's saved address, reused by later
 * runs ahead of whatever the server advertises β€” and only for this server.
 *
 * @returns whether anything changed.
 */
export function rememberGatewayOverride(
  serverUrl: string,
  endpoint: DeviceGatewayEndpoint,
): boolean {
  if (endpoint.source !== 'override') return false;
  if (loadDeviceGatewayUrl(serverUrl) === endpoint.url) return false;

  saveDeviceGatewayUrl(serverUrl, endpoint.url);
  return true;
}

const SOURCE_LABELS: Record<DeviceGatewayUrlSource, string> = {
  manual: 'saved for this server',
  official: 'official default',
  override: '--gateway',
  server: 'advertised by the server',
};

/** `https://gw.example.com (advertised by the server)`, safe to print. */
export function describeDeviceGateway(endpoint: DeviceGatewayEndpoint): string {
  return `${redactUrlCredentials(endpoint.url)} (${SOURCE_LABELS[endpoint.source]})`;
}

/** What went wrong and what to do about it, safe to print. */
export function describeDeviceGatewayFailure(
  failure: DeviceGatewayFailure,
  serverUrl: string,
): { detail: string; fix: string } {
  const server = redactUrlCredentials(serverUrl);
  const remember = `Run '${CLI_PRIMARY_BIN} connect --gateway <url>' once with this server's device gateway; it is remembered for ${server} only.`;

  switch (failure.reason) {
    case 'discovery_failed': {
      return {
        detail: `Could not read the device gateway address from ${server}: ${failure.detail ?? 'unknown error'}.`,
        fix: `Check that ${server} is reachable and is the server you logged in to, then retry. A gateway set with '--gateway <url>' is remembered for this server and skips the lookup.`,
      };
    }
    case 'invalid_advertised_url': {
      return {
        detail: `${server} advertises a device gateway address that is not a valid http(s) URL.`,
        fix: `Ask the server administrator to fix DEVICE_GATEWAY_PUBLIC_URL. Until then: ${remember}`,
      };
    }
    case 'invalid_manual': {
      return {
        detail: `The device gateway address saved for ${server} is not a valid http(s) URL.`,
        fix: `Replace it: ${remember}`,
      };
    }
    case 'invalid_override': {
      return {
        detail: '--gateway is not a valid http(s) URL without credentials, query or fragment.',
        fix: 'Pass the gateway base URL, e.g. --gateway https://gateway.example.com',
      };
    }
    case 'not_configured': {
      return {
        detail: `${server} does not advertise a device gateway, and none is saved for it.`,
        fix: remember,
      };
    }
  }
}