MCPcopy Create free account
hub / github.com/astercloud/aster / parseResponse

Method parseResponse

pkg/executionplan/generator.go:290–334  ·  view source on GitHub ↗

parseResponse 解析 LLM 响应

(content string)

Source from the content-addressed store, hash-verified

288
289// parseResponse 解析 LLM 响应
290func (g *Generator) parseResponse(content string) (*ExecutionPlan, error) {
291 // 尝试直接解析 JSON
292 var planResp planResponse
293 if err := json.Unmarshal([]byte(content), &planResp); err != nil {
294 // 如果直接解析失败,尝试提取 JSON 部分
295 jsonStr, extractErr := extractJSON(content)
296 if extractErr != nil {
297 return nil, fmt.Errorf("failed to extract JSON from response: %w (original error: %w)", extractErr, err)
298 }
299 if err := json.Unmarshal([]byte(jsonStr), &planResp); err != nil {
300 return nil, fmt.Errorf("failed to parse extracted JSON: %w", err)
301 }
302 }
303
304 // 创建执行计划
305 plan := NewExecutionPlan(planResp.Description)
306
307 // 添加步骤
308 for i, stepResp := range planResp.Steps {
309 step := plan.AddStep(stepResp.ToolName, stepResp.Description, stepResp.Parameters)
310 step.Input = stepResp.Input
311
312 // 处理依赖关系
313 if len(stepResp.DependsOn) > 0 {
314 dependsOnIDs := make([]string, 0, len(stepResp.DependsOn))
315 for _, depIdx := range stepResp.DependsOn {
316 if depIdx >= 0 && depIdx < i {
317 // 获取依赖步骤的 ID
318 depStep := plan.GetStep(depIdx)
319 if depStep != nil {
320 dependsOnIDs = append(dependsOnIDs, depStep.ID)
321 }
322 }
323 }
324 step.DependsOn = dependsOnIDs
325 }
326 }
327
328 // 设置状态为待审批
329 if plan.Options != nil && plan.Options.RequireApproval && !plan.Options.AutoApprove {
330 plan.Status = StatusPendingApproval
331 }
332
333 return plan, nil
334}
335
336// extractJSON 从文本中提取 JSON 部分
337func extractJSON(text string) (string, error) {

Callers 1

GenerateMethod · 0.95

Calls 4

AddStepMethod · 0.95
GetStepMethod · 0.95
NewExecutionPlanFunction · 0.85
extractJSONFunction · 0.70

Tested by

no test coverage detected