File size: 4,047 Bytes
1e49a59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""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