This commit is contained in:
renzhiyuan 2026-07-21 18:03:58 +08:00
parent 18771c745f
commit 7effc7a806
1 changed files with 40 additions and 5 deletions

View File

@ -265,15 +265,51 @@ func (s *SDKGeneratorService) anaMd(ctx context.Context, task *models.Task) (err
}
func (s *SDKGeneratorService) valid(ctx context.Context, task *models.Task) (err error) {
validRes, err := task.CallLLM.Do(ctx, prompts.GetValidatePrompt(task.Refine, task.SdkName, task.Resp))
if err != nil {
return fmt.Errorf("调用大模型失败: %v", err)
}
if validRes != "OK" {
task.Valid = validRes
// ✅ 清理返回结果
cleaned := s.cleanValidationResult(validRes)
// ✅ 判断是否为 OK
if cleaned == "OK" {
task.Valid = "OK"
return nil
}
return err
// 验证不通过,保存修复后的代码
task.Valid = validRes
return nil
}
// cleanValidationResult 清理验证结果
func (s *SDKGeneratorService) cleanValidationResult(result string) string {
// 1. 去除首尾空白
cleaned := strings.TrimSpace(result)
// 2. 去除 Markdown 代码块标记
cleaned = strings.TrimPrefix(cleaned, "```")
cleaned = strings.TrimSuffix(cleaned, "```")
cleaned = strings.TrimSpace(cleaned)
// 3. 去除可能的引号
cleaned = strings.Trim(cleaned, "\"")
cleaned = strings.Trim(cleaned, "'")
// 4. 只取前两行(防止 OK 后面跟了其他内容)
lines := strings.Split(cleaned, "\n")
if len(lines) > 0 {
cleaned = strings.TrimSpace(lines[0])
}
// 5. 去除可能的尾随标点
cleaned = strings.TrimSuffix(cleaned, ".")
cleaned = strings.TrimSuffix(cleaned, "")
cleaned = strings.TrimSuffix(cleaned, "!")
return cleaned
}
func (s *SDKGeneratorService) fix(ctx context.Context, task *models.Task, errMsg string, fixCount int) (err error) {
@ -309,7 +345,6 @@ func (s *SDKGeneratorService) creatFile(ctx context.Context, task *models.Task)
}
files, err := extractor.Extract(extract)
if err != nil {
return fmt.Errorf("提取代码失败: %v", err)
}