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.

4.3 KB
Raw Blame
import { isMachineErrorMessage } from '@/utils/machineErrorMessage';

/**
 * What a failed environment action should say to the person.
 *
 * The server answers refusals in two shapes neither of which is a sentence:
 * a bare code such as `ENVIRONMENT_HAS_INSTANCES`, and, for input the router
 * rejected, the JSON of the validation issues. Both reached the toast verbatim,
 * so a refusal made on purpose read like a crash. Each is turned into the
 * message it stands for here; anything else is shown as it came, and an error
 * with no message at all β€” or one that is plainly machine output rather than a
 * sentence β€” falls back to the caller's generic line.
 */
const CODE_KEYS: Record<string, string> = {
  // A drive download over the size the sandbox packs in one request.
  DOWNLOAD_TOO_LARGE: 'drive.explorer.downloadTooLarge',
  // A deployment whose sandbox has no shell to pack a download with.
  DOWNLOAD_UNAVAILABLE: 'drive.explorer.downloadUnavailable',
  DUPLICATE_INSTANCE_DIRECTORY: 'environments.files.folderTaken',
  ENVIRONMENT_HAS_INSTANCES: 'environments.hasInstances',
  // The execution plane calls this an instance now too. The old
  // code is kept until that rename is deployed everywhere, so a refusal from a
  // server still on the previous build reads as a sentence rather than a code.
  ENVIRONMENT_IN_PROJECT: 'environments.inProject',
  ENVIRONMENT_IN_USE: 'environments.instances.inUse',
  // Initializing a folder that already holds work would mix a checkout into it.
  FOLDER_NOT_EMPTY: 'environments.files.folderNotEmpty',
  INSTANCE_BUILDING: 'environments.instances.stopBuilding',
  INSTANCE_BUSY: 'environments.instances.stopBusy',
  INSTANCE_IN_USE: 'environments.instances.inUse',
  // An environment directory is written by its own sessions alone.
  INSTANCE_READ_ONLY: 'environments.files.readOnly',
  // A private environment's directory left on the public drive.
  INSTANCE_REBUILD_REQUIRED: 'environments.instances.rebuildPersonalRequired',
  INSTANCE_STARTING: 'environments.instances.stopStarting',
  INSTANCE_STOP_FAILED: 'environments.instances.stopRefused',
  // Deleting a folder that holds a private environment directory left on the public drive.
  PATH_HOLDS_HIDDEN: 'drive.cleanup.holdsHidden',
  PATH_NOT_FOUND: 'environments.files.notFound',
  PATH_OUTSIDE_INSTANCE: 'environments.files.invalidPath',
  // Retryable on purpose: the run is still packing its snapshot, and the
  // execution plane would rather leave it running than cut the save off.
  SNAPSHOT_IN_PROGRESS: 'environments.instances.stopSnapshotPending',
  // A storage service deployed before the drive could be analyzed.
  storage_analysis_unavailable: 'drive.cleanup.unavailable',
  // `regenerableOnly` asked for, and some path is not a dependency, cache or build output.
  storage_paths_not_regenerable: 'drive.cleanup.notRegenerable',
};

/**
 * A refusal that arrived as a bare machine code this build has no sentence for
 * β€” a newer server, or a code only another surface handles. Printing it puts
 * `WORKSPACE_NOT_CONFIGURED` in front of someone; the caller's own line at
 * least names the action that failed.
 */
const isUntranslatedCode = (message: string): boolean => /^[A-Z][\dA-Z_]{2,}$/.test(message);

const readIssues = (message: string): string[] | undefined => {
  if (!message.startsWith('[')) return;
  try {
    const parsed = JSON.parse(message) as unknown;
    if (
      Array.isArray(parsed) &&
      parsed.length > 0 &&
      parsed.every((issue) => typeof (issue as { message?: unknown })?.message === 'string')
    ) {
      return parsed.map((issue) => (issue as { message: string }).message);
    }
  } catch {
    // not JSON: an ordinary message that happens to start with a bracket
  }
};

export const describeError = (
  error: unknown,
  t: (key: any, options?: any) => string,
  fallback: string,
): string => {
  const message = (error as { message?: unknown })?.message;
  if (typeof message !== 'string' || !message) return fallback;

  const key = CODE_KEYS[message];
  if (key) return t(key);

  if (isMachineErrorMessage(message) || isUntranslatedCode(message)) return fallback;

  const issues = readIssues(message);
  if (issues) {
    if (issues.some((issue) => /relative path inside the workspace/i.test(issue))) {
      return t('environments.files.invalidPath');
    }
    return issues.join('; ');
  }

  return message;
};