<textarea id="input" placeholder="Type your message... (try: explain redux in 2 sentences)"></textarea>
<button id="send">Send</button>
</div>
<div class="meta">Tip: open devtools to see the conversation JSON (for development)</div>
</div>
// Simple chat demo with mocked streaming response
const messagesEl = document.getElementById('messages');
const input = document.getElementById('input');
const sendBtn = document.getElementById('send');
function appendMessage(text, who='bot'){
const el = document.createElement('div');
el.className = 'msg ' + who;
el.textContent = text;
messagesEl.appendChild(el);
messagesEl.scrollTop = messagesEl.scrollHeight;
return el;
}
function mockStreamResponse(prompt, onToken){
// A simple token-splitting mock for streaming
const canned = {
'hello': 'Hello! I\'m a demo bot. I can explain things, produce examples, and help structure projects.',
'explain redux': 'Redux is a predictable state container for JavaScript apps. It centralizes state in a single store and uses pure reducers to update state in response to actions.'
};
const base = Object.keys(canned).find(k => prompt.toLowerCase().includes(k)) ? canned[Object.keys(canned).find(k => prompt.toLowerCase().includes(k))] : 'I\'m a demo bot. To connect a real model, run a backend proxy to your OpenAI/LLM provider.';
let i = 0;
const interval = setInterval(()=>{
if(i >= base.length){ clearInterval(interval); return; }
onToken(base.slice(0,i+1));
i += Math.max(1, Math.floor(Math.random()*3));
}, 60);
return () => clearInterval(interval);
}
sendBtn.addEventListener('click', async ()=>{
const text = input.value.trim();
if(!text) return;
appendMessage(text,'user');
input.value = '';
// start bot placeholder
const botEl = appendMessage('...', 'bot');
let stopMock = mockStreamResponse(text, (partial)=>{
botEl.textContent = partial;
messagesEl.scrollTop = messagesEl.scrollHeight;
});
// Example: if you have a server, you can call it here
// fetch('/api/chat', {method:'POST', body: JSON.stringify({prompt:text}), headers:{'Content-Type':'application/json'}})
// .then(r=>r.json()).then(data => {/* stream or show result */})
});
input.addEventListener('keydown',...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.