@@ -33,85 +33,77 @@ public class ResponseTemplateParser {
33
33
34
34
private static final Handlebars handlebars = new Handlebars ();
35
35
36
- // 支持 {{.}} 或 {{.xxx}} 或 {{.xxx.yyy}} 等多层级变量
36
+ // Supports {{.}} or {{.xxx}} or {{.xxx.yyy}} multi-level variables
37
37
private static final Pattern TEMPLATE_PATTERN = Pattern .compile ("\\ {\\ {\\ s*\\ .([\\ w\\ $\\ [\\ ]\\ .]*)\\ s*}}" ,
38
38
Pattern .DOTALL );
39
39
40
- // 检测是否包含多层级路径访问(如 {{.xxx.yyy}})
40
+ // Detects multi-level path access patterns like {{.xxx.yyy}}
41
+ // This regex is fully covered by unit tests in ResponseTemplateParserTest.java
41
42
private static final Pattern MULTI_LEVEL_PATTERN = Pattern .compile ("\\ {\\ {\\ s*\\ .\\ w+\\ .[\\ w\\ .]+\\ s*}}" );
42
43
43
44
/**
44
- * 处理响应模板
45
- * @param rawResponse 原始响应(JSON或文本)
46
- * @param responseTemplate 模板字符串(可为jsonPath、模板、 null/空)
47
- * @return 处理后的字符串
45
+ * Process response template
46
+ * @param rawResponse raw response (JSON or text)
47
+ * @param responseTemplate template string (can be jsonPath, template, null/empty)
48
+ * @return processed string
48
49
*/
49
50
public static String parse (String rawResponse , String responseTemplate ) {
50
51
if (!StringUtils .hasText (responseTemplate ) || "{{.}}" .equals (responseTemplate .trim ())) {
51
- // 原样输出
52
+ // Return raw output
52
53
return rawResponse ;
53
54
}
54
55
55
- // jsonPath 提取
56
+ // JsonPath extraction
56
57
if (responseTemplate .trim ().startsWith ("$." ) || responseTemplate .trim ().startsWith ("$[" )) {
57
58
try {
58
59
Object result = JsonPath .read (rawResponse , responseTemplate .trim ());
59
60
return result != null ? result .toString () : "" ;
60
61
}
61
62
catch (Exception e ) {
62
- // jsonPath 失败,降级为模板处理
63
+ // JsonPath failed, fallback to template processing
63
64
}
64
65
}
65
66
66
- // 检测是否包含多层级路径访问,如果包含则使用 Handlebars 引擎
67
+ // Detect multi-level path access
67
68
if (MULTI_LEVEL_PATTERN .matcher (responseTemplate ).find ()) {
68
69
return parseWithHandlebars (rawResponse , responseTemplate );
69
70
}
70
71
71
- // 简单模板变量替换(保持原有逻辑以确保向后兼容)
72
+ // Simple template variable replacement (maintain backward compatibility)
72
73
return parseWithSimpleTemplate (rawResponse , responseTemplate );
73
74
}
74
75
75
- /**
76
- * 使用 Handlebars 引擎处理多层级模板 兼容 higress.cn/ai/mcp-server 的模板语法 {{ .xxx.yyy }}
77
- */
78
76
private static String parseWithHandlebars (String rawResponse , String responseTemplate ) {
79
77
try {
80
- // 1. 预处理模板:转换语法以兼容 Handlebars
78
+ // 1. Preprocess template: convert syntax to be compatible with Handlebars
81
79
String handlebarsTemplateStr = responseTemplate
82
- // 移除点号前缀: {{ .xxx.yyy }} -> {{xxx.yyy}}
80
+ // Remove dot prefix: {{ .xxx.yyy }} -> {{xxx.yyy}}
83
81
.replaceAll ("\\ {\\ {\\ s*\\ ." , "{{" )
84
- // 转换数组访问语法: {{users.[0].name}} -> {{users.0.name}}
82
+ // Convert array access syntax: {{users.[0].name}} -> {{users.0.name}}
85
83
.replaceAll ("\\ [([0-9]+)\\ ]" , "$1" );
86
84
87
- // 2. 编译模板
85
+ // 2. Compile template
88
86
Template template = handlebars .compileInline (handlebarsTemplateStr );
89
87
90
- // 3. 准备数据上下文:将JSON字符串解析为 Map
91
88
Map <String , Object > dataContext ;
92
89
boolean isJson = rawResponse .trim ().startsWith ("{" ) || rawResponse .trim ().startsWith ("[" );
93
90
if (isJson ) {
94
91
dataContext = objectMapper .readValue (rawResponse , new TypeReference <Map <String , Object >>() {
95
92
});
96
93
}
97
94
else {
98
- // 非JSON数据,创建一个包含原始响应的上下文
95
+ // Non-JSON data, create a context containing the raw response
99
96
dataContext = Map .of ("_raw" , rawResponse );
100
97
}
101
98
102
- // 4. 应用模板并返回结果
103
99
return template .apply (dataContext );
104
100
105
101
}
106
102
catch (Exception e ) {
107
- // Handlebars 处理失败,降级为简单模板处理
108
103
return parseWithSimpleTemplate (rawResponse , responseTemplate );
109
104
}
110
105
}
111
106
112
- /**
113
- * 简单模板变量替换(原有逻辑)
114
- */
115
107
private static String parseWithSimpleTemplate (String rawResponse , String responseTemplate ) {
116
108
try {
117
109
Map <String , Object > context = null ;
0 commit comments