@@ -10,21 +10,21 @@ import {
10
10
ApplyState ,
11
11
ChatHistoryItem ,
12
12
ChatMessage ,
13
+ CodeToEdit ,
14
+ ContextItem ,
13
15
ContextItemWithId ,
14
16
FileSymbolMap ,
15
- Session ,
17
+ MessageModes ,
16
18
PromptLog ,
17
- CodeToEdit ,
19
+ Session ,
18
20
ToolCall ,
19
- ContextItem ,
20
- MessageModes ,
21
21
} from "core" ;
22
22
import { incrementalParseJson } from "core/util/incrementalParseJson" ;
23
23
import { renderChatMessage } from "core/util/messageContent" ;
24
24
import { v4 as uuidv4 } from "uuid" ;
25
+ import { RootState } from "../store" ;
25
26
import { streamResponseThunk } from "../thunks/streamResponse" ;
26
27
import { findCurrentToolCall } from "../util" ;
27
- import { RootState } from "../store" ;
28
28
29
29
// We need this to handle reorderings (e.g. a mid-array deletion) of the messages array.
30
30
// The proper fix is adding a UUID to all chat messages, but this is the temp workaround.
@@ -284,72 +284,74 @@ export const sessionSlice = createSlice({
284
284
state . streamAborter . abort ( ) ;
285
285
state . streamAborter = new AbortController ( ) ;
286
286
} ,
287
- streamUpdate : ( state , action : PayloadAction < ChatMessage > ) => {
287
+ streamUpdate : ( state , action : PayloadAction < ChatMessage [ ] > ) => {
288
288
if ( state . history . length ) {
289
- const lastMessage = state . history [ state . history . length - 1 ] ;
290
-
291
- if (
292
- action . payload . role &&
293
- ( lastMessage . message . role !== action . payload . role ||
294
- // This is when a tool call comes after assistant text
295
- ( lastMessage . message . content !== "" &&
296
- action . payload . role === "assistant" &&
297
- action . payload . toolCalls ?. length ) )
298
- ) {
299
- const baseHistoryItem = getBaseHistoryItem ( ) ;
300
-
301
- // Create a new message
302
- const historyItem : ChatHistoryItemWithMessageId = {
303
- ...baseHistoryItem ,
304
- message : { ...baseHistoryItem . message , ...action . payload } ,
305
- } ;
306
-
307
- if ( action . payload . role === "assistant" && action . payload . toolCalls ) {
308
- const [ _ , parsedArgs ] = incrementalParseJson (
309
- action . payload . toolCalls [ 0 ] . function . arguments ,
310
- ) ;
311
- historyItem . toolCallState = {
312
- status : "generating" ,
313
- toolCall : action . payload . toolCalls [ 0 ] as ToolCall ,
314
- toolCallId : action . payload . toolCalls [ 0 ] . id ,
315
- parsedArgs,
289
+ for ( const message of action . payload ) {
290
+ const lastMessage = state . history [ state . history . length - 1 ] ;
291
+
292
+ if (
293
+ message . role &&
294
+ ( lastMessage . message . role !== message . role ||
295
+ // This is when a tool call comes after assistant text
296
+ ( lastMessage . message . content !== "" &&
297
+ message . role === "assistant" &&
298
+ message . toolCalls ?. length ) )
299
+ ) {
300
+ const baseHistoryItem = getBaseHistoryItem ( ) ;
301
+
302
+ // Create a new message
303
+ const historyItem : ChatHistoryItemWithMessageId = {
304
+ ...baseHistoryItem ,
305
+ message : { ...baseHistoryItem . message , ...message } ,
316
306
} ;
317
- }
318
307
319
- state . history . push ( historyItem ) ;
320
- } else {
321
- // Add to the existing message
322
- const msg = state . history [ state . history . length - 1 ] . message ;
323
- if ( action . payload . content ) {
324
- msg . content += renderChatMessage ( action . payload ) ;
325
- } else if (
326
- action . payload . role === "assistant" &&
327
- action . payload . toolCalls &&
328
- msg . role === "assistant"
329
- ) {
330
- if ( ! msg . toolCalls ) {
331
- msg . toolCalls = [ ] ;
308
+ if ( message . role === "assistant" && message . toolCalls ) {
309
+ const [ _ , parsedArgs ] = incrementalParseJson (
310
+ message . toolCalls [ 0 ] . function . arguments ,
311
+ ) ;
312
+ historyItem . toolCallState = {
313
+ status : "generating" ,
314
+ toolCall : message . toolCalls [ 0 ] as ToolCall ,
315
+ toolCallId : message . toolCalls [ 0 ] . id ,
316
+ parsedArgs,
317
+ } ;
332
318
}
333
- action . payload . toolCalls . forEach ( ( toolCall , i ) => {
334
- if ( msg . toolCalls . length <= i ) {
335
- msg . toolCalls . push ( toolCall ) ;
336
- } else {
337
- msg . toolCalls [ i ] . function . arguments +=
338
- toolCall . function . arguments ;
339
-
340
- const [ _ , parsedArgs ] = incrementalParseJson (
341
- msg . toolCalls [ i ] . function . arguments ,
342
- ) ;
343
-
344
- state . history [
345
- state . history . length - 1
346
- ] . toolCallState . parsedArgs = parsedArgs ;
347
- state . history [
348
- state . history . length - 1
349
- ] . toolCallState . toolCall . function . arguments +=
350
- toolCall . function . arguments ;
319
+
320
+ state . history . push ( historyItem ) ;
321
+ } else {
322
+ // Add to the existing message
323
+ const msg = state . history [ state . history . length - 1 ] . message ;
324
+ if ( message . content ) {
325
+ msg . content += renderChatMessage ( message ) ;
326
+ } else if (
327
+ message . role === "assistant" &&
328
+ message . toolCalls &&
329
+ msg . role === "assistant"
330
+ ) {
331
+ if ( ! msg . toolCalls ) {
332
+ msg . toolCalls = [ ] ;
351
333
}
352
- } ) ;
334
+ message . toolCalls . forEach ( ( toolCall , i ) => {
335
+ if ( msg . toolCalls . length <= i ) {
336
+ msg . toolCalls . push ( toolCall ) ;
337
+ } else {
338
+ msg . toolCalls [ i ] . function . arguments +=
339
+ toolCall . function . arguments ;
340
+
341
+ const [ _ , parsedArgs ] = incrementalParseJson (
342
+ msg . toolCalls [ i ] . function . arguments ,
343
+ ) ;
344
+
345
+ state . history [
346
+ state . history . length - 1
347
+ ] . toolCallState . parsedArgs = parsedArgs ;
348
+ state . history [
349
+ state . history . length - 1
350
+ ] . toolCallState . toolCall . function . arguments +=
351
+ toolCall . function . arguments ;
352
+ }
353
+ } ) ;
354
+ }
353
355
}
354
356
}
355
357
}
0 commit comments