Skip to content

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

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

import { BoxDto } from '../../box/dto/box.dto'
import { publicBoxStatus } from '../../box/utils/box-public-status'
import {
  AUTO_DELETE_DISABLED,
  DEFAULT_AUTO_STOP_SECONDS,
  DEFAULT_AUTO_RESUME,
} from '../../box/constants/box-lifecycle.constants'
import { BoxResponseDto } from '../dto/box-response.dto'
import { CreateBoxDto as RestCreateBoxDto } from '../dto/create-box.dto'
import { CreateBoxDto } from '../../box/dto/create-box.dto'

export function boxToBoxResponse(box: BoxDto): BoxResponseDto {
  return {
    box_id: box.id,
    name: box.name,
    status: publicBoxStatus(box.state),
    created_at: box.createdAt || new Date().toISOString(),
    updated_at: box.updatedAt || new Date().toISOString(),
    last_activity_at: box.lastActivityAt,
    image: box.image || '',
    cpus: box.cpu || 1,
    memory_mib: (box.memory || 1) * 1024,
    privileged: box.privileged,
    labels: box.labels || {},
    auto_stop: box.autoStop ?? DEFAULT_AUTO_STOP_SECONDS,
    auto_delete: box.autoDelete ?? AUTO_DELETE_DISABLED,
    auto_resume: box.autoResume ?? DEFAULT_AUTO_RESUME,
    // 0 is a real exit code, so this passes the value through. A falsy check
    // here would report every clean exit as "no exit code recorded".
    exit_code: box.exitCode,
    network: box.network,
  }
}

export function createBoxToCreateBox(dto: RestCreateBoxDto, target?: string): CreateBoxDto {
  const createDto = new CreateBoxDto()
  createDto.name = dto.name
  createDto.image = dto.image
  // `user` feeds two unrelated things. `osUser` is a Daytona-era provisioning
  // label — it only ever became a DAYTONA_SANDBOX_USER env var, was never the
  // container's process user, and today just keys the warm pool. `runAsUser`
  // is the OCI process.user override (docker `-u`) that actually reaches the
  // guest, and is left undefined when the caller did not ask for one so the
  // image's own USER directive stands.
  createDto.user = dto.user
  createDto.runAsUser = dto.user
  createDto.privileged = dto.advanced?.privileged ?? false
  createDto.workingDir = dto.working_dir
  createDto.entrypoint = dto.entrypoint
  createDto.cmd = dto.cmd
  createDto.env = dto.env
  createDto.cpu = dto.cpus
  createDto.memory = dto.memory_mib == null ? undefined : dto.memory_mib / 1024
  createDto.disk = dto.disk_size_gb
  createDto.target = target
  createDto.autoStop = dto.auto_stop
  createDto.autoDelete = dto.auto_delete
  createDto.autoResume = dto.auto_resume
  createDto.volumes = dto.volumes?.map((volume) => ({
    // `volumeId` is the internal DTO's name for what the volume service looks
    // up by id *or* by name (see VolumeService.validateVolumes). Filestore
    // volumes are Volume rows like any other and resolve the same way.
    volumeId: volume.managed_volume,
    mountPath: volume.guest_path,
  }))
  createDto.secrets = dto.secrets?.map((secret) =>
    'secret' in secret
      ? { secret: secret.secret }
      : {
          name: secret.name,
          value: secret.value,
          hosts: secret.hosts,
          placeholder: secret.placeholder,
          headers: secret.headers,
        },
  )
  // Only an explicit inbound.mode=enabled enables inbound access. Omitting
  // inbound (including the legacy flat network shape) leaves it disabled.
  createDto.inboundEnabled = dto.network?.inbound?.mode === 'enabled'
  if (dto.network) {
    const allowNet = dto.network.outbound?.allow_net?.map((entry) => entry.trim()).filter(Boolean)
    createDto.outboundEnabled = dto.network.outbound?.mode !== 'disabled'
    createDto.outboundAllowNet = allowNet?.length ? allowNet : undefined
  }
  return createDto
}