Skip to content

Commit e946fda

Browse files
committed
调整template
1 parent df782b7 commit e946fda

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

backend/modules/evaluation/domain/service/javascript_code_builder.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010

1111
"github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/component"
1212
"github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/entity"
13-
"github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/service/templates"
13+
"github.com/coze-dev/coze-loop/backend/modules/evaluation/pkg/conf/templates"
1414
)
1515

1616
// JavaScriptCodeBuilder JavaScript代码构建器
17-
type JavaScriptCodeBuilder struct{
17+
type JavaScriptCodeBuilder struct {
1818
runtime component.IRuntime // 运行时实例,用于获取return_val函数
1919
}
2020

@@ -54,7 +54,7 @@ func (b *JavaScriptCodeBuilder) BuildCode(input *entity.EvaluatorInputData, code
5454
// 使用strings.Replace替换占位符
5555
// 替换return_val函数占位符 - 现在从runtime获取
5656
jsCode = strings.Replace(jsCode, "{{RETURN_VAL_FUNCTION}}", b.getReturnValFunctionFromRuntime(), 1)
57-
57+
5858
// 替换turn变量占位符
5959
jsCode = strings.Replace(jsCode, "{{TURN_DATA}}", turnDataStr, 1)
6060

@@ -122,6 +122,7 @@ func (b *JavaScriptCodeBuilder) validateInputData(inputData map[string]interface
122122

123123
return nil
124124
}
125+
125126
// buildInputData 构建代码执行的输入数据
126127
func (b *JavaScriptCodeBuilder) buildInputData(input *entity.EvaluatorInputData) (map[string]interface{}, error) {
127128
inputData := make(map[string]interface{})
@@ -168,20 +169,21 @@ func (b *JavaScriptCodeBuilder) buildInputData(input *entity.EvaluatorInputData)
168169

169170
return inputData, nil
170171
}
172+
171173
// BuildSyntaxCheckCode 构建JavaScript语法检查代码
172174
func (b *JavaScriptCodeBuilder) BuildSyntaxCheckCode(userCode string) string {
173175
// 使用模板构建语法检查代码
174176
syntaxCheckTemplate := templates.JavaScriptSyntaxCheckTemplate
175-
177+
176178
// 转义用户代码中的特殊字符,确保能正确嵌入到模板字符串中
177179
escapedCode := b.escapeCodeForTemplate(userCode)
178-
180+
179181
// 替换return_val函数占位符 - 现在从runtime获取
180182
syntaxCheckCode := strings.Replace(syntaxCheckTemplate, "{{RETURN_VAL_FUNCTION}}", b.getReturnValFunctionFromRuntime(), 1)
181-
183+
182184
// 替换模板中的用户代码占位符
183185
syntaxCheckCode = strings.Replace(syntaxCheckCode, "{{USER_CODE}}", escapedCode, 1)
184-
186+
185187
return syntaxCheckCode
186188
}
187189

@@ -208,7 +210,7 @@ func (b *JavaScriptCodeBuilder) getReturnValFunctionFromRuntime() string {
208210
if b.runtime != nil {
209211
return b.runtime.GetReturnValFunction()
210212
}
211-
213+
212214
// 如果没有runtime实例,使用默认实现保持向后兼容
213215
return `
214216
// return_val函数实现
@@ -220,4 +222,4 @@ function return_val(value) {
220222
console.log(value);
221223
}
222224
`
223-
}
225+
}

backend/modules/evaluation/domain/service/python_code_builder.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010

1111
"github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/component"
1212
"github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/entity"
13-
"github.com/coze-dev/coze-loop/backend/modules/evaluation/domain/service/templates"
13+
"github.com/coze-dev/coze-loop/backend/modules/evaluation/pkg/conf/templates"
1414
)
1515

1616
// PythonCodeBuilder Python代码构建器
17-
type PythonCodeBuilder struct{
17+
type PythonCodeBuilder struct {
1818
runtime component.IRuntime // 运行时实例,用于获取return_val函数
1919
}
2020

@@ -54,7 +54,7 @@ func (b *PythonCodeBuilder) BuildCode(input *entity.EvaluatorInputData, codeVers
5454
// 使用strings.Replace替换占位符
5555
// 替换return_val函数占位符 - 现在从runtime获取
5656
pythonCode = strings.Replace(pythonCode, "{{RETURN_VAL_FUNCTION}}", b.getReturnValFunctionFromRuntime(), 1)
57-
57+
5858
// 替换turn变量占位符
5959
pythonCode = strings.Replace(pythonCode, "{{TURN_DATA}}", turnDataStr, 1)
6060

@@ -113,7 +113,7 @@ func (b *PythonCodeBuilder) getReturnValFunctionFromRuntime() string {
113113
if b.runtime != nil {
114114
return b.runtime.GetReturnValFunction()
115115
}
116-
116+
117117
// 如果没有runtime实例,使用默认实现保持向后兼容
118118
return `
119119
# return_val函数实现
@@ -151,21 +151,22 @@ func (b *PythonCodeBuilder) validateInputData(inputData map[string]interface{})
151151

152152
return nil
153153
}
154+
154155
// BuildSyntaxCheckCode 构建Python语法检查代码
155156
func (b *PythonCodeBuilder) BuildSyntaxCheckCode(userCode string) string {
156157
// 使用模板构建语法检查代码
157158
syntaxCheckTemplate := templates.PythonSyntaxCheckTemplate
158-
159+
159160
// 转义用户代码中的特殊字符,确保能正确嵌入到三引号字符串中
160161
escapedCode := strings.ReplaceAll(userCode, "\\", "\\\\")
161162
escapedCode = strings.ReplaceAll(escapedCode, `"""`, `\"\"\"`)
162-
163+
163164
// 替换return_val函数占位符 - 现在从runtime获取
164165
syntaxCheckCode := strings.Replace(syntaxCheckTemplate, "{{RETURN_VAL_FUNCTION}}", b.getReturnValFunctionFromRuntime(), 1)
165-
166+
166167
// 替换模板中的用户代码占位符
167168
syntaxCheckCode = strings.Replace(syntaxCheckCode, "{{USER_CODE}}", escapedCode, 1)
168-
169+
169170
return syntaxCheckCode
170171
}
171172

@@ -209,4 +210,4 @@ func (b *PythonCodeBuilder) buildInputData(input *entity.EvaluatorInputData) (ma
209210
}
210211

211212
return inputData, nil
212-
}
213+
}

0 commit comments

Comments
 (0)