Files
claude-relay-service/src/utils/sseParser.js
曾庆雷 7a6c287a7e 修复标准Gemini API流式响应的缓冲区和解析问题
- 新增通用SSE解析器(src/utils/sseParser.js)
- 添加streamBuffer处理TCP数据包分割
- 统一两种API方式的SSE解析逻辑
- 记录解析失败和usage缺失的详细日志
2025-11-14 11:17:14 +08:00

53 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Server-Sent Events (SSE) 解析工具
*
* 用于解析标准 SSE 格式的数据流
* 当前主要用于 Gemini API 的流式响应处理
*
* @module sseParser
*/
/**
* 解析单行 SSE 数据
*
* @param {string} line - SSE 格式的行(如:"data: {json}\n"
* @returns {Object} 解析结果
* @returns {'data'|'control'|'other'|'invalid'} .type - 行类型
* @returns {Object|null} .data - 解析后的 JSON 数据(仅 type='data' 时)
* @returns {string} .line - 原始行内容
* @returns {string} [.jsonStr] - JSON 字符串
* @returns {Error} [.error] - 解析错误(仅 type='invalid' 时)
*
* @example
* // 数据行
* parseSSELine('data: {"key":"value"}')
* // => { type: 'data', data: {key: 'value'}, line: '...', jsonStr: '...' }
*
* @example
* // 控制行
* parseSSELine('data: [DONE]')
* // => { type: 'control', data: null, line: '...', jsonStr: '[DONE]' }
*/
function parseSSELine(line) {
if (!line.startsWith('data: ')) {
return { type: 'other', line, data: null }
}
const jsonStr = line.substring(6).trim()
if (!jsonStr || jsonStr === '[DONE]') {
return { type: 'control', line, data: null, jsonStr }
}
try {
const data = JSON.parse(jsonStr)
return { type: 'data', line, data, jsonStr }
} catch (e) {
return { type: 'invalid', line, data: null, jsonStr, error: e }
}
}
module.exports = {
parseSSELine
}