mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
feat: 处理sse传输
This commit is contained in:
@@ -135,15 +135,23 @@ async function* handleStreamResponse(response, model, apiKeyId) {
|
|||||||
for await (const chunk of response.data) {
|
for await (const chunk of response.data) {
|
||||||
buffer += chunk.toString();
|
buffer += chunk.toString();
|
||||||
|
|
||||||
// 处理可能的多个 JSON 对象
|
// 处理 SSE 格式的数据
|
||||||
const lines = buffer.split('\n');
|
const lines = buffer.split('\n');
|
||||||
buffer = lines.pop() || ''; // 保留最后一个不完整的行
|
buffer = lines.pop() || ''; // 保留最后一个不完整的行
|
||||||
|
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
if (!line.trim()) continue;
|
if (!line.trim()) continue;
|
||||||
|
|
||||||
|
// 处理 SSE 格式: "data: {...}"
|
||||||
|
let jsonData = line;
|
||||||
|
if (line.startsWith('data: ')) {
|
||||||
|
jsonData = line.substring(6).trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!jsonData || jsonData === '[DONE]') continue;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(line);
|
const data = JSON.parse(jsonData);
|
||||||
|
|
||||||
// 更新使用量统计
|
// 更新使用量统计
|
||||||
if (data.usageMetadata) {
|
if (data.usageMetadata) {
|
||||||
@@ -171,7 +179,7 @@ async function* handleStreamResponse(response, model, apiKeyId) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.debug('Error parsing JSON line:', e.message);
|
logger.debug('Error parsing JSON line:', e.message, 'Line:', jsonData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -179,10 +187,17 @@ async function* handleStreamResponse(response, model, apiKeyId) {
|
|||||||
// 处理剩余的 buffer
|
// 处理剩余的 buffer
|
||||||
if (buffer.trim()) {
|
if (buffer.trim()) {
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(buffer);
|
let jsonData = buffer.trim();
|
||||||
const openaiResponse = convertGeminiResponse(data, model, true);
|
if (jsonData.startsWith('data: ')) {
|
||||||
if (openaiResponse) {
|
jsonData = jsonData.substring(6).trim();
|
||||||
yield `data: ${JSON.stringify(openaiResponse)}\n\n`;
|
}
|
||||||
|
|
||||||
|
if (jsonData && jsonData !== '[DONE]') {
|
||||||
|
const data = JSON.parse(jsonData);
|
||||||
|
const openaiResponse = convertGeminiResponse(data, model, true);
|
||||||
|
if (openaiResponse) {
|
||||||
|
yield `data: ${JSON.stringify(openaiResponse)}\n\n`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.debug('Error parsing final buffer:', e.message);
|
logger.debug('Error parsing final buffer:', e.message);
|
||||||
|
|||||||
Reference in New Issue
Block a user