import { initializeAllContainers, registerContainers } from '@bytelyst/cosmos'; import type { ContainerConfig } from '@bytelyst/cosmos'; import { config } from './config.js'; const CONTAINER_DEFS: Record = { products: { partitionKeyPath: '/id' }, users: { partitionKeyPath: '/id' }, settings: { partitionKeyPath: '/userId' }, devices: { partitionKeyPath: '/userId' }, notification_prefs: { partitionKeyPath: '/userId' }, audit_log: { partitionKeyPath: '/category', defaultTtl: 90 * 86400 }, feature_flags: { partitionKeyPath: '/id' }, // Growth modules invitation_codes: { partitionKeyPath: '/id' }, referrals: { partitionKeyPath: '/id' }, // Billing modules subscriptions: { partitionKeyPath: '/userId' }, payments: { partitionKeyPath: '/userId' }, licenses: { partitionKeyPath: '/id' }, plans: { partitionKeyPath: '/id' }, usage_daily: { partitionKeyPath: '/userId' }, // API tokens api_tokens: { partitionKeyPath: '/id' }, // Tracker modules tracker_items: { partitionKeyPath: '/id' }, comments: { partitionKeyPath: '/itemId' }, votes: { partitionKeyPath: '/itemId' }, // Themes themes: { partitionKeyPath: '/id' }, // Waitlist (pre-launch signups) waitlist: { partitionKeyPath: '/email' }, // Sessions (refresh token rotation + device tracking) sessions: { partitionKeyPath: '/userId', defaultTtl: 30 * 86400 }, // Email/push delivery log delivery_log: { partitionKeyPath: '/pk', defaultTtl: 90 * 86400 }, // Status page incidents incidents: { partitionKeyPath: '/productId' }, // Password reset + email verification password_reset_tokens: { partitionKeyPath: '/productId', defaultTtl: 86400 }, email_verifications: { partitionKeyPath: '/productId', defaultTtl: 7 * 86400 }, // IP allow/deny rules ip_rules: { partitionKeyPath: '/productId' }, // Data exports export_jobs: { partitionKeyPath: '/productId', defaultTtl: 30 * 86400 }, // Maintenance windows maintenance_windows: { partitionKeyPath: '/productId' }, // Scheduled jobs job_definitions: { partitionKeyPath: '/productId' }, job_runs: { partitionKeyPath: '/productId:jobName' }, // Telemetry (client diagnostics — see docs/WINDSURF/CLIENT_TELEMETRY_DESIGN.md) telemetry_events: { partitionKeyPath: '/pk', defaultTtl: 30 * 86400 }, telemetry_error_clusters: { partitionKeyPath: '/pk', defaultTtl: 90 * 86400 }, telemetry_collection_policies: { partitionKeyPath: '/productId' }, // P2 — Product Intelligence experiments: { partitionKeyPath: '/id' }, experiment_assignments: { partitionKeyPath: '/experimentId' }, analytics_rollups: { partitionKeyPath: '/productId' }, feedback: { partitionKeyPath: '/productId' }, impersonation_sessions: { partitionKeyPath: '/productId', defaultTtl: 90 * 86400 }, changelog: { partitionKeyPath: '/productId' }, }; export async function initCosmosIfNeeded(): Promise { registerContainers(CONTAINER_DEFS); const shouldInit = config.NODE_ENV !== 'production' || process.env.COSMOS_AUTO_INIT === 'true'; if (!shouldInit) return; try { await initializeAllContainers(); process.stdout.write('[platform-service] Cosmos containers ensured\n'); } catch (err) { const msg = err instanceof Error ? err.message : String(err); process.stderr.write(`[platform-service] Cosmos init failed: ${msg}\n`); } }