File size: 1,987 Bytes
9fdb075
 
 
 
 
 
 
6a87313
9fdb075
 
 
 
 
6a87313
 
 
 
 
 
 
 
9fdb075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * Code Retry Service - 工具函数
 */

/**
 * 从 AI 响应中提取代码
 */
export function extractCodeFromResponse(text: string, outputMode: 'video' | 'image' = 'video'): string {
  if (!text) return ''

  // 移除 think 标签
  const sanitized = text.replace(/<think>[\s\S]*?<\/think>/gi, '')

  if (outputMode === 'image') {
    const codeMatch = sanitized.match(/```(?:python)?([\s\S]*?)```/i)
    if (codeMatch) {
      return codeMatch[1].trim()
    }
    return sanitized.trim()
  }

  // 优先匹配锚点协议
  const anchorMatch = sanitized.match(/### START ###([\s\S]*?)### END ###/)
  if (anchorMatch) {
    return anchorMatch[1].trim()
  }

  // 匹配 Markdown 代码块
  const codeMatch = sanitized.match(/```(?:python)?([\s\S]*?)```/i)
  if (codeMatch) {
    return codeMatch[1].trim()
  }

  // 返回原始文本(去除首尾空白)
  return sanitized.trim()
}

/**
 * 从错误信息中提取错误类型
 */
export function getErrorType(stderr: string): string {
  if (!stderr) return 'Unknown'

  const errorPatterns = [
    { name: 'NameError', pattern: /NameError/i },
    { name: 'SyntaxError', pattern: /SyntaxError/i },
    { name: 'AttributeError', pattern: /AttributeError/i },
    { name: 'ImportError', pattern: /ImportError/i },
    { name: 'TypeError', pattern: /TypeError/i },
    { name: 'ValueError', pattern: /ValueError/i },
    { name: 'RuntimeError', pattern: /RuntimeError/i },
    { name: 'IndentationError', pattern: /IndentationError/i },
  ]

  for (const { name, pattern } of errorPatterns) {
    if (pattern.test(stderr)) {
      return name
    }
  }

  return 'Unknown'
}

/**
 * 从错误信息中提取完整错误描述(包含类型)
 */
export function extractErrorMessage(stderr: string): string {
  if (!stderr) return 'Unknown error'

  // 提取最后一行错误信息
  const lines = stderr.trim().split('\n')
  const lastLine = lines[lines.length - 1]?.trim()

  return lastLine || stderr.slice(0, 500)
}