diff options
Diffstat (limited to 'continuedev/src')
| -rw-r--r-- | continuedev/src/continuedev/libs/agent.py | 180 | ||||
| -rw-r--r-- | continuedev/src/continuedev/libs/core.py | 289 | ||||
| -rw-r--r-- | continuedev/src/continuedev/libs/env.py | 7 | ||||
| -rw-r--r-- | continuedev/src/continuedev/libs/sdk.py | 39 | ||||
| -rw-r--r-- | continuedev/src/continuedev/libs/steps/core/core.py | 91 | ||||
| -rw-r--r-- | continuedev/src/continuedev/libs/steps/main.py | 25 | ||||
| -rw-r--r-- | continuedev/src/continuedev/models/main.py | 5 | ||||
| -rw-r--r-- | continuedev/src/continuedev/server/notebook.py | 9 | 
8 files changed, 357 insertions, 288 deletions
| diff --git a/continuedev/src/continuedev/libs/agent.py b/continuedev/src/continuedev/libs/agent.py new file mode 100644 index 00000000..466aa234 --- /dev/null +++ b/continuedev/src/continuedev/libs/agent.py @@ -0,0 +1,180 @@ +import traceback +import time +from typing import Callable, Coroutine, List +from ..models.filesystem_edit import FileEditWithFullContents +from .llm import LLM +from .observation import Observation +from ..server.ide_protocol import AbstractIdeProtocolServer +from .util.queue import AsyncSubscriptionQueue +from ..models.main import ContinueBaseModel +from .core import Policy, History, FullState, Step, HistoryNode +from .steps.core.core import ReversibleStep, ManualEditStep, UserInputStep +from .sdk import ContinueSDK + + +class Agent(ContinueBaseModel): +    llm: LLM +    policy: Policy +    ide: AbstractIdeProtocolServer +    history: History = History.from_empty() +    continue_sdk: "ContinueSDK" +    _on_update_callbacks: List[Callable[[FullState], None]] = [] + +    _active: bool = False +    _should_halt: bool = False +    _main_user_input_queue: List[str] = [] + +    _user_input_queue = AsyncSubscriptionQueue() + +    class Config: +        arbitrary_types_allowed = True + +    def get_full_state(self) -> FullState: +        return FullState(history=self.history, active=self._active, user_input_queue=self._main_user_input_queue) + +    def on_update(self, callback: Callable[["FullState"], None]): +        """Subscribe to changes to state""" +        self._on_update_callbacks.append(callback) + +    def update_subscribers(self): +        full_state = self.get_full_state() +        for callback in self._on_update_callbacks: +            callback(full_state) + +    def __get_step_params(self, step: "Step"): +        return ContinueSDK(agent=self, llm=self.llm.with_system_message(step.system_message)) + +    def give_user_input(self, input: str, index: int): +        self._user_input_queue.post(index, input) + +    async def wait_for_user_input(self) -> str: +        self._active = False +        self.update_subscribers() +        await self._user_input_queue.get(self.history.current_index) +        self._active = True +        self.update_subscribers() + +    _manual_edits_buffer: List[FileEditWithFullContents] = [] + +    async def reverse_to_index(self, index: int): +        try: +            while self.history.get_current_index() >= index: +                current_step = self.history.get_current().step +                self.history.step_back() +                if issubclass(current_step.__class__, ReversibleStep): +                    await current_step.reverse(self.__get_step_params(current_step)) + +                self.update_subscribers() +        except Exception as e: +            print(e) + +    def handle_manual_edits(self, edits: List[FileEditWithFullContents]): +        for edit in edits: +            self._manual_edits_buffer.append(edit) +            # TODO: You're storing a lot of unecessary data here. Can compress into EditDiffs on the spot, and merge. +            # self._manual_edits_buffer = merge_file_edit(self._manual_edits_buffer, edit) + +    def handle_traceback(self, traceback: str): +        raise NotImplementedError + +    _step_depth: int = 0 + +    async def _run_singular_step(self, step: "Step", is_future_step: bool = False) -> Coroutine[Observation, None, None]: +        if not is_future_step: +            # Check manual edits buffer, clear out if needed by creating a ManualEditStep +            if len(self._manual_edits_buffer) > 0: +                manualEditsStep = ManualEditStep.from_sequence( +                    self._manual_edits_buffer) +                self._manual_edits_buffer = [] +                await self._run_singular_step(manualEditsStep) + +        # Update history - do this first so we get top-first tree ordering +        self.history.add_node(HistoryNode( +            step=step, observation=None, depth=self._step_depth)) + +        # Run step +        self._step_depth += 1 +        observation = await step(self.__get_step_params(step)) +        self._step_depth -= 1 + +        # Add observation to history +        self.history.get_current().observation = observation + +        # Update its description +        step._set_description(await step.describe(self.llm)) + +        # Call all subscribed callbacks +        self.update_subscribers() + +        return observation + +    async def run_from_step(self, step: "Step"): +        # if self._active: +        #     raise RuntimeError("Agent is already running") +        self._active = True + +        next_step = step +        is_future_step = False +        while not (next_step is None or self._should_halt): +            try: +                if is_future_step: +                    # If future step, then we are replaying and need to delete the step from history so it can be replaced +                    self.history.remove_current_and_substeps() + +                observation = await self._run_singular_step(next_step, is_future_step) +                if next_step := self.policy.next(self.history): +                    is_future_step = False +                elif next_step := self.history.take_next_step(): +                    is_future_step = True +                else: +                    next_step = None + +            except Exception as e: +                print( +                    f"Error while running step: \n{''.join(traceback.format_tb(e.__traceback__))}\n{e}") +                next_step = None + +        self._active = False + +        # Doing this so active can make it to the frontend after steps are done. But want better state syncing tools +        for callback in self._on_update_callbacks: +            callback(None) + +    async def run_from_observation(self, observation: Observation): +        next_step = self.policy.next(self.history) +        await self.run_from_step(next_step) + +    async def run_policy(self): +        first_step = self.policy.next(self.history) +        await self.run_from_step(first_step) + +    async def _request_halt(self): +        if self._active: +            self._should_halt = True +            while self._active: +                time.sleep(0.1) +        self._should_halt = False +        return None + +    async def accept_user_input(self, user_input: str): +        self._main_user_input_queue.append(user_input) +        self.update_subscribers() + +        if len(self._main_user_input_queue) > 1: +            return + +        # await self._request_halt() +        # Just run the step that takes user input, and +        # then up to the policy to decide how to deal with it. +        self._main_user_input_queue.pop(0) +        self.update_subscribers() +        await self.run_from_step(UserInputStep(user_input=user_input)) + +        while len(self._main_user_input_queue) > 0: +            await self.run_from_step(UserInputStep( +                user_input=self._main_user_input_queue.pop(0))) + +    async def accept_refinement_input(self, user_input: str, index: int): +        await self._request_halt() +        await self.reverse_to_index(index) +        await self.run_from_step(UserInputStep(user_input=user_input)) diff --git a/continuedev/src/continuedev/libs/core.py b/continuedev/src/continuedev/libs/core.py index 6a8a83ba..9d432c4b 100644 --- a/continuedev/src/continuedev/libs/core.py +++ b/continuedev/src/continuedev/libs/core.py @@ -1,18 +1,9 @@ -import traceback -import time  from typing import Callable, Coroutine, Dict, Generator, List, Tuple, Union -from ..models.filesystem_edit import EditDiff, FileEdit, FileEditWithFullContents, FileSystemEdit -from ..models.filesystem import FileSystem -from pydantic import BaseModel, parse_file_as, validator -from .llm import LLM -from .observation import Observation, UserInputObservation -from ..server.ide_protocol import AbstractIdeProtocolServer -from .util.queue import AsyncSubscriptionQueue - -class ContinueBaseModel(BaseModel): -    class Config: -        underscore_attrs_are_private = True +from ..models.main import ContinueBaseModel +from pydantic import validator +from .llm import LLM +from .observation import Observation  class HistoryNode(ContinueBaseModel): @@ -86,198 +77,11 @@ class Policy(ContinueBaseModel):  class ContinueSDK: -    """The SDK provided as parameters to a step""" -    llm: LLM -    ide: AbstractIdeProtocolServer -    __agent: "Agent" - -    def __init__(self, agent: "Agent", llm: Union[LLM, None] = None): -        if llm is None: -            self.llm = agent.llm -        else: -            self.llm = llm -        self.ide = agent.ide -        self.__agent = agent - -    @property -    def history(self) -> History: -        return self.__agent.history - -    async def run_step(self, step: "Step") -> Coroutine[Observation, None, None]: -        return await self.__agent._run_singular_step(step) +    pass -    async def apply_filesystem_edit(self, edit: FileSystemEdit): -        await self.run_step(FileSystemEditStep(edit=edit)) -    async def wait_for_user_input(self) -> str: -        return await self.__agent.wait_for_user_input() - - -class Agent(ContinueBaseModel): -    llm: LLM -    policy: Policy -    ide: AbstractIdeProtocolServer -    history: History = History.from_empty() -    _on_update_callbacks: List[Callable[["FullState"], None]] = [] - -    _active: bool = False -    _should_halt: bool = False -    _main_user_input_queue: List[str] = [] - -    _user_input_queue = AsyncSubscriptionQueue() - -    class Config: -        arbitrary_types_allowed = True - -    def get_full_state(self) -> FullState: -        return FullState(history=self.history, active=self._active, user_input_queue=self._main_user_input_queue) - -    def on_update(self, callback: Callable[["FullState"], None]): -        """Subscribe to changes to state""" -        self._on_update_callbacks.append(callback) - -    def update_subscribers(self): -        full_state = self.get_full_state() -        for callback in self._on_update_callbacks: -            callback(full_state) - -    def __get_step_params(self, step: "Step"): -        return ContinueSDK(agent=self, llm=self.llm.with_system_message(step.system_message)) - -    def give_user_input(self, input: str, index: int): -        self._user_input_queue.post(index, input) - -    async def wait_for_user_input(self) -> str: -        self._active = False -        self.update_subscribers() -        await self._user_input_queue.get(self.history.current_index) -        self._active = True -        self.update_subscribers() - -    _manual_edits_buffer: List[FileEditWithFullContents] = [] - -    async def reverse_to_index(self, index: int): -        try: -            while self.history.get_current_index() >= index: -                current_step = self.history.get_current().step -                self.history.step_back() -                if issubclass(current_step.__class__, ReversibleStep): -                    await current_step.reverse(self.__get_step_params(current_step)) - -                self.update_subscribers() -        except Exception as e: -            print(e) - -    def handle_manual_edits(self, edits: List[FileEditWithFullContents]): -        for edit in edits: -            self._manual_edits_buffer.append(edit) -            # TODO: You're storing a lot of unecessary data here. Can compress into EditDiffs on the spot, and merge. -            # self._manual_edits_buffer = merge_file_edit(self._manual_edits_buffer, edit) - -    def handle_traceback(self, traceback: str): -        raise NotImplementedError - -    _step_depth: int = 0 - -    async def _run_singular_step(self, step: "Step", is_future_step: bool = False) -> Coroutine[Observation, None, None]: -        if not is_future_step: -            # Check manual edits buffer, clear out if needed by creating a ManualEditStep -            if len(self._manual_edits_buffer) > 0: -                manualEditsStep = ManualEditStep.from_sequence( -                    self._manual_edits_buffer) -                self._manual_edits_buffer = [] -                await self._run_singular_step(manualEditsStep) - -        # Update history - do this first so we get top-first tree ordering -        self.history.add_node(HistoryNode( -            step=step, observation=None, depth=self._step_depth)) - -        # Run step -        self._step_depth += 1 -        observation = await step(self.__get_step_params(step)) -        self._step_depth -= 1 - -        # Add observation to history -        self.history.get_current().observation = observation - -        # Update its description -        step._set_description(await step.describe(self.llm)) - -        # Call all subscribed callbacks -        self.update_subscribers() - -        return observation - -    async def run_from_step(self, step: "Step"): -        # if self._active: -        #     raise RuntimeError("Agent is already running") -        self._active = True - -        next_step = step -        is_future_step = False -        while not (next_step is None or self._should_halt): -            try: -                if is_future_step: -                    # If future step, then we are replaying and need to delete the step from history so it can be replaced -                    self.history.remove_current_and_substeps() - -                observation = await self._run_singular_step(next_step, is_future_step) -                if next_step := self.policy.next(self.history): -                    is_future_step = False -                elif next_step := self.history.take_next_step(): -                    is_future_step = True -                else: -                    next_step = None - -            except Exception as e: -                print( -                    f"Error while running step: \n{''.join(traceback.format_tb(e.__traceback__))}\n{e}") -                next_step = None - -        self._active = False - -        # Doing this so active can make it to the frontend after steps are done. But want better state syncing tools -        for callback in self._on_update_callbacks: -            callback(None) - -    async def run_from_observation(self, observation: Observation): -        next_step = self.policy.next(self.history) -        await self.run_from_step(next_step) - -    async def run_policy(self): -        first_step = self.policy.next(self.history) -        await self.run_from_step(first_step) - -    async def _request_halt(self): -        if self._active: -            self._should_halt = True -            while self._active: -                time.sleep(0.1) -        self._should_halt = False -        return None - -    async def accept_user_input(self, user_input: str): -        self._main_user_input_queue.append(user_input) -        self.update_subscribers() - -        if len(self._main_user_input_queue) > 1: -            return - -        # await self._request_halt() -        # Just run the step that takes user input, and -        # then up to the policy to decide how to deal with it. -        self._main_user_input_queue.pop(0) -        self.update_subscribers() -        await self.run_from_step(UserInputStep(user_input=user_input)) - -        while len(self._main_user_input_queue) > 0: -            await self.run_from_step(UserInputStep( -                user_input=self._main_user_input_queue.pop(0))) - -    async def accept_refinement_input(self, user_input: str, index: int): -        await self._request_halt() -        await self.reverse_to_index(index) -        await self.run_from_step(UserInputStep(user_input=user_input)) +class SequentialStep: +    pass  class Step(ContinueBaseModel): @@ -331,85 +135,6 @@ class Step(ContinueBaseModel):          return SequentialStep(steps=steps) -class SequentialStep(Step): -    steps: list[Step] -    hide: bool = True - -    async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: -        for step in self.steps: -            observation = await sdk.run_step(step) -        return observation - - -class ReversibleStep(Step): -    async def reverse(self, sdk: ContinueSDK): -        raise NotImplementedError - - -class FileSystemEditStep(ReversibleStep): -    edit: FileSystemEdit -    _diff: Union[EditDiff, None] = None - -    hide: bool = True - -    async def run(self, sdk: "ContinueSDK") -> Coroutine[Observation, None, None]: -        self._diff = await sdk.ide.applyFileSystemEdit(self.edit) -        return None - -    async def reverse(self, sdk: "ContinueSDK"): -        await sdk.ide.applyFileSystemEdit(self._diff.backward) -        # Where and when should file saves happen? - - -class ManualEditStep(ReversibleStep): -    edit_diff: EditDiff -    hide: bool = True - -    hide: bool = True - -    async def describe(self, llm: LLM) -> Coroutine[str, None, None]: -        return "Manual edit step" -        # TODO - only handling FileEdit here, but need all other types of FileSystemEdits -        # Also requires the merge_file_edit function -        # return llm.complete(dedent(f"""This code was replaced: - -        #     {self.edit_diff.backward.replacement} - -        #     With this code: - -        #     {self.edit_diff.forward.replacement} - -        #     Maximally concise summary of changes in bullet points (can use markdown): -        # """)) - -    @classmethod -    def from_sequence(cls, edits: List[FileEditWithFullContents]) -> "ManualEditStep": -        diffs = [] -        for edit in edits: -            _, diff = FileSystem.apply_edit_to_str( -                edit.fileContents, edit.fileEdit) -            diffs.append(diff) -        return cls(edit_diff=EditDiff.from_sequence(diffs)) - -    async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: -        return None - -    async def reverse(self, sdk: ContinueSDK): -        await sdk.ide.applyFileSystemEdit(self.edit_diff.backward) - - -class UserInputStep(Step): -    user_input: str -    name: str = "User Input" -    hide: bool = True - -    async def describe(self, llm: LLM) -> Coroutine[str, None, None]: -        return self.user_input - -    async def run(self, sdk: ContinueSDK) -> Coroutine[UserInputObservation, None, None]: -        return UserInputObservation(user_input=self.user_input) - -  class ValidatorObservation(Observation):      passed: bool      observation: Observation diff --git a/continuedev/src/continuedev/libs/env.py b/continuedev/src/continuedev/libs/env.py new file mode 100644 index 00000000..d7275b41 --- /dev/null +++ b/continuedev/src/continuedev/libs/env.py @@ -0,0 +1,7 @@ +from dotenv import load_dotenv +import os + +load_dotenv() + + +openai_api_key = os.getenv("OPENAI_API_KEY") diff --git a/continuedev/src/continuedev/libs/sdk.py b/continuedev/src/continuedev/libs/sdk.py new file mode 100644 index 00000000..0295bf35 --- /dev/null +++ b/continuedev/src/continuedev/libs/sdk.py @@ -0,0 +1,39 @@ +from typing import Coroutine, Union +from ..models.filesystem_edit import FileSystemEdit +from .llm import LLM +from .observation import Observation +from ..server.ide_protocol import AbstractIdeProtocolServer +from .core import History, Step +from .steps.core.core import * + + +class Agent: +    pass + + +class ContinueSDK: +    """The SDK provided as parameters to a step""" +    llm: LLM +    ide: AbstractIdeProtocolServer +    __agent: Agent + +    def __init__(self, agent: Agent, llm: Union[LLM, None] = None): +        if llm is None: +            self.llm = agent.llm +        else: +            self.llm = llm +        self.ide = agent.ide +        self.__agent = agent + +    @property +    def history(self) -> History: +        return self.__agent.history + +    async def run_step(self, step: Step) -> Coroutine[Observation, None, None]: +        return await self.__agent._run_singular_step(step) + +    async def apply_filesystem_edit(self, edit: FileSystemEdit): +        await self.run_step(FileSystemEditStep(edit=edit)) + +    async def wait_for_user_input(self) -> str: +        return await self.__agent.wait_for_user_input() diff --git a/continuedev/src/continuedev/libs/steps/core/core.py b/continuedev/src/continuedev/libs/steps/core/core.py new file mode 100644 index 00000000..23086b53 --- /dev/null +++ b/continuedev/src/continuedev/libs/steps/core/core.py @@ -0,0 +1,91 @@ +# These steps are depended upon by ContinueSDK +from typing import Callable, Coroutine, Dict, Generator, List, Tuple, Union + +from ....models.filesystem_edit import EditDiff, FileEditWithFullContents, FileSystemEdit +from ....models.filesystem import FileSystem +from ...llm import LLM +from ...observation import Observation, UserInputObservation +from ...core import Step + + +class ContinueSDK: +    pass + + +class SequentialStep(Step): +    steps: list[Step] +    hide: bool = True + +    async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: +        for step in self.steps: +            observation = await sdk.run_step(step) +        return observation + + +class ReversibleStep(Step): +    async def reverse(self, sdk: ContinueSDK): +        raise NotImplementedError + + +class FileSystemEditStep(ReversibleStep): +    edit: FileSystemEdit +    _diff: Union[EditDiff, None] = None + +    hide: bool = True + +    async def run(self, sdk: "ContinueSDK") -> Coroutine[Observation, None, None]: +        self._diff = await sdk.ide.applyFileSystemEdit(self.edit) +        return None + +    async def reverse(self, sdk: "ContinueSDK"): +        await sdk.ide.applyFileSystemEdit(self._diff.backward) +        # Where and when should file saves happen? + + +class ManualEditStep(ReversibleStep): +    edit_diff: EditDiff +    hide: bool = True + +    hide: bool = True + +    async def describe(self, llm: LLM) -> Coroutine[str, None, None]: +        return "Manual edit step" +        # TODO - only handling FileEdit here, but need all other types of FileSystemEdits +        # Also requires the merge_file_edit function +        # return llm.complete(dedent(f"""This code was replaced: + +        #     {self.edit_diff.backward.replacement} + +        #     With this code: + +        #     {self.edit_diff.forward.replacement} + +        #     Maximally concise summary of changes in bullet points (can use markdown): +        # """)) + +    @classmethod +    def from_sequence(cls, edits: List[FileEditWithFullContents]) -> "ManualEditStep": +        diffs = [] +        for edit in edits: +            _, diff = FileSystem.apply_edit_to_str( +                edit.fileContents, edit.fileEdit) +            diffs.append(diff) +        return cls(edit_diff=EditDiff.from_sequence(diffs)) + +    async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: +        return None + +    async def reverse(self, sdk: ContinueSDK): +        await sdk.ide.applyFileSystemEdit(self.edit_diff.backward) + + +class UserInputStep(Step): +    user_input: str +    name: str = "User Input" +    hide: bool = True + +    async def describe(self, llm: LLM) -> Coroutine[str, None, None]: +        return self.user_input + +    async def run(self, sdk: ContinueSDK) -> Coroutine[UserInputObservation, None, None]: +        return UserInputObservation(user_input=self.user_input) diff --git a/continuedev/src/continuedev/libs/steps/main.py b/continuedev/src/continuedev/libs/steps/main.py index 70953e95..555c1180 100644 --- a/continuedev/src/continuedev/libs/steps/main.py +++ b/continuedev/src/continuedev/libs/steps/main.py @@ -51,6 +51,31 @@ class RunCommandStep(Step):              return TextObservation(text=stdout) +def ShellCommandsStep(Step): +    cmds: List[str] +    name: str = "Run Shell Commands" + +    async def describe(self, llm: LLM) -> Coroutine[str, None, None]: +        return "\n".join(self.cmds) + +    async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: +        cwd = await sdk.ide.getWorkspaceDirectory() + +        process = subprocess.Popen( +            '/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=cwd) + +        stdin_input = "\n".join(self.cmds) +        out, err = process.communicate(stdin_input.encode()) + +        # TODO: How to await?? + +        # If it fails, return the error +        if err is not None and err != "": +            return TextObservation(text=err) + +        return None + +  class WaitForUserInputStep(Step):      prompt: str      name: str = "Waiting for user input" diff --git a/continuedev/src/continuedev/models/main.py b/continuedev/src/continuedev/models/main.py index 081ec4af..7986b30c 100644 --- a/continuedev/src/continuedev/models/main.py +++ b/continuedev/src/continuedev/models/main.py @@ -4,6 +4,11 @@ from pydantic import BaseModel, root_validator  from functools import total_ordering +class ContinueBaseModel(BaseModel): +    class Config: +        underscore_attrs_are_private = True + +  @total_ordering  class Position(BaseModel):      line: int diff --git a/continuedev/src/continuedev/server/notebook.py b/continuedev/src/continuedev/server/notebook.py index c9d4edc5..5eb151d7 100644 --- a/continuedev/src/continuedev/server/notebook.py +++ b/continuedev/src/continuedev/server/notebook.py @@ -6,20 +6,17 @@ from uvicorn.main import Server  from ..models.filesystem_edit import FileEditWithFullContents  from ..libs.policy import DemoPolicy -from ..libs.core import Agent, FullState, History, Step +from ..libs.core import FullState, History, Step +from ..libs.agent import Agent  from ..libs.steps.nate import ImplementAbstractMethodStep  from ..libs.observation import Observation -from dotenv import load_dotenv  from ..libs.llm.openai import OpenAI  from .ide_protocol import AbstractIdeProtocolServer -import os +from ..libs.env import openai_api_key  import asyncio  import nest_asyncio  nest_asyncio.apply() -load_dotenv() -openai_api_key = os.getenv("OPENAI_API_KEY") -  router = APIRouter(prefix="/notebook", tags=["notebook"])  # Graceful shutdown by closing websockets | 
