import json import ai_client import mma_prompts import re def generate_tickets(track_brief: str, module_skeletons: str) -> list[dict]: """ Tier 2 (Tech Lead) call. Breaks down a Track Brief and module skeletons into discrete Tier 3 Tickets. """ # 1. Set Tier 2 Model (Tech Lead - Flash) # 2. Construct Prompt system_prompt = mma_prompts.PROMPTS.get("tier2_sprint_planning") user_message = ( f"### TRACK BRIEF:\n{track_brief}\n\n" f"### MODULE SKELETONS:\n{module_skeletons}\n\n" "Please generate the implementation tickets for this track." ) # Set custom system prompt for this call old_system_prompt = ai_client._custom_system_prompt ai_client.set_custom_system_prompt(system_prompt) try: # 3. Call Tier 2 Model response = ai_client.send( md_content="", user_message=user_message ) # 4. Parse JSON Output # Extract JSON array from markdown code blocks if present json_match = response.strip() if "```json" in json_match: json_match = json_match.split("```json")[1].split("```")[0].strip() elif "```" in json_match: json_match = json_match.split("```")[1].split("```")[0].strip() # If it's still not valid JSON, try to find a [ ... ] block if not (json_match.startswith('[') and json_match.endswith(']')): match = re.search(r'\[\s*\{.*\}\s*\]', json_match, re.DOTALL) if match: json_match = match.group(0) tickets = json.loads(json_match) return tickets except Exception as e: print(f"Error parsing Tier 2 response: {e}") # print(f"Raw response: {response}") return [] finally: # Restore old system prompt ai_client.set_custom_system_prompt(old_system_prompt) from dag_engine import TrackDAG from models import Ticket def topological_sort(tickets: list[dict]) -> list[dict]: """ Sorts a list of tickets based on their 'depends_on' field. Raises ValueError if a circular dependency or missing internal dependency is detected. """ # 1. Convert to Ticket objects for TrackDAG ticket_objs = [] for t_data in tickets: ticket_objs.append(Ticket.from_dict(t_data)) # 2. Use TrackDAG for validation and sorting dag = TrackDAG(ticket_objs) try: sorted_ids = dag.topological_sort() except ValueError as e: raise ValueError(f"DAG Validation Error: {e}") # 3. Return sorted dictionaries ticket_map = {t['id']: t for t in tickets} return [ticket_map[tid] for tid in sorted_ids] if __name__ == "__main__": # Quick test if run directly test_brief = "Implement a new feature." test_skeletons = "class NewFeature: pass" tickets = generate_tickets(test_brief, test_skeletons) print(json.dumps(tickets, indent=2))