Skip to content

Commit 06de6d9

Browse files
lsp logs formatting and small fixes
1 parent a07b67d commit 06de6d9

File tree

8 files changed

+151
-120
lines changed

8 files changed

+151
-120
lines changed

codeflash/api/aiservice.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def optimize_python_code( # noqa: D417
133133
"repo_name": git_repo_name,
134134
}
135135

136-
logger.info("!lsp|tags|Generating optimized candidates…")
136+
logger.info("!lsp|Generating optimized candidates…")
137137
console.rule()
138138
try:
139139
response = self.make_ai_service_request("/optimize", payload=payload, timeout=600)
@@ -144,10 +144,10 @@ def optimize_python_code( # noqa: D417
144144

145145
if response.status_code == 200:
146146
optimizations_json = response.json()["optimizations"]
147-
logger.info(f"!lsp|tags|Generated {len(optimizations_json)} candidate optimizations.")
147+
logger.info(f"!lsp|Generated {len(optimizations_json)} candidate optimizations.")
148148
console.rule()
149149
end_time = time.perf_counter()
150-
logger.debug(f"!lsp|tags|Generating optimizations took {end_time - start_time:.2f} seconds.")
150+
logger.debug(f"!lsp|Generating possible optimizations took {end_time - start_time:.2f} seconds.")
151151
return self._get_valid_candidates(optimizations_json)
152152
try:
153153
error = response.json()["error"]
@@ -209,7 +209,7 @@ def optimize_python_code_line_profiler( # noqa: D417
209209
if response.status_code == 200:
210210
optimizations_json = response.json()["optimizations"]
211211
logger.info(
212-
f"!lsp|tags|Generated {len(optimizations_json)} candidate optimizations using line profiler information."
212+
f"!lsp|Generated {len(optimizations_json)} candidate optimizations using line profiler information."
213213
)
214214
console.rule()
215215
return self._get_valid_candidates(optimizations_json)
@@ -332,7 +332,7 @@ def get_new_explanation( # noqa: D417
332332
"original_explanation": original_explanation,
333333
"dependency_code": dependency_code,
334334
}
335-
logger.info("loading|tags|Generating explanation")
335+
logger.info("loading|Generating explanation")
336336
console.rule()
337337
try:
338338
response = self.make_ai_service_request("/explain", payload=payload, timeout=60)
@@ -377,7 +377,7 @@ def generate_ranking( # noqa: D417
377377
"optimization_ids": optimization_ids,
378378
"python_version": platform.python_version(),
379379
}
380-
logger.info("Generating ranking")
380+
logger.info("loading|Generating ranking")
381381
console.rule()
382382
try:
383383
response = self.make_ai_service_request("/rank", payload=payload, timeout=60)

codeflash/code_utils/git_worktree_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def create_detached_worktree(module_root: Path) -> Optional[Path]:
6262
)
6363

6464
if not uni_diff_text.strip():
65-
logger.info("!lsp|tags|No uncommitted changes to copy to worktree.")
65+
logger.info("!lsp|No uncommitted changes to copy to worktree.")
6666
return worktree_dir
6767

6868
# Write the diff to a temporary file

codeflash/discovery/functions_to_optimize.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ def get_functions_to_optimize(
173173
with warnings.catch_warnings():
174174
warnings.simplefilter(action="ignore", category=SyntaxWarning)
175175
if optimize_all:
176-
logger.info("!lsp|tags|Finding all functions in the module '%s'…", optimize_all)
176+
logger.info("!lsp|Finding all functions in the module '%s'…", optimize_all)
177177
console.rule()
178178
functions = get_all_files_and_functions(Path(optimize_all))
179179
elif replay_test:
180180
functions, trace_file_path = get_all_replay_test_functions(
181181
replay_test=replay_test, test_cfg=test_cfg, project_root_path=project_root
182182
)
183183
elif file is not None:
184-
logger.info("!lsp|tags|Finding all functions in the file '%s'…", file)
184+
logger.info("!lsp|Finding all functions in the file '%s'…", file)
185185
console.rule()
186186
functions = find_all_functions_in_file(file)
187187
if only_get_this_function is not None:
@@ -219,7 +219,7 @@ def get_functions_to_optimize(
219219
functions, test_cfg.tests_root, ignore_paths, project_root, module_root, previous_checkpoint_functions
220220
)
221221

222-
logger.info(f"!lsp|tags|Found {functions_count} function{'s' if functions_count > 1 else ''} to optimize")
222+
logger.info(f"!lsp|Found {functions_count} function{'s' if functions_count > 1 else ''} to optimize")
223223
if optimize_all:
224224
three_min_in_ns = int(1.8e11)
225225
console.rule()

codeflash/lsp/helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def report_to_markdown_table(report: dict[TestType, dict[str, int]], title: str)
3939
continue
4040
lines.append(f"| {test_type.to_name()} | {passed} | {failed} |")
4141
table = "\n".join(lines)
42-
return f"### {title}\n{table}"
42+
if title:
43+
return f"### {title}\n{table}"
44+
return table
4345

4446

4547
def simplify_worktree_paths(msg: str, highlight: bool = True) -> str: # noqa: FBT001, FBT002

codeflash/lsp/lsp_logger.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ class LspMessageTags:
1414
lsp: bool = False # lsp (lsp only)
1515
force_lsp: bool = False # force_lsp (you can use this to force a message to be sent to the LSP even if the level is not supported)
1616
loading: bool = False # loading (you can use this to indicate that the message is a loading message)
17-
17+
highlight: bool = False # highlight (you can use this to highlight the message by wrapping it in ``)
1818
h1: bool = False # h1
1919
h2: bool = False # h2
2020
h3: bool = False # h3
2121
h4: bool = False # h4
2222

2323

24+
def add_highlight_tags(msg: str, tags: LspMessageTags) -> str:
25+
if tags.highlight:
26+
return "`" + msg + "`"
27+
return msg
28+
29+
2430
def add_heading_tags(msg: str, tags: LspMessageTags) -> str:
2531
if tags.h1:
2632
return "# " + msg
@@ -33,9 +39,9 @@ def add_heading_tags(msg: str, tags: LspMessageTags) -> str:
3339
return msg
3440

3541

36-
# TODO: Make this work when optimizing extract_tags from lsp code message that has |tags|
3742
def extract_tags(msg: str) -> tuple[Optional[LspMessageTags], str]:
38-
parts = msg.split("|tags|")
43+
delimiter = "|"
44+
parts = msg.split(delimiter)
3945
if len(parts) == 2:
4046
message_tags = LspMessageTags()
4147
tags = {tag.strip() for tag in parts[0].split(",")}
@@ -47,6 +53,8 @@ def extract_tags(msg: str) -> tuple[Optional[LspMessageTags], str]:
4753
message_tags.force_lsp = True
4854
if "loading" in tags:
4955
message_tags.loading = True
56+
if "highlight" in tags:
57+
message_tags.highlight = True
5058
if "h1" in tags:
5159
message_tags.h1 = True
5260
if "h2" in tags:
@@ -55,7 +63,7 @@ def extract_tags(msg: str) -> tuple[Optional[LspMessageTags], str]:
5563
message_tags.h3 = True
5664
if "h4" in tags:
5765
message_tags.h4 = True
58-
return message_tags, parts[1]
66+
return message_tags, delimiter.join(parts[1:])
5967

6068
return None, msg
6169

@@ -74,17 +82,21 @@ def enhanced_log(
7482
actual_log_fn(msg, *args, **kwargs)
7583
return
7684

77-
tags, clean_msg = extract_tags(msg)
85+
is_lsp_json_message = msg.startswith('{"type"')
86+
is_normal_text_message = not is_lsp_json_message
87+
88+
# extract tags only from the text messages (not the json ones)
89+
tags, clean_msg = extract_tags(msg) if is_normal_text_message else (None, msg)
90+
7891
lsp_enabled = is_LSP_enabled()
7992
lsp_only = tags and tags.lsp
8093

8194
if not lsp_enabled and not lsp_only:
95+
# normal logging
8296
actual_log_fn(clean_msg, *args, **kwargs)
8397
return
8498

8599
#### LSP mode ####
86-
is_lsp_json_message = clean_msg.startswith('{"type"')
87-
is_normal_text_message = not is_lsp_json_message
88100
final_tags = tags if tags else LspMessageTags()
89101

90102
unsupported_level = level not in supported_lsp_log_levels
@@ -93,6 +105,7 @@ def enhanced_log(
93105

94106
if is_normal_text_message:
95107
clean_msg = add_heading_tags(clean_msg, final_tags)
108+
clean_msg = add_highlight_tags(clean_msg, final_tags)
96109
clean_msg = LspTextMessage(text=clean_msg, takes_time=final_tags.loading).serialize()
97110

98111
actual_log_fn(clean_msg, *args, **kwargs)

0 commit comments

Comments
 (0)