@growthub/compiler-core
Version:
Core decomposition engine and orchestration logic for Growthub Marketing OS
1 lines • 9.59 kB
Source Map (JSON)
{"version":3,"sources":["../src/decomposition.ts"],"sourcesContent":["/**\n * @growthub/compiler-core/decomposition\n * Core decomposition engine patterns and utilities\n */\n\nimport { z } from 'zod'\nimport type { BrandKitSchema } from '@growthub/schemas'\n\n// 2025 Real-Time CSI Schema for Decomposition Events\nexport const DecompositionEventSchema = 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 context: z.object({\n brandKit: z.object({\n id: z.string(),\n brand_name: z.string(),\n colors: z.object({\n primary: z.string().optional(),\n secondary: z.string().optional(),\n accent: z.string().optional(),\n neutral: z.string().optional(),\n }).optional(),\n messaging: z.string().optional(),\n }),\n referenceImages: z.array(z.object({\n url: z.string(),\n type: z.string(),\n description: z.string()\n })).default([]),\n }),\n})\n\nexport type DecompositionEvent = z.infer<typeof DecompositionEventSchema>\n\n// OpenAI Structured Output Schema for Decomposition Validation\nexport const decompositionStructuredOutput = {\n type: \"json_schema\" as const,\n json_schema: {\n name: \"decomposition_event\",\n strict: true,\n schema: {\n type: \"object\",\n properties: {\n threadId: { type: \"string\", format: \"uuid\" },\n userId: { type: \"string\", minLength: 1 },\n agentType: { \n type: \"string\", \n enum: [\"CONTENT_GENERATION_AGENT\", \"TEXT_ANALYSIS_AGENT\"] \n },\n prompt: { type: \"string\", minLength: 1 },\n context: {\n type: \"object\",\n properties: {\n brandKit: {\n type: \"object\",\n properties: {\n id: { type: \"string\" },\n brand_name: { type: \"string\" },\n colors: {\n type: \"object\",\n properties: {\n primary: { type: \"string\" },\n secondary: { type: \"string\" },\n accent: { type: \"string\" },\n neutral: { type: \"string\" }\n },\n required: [\"primary\", \"secondary\", \"accent\", \"neutral\"],\n additionalProperties: false\n },\n messaging: { type: \"string\" }\n },\n required: [\"id\", \"brand_name\", \"colors\", \"messaging\"],\n additionalProperties: false\n },\n referenceImages: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n url: { type: \"string\" },\n type: { type: \"string\" },\n description: { type: \"string\" }\n },\n required: [\"url\", \"type\", \"description\"],\n additionalProperties: false\n }\n }\n },\n required: [\"brandKit\", \"referenceImages\"],\n additionalProperties: false\n }\n },\n required: [\"threadId\", \"userId\", \"agentType\", \"prompt\", \"context\"],\n additionalProperties: false\n }\n }\n}\n\n// Decomposition Result Types\nexport interface DecompositionResult {\n threadId: string\n userId: string\n agentType: 'CONTENT_GENERATION_AGENT' | 'TEXT_ANALYSIS_AGENT'\n steps: DecompositionStep[]\n metadata: {\n startedAt: string\n completedAt?: string\n duration?: number\n status: 'pending' | 'running' | 'completed' | 'failed'\n }\n}\n\nexport interface DecompositionStep {\n stepName: string\n stepType: 'analysis' | 'planning' | 'coordination' | 'execution'\n status: 'pending' | 'running' | 'completed' | 'failed'\n result?: any\n metadata?: Record<string, any>\n timestamp: string\n}\n\n// Brand Context Transformer\nexport interface BrandContext {\n brand_name: string\n colors: string[]\n messaging: string | null\n referenceImages: Array<{\n url: string\n type: string\n description: string\n }>\n}\n\n/**\n * Transform context from DecompositionEvent to BrandContext format\n */\nexport function transformBrandContext(context: DecompositionEvent['context']): BrandContext {\n return {\n brand_name: context.brandKit.brand_name,\n colors: context.brandKit.colors \n ? Object.values(context.brandKit.colors).filter(Boolean) as string[]\n : [],\n messaging: context.brandKit.messaging || null,\n referenceImages: context.referenceImages,\n }\n}\n\n/**\n * Validate decomposition event data with Zod\n */\nexport function validateDecompositionEvent(data: unknown): DecompositionEvent {\n return DecompositionEventSchema.parse(data)\n}\n\n/**\n * Create decomposition step metadata\n */\nexport function createDecompositionStep(\n stepName: string,\n stepType: DecompositionStep['stepType'],\n metadata?: Record<string, any>\n): DecompositionStep {\n return {\n stepName,\n stepType,\n status: 'pending',\n metadata,\n timestamp: new Date().toISOString()\n }\n}\n\n/**\n * Update decomposition step status\n */\nexport function updateDecompositionStep(\n step: DecompositionStep,\n status: DecompositionStep['status'],\n result?: any,\n metadata?: Record<string, any>\n): DecompositionStep {\n return {\n ...step,\n status,\n result,\n metadata: { ...step.metadata, ...metadata },\n timestamp: new Date().toISOString()\n }\n}\n\n/**\n * Calculate decomposition progress percentage\n */\nexport function calculateDecompositionProgress(steps: DecompositionStep[]): number {\n if (steps.length === 0) return 0\n \n const completedSteps = steps.filter(step => step.status === 'completed').length\n return Math.round((completedSteps / steps.length) * 100)\n}\n\n/**\n * Check if decomposition is complete\n */\nexport function isDecompositionComplete(steps: DecompositionStep[]): boolean {\n return steps.length > 0 && steps.every(step => \n step.status === 'completed' || step.status === 'failed'\n )\n}\n\n/**\n * Get current decomposition step\n */\nexport function getCurrentDecompositionStep(steps: DecompositionStep[]): DecompositionStep | null {\n return steps.find(step => step.status === 'running') || \n steps.find(step => step.status === 'pending') ||\n null\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,iBAAkB;AAIX,IAAM,2BAA2B,aAAE,OAAO;AAAA,EAC/C,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,SAAS,aAAE,OAAO;AAAA,IAChB,UAAU,aAAE,OAAO;AAAA,MACjB,IAAI,aAAE,OAAO;AAAA,MACb,YAAY,aAAE,OAAO;AAAA,MACrB,QAAQ,aAAE,OAAO;AAAA,QACf,SAAS,aAAE,OAAO,EAAE,SAAS;AAAA,QAC7B,WAAW,aAAE,OAAO,EAAE,SAAS;AAAA,QAC/B,QAAQ,aAAE,OAAO,EAAE,SAAS;AAAA,QAC5B,SAAS,aAAE,OAAO,EAAE,S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