refactor: improve SSE response handling in Playground

- Simplify event listener logic for streaming responses
- Add null-safe checks for payload content
- Optimize message generation and completion flow
This commit is contained in:
1808837298@qq.com
2025-02-10 20:24:14 +08:00
parent 70083ecd27
commit 39aacf5fb6

View File

@@ -157,20 +157,17 @@ const Playground = () => {
payload: JSON.stringify(payload),
});
source.addEventListener("message", (e) => {
if (e.data !== "[DONE]") {
let payload = JSON.parse(e.data);
// console.log("Payload: ", payload);
if (payload.choices.length === 0) {
source.close();
completeMessage();
} else {
let text = payload.choices[0].delta.content;
if (text) {
generateMockResponse(text);
}
}
} else {
// 只有收到 [DONE] 时才结束
if (e.data === "[DONE]") {
source.close();
completeMessage();
return;
}
let payload = JSON.parse(e.data);
// 检查是否有 delta content
if (payload.choices?.[0]?.delta?.content) {
generateMockResponse(payload.choices[0].delta.content);
}
});