:boom: [Fix] handle_read_stream_data(): use recursive to replace while(true) to avoid page stuck if stop inproperly
Browse files- networks/llm_requester.js +19 -21
networks/llm_requester.js
CHANGED
@@ -95,30 +95,28 @@ export class ChatCompletionsRequester {
|
|
95 |
nickname: `${this.agent_info.name} (${this.model})`,
|
96 |
});
|
97 |
}
|
98 |
-
async handle_read_stream_data(reader) {
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
let new_line_index;
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
this.content_displayer_updater =
|
114 |
-
new ContentDisplayerUpdater();
|
115 |
-
}
|
116 |
-
update_message(json_chunks, this.content_displayer_updater);
|
117 |
-
} catch (e) {
|
118 |
-
console.warn("Invalid JSON:", json_line);
|
119 |
}
|
|
|
|
|
|
|
120 |
}
|
121 |
}
|
|
|
122 |
}
|
123 |
async post() {
|
124 |
this.construct_request_params();
|
|
|
95 |
nickname: `${this.agent_info.name} (${this.model})`,
|
96 |
});
|
97 |
}
|
98 |
+
async handle_read_stream_data(reader, buffer = "") {
|
99 |
+
const { done, value } = await reader.read();
|
100 |
+
if (done && buffer.length === 0) {
|
101 |
+
return;
|
102 |
+
}
|
103 |
+
buffer += done ? "" : stringify_stream_bytes(value);
|
104 |
+
let new_line_index;
|
105 |
+
while ((new_line_index = buffer.indexOf("\n")) !== -1) {
|
106 |
+
let json_line = buffer.slice(0, new_line_index);
|
107 |
+
buffer = buffer.slice(new_line_index + 1);
|
108 |
+
try {
|
109 |
+
let json_chunks = jsonize_stream_data(json_line);
|
110 |
+
if (!this.content_displayer_updater) {
|
111 |
+
this.content_displayer_updater =
|
112 |
+
new ContentDisplayerUpdater();
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
}
|
114 |
+
update_message(json_chunks, this.content_displayer_updater);
|
115 |
+
} catch (e) {
|
116 |
+
console.warn("Invalid JSON:", json_line);
|
117 |
}
|
118 |
}
|
119 |
+
return this.handle_read_stream_data(reader, buffer);
|
120 |
}
|
121 |
async post() {
|
122 |
this.construct_request_params();
|