Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用XStream 报错:Error: The key-value separator ":" is not found in the sse line chunk! #447

Open
EternallyTm opened this issue Jan 5, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@EternallyTm
Copy link

重现步骤

const response = await fetch('/api/v1/chat/assistant/chat', {
method: 'POST',
body: JSON.stringify(chatParam),
headers: {
'Content-Type': 'application/json',
'Authorization': Bearer ${token},
'Accept': 'text/event-stream', // 告诉服务器返回流式事件
},
})
if (!response || !response.body) {
onError(new Error("请求发生异常"));
return;
}
if (!response.ok) {
onError(new Error(HTTP error! Status: ${response.status}));
}
for await (const chunk of XStream({
readableStream: response.body,
})) {
console.log(chunk);
}
使用如下代码处理接收后端流式输出,会报错:Error: The key-value separator ":" is not found in the sse line chunk!
后端请求结果如下:
data: {"message": "你好", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "!", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "作为", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "专业的", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "AI", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "助手", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": ",", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "我", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "随时", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "准备", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "回答", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "你的", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "问题", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "或", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "帮助你", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "解决问题", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "。", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "有什么", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "我可以", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "帮助", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "你的", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "吗", "id": "RTOy7TQVDAv3OIhp"}

data: {"message": "?", "id": "RTOy7TQVDAv3OIhp"}

当前行为

No response

预期行为

No response

上下文

No response

版本

1.0.4

您在哪些浏览器上遇到了这个问题?

No response

@EternallyTm EternallyTm added the bug Something isn't working label Jan 5, 2025
@YumoImer
Copy link
Collaborator

YumoImer commented Jan 6, 2025

看数据没什么问题,确认下 response 是否是以空行结尾,感觉问题容易出现在这里。

比如一个正常的 SSE 返回结尾应该是这样「line 63 为空行」:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants