This is an automated email from the ASF dual-hosted git repository.
lizhimins pushed a commit to branch rocketmq-studio
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git
The following commit(s) were added to refs/heads/rocketmq-studio by this push:
new d825bc13e fix(ai): strip complete outer fence info strings from
enhanced prompts (#6680)
d825bc13e is described below
commit d825bc13ed09032da80d0148b32e1863c72fb486
Author: 健康宝 <[email protected]>
AuthorDate: Thu Oct 15 18:19:01 2026 +0800
fix(ai): strip complete outer fence info strings from enhanced prompts
(#6680)
---
.../ops/ai/conversation/agent/PromptEnhancer.java | 13 ++++-
.../ai/conversation/agent/PromptEnhancerTest.java | 57 ++++++++++++++++++++++
2 files changed, 68 insertions(+), 2 deletions(-)
diff --git
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/agent/PromptEnhancer.java
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/agent/PromptEnhancer.java
index cd8b129ac..2164ee3a4 100644
---
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/agent/PromptEnhancer.java
+++
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/agent/PromptEnhancer.java
@@ -116,8 +116,17 @@ public class PromptEnhancer {
return null;
}
String cleaned = enhanced.trim();
- if (cleaned.startsWith("```")) {
- cleaned = cleaned.replaceAll("(?s)^```[a-zA-Z]*\\s*",
"").replaceAll("(?s)```\\s*$", "").trim();
+ // Only a complete outer fence is stripped, and the whole opening fence
+ // line goes: the info string is not limited to letters (```json5,
+ // ```plain-text), and a letters-only pattern left its tail as the
+ // first line of the prompt. Fences embedded in the body and unclosed
+ // fences stay untouched.
+ String[] lines = cleaned.split("\n", -1);
+ if (lines.length >= 2 && lines[0].startsWith("```")
+ && lines[lines.length - 1].startsWith("```")
+ && lines[lines.length - 1].substring(3).trim().isEmpty()) {
+ cleaned = String.join("\n",
+ java.util.Arrays.copyOfRange(lines, 1, lines.length -
1)).trim();
}
return cleaned;
}
diff --git
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/agent/PromptEnhancerTest.java
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/agent/PromptEnhancerTest.java
new file mode 100644
index 000000000..4fa63eab4
--- /dev/null
+++
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/agent/PromptEnhancerTest.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.studio.ops.ai.conversation.agent;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class PromptEnhancerTest {
+
+ @ParameterizedTest
+ @ValueSource(strings = {"json5", "plain-text", "markdown", "js", ""})
+ void cleanStripsTheWholeOpeningFenceLineTest(String info) {
+ String wrapped = "```" + info + "\nrewrite of the prompt\n```\n";
+ // A letters-only pattern used to leave the tail of the info string
+ // ("5", "-text") as the first line of the enhanced prompt.
+ assertThat(PromptEnhancer.clean(wrapped)).isEqualTo("rewrite of the
prompt");
+ }
+
+ @Test
+ void cleanKeepsUnclosedFencesTest() {
+
assertThat(PromptEnhancer.clean("```\nrewrite")).isEqualTo("```\nrewrite");
+ }
+
+ @Test
+ void cleanKeepsFencesEmbeddedInTheBodyTest() {
+ String body = "before\n```sql\nSELECT 1\n```\nafter";
+ assertThat(PromptEnhancer.clean(body)).isEqualTo(body);
+ }
+
+ @Test
+ void cleanHandlesMultiLineRewritesTest() {
+ String wrapped = "```json5\nfirst\n\nsecond\n```";
+ assertThat(PromptEnhancer.clean(wrapped)).isEqualTo("first\n\nsecond");
+ }
+
+ @Test
+ void cleanReturnsNullForNullInputTest() {
+ assertThat(PromptEnhancer.clean(null)).isNull();
+ }
+}