| 1 | """ |
| 2 | An agent, which is only responsible for the summarize locations tool call. |
| 3 | """ |
| 4 | |
| 5 | import json |
| 6 | import re |
| 7 | from collections.abc import Callable |
| 8 | from copy import deepcopy |
| 9 | from os.path import join as pjoin |
| 10 | from pathlib import Path |
| 11 | |
| 12 | from loguru import logger |
| 13 | |
| 14 | from app.api import agent_common |
| 15 | from app.data_structures import MessageThread |
| 16 | from app.log import print_acr, print_fix_loc_generation |
| 17 | from app.model import common |
| 18 | from app.task import Task |
| 19 | |
| 20 | SYSTEM_PROMPT = """You are a software developer maintaining a large project. |
| 21 | You are working on an issue submitted to your project. |
| 22 | The issue contains a description marked between <issue> and </issue>. |
| 23 | You ultimate goal is to write a list of locations that you can give to another devloper. |
| 24 | """ |
| 25 | |
| 26 | |
| 27 | USER_PROMPT_INIT = """Write a list of fix locations, based on the retrieved context.\n\n |
| 28 | Return the list of locations in the format below.\n\nWithin `<file></file>`, replace `...` with actual file path. Within `<class></class>`, replace `...` with a class name if needed. Within `<method></method`, replace `...` with a method name.\n\n |
| 29 | You can write multiple locations if needed. |
| 30 | |
| 31 | ``` |
| 32 | # candidate 1 |
| 33 | <file>...</file> |
| 34 | <class>...</class> |
| 35 | <method>...</method> |
| 36 | |
| 37 | |
| 38 | # candidate 2 |
| 39 | <file>...</file> |
| 40 | <class>...</class> |
| 41 | <method>...</method> |
| 42 | |
| 43 | # candidate 3 |
| 44 | ... |
| 45 | ``` |
| 46 | """ |
| 47 | |
| 48 | |
| 49 | def run_with_retries( |
| 50 | message_thread: MessageThread, |
| 51 | output_dir: str, |
| 52 | task: Task, |
| 53 | retries=3, |
| 54 | print_callback: Callable[[dict], None] | None = None, |
| 55 | ) -> tuple[str, float, int, int]: |
| 56 | """ |
| 57 | Since the agent may not always write a correct list, we allow for retries. |
| 58 | This is a wrapper around the actual run. |
| 59 | """ |
| 60 | # (1) replace system prompt |
| 61 | messages = deepcopy(message_thread.messages) |
| 62 | new_thread: MessageThread = MessageThread(messages=messages) |
| 63 | new_thread = agent_common.replace_system_prompt(new_thread, SYSTEM_PROMPT) |
| 64 | |
| 65 | # (2) add the initial user prompt |
| 66 | new_thread.add_user(USER_PROMPT_INIT) |
| 67 | print_acr( |
| 68 | USER_PROMPT_INIT, "fix location generation", print_callback=print_callback |
| 69 | ) |
| 70 | |
| 71 | can_stop = False |
| 72 | result_msg = "" |
| 73 | |
| 74 | logger.info("Starting the agent to propose fix locations.") |
| 75 | |
| 76 | for i in range(1, retries + 2): |
| 77 | if i > 1: |
| 78 | debug_file = pjoin(output_dir, f"debug_agent_propose_locs_{i - 1}.json") |
| 79 | with open(debug_file, "w") as f: |
| 80 | json.dump(new_thread.to_msg(), f, indent=4) |
| 81 | |
| 82 | if can_stop or i > retries: |
| 83 | break |
| 84 | |
| 85 | logger.info(f"Trying to propose fix locations. Try {i} of {retries}.") |
| 86 | |
| 87 | raw_location_file = pjoin(output_dir, f"agent_loc_list_{i}") |
| 88 | |
| 89 | # actually calling model |
| 90 | res_text, *_ = common.SELECTED_MODEL.call(new_thread.to_msg()) |
| 91 | |
| 92 | new_thread.add_model(res_text, []) # no tools |
| 93 | |
| 94 | logger.info(f"Fix locations produced in try {i}. Writing locations into file.") |
| 95 | |
| 96 | with open(raw_location_file, "w") as f: |
| 97 | f.write(res_text) |
| 98 | |
| 99 | # print("HI", raw_location_file) |
| 100 | |
| 101 | print_fix_loc_generation( |
| 102 | res_text, f"try {i} / {retries}", print_callback=print_callback |
| 103 | ) |
| 104 | |
| 105 | fragment_pattern = re.compile( |
| 106 | r"<file>(.*?)</file>\s*<class>(.*?)</class>\s*<method>(.*?)</method>" |
| 107 | ) |
| 108 | |
| 109 | result_msg = "No fragments found" |
| 110 | res = [] |
| 111 | for match in fragment_pattern.finditer(res_text): |
| 112 | res.append( |
| 113 | { |
| 114 | "file": match.group(1), |
| 115 | "class": match.group(2), |
| 116 | "method": match.group(3), |
| 117 | } |
| 118 | ) |
| 119 | result_msg = "Found fragments" |
| 120 | |
| 121 | can_stop = res != [] |
| 122 | Path(output_dir, f"agent_fix_locations_{i}.json").write_text( |
| 123 | json.dumps(res, indent=4) |
| 124 | ) |
| 125 | return result_msg |