mirror of
				https://github.com/SillyTavern/SillyTavern.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	More reliable Cohere stream parsing
This commit is contained in:
		| @@ -48,32 +48,22 @@ function postProcessPrompt(messages, type, charName, userName) { | |||||||
|  */ |  */ | ||||||
| async function parseCohereStream(jsonStream, request, response) { | async function parseCohereStream(jsonStream, request, response) { | ||||||
|     try { |     try { | ||||||
|         let partialData = ''; |  | ||||||
|         jsonStream.body.on('data', (data) => { |         jsonStream.body.on('data', (data) => { | ||||||
|             const chunk = data.toString(); |  | ||||||
|             partialData += chunk; |  | ||||||
|             while (true) { |  | ||||||
|                 let json; |  | ||||||
|             try { |             try { | ||||||
|                     json = JSON.parse(partialData); |                 const json = JSON.parse(data.toString()); | ||||||
|                 } catch (e) { |  | ||||||
|                     break; |  | ||||||
|                 } |  | ||||||
|                 if (json.message) { |                 if (json.message) { | ||||||
|                     const message = json.message || 'Unknown error'; |                     const message = json.message || 'Unknown error'; | ||||||
|                     const chunk = { error: { message: message } }; |                     const chunk = { error: { message: message } }; | ||||||
|                     response.write(`data: ${JSON.stringify(chunk)}\n\n`); |                     response.write(`data: ${JSON.stringify(chunk)}\n\n`); | ||||||
|                     partialData = ''; |  | ||||||
|                     break; |  | ||||||
|                 } else if (json.event_type === 'text-generation') { |                 } else if (json.event_type === 'text-generation') { | ||||||
|                     const text = json.text || ''; |                     const text = json.text || ''; | ||||||
|                     const chunk = { choices: [{ text }] }; |                     const chunk = { choices: [{ text }] }; | ||||||
|                     response.write(`data: ${JSON.stringify(chunk)}\n\n`); |                     response.write(`data: ${JSON.stringify(chunk)}\n\n`); | ||||||
|                     partialData = ''; |  | ||||||
|                 } else { |                 } else { | ||||||
|                     partialData = ''; |                     return; | ||||||
|                     break; |  | ||||||
|                 } |                 } | ||||||
|  |             } catch (e) { | ||||||
|  |                 // ignore | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user