import { PLAY_RUNTIME_BACKENDS, type PlayRuntimeBackendId } from './backend'; export const PLAY_RUNTIME_ENVIRONMENTS = ['preview'] as const; export type PlayRuntimeEnvironment = (typeof PLAY_RUNTIME_ENVIRONMENTS)[number]; export type PlayRuntimeSelection = { environment: 'preview'; /** Caller-named isolation scope inside a remote runtime environment. */ namespace: string; /** Explicit managed-sandbox executor. Omission preserves Daytona compatibility. */ backend?: Extract; }; const PLAY_RUNTIME_NAMESPACE_PATTERN = /^[a-z][a-z0-9-]{0,30}$/; export function normalizePlayRuntimeNamespace(value: unknown): string | null { if (typeof value !== 'string') return null; const normalized = value.trim(); return PLAY_RUNTIME_NAMESPACE_PATTERN.test(normalized) ? normalized : null; } /** Validate the selector as one value so the SDK and server cannot drift. */ export function normalizePlayRuntimeSelection( value: unknown, ): PlayRuntimeSelection | null { if (!value || typeof value !== 'object' || Array.isArray(value)) return null; const record = value as Record; if ( Object.keys(record).some( (key) => key !== 'environment' && key !== 'namespace' && key !== 'backend', ) || record.environment !== 'preview' ) { return null; } const namespace = normalizePlayRuntimeNamespace(record.namespace); if (!namespace) return null; if (record.backend === undefined) { return { environment: 'preview', namespace }; } if ( record.backend !== PLAY_RUNTIME_BACKENDS.daytona && record.backend !== PLAY_RUNTIME_BACKENDS.modal ) { return null; } return { environment: 'preview', namespace, backend: record.backend }; } export function normalizePlayRuntimeEnvironment( value: unknown, ): PlayRuntimeEnvironment | null { return typeof value === 'string' && PLAY_RUNTIME_ENVIRONMENTS.includes(value as PlayRuntimeEnvironment) ? (value as PlayRuntimeEnvironment) : null; }