From 86ebb3f424f8f886245212d76794fad9d60dd5b1 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 4 Jan 2025 10:42:08 -0500 Subject: [PATCH] back 2 basics --- src/assets/js/main.js | 40 ++++++++-------------------------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/src/assets/js/main.js b/src/assets/js/main.js index 1fff881..4e25e14 100644 --- a/src/assets/js/main.js +++ b/src/assets/js/main.js @@ -21,43 +21,19 @@ async function sendGen() { } async function readResponse(response) { + console.log(response); const reader = response.body.getReader(); const decoder = new TextDecoder('utf-8'); try { // Recursive function to read the stream - function read() { - reader.read().then(({ value, done }) => { - if (done) { - console.log("Stream complete"); - 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(); + reader.read().then(( done, value ) => { + console.log(`Done: ${done}`); + console.log(`Value: ${value}`); + if (done) { + return; + } + }); } catch (error) { outputArea.innerHTML = `Error: ${error.message}`; }