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),
|
payload: JSON.stringify(payload),
|
||||||
});
|
});
|
||||||
source.addEventListener("message", (e) => {
|
source.addEventListener("message", (e) => {
|
||||||
if (e.data !== "[DONE]") {
|
// 只有收到 [DONE] 时才结束
|
||||||
let payload = JSON.parse(e.data);
|
if (e.data === "[DONE]") {
|
||||||
// console.log("Payload: ", payload);
|
source.close();
|
||||||
if (payload.choices.length === 0) {
|
|
||||||
source.close();
|
|
||||||
completeMessage();
|
|
||||||
} else {
|
|
||||||
let text = payload.choices[0].delta.content;
|
|
||||||
if (text) {
|
|
||||||
generateMockResponse(text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
completeMessage();
|
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