| 1 | import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; |
| 2 | import { JSONContent } from "@tiptap/core"; |
| 3 | import { InputModifiers, ToolResultChatMessage, UserChatMessage } from "core"; |
| 4 | import { constructMessages } from "core/llm/constructMessages"; |
| 5 | import { getApplicableRules } from "core/llm/rules/getSystemMessageWithRules"; |
| 6 | import posthog from "posthog-js"; |
| 7 | import { v4 as uuidv4 } from "uuid"; |
| 8 | import { resolveEditorContent } from "../../components/mainInput/TipTapEditor/utils/resolveEditorContent"; |
| 9 | import { getBaseSystemMessage } from "../../util"; |
| 10 | import { selectSelectedChatModel } from "../slices/configSlice"; |
| 11 | import { |
| 12 | setAppliedRulesAtIndex, |
| 13 | submitEditorAndInitAtIndex, |
| 14 | updateHistoryItemAtIndex, |
| 15 | } from "../slices/sessionSlice"; |
| 16 | import { ThunkApiType } from "../store"; |
| 17 | import { resetStateForNewMessage } from "./resetStateForNewMessage"; |
| 18 | import { streamNormalInput } from "./streamNormalInput"; |
| 19 | import { streamThunkWrapper } from "./streamThunkWrapper"; |
| 20 | import { updateFileSymbolsFromFiles } from "./updateFileSymbols"; |
| 21 | |
| 22 | export const streamResponseThunk = createAsyncThunk< |
| 23 | void, |
| 24 | { |
| 25 | editorState: JSONContent; |
| 26 | modifiers: InputModifiers; |
| 27 | index?: number; |
| 28 | }, |
| 29 | ThunkApiType |
| 30 | >( |
| 31 | "chat/streamResponse", |
| 32 | async ({ editorState, modifiers, index }, { dispatch, extra, getState }) => { |
| 33 | await dispatch( |
| 34 | streamThunkWrapper(async () => { |
| 35 | const state = getState(); |
| 36 | const selectedChatModel = selectSelectedChatModel(state); |
| 37 | const inputIndex = index ?? state.session.history.length; // Either given index or concat to end |
| 38 | |
| 39 | if (!selectedChatModel) { |
| 40 | throw new Error("No chat model selected"); |
| 41 | } |
| 42 | |
| 43 | dispatch( |
| 44 | submitEditorAndInitAtIndex({ index: inputIndex, editorState }), |
| 45 | ); |
| 46 | resetStateForNewMessage(); |
| 47 | |
| 48 | const defaultContextProviders = |
| 49 | state.config.config.experimental?.defaultContext ?? []; |
| 50 | |
| 51 | if (!selectedChatModel) { |
| 52 | console.error( |
| 53 | "gatherContext thunk: Cannot gather context, no model selected", |
| 54 | ); |
| 55 | throw new Error("No chat model selected"); |
| 56 | } |
| 57 | |
| 58 | // Resolve context providers and construct new history |
| 59 | const { |
| 60 | selectedContextItems, |
| 61 | selectedCode, |
| 62 | content, |
| 63 | legacyCommandWithInput, |
| 64 | } = await resolveEditorContent({ |
| 65 | editorState, |
| 66 | modifiers, |
| 67 | ideMessenger: extra.ideMessenger, |
| 68 | defaultContextProviders, |
| 69 | availableSlashCommands: state.config.config.slashCommands, |
| 70 | dispatch, |
| 71 | }); |
| 72 | |
| 73 | // symbols for both context items AND selected codeblocks |
| 74 | const filesForSymbols = [ |
| 75 | ...selectedContextItems |
| 76 | .filter((item) => item.uri?.type === "file" && item?.uri?.value) |
| 77 | .map((item) => item.uri!.value), |
| 78 | ...selectedCode.map((rif) => rif.filepath), |
| 79 | ]; |
| 80 | dispatch(updateFileSymbolsFromFiles(filesForSymbols)); |
| 81 | |
| 82 | dispatch( |
| 83 | updateHistoryItemAtIndex({ |
| 84 | index: inputIndex, |
| 85 | updates: { |
| 86 | message: { |
| 87 | role: "user", |
| 88 | content, |
| 89 | id: uuidv4(), |
| 90 | }, |
| 91 | contextItems: selectedContextItems, |
| 92 | }, |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | // Get updated history after the update |
| 97 | const updatedHistory = getState().session.history; |
| 98 | |
| 99 | // Determine which rules apply to this message |
| 100 | const userMsg = updatedHistory[inputIndex].message; |
| 101 | const rules = getState().config.config.rules; |
| 102 | const rulePolicies = getState().ui.ruleSettings; |
| 103 | |
| 104 | // Calculate applicable rules once |
| 105 | // We need to check the message type to match what getApplicableRules expects |
| 106 | const applicableRules = getApplicableRules( |
| 107 | userMsg.role === "user" || userMsg.role === "tool" |
| 108 | ? (userMsg as UserChatMessage | ToolResultChatMessage) |
| 109 | : undefined, |
| 110 | rules, |
| 111 | selectedContextItems, |
| 112 | rulePolicies, |
| 113 | ); |
| 114 | |
| 115 | // Store in history for UI display |
| 116 | dispatch( |
| 117 | setAppliedRulesAtIndex({ |
| 118 | index: inputIndex, |
| 119 | appliedRules: applicableRules, |
| 120 | }), |
| 121 | ); |
| 122 | |
| 123 | const messageMode = getState().session.mode; |
| 124 | const baseChatOrAgentSystemMessage = getBaseSystemMessage( |
| 125 | selectedChatModel, |
| 126 | messageMode, |
| 127 | ); |
| 128 | |
| 129 | const messages = constructMessages( |
| 130 | messageMode, |
| 131 | [...updatedHistory], |
| 132 | baseChatOrAgentSystemMessage, |
| 133 | applicableRules, |
| 134 | rulePolicies, |
| 135 | ); |
| 136 | |
| 137 | posthog.capture("step run", { |
| 138 | step_name: "User Input", |
| 139 | params: {}, |
| 140 | }); |
| 141 | posthog.capture("userInput", {}); |
| 142 | |
| 143 | if (legacyCommandWithInput) { |
| 144 | posthog.capture("step run", { |
| 145 | step_name: legacyCommandWithInput.command.name, |
| 146 | params: {}, |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | unwrapResult( |
| 151 | await dispatch( |
| 152 | streamNormalInput({ |
| 153 | messages, |
| 154 | legacySlashCommandData: legacyCommandWithInput |
| 155 | ? { |
| 156 | command: legacyCommandWithInput.command, |
| 157 | contextItems: selectedContextItems, |
| 158 | historyIndex: inputIndex, |
| 159 | input: legacyCommandWithInput.input, |
| 160 | selectedCode, |
| 161 | } |
| 162 | : undefined, |
| 163 | }), |
| 164 | ), |
| 165 | ); |
| 166 | }), |
| 167 | ); |
| 168 | }, |
| 169 | ); |