-
Notifications
You must be signed in to change notification settings - Fork 88
refactor(CodeGeneratorNode): Code Extraction Improvements #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f2ed68c
refactor(CodeGeneratorNode): 提取代码生成节点中的代码提取逻辑
canfuu 2b47260
refactor(AssistantAgent): improve test readability and maintainability
canfuu 8335fe9
Merge remote-tracking branch 'github/main' into feature/fix_execute_c…
canfuu 373e192
Merge remote-tracking branch 'github/main' into feature/fix_execute_c…
canfuu 0c01426
chore(pom.xml): 更新版本号至0.1.9
canfuu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -48,6 +48,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Set; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.regex.Matcher; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.regex.Pattern; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.stream.Collectors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -851,22 +853,71 @@ private String extractCodeFromResponse(ModelResponse response) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalStateException("Generated code is empty"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 移除markdown标记(如果有) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| String code = content.trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (code.startsWith("```")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 去掉 ```python 或 ```java 等标记 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| int firstNewLine = code.indexOf('\n'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return extractCodeFromContent(content); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * 从 LLM 输出的原始文本中提取纯 Python 代码。 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <p>提取策略(按优先级): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <ol> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <li>从 {@code ```python ... ```} 或 {@code ``` ... ```} 代码块中提取(多个代码块取最后一个)</li> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <li>整个内容以 {@code ```} 开头时剥离 markdown 标记(向后兼容)</li> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <li>无 markdown 标记时,查找 {@code def } 函数定义位置并截取(剥离自然语言前缀)</li> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <li>兜底:返回整个内容</li> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * </ol> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param content LLM 输出的原始文本 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return 提取后的纯代码 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| static String extractCodeFromContent(String content) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (content == null || content.trim().isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return content; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| String trimmed = content.trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 1. 尝试从内容中提取 ```python ... ``` 或 ``` ... ``` 代码块 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // LLM 经常在代码块前输出自然语言分析文字,必须正确剥离 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 使用 DOTALL 让 . 匹配换行符 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pattern codeBlockPattern = Pattern.compile("```(?:python|py)?\\s*\\n(.*?)```", Pattern.DOTALL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Matcher matcher = codeBlockPattern.matcher(trimmed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (matcher.find()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 如果有多个代码块,取最后一个(通常最终版本在最后) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| String lastBlock = matcher.group(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (matcher.find()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastBlock = matcher.group(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| String code = lastBlock.trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!code.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+885
to
+895
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (matcher.find()) { | |
| // 如果有多个代码块,取最后一个(通常最终版本在最后) | |
| String lastBlock = matcher.group(1); | |
| while (matcher.find()) { | |
| lastBlock = matcher.group(1); | |
| } | |
| String code = lastBlock.trim(); | |
| if (!code.isEmpty()) { | |
| return code; | |
| } | |
| } | |
| // 遍历所有匹配,记录“最后一个非空代码块” | |
| String lastNonEmptyCodeBlock = null; | |
| while (matcher.find()) { | |
| String block = matcher.group(1); | |
| if (block != null) { | |
| String candidate = block.trim(); | |
| if (!candidate.isEmpty()) { | |
| lastNonEmptyCodeBlock = candidate; | |
| } | |
| } | |
| } | |
| if (lastNonEmptyCodeBlock != null) { | |
| return lastNonEmptyCodeBlock; | |
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
该模块的父 POM 已通过根
pom.xml导入了spring-boot-dependencies(BOM)来统一管理测试依赖版本;仓库内其它模块引入junit-jupiter也未显式声明版本。这里固定写死5.11.0可能导致版本漂移/冲突(尤其是多模块依赖收敛时)。建议移除<version>,交由 BOM 统一管理。