@@ -2,7 +2,7 @@ import './style.css';
22import 'highlight.js/styles/github.css' ;
33import { renderMarkdown , enhanceCodeBlocksHtml } from './lib/markdown.js' ;
44import { looseJsonParse , repairModelOutput } from './lib/json-repair.js' ;
5- import { chat , image , textModels } from '../Libs/pollilib/index.js' ;
5+ import { chat , chatStream , image , textModels } from '../Libs/pollilib/index.js' ;
66import { generateSeed } from './seed.js' ;
77import { createPollinationsClient } from './pollinations-client.js' ;
88import {
@@ -281,6 +281,53 @@ function renderDebugPanel(extra = {}) {
281281 }
282282}
283283
284+ // Fast-path streaming for text-only prompts to improve perceived latency
285+ async function sendPromptStreaming ( prompt ) {
286+ const selectedModel = getSelectedModel ( ) ;
287+ if ( ! selectedModel ) throw new Error ( 'No model selected.' ) ;
288+ if ( ! client ) throw new Error ( 'Pollinations client is not ready.' ) ;
289+ const endpoints = buildEndpointSequence ( selectedModel ) ;
290+ if ( ! endpoints . length ) throw new Error ( `No endpoints available for model "${ selectedModel . label ?? selectedModel . id } ".` ) ;
291+
292+ const startingLength = state . conversation . length ;
293+ // Do NOT inject the JSON primer for streaming text-only turns
294+ state . conversation . push ( { role : 'user' , content : prompt } ) ;
295+ try {
296+ setStatus ( 'Streaming response…' ) ;
297+ const assistantMsg = addMessage ( { role : 'assistant' , type : 'text' , content : '' } ) ;
298+ const pinnedId = state . pinnedModelId || selectedModel . id ;
299+ const endpoint = endpoints [ 0 ] || 'openai' ;
300+ state . activeModel = { id : pinnedId , endpoint, info : selectedModel } ;
301+ if ( ! state . pinnedModelId ) state . pinnedModelId = pinnedId ;
302+ let streamed = '' ;
303+ try {
304+ for await ( const chunk of chatStream ( { model : pinnedId , endpoint, messages : state . conversation , seed : generateSeed ( ) } , client ) ) {
305+ if ( typeof chunk === 'string' && chunk ) {
306+ streamed += chunk ;
307+ assistantMsg . content = streamed ;
308+ renderMessages ( ) ;
309+ }
310+ }
311+ } catch ( e ) {
312+ // Fallback to existing non-stream flow
313+ console . warn ( 'Streaming failed; falling back to standard request' , e ) ;
314+ state . conversation . length = startingLength ; // revert user injection
315+ return await sendPrompt ( prompt ) ;
316+ }
317+ if ( streamed . trim ( ) ) {
318+ state . conversation . push ( { role : 'assistant' , content : streamed } ) ;
319+ if ( state . voicePlayback && els . voiceSelect . value ) {
320+ void speakMessage ( assistantMsg , { autoplay : true } ) ;
321+ }
322+ }
323+ resetStatusIfIdle ( ) ;
324+ } catch ( error ) {
325+ console . error ( 'Chat error (streaming)' , error ) ;
326+ state . conversation . length = startingLength ;
327+ throw error ;
328+ }
329+ }
330+
284331async function copyLogsToClipboard ( ) {
285332 try {
286333 const data = ( globalThis && globalThis . __PANEL_LOG__ ) || [ ] ;
@@ -1859,8 +1906,8 @@ async function generateImageAsset(prompt, { width, height, model: imageModel, se
18591906 }
18601907 const resolvedSeed = ( typeof seed === 'number' || ( typeof seed === 'string' && seed . trim ( ) . length ) ) ? seed : generateSeed ( ) ;
18611908 const dims = [ ] ;
1862- const w = Number ( width ) || 1024 ;
1863- const h = Number ( height ) || 1024 ;
1909+ const w = Number ( width ) || 768 ;
1910+ const h = Number ( height ) || 768 ;
18641911 dims . push ( [ w , h ] ) ;
18651912 if ( w > 512 || h > 512 ) dims . push ( [ 512 , 512 ] ) ;
18661913
@@ -2338,7 +2385,13 @@ els.form.addEventListener('submit', async event => {
23382385 console . info ( 'Generated Pollinations image with seed %s.' , seed ) ;
23392386 resetStatusIfIdle ( ) ;
23402387 } else {
2341- await sendPrompt ( raw ) ;
2388+ // Stream for non-image prompts to speed up perceived latency
2389+ const wantsImage = hasImageIntent ( raw ) ;
2390+ if ( ! wantsImage ) {
2391+ await sendPromptStreaming ( raw ) ;
2392+ } else {
2393+ await sendPrompt ( raw ) ;
2394+ }
23422395 }
23432396 } catch ( error ) {
23442397 console . error ( 'Submission error' , error ) ;
0 commit comments