MCPcopy Create free account
hub / github.com/danielmiessler/Fabric / create_pattern

Function create_pattern

scripts/python_ui/streamlit.py:376–476  ·  view source on GitHub ↗

Create a new pattern with necessary files and structure.

(
    pattern_name: str, content: Optional[str] = None
)

Source from the content-addressed store, hash-verified

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

Callers 2

pattern_creation_wizardFunction · 0.85
pattern_creation_uiFunction · 0.85

Calls 2

runFunction · 0.85
validate_patternFunction · 0.85

Tested by

no test coverage detected