Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

7.8 KB
Raw Blame
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

export function generateClientMessageId(): string {
  return globalThis.crypto?.randomUUID?.()
    ?? `msg-${Date.now()}-${Math.random().toString(36).slice(2)}`
}

export function getApiUrl(): string {
  // Client-side: use environment variable or fallback to relative path
  const apiUrl = process.env.NEXT_PUBLIC_API_URL || ''
  return apiUrl
}

const ASCII_CONTROL_OR_DEL = /[\u0000-\u001F\u007F]/
const EXPLICIT_SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*:/
const HTTP_AUTHORITY = /^https?:\/\/[^/?#\\]/i

function hasUnsafeUrlText(value: string): boolean {
  return (
    value.length === 0
    || value.trim() !== value
    || ASCII_CONTROL_OR_DEL.test(value)
    || value.includes("\\")
  )
}

function isHttpUrlWithAuthority(value: string): boolean {
  if (!HTTP_AUTHORITY.test(value)) return false
  try {
    const parsed = new URL(value)
    return (
      (parsed.protocol === "http:" || parsed.protocol === "https:")
      && parsed.hostname.length > 0
    )
  } catch {
    return false
  }
}

function normalizeRelativeLogoBase(apiBase: string): string | null {
  if (
    apiBase.trim() !== apiBase
    || ASCII_CONTROL_OR_DEL.test(apiBase)
    || apiBase.includes("\\")
    || apiBase.includes("?")
    || apiBase.includes("#")
  ) return null

  if (apiBase === "" || /^\/+$/u.test(apiBase)) return ""

  if (HTTP_AUTHORITY.test(apiBase)) {
    return isHttpUrlWithAuthority(apiBase)
      ? apiBase.replace(/\/+$/u, "")
      : null
  }

  if (
    EXPLICIT_SCHEME.test(apiBase)
    || apiBase.startsWith("//")
    || !/^\/[^/]/u.test(apiBase)
  ) return null

  return apiBase.replace(/\/+$/u, "")
}

/**
 * Resolves an Agent logo API value to an image URL without accepting
 * protocol-relative, scheme-like, or malformed URL forms.
 */
export function resolveAgentLogoUrl(value: unknown, apiBase: string): string | null {
  if (typeof value !== "string" || hasUnsafeUrlText(value)) return null

  if (/^https?:/i.test(value)) {
    return isHttpUrlWithAuthority(value) ? value : null
  }

  if (EXPLICIT_SCHEME.test(value) || value.startsWith("//")) return null

  const relativePath = value.startsWith("/") ? value.slice(1) : value
  if (relativePath.length === 0 || /^\/+$/u.test(relativePath)) return null

  const normalizedBase = normalizeRelativeLogoBase(apiBase)
  if (normalizedBase === null) return null
  return `${normalizedBase}/${relativePath}`
}

export function getFilePublicPreviewUrl(fileId: string, apiUrl = getApiUrl()): string {
  return `${apiUrl}/api/files/public/preview/${encodeURIComponent(fileId)}`
}

export function getFilePublicDownloadUrl(fileId: string, apiUrl = getApiUrl()): string {
  return `${apiUrl}/api/files/public/download/${encodeURIComponent(fileId)}`
}

export function getFileRelativePreviewUrl(
  fileId: string,
  relativePath: string,
  apiUrl = getApiUrl(),
): string {
  const baseUrl = new URL(getFilePublicPreviewUrl(fileId, apiUrl), typeof window !== 'undefined' ? window.location.origin : 'http://localhost')
  baseUrl.searchParams.set('relative_path', relativePath)
  if (apiUrl) {
    return baseUrl.toString()
  }
  return `${baseUrl.pathname}${baseUrl.search}${baseUrl.hash}`
}

export function getUploadApiUrl(): string {
  // Direct-to-backend URL for file uploads. The Next.js dev-server proxy
  // (rewrites in next.config.mjs) buffers entire multipart bodies into Node
  // memory before forwarding; bodies over ~5MB trigger an internal 30s
  // timeout and the client sees a plain-text "Internal Server Error" 500.
  // To avoid that, point uploads directly at the FastAPI backend.
  //
  // - In dev, set NEXT_PUBLIC_UPLOAD_API_URL=http://localhost:8000 in
  //   frontend/.env.local. FastAPI's CORS middleware already allows "*".
  // - In production (nginx + same-origin), leave it unset and uploads will
  //   reuse NEXT_PUBLIC_API_URL (which is empty by default → same-origin).
  return (
    process.env.NEXT_PUBLIC_UPLOAD_API_URL
    || process.env.NEXT_PUBLIC_API_URL
    || ''
  )
}

export function getAuthHeaders(token: string | null): Record<string, string> {
  if (!token) return {}
  return {
    'Authorization': `Bearer ${token}`
  }
}

export function getWsUrl(): string {
  // 1. Use explicit env var if set (production/staging)
  if (process.env.NEXT_PUBLIC_WS_URL) {
    return process.env.NEXT_PUBLIC_WS_URL
  }

  // 2. Auto-construct from current location (development/same-domain)
  if (typeof window !== 'undefined') {
    const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
    return `${protocol}//${window.location.host}`
  }

  // 3. Fallback for SSR (shouldn't happen for WS, but safe)
  return ''
}

export async function fetchWithAuth(url: string, token: string | null, options: RequestInit = {}): Promise<Response> {
  const headers = {
    ...options.headers,
    ...getAuthHeaders(token)
  }

  return fetch(url, {
    ...options,
    headers
  })
}

export function formatDate(date: Date | string): string {
  const d = new Date(date)
  return d.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

export function formatFileSize(bytes: number): string {
  if (bytes === 0) return '0 Bytes'

  const k = 1024
  const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))

  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

export function debounce<T extends (...args: any[]) => any>(
  func: T,
  wait: number
): (...args: Parameters<T>) => void {
  let timeout: NodeJS.Timeout | null = null

  return (...args: Parameters<T>) => {
    if (timeout) {
      clearTimeout(timeout)
    }

    timeout = setTimeout(() => {
      func(...args)
    }, wait)
  }
}

export function throttle<T extends (...args: any[]) => any>(
  func: T,
  limit: number
): (...args: Parameters<T>) => void {
  let inThrottle: boolean = false

  return (...args: Parameters<T>) => {
    if (!inThrottle) {
      func(...args)
      inThrottle = true
      setTimeout(() => {
        inThrottle = false
      }, limit)
    }
  }
}

export function generateId(): string {
  return Math.random().toString(36).substr(2, 9)
}

export function isValidUrl(url: string): boolean {
  try {
    new URL(url)
    return true
  } catch {
    return false
  }
}

export function truncateText(text: string, maxLength: number): string {
  if (text.length <= maxLength) return text
  return text.slice(0, maxLength) + '...'
}

export function sanitizeFileName(fileName: string): string {
  return fileName.replace(/[^\w\s.-]/g, '').replace(/\s+/g, '_')
}

export function isHtmlFile(fileName: string): boolean {
  if (!fileName) return false
  const lowerName = fileName.toLowerCase()
  return lowerName.endsWith('.html') || lowerName.endsWith('.htm')
}

export function isMarkdownFile(fileName: string): boolean {
  if (!fileName) return false
  return fileName.toLowerCase().endsWith('.md')
}

export function isCsvFile(fileName: string): boolean {
  if (!fileName) return false
  return fileName.toLowerCase().endsWith('.csv')
}

export function isImageFile(fileName: string): boolean {
  if (!fileName) return false
  return /\.(jpg|jpeg|png|gif|webp|svg)$/i.test(fileName)
}

export function shouldAutoOpenTaskPreview(fileName: string): boolean {
  if (!fileName) return false

  const lowerName = fileName.toLowerCase()
  if (isImageFile(lowerName)) return false

  return (
    lowerName.endsWith('.pdf')
    || lowerName.endsWith('.ppt')
    || lowerName.endsWith('.pptx')
    || lowerName.endsWith('.docx')
    || lowerName.endsWith('.xlsx')
    || isCsvFile(lowerName)
    || isHtmlFile(lowerName)
  )
}

export function isToggleableFile(fileName: string): boolean {
  return isHtmlFile(fileName) || isMarkdownFile(fileName) || isCsvFile(fileName)
}