Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion assistant-agent-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.agent.assistant</groupId>
<artifactId>assistant-agent</artifactId>
<version>0.1.7</version>
<version>0.1.9</version>
</parent>

<artifactId>assistant-agent-autoconfigure</artifactId>
Expand Down Expand Up @@ -78,6 +78,14 @@
<type>pom</type>
<optional>true</optional>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.0</version>
Copy link

Copilot AI Mar 4, 2026

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 统一管理。

Suggested change
<version>5.11.0</version>

Copilot uses AI. Check for mistakes.
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extractCodeFromContent 在检测到多个 fenced code block 时始终取“最后一个”匹配;如果最后一个代码块为空(例如模型输出了一个空的 python 结尾块),这里会丢掉前面非空的代码块并进入后续兜底逻辑,最终可能返回包含 markdown/自然语言的原文。建议在遍历 matcher 时记录“最后一个非空代码块”,优先返回最后一个非空 block。

Suggested change
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;
}

Copilot uses AI. Check for mistakes.

// 2. 整个内容以 ``` 开头的情况(向后兼容)
if (trimmed.startsWith("```")) {
int firstNewLine = trimmed.indexOf('\n');
if (firstNewLine > 0) {
code = code.substring(firstNewLine + 1);
trimmed = trimmed.substring(firstNewLine + 1);
}
// 去掉末尾的 ```
if (code.endsWith("```")) {
code = code.substring(0, code.length() - 3);
if (trimmed.endsWith("```")) {
trimmed = trimmed.substring(0, trimmed.length() - 3);
}
code = code.trim();
return trimmed.trim();
}

// 3. 没有 markdown 标记,尝试查找 def 函数定义并截取
// 处理 LLM 在代码前面输出自然语言但没用 ``` 包裹的情况
int defIndex = trimmed.indexOf("\ndef ");
if (defIndex < 0) {
defIndex = trimmed.startsWith("def ") ? 0 : -1;
}
if (defIndex >= 0) {
return trimmed.substring(defIndex).trim();
}

return code;
// 4. 兜底:返回整个内容
return trimmed;
}
}

Loading