Elffuss BraidHub

kikocis/auto-code-rover public

831d2a8f2ea0 · 4.2 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, title=title, title_align="left", border_style="magenta", width=WIDTH
78 )
79 console.print(panel)
80
81 if print_callback:
82 print_callback(
83 {"title": f"{name} ({desc})", "message": msg, "category": "auto_code_rover"}
84 )
85
86
87def print_retrieval(
88 msg: str, desc="", print_callback: Callable[[dict], None] | None = None
89) -> None:
90 if not print_stdout:
91 return
92
93 msg = replace_html_tags(msg)
94 markdown = Markdown(msg)
95
96 name = "Context Retrieval Agent"
97 if desc:
98 title = f"{name} ({desc})"
99 else:
100 title = name
101
102 panel = Panel(
103 markdown, title=title, title_align="left", border_style="blue", width=WIDTH
104 )
105 console.print(panel)
106 if print_callback:
107 print_callback(
108 {
109 "title": f"{name} ({desc})",
110 "message": msg,
111 "category": "context_retrieval_agent",
112 }
113 )
114
115
116def print_patch_generation(
117 msg: str, desc="", print_callback: Callable[[dict], None] | None = None
118) -> None:
119 if not print_stdout:
120 return
121
122 msg = replace_html_tags(msg)
123 markdown = Markdown(msg)
124
125 name = "Patch Generation"
126 if desc:
127 title = f"{name} ({desc})"
128 else:
129 title = name
130
131 panel = Panel(
132 markdown, title=title, title_align="left", border_style="yellow", width=WIDTH
133 )
134 console.print(panel)
135 if print_callback:
136 print_callback(
137 {
138 "title": f"{name} ({desc})",
139 "message": msg,
140 "category": "patch_generation",
141 }
142 )
143
144
145def print_issue(content: str) -> None:
146 if not print_stdout:
147 return
148
149 title = "Issue description"
150 panel = Panel(
151 content, title=title, title_align="left", border_style="red", width=WIDTH
152 )
153 console.print(panel)
154
155
156def log_and_print(msg):
157 logger.info(msg)
158 if print_stdout:
159 console.print(msg)
160
161
162def log_and_cprint(msg, **kwargs):
163 logger.info(msg)
164 if print_stdout:
165 console.print(msg, **kwargs)
166
167
168def log_and_always_print(msg):
169 """
170 A mode which always print to stdout, no matter what.
171 Useful when running multiple tasks and we just want to see the important information.
172 """
173 logger.info(msg)
174 # always include time for important messages
175 t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
176 console.print(f"\n[{t}] {msg}")
177
178
179def print_with_time(msg):
180 """
181 Print a msg to console with timestamp.
182 """
183 t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
184 console.print(f"\n[{t}] {msg}")