UNPKG

@simonecoelhosfo/optimizely-mcp-server

Version:

Optimizely MCP Server for AI assistants with integrated CLI tools

142 lines 4.68 kB
/** * Intelligent Payload Parser * @description Transforms AI-generated payloads into correct API formats before validation * * Purpose: Eliminate template mode friction by intelligently parsing creative AI payloads * and automatically converting them to the expected format for orchestration. * * Key Features: * - Non-destructive: Graceful fallback to template mode if parsing fails * - Pattern recognition: Detects page, event, variation patterns in payloads * - Field mapping: Maps AI field names to template field names using fuzzy matching * - Structure analysis: Finds data in deeply nested payload structures * - Format transformation: Converts complex objects to API-expected formats * * @author Optimizely MCP Server * @version 1.0.0 */ import { FieldMapping } from './FieldMapper.js'; export interface PayloadParseResult { success: boolean; transformedPayload?: any; errors?: string[]; suggestions?: string[]; confidence: number; appliedTransformations?: string[]; } export interface ParsingOptions { entityType: string; operation: string; platform?: 'feature' | 'web'; strict?: boolean; enableFuzzyMatching?: boolean; } export interface PayloadFieldMapping { aiFieldName: string; templateFieldName: string; confidence: number; transformer?: (value: any) => any; } export interface PatternMatch { pattern: string; confidence: number; suggestedEntityType?: string; suggestedOperation?: string; } /** * Main Intelligent Payload Parser class */ export declare class IntelligentPayloadParser { private readonly logger; private fuse; private patternEngine; private fieldMapper; private templateAutoFiller; private rulesetTransformer; private jsonataExtractor; constructor(); /** * Main entry point: Parse and transform an AI-generated payload */ parsePayload(payload: any, options: ParsingOptions): Promise<PayloadParseResult>; /** * Transform the payload structure based on mappings and patterns */ private transformPayload; /** * Apply structure-specific transformations using JSONPath */ private applyStructureTransformation; /** * Flatten nested ruleset_data to root level using JSONPath */ private flattenNestedRulesetData; /** * Wrap rules array in proper ruleset structure */ private wrapRulesInRulesetStructure; /** * Normalize ruleset field names and structure */ private normalizeRulesetFields; /** * Transform ruleset structure for API compatibility */ private transformRulesetStructure; /** * Handle event type routing for non-custom events * @description Detects when event_type is not "custom" and transforms payload for page event endpoint */ private handleEventTypeRouting; /** * Handle flag update with ab_test structure * @description Detects when variations/metrics are at root level and wraps them in ab_test */ private handleFlagAbTestStructure; /** * Specialized handler for ruleset payload transformations */ transformRulesetPayload(payload: any): Promise<PayloadParseResult>; /** * Get field mapping suggestions for a given entity type */ getFieldMappingSuggestions(entityType: string, aiFieldNames: string[]): Promise<FieldMapping[]>; /** * Analyze payload structure and provide insights */ analyzePayloadStructure(payload: any): Promise<{ depth: number; fieldCount: number; suspectedEntityTypes: string[]; recommendedTransformations: string[]; }>; /** * Correct JSON Patch paths for ruleset updates * Fixes common mistakes like /rules/{key}/variations -> /rules/{key}/actions/0/changes/0/value/variations */ private correctJsonPatchPaths; /** * Convert a ruleset object to JSON Patch operations */ private convertRulesetObjectToJsonPatch; /** * Fix common variation creation confusion where agents put flag key in the variation's key field * Example mistake: * { * "key": "user_journey_f461", // This is actually the flag key * "name": "control_f461" // This looks like the variation key * } * * Should be: * { * "key": "control_f461", // Variation key * "flag_key": "user_journey_f461" // Flag key * } */ private fixVariationKeyConfusion; } /** * Singleton instance for global use */ export declare const intelligentPayloadParser: IntelligentPayloadParser; //# sourceMappingURL=IntelligentPayloadParser.d.ts.map