()
| 79142 | }); |
| 79143 | } |
| 79144 | function getResponseStream(inputStream) { |
| 79145 | const reader = inputStream.getReader(); |
| 79146 | const stream4 = new ReadableStream({ |
| 79147 | start(controller) { |
| 79148 | let currentText = ""; |
| 79149 | return pump(); |
| 79150 | function pump() { |
| 79151 | return reader.read().then(({ value, done }) => { |
| 79152 | if (done) { |
| 79153 | if (currentText.trim()) { |
| 79154 | controller.error(new GoogleGenerativeAIError("Failed to parse stream")); |
| 79155 | return; |
| 79156 | } |
| 79157 | controller.close(); |
| 79158 | return; |
| 79159 | } |
| 79160 | currentText += value; |
| 79161 | let match = currentText.match(responseLineRE); |
| 79162 | let parsedResponse; |
| 79163 | while (match) { |
| 79164 | try { |
| 79165 | parsedResponse = JSON.parse(match[1]); |
| 79166 | } catch (e3) { |
| 79167 | controller.error(new GoogleGenerativeAIError(`Error parsing JSON response: "${match[1]}"`)); |
| 79168 | return; |
| 79169 | } |
| 79170 | controller.enqueue(parsedResponse); |
| 79171 | currentText = currentText.substring(match[0].length); |
| 79172 | match = currentText.match(responseLineRE); |
| 79173 | } |
| 79174 | return pump(); |
| 79175 | }).catch((e3) => { |
| 79176 | let err = e3; |
| 79177 | err.stack = e3.stack; |
| 79178 | if (err.name === "AbortError") { |
| 79179 | err = new GoogleGenerativeAIAbortError("Request aborted when reading from the stream"); |
| 79180 | } else { |
| 79181 | err = new GoogleGenerativeAIError("Error reading from the stream"); |
| 79182 | } |
no test coverage detected
searching dependent graphs…