@@ -63,6 +63,8 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
63
63
const totalMessageContent = messages . reduce ( ( acc , message ) => acc + message . content , '' ) ;
64
64
logger . debug ( `Total message length: ${ totalMessageContent . split ( ' ' ) . length } , words` ) ;
65
65
66
+ let lastChunk : string | undefined = undefined ;
67
+
66
68
const dataStream = createDataStream ( {
67
69
async execute ( dataStream ) {
68
70
const filePaths = getFilePaths ( files || { } ) ;
@@ -247,15 +249,42 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
247
249
}
248
250
}
249
251
} ) ( ) ;
250
-
251
252
result . mergeIntoDataStream ( dataStream ) ;
252
253
} ,
253
254
onError : ( error : any ) => `Custom error: ${ error . message } ` ,
254
255
} ) . pipeThrough (
255
256
new TransformStream ( {
256
257
transform : ( chunk , controller ) => {
258
+ if ( ! lastChunk ) {
259
+ lastChunk = ' ' ;
260
+ }
261
+
262
+ if ( typeof chunk === 'string' ) {
263
+ if ( chunk . startsWith ( 'g' ) && ! lastChunk . startsWith ( 'g' ) ) {
264
+ controller . enqueue ( encoder . encode ( `0: "<div class=\\"__boltThought__\\">"\n` ) ) ;
265
+ }
266
+
267
+ if ( lastChunk . startsWith ( 'g' ) && ! chunk . startsWith ( 'g' ) ) {
268
+ controller . enqueue ( encoder . encode ( `0: "</div>\\n"\n` ) ) ;
269
+ }
270
+ }
271
+
272
+ lastChunk = chunk ;
273
+
274
+ let transformedChunk = chunk ;
275
+
276
+ if ( typeof chunk === 'string' && chunk . startsWith ( 'g' ) ) {
277
+ let content = chunk . split ( ':' ) . slice ( 1 ) . join ( ':' ) ;
278
+
279
+ if ( content . endsWith ( '\n' ) ) {
280
+ content = content . slice ( 0 , content . length - 1 ) ;
281
+ }
282
+
283
+ transformedChunk = `0:${ content } \n` ;
284
+ }
285
+
257
286
// Convert the string stream to a byte stream
258
- const str = typeof chunk === 'string' ? chunk : JSON . stringify ( chunk ) ;
287
+ const str = typeof transformedChunk === 'string' ? transformedChunk : JSON . stringify ( transformedChunk ) ;
259
288
controller . enqueue ( encoder . encode ( str ) ) ;
260
289
} ,
261
290
} ) ,
0 commit comments