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:
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user