File size: 2,736 Bytes
f2534b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from typing import Optional, Tuple

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import TerminalFormatter

from gensim.utils import (
    add_to_txt,
    extract_code,
    generate_feedback,
)


def _load_prompt_template(cfg) -> str:
    """Load the repair prompt from disk or fall back to a default template."""
    candidate_paths = [
        os.path.join("prompts", cfg.get("prompt_folder", ""), "cliport_prompt_code_repair.txt"),
        os.path.join("prompts", "cliport_prompt_code_repair.txt"),
    ]

    for path in candidate_paths:
        if path and os.path.exists(path):
            with open(path, "r") as handle:
                return handle.read()

    # Fallback prompt if no file exists.
    return (
        "You are an expert CLIport task engineer. The task name is TASK_NAME_TEMPLATE.\n"
        "Task description: TASK_DESCRIPTION_TEMPLATE\n"
        "Attempt AUTO_FIX_ATTEMPT_TEMPLATE failed with the following error:\n"
        "ERROR_LOG_TEMPLATE\n\n"
        "Fix the implementation below and return ONLY the corrected code wrapped in a python code block:\n"
        "CURRENT_CODE_TEMPLATE"
    )


def attempt_code_repair(
    cfg,
    task_metadata: dict,
    current_code: str,
    error_log: str,
    attempt_idx: int,
    interaction_log,
) -> Optional[Tuple[str, str]]:
    """Ask the LLM to repair the current code snippet using the provided traceback."""
    prompt_template = _load_prompt_template(cfg)
    prompt = prompt_template

    task_name = task_metadata.get("task-name", "unknown-task")
    task_desc = task_metadata.get("task-description", "No description provided.")
    prompt = prompt.replace("TASK_NAME_TEMPLATE", task_name)
    prompt = prompt.replace("TASK_DESCRIPTION_TEMPLATE", task_desc)
    prompt = prompt.replace("CURRENT_CODE_TEMPLATE", current_code.strip())
    prompt = prompt.replace("ERROR_LOG_TEMPLATE", error_log.strip())
    prompt = prompt.replace("AUTO_FIX_ATTEMPT_TEMPLATE", str(attempt_idx + 1))

    temperature = cfg.get("gpt_temperature", 0.0)
    response = generate_feedback(
        prompt,
        temperature=temperature,
        interaction_txt=interaction_log,
    )

    repaired_code, class_name = extract_code(response)
    if not repaired_code:
        print("Auto-fix failed to return code.")
        return None

    add_to_txt(
        interaction_log,
        f"[AUTO-FIX ATTEMPT {attempt_idx + 1}] Updated code for {task_name}",
        with_print=True,
    )
    print(
        highlight(
            f"[AUTO-FIX ATTEMPT {attempt_idx + 1}] Generated class: {class_name}",
            PythonLexer(),
            TerminalFormatter(),
        )
    )
    return repaired_code, class_name