bpm-agent / utils /markdown_loader.py
limonad's picture
Upload 24 files
1e49a59 verified
"""Utility to load and parse agent definitions and process specifications from markdown files."""
import re
from pathlib import Path
from typing import Dict, Any, Optional, Tuple
def load_process_specifications(docs_dir: Path) -> Tuple[str, str]:
"""
Load process description and constraints from markdown files.
Args:
docs_dir: Directory containing the process specification files
Returns:
Tuple of (process_description, process_constraints) as strings
"""
process_desc_path = docs_dir / "process-description.md"
process_constraints_path = docs_dir / "process-constraints.md"
process_description = ""
if process_desc_path.exists():
process_description = process_desc_path.read_text(encoding="utf-8")
process_constraints = ""
if process_constraints_path.exists():
process_constraints = process_constraints_path.read_text(encoding="utf-8")
return process_description, process_constraints
def load_agent_definition(markdown_path: Path) -> Dict[str, Any]:
"""
Load agent definition from a markdown file.
Args:
markdown_path: Path to the markdown file containing agent definition
Returns:
Dictionary containing parsed agent configuration
"""
if not markdown_path.exists():
raise FileNotFoundError(f"Agent definition file not found: {markdown_path}")
content = markdown_path.read_text(encoding="utf-8")
# Parse the markdown content
agent_def = {
"name": _extract_section(content, "Agent Name"),
"role": _extract_section(content, "Role"),
"capabilities": _extract_section(content, "Capabilities"),
"system_prompt": _extract_section(content, "System Prompt"),
"tools": _extract_section(content, "Tools"),
"behavior_guidelines": _extract_section(content, "Behavior Guidelines"),
"examples": _extract_section(content, "Example Interactions"),
"configuration": _extract_section(content, "Configuration"),
}
return agent_def
def _extract_section(content: str, section_name: str) -> Optional[str]:
"""
Extract a section from markdown content.
Args:
content: Full markdown content
section_name: Name of the section to extract
Returns:
Section content or None if not found
"""
# Pattern to match section headers (## Section Name)
pattern = rf"##\s+{re.escape(section_name)}\s*\n(.*?)(?=\n##|\Z)"
match = re.search(pattern, content, re.DOTALL | re.IGNORECASE)
if match:
section_content = match.group(1).strip()
return section_content
return None
def get_system_prompt(agent_def: Dict[str, Any], process_description: str = "",
process_constraints: str = "") -> str:
"""
Extract and format the system prompt from agent definition with process context.
Args:
agent_def: Agent definition dictionary
process_description: Imperative process description
process_constraints: Normative process constraints
Returns:
Formatted system prompt string with process context
"""
system_prompt = agent_def.get("system_prompt", "")
# Add role context if available
if agent_def.get("role"):
system_prompt = f"Role: {agent_def['role']}\n\n{system_prompt}"
# Add behavior guidelines if available
if agent_def.get("behavior_guidelines"):
system_prompt += f"\n\nBehavior Guidelines:\n{agent_def['behavior_guidelines']}"
# Add process context if available
if process_description:
system_prompt += f"\n\n## Process Description (Imperative)\n{process_description}"
if process_constraints:
system_prompt += f"\n\n## Process Constraints (Normative - STRICT)\n{process_constraints}"
system_prompt += "\n\nREMEMBER: The process constraints are MANDATORY and must NEVER be violated."
return system_prompt
# Made with Bob