diff --git a/gui/src/components/mainInput/ComboBox.tsx b/gui/src/components/mainInput/ComboBox.tsx index 1405e2411..4af7c9661 100644 --- a/gui/src/components/mainInput/ComboBox.tsx +++ b/gui/src/components/mainInput/ComboBox.tsx @@ -372,7 +372,8 @@ const ComboBox = React.forwardRef((props: ComboBoxProps, ref) => { ); const selectedContextItems = useSelector((state: RootStore) => { if (typeof props.index !== "undefined") { - return state.sessionState.contextItemsAtIndex[props.index] || []; + return (state.sessionState.history[props.index].params["context_items"] || + []) as ContextItem[]; } else { return state.sessionState.context_items || []; } diff --git a/gui/src/pages/gui.tsx b/gui/src/pages/gui.tsx index d8ad728cf..accad00b1 100644 --- a/gui/src/pages/gui.tsx +++ b/gui/src/pages/gui.tsx @@ -49,6 +49,7 @@ import { useNavigate } from "react-router-dom"; import { setTakenActionTrue } from "../redux/slices/miscSlice"; import { addContextItemAtIndex, + clearContextItems, deleteAtIndex, newSession, setActive, @@ -305,7 +306,10 @@ function GUI(props: GUIProps) { observations: [], logs: [], step_type: "UserInputStep", - params: { user_input: input }, + params: { + user_input: input, + context_items: sessionState.context_items, + }, hide: false, depth: 0, }, @@ -314,14 +318,6 @@ function GUI(props: GUIProps) { history: newHistory, context_items: sessionState.context_items, }; - for (let contextItem of sessionState.context_items) { - dispatch( - addContextItemAtIndex({ - item: contextItem, - index: newHistory.length - 1, - }) - ); - } client.runFromState(state); newHistory.push({ name: "Generating Response...", @@ -335,6 +331,15 @@ function GUI(props: GUIProps) { }); dispatch(setHistory(newHistory)); dispatch(setActive(true)); + for (let contextItem of sessionState.context_items) { + dispatch( + addContextItemAtIndex({ + item: contextItem, + index: newHistory.length - 1, + }) + ); + } + dispatch(clearContextItems()); // Increment localstorage counter for popup const counter = localStorage.getItem("mainTextEntryCounter"); @@ -742,7 +747,10 @@ function GUI(props: GUIProps) { observations: [], logs: [], step_type: "UserInputStep", - params: { user_input: value }, + params: { + user_input: value, + context_items: sessionState.context_items, + }, hide: false, depth: 0, }, @@ -753,6 +761,7 @@ function GUI(props: GUIProps) { history: newHistory, context_items: sessionState.context_items, }; + dispatch(clearContextItems()); client.runFromState(state); } diff --git a/gui/src/redux/slices/sessionStateReducer.ts b/gui/src/redux/slices/sessionStateReducer.ts index 7cbf10de4..e2c8e4ceb 100644 --- a/gui/src/redux/slices/sessionStateReducer.ts +++ b/gui/src/redux/slices/sessionStateReducer.ts @@ -75,7 +75,6 @@ const TEST_CONTEXT_ITEMS: ContextItem[] = [ export interface SessionFullState { history: StepDescription[]; - contextItemsAtIndex: { [index: number]: ContextItem[] }; context_items: ContextItem[]; active: boolean; title: string; @@ -89,7 +88,6 @@ export const sessionStateSlice = createSlice({ context_items: [], active: false, title: "New Session", - contextItemsAtIndex: {}, session_id: v4(), }, reducers: { @@ -223,16 +221,22 @@ export const sessionStateSlice = createSlice({ state: SessionFullState, { payload }: { payload: { item: ContextItem; index: number } } ) => { - return { - ...state, - contextItemsAtIndex: { - ...state.contextItemsAtIndex, - [payload.index]: [ - ...(state.contextItemsAtIndex[payload.index] ?? []), + let history = [...state.history]; + history[payload.index] = { + ...history[payload.index], + params: { + ...history[payload.index].params, + context_items: [ + ...((history[payload.index].params?.context_items as any) || []), payload.item, ], }, }; + + return { + ...state, + history, + }; }, addHighlightedCode: ( state: SessionFullState, @@ -327,20 +331,32 @@ export const sessionStateSlice = createSlice({ } else { return { ...state, - contextItemsAtIndex: { - ...state.contextItemsAtIndex, - [payload.index]: ( - state.contextItemsAtIndex[payload.index] ?? [] - ).filter( - (item) => - !ids.includes( - `${item.description.id.provider_title}-${item.description.id.item_id}` - ) - ), - }, + history: state.history.map((step, i) => { + if (i === payload.index) { + return { + ...step, + params: { + ...step.params, + context_items: (step.params?.context_items as any).filter( + (item: ContextItem) => + !ids.includes( + `${item.description.id.provider_title}-${item.description.id.item_id}` + ) + ), + }, + }; + } + return step; + }), }; } }, + clearContextItems: (state: SessionFullState) => { + return { + ...state, + context_items: [], + }; + }, setEditingAtIds: ( state: SessionFullState, { @@ -362,17 +378,25 @@ export const sessionStateSlice = createSlice({ } else { return { ...state, - contextItemsAtIndex: { - ...state.contextItemsAtIndex, - [payload.index]: ( - state.contextItemsAtIndex[payload.index] ?? [] - ).map((item) => { + history: state.history.map((step, i) => { + if (i === payload.index) { return { - ...item, - editing: ids.includes(item.description.id.item_id), + ...step, + params: { + ...step.params, + context_items: (step.params?.context_items as any).map( + (item: ContextItem) => { + return { + ...item, + editing: ids.includes(item.description.id.item_id), + }; + } + ), + }, }; - }), - }, + } + return step; + }), }; } }, @@ -391,5 +415,6 @@ export const { deleteContextWithIds, setTitle, setEditingAtIds, + clearContextItems, } = sessionStateSlice.actions; export default sessionStateSlice.reducer; diff --git a/server/continuedev/core/main.py b/server/continuedev/core/main.py index 2c0030c8e..0bbcdc5f3 100644 --- a/server/continuedev/core/main.py +++ b/server/continuedev/core/main.py @@ -340,7 +340,7 @@ class Step(ContinueBaseModel): return "Running step: " + self.name def on_stop(self, sdk: ContinueSDK) -> Optional[StepGenerator]: - pass + return None def dict(self, *args, **kwargs): d = super().dict(*args, **kwargs) diff --git a/server/continuedev/core/sdk.py b/server/continuedev/core/sdk.py index 98487f749..c54299468 100644 --- a/server/continuedev/core/sdk.py +++ b/server/continuedev/core/sdk.py @@ -215,27 +215,28 @@ class ContinueSDK(AbstractContinueSDK): raise ContinueCustomException(message, title, with_step) async def get_chat_context(self) -> List[ChatMessage]: - history_context = list( - map( - lambda step: ChatMessage( - role="user" if step.step_type == "UserInputStep" else "assistant", + msgs = [] + + for step in filter(lambda x: x.hide is False, self.history): + role = "assistant" + if step.step_type == "UserInputStep": + role = "user" + msgs.extend( + await self.__autopilot.context_manager.get_chat_messages( + [ContextItem(**itm) for itm in step.params["context_items"]] + ) + ) + + msgs.append( + ChatMessage( + role=role, name=step.step_type, content=step.description or f"Ran function {step.name}", summary=f"Called function {step.name}", - ), - filter(lambda x: x.hide is False, self.history[:-1]), + ) ) - ) - context_messages: List[ - ChatMessage - ] = await self.__autopilot.context_manager.get_chat_messages(self.context_items) - - # Insert at the end, but don't insert after latest user message or function call - for msg in context_messages: - history_context.insert(-1, msg) - - return history_context + return msgs async def get_context_item_chat_messages( self, exclude: Optional[str] = None diff --git a/server/continuedev/core/steps.py b/server/continuedev/core/steps.py index 0150b45b8..8439d45db 100644 --- a/server/continuedev/core/steps.py +++ b/server/continuedev/core/steps.py @@ -3,7 +3,7 @@ import difflib import subprocess import time from textwrap import dedent -from typing import Any, Dict, List, Optional, Union +from typing import Any, AsyncGenerator, Dict, List, Optional, Union from pydantic import Field @@ -20,7 +20,15 @@ from ..libs.util.templating import render_prompt_template from ..models.filesystem import FileSystem, RangeInFile, RangeInFileWithContents from ..models.filesystem_edit import EditDiff, FileEditWithFullContents, FileSystemEdit from .abstract_sdk import AbstractContinueSDK -from .main import ChatMessage, ContinueCustomException, SessionUpdate, SetStep, Step +from .main import ( + ChatMessage, + ContextItem, + ContinueCustomException, + DeltaStep, + SessionUpdate, + SetStep, + Step, +) from .observation import TextObservation, UserInputObservation @@ -373,7 +381,9 @@ Please output the code to be inserted at the cursor in order to fulfill the user or "" in line ) - async def stream_rif(self, rif: RangeInFileWithContents, sdk: AbstractContinueSDK): + async def stream_rif( + self, rif: RangeInFileWithContents, sdk: AbstractContinueSDK + ) -> AsyncGenerator[SetStep, None]: await sdk.ide.saveFile(rif.filepath) full_file_contents = await sdk.ide.readFile(rif.filepath) @@ -643,6 +653,10 @@ Please output the code to be inserted at the cursor in order to fulfill the user try: last_task_time = time.time() async for chunk in generator: + yield SetStep( + hide=False + ) # Doing this so that there are breakpoints for cancellation + # Stop early if it is repeating the file_suffix or the step was deleted if repeating_file_suffix: break @@ -757,7 +771,8 @@ Please output the code to be inserted at the cursor in order to fulfill the user for rif in rif_with_contents: await sdk.ide.setSuggestionsLocked(rif.filepath, True) - await self.stream_rif(rif, sdk) + async for update in self.stream_rif(rif, sdk): + yield update await sdk.ide.setSuggestionsLocked(rif.filepath, False) changes = "\n".join( @@ -857,6 +872,7 @@ class ManualEditStep(ReversibleStep): class UserInputStep(Step): user_input: str + context_items: List[ContextItem] = [] name: str = "User Input" hide: bool = False diff --git a/server/continuedev/plugins/steps/main.py b/server/continuedev/plugins/steps/main.py index 29b23fe6b..81c48810f 100644 --- a/server/continuedev/plugins/steps/main.py +++ b/server/continuedev/plugins/steps/main.py @@ -4,7 +4,7 @@ from typing import List, Optional, Union from pydantic import BaseModel, Field -from ...core.main import ContinueCustomException, SetStep, Step +from ...core.main import ContextItem, ContinueCustomException, SetStep, Step from ...core.sdk import ContinueSDK, Models from ...core.steps import DefaultModelEditCodeStep from ...libs.llm.base import LLM @@ -371,6 +371,7 @@ class EditHighlightedCodeStep(Step): class UserInputStep(Step): user_input: str + context_items: List[ContextItem] = [] class SolveTracebackStep(Step):