mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
md support extract from md if returned from llms
This commit is contained in:
parent
88158b51d5
commit
e18a111d2b
1 changed files with 21 additions and 10 deletions
|
|
@ -56,6 +56,15 @@ JS_PATTERN = re.compile(r"^```(?:javascript|js|typescript|ts)?\s*\n(.*?)\n```",
|
|||
JS_IDENTIFIER_PATTERN = re.compile(r"^[a-zA-Z_$][a-zA-Z0-9_$]*$")
|
||||
|
||||
|
||||
_MARKDOWN_CODE_BLOCK_RE = re.compile(r"^```[^\n]*\n(.*?)\n```", re.MULTILINE | re.DOTALL)
|
||||
|
||||
|
||||
def _extract_code_blocks_from_markdown(source: str) -> list[str]:
|
||||
"""Extract code from markdown code blocks, or return source as-is if not markdown."""
|
||||
blocks = _MARKDOWN_CODE_BLOCK_RE.findall(source)
|
||||
return blocks if blocks else [source]
|
||||
|
||||
|
||||
def _is_valid_js_identifier(name: str) -> bool:
|
||||
"""Check if a name is a valid JavaScript identifier (not a reserved word).
|
||||
|
||||
|
|
@ -464,16 +473,18 @@ def validate_javascript_testgen_request_data(data: TestGenSchema) -> None:
|
|||
if not validate_trace_id(data.trace_id):
|
||||
raise HttpError(400, "Invalid trace ID. Please provide a valid UUIDv4.")
|
||||
|
||||
# Validate syntax based on language
|
||||
if data.language == "typescript":
|
||||
is_valid, error = validate_typescript_syntax(data.source_code_being_tested)
|
||||
lang_name = "TypeScript"
|
||||
else:
|
||||
is_valid, error = validate_javascript_syntax(data.source_code_being_tested)
|
||||
lang_name = "JavaScript"
|
||||
|
||||
if not is_valid:
|
||||
raise HttpError(400, f"Invalid source code. It is not valid {lang_name}: {error}")
|
||||
# Validate syntax of each code block (source may be markdown-formatted with ```lang:file blocks)
|
||||
source = data.source_code_being_tested
|
||||
code_blocks = _extract_code_blocks_from_markdown(source)
|
||||
for block in code_blocks:
|
||||
if data.language == "typescript":
|
||||
is_valid, error = validate_typescript_syntax(block)
|
||||
lang_name = "TypeScript"
|
||||
else:
|
||||
is_valid, error = validate_javascript_syntax(block)
|
||||
lang_name = "JavaScript"
|
||||
if not is_valid:
|
||||
raise HttpError(400, f"Invalid source code. It is not valid {lang_name}: {error}")
|
||||
|
||||
|
||||
async def testgen_javascript(
|
||||
|
|
|
|||
Loading…
Reference in a new issue