Improvement Agent: Generate improved code (SVG or mxGraph XML) based on previous critique Use initial generation prompt + previous evaluation result to guide improvement
(
code: str,
code_image: Image.Image,
paper_content: str,
reference_figures: List[Image.Image],
iteration: int,
previous_critique: Optional[Dict],
human_guidance: Optional[str] = None,
topic: str = 'paper',
output_format: str = None
)
| 1560 | |
| 1561 | |
| 1562 | def improve_code( |
| 1563 | code: str, |
| 1564 | code_image: Image.Image, |
| 1565 | paper_content: str, |
| 1566 | reference_figures: List[Image.Image], |
| 1567 | iteration: int, |
| 1568 | previous_critique: Optional[Dict], |
| 1569 | human_guidance: Optional[str] = None, |
| 1570 | topic: str = 'paper', |
| 1571 | output_format: str = None |
| 1572 | ) -> Optional[str]: |
| 1573 | """ |
| 1574 | Improvement Agent: Generate improved code (SVG or mxGraph XML) based on previous critique |
| 1575 | Use initial generation prompt + previous evaluation result to guide improvement |
| 1576 | """ |
| 1577 | output_format = output_format or CONFIG.get('OUTPUT_FORMAT', 'svg') |
| 1578 | format_name = 'mxGraph XML' if output_format == 'mxgraphxml' else 'SVG' |
| 1579 | |
| 1580 | # Determine code extraction patterns based on format |
| 1581 | if output_format == 'mxgraphxml': |
| 1582 | start_tag = '<mxfile' |
| 1583 | end_tag = '</mxfile>' |
| 1584 | end_offset = 9 |
| 1585 | else: |
| 1586 | start_tag = '<svg' |
| 1587 | end_tag = '</svg>' |
| 1588 | end_offset = 6 |
| 1589 | |
| 1590 | try: |
| 1591 | # Get initial generation prompt as base |
| 1592 | base_prompt = get_initial_prompt_template(topic, paper_content, output_format) |
| 1593 | |
| 1594 | # Add previous evaluation feedback on top of initial prompt |
| 1595 | improvement_prompt = base_prompt |
| 1596 | |
| 1597 | # Add previous evaluation result as improvement guidance |
| 1598 | if previous_critique: |
| 1599 | try: |
| 1600 | previous_eval_json_str = json.dumps(previous_critique, ensure_ascii=False, indent=2) |
| 1601 | improvement_prompt += f""" |
| 1602 | |
| 1603 | **Previous Iteration Evaluation (Iteration {iteration-1}):** |
| 1604 | The previous design received the following evaluation: |
| 1605 | |
| 1606 | {previous_eval_json_str} |
| 1607 | |
| 1608 | **CRITICAL IMPROVEMENT REQUIREMENTS:** |
| 1609 | 1. You MUST systematically address each item in 'specific_issues' |
| 1610 | 2. You MUST implement each item in 'improvement_suggestions' |
| 1611 | 3. Focus especially on fixing: |
| 1612 | - Component overlap and alignment issues |
| 1613 | - Connector correctness and clarity |
| 1614 | - Layout balance and aesthetic quality |
| 1615 | - Content accuracy and completeness |
| 1616 | - Placeholder compliance |
| 1617 | """ |
| 1618 | except Exception as e: |
| 1619 | print(f"[Improvement Agent] Cannot format previous_critique: {e}") |
no test coverage detected