API Stream mode coding for Javascript(Successful)

The API Streaming mode is very quick response I feel.
Coding by using fetch interface is little hard,but it done successful.

Stream fetch for Javascript coding
[Drive this script by Example]
(CallGPT Bench)

3 Likes

Nice

Breaking my head over 3 backtics to code /code fields

As stream part message is not always 3 backticks long

Ask: javascript for loop examples

Ace editor with ai
https://ldijkman.github.io/Ace_Seventh_Heaven/The_AfterWorld.html



User
javascript string with multiple  tripple ``` backticks replace for alternating <dode> </code>
ChatGPT
Certainly! To replace the triple backticks ``` with alternating <code> and <dode> tags in JavaScript, you can achieve this using the replace() method along with a regular expression to identify and alternate the replacements.

Here's an example:

javascript

Copy code
let inputString = "```This is a sample code block.``` Here is another ```code snippet```.";

let count = 0;
let replacedString = inputString.replace(/```/g, function(match) {
  count++;
  return count % 2 === 0 ? '</code>' : '<dode>';
});

console.log(replacedString);

This code will replace every occurrence of triple backticks ``` with alternating and tags. Adjust this logic according to your specific requirements if needed.

<p id="text"></p>

<script>
let inputString = "```This is a sample code block.``` Here is another ```code snippet```.";

let count = 0;
let replacedString = inputString.replace(/```/g, function(match) {
  count++;
  return count % 2 === 0 ? '</textarea>' : '<textarea>'; 
});

console.log(replacedString);
text.innerHTML=replacedString;
</script>

https://ldijkman.github.io/Ace_Seventh_Heaven/The_AfterWorld.html

Well done. I played around with the SSE data that comes back from body containing {stream: true} and it sure is ugly to parse it down to the useful pieces… but at least in Web it will always be a complete chunk for that JSON – in NodeJS the first chunk is usually incomplete, so requires extra handling.

PS: if you pass the “signal” property to the fetch() call you should be able to do immediate cancel button. Search MDN for AbortController.