Hansimov commited on
Commit
a7bf5ad
1 Parent(s): 09f5b61

:boom: [Fix] handle_read_stream_data(): use recursive to replace while(true) to avoid page stuck if stop inproperly

Browse files
Files changed (1) hide show
  1. 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
- let buffer = "";
100
- while (true) {
101
- const { done, value } = await reader.read();
102
- if (done && buffer.length === 0) {
103
- break;
104
- }
105
- buffer += done ? "" : stringify_stream_bytes(value);
106
- let new_line_index;
107
- while ((new_line_index = buffer.indexOf("\n")) !== -1) {
108
- let json_line = buffer.slice(0, new_line_index);
109
- buffer = buffer.slice(new_line_index + 1);
110
- try {
111
- let json_chunks = jsonize_stream_data(json_line);
112
- if (!this.content_displayer_updater) {
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();