UNPKG

@growthub/compiler-core

Version:

Core decomposition engine and orchestration logic for Growthub Marketing OS

1 lines 11.2 kB
{"version":3,"sources":["../src/orchestration.ts"],"sourcesContent":["/**\n * @growthub/compiler-core/orchestration\n * Core orchestration patterns for agent coordination and execution\n */\n\nimport { z } from 'zod'\nimport type { DecompositionEvent, DecompositionStep, BrandContext } from './decomposition'\n\n// Orchestration Request Schema\nexport const OrchestrationRequestSchema = z.object({\n threadId: z.string().uuid(),\n userId: z.string().min(1),\n agentType: z.enum(['CONTENT_GENERATION_AGENT', 'TEXT_ANALYSIS_AGENT']),\n prompt: z.string().min(1),\n brandContext: z.object({\n brand_name: z.string(),\n colors: z.array(z.string()).default([]),\n messaging: z.string().nullable(),\n referenceImages: z.array(z.object({\n url: z.string(),\n type: z.string(), \n description: z.string()\n })).default([])\n }),\n executionPriority: z.enum(['low', 'normal', 'high']).default('normal'),\n maxRetries: z.number().int().min(0).max(3).default(1),\n metadata: z.record(z.any()).optional()\n})\n\nexport type OrchestrationRequest = z.infer<typeof OrchestrationRequestSchema>\n\n// Orchestration Result Types\nexport interface OrchestrationResult {\n success: boolean\n threadId: string\n userId: string\n agentType: string\n orchestrationId: string\n steps: OrchestrationStep[]\n metadata: {\n startedAt: string\n completedAt?: string\n duration?: number\n status: 'pending' | 'running' | 'completed' | 'failed'\n retryCount: number\n }\n error?: {\n message: string\n code: string\n details?: any\n }\n}\n\nexport interface OrchestrationStep {\n stepId: string\n stepName: string\n stepType: 'decomposition' | 'analysis' | 'execution' | 'coordination' | 'completion'\n status: 'pending' | 'running' | 'completed' | 'failed' | 'skipped'\n progress: number\n result?: any\n metadata?: {\n startedAt?: string\n completedAt?: string\n duration?: number\n retryCount?: number\n [key: string]: any\n }\n dependencies?: string[]\n error?: {\n message: string\n code: string\n retry?: boolean\n }\n}\n\n// CSI (Current Step Information) Types\nexport interface CSI {\n completedSteps: string[]\n currentProgress: number\n totalSteps: number\n currentStep: string\n metadata?: Record<string, any>\n}\n\n/**\n * Create orchestration step with metadata\n */\nexport function createOrchestrationStep(\n stepId: string,\n stepName: string,\n stepType: OrchestrationStep['stepType'],\n dependencies: string[] = []\n): OrchestrationStep {\n return {\n stepId,\n stepName,\n stepType,\n status: 'pending',\n progress: 0,\n dependencies,\n metadata: {\n startedAt: new Date().toISOString(),\n retryCount: 0\n }\n }\n}\n\n/**\n * Update orchestration step status and progress\n */\nexport function updateOrchestrationStep(\n step: OrchestrationStep,\n status: OrchestrationStep['status'],\n progress: number,\n result?: any,\n metadata?: Record<string, any>\n): OrchestrationStep {\n const updatedStep = {\n ...step,\n status,\n progress,\n result,\n metadata: {\n ...step.metadata,\n ...metadata,\n }\n }\n\n if (status === 'completed' || status === 'failed') {\n updatedStep.metadata!.completedAt = new Date().toISOString()\n \n if (step.metadata?.startedAt) {\n const duration = Date.now() - new Date(step.metadata.startedAt).getTime()\n updatedStep.metadata!.duration = duration\n }\n }\n\n return updatedStep\n}\n\n/**\n * Check if step dependencies are satisfied\n */\nexport function areDependenciesSatisfied(\n step: OrchestrationStep,\n allSteps: OrchestrationStep[]\n): boolean {\n if (!step.dependencies || step.dependencies.length === 0) {\n return true\n }\n\n return step.dependencies.every(depId => {\n const depStep = allSteps.find(s => s.stepId === depId)\n return depStep?.status === 'completed'\n })\n}\n\n/**\n * Get next executable steps based on dependencies\n */\nexport function getNextExecutableSteps(steps: OrchestrationStep[]): OrchestrationStep[] {\n return steps.filter(step => \n step.status === 'pending' && areDependenciesSatisfied(step, steps)\n )\n}\n\n/**\n * Calculate overall orchestration progress\n */\nexport function calculateOrchestrationProgress(steps: OrchestrationStep[]): number {\n if (steps.length === 0) return 0\n\n const totalProgress = steps.reduce((sum, step) => sum + step.progress, 0)\n return Math.round(totalProgress / steps.length)\n}\n\n/**\n * Check if orchestration is complete\n */\nexport function isOrchestrationComplete(steps: OrchestrationStep[]): boolean {\n return steps.length > 0 && steps.every(step => \n step.status === 'completed' || step.status === 'failed' || step.status === 'skipped'\n )\n}\n\n/**\n * Get orchestration status based on steps\n */\nexport function getOrchestrationStatus(steps: OrchestrationStep[]): 'pending' | 'running' | 'completed' | 'failed' {\n if (steps.length === 0) return 'pending'\n \n const hasRunning = steps.some(step => step.status === 'running')\n if (hasRunning) return 'running'\n \n const hasFailed = steps.some(step => step.status === 'failed')\n if (hasFailed) return 'failed'\n \n const allComplete = steps.every(step => \n step.status === 'completed' || step.status === 'skipped'\n )\n if (allComplete) return 'completed'\n \n return 'pending'\n}\n\n/**\n * Create CSI from orchestration steps\n */\nexport function createCSIFromSteps(steps: OrchestrationStep[]): CSI {\n const completedSteps = steps\n .filter(step => step.status === 'completed')\n .map(step => step.stepName)\n \n const currentStep = steps.find(step => step.status === 'running')\n const progress = calculateOrchestrationProgress(steps)\n\n return {\n completedSteps,\n currentProgress: progress,\n totalSteps: steps.length,\n currentStep: currentStep?.stepName || 'pending',\n metadata: {\n timestamp: new Date().toISOString(),\n stepsStatus: steps.map(step => ({\n stepName: step.stepName,\n status: step.status,\n progress: step.progress\n }))\n }\n }\n}\n\n/**\n * Validate orchestration request\n */\nexport function validateOrchestrationRequest(data: unknown): OrchestrationRequest {\n return OrchestrationRequestSchema.parse(data)\n}\n\n/**\n * Generate unique orchestration ID\n */\nexport function generateOrchestrationId(userId: string, threadId: string): string {\n const timestamp = Date.now()\n return `orch_${userId.slice(0, 8)}_${threadId.slice(0, 8)}_${timestamp}`\n}\n\n/**\n * Create standard orchestration steps for content generation\n */\nexport function createContentGenerationSteps(): OrchestrationStep[] {\n return [\n createOrchestrationStep('intent_analysis', 'Intent Analysis', 'analysis'),\n createOrchestrationStep('brand_analysis', 'Brand Analysis', 'analysis', ['intent_analysis']),\n createOrchestrationStep('complexity_assessment', 'Complexity Assessment', 'analysis', ['intent_analysis', 'brand_analysis']),\n createOrchestrationStep('execution_planning', 'Execution Planning', 'coordination', ['complexity_assessment']),\n createOrchestrationStep('content_generation', 'Content Generation', 'execution', ['execution_planning']),\n createOrchestrationStep('finalization', 'Finalization', 'completion', ['content_generation'])\n ]\n}\n\n/**\n * Create error for orchestration step\n */\nexport function createOrchestrationError(\n message: string,\n code: string,\n retry: boolean = false,\n details?: any\n): OrchestrationStep['error'] {\n return {\n message,\n code,\n retry,\n ...(details && { details })\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,iBAAkB;AAIX,IAAM,6BAA6B,aAAE,OAAO;AAAA,EACjD,UAAU,aAAE,OAAO,EAAE,KAAK;AAAA,EAC1B,QAAQ,aAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EACxB,WAAW,aAAE,KAAK,CAAC,4BAA4B,qBAAqB,CAAC;AAAA,EACrE,QAAQ,aAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EACxB,cAAc,aAAE,OAAO;AAAA,IACrB,YAAY,aAAE,OAAO;AAAA,IACrB,QAAQ,aAAE,MAAM,aAAE,OAAO,CAAC,EAAE,QAAQ,CAAC,CAAC;AAAA,IACtC,WAAW,aAAE,OAAO,EAAE,SAAS;AAAA,IAC/B,iBAAiB,aAAE,MAAM,aAAE,OAAO;AAAA,MAChC,KAAK,aAAE,OAAO;AAAA,MACd,MAAM,aAAE,OAAO;AAAA,MACf,aAAa,aAAE,OAAO;AAAA,IACxB,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;AAAA,EAChB,CAAC;AAAA,EACD,mBAAmB,aAAE,KAAK,CAAC,OAAO,UAAU,MAAM,CAAC,EAAE,QAAQ,QAAQ;AAAA,EACrE,YAAY,aAAE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,IAAI,CAAC,EAAE,QAAQ,CAAC;AAAA,EACpD,UAAU,aAAE,OAAO,aAAE,IAAI,CAAC,EAAE,SAAS;AACvC,CAAC;AA4DM,SAAS,wBACd,QACA,UACA,UACA,eAAyB,CAAC,GACP;AACnB,SAAO;AAAA,IACL;AA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