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