Elffuss BraidHub

kikocis/auto-code-rover public

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