Merge branch 'docs' of github.com:continuedev/continue into docs

This commit is contained in:
Ty Dunn 2023-05-30 16:03:57 +02:00
commit fbdabc9d19
18 changed files with 255 additions and 184 deletions

View file

@ -0,0 +1,180 @@
import traceback
import time
from typing import Callable, Coroutine, List
from ..models.filesystem_edit import FileEditWithFullContents
from ..libs.llm import LLM
from .observation import Observation
from ..server.ide_protocol import AbstractIdeProtocolServer
from ..libs.util.queue import AsyncSubscriptionQueue
from ..models.main import ContinueBaseModel
from .main import Policy, History, FullState, Step, HistoryNode
from ..libs.steps.core.core import ReversibleStep, ManualEditStep, UserInputStep
from .sdk import ContinueSDK
class Autopilot(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(autopilot=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()
user_input = await self._user_input_queue.get(self.history.current_index)
self._active = True
self.update_subscribers()
return user_input
_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("Autopilot 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))

View file

@ -89,31 +89,31 @@ class ContinueSDK:
"""The SDK provided as parameters to a step"""
llm: LLM
ide: AbstractIdeProtocolServer
__agent: "Agent"
__autopilot: "Autopilot"
def __init__(self, agent: "Agent", llm: Union[LLM, None] = None):
def __init__(self, autopilot: "Autopilot", llm: Union[LLM, None] = None):
if llm is None:
self.llm = agent.llm
self.llm = autopilot.llm
else:
self.llm = llm
self.ide = agent.ide
self.__agent = agent
self.ide = autopilot.ide
self.__autopilot = autopilot
@property
def history(self) -> History:
return self.__agent.history
return self.__autopilot.history
async def run_step(self, step: "Step") -> Coroutine[Observation, None, None]:
return await self.__agent._run_singular_step(step)
return await self.__autopilot._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()
return await self.__autopilot.wait_for_user_input()
class Agent(ContinueBaseModel):
class Autopilot(ContinueBaseModel):
llm: LLM
policy: Policy
ide: AbstractIdeProtocolServer
@ -142,7 +142,7 @@ class Agent(ContinueBaseModel):
callback(full_state)
def __get_step_params(self, step: "Step"):
return ContinueSDK(agent=self, llm=self.llm.with_system_message(step.system_message))
return ContinueSDK(autopilot=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)
@ -210,7 +210,7 @@ class Agent(ContinueBaseModel):
async def run_from_step(self, step: "Step"):
# if self._active:
# raise RuntimeError("Agent is already running")
# raise RuntimeError("Autopilot is already running")
self._active = True
next_step = step

View file

@ -5,7 +5,7 @@ from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from ..models.main import FileEdit, DeleteDirectory, DeleteFile, AddDirectory, AddFile, FileSystemEdit, Position, Range, RenameFile, RenameDirectory, SequentialFileSystemEdit
from ..models.filesystem import FileSystem
from ..libs.main import Agent
from ..libs.main import Autopilot
from ..libs.map_path import map_path
from ..libs.steps.main import ManualEditAction
import shutil
@ -65,15 +65,15 @@ def calculate_diff(filepath: str, original: str, updated: str) -> List[FileEdit]
# The whole usage of watchdog here should only be specific to RealFileSystem, you want to have a different "Observer" class for VirtualFileSystem, which would depend on being sent notifications
class CopyCodebaseEventHandler(PatternMatchingEventHandler):
def __init__(self, ignore_directories: List[str], ignore_patterns: List[str], agent: Agent, orig_root: str, copy_root: str, filesystem: FileSystem):
def __init__(self, ignore_directories: List[str], ignore_patterns: List[str], autopilot: Autopilot, orig_root: str, copy_root: str, filesystem: FileSystem):
super().__init__(ignore_directories=ignore_directories, ignore_patterns=ignore_patterns)
self.agent = agent
self.autopilot = autopilot
self.orig_root = orig_root
self.copy_root = copy_root
self.filesystem = filesystem
# For now, we'll just make the update immediately, but eventually need to sync with agent.
# It should be the agent that makes the update right? It's just another action, everything comes from a single stream.
# For now, we'll just make the update immediately, but eventually need to sync with autopilot.
# It should be the autopilot that makes the update right? It's just another action, everything comes from a single stream.
def _event_to_edit(self, event) -> Union[FileSystemEdit, None]:
# NOTE: You'll need to map paths to create both an action within the copy filesystem (the one you take) and one in the original fileystem (the one you'll record and allow the user to accept). Basically just need a converter built in to the FileSystemEdit class
@ -110,13 +110,13 @@ class CopyCodebaseEventHandler(PatternMatchingEventHandler):
return
edit = edit.with_mapped_paths(self.orig_root, self.copy_root)
action = ManualEditAction(edit)
self.agent.act(action)
self.autopilot.act(action)
def maintain_copy_workspace(agent: Agent, filesystem: FileSystem, orig_root: str, copy_root: str):
def maintain_copy_workspace(autopilot: Autopilot, filesystem: FileSystem, orig_root: str, copy_root: str):
observer = Observer()
event_handler = CopyCodebaseEventHandler(
[".git"], [], agent, orig_root, copy_root, filesystem)
[".git"], [], autopilot, orig_root, copy_root, filesystem)
observer.schedule(event_handler, orig_root, recursive=True)
observer.start()
try:

View file

@ -1,26 +0,0 @@
from typing import List
import pluggy
from .step import hookspecs
from .step.libs import hello_world
builtin_libs = [hello_world]
def get_plugin_manager(use_plugins: List[str]) -> pluggy.PluginManager:
pm = pluggy.PluginManager("continue.step")
pm.add_hookspecs(hookspecs)
pm.load_setuptools_entrypoints("continue.step")
# Only use plugins that are specified in the config file
for plugin, name in pm.list_name_plugin():
if name not in use_plugins:
pm.set_blocked(plugin)
# Print warning if plugin not found
for name in use_plugins:
if not pm.has_plugin(name):
print(f"Plugin {name} not found.")
for lib in builtin_libs:
pm.register(lib)
return pm

View file

@ -1,21 +0,0 @@
def load_validator_plugin(config: ValidatorPluginConfig) -> Validator:
if config.name == "continue.tb_validator":
return PythonTracebackValidator(config.cmd, config.cwd)
elif config.name == "continue.pytest_validator":
return PytestValidator(cwd=config.cwd)
else:
raise KeyError("Unknown validator plugin name")
def load_llm_plugin(config: LLMPluginConfig) -> LLM:
if config.provider == "openai":
return OpenAI(api_key=config.api_key)
else:
raise KeyError("Unknown LLM provider: " + config.provider)
def load_policy_plugin(config: PolicyPluginConfig) -> Policy:
if config.name == "continue.random_policy":
return RandomPolicy()
elif config.name == "continue.dfs_policy":
return DFSPolicy()
else:
raise KeyError("Unknown policy plugin name")

View file

@ -1,4 +0,0 @@
import pluggy
hookimpl = pluggy.HookimplMarker("continue.policy")
"""Marker to be imported and used in plugins (and for own implementations)"""

View file

@ -1,10 +0,0 @@
from typing import List, Tuple
import pluggy
from ...libs.policy import Policy, Step
hookspec = pluggy.HookspecMarker("continue.policy")
class PolicyPlugin(Policy):
@hookspec
def next(self) -> Step:
"""Get the next step to run"""

View file

@ -1,22 +0,0 @@
from plugins import policy
from ....libs.observation import Observation
from ....libs.steps import Step
from ....libs.core import History
class AlternatingPolicy:
"""A Policy that alternates between two steps."""
def __init__(self, first: Step, second: Step):
self.first = first
self.second = second
self.last_was_first = False
@policy.hookimpl
def next(self, history: History) -> Step:
if self.last_was_first:
self.last_was_first = False
return self.second
else:
self.last_was_first = True
return self.first

View file

@ -1,4 +0,0 @@
import pluggy
hookimpl = pluggy.HookimplMarker("continue.step")
"""Marker to be imported and used in plugins (and for own implementations)"""

View file

@ -1,13 +0,0 @@
from typing import Coroutine
import pluggy
from ...libs.core import ContinueSDK, Step, Observation
hookspec = pluggy.HookspecMarker("continue.step")
# Perhaps Actions should be generic about what their inputs must be.
class StepPlugin(Step):
@hookspec
async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]:
"""Run"""

View file

@ -1,9 +0,0 @@
from ....plugins import step
from ....libs.steps import ContinueSDK
class HelloWorldStep:
"""A Step that prints "Hello World!"."""
@step.hookimpl
def run(sdk: ContinueSDK):
print("Hello World!")

View file

@ -122,7 +122,7 @@ class IdeProtocolServer(AbstractIdeProtocolServer):
pass
async def setFileOpen(self, filepath: str, open: bool = True):
# Agent needs access to this.
# Autopilot needs access to this.
await self.websocket.send_json({
"messageType": "setFileOpen",
"filepath": filepath,
@ -147,12 +147,12 @@ class IdeProtocolServer(AbstractIdeProtocolServer):
responses = await asyncio.gather(*[
self._receive_json(ShowSuggestionResponse)
for i in range(len(suggestions))
]) # WORKING ON THIS FLOW HERE. Fine now to just await for response, instead of doing something fancy with a "waiting" state on the agent.
]) # WORKING ON THIS FLOW HERE. Fine now to just await for response, instead of doing something fancy with a "waiting" state on the autopilot.
# Just need connect the suggestionId to the IDE (and the notebook)
return any([r.accepted for r in responses])
# ------------------------------- #
# Here needs to pass message onto the Agent OR Agent just subscribes.
# Here needs to pass message onto the Autopilot OR Autopilot just subscribes.
# This is where you might have triggers: plugins can subscribe to certian events
# like file changes, tracebacks, etc...
@ -160,12 +160,12 @@ class IdeProtocolServer(AbstractIdeProtocolServer):
pass
def onTraceback(self, traceback: Traceback):
# Same as below, maybe not every agent?
# Same as below, maybe not every autopilot?
for _, session in self.session_manager.sessions.items():
session.agent.handle_traceback(traceback)
session.autopilot.handle_traceback(traceback)
def onFileSystemUpdate(self, update: FileSystemEdit):
# Access to Agent (so SessionManager)
# Access to Autopilot (so SessionManager)
pass
def onCloseNotebook(self, session_id: str):
@ -176,10 +176,10 @@ class IdeProtocolServer(AbstractIdeProtocolServer):
pass
def onFileEdits(self, edits: List[FileEditWithFullContents]):
# Send the file edits to ALL agents.
# Send the file edits to ALL autopilots.
# Maybe not ideal behavior
for _, session in self.session_manager.sessions.items():
session.agent.handle_manual_edits(edits)
session.autopilot.handle_manual_edits(edits)
# Request information. Session doesn't matter.
async def getOpenFiles(self) -> List[str]:

View file

@ -6,7 +6,7 @@ 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 Autopilot, FullState, History, Step
from ..libs.steps.nate import ImplementAbstractMethodStep
from ..libs.observation import Observation
from dotenv import load_dotenv
@ -41,16 +41,16 @@ Server.handle_exit = AppStatus.handle_exit
class Session:
session_id: str
agent: Agent
autopilot: Autopilot
ws: Union[WebSocket, None]
def __init__(self, session_id: str, agent: Agent):
def __init__(self, session_id: str, autopilot: Autopilot):
self.session_id = session_id
self.agent = agent
self.autopilot = autopilot
self.ws = None
class DemoAgent(Agent):
class DemoAutopilot(Autopilot):
first_seen: bool = False
cumulative_edit_string = ""
@ -78,20 +78,20 @@ class SessionManager:
def new_session(self, ide: AbstractIdeProtocolServer) -> str:
cmd = "python3 /Users/natesesti/Desktop/continue/extension/examples/python/main.py"
agent = DemoAgent(llm=OpenAI(api_key=openai_api_key),
policy=DemoPolicy(cmd=cmd), ide=ide)
autopilot = DemoAutopilot(llm=OpenAI(api_key=openai_api_key),
policy=DemoPolicy(cmd=cmd), ide=ide)
session_id = str(uuid4())
session = Session(session_id=session_id, agent=agent)
session = Session(session_id=session_id, autopilot=autopilot)
self.sessions[session_id] = session
def on_update(state: FullState):
session_manager.send_ws_data(session_id, {
"messageType": "state",
"state": agent.get_full_state().dict()
"state": autopilot.get_full_state().dict()
})
agent.on_update(on_update)
asyncio.create_task(agent.run_policy())
autopilot.on_update(on_update)
asyncio.create_task(autopilot.run_policy())
return session_id
def remove_session(self, session_id: str):
@ -147,7 +147,7 @@ async def websocket_endpoint(websocket: WebSocket, session: Session = Depends(we
# Update any history that may have happened before connection
await websocket.send_json({
"messageType": "state",
"state": session_manager.get_session(session.session_id).agent.get_full_state().dict()
"state": session_manager.get_session(session.session_id).autopilot.get_full_state().dict()
})
print("Session started", data)
while AppStatus.should_exit is False:
@ -162,17 +162,17 @@ async def websocket_endpoint(websocket: WebSocket, session: Session = Depends(we
if messageType == "main_input":
# Do something with user input
asyncio.create_task(
session.agent.accept_user_input(data["value"]))
session.autopilot.accept_user_input(data["value"]))
elif messageType == "step_user_input":
asyncio.create_task(
session.agent.give_user_input(data["value"], data["index"]))
session.autopilot.give_user_input(data["value"], data["index"]))
elif messageType == "refinement_input":
asyncio.create_task(
session.agent.accept_refinement_input(data["value"], data["index"]))
session.autopilot.accept_refinement_input(data["value"], data["index"]))
elif messageType == "reverse":
# Reverse the history to the given index
asyncio.create_task(
session.agent.reverse_to_index(data["index"]))
session.autopilot.reverse_to_index(data["index"]))
except Exception as e:
print(e)
@ -182,17 +182,17 @@ async def websocket_endpoint(websocket: WebSocket, session: Session = Depends(we
@router.post("/run")
def request_run(step: Step, session=Depends(session)):
"""Tell an agent to take a specific action."""
asyncio.create_task(session.agent.run_from_step(step))
"""Tell an autopilot to take a specific action."""
asyncio.create_task(session.autopilot.run_from_step(step))
return "Success"
@router.get("/history")
def get_history(session=Depends(session)) -> History:
return session.agent.history
return session.autopilot.history
@router.post("/observation")
def post_observation(observation: Observation, session=Depends(session)):
asyncio.create_task(session.agent.run_from_observation(observation))
asyncio.create_task(session.autopilot.run_from_observation(observation))
return "Success"

View file

@ -9,11 +9,12 @@ The `Continue Core` connects the [SDK](./sdk.md) and [GUI](./gui.md) with the [I
## What else to know
The `Core` includes
- IDE protocol
- GUI protocol
- SDK
- Agent
- Autopilot
There is a two-way sync between an IDE and the GUI that happens through Core.
**Q: does this make sense as a concept?**
**Q: does this make sense as a concept?**

View file

@ -13,4 +13,4 @@ An `LLM` is short for Large Language Model, which includes models like GPT-4, St
**Q: should we call this LLM? Perhaps just model?**
**Q: should this abstraction be connected to agent?**
**Q: should this abstraction be connected to autopilot?**

View file

@ -13,40 +13,39 @@
/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
const sidebars = {
docsSidebar: [
'intro',
'getting-started',
'install',
'how-continue-works',
"intro",
"getting-started",
"install",
"how-continue-works",
{
type: 'category',
label: 'Walkthroughs',
type: "category",
label: "Walkthroughs",
items: [
'walkthroughs/use-the-gui',
'walkthroughs/use-a-recipe',
'walkthroughs/create-a-recipe',
'walkthroughs/share-a-recipe',
"walkthroughs/use-the-gui",
"walkthroughs/use-a-recipe",
"walkthroughs/create-a-recipe",
"walkthroughs/share-a-recipe",
],
},
{
type: 'category',
label: 'Concepts',
type: "category",
label: "Concepts",
items: [
'concepts/autopilot',
'concepts/core',
'concepts/gui',
'concepts/history',
'concepts/ide',
'concepts/llm',
'concepts/policy',
'concepts/recipe',
'concepts/sdk',
'concepts/step',
"concepts/autopilot",
"concepts/core",
"concepts/gui",
"concepts/history",
"concepts/ide",
"concepts/llm",
"concepts/policy",
"concepts/recipe",
"concepts/sdk",
"concepts/step",
],
},
'telemetry',
"telemetry",
],
};
module.exports = sidebars;
module.exports = sidebars;