| 1 | import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; |
| 2 | import { JSONContent } from "@tiptap/core"; |
| 3 | import { InputModifiers } from "core"; |
| 4 | import posthog from "posthog-js"; |
| 5 | import { v4 as uuidv4 } from "uuid"; |
| 6 | import { resolveEditorContent } from "../../components/mainInput/TipTapEditor/utils/resolveEditorContent"; |
| 7 | import { selectSelectedChatModel } from "../slices/configSlice"; |
| 8 | import { |
| 9 | resetNextCodeBlockToApplyIndex, |
| 10 | submitEditorAndInitAtIndex, |
| 11 | updateHistoryItemAtIndex, |
| 12 | } from "../slices/sessionSlice"; |
| 13 | import { ThunkApiType } from "../store"; |
| 14 | import { streamNormalInput } from "./streamNormalInput"; |
| 15 | import { streamThunkWrapper } from "./streamThunkWrapper"; |
| 16 | import { updateFileSymbolsFromFiles } from "./updateFileSymbols"; |
| 17 | |
| 18 | export const streamResponseThunk = createAsyncThunk< |
| 19 | void, |
| 20 | { |
| 21 | editorState: JSONContent; |
| 22 | modifiers: InputModifiers; |
| 23 | index?: number; |
| 24 | }, |
| 25 | ThunkApiType |
| 26 | >( |
| 27 | "chat/streamResponse", |
| 28 | async ({ editorState, modifiers, index }, { dispatch, extra, getState }) => { |
| 29 | await dispatch( |
| 30 | streamThunkWrapper(async () => { |
| 31 | const state = getState(); |
| 32 | const selectedChatModel = selectSelectedChatModel(state); |
| 33 | const inputIndex = index ?? state.session.history.length; // Either given index or concat to end |
| 34 | |
| 35 | if (!selectedChatModel) { |
| 36 | throw new Error("No chat model selected"); |
| 37 | } |
| 38 | |
| 39 | dispatch( |
| 40 | submitEditorAndInitAtIndex({ index: inputIndex, editorState }), |
| 41 | ); |
| 42 | |
| 43 | dispatch(resetNextCodeBlockToApplyIndex()); |
| 44 | |
| 45 | const defaultContextProviders = |
| 46 | state.config.config.experimental?.defaultContext ?? []; |
| 47 | |
| 48 | if (!selectedChatModel) { |
| 49 | console.error( |
| 50 | "gatherContext thunk: Cannot gather context, no model selected", |
| 51 | ); |
| 52 | throw new Error("No chat model selected"); |
| 53 | } |
| 54 | |
| 55 | // Resolve context providers and construct new history |
| 56 | const { |
| 57 | selectedContextItems, |
| 58 | selectedCode, |
| 59 | content, |
| 60 | legacyCommandWithInput, |
| 61 | } = await resolveEditorContent({ |
| 62 | editorState, |
| 63 | modifiers, |
| 64 | ideMessenger: extra.ideMessenger, |
| 65 | defaultContextProviders, |
| 66 | availableSlashCommands: state.config.config.slashCommands, |
| 67 | dispatch, |
| 68 | getState, |
| 69 | }); |
| 70 | |
| 71 | // symbols for both context items AND selected codeblocks |
| 72 | const filesForSymbols = [ |
| 73 | ...selectedContextItems |
| 74 | .filter((item) => item.uri?.type === "file" && item?.uri?.value) |
| 75 | .map((item) => item.uri!.value), |
| 76 | ...selectedCode.map((rif) => rif.filepath), |
| 77 | ]; |
| 78 | void dispatch(updateFileSymbolsFromFiles(filesForSymbols)); |
| 79 | |
| 80 | dispatch( |
| 81 | updateHistoryItemAtIndex({ |
| 82 | index: inputIndex, |
| 83 | updates: { |
| 84 | message: { |
| 85 | role: "user", |
| 86 | content, |
| 87 | id: uuidv4(), |
| 88 | }, |
| 89 | contextItems: selectedContextItems, |
| 90 | }, |
| 91 | }), |
| 92 | ); |
| 93 | |
| 94 | posthog.capture("step run", { |
| 95 | step_name: "User Input", |
| 96 | params: {}, |
| 97 | }); |
| 98 | posthog.capture("userInput", {}); |
| 99 | |
| 100 | if (legacyCommandWithInput) { |
| 101 | posthog.capture("step run", { |
| 102 | step_name: legacyCommandWithInput.command.name, |
| 103 | params: {}, |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | unwrapResult( |
| 108 | await dispatch( |
| 109 | streamNormalInput({ |
| 110 | legacySlashCommandData: legacyCommandWithInput |
| 111 | ? { |
| 112 | command: legacyCommandWithInput.command, |
| 113 | contextItems: selectedContextItems, |
| 114 | historyIndex: inputIndex, |
| 115 | input: legacyCommandWithInput.input, |
| 116 | selectedCode, |
| 117 | } |
| 118 | : undefined, |
| 119 | }), |
| 120 | ), |
| 121 | ); |
| 122 | }), |
| 123 | ); |
| 124 | }, |
| 125 | ); |