Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
@@ -82,33 +82,30 @@ app.post('/gen', async (req, res) => {
|
|
82 |
responseType: 'stream',
|
83 |
});
|
84 |
|
85 |
-
let
|
|
|
86 |
response.data.on('data', (chunk) => {
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
});
|
89 |
|
90 |
await new Promise((resolve) => {
|
91 |
response.data.on('end', () => {
|
|
|
92 |
resolve();
|
93 |
});
|
94 |
});
|
95 |
-
|
96 |
-
const jsonData = JSON.parse(fullData);
|
97 |
-
res.json({ content: jsonData.candidates[0].content.parts.map(part => part.text).join('') });
|
98 |
-
|
99 |
-
/*
|
100 |
-
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
|
101 |
-
console.log(response.data);
|
102 |
-
|
103 |
-
const content = JSON.parse(response.data);
|
104 |
-
res.json({ content: content.candidates[0].content.parts.map(part => part.text).join('') });
|
105 |
-
|
106 |
-
} else {
|
107 |
-
const content = response.data;
|
108 |
-
console.log(content);
|
109 |
-
res.json({ content: '+ошибка+❌ Не удалось прочитать ответ.-ошибка-' });
|
110 |
-
}
|
111 |
-
*/
|
112 |
} catch (error) {
|
113 |
console.error(error);
|
114 |
res.json({ content: '+ошибка+❌ Произошла ошибка сервера при генерации.-ошибка-' });
|
|
|
82 |
responseType: 'stream',
|
83 |
});
|
84 |
|
85 |
+
let fullContent = '';
|
86 |
+
|
87 |
response.data.on('data', (chunk) => {
|
88 |
+
const chunkString = chunk.toString();
|
89 |
+
// Разбиваем на строки, убираем пустые строки
|
90 |
+
const dataLines = chunkString.split('\n').filter(line => line.trim() !== '');
|
91 |
+
dataLines.forEach(line => {
|
92 |
+
try {
|
93 |
+
// Парсим строку как JSON
|
94 |
+
const jsonData = JSON.parse(line.trim().substring(6));
|
95 |
+
const textPart = jsonData.candidates[0].content.parts.map(part => part.text).join('');
|
96 |
+
fullContent += textPart;
|
97 |
+
} catch (error) {
|
98 |
+
console.error('Ошибка парсинга фрагмента:', error, line); // Выводим строку для отладки
|
99 |
+
}
|
100 |
+
});
|
101 |
});
|
102 |
|
103 |
await new Promise((resolve) => {
|
104 |
response.data.on('end', () => {
|
105 |
+
res.json({ content: fullContent });
|
106 |
resolve();
|
107 |
});
|
108 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
} catch (error) {
|
110 |
console.error(error);
|
111 |
res.json({ content: '+ошибка+❌ Произошла ошибка сервера при генерации.-ошибка-' });
|