back 2 basics
This commit is contained in:
parent
f684f48cbe
commit
86ebb3f424
@ -21,43 +21,19 @@ async function sendGen() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function readResponse(response) {
|
async function readResponse(response) {
|
||||||
|
console.log(response);
|
||||||
const reader = response.body.getReader();
|
const reader = response.body.getReader();
|
||||||
const decoder = new TextDecoder('utf-8');
|
const decoder = new TextDecoder('utf-8');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Recursive function to read the stream
|
// Recursive function to read the stream
|
||||||
function read() {
|
reader.read().then(( done, value ) => {
|
||||||
reader.read().then(({ value, done }) => {
|
console.log(`Done: ${done}`);
|
||||||
|
console.log(`Value: ${value}`);
|
||||||
if (done) {
|
if (done) {
|
||||||
console.log("Stream complete");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the stream chunk and split into lines
|
|
||||||
const chunk = decoder.decode(value, { stream: true });
|
|
||||||
const lines = chunk.split(/\r?\n/); // Split by newlines
|
|
||||||
|
|
||||||
// Process each line
|
|
||||||
for (const line of lines) {
|
|
||||||
if (line.trim()) { // Skip empty lines
|
|
||||||
try {
|
|
||||||
const json = JSON.parse(line); // Parse each line as JSON
|
|
||||||
if (json.response) {
|
|
||||||
outputArea.value += json.response; // Append response text to the outputArea
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Failed to parse JSON:", err, line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Continue reading
|
|
||||||
read();
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// Start reading
|
|
||||||
read();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
outputArea.innerHTML = `<span class="text-red-500">Error: ${error.message}</span>`;
|
outputArea.innerHTML = `<span class="text-red-500">Error: ${error.message}</span>`;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user