diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index 90ec92fb9..25cf28970 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -5,6 +5,7 @@ import { ChatHistory, ChatHistoryItem, ChatMessage, + ContinueSDK, LLMReturnValue, MessageContent, SlashCommand, @@ -19,7 +20,7 @@ import { useSelector } from "react-redux"; import resolveEditorContent from "../components/mainInput/resolveInput"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; import { - addContextItems, + addContextItemsAtIndex, addLogs, initNewActiveMessage, resubmitAtIndex, @@ -120,19 +121,25 @@ function useChatHandler(dispatch: Dispatch) { async function _streamSlashCommand( messages: ChatMessage[], slashCommand: SlashCommand, - input: string + input: string, + historyIndex: number ) { let generator: AsyncGenerator; if (slashCommand.runInNodeJs) { generator = _streamSlashCommandFromVsCode(messages, slashCommand, input); } else { - const sdk = { + const sdk: ContinueSDK = { input, history: messages, ide: new ExtensionIde(), llm: defaultModel, addContextItem: (item) => { - dispatch(addContextItems([item])); + dispatch( + addContextItemsAtIndex({ + index: historyIndex, + contextItems: [item], + }) + ); }, contextItems, params: slashCommand.params, @@ -181,10 +188,11 @@ function useChatHandler(dispatch: Dispatch) { }; let newHistory: ChatHistory = [...history.slice(0, index), historyItem]; + const historyIndex = index || newHistory.length - 1; dispatch( setMessageAtIndex({ message, - index: index || newHistory.length - 1, + index: historyIndex, contextItems, }) ); @@ -217,7 +225,12 @@ function useChatHandler(dispatch: Dispatch) { user_input: commandInput, }, }); - await _streamSlashCommand(messages, slashCommand, commandInput); + await _streamSlashCommand( + messages, + slashCommand, + commandInput, + historyIndex + ); } } catch (e) { console.log("Continue: error streaming response: ", e); diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index 9d881ceea..df0a792e5 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -264,6 +264,22 @@ export const stateSlice = createSlice({ state.history[payload.index].message = payload.message; state.history[payload.index].contextItems = payload.contextItems || []; }, + addContextItemsAtIndex: ( + state, + { + payload, + }: { + payload: { + index: number; + contextItems: ContextItemWithId[]; + }; + } + ) => { + console.log("addContextItemsAtIndex", payload, state.history); + if (payload.index < state.history.length) { + state.history[payload.index].contextItems.push(...payload.contextItems); + } + }, setInactive: (state) => { return { ...state, @@ -450,6 +466,7 @@ export const stateSlice = createSlice({ export const { setContextItemsAtIndex, addContextItems, + addContextItemsAtIndex, setInactive, streamUpdate, newSession,