Elffuss BraidHub

kikocis/auto-code-rover public

0cb0fe7a2cd9 · 4.9 KiBRawPermalink
1import time
2from collections.abc import Callable
3from os import get_terminal_size
4
5from loguru import logger
6from rich.console import Console
7from rich.markdown import Markdown
8from rich.markup import escape
9from rich.panel import Panel
10
11
12def terminal_width():
13 return get_terminal_size().columns
14
15
16WIDTH = min(120, terminal_width() - 10)
17
18console = Console()
19
20print_stdout = True
21
22
23def log_exception(exception):
24 logger.exception(exception)
25
26
27def print_banner(msg: str) -> None:
28 if not print_stdout:
29 return
30
31 banner = f" {msg} ".center(WIDTH, "=")
32 console.print()
33 console.print(banner, style="bold")
34 console.print()
35
36
37def replace_html_tags(content: str):
38 """
39 Helper method to process the content before printing to markdown.
40 """
41 replace_dict = {
42 "<file>": "[file]",
43 "<class>": "[class]",
44 "<func>": "[func]",
45 "<method>": "[method]",
46 "<code>": "[code]",
47 "<original>": "[original]",
48 "<patched>": "[patched]",
49 "</file>": "[/file]",
50 "</class>": "[/class]",
51 "</func>": "[/func]",
52 "</method>": "[/method]",
53 "</code>": "[/code]",
54 "</original>": "[/original]",
55 "</patched>": "[/patched]",
56 }
57 for key, value in replace_dict.items():
58 content = content.replace(key, value)
59 return content
60
61
62def print_acr(
63 msg: str, desc="", print_callback: Callable[[dict], None] | None = None
64) -> None:
65 if not print_stdout:
66 return
67
68 msg = replace_html_tags(msg)
69 markdown = Markdown(msg)
70
71 name = "AutoCodeRover"
72 if desc:
73 title = f"{name} ({desc})"
74 else:
75 title = name
76
77 panel = Panel(
78 markdown, title=title, title_align="left", border_style="magenta", width=WIDTH
79 )
80 console.print(panel)
81
82 if print_callback:
83 print_callback(
84 {"title": f"{name} ({desc})", "message": msg, "category": "auto_code_rover"}
85 )
86
87
88def print_retrieval(
89 msg: str, desc="", print_callback: Callable[[dict], None] | None = None
90) -> None:
91 if not print_stdout:
92 return
93
94 msg = replace_html_tags(msg)
95 markdown = Markdown(msg)
96
97 name = "Context Retrieval Agent"
98 if desc:
99 title = f"{name} ({desc})"
100 else:
101 title = name
102
103 panel = Panel(
104 markdown, title=title, title_align="left", border_style="blue", width=WIDTH
105 )
106 console.print(panel)
107 if print_callback:
108 print_callback(
109 {
110 "title": f"{name} ({desc})",
111 "message": msg,
112 "category": "context_retrieval_agent",
113 }
114 )
115
116
117def print_patch_generation(
118 msg: str, desc="", print_callback: Callable[[dict], None] | None = None
119) -> None:
120 if not print_stdout:
121 return
122
123 msg = replace_html_tags(msg)
124 markdown = Markdown(msg)
125
126 name = "Patch Generation"
127 if desc:
128 title = f"{name} ({desc})"
129 else:
130 title = name
131
132 panel = Panel(
133 markdown, title=title, title_align="left", border_style="yellow", width=WIDTH
134 )
135 console.print(panel)
136 if print_callback:
137 print_callback(
138 {
139 "title": f"{name} ({desc})",
140 "message": msg,
141 "category": "patch_generation",
142 }
143 )
144
145
146def print_fix_loc_generation(
147 msg: str, desc="", print_callback: Callable[[dict], None] | None = None
148) -> None:
149 if not print_stdout:
150 return
151
152 msg = replace_html_tags(msg)
153 markdown = Markdown(msg)
154
155 name = "Fix Location Generation"
156 if desc:
157 title = f"{name} ({desc})"
158 else:
159 title = name
160
161 panel = Panel(
162 markdown, title=title, title_align="left", border_style="green", width=WIDTH
163 )
164 console.print(panel)
165 if print_callback:
166 print_callback(
167 {
168 "title": f"{name} ({desc})",
169 "message": msg,
170 "category": "fix_loc_generation",
171 }
172 )
173
174
175def print_issue(content: str) -> None:
176 if not print_stdout:
177 return
178
179 title = "Issue description"
180 panel = Panel(
181 escape(content),
182 title=title,
183 title_align="left",
184 border_style="red",
185 width=WIDTH,
186 )
187 console.print(panel)
188
189
190def log_and_print(msg):
191 logger.info(msg)
192 if print_stdout:
193 console.print(msg)
194
195
196def log_and_cprint(msg, **kwargs):
197 logger.info(msg)
198 if print_stdout:
199 console.print(msg, **kwargs)
200
201
202def log_and_always_print(msg):
203 """
204 A mode which always print to stdout, no matter what.
205 Useful when running multiple tasks and we just want to see the important information.
206 """
207 logger.info(msg)
208 # always include time for important messages
209 t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
210 console.print(f"\n[{t}] {msg}")
211
212
213def print_with_time(msg):
214 """
215 Print a msg to console with timestamp.
216 """
217 t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
218 console.print(f"\n[{t}] {msg}")