Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

3.3 KB
Raw Blame
import * as chalk from 'chalk'
import * as e2b from 'e2b'
import * as highlight from 'cli-highlight'
import * as boxen from 'boxen'

import { cwdRelative } from './filesystem'
import { UserConfig } from '../user'

export const primaryColor = '#FFB766'

export function asFormattedConfig(config: UserConfig) {
  const email = asBold(config.identity.email)
  const project = config.projectName
    ? asBold(config.projectName)
    : asRed('Log out and log in to get project name')
  const projectId = asBold(config.projectId)
  return `You are logged in as ${email},\nSelected project: ${project} (${projectId})`
}

export function asFormattedTeam(
  team: e2b.components['schemas']['Team'],
  selected: string
) {
  const name = asBold(team.name)
  const id = asBold(team.teamID)
  const isSelected =
    team.teamID == selected ? asPrimary(' (currently selected project)') : ''
  return `${name} (${id})${isSelected}`
}

export type SandboxTemplateRef = Pick<
  e2b.components['schemas']['Template'],
  'templateID'
> & {
  aliases?: e2b.components['schemas']['Template']['aliases']
}

export function asFormattedSandboxTemplate(template: SandboxTemplateRef) {
  const aliases = listAliases(template.aliases)

  const name = aliases ? asBold(aliases) : ''
  const id = `${template.templateID} `

  return `${id}${name}`.trim()
}

export function asRed(text: string) {
  return chalk.default.redBright(text)
}

export function asFormattedError(text: string | undefined, err?: any) {
  return chalk.default.redBright(
    `${text ? `${text} \n` : ''}${err ? err.stack : ''}\n`
  )
}

export function asBold(content: string) {
  return chalk.default.bold(content)
}

export function asPrimary(content: string) {
  return chalk.default.hex(primaryColor)(content)
}

export function asTimestamp(content: string) {
  return chalk.default.blue(content)
}

export function asLocal(pathInLocal?: string) {
  return chalk.default.blue(pathInLocal)
}

export function asLocalRelative(absolutePathInLocal?: string) {
  if (!absolutePathInLocal) return ''
  return asLocal('./' + cwdRelative(absolutePathInLocal))
}

export function withUnderline(content: string) {
  return chalk.default.underline(content)
}

export function listAliases(aliases: string[] | undefined) {
  if (!aliases) return undefined
  return aliases.join(', ')
}

export function asTypescript(code: string) {
  return highlight.default(code, {
    language: 'typescript',
    ignoreIllegals: true,
  })
}

export function asPython(code: string) {
  return highlight.default(code, { language: 'python', ignoreIllegals: true })
}

export const borderStyle = {
  topLeft: '',
  topRight: '',
  bottomLeft: '',
  bottomRight: '',
  top: '',
  bottom: '',
  left: '',
  right: '',
} as const

const horizontalPadding = 2
const verticalPadding = 1

export function withDelimiter(
  content: string,
  title: string,
  isLast?: boolean
) {
  return boxen.default(content, {
    borderStyle: {
      ...borderStyle,
      top: '─',
      bottom: isLast ? '─' : '',
    },
    titleAlignment: 'center',
    float: 'left',
    title: title ? asBold(title) : undefined,
    margin: {
      top: 0,
      bottom: 0,
      left: 1,
      right: 0,
    },
    fullscreen: (w) => [w, 0],
    padding: {
      bottom: isLast ? verticalPadding : 0,
      left: horizontalPadding,
      right: horizontalPadding,
      top: verticalPadding,
    },
  })
}