mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
fix: skip Python validation for Java/JS in optimize-line-profiler endpoint (#2478)
## Summary - Fix `/optimize-line-profiler` endpoint rejecting Java/JS/TS requests with `"Invalid Python version"` error by moving `parse_python_version()` and Python syntax validation inside `if is_python:` block - Fix code extraction regex in Java and JS/TS line profiler optimizers to handle LLM responses with ```` ```java:FileName.java ```` format (optional `:filename` suffix) --------- Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: HeshamHM28 <HeshamHM28@users.noreply.github.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
7deb16819e
commit
ea23cf06a6
3 changed files with 20 additions and 20 deletions
|
|
@ -43,8 +43,8 @@ JAVA_PROMPTS_DIR = current_dir / "prompts" / "optimizer"
|
|||
# Load Java system prompt
|
||||
JAVA_SYSTEM_PROMPT_TEXT = (JAVA_PROMPTS_DIR / "system_prompt.md").read_text()
|
||||
|
||||
# Pattern to extract code blocks from Java LLM response (single file, no file path)
|
||||
JAVA_CODE_PATTERN = re.compile(r"```(?:java)\s*\n(.*?)```", re.MULTILINE | re.DOTALL)
|
||||
# Pattern to extract code blocks from Java LLM response (handles optional :filename suffix)
|
||||
JAVA_CODE_PATTERN = re.compile(r"```(?:java)(?::[^\n]*)?\s*\n(.*?)```", re.MULTILINE | re.DOTALL)
|
||||
|
||||
# Pattern to extract code blocks with file paths (multi-file context)
|
||||
JAVA_CODE_WITH_PATH_PATTERN = re.compile(r"```(?:java):([^\n]+)\n(.*?)```", re.MULTILINE | re.DOTALL)
|
||||
|
|
|
|||
|
|
@ -38,8 +38,8 @@ JS_PROMPTS_DIR = current_dir / "prompts" / "optimizer"
|
|||
# Load JavaScript system prompt
|
||||
JS_SYSTEM_PROMPT_TEXT = (JS_PROMPTS_DIR / "system_prompt.md").read_text()
|
||||
|
||||
# Pattern to extract code blocks from JavaScript LLM response (single file, no file path)
|
||||
JS_CODE_PATTERN = re.compile(r"```(?:javascript|js|typescript|ts)\s*\n(.*?)```", re.MULTILINE | re.DOTALL)
|
||||
# Pattern to extract code blocks from JavaScript LLM response (handles optional :filename suffix)
|
||||
JS_CODE_PATTERN = re.compile(r"```(?:javascript|js|typescript|ts)(?::[^\n]*)?\s*\n(.*?)```", re.MULTILINE | re.DOTALL)
|
||||
|
||||
# Pattern to extract code blocks with file paths (multi-file context)
|
||||
JS_CODE_WITH_PATH_PATTERN = re.compile(
|
||||
|
|
|
|||
|
|
@ -200,21 +200,21 @@ async def optimize(request, data: OptimizeSchemaLP) -> tuple[int, OptimizeRespon
|
|||
is_javascript = language in ("javascript", "typescript")
|
||||
is_python = language == "python"
|
||||
|
||||
if is_python and not data.python_version:
|
||||
return 400, OptimizeErrorResponseSchema(error="Python version is required.")
|
||||
try:
|
||||
python_version: tuple[int, int, int] = parse_python_version(data.python_version)
|
||||
except: # noqa: E722
|
||||
return 400, OptimizeErrorResponseSchema(
|
||||
error="Invalid Python version, it should look like 3.x.x. We only support Python 3.9 and above."
|
||||
)
|
||||
try:
|
||||
if is_python:
|
||||
if is_python:
|
||||
if not data.python_version:
|
||||
return 400, OptimizeErrorResponseSchema(error="Python version is required.")
|
||||
try:
|
||||
python_version: tuple[int, int, int] = parse_python_version(data.python_version)
|
||||
except: # noqa: E722
|
||||
return 400, OptimizeErrorResponseSchema(
|
||||
error="Invalid Python version, it should look like 3.x.x. We only support Python 3.9 and above."
|
||||
)
|
||||
try:
|
||||
ctx.validate_and_parse_source_code(code=data.source_code, feature_version=python_version[:2])
|
||||
except SyntaxError:
|
||||
return 400, OptimizeErrorResponseSchema(
|
||||
error="Invalid source code. It is not valid Python code. Please check syntax of your code."
|
||||
)
|
||||
except SyntaxError:
|
||||
return 400, OptimizeErrorResponseSchema(
|
||||
error="Invalid source code. It is not valid Python code. Please check syntax of your code."
|
||||
)
|
||||
if not validate_trace_id(data.trace_id):
|
||||
return 400, OptimizeErrorResponseSchema(error="Invalid trace ID. Please provide a valid UUIDv4.")
|
||||
if data.line_profiler_results is None:
|
||||
|
|
@ -276,8 +276,8 @@ async def optimize(request, data: OptimizeSchemaLP) -> tuple[int, OptimizeRespon
|
|||
|
||||
elif language == "java":
|
||||
# Java path
|
||||
from aiservice.validators.java_validator import validate_java_syntax
|
||||
from core.languages.java.optimizer import is_multi_context_java
|
||||
from aiservice.validators.java_validator import validate_java_syntax # noqa: PLC0415
|
||||
from core.languages.java.optimizer import is_multi_context_java # noqa: PLC0415
|
||||
|
||||
# Demo hack shortcut
|
||||
if should_hack_for_demo_java(data.source_code):
|
||||
|
|
|
|||
Loading…
Reference in a new issue