Create a new pattern with necessary files and structure.
(
pattern_name: str, content: Optional[str] = None
)
| 373 | |
| 374 | |
| 375 | def create_pattern( |
| 376 | pattern_name: str, content: Optional[str] = None |
| 377 | ) -> Tuple[bool, str]: |
| 378 | """Create a new pattern with necessary files and structure.""" |
| 379 | new_pattern_path = None |
| 380 | try: |
| 381 | # Validate pattern name |
| 382 | if not pattern_name: |
| 383 | logger.error("Pattern name cannot be empty") |
| 384 | return False, "Pattern name cannot be empty." |
| 385 | |
| 386 | # Check if pattern already exists |
| 387 | new_pattern_path = os.path.join(pattern_dir, pattern_name) |
| 388 | if os.path.exists(new_pattern_path): |
| 389 | logger.error(f"Pattern {pattern_name} already exists") |
| 390 | return False, "Pattern already exists." |
| 391 | |
| 392 | # Create pattern directory |
| 393 | os.makedirs(new_pattern_path) |
| 394 | logger.info(f"Created pattern directory: {new_pattern_path}") |
| 395 | |
| 396 | # If content is provided, use fabric create_pattern to structure it |
| 397 | if content: |
| 398 | logger.info( |
| 399 | f"Structuring content for pattern '{pattern_name}' using Fabric" |
| 400 | ) |
| 401 | try: |
| 402 | # Get current model and provider configuration |
| 403 | current_provider = st.session_state.config.get("vendor") |
| 404 | current_model = st.session_state.config.get("model") |
| 405 | |
| 406 | if not current_provider or not current_model: |
| 407 | raise ValueError("Please select a provider and model first.") |
| 408 | |
| 409 | # Execute fabric create_pattern with input content |
| 410 | cmd = ["fabric", "--pattern", "create_pattern"] |
| 411 | if current_provider and current_model: |
| 412 | cmd.extend(["--vendor", current_provider, "--model", current_model]) |
| 413 | |
| 414 | logger.debug(f"Running command: {' '.join(cmd)}") |
| 415 | logger.debug(f"Input content:\n{content}") |
| 416 | |
| 417 | # Execute pattern |
| 418 | result = run( |
| 419 | cmd, input=content, capture_output=True, text=True, check=True |
| 420 | ) |
| 421 | structured_content = result.stdout.strip() |
| 422 | |
| 423 | if not structured_content: |
| 424 | raise ValueError("No output received from create_pattern") |
| 425 | |
| 426 | # Save the structured content to system.md |
| 427 | system_file = os.path.join(new_pattern_path, "system.md") |
| 428 | with open(system_file, "w") as f: |
| 429 | f.write(structured_content) |
| 430 | |
| 431 | # Validate the created pattern |
| 432 | is_valid, validation_message = validate_pattern(pattern_name) |
no test coverage detected