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