Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

1.4 KB
Raw Blame
/*
 * Copyright 2025 Daytona Platforms Inc.
 * Modified by BoxLite AI, 2025-2026
 * SPDX-License-Identifier: AGPL-3.0
 */

import { Box, BoxDesiredState } from '@boxlite-ai/api-client'

type BoxIdentity = Pick<Box, 'id' | 'name'> & Partial<Pick<Box, 'desiredState'>>

export const MISSING_BOX_ID_LABEL = 'Not available'

export function getBoxPublicId(box: Partial<Pick<Box, 'id'>> | undefined): string {
  return box?.id || ''
}

export function getBoxPublicIdLabel(box: Partial<Pick<Box, 'id'>> | undefined): string {
  return getBoxPublicId(box) || MISSING_BOX_ID_LABEL
}

export function getBoxRouteId(box: Partial<Pick<Box, 'id'>> | undefined): string {
  return box?.id || ''
}

export function getBoxDisplayName(box: BoxIdentity): string {
  const name = getNormalizedBoxName(box)
  if (name && name !== box.id && !isUuidLike(name)) {
    return name
  }
  return box.id || 'Box'
}

function getNormalizedBoxName(box: BoxIdentity): string {
  if (box.desiredState === BoxDesiredState.DESTROYED && box.name.startsWith('DESTROYED_')) {
    const withoutPrefix = box.name.substring(10)
    const lastUnderscoreIndex = withoutPrefix.lastIndexOf('_')
    if (lastUnderscoreIndex !== -1) {
      return withoutPrefix.substring(0, lastUnderscoreIndex)
    }
    return withoutPrefix
  }

  return box.name
}

function isUuidLike(value: string): boolean {
  return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value)
}