| 1 | import { |
| 2 | ActionReducerMapBuilder, |
| 3 | AsyncThunk, |
| 4 | PayloadAction, |
| 5 | createSelector, |
| 6 | createSlice, |
| 7 | } from "@reduxjs/toolkit"; |
| 8 | import { JSONContent } from "@tiptap/react"; |
| 9 | import { |
| 10 | ApplyState, |
| 11 | ChatHistoryItem, |
| 12 | ChatMessage, |
| 13 | ContextItem, |
| 14 | ContextItemWithId, |
| 15 | FileSymbolMap, |
| 16 | MessageModes, |
| 17 | PromptLog, |
| 18 | RuleWithSource, |
| 19 | Session, |
| 20 | SessionMetadata, |
| 21 | ToolCallDelta, |
| 22 | ToolCallState, |
| 23 | } from "core"; |
| 24 | import { NEW_SESSION_TITLE } from "core/util/constants"; |
| 25 | import { incrementalParseJson } from "core/util/incrementalParseJson"; |
| 26 | import { renderChatMessage } from "core/util/messageContent"; |
| 27 | import { findUriInDirs, getUriPathBasename } from "core/util/uri"; |
| 28 | import { v4 as uuidv4 } from "uuid"; |
| 29 | import { RootState } from "../store"; |
| 30 | import { streamResponseThunk } from "../thunks/streamResponse"; |
| 31 | import { findCurrentToolCall, findToolCall } from "../util"; |
| 32 | |
| 33 | // We need this to handle reorderings (e.g. a mid-array deletion) of the messages array. |
| 34 | // The proper fix is adding a UUID to all chat messages, but this is the temp workaround. |
| 35 | export type ChatHistoryItemWithMessageId = ChatHistoryItem & { |
| 36 | message: ChatMessage & { id: string }; |
| 37 | }; |
| 38 | |
| 39 | type SessionState = { |
| 40 | lastSessionId?: string; |
| 41 | allSessionMetadata: SessionMetadata[]; |
| 42 | history: ChatHistoryItemWithMessageId[]; |
| 43 | isStreaming: boolean; |
| 44 | title: string; |
| 45 | id: string; |
| 46 | streamAborter: AbortController; |
| 47 | mainEditorContentTrigger?: JSONContent | undefined; |
| 48 | symbols: FileSymbolMap; |
| 49 | mode: MessageModes; |
| 50 | codeBlockApplyStates: { |
| 51 | states: ApplyState[]; |
| 52 | curIndex: number; |
| 53 | }; |
| 54 | newestToolbarPreviewForInput: Record<string, string>; |
| 55 | }; |
| 56 | |
| 57 | const initialState: SessionState = { |
| 58 | allSessionMetadata: [], |
| 59 | history: [], |
| 60 | isStreaming: false, |
| 61 | title: NEW_SESSION_TITLE, |
| 62 | id: uuidv4(), |
| 63 | streamAborter: new AbortController(), |
| 64 | symbols: {}, |
| 65 | mode: "chat", |
| 66 | codeBlockApplyStates: { |
| 67 | states: [], |
| 68 | curIndex: 0, |
| 69 | }, |
| 70 | lastSessionId: undefined, |
| 71 | newestToolbarPreviewForInput: {}, |
| 72 | }; |
| 73 | |
| 74 | export const sessionSlice = createSlice({ |
| 75 | name: "session", |
| 76 | initialState, |
| 77 | reducers: { |
| 78 | addPromptCompletionPair: ( |
| 79 | state, |
| 80 | { payload }: PayloadAction<PromptLog[]>, |
| 81 | ) => { |
| 82 | if (!state.history.length) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | const lastMessage = state.history[state.history.length - 1]; |
| 87 | |
| 88 | lastMessage.promptLogs = lastMessage.promptLogs |
| 89 | ? lastMessage.promptLogs.concat(payload) |
| 90 | : payload; |
| 91 | |
| 92 | // Inactive thinking for reasoning models when '</think>' tag is not received on request completion |
| 93 | if (lastMessage.reasoning?.active) { |
| 94 | lastMessage.reasoning.active = false; |
| 95 | lastMessage.reasoning.endAt = Date.now(); |
| 96 | } |
| 97 | }, |
| 98 | setActive: (state) => { |
| 99 | state.isStreaming = true; |
| 100 | }, |
| 101 | setIsGatheringContext: (state, { payload }: PayloadAction<boolean>) => { |
| 102 | const curMessage = state.history.at(-1); |
| 103 | if (curMessage) { |
| 104 | curMessage.isGatheringContext = payload; |
| 105 | } |
| 106 | }, |
| 107 | clearLastEmptyResponse: (state) => { |
| 108 | if (state.history.length < 2) { |
| 109 | return; |
| 110 | } |
| 111 | const lastMessage = state.history[state.history.length - 1]; |
| 112 | |
| 113 | // Only clear in the case of an empty message |
| 114 | if (!lastMessage.message.content.length) { |
| 115 | state.mainEditorContentTrigger = |
| 116 | state.history[state.history.length - 2].editorState; |
| 117 | state.history = state.history.slice(0, -2); |
| 118 | // TODO is this logic correct for tool use conversations? |
| 119 | // Maybe slice at last index of "user" role message? |
| 120 | } |
| 121 | }, |
| 122 | // Trigger value picked up by editor with isMainInput to set its content |
| 123 | setMainEditorContentTrigger: ( |
| 124 | state, |
| 125 | action: PayloadAction<JSONContent | undefined>, |
| 126 | ) => { |
| 127 | state.mainEditorContentTrigger = action.payload; |
| 128 | }, |
| 129 | updateFileSymbols: (state, action: PayloadAction<FileSymbolMap>) => { |
| 130 | state.symbols = { |
| 131 | ...state.symbols, |
| 132 | ...action.payload, |
| 133 | }; |
| 134 | }, |
| 135 | setContextItemsAtIndex: ( |
| 136 | state, |
| 137 | { |
| 138 | payload: { index, contextItems }, |
| 139 | }: PayloadAction<{ |
| 140 | index: number; |
| 141 | contextItems: ChatHistoryItem["contextItems"]; |
| 142 | }>, |
| 143 | ) => { |
| 144 | if (state.history[index]) { |
| 145 | state.history[index].contextItems = contextItems; |
| 146 | } |
| 147 | }, |
| 148 | submitEditorAndInitAtIndex: ( |
| 149 | state, |
| 150 | { |
| 151 | payload, |
| 152 | }: PayloadAction<{ |
| 153 | index: number; |
| 154 | editorState: JSONContent; |
| 155 | }>, |
| 156 | ) => { |
| 157 | const { index, editorState } = payload; |
| 158 | |
| 159 | if (state.history.length && index < state.history.length) { |
| 160 | // Resubmission - update input message, truncate history after resubmit with new empty response message |
| 161 | if (index % 2 === 1) { |
| 162 | console.warn( |
| 163 | "Corrupted history: resubmitting at odd index, shouldn't happen", |
| 164 | ); |
| 165 | } |
| 166 | const historyItem = state.history[index]; |
| 167 | |
| 168 | historyItem.message.content = ""; // IMPORTANT - this is quickly updated by resolveEditorContent based on editor state prior to streaming |
| 169 | historyItem.editorState = payload.editorState; |
| 170 | historyItem.contextItems = []; |
| 171 | |
| 172 | state.history = state.history.slice(0, index + 1).concat({ |
| 173 | message: { |
| 174 | id: uuidv4(), |
| 175 | role: "assistant", |
| 176 | content: "", // IMPORTANT - this is subsequently updated by response streaming |
| 177 | }, |
| 178 | contextItems: [], |
| 179 | }); |
| 180 | } else { |
| 181 | // New input/response messages |
| 182 | state.history = state.history.concat([ |
| 183 | { |
| 184 | message: { |
| 185 | id: uuidv4(), |
| 186 | role: "user", |
| 187 | content: "", // IMPORTANT - this is quickly updated by resolveEditorContent based on editor state prior to streaming |
| 188 | }, |
| 189 | contextItems: [], |
| 190 | editorState, |
| 191 | }, |
| 192 | { |
| 193 | message: { |
| 194 | id: uuidv4(), |
| 195 | role: "assistant", |
| 196 | content: "", // IMPORTANT - this is subsequently updated by response streaming |
| 197 | }, |
| 198 | contextItems: [], |
| 199 | }, |
| 200 | ]); |
| 201 | } |
| 202 | |
| 203 | state.isStreaming = true; |
| 204 | }, |
| 205 | deleteMessage: (state, action: PayloadAction<number>) => { |
| 206 | // Deletes the current assistant message and the previous user message |
| 207 | state.history.splice(action.payload - 1, 2); |
| 208 | }, |
| 209 | updateHistoryItemAtIndex: ( |
| 210 | state, |
| 211 | { |
| 212 | payload, |
| 213 | }: PayloadAction<{ |
| 214 | index: number; |
| 215 | updates: Partial<ChatHistoryItemWithMessageId>; |
| 216 | }>, |
| 217 | ) => { |
| 218 | const { index, updates } = payload; |
| 219 | if (index !== 0 && !state.history[index]) { |
| 220 | console.error( |
| 221 | `attempting to update history item at nonexistent index ${index}`, |
| 222 | updates, |
| 223 | ); |
| 224 | return; |
| 225 | } |
| 226 | state.history[index] = { |
| 227 | ...state.history[index], |
| 228 | ...updates, |
| 229 | }; |
| 230 | }, |
| 231 | addContextItemsAtIndex: ( |
| 232 | state, |
| 233 | { |
| 234 | payload, |
| 235 | }: PayloadAction<{ |
| 236 | index: number; |
| 237 | contextItems: ContextItemWithId[]; |
| 238 | }>, |
| 239 | ) => { |
| 240 | const historyItem = state.history[payload.index]; |
| 241 | |
| 242 | if (!historyItem) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | historyItem.contextItems = [ |
| 247 | ...historyItem.contextItems, |
| 248 | ...payload.contextItems, |
| 249 | ]; |
| 250 | }, |
| 251 | setAppliedRulesAtIndex: ( |
| 252 | state, |
| 253 | { |
| 254 | payload, |
| 255 | }: PayloadAction<{ |
| 256 | index: number; |
| 257 | appliedRules: RuleWithSource[]; |
| 258 | }>, |
| 259 | ) => { |
| 260 | if (state.history[payload.index]) { |
| 261 | state.history[payload.index].appliedRules = payload.appliedRules; |
| 262 | } |
| 263 | }, |
| 264 | setInactive: (state) => { |
| 265 | const curMessage = state.history.at(-1); |
| 266 | |
| 267 | if (curMessage) { |
| 268 | curMessage.isGatheringContext = false; |
| 269 | } |
| 270 | |
| 271 | state.isStreaming = false; |
| 272 | }, |
| 273 | abortStream: (state) => { |
| 274 | state.streamAborter.abort(); |
| 275 | state.streamAborter = new AbortController(); |
| 276 | }, |
| 277 | streamUpdate: (state, action: PayloadAction<ChatMessage[]>) => { |
| 278 | if (state.history.length) { |
| 279 | function toolCallDeltaToState( |
| 280 | toolCallDelta: ToolCallDelta, |
| 281 | ): ToolCallState { |
| 282 | const [_, parsedArgs] = incrementalParseJson( |
| 283 | toolCallDelta.function?.arguments ?? "{}", |
| 284 | ); |
| 285 | return { |
| 286 | status: "generating", |
| 287 | toolCall: { |
| 288 | id: toolCallDelta.id ?? "", |
| 289 | type: toolCallDelta.type ?? "function", |
| 290 | function: { |
| 291 | name: toolCallDelta.function?.name ?? "", |
| 292 | arguments: toolCallDelta.function?.arguments ?? "", |
| 293 | }, |
| 294 | }, |
| 295 | toolCallId: toolCallDelta.id ?? "", |
| 296 | parsedArgs, |
| 297 | }; |
| 298 | } |
| 299 | |
| 300 | for (const message of action.payload) { |
| 301 | const lastItem = state.history[state.history.length - 1]; |
| 302 | const lastMessage = lastItem.message; |
| 303 | |
| 304 | if (message.role === "thinking" && message.redactedThinking) { |
| 305 | console.log("add redacted_thinking blocks"); |
| 306 | |
| 307 | state.history.push({ |
| 308 | message: { |
| 309 | role: "thinking", |
| 310 | content: "internal reasoning is hidden due to safety reasons", |
| 311 | redactedThinking: message.redactedThinking, |
| 312 | id: uuidv4(), |
| 313 | }, |
| 314 | contextItems: [], |
| 315 | }); |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | if ( |
| 320 | lastMessage.role !== message.role || |
| 321 | // This is for when a tool call comes immediately before/after tool call |
| 322 | (lastMessage.role === "assistant" && |
| 323 | message.role === "assistant" && |
| 324 | // Last message isn't completely new |
| 325 | !(!lastMessage.toolCalls?.length && !lastMessage.content) && |
| 326 | // And there's a difference in tool call presence |
| 327 | (lastMessage.toolCalls?.length ?? 0) !== |
| 328 | (message.toolCalls?.length ?? 0)) |
| 329 | ) { |
| 330 | // Create a new message |
| 331 | const historyItem: ChatHistoryItemWithMessageId = { |
| 332 | message: { |
| 333 | ...message, |
| 334 | content: renderChatMessage(message), |
| 335 | id: uuidv4(), |
| 336 | }, |
| 337 | contextItems: [], |
| 338 | }; |
| 339 | if (message.role === "assistant" && message.toolCalls?.[0]) { |
| 340 | const toolCallDelta = message.toolCalls[0]; |
| 341 | if ( |
| 342 | !( |
| 343 | toolCallDelta.id && |
| 344 | toolCallDelta.function?.arguments !== undefined && |
| 345 | toolCallDelta.function?.name && |
| 346 | toolCallDelta.type |
| 347 | ) |
| 348 | ) { |
| 349 | console.warn( |
| 350 | "Received streamed tool call without required fields", |
| 351 | toolCallDelta, |
| 352 | ); |
| 353 | } |
| 354 | |
| 355 | historyItem.toolCallState = toolCallDeltaToState(toolCallDelta); |
| 356 | } |
| 357 | state.history.push(historyItem); |
| 358 | } else { |
| 359 | // Add to the existing message |
| 360 | if (message.content) { |
| 361 | const messageContent = renderChatMessage(message); |
| 362 | if (messageContent.includes("<think>")) { |
| 363 | lastItem.reasoning = { |
| 364 | startAt: Date.now(), |
| 365 | active: true, |
| 366 | text: messageContent.replace("<think>", "").trim(), |
| 367 | }; |
| 368 | } else if ( |
| 369 | lastItem.reasoning?.active && |
| 370 | messageContent.includes("</think>") |
| 371 | ) { |
| 372 | const [reasoningEnd, answerStart] = |
| 373 | messageContent.split("</think>"); |
| 374 | lastItem.reasoning.text += reasoningEnd.trimEnd(); |
| 375 | lastItem.reasoning.active = false; |
| 376 | lastItem.reasoning.endAt = Date.now(); |
| 377 | lastMessage.content += answerStart.trimStart(); |
| 378 | } else if (lastItem.reasoning?.active) { |
| 379 | lastItem.reasoning.text += messageContent; |
| 380 | } else { |
| 381 | // Note this only works because new message above |
| 382 | // was already rendered from parts to string |
| 383 | lastMessage.content += messageContent; |
| 384 | } |
| 385 | } else if (message.role === "thinking" && message.signature) { |
| 386 | if (lastMessage.role === "thinking") { |
| 387 | console.log("add signature", message.signature); |
| 388 | lastMessage.signature = message.signature; |
| 389 | } |
| 390 | } else if ( |
| 391 | message.role === "assistant" && |
| 392 | message.toolCalls?.[0] && |
| 393 | lastMessage.role === "assistant" |
| 394 | ) { |
| 395 | // Intentionally only supporting one tool call for now. |
| 396 | const toolCallDelta = message.toolCalls[0]; |
| 397 | |
| 398 | // Update message tool call with delta data |
| 399 | const newArgs = |
| 400 | (lastMessage.toolCalls?.[0]?.function?.arguments ?? "") + |
| 401 | (toolCallDelta.function?.arguments ?? ""); |
| 402 | if (lastMessage.toolCalls?.[0]) { |
| 403 | lastMessage.toolCalls[0].function = { |
| 404 | name: |
| 405 | toolCallDelta.function?.name ?? |
| 406 | lastMessage.toolCalls[0].function?.name ?? |
| 407 | "", |
| 408 | arguments: newArgs, |
| 409 | }; |
| 410 | } else { |
| 411 | lastMessage.toolCalls = [toolCallDelta]; |
| 412 | } |
| 413 | |
| 414 | // Update current tool call state |
| 415 | if (!lastItem.toolCallState) { |
| 416 | console.warn( |
| 417 | "Received streamed tool call response prior to initial tool call delta", |
| 418 | ); |
| 419 | lastItem.toolCallState = toolCallDeltaToState(toolCallDelta); |
| 420 | } |
| 421 | |
| 422 | const [_, parsedArgs] = incrementalParseJson(newArgs); |
| 423 | lastItem.toolCallState.parsedArgs = parsedArgs; |
| 424 | lastItem.toolCallState.toolCall.function.arguments = newArgs; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | }, |
| 430 | newSession: (state, { payload }: PayloadAction<Session | undefined>) => { |
| 431 | state.lastSessionId = state.id; |
| 432 | |
| 433 | state.streamAborter.abort(); |
| 434 | state.streamAborter = new AbortController(); |
| 435 | |
| 436 | state.isStreaming = false; |
| 437 | state.symbols = {}; |
| 438 | |
| 439 | if (payload) { |
| 440 | state.history = payload.history as any; |
| 441 | state.title = payload.title; |
| 442 | state.id = payload.sessionId; |
| 443 | } else { |
| 444 | state.history = []; |
| 445 | state.title = NEW_SESSION_TITLE; |
| 446 | state.id = uuidv4(); |
| 447 | } |
| 448 | }, |
| 449 | updateSessionTitle: (state, { payload }: PayloadAction<string>) => { |
| 450 | state.title = payload; |
| 451 | }, |
| 452 | setAllSessionMetadata: ( |
| 453 | state, |
| 454 | { payload }: PayloadAction<SessionMetadata[]>, |
| 455 | ) => { |
| 456 | state.allSessionMetadata = payload; |
| 457 | }, |
| 458 | ////////////////////////////////////////////////////////////////////////////////// |
| 459 | // These are for optimistic session metadata updates, especially for History page |
| 460 | addSessionMetadata: ( |
| 461 | state, |
| 462 | { payload }: PayloadAction<SessionMetadata>, |
| 463 | ) => { |
| 464 | state.allSessionMetadata = [...state.allSessionMetadata, payload]; |
| 465 | }, |
| 466 | updateSessionMetadata: ( |
| 467 | state, |
| 468 | { |
| 469 | payload, |
| 470 | }: PayloadAction< |
| 471 | { |
| 472 | sessionId: string; |
| 473 | } & Partial<SessionMetadata> |
| 474 | >, |
| 475 | ) => { |
| 476 | state.allSessionMetadata = state.allSessionMetadata.map((session) => |
| 477 | session.sessionId === payload.sessionId |
| 478 | ? { |
| 479 | ...session, |
| 480 | ...payload, |
| 481 | } |
| 482 | : session, |
| 483 | ); |
| 484 | if (payload.title && payload.sessionId === state.id) { |
| 485 | state.title = payload.title; |
| 486 | } |
| 487 | }, |
| 488 | deleteSessionMetadata: (state, { payload }: PayloadAction<string>) => { |
| 489 | // Note, should not be allowed to delete current session from chat session |
| 490 | state.allSessionMetadata = state.allSessionMetadata.filter( |
| 491 | (session) => session.sessionId !== payload, |
| 492 | ); |
| 493 | }, |
| 494 | ////////////////////////////////////////////////////////////////////////////////// |
| 495 | addHighlightedCode: ( |
| 496 | state, |
| 497 | { |
| 498 | payload, |
| 499 | }: PayloadAction<{ rangeInFileWithContents: any; edit: boolean }>, |
| 500 | ) => { |
| 501 | let contextItems = |
| 502 | state.history[state.history.length - 1].contextItems ?? []; |
| 503 | |
| 504 | contextItems = contextItems.map((item) => { |
| 505 | return { ...item, editing: false }; |
| 506 | }); |
| 507 | |
| 508 | const { relativePathOrBasename } = findUriInDirs( |
| 509 | payload.rangeInFileWithContents.filepath, |
| 510 | window.workspacePaths ?? [], |
| 511 | ); |
| 512 | const fileName = getUriPathBasename( |
| 513 | payload.rangeInFileWithContents.filepath, |
| 514 | ); |
| 515 | |
| 516 | const lineNums = `(${ |
| 517 | payload.rangeInFileWithContents.range.start.line + 1 |
| 518 | }-${payload.rangeInFileWithContents.range.end.line + 1})`; |
| 519 | |
| 520 | contextItems.push({ |
| 521 | name: `${fileName} ${lineNums}`, |
| 522 | description: relativePathOrBasename, |
| 523 | id: { |
| 524 | providerTitle: "code", |
| 525 | itemId: uuidv4(), |
| 526 | }, |
| 527 | content: payload.rangeInFileWithContents.contents, |
| 528 | editing: true, |
| 529 | editable: true, |
| 530 | uri: { |
| 531 | type: "file", |
| 532 | value: payload.rangeInFileWithContents.filepath, |
| 533 | }, |
| 534 | }); |
| 535 | |
| 536 | state.history[state.history.length - 1].contextItems = contextItems; |
| 537 | }, |
| 538 | updateApplyState: (state, { payload }: PayloadAction<ApplyState>) => { |
| 539 | const applyState = state.codeBlockApplyStates.states.find( |
| 540 | (state) => state.streamId === payload.streamId, |
| 541 | ); |
| 542 | |
| 543 | if (!applyState) { |
| 544 | state.codeBlockApplyStates.states.push(payload); |
| 545 | } else { |
| 546 | applyState.status = payload.status ?? applyState.status; |
| 547 | applyState.numDiffs = payload.numDiffs ?? applyState.numDiffs; |
| 548 | applyState.filepath = payload.filepath ?? applyState.filepath; |
| 549 | } |
| 550 | |
| 551 | if (payload.status === "done") { |
| 552 | state.codeBlockApplyStates.curIndex++; |
| 553 | } |
| 554 | }, |
| 555 | resetNextCodeBlockToApplyIndex: (state) => { |
| 556 | state.codeBlockApplyStates.curIndex = 0; |
| 557 | }, |
| 558 | |
| 559 | // TOOL CALL STATE |
| 560 | setToolGenerated: ( |
| 561 | state, |
| 562 | action: PayloadAction<{ |
| 563 | toolCallId: string; |
| 564 | }>, |
| 565 | ) => { |
| 566 | const toolCallState = findToolCall( |
| 567 | state.history, |
| 568 | action.payload.toolCallId, |
| 569 | ); |
| 570 | if (toolCallState) { |
| 571 | toolCallState.status = "generated"; |
| 572 | } |
| 573 | }, |
| 574 | updateToolCallOutput: ( |
| 575 | state, |
| 576 | action: PayloadAction<{ |
| 577 | toolCallId: string; |
| 578 | contextItems: ContextItem[]; |
| 579 | }>, |
| 580 | ) => { |
| 581 | const toolCallState = findToolCall( |
| 582 | state.history, |
| 583 | action.payload.toolCallId, |
| 584 | ); |
| 585 | if (toolCallState) { |
| 586 | toolCallState.output = action.payload.contextItems; |
| 587 | } |
| 588 | }, |
| 589 | cancelToolCall: ( |
| 590 | state, |
| 591 | action: PayloadAction<{ |
| 592 | toolCallId: string; |
| 593 | }>, |
| 594 | ) => { |
| 595 | const toolCallState = findToolCall( |
| 596 | state.history, |
| 597 | action.payload.toolCallId, |
| 598 | ); |
| 599 | if (toolCallState) { |
| 600 | toolCallState.status = "canceled"; |
| 601 | } |
| 602 | }, |
| 603 | errorToolCall: ( |
| 604 | state, |
| 605 | action: PayloadAction<{ |
| 606 | toolCallId: string; |
| 607 | }>, |
| 608 | ) => { |
| 609 | const toolCallState = findToolCall( |
| 610 | state.history, |
| 611 | action.payload.toolCallId, |
| 612 | ); |
| 613 | if (toolCallState) { |
| 614 | toolCallState.status = "errored"; |
| 615 | } |
| 616 | }, |
| 617 | acceptToolCall: ( |
| 618 | state, |
| 619 | action: PayloadAction<{ |
| 620 | toolCallId: string; |
| 621 | }>, |
| 622 | ) => { |
| 623 | const toolCallState = findToolCall( |
| 624 | state.history, |
| 625 | action.payload.toolCallId, |
| 626 | ); |
| 627 | if (toolCallState) { |
| 628 | toolCallState.status = "done"; |
| 629 | } |
| 630 | }, |
| 631 | setToolCallCalling: ( |
| 632 | state, |
| 633 | action: PayloadAction<{ |
| 634 | toolCallId: string; |
| 635 | }>, |
| 636 | ) => { |
| 637 | const toolCallState = findToolCall( |
| 638 | state.history, |
| 639 | action.payload.toolCallId, |
| 640 | ); |
| 641 | if (toolCallState) { |
| 642 | toolCallState.status = "calling"; |
| 643 | } |
| 644 | }, |
| 645 | setMode: (state, action: PayloadAction<MessageModes>) => { |
| 646 | state.mode = action.payload; |
| 647 | }, |
| 648 | setNewestToolbarPreviewForInput: ( |
| 649 | state, |
| 650 | { |
| 651 | payload, |
| 652 | }: PayloadAction<{ |
| 653 | inputId: string; |
| 654 | contextItemId: string; |
| 655 | }>, |
| 656 | ) => { |
| 657 | state.newestToolbarPreviewForInput[payload.inputId] = |
| 658 | payload.contextItemId; |
| 659 | }, |
| 660 | }, |
| 661 | selectors: { |
| 662 | selectIsGatheringContext: (state) => { |
| 663 | const curHistoryItem = state.history.at(-1); |
| 664 | return curHistoryItem?.isGatheringContext || false; |
| 665 | }, |
| 666 | }, |
| 667 | extraReducers: (builder) => { |
| 668 | addPassthroughCases(builder, [streamResponseThunk]); |
| 669 | }, |
| 670 | }); |
| 671 | |
| 672 | function addPassthroughCases( |
| 673 | builder: ActionReducerMapBuilder<SessionState>, |
| 674 | thunks: AsyncThunk<any, any, any>[], |
| 675 | ) { |
| 676 | thunks.forEach((thunk) => { |
| 677 | builder |
| 678 | .addCase(thunk.fulfilled, (state, action) => {}) |
| 679 | .addCase(thunk.rejected, (state, action) => {}) |
| 680 | .addCase(thunk.pending, (state, action) => {}); |
| 681 | }); |
| 682 | } |
| 683 | |
| 684 | export const selectCurrentToolCall = createSelector( |
| 685 | (store: RootState) => store.session.history, |
| 686 | (history) => { |
| 687 | return findCurrentToolCall(history); |
| 688 | }, |
| 689 | ); |
| 690 | |
| 691 | export const selectApplyStateByStreamId = createSelector( |
| 692 | [ |
| 693 | (state: RootState) => state.session.codeBlockApplyStates.states, |
| 694 | (state: RootState, streamId?: string) => streamId, |
| 695 | ], |
| 696 | (states, streamId) => { |
| 697 | return states.find((state) => state.streamId === streamId); |
| 698 | }, |
| 699 | ); |
| 700 | |
| 701 | export const selectApplyStateByToolCallId = createSelector( |
| 702 | [ |
| 703 | (state: RootState) => state.session.codeBlockApplyStates.states, |
| 704 | (state: RootState, toolCallId?: string) => toolCallId, |
| 705 | ], |
| 706 | (states, toolCallId) => { |
| 707 | return states.find((state) => state.toolCallId === toolCallId); |
| 708 | }, |
| 709 | ); |
| 710 | |
| 711 | export const { |
| 712 | updateFileSymbols, |
| 713 | setContextItemsAtIndex, |
| 714 | addContextItemsAtIndex, |
| 715 | setAppliedRulesAtIndex, |
| 716 | setInactive, |
| 717 | streamUpdate, |
| 718 | newSession, |
| 719 | updateSessionTitle, |
| 720 | addHighlightedCode, |
| 721 | addPromptCompletionPair, |
| 722 | setActive, |
| 723 | submitEditorAndInitAtIndex, |
| 724 | updateHistoryItemAtIndex, |
| 725 | clearLastEmptyResponse, |
| 726 | setMainEditorContentTrigger, |
| 727 | deleteMessage, |
| 728 | setIsGatheringContext, |
| 729 | resetNextCodeBlockToApplyIndex, |
| 730 | updateApplyState, |
| 731 | abortStream, |
| 732 | setToolCallCalling, |
| 733 | cancelToolCall, |
| 734 | errorToolCall, |
| 735 | acceptToolCall, |
| 736 | setToolGenerated, |
| 737 | updateToolCallOutput, |
| 738 | setMode, |
| 739 | setAllSessionMetadata, |
| 740 | addSessionMetadata, |
| 741 | updateSessionMetadata, |
| 742 | deleteSessionMetadata, |
| 743 | setNewestToolbarPreviewForInput, |
| 744 | } = sessionSlice.actions; |
| 745 | |
| 746 | export const { selectIsGatheringContext } = sessionSlice.selectors; |
| 747 | |
| 748 | export default sessionSlice.reducer; |