/** * Core presentation types for workflow orchestration * * This file defines the foundational types for presentation workflow * orchestration, providing a robust type system that extends the * existing backup-workflows infrastructure to support presentation * lifecycle management. */ import { z } from 'zod'; // ============================================================================ // Base Presentation Types // ============================================================================ /** * Presentation lifecycle states */ export enum PresentationState { DRAFT = 'draft', SCHEDULED = 'scheduled', STARTING = 'starting', RUNNING = 'running', PAUSED = 'paused', STOPPING = 'stopping', COMPLETED = 'completed', FAILED = 'failed', CANCELLED = 'cancelled', CLEANING_UP = 'cleaning_up' } /** * Presentation workflow event types for triggers */ export enum PresentationEventType { PRESENTATION_CREATED = 'presentation.created', PRESENTATION_SCHEDULED = 'presentation.scheduled', PRESENTATION_STARTED = 'presentation.started', PRESENTATION_PAUSED = 'presentation.paused', PRESENTATION_RESUMED = 'presentation.resumed', PRESENTATION_STOPPED = 'presentation.stopped', PRESENTATION_COMPLETED = 'presentation.completed', PRESENTATION_FAILED = 'presentation.failed', PRESENTATION_CANCELLED = 'presentation.cancelled', RESOURCE_ALLOCATED = 'resource.allocated', RESOURCE_RELEASED = 'resource.released', CLEANUP_REQUESTED = 'cleanup.requested', CLEANUP_COMPLETED = 'cleanup.completed' } /** * Priority levels for workflow execution */ export enum WorkflowPriority { LOW = 1, NORMAL = 5, HIGH = 8, CRITICAL = 10 } // ============================================================================ // Core Data Structures // ============================================================================ /** * Presentation configuration schema */ export const PresentationConfigSchema = z.object({ id: z.string().uuid(), title: z.string().min(1).max(255), description: z.string().optional(), ownerId: z.string().uuid(), templateId: z.string().uuid().optional(), scheduledAt: z.string().datetime().optional(), duration: z.number().positive().optional(), // in minutes maxAttendees: z.number().positive().optional(), isPublic: z.boolean().default(false), settings: z.record(z.any()).default({}), metadata: z.record(z.string()).default({}) }); export type PresentationConfig = z.infer; /** * Presentation resource requirements */ export const ResourceRequirementsSchema = z.object({ cpu: z.number().positive().default(1), // CPU cores memory: z.number().positive().default(1024), // MB storage: z.number().positive().default(1024), // MB bandwidth: z.number().positive().default(100), // Mbps gpu: z.boolean().default(false), specialized: z.array(z.string()).default([]) // Special requirements }); export type ResourceRequirements = z.infer; /** * Workflow execution context */ export const WorkflowContextSchema = z.object({ presentationId: z.string().uuid(), userId: z.string().uuid(), sessionId: z.string().uuid(), workflowId: z.string(), executionId: z.string().uuid(), priority: z.nativeEnum(WorkflowPriority).default(WorkflowPriority.NORMAL), timeout: z.number().positive().default(3600), // seconds retryPolicy: z.object({ maxAttempts: z.number().positive().default(3), backoffStrategy: z.enum(['linear', 'exponential']).default('exponential'), baseDelay: z.number().positive().default(1000) // milliseconds }).default({}), environment: z.record(z.string()).default({}) }); export type WorkflowContext = z.infer; // ============================================================================ // Workflow Operation Types // ============================================================================ /** * Base workflow operation interface */ export interface WorkflowOperation { id: string; type: string; name: string; description?: string; inputs: Record; outputs?: Record; metadata?: Record; } /** * Presentation lifecycle operations */ export interface PresentationStartOperation extends WorkflowOperation { type: 'presentation.start'; inputs: { presentationConfig: PresentationConfig; resourceRequirements: ResourceRequirements; context: WorkflowContext; }; outputs?: { sessionId: string; resourceAllocation: Record; startedAt: string; }; } export interface PresentationStopOperation extends WorkflowOperation { type: 'presentation.stop'; inputs: { presentationId: string; sessionId: string; reason?: string; graceful: boolean; context: WorkflowContext; }; outputs?: { stoppedAt: string; finalState: PresentationState; resourcesReleased: boolean; }; } export interface ResourceCleanupOperation extends WorkflowOperation { type: 'resource.cleanup'; inputs: { presentationId: string; sessionId: string; resourceIds: string[]; context: WorkflowContext; }; outputs?: { cleanedResources: string[]; cleanupCompletedAt: string; }; } // ============================================================================ // Workflow Definition Types // ============================================================================ /** * Workflow step definition */ export interface WorkflowStep { id: string; name: string; type: string; operation: WorkflowOperation; dependencies: string[]; condition?: string; // Expression to evaluate timeout?: number; retryPolicy?: { maxAttempts: number; backoffStrategy: 'linear' | 'exponential'; baseDelay: number; }; onSuccess?: WorkflowStep[]; onFailure?: WorkflowStep[]; onTimeout?: WorkflowStep[]; } /** * Complete workflow definition */ export interface PresentationWorkflow { id: string; name: string; description?: string; version: string; trigger: { events: PresentationEventType[]; condition?: string; }; steps: WorkflowStep[]; outputs?: Record; metadata?: Record; createdAt: string; updatedAt: string; } // ============================================================================ // Execution State Types // ============================================================================ /** * Workflow execution state */ export interface WorkflowExecution { id: string; workflowId: string; presentationId: string; state: 'pending' | 'running' | 'completed' | 'failed' | 'cancelled'; currentStep?: string; context: WorkflowContext; startedAt?: string; completedAt?: string; error?: { message: string; code?: string; stack?: string; step?: string; }; outputs?: Record; metadata: Record; } /** * Step execution result */ export interface StepExecutionResult { stepId: string; status: 'success' | 'failure' | 'timeout' | 'skipped'; outputs?: Record; error?: { message: string; code?: string; stack?: string; }; duration: number; // milliseconds executedAt: string; metadata?: Record; } // ============================================================================ // Registry and Configuration Types // ============================================================================ /** * Workflow registry entry */ export interface WorkflowRegistryEntry { workflow: PresentationWorkflow; isActive: boolean; registeredAt: string; lastExecuted?: string; executionCount: number; averageExecutionTime?: number; // milliseconds successRate?: number; // 0-1 } /** * Orchestrator configuration */ export const OrchestratorConfigSchema = z.object({ maxConcurrentWorkflows: z.number().positive().default(10), defaultTimeout: z.number().positive().default(3600), // seconds enableRetries: z.boolean().default(true), retryPolicy: z.object({ maxAttempts: z.number().positive().default(3), backoffStrategy: z.enum(['linear', 'exponential']).default('exponential'), baseDelay: z.number().positive().default(1000) }).default({}), monitoring: z.object({ enableMetrics: z.boolean().default(true), enableTracing: z.boolean().default(true), enableLogging: z.boolean().default(true), metricsInterval: z.number().positive().default(60000) // milliseconds }).default({}), storage: z.object({ provider: z.enum(['memory', 'redis', 'postgresql']).default('memory'), connectionString: z.string().optional(), retentionDays: z.number().positive().default(30) }).default({}) }); export type OrchestratorConfig = z.infer; // ============================================================================ // Event and Messaging Types // ============================================================================ /** * Workflow event payload */ export interface WorkflowEventPayload { eventType: PresentationEventType; presentationId: string; sessionId?: string; userId?: string; data: Record; timestamp: string; metadata?: Record; } /** * Workflow trigger configuration */ export interface WorkflowTrigger { id: string; workflowId: string; events: PresentationEventType[]; condition?: string; // JavaScript expression isActive: boolean; priority: WorkflowPriority; metadata?: Record; } // ============================================================================ // Integration Types // ============================================================================ /** * Supabase integration context */ export interface SupabaseWorkflowContext { connectionId: string; userId?: string; tablePermissions: Record; // table -> operations rls: { enabled: boolean; policies: string[]; }; transaction?: { id: string; isolationLevel: 'read_uncommitted' | 'read_committed' | 'repeatable_read' | 'serializable'; }; } /** * Backup workflow compatibility types */ export interface BackupWorkflowContext { backupId?: string; backupType?: 'full' | 'incremental'; retentionDays?: number; storageProvider?: string; encryptionEnabled?: boolean; } // ============================================================================ // Type Guards and Utilities // ============================================================================ /** * Type guard for presentation operations */ export function isPresentationOperation(operation: WorkflowOperation): operation is PresentationStartOperation | PresentationStopOperation | ResourceCleanupOperation { return operation.type.startsWith('presentation.') || operation.type.startsWith('resource.'); } /** * Type guard for workflow execution */ export function isWorkflowExecution(obj: any): obj is WorkflowExecution { return typeof obj === 'object' && typeof obj.id === 'string' && typeof obj.workflowId === 'string' && typeof obj.presentationId === 'string' && ['pending', 'running', 'completed', 'failed', 'cancelled'].includes(obj.state); } /** * Default configuration factory */ export function createDefaultOrchestratorConfig(): OrchestratorConfig { return OrchestratorConfigSchema.parse({}); } export function createDefaultWorkflowContext(presentationId: string, userId: string): WorkflowContext { return WorkflowContextSchema.parse({ presentationId, userId, sessionId: crypto.randomUUID(), workflowId: '', executionId: crypto.randomUUID() }); }