Skip to content

Commit edd8b9f

Browse files
tjx222tanjinxiang
andauthored
fix: 优化执行步骤获取逻辑和SQL生成流程, 解决重新生成死循环问题 #130 (#140)
* 优化执行步骤获取逻辑和SQL生成流程, 解决重新生成死循环问题 - 重构 `PlanProcessUtil.java` 中的 getCurrentExecutionStep 方法,新增重载版本以支持传入 plan 参数, 解决重新生成的sql 未生效。 - 优化 SQL 生成完成后的返回逻辑,将返回最佳 SQL 改为返回空 Flux 避免死循环 * fix: format code --------- Co-authored-by: tanjinxiang <[email protected]>
1 parent 1fc35b8 commit edd8b9f

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

spring-ai-alibaba-data-agent-chat/src/main/java/com/alibaba/cloud/ai/dataagent/node/SqlGenerateNode.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public Map<String, Object> apply(OverAllState state) throws Exception {
7676

7777
// Get necessary input parameters
7878
Plan plan = PlanProcessUtil.getPlan(state);
79-
ExecutionStep executionStep = PlanProcessUtil.getCurrentExecutionStep(state);
79+
ExecutionStep executionStep = PlanProcessUtil.getCurrentExecutionStep(plan,
80+
PlanProcessUtil.getCurrentStepNumber(state));
8081
ExecutionStep.ToolParameters toolParameters = executionStep.getToolParameters();
8182

8283
// Execute business logic first - determine what needs to be regenerated
@@ -219,11 +220,11 @@ public Flux<String> get() {
219220
roundRef.get())
220221
.collect(StringBuilder::new, StringBuilder::append)
221222
.map(StringBuilder::toString)
222-
.flatMapMany(sql -> Flux.just(nl2SqlService.sqlTrim(sql)).expand(newSql -> {
223+
.flatMapMany(sql -> Flux.just(nl2SqlService.sqlTrim(sql)).expandDeep(newSql -> {
223224
if (checkSqlFunc.apply(newSql) || roundRef.getAndIncrement() > MAX_OPTIMIZATION_ROUNDS) {
224225
String bestSql = bestSqlRef.get();
225226
bestSqlConsumer.accept(bestSql);
226-
return Flux.just(bestSql);
227+
return Flux.empty();
227228
}
228229
else {
229230
return this.get();
@@ -243,7 +244,7 @@ public Flux<String> get() {
243244
if (checkSqlFunc.apply(newSql) || roundRef.getAndIncrement() > MAX_OPTIMIZATION_ROUNDS) {
244245
String bestSql = bestSqlRef.get();
245246
bestSqlConsumer.accept(bestSql);
246-
return Flux.just(bestSql);
247+
return Flux.empty();
247248
}
248249
else {
249250
return reGenerateSupplier.get();

spring-ai-alibaba-data-agent-chat/src/main/java/com/alibaba/cloud/ai/dataagent/util/PlanProcessUtil.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,18 @@ private PlanProcessUtil() {
6060
public static ExecutionStep getCurrentExecutionStep(OverAllState state) {
6161
Plan plan = getPlan(state);
6262
int currentStep = getCurrentStepNumber(state);
63+
return getCurrentExecutionStep(plan, currentStep);
64+
}
6365

66+
/**
67+
* Get the current execution step from the plan
68+
* @param plan the plan object
69+
* @param currentStep current step
70+
* @return the current execution step
71+
* @throws IllegalStateException if plan output is empty, plan parsing fails, or step
72+
* index is out of range
73+
*/
74+
public static ExecutionStep getCurrentExecutionStep(Plan plan, Integer currentStep) {
6475
List<ExecutionStep> executionPlan = plan.getExecutionPlan();
6576
if (executionPlan == null || executionPlan.isEmpty()) {
6677
throw new IllegalStateException("执行计划为空");

0 commit comments

Comments
 (0)