分析流式响应结构
(body []byte, reqID string)
| 1528 | |
| 1529 | // 分析流式响应结构 |
| 1530 | func analyzeStreamStructure(body []byte, reqID string) { |
| 1531 | bodyStr := string(body) |
| 1532 | lines := strings.Split(bodyStr, "\n") |
| 1533 | |
| 1534 | // 统计各种类型的行数 |
| 1535 | dataLines := 0 |
| 1536 | jsonLines := 0 |
| 1537 | doneLines := 0 |
| 1538 | |
| 1539 | // 响应长度增长模式 |
| 1540 | responseLengths := make([]int, 0) |
| 1541 | |
| 1542 | for _, line := range lines { |
| 1543 | line = strings.TrimSpace(line) |
| 1544 | if !strings.HasPrefix(line, "data:") { |
| 1545 | continue |
| 1546 | } |
| 1547 | |
| 1548 | dataLines++ |
| 1549 | jsonStr := strings.TrimPrefix(line, "data:") |
| 1550 | jsonStr = strings.TrimSpace(jsonStr) |
| 1551 | |
| 1552 | if jsonStr == "[DONE]" { |
| 1553 | doneLines++ |
| 1554 | continue |
| 1555 | } |
| 1556 | |
| 1557 | if jsonStr == "" { |
| 1558 | continue |
| 1559 | } |
| 1560 | |
| 1561 | var obj map[string]interface{} |
| 1562 | if err := json.Unmarshal([]byte(jsonStr), &obj); err != nil { |
| 1563 | continue |
| 1564 | } |
| 1565 | |
| 1566 | jsonLines++ |
| 1567 | |
| 1568 | // 记录响应长度 |
| 1569 | if resp, ok := obj["response"]; ok { |
| 1570 | if respStr, ok := resp.(string); ok { |
| 1571 | responseLengths = append(responseLengths, len(respStr)) |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | // 分析响应长度增长模式 |
| 1577 | isIncremental := true |
| 1578 | for i := 1; i < len(responseLengths); i++ { |
| 1579 | if responseLengths[i] < responseLengths[i-1] { |
| 1580 | isIncremental = false |
| 1581 | break |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | // 打印分析结果 |
| 1586 | logInfo("[reqID:%s] 流式响应结构分析: 总行数=%d, data行数=%d, JSON行数=%d, DONE行数=%d", |
| 1587 | reqID, len(lines), dataLines, jsonLines, doneLines) |
no test coverage detected