Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

3.1 KB
Raw Blame
import type {
  AssetLibraryError,
  AssetLibraryListResult,
  AssetLibraryOpenRequest,
  AssetLibraryOpenResult,
  AssetLibraryReadRequest,
  AssetLibraryReadResult,
} from '../../shared/types/assetLibrary'
import { projectAssetLibraryEntry, type ProjectedAssetLibraryEntry } from './assetLibraryProjection'

export interface AssetLibraryPreloadApi {
  list: () => Promise<AssetLibraryListResult>
  read: (request: AssetLibraryReadRequest) => Promise<AssetLibraryReadResult>
  open: (request: AssetLibraryOpenRequest) => Promise<AssetLibraryOpenResult>
}

export type ProjectedAssetLibraryListResult =
  | { success: true, entries: ProjectedAssetLibraryEntry[] }
  | { success: false, error: AssetLibraryError }

export type ProjectedAssetLibraryReadResult =
  | { success: true, entry: ProjectedAssetLibraryEntry, preview: Extract<AssetLibraryReadResult, { success: true }>['preview'] }
  | { success: false, error: AssetLibraryError }

export type ProjectedAssetLibraryOpenResult =
  | { success: true, entry: ProjectedAssetLibraryEntry }
  | { success: false, error: AssetLibraryError }

function unsafeError(message = 'Workspace path must stay under Workflows/ or Exports/.'): AssetLibraryError {
  return { code: 'unsafe-path', message }
}

function validateWorkspacePath(workspacePath: string): AssetLibraryError | null {
  const trimmed = workspacePath.trim()
  if (!trimmed || trimmed.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(trimmed) || trimmed.startsWith('\\\\')) return unsafeError()
  if (/%2e|%2f|%5c/i.test(trimmed) || trimmed.split(/[\\/]+/).includes('..')) return unsafeError()
  const normalized = trimmed.replace(/\\/g, '/')
  if (!normalized.startsWith('Workflows/') && !normalized.startsWith('Exports/')) return unsafeError()
  return null
}

export function createAssetLibraryService(api: AssetLibraryPreloadApi) {
  return {
    async list(): Promise<ProjectedAssetLibraryListResult> {
      const result = await api.list()
      if (!result.success) return result
      return { success: true, entries: result.entries.map(projectAssetLibraryEntry) }
    },
    async read(request: AssetLibraryReadRequest): Promise<ProjectedAssetLibraryReadResult> {
      const invalid = validateWorkspacePath(request.workspacePath) ?? (request.sourceWorkspacePath ? validateWorkspacePath(request.sourceWorkspacePath) : null)
      if (invalid) return { success: false, error: invalid }
      const result = await api.read(request)
      if (!result.success) return result
      return { success: true, entry: projectAssetLibraryEntry(result.entry), preview: result.preview }
    },
    async open(request: AssetLibraryOpenRequest): Promise<ProjectedAssetLibraryOpenResult> {
      const invalid = validateWorkspacePath(request.workspacePath) ?? (request.sourceWorkspacePath ? validateWorkspacePath(request.sourceWorkspacePath) : null)
      if (invalid) return { success: false, error: invalid }
      const result = await api.open(request)
      if (!result.success) return result
      return { success: true, entry: projectAssetLibraryEntry(result.entry) }
    },
  }
}

export function getDefaultAssetLibraryService() {
  return createAssetLibraryService(window.electron.workspace.library)
}