| 50 | } |
| 51 | |
| 52 | const write_json = async () => { |
| 53 | try { |
| 54 | while (true) { |
| 55 | const { done, value } = await reader.read() |
| 56 | |
| 57 | if (done) { |
| 58 | const result_block = JSON.parse(tempResult) |
| 59 | if (result_block.code === 500) { |
| 60 | return Promise.reject(result_block.message) |
| 61 | } else { |
| 62 | if (result_block.content) { |
| 63 | ChatManagement.append(chat.id, result_block.content) |
| 64 | } |
| 65 | } |
| 66 | ChatManagement.close(chat.id) |
| 67 | return |
| 68 | } |
| 69 | |
| 70 | if (value) { |
| 71 | const decoder = new TextDecoder('utf-8') |
| 72 | tempResult += decoder.decode(value) |
| 73 | } |
| 74 | } |
| 75 | } catch (e) { |
| 76 | return Promise.reject(e) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return stream ? write_stream : write_json |
| 81 | } |