Execute a sequence of patterns in a chain, passing output from each to the next. Args: patterns_sequence: List of pattern names to execute in sequence initial_input: Initial input text to start the chain Returns: Dict containing results from each stage of the chain
(patterns_sequence: List[str], initial_input: str)
| 1139 | |
| 1140 | |
| 1141 | def execute_pattern_chain(patterns_sequence: List[str], initial_input: str) -> Dict: |
| 1142 | """Execute a sequence of patterns in a chain, passing output from each to the next. |
| 1143 | |
| 1144 | Args: |
| 1145 | patterns_sequence: List of pattern names to execute in sequence |
| 1146 | initial_input: Initial input text to start the chain |
| 1147 | |
| 1148 | Returns: |
| 1149 | Dict containing results from each stage of the chain |
| 1150 | """ |
| 1151 | logger.info( |
| 1152 | f"Starting pattern chain execution with {len(patterns_sequence)} patterns" |
| 1153 | ) |
| 1154 | chain_results = { |
| 1155 | "sequence": patterns_sequence, |
| 1156 | "stages": [], |
| 1157 | "final_output": None, |
| 1158 | "metadata": { |
| 1159 | "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), |
| 1160 | "success": False, |
| 1161 | }, |
| 1162 | } |
| 1163 | |
| 1164 | current_input = initial_input |
| 1165 | |
| 1166 | try: |
| 1167 | for i, pattern in enumerate(patterns_sequence, 1): |
| 1168 | logger.info(f"Chain Stage {i}: Executing pattern '{pattern}'") |
| 1169 | stage_result = { |
| 1170 | "pattern": pattern, |
| 1171 | "input": current_input, |
| 1172 | "output": None, |
| 1173 | "success": False, |
| 1174 | "error": None, |
| 1175 | } |
| 1176 | |
| 1177 | try: |
| 1178 | cmd = ["fabric", "--pattern", pattern] |
| 1179 | result = run( |
| 1180 | cmd, input=current_input, capture_output=True, text=True, check=True |
| 1181 | ) |
| 1182 | output = result.stdout.strip() |
| 1183 | |
| 1184 | if output: |
| 1185 | stage_result["output"] = output |
| 1186 | stage_result["success"] = True |
| 1187 | current_input = output # Use this output as input for next pattern |
| 1188 | logger.debug(f"Stage {i} completed successfully") |
| 1189 | else: |
| 1190 | stage_result["error"] = "Pattern generated no output" |
| 1191 | logger.warning(f"Pattern {pattern} generated no output") |
| 1192 | |
| 1193 | except CalledProcessError as e: |
| 1194 | error_msg = f"Error executing pattern: {e.stderr.strip()}" |
| 1195 | stage_result["error"] = error_msg |
| 1196 | logger.error(error_msg) |
| 1197 | break |
| 1198 |
no test coverage detected