diff options
author | Nate Sesti <33237525+sestinj@users.noreply.github.com> | 2023-10-09 18:37:27 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-09 18:37:27 -0700 |
commit | f09150617ed2454f3074bcf93f53aae5ae637d40 (patch) | |
tree | 5cfe614a64d921dfe58b049f426d67a8b832c71f /server/continuedev/plugins | |
parent | 985304a213f620cdff3f8f65f74ed7e3b79be29d (diff) | |
download | sncontinue-f09150617ed2454f3074bcf93f53aae5ae637d40.tar.gz sncontinue-f09150617ed2454f3074bcf93f53aae5ae637d40.tar.bz2 sncontinue-f09150617ed2454f3074bcf93f53aae5ae637d40.zip |
Preview (#541)
* Strong typing (#533)
* refactor: :recycle: get rid of continuedev.src.continuedev structure
* refactor: :recycle: switching back to server folder
* feat: :sparkles: make config.py imports shorter
* feat: :bookmark: publish as pre-release vscode extension
* refactor: :recycle: refactor and add more completion params to ui
* build: :building_construction: download from preview S3
* fix: :bug: fix paths
* fix: :green_heart: package:pre-release
* ci: :green_heart: more time for tests
* fix: :green_heart: fix build scripts
* fix: :bug: fix import in run.py
* fix: :bookmark: update version to try again
* ci: 💚 Update package.json version [skip ci]
* refactor: :fire: don't check for old extensions version
* fix: :bug: small bug fixes
* fix: :bug: fix config.py import paths
* ci: 💚 Update package.json version [skip ci]
* ci: :green_heart: platform-specific builds test #1
* feat: :green_heart: ship with binary
* fix: :green_heart: fix copy statement to include.exe for windows
* fix: :green_heart: cd extension before packaging
* chore: :loud_sound: count tokens generated
* fix: :green_heart: remove npm_config_arch
* fix: :green_heart: publish as pre-release!
* chore: :bookmark: update version
* perf: :green_heart: hardcode distro paths
* fix: :bug: fix yaml syntax error
* chore: :bookmark: update version
* fix: :green_heart: update permissions and version
* feat: :bug: kill old server if needed
* feat: :lipstick: update marketplace icon for pre-release
* ci: 💚 Update package.json version [skip ci]
* feat: :sparkles: auto-reload for config.py
* feat: :wrench: update default config.py imports
* feat: :sparkles: codelens in config.py
* feat: :sparkles: select model param count from UI
* ci: 💚 Update package.json version [skip ci]
* feat: :sparkles: more model options, ollama error handling
* perf: :zap: don't show server loading immediately
* fix: :bug: fixing small UI details
* ci: 💚 Update package.json version [skip ci]
* feat: :rocket: headers param on LLM class
* fix: :bug: fix headers for openai.;y
* feat: :sparkles: highlight code on cmd+shift+L
* ci: 💚 Update package.json version [skip ci]
* feat: :lipstick: sticky top bar in gui.tsx
* fix: :loud_sound: websocket logging and horizontal scrollbar
* ci: 💚 Update package.json version [skip ci]
* feat: :sparkles: allow AzureOpenAI Service through GGML
* ci: 💚 Update package.json version [skip ci]
* fix: :bug: fix automigration
* ci: 💚 Update package.json version [skip ci]
* ci: :green_heart: upload binaries in ci, download apple silicon
* chore: :fire: remove notes
* fix: :green_heart: use curl to download binary
* fix: :green_heart: set permissions on apple silicon binary
* fix: :green_heart: testing
* fix: :green_heart: cleanup file
* fix: :green_heart: fix preview.yaml
* fix: :green_heart: only upload once per binary
* fix: :green_heart: install rosetta
* ci: :green_heart: download binary after tests
* ci: 💚 Update package.json version [skip ci]
* ci: :green_heart: prepare ci for merge to main
---------
Co-authored-by: GitHub Action <action@github.com>
Diffstat (limited to 'server/continuedev/plugins')
62 files changed, 4427 insertions, 0 deletions
diff --git a/server/continuedev/plugins/context_providers/__init__.py b/server/continuedev/plugins/context_providers/__init__.py new file mode 100644 index 00000000..0123bb7b --- /dev/null +++ b/server/continuedev/plugins/context_providers/__init__.py @@ -0,0 +1,7 @@ +from .diff import DiffContextProvider # noqa: F401 +from .filetree import FileTreeContextProvider # noqa: F401 +from .github import GitHubIssuesContextProvider # noqa: F401 +from .google import GoogleContextProvider # noqa: F401 +from .search import SearchContextProvider # noqa: F401 +from .terminal import TerminalContextProvider # noqa: F401 +from .url import URLContextProvider # noqa: F401 diff --git a/server/continuedev/plugins/context_providers/diff.py b/server/continuedev/plugins/context_providers/diff.py new file mode 100644 index 00000000..05da3547 --- /dev/null +++ b/server/continuedev/plugins/context_providers/diff.py @@ -0,0 +1,73 @@ +import subprocess +from typing import List + +from pydantic import Field + +from ...core.context import ContextProvider +from ...core.main import ( + ContextItem, + ContextItemDescription, + ContextItemId, + ContinueCustomException, +) + + +class DiffContextProvider(ContextProvider): + """ + Type '@diff' to reference all of the changes you've made to your current branch. This is useful if you want to summarize what you've done or ask for a general review of your work before committing. + """ + + title = "diff" + display_title = "Diff" + description = "Output of 'git diff' in current repo" + dynamic = True + + _DIFF_CONTEXT_ITEM_ID = "diff" + + workspace_dir: str = Field( + None, description="The workspace directory in which to run `git diff`" + ) + + @property + def BASE_CONTEXT_ITEM(self): + return ContextItem( + content="", + description=ContextItemDescription( + name="Diff", + description="Reference the output of 'git diff' for the current workspace", + id=ContextItemId( + provider_title=self.title, item_id=self._DIFF_CONTEXT_ITEM_ID + ), + ), + ) + + async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]: + self.workspace_dir = workspace_dir + return [self.BASE_CONTEXT_ITEM] + + async def get_item(self, id: ContextItemId, query: str) -> ContextItem: + if not id.provider_title == self.title: + raise Exception("Invalid provider title for item") + + result = subprocess.run( + ["git", "diff"], cwd=self.workspace_dir, capture_output=True, text=True + ) + diff = result.stdout + error = result.stderr + if error.strip() != "": + if error.startswith("warning: Not a git repository"): + raise ContinueCustomException( + title="Not a git repository", + message="The @diff context provider only works in git repositories.", + ) + raise ContinueCustomException( + title="Error running git diff", + message=f"Error running git diff:\n\n{error}", + ) + + if diff.strip() == "": + diff = "No changes" + + ctx_item = self.BASE_CONTEXT_ITEM.copy() + ctx_item.content = diff + return ctx_item diff --git a/server/continuedev/plugins/context_providers/dynamic.py b/server/continuedev/plugins/context_providers/dynamic.py new file mode 100644 index 00000000..50567621 --- /dev/null +++ b/server/continuedev/plugins/context_providers/dynamic.py @@ -0,0 +1,75 @@ +from abc import ABC, abstractmethod +from typing import List + +from ...core.context import ContextProvider +from ...core.main import ContextItem, ContextItemDescription, ContextItemId +from ...libs.util.create_async_task import create_async_task +from .util import remove_meilisearch_disallowed_chars + + +class DynamicProvider(ContextProvider, ABC): + """ + A title representing the provider + """ + + title: str + """A name representing the provider. Probably use capitalized version of title""" + + name: str + + workspace_dir: str = None + dynamic: bool = True + + @property + def BASE_CONTEXT_ITEM(self): + return ContextItem( + content="", + description=ContextItemDescription( + name=self.name, + description=self.description, + id=ContextItemId(provider_title=self.title, item_id=self.title), + ), + ) + + async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]: + self.workspace_dir = workspace_dir + create_async_task(self.setup()) + return [self.BASE_CONTEXT_ITEM] + + async def get_item(self, id: ContextItemId, query: str) -> ContextItem: + if not id.provider_title == self.title: + raise Exception("Invalid provider title for item") + + query = query.lstrip(self.title + " ") + results = await self.get_content(query) + + ctx_item = self.BASE_CONTEXT_ITEM.copy() + ctx_item.content = results + ctx_item.description.name = f"{self.name}: '{query}'" + ctx_item.description.id.item_id = remove_meilisearch_disallowed_chars(query) + return ctx_item + + @abstractmethod + async def get_content(self, query: str) -> str: + """Retrieve the content given the query + (e.g. search the codebase, return search results)""" + raise NotImplementedError + + @abstractmethod + async def setup(self): + """Run any setup needed (e.g. indexing the codebase)""" + raise NotImplementedError + + +""" +class ExampleDynamicProvider(DynamicProvider): + title = "example" + name = "Example" + description = "Example description" + + async def get_content(self, query: str) -> str: + return f"Example content for '{query}'" + + async def setup(self): + print("Example setup") +""" diff --git a/server/continuedev/plugins/context_providers/embeddings.py b/server/continuedev/plugins/context_providers/embeddings.py new file mode 100644 index 00000000..86cba311 --- /dev/null +++ b/server/continuedev/plugins/context_providers/embeddings.py @@ -0,0 +1,81 @@ +import os +import uuid +from typing import List, Optional + +from pydantic import BaseModel + +from ...core.context import ContextProvider +from ...core.main import ContextItem, ContextItemDescription, ContextItemId +from ...libs.chroma.query import ChromaIndexManager + + +class EmbeddingResult(BaseModel): + filename: str + content: str + + +class EmbeddingsProvider(ContextProvider): + title = "embed" + + display_title = "Embeddings Search" + description = "Search the codebase using embeddings" + dynamic = True + requires_query = True + + workspace_directory: str + + EMBEDDINGS_CONTEXT_ITEM_ID = "embeddings" + + index_manager: Optional[ChromaIndexManager] = None + + class Config: + arbitrary_types_allowed = True + + @property + def index(self): + if self.index_manager is None: + self.index_manager = ChromaIndexManager(self.workspace_directory) + return self.index_manager + + @property + def BASE_CONTEXT_ITEM(self): + return ContextItem( + content="", + description=ContextItemDescription( + name="Embedding Search", + description="Enter a query to embedding search codebase", + id=ContextItemId( + provider_title=self.title, item_id=self.EMBEDDINGS_CONTEXT_ITEM_ID + ), + ), + ) + + async def _get_query_results(self, query: str) -> str: + results = self.index.query_codebase_index(query) + + ret = [] + for node in results.source_nodes: + resource_name = list(node.node.relationships.values())[0] + filepath = resource_name[: resource_name.index("::")] + ret.append(EmbeddingResult(filename=filepath, content=node.node.text)) + + return ret + + async def provide_context_items(self) -> List[ContextItem]: + self.index.create_codebase_index() # TODO Synchronous here is not ideal + + return [self.BASE_CONTEXT_ITEM] + + async def add_context_item(self, id: ContextItemId, query: str): + if not id.provider_title == self.title: + raise Exception("Invalid provider title for item") + + results = await self._get_query_results(query) + + for i in range(len(results)): + result = results[i] + ctx_item = self.BASE_CONTEXT_ITEM.copy() + ctx_item.description.name = os.path.basename(result.filename) + ctx_item.content = f"{result.filename}\n```\n{result.content}\n```" + ctx_item.description.id.item_id = uuid.uuid4().hex + self.selected_items.append(ctx_item) diff --git a/server/continuedev/plugins/context_providers/file.py b/server/continuedev/plugins/context_providers/file.py new file mode 100644 index 00000000..4cfbcfdb --- /dev/null +++ b/server/continuedev/plugins/context_providers/file.py @@ -0,0 +1,136 @@ +import asyncio +import os +from typing import List, Optional + +from ...core.context import ContextProvider +from ...core.main import ContextItem, ContextItemDescription, ContextItemId +from ...core.sdk import ContinueSDK +from ...libs.util.filter_files import DEFAULT_IGNORE_PATTERNS +from ...libs.util.logging import logger +from .util import remove_meilisearch_disallowed_chars + +MAX_SIZE_IN_CHARS = 50_000 + + +async def get_file_contents(filepath: str, sdk: ContinueSDK) -> str: + try: + return (await sdk.ide.readFile(filepath))[:MAX_SIZE_IN_CHARS] + except Exception as _: + return None + + +class FileContextProvider(ContextProvider): + """ + The FileContextProvider is a ContextProvider that allows you to search files in the open workspace. + """ + + title = "file" + ignore_patterns: List[str] = DEFAULT_IGNORE_PATTERNS + + display_title = "Files" + description = "Reference files in the current workspace" + dynamic = False + + async def start(self, *args): + await super().start(*args) + + async def on_file_saved(filepath: str, contents: str): + item = await self.get_context_item_for_filepath(filepath) + if item is None: + return + await self.update_documents([item], self.sdk.ide.workspace_directory) + + async def on_files_created(filepaths: List[str]): + items = await asyncio.gather( + *[ + self.get_context_item_for_filepath(filepath) + for filepath in filepaths + ] + ) + items = [item for item in items if item is not None] + await self.update_documents(items, self.sdk.ide.workspace_directory) + + async def on_files_deleted(filepaths: List[str]): + ids = [self.get_id_for_filepath(filepath) for filepath in filepaths] + + await self.delete_documents(ids) + + async def on_files_renamed(old_filepaths: List[str], new_filepaths: List[str]): + if self.sdk.ide.workspace_directory is None: + return + + old_ids = [self.get_id_for_filepath(filepath) for filepath in old_filepaths] + new_docs = await asyncio.gather( + *[ + self.get_context_item_for_filepath(filepath) + for filepath in new_filepaths + ] + ) + new_docs = [doc for doc in new_docs if doc is not None] + + await self.delete_documents(old_ids) + await self.update_documents(new_docs, self.sdk.ide.workspace_directory) + + self.sdk.ide.subscribeToFileSaved(on_file_saved) + self.sdk.ide.subscribeToFilesCreated(on_files_created) + self.sdk.ide.subscribeToFilesDeleted(on_files_deleted) + self.sdk.ide.subscribeToFilesRenamed(on_files_renamed) + + def get_id_for_filepath(self, absolute_filepath: str) -> str: + return remove_meilisearch_disallowed_chars(absolute_filepath) + + async def get_context_item_for_filepath( + self, absolute_filepath: str + ) -> Optional[ContextItem]: + content = await get_file_contents(absolute_filepath, self.sdk) + if content is None: + return None + + workspace_dir = self.sdk.ide.workspace_directory + if ( + os.path.splitdrive(workspace_dir)[0] + != os.path.splitdrive(absolute_filepath)[0] + ): + workspace_dir = ( + os.path.splitdrive(absolute_filepath)[0] + + os.path.splitdrive(workspace_dir)[1] + ) + + try: + relative_to_workspace = os.path.relpath(absolute_filepath, workspace_dir) + except Exception as e: + logger.warning(f"Error getting relative path: {e}") + return None + + return ContextItem( + content=content[: min(2000, len(content))], + description=ContextItemDescription( + name=os.path.basename(absolute_filepath), + # We should add the full path to the ContextItem + # It warrants a data modeling discussion and has no immediate use case + description=relative_to_workspace, + id=ContextItemId( + provider_title=self.title, + item_id=self.get_id_for_filepath(absolute_filepath), + ), + ), + ) + + async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]: + contents = await self.sdk.ide.listDirectoryContents(workspace_dir, True) + if contents is None: + return [] + + absolute_filepaths: List[str] = [] + for filepath in contents[:1000]: + absolute_filepaths.append(filepath) + + items = await asyncio.gather( + *[ + self.get_context_item_for_filepath(filepath) + for filepath in absolute_filepaths + ] + ) + items = list(filter(lambda item: item is not None, items)) + + return items diff --git a/server/continuedev/plugins/context_providers/filetree.py b/server/continuedev/plugins/context_providers/filetree.py new file mode 100644 index 00000000..5b3d3a50 --- /dev/null +++ b/server/continuedev/plugins/context_providers/filetree.py @@ -0,0 +1,89 @@ +from typing import List + +from pydantic import BaseModel, Field + +from ...core.context import ContextProvider +from ...core.main import ContextItem, ContextItemDescription, ContextItemId + + +class Directory(BaseModel): + name: str + files: List[str] + directories: List["Directory"] + + +def format_file_tree(tree: Directory, indentation: str = "") -> str: + result = "" + for file in tree.files: + result += f"{indentation}{file}\n" + + for directory in tree.directories: + result += f"{indentation}{directory.name}/\n" + result += format_file_tree(directory, indentation + " ") + + return result + + +def split_path(path: str, with_root=None) -> List[str]: + parts = path.split("/") if "/" in path else path.split("\\") + if with_root is not None: + root_parts = split_path(with_root) + parts = parts[len(root_parts) - 1 :] + + return parts + + +class FileTreeContextProvider(ContextProvider): + """Type '@tree' to reference the contents of your current workspace. The LLM will be able to see the nested directory structure of your project.""" + + title = "tree" + display_title = "File Tree" + description = "Add a formatted file tree of this directory to the context" + dynamic = True + + workspace_dir: str = Field(None, description="The workspace directory to display") + + async def _get_file_tree(self, directory: str) -> str: + contents = await self.sdk.ide.listDirectoryContents(directory, recursive=True) + + tree = Directory( + name=split_path(self.workspace_dir)[-1], files=[], directories=[] + ) + + for file in contents: + parts = split_path(file, with_root=self.workspace_dir) + + current_tree = tree + for part in parts[:-1]: + if part not in [d.name for d in current_tree.directories]: + current_tree.directories.append( + Directory(name=part, files=[], directories=[]) + ) + + current_tree = [d for d in current_tree.directories if d.name == part][ + 0 + ] + + current_tree.files.append(parts[-1]) + + return format_file_tree(tree) + + async def _filetree_context_item(self): + return ContextItem( + content=await self._get_file_tree(self.workspace_dir), + description=ContextItemDescription( + name="File Tree", + description="Add a formatted file tree of this directory to the context", + id=ContextItemId(provider_title=self.title, item_id=self.title), + ), + ) + + async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]: + self.workspace_dir = workspace_dir + return [await self._filetree_context_item()] + + async def get_item(self, id: ContextItemId, query: str) -> ContextItem: + if not id.provider_title == self.title: + raise Exception("Invalid provider title for item") + + return await self._filetree_context_item() diff --git a/server/continuedev/plugins/context_providers/github.py b/server/continuedev/plugins/context_providers/github.py new file mode 100644 index 00000000..c031f310 --- /dev/null +++ b/server/continuedev/plugins/context_providers/github.py @@ -0,0 +1,49 @@ +from typing import List + +from github import Auth, Github +from pydantic import Field + +from ...core.context import ( + ContextItem, + ContextItemDescription, + ContextItemId, + ContextProvider, +) + + +class GitHubIssuesContextProvider(ContextProvider): + """ + The GitHubIssuesContextProvider is a ContextProvider that allows you to search GitHub issues in a repo. Type '@issue' to reference the title and contents of an issue. + """ + + title = "issues" + repo_name: str = Field( + ..., description="The name of the GitHub repo from which to pull issues" + ) + auth_token: str = Field( + ..., + description="The GitHub auth token to use to authenticate with the GitHub API", + ) + + display_title = "GitHub Issues" + description = "Reference GitHub issues" + dynamic = False + + async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]: + auth = Auth.Token(self.auth_token) + gh = Github(auth=auth) + + repo = gh.get_repo(self.repo_name) + issues = repo.get_issues().get_page(0) + + return [ + ContextItem( + content=issue.body, + description=ContextItemDescription( + name=f"Issue #{issue.number}", + description=issue.title, + id=ContextItemId(provider_title=self.title, item_id=issue.id), + ), + ) + for issue in issues + ] diff --git a/server/continuedev/plugins/context_providers/google.py b/server/continuedev/plugins/context_providers/google.py new file mode 100644 index 00000000..852f4e9a --- /dev/null +++ b/server/continuedev/plugins/context_providers/google.py @@ -0,0 +1,70 @@ +import json +from typing import List + +import aiohttp +from pydantic import Field + +from ...core.context import ContextProvider +from ...core.main import ContextItem, ContextItemDescription, ContextItemId +from .util import remove_meilisearch_disallowed_chars + + +class GoogleContextProvider(ContextProvider): + """Type '@google' to reference the results of a Google search. For example, type "@google python tutorial" if you want to search and discuss ways of learning Python.""" + + title = "google" + display_title = "Google" + description = "Search Google" + dynamic = True + requires_query = True + + serper_api_key: str = Field( + ..., + description="Your SerpAPI key, used to programmatically make Google searches. You can get a key at https://serper.dev.", + ) + + _GOOGLE_CONTEXT_ITEM_ID = "google_search" + + @property + def BASE_CONTEXT_ITEM(self): + return ContextItem( + content="", + description=ContextItemDescription( + name="Google Search", + description="Enter a query to search google", + id=ContextItemId( + provider_title=self.title, item_id=self._GOOGLE_CONTEXT_ITEM_ID + ), + ), + ) + + async def _google_search(self, query: str) -> str: + url = "https://google.serper.dev/search" + + payload = json.dumps({"q": query}) + headers = {"X-API-KEY": self.serper_api_key, "Content-Type": "application/json"} + + async with aiohttp.ClientSession() as session: + async with session.post(url, headers=headers, data=payload) as response: + return await response.text() + + async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]: + return [self.BASE_CONTEXT_ITEM] + + async def get_item(self, id: ContextItemId, query: str) -> ContextItem: + if not id.provider_title == self.title: + raise Exception("Invalid provider title for item") + + results = await self._google_search(query) + json_results = json.loads(results) + content = f"Google Search: {query}\n\n" + if answerBox := json_results.get("answerBox"): + content += f"Answer Box ({answerBox['title']}): {answerBox['answer']}\n\n" + + for result in json_results["organic"]: + content += f"{result['title']}\n{result['link']}\n{result['snippet']}\n\n" + + ctx_item = self.BASE_CONTEXT_ITEM.copy() + ctx_item.content = content + ctx_item.description.id.item_id = remove_meilisearch_disallowed_chars(query) + return ctx_item diff --git a/server/continuedev/plugins/context_providers/highlighted_code.py b/server/continuedev/plugins/context_providers/highlighted_code.py new file mode 100644 index 00000000..3304a71d --- /dev/null +++ b/server/continuedev/plugins/context_providers/highlighted_code.py @@ -0,0 +1,293 @@ +import os +from typing import Any, Dict, List, Optional + +from pydantic import BaseModel + +from ...core.context import ( + ContextItem, + ContextItemDescription, + ContextItemId, + ContextProvider, +) +from ...core.main import ChatMessage +from ...models.filesystem import RangeInFileWithContents +from ...models.main import Range + + +class HighlightedRangeContextItem(BaseModel): + rif: RangeInFileWithContents + item: ContextItem + + +class HighlightedCodeContextProvider(ContextProvider): + """ + The ContextProvider class is a plugin that lets you provide new information to the LLM by typing '@'. + When you type '@', the context provider will be asked to populate a list of options. + These options will be updated on each keystroke. + When you hit enter on an option, the context provider will add that item to the autopilot's list of context (which is all stored in the ContextManager object). + """ + + title = "code" + display_title = "Highlighted Code" + description = "Highlight code" + dynamic = True + + ide: Any # IdeProtocolServer + + highlighted_ranges: List[HighlightedRangeContextItem] = [] + adding_highlighted_code: bool = True + # Controls whether you can have more than one highlighted range. Now always True. + + should_get_fallback_context_item: bool = True + last_added_fallback: bool = False + + async def _get_fallback_context_item(self) -> HighlightedRangeContextItem: + # Used to automatically include the currently open file. Disabled for now. + return None + + if not self.should_get_fallback_context_item: + return None + + visible_files = await self.ide.getVisibleFiles() + if len(visible_files) > 0: + content = await self.ide.readFile(visible_files[0]) + rif = RangeInFileWithContents.from_entire_file(visible_files[0], content) + + item = self._rif_to_context_item(rif, 0, True) + item.description.name = self._rif_to_name(rif, show_line_nums=False) + + self.last_added_fallback = True + return HighlightedRangeContextItem(rif=rif, item=item) + + return None + + async def get_selected_items(self) -> List[ContextItem]: + items = [hr.item for hr in self.highlighted_ranges] + + if len(items) == 0 and ( + fallback_item := await self._get_fallback_context_item() + ): + items = [fallback_item.item] + + return items + + async def get_chat_messages(self) -> List[ContextItem]: + ranges = self.highlighted_ranges + if len(ranges) == 0 and ( + fallback_item := await self._get_fallback_context_item() + ): + ranges = [fallback_item] + + return [ + ChatMessage( + role="user", + content=f"Code in this file is highlighted ({r.rif.filepath}):\n```\n{r.rif.contents}\n```", + summary=f"Code in this file is highlighted: {r.rif.filepath}", + ) + for r in ranges + ] + + def _make_sure_is_editing_range(self): + """If none of the highlighted ranges are currently being edited, the first should be selected""" + if len(self.highlighted_ranges) == 0: + return + if not any(map(lambda x: x.item.editing, self.highlighted_ranges)): + self.highlighted_ranges[0].item.editing = True + + def _disambiguate_highlighted_ranges(self): + """If any files have the same name, also display their folder name""" + name_status: Dict[ + str, set + ] = {} # basename -> set of full paths with that basename + for hr in self.highlighted_ranges: + basename = os.path.basename(hr.rif.filepath) + if basename in name_status: + name_status[basename].add(hr.rif.filepath) + else: + name_status[basename] = {hr.rif.filepath} + + for hr in self.highlighted_ranges: + basename = os.path.basename(hr.rif.filepath) + if len(name_status[basename]) > 1: + hr.item.description.name = self._rif_to_name( + hr.rif, + display_filename=os.path.join( + os.path.basename(os.path.dirname(hr.rif.filepath)), basename + ), + ) + else: + hr.item.description.name = self._rif_to_name( + hr.rif, display_filename=basename + ) + + async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]: + return [] + + async def get_item(self, id: ContextItemId, query: str) -> ContextItem: + raise NotImplementedError() + + async def clear_context(self): + self.highlighted_ranges = [] + self.adding_highlighted_code = False + self.should_get_fallback_context_item = True + self.last_added_fallback = False + + async def delete_context_with_ids( + self, ids: List[ContextItemId] + ) -> List[ContextItem]: + ids_to_delete = [id.item_id for id in ids] + + kept_ranges = [] + for hr in self.highlighted_ranges: + if hr.item.description.id.item_id not in ids_to_delete: + kept_ranges.append(hr) + self.highlighted_ranges = kept_ranges + + self._make_sure_is_editing_range() + + if len(self.highlighted_ranges) == 0 and self.last_added_fallback: + self.should_get_fallback_context_item = False + + return [hr.item for hr in self.highlighted_ranges] + + def _rif_to_name( + self, + rif: RangeInFileWithContents, + display_filename: str = None, + show_line_nums: bool = True, + ) -> str: + line_nums = ( + f" ({rif.range.start.line + 1}-{rif.range.end.line + 1})" + if show_line_nums + else "" + ) + return f"{display_filename or os.path.basename(rif.filepath)}{line_nums}" + + def _rif_to_context_item( + self, rif: RangeInFileWithContents, idx: int, editing: bool + ) -> ContextItem: + return ContextItem( + description=ContextItemDescription( + name=self._rif_to_name(rif), + description=rif.filepath, + id=ContextItemId(provider_title=self.title, item_id=str(idx)), + ), + content=rif.contents, + editing=editing if editing is not None else False, + editable=True, + ) + + async def handle_highlighted_code( + self, + range_in_files: List[RangeInFileWithContents], + edit: Optional[bool] = False, + ): + self.should_get_fallback_context_item = True + self.last_added_fallback = False + + # Filter out rifs from ~/.continue/diffs folder + range_in_files = [ + rif + for rif in range_in_files + if not os.path.dirname(rif.filepath) + == os.path.expanduser("~/.continue/diffs") + ] + + # If not adding highlighted code + if not self.adding_highlighted_code: + if ( + len(self.highlighted_ranges) == 1 + and len(range_in_files) <= 1 + and ( + len(range_in_files) == 0 + or range_in_files[0].range.start == range_in_files[0].range.end + ) + ): + # If un-highlighting the range to edit, then remove the range + self.highlighted_ranges = [] + elif len(range_in_files) > 0: + # Otherwise, replace the current range with the new one + # This is the first range to be highlighted + self.highlighted_ranges = [ + HighlightedRangeContextItem( + rif=range_in_files[0], + item=self._rif_to_context_item(range_in_files[0], 0, edit), + ) + ] + + return + + # If editing, make sure none of the other ranges are editing + if edit: + for hr in self.highlighted_ranges: + hr.item.editing = False + + # If new range overlaps with any existing, keep the existing but merged + new_ranges = [] + for i, new_hr in enumerate(range_in_files): + found_overlap_with = None + for existing_rif in self.highlighted_ranges: + if ( + new_hr.filepath == existing_rif.rif.filepath + and new_hr.range.overlaps_with(existing_rif.rif.range) + ): + existing_rif.rif.range = existing_rif.rif.range.merge_with( + new_hr.range + ) + found_overlap_with = existing_rif + break + + if found_overlap_with is None: + new_ranges.append( + HighlightedRangeContextItem( + rif=new_hr, + item=self._rif_to_context_item( + new_hr, len(self.highlighted_ranges) + i, edit + ), + ) + ) + elif edit: + # Want to update the range so it's only the newly selected portion + found_overlap_with.rif.range = new_hr.range + found_overlap_with.item.editing = True + + self.highlighted_ranges = self.highlighted_ranges + new_ranges + + self._make_sure_is_editing_range() + self._disambiguate_highlighted_ranges() + + async def set_editing_at_ids(self, ids: List[str]): + # Don't do anything if there are no valid ids here + count = 0 + for hr in self.highlighted_ranges: + if hr.item.description.id.item_id in ids: + count += 1 + + if count == 0: + return + + for hr in self.highlighted_ranges: + hr.item.editing = hr.item.description.id.item_id in ids + + async def add_context_item( + self, id: ContextItemId, query: str, prev: List[ContextItem] = None + ) -> List[ContextItem]: + raise NotImplementedError() + + async def manually_add_context_item(self, context_item: ContextItem): + full_file_content = await self.ide.readFile( + context_item.description.description + ) + self.highlighted_ranges.append( + HighlightedRangeContextItem( + rif=RangeInFileWithContents( + filepath=context_item.description.description, + range=Range.from_lines_snippet_in_file( + content=full_file_content, + snippet=context_item.content, + ), + contents=context_item.content, + ), + item=context_item, + ) + ) diff --git a/server/continuedev/plugins/context_providers/search.py b/server/continuedev/plugins/context_providers/search.py new file mode 100644 index 00000000..a36b2a0a --- /dev/null +++ b/server/continuedev/plugins/context_providers/search.py @@ -0,0 +1,90 @@ +from typing import List + +from pydantic import Field +from ripgrepy import Ripgrepy + +from ...core.context import ContextProvider +from ...core.main import ContextItem, ContextItemDescription, ContextItemId +from ...libs.util.logging import logger +from ...libs.util.ripgrep import get_rg_path +from .util import remove_meilisearch_disallowed_chars + + +class SearchContextProvider(ContextProvider): + """Type '@search' to reference the results of codebase search, just like the results you would get from VS Code search.""" + + title = "search" + display_title = "Search" + description = "Search the workspace for all matches of an exact string (e.g. '@search console.log')" + dynamic = True + requires_query = True + + _SEARCH_CONTEXT_ITEM_ID = "search" + + workspace_dir: str = Field(None, description="The workspace directory to search") + + @property + def BASE_CONTEXT_ITEM(self): + return ContextItem( + content="", + description=ContextItemDescription( + name="Search", + description="Search the workspace for all matches of an exact string (e.g. '@search console.log')", + id=ContextItemId( + provider_title=self.title, item_id=self._SEARCH_CONTEXT_ITEM_ID + ), + ), + ) + + async def _search(self, query: str) -> str: + rg = Ripgrepy(query, self.workspace_dir, rg_path=get_rg_path()) + results = rg.I().context(2).run() + return f"Search results in workspace for '{query}':\n\n{results}" + + # Custom display below - TODO + + # Gather results per file + file_to_matches = {} + for result in results: + if result["type"] == "match": + data = result["data"] + filepath = data["path"]["text"] + if filepath not in file_to_matches: + file_to_matches[filepath] = [] + + line_num_and_line = f"{data['line_number']}: {data['lines']['text']}" + file_to_matches[filepath].append(line_num_and_line) + + # Format results + content = f"Search results in workspace for '{query}':\n\n" + for filepath, matches in file_to_matches.items(): + content += f"{filepath}\n" + for match in matches: + content += f"{match}\n" + content += "\n" + + return content + + async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]: + self.workspace_dir = workspace_dir + + try: + Ripgrepy("", workspace_dir, rg_path=get_rg_path()) + except Exception as e: + logger.warning(f"Failed to initialize ripgrepy: {e}") + return [] + + return [self.BASE_CONTEXT_ITEM] + + async def get_item(self, id: ContextItemId, query: str) -> ContextItem: + if not id.provider_title == self.title: + raise Exception("Invalid provider title for item") + + query = query.lstrip("search ") + results = await self._search(query) + + ctx_item = self.BASE_CONTEXT_ITEM.copy() + ctx_item.content = results + ctx_item.description.name = f"Search: '{query}'" + ctx_item.description.id.item_id = remove_meilisearch_disallowed_chars(query) + return ctx_item diff --git a/server/continuedev/plugins/context_providers/terminal.py b/server/continuedev/plugins/context_providers/terminal.py new file mode 100644 index 00000000..c63239e4 --- /dev/null +++ b/server/continuedev/plugins/context_providers/terminal.py @@ -0,0 +1,49 @@ +from typing import Any, Coroutine, List + +from pydantic import Field + +from ...core.context import ContextProvider +from ...core.main import ChatMessage, ContextItem, ContextItemDescription, ContextItemId + + +class TerminalContextProvider(ContextProvider): + """Type '@terminal' to reference the contents of your IDE's terminal.""" + + title = "terminal" + display_title = "Terminal" + description = "Reference the contents of the terminal" + dynamic = True + + get_last_n_commands: int = Field( + 3, description="The number of previous commands to reference" + ) + + def _terminal_context_item(self, content: str = ""): + return ContextItem( + content=content, + description=ContextItemDescription( + name="Terminal", + description="Reference the contents of the VS Code terminal", + id=ContextItemId(provider_title=self.title, item_id=self.title), + ), + ) + + async def get_chat_messages(self) -> Coroutine[Any, Any, List[ChatMessage]]: + msgs = await super().get_chat_messages() + for msg in msgs: + msg.summary = msg.content[-1000:] + return msgs + + async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]: + return [self._terminal_context_item()] + + async def get_item(self, id: ContextItemId, query: str) -> ContextItem: + if not id.provider_title == self.title: + raise Exception("Invalid provider title for item") + + terminal_contents = await self.sdk.ide.getTerminalContents( + self.get_last_n_commands + ) + terminal_contents = terminal_contents[-5000:] + + return self._terminal_context_item(terminal_contents) diff --git a/server/continuedev/plugins/context_providers/url.py b/server/continuedev/plugins/context_providers/url.py new file mode 100644 index 00000000..1ed7c18e --- /dev/null +++ b/server/continuedev/plugins/context_providers/url.py @@ -0,0 +1,104 @@ +from typing import List + +import requests +from bs4 import BeautifulSoup +from pydantic import Field + +from ...core.context import ContextProvider +from ...core.main import ContextItem, ContextItemDescription, ContextItemId +from .util import remove_meilisearch_disallowed_chars + + +class URLContextProvider(ContextProvider): + """Type '@url' to reference the contents of a URL. You can either reference preset URLs, or reference one dynamically by typing '@url https://example.com'. The text contents of the page will be fetched and used as context.""" + + title = "url" + display_title = "URL" + description = "Reference the contents of a webpage" + dynamic = True + requires_query = True + + # Allows users to provide a list of preset urls + preset_urls: List[str] = Field( + [], + description="A list of preset URLs that you will be able to quickly reference by typing '@url'", + ) + + # Static items loaded from preset_urls + static_url_context_items: List[ContextItem] = [] + + # There is only a single dynamic url context item, so it has a static id + _DYNAMIC_URL_CONTEXT_ITEM_ID = "url" + + # This is a template dynamic item that will generate context item on demand + # when get item is called + @property + def DYNAMIC_CONTEXT_ITEM(self): + return ContextItem( + content="", + description=ContextItemDescription( + name="Dynamic URL", + description="Reference the contents of a webpage (e.g. '@url https://www.w3schools.com/python/python_ref_functions.asp')", + id=ContextItemId( + provider_title=self.title, item_id=self._DYNAMIC_URL_CONTEXT_ITEM_ID + ), + ), + ) + + def static_url_context_item_from_url(self, url: str) -> ContextItem: + content, title = self._get_url_text_contents_and_title(url) + return ContextItem( + content=content, + description=ContextItemDescription( + name=title, + description=f"Contents of {url}", + id=ContextItemId( + provider_title=self.title, + item_id=remove_meilisearch_disallowed_chars(url), + ), + ), + ) + + def _get_url_text_contents_and_title(self, url: str) -> (str, str): + response = requests.get(url) + soup = BeautifulSoup(response.text, "html.parser") + title = url.replace("https://", "").replace("http://", "").replace("www.", "") + if soup.title is not None: + title = soup.title.string + return soup.get_text(), title + + async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]: + self.static_url_context_items = [ + self.static_url_context_item_from_url(url) for url in self.preset_urls + ] + + return [self.DYNAMIC_CONTEXT_ITEM] + self.static_url_context_items + + async def get_item(self, id: ContextItemId, query: str) -> ContextItem: + # Check if the item is a static item + matching_static_item = next( + ( + item + for item in self.static_url_context_items + if item.description.id.item_id == id.item_id + ), + None, + ) + if matching_static_item: + return matching_static_item + + # Check if the item is the dynamic item + if not id.provider_title == self.title: + raise Exception("Invalid provider title for item") + + # Generate the dynamic item + url = query.lstrip("url ").strip() + if url is None or url == "": + return None + content, title = self._get_url_text_contents_and_title(url) + + ctx_item = self.DYNAMIC_CONTEXT_ITEM.copy() + ctx_item.content = content + ctx_item.description.name = title + ctx_item.description.id.item_id = remove_meilisearch_disallowed_chars(url) + return ctx_item diff --git a/server/continuedev/plugins/context_providers/util.py b/server/continuedev/plugins/context_providers/util.py new file mode 100644 index 00000000..61bea8aa --- /dev/null +++ b/server/continuedev/plugins/context_providers/util.py @@ -0,0 +1,5 @@ +import re + + +def remove_meilisearch_disallowed_chars(id: str) -> str: + return re.sub(r"[^0-9a-zA-Z_-]", "", id) diff --git a/server/continuedev/plugins/policies/commit.py b/server/continuedev/plugins/policies/commit.py new file mode 100644 index 00000000..2fa43676 --- /dev/null +++ b/server/continuedev/plugins/policies/commit.py @@ -0,0 +1,77 @@ +# An agent that makes a full commit in the background +# Plans +# Write code +# Reviews code +# Cleans up + +# It's important that agents are configurable, because people need to be able to specify +# which hooks they want to run. Specific linter, run tests, etc. +# And all of this can be easily specified in the Policy. + + +from textwrap import dedent +from typing import Literal + +from ...core.config import ContinueConfig +from ...core.main import History, Policy, Step +from ...core.observation import TextObservation +from ...core.sdk import ContinueSDK + + +class PlanStep(Step): + user_input: str + + _prompt = dedent( + """\ + You were given the following instructions: "{user_input}". + + Create a plan for how you will complete the task. + + Here are relevant files: + + {relevant_files} + + Your plan will include: + 1. A high-level description of how you are going to accomplish the task + 2. A list of which files you will edit + 3. A description of what you will change in each file + """ + ) + + async def run(self, sdk: ContinueSDK): + plan = await sdk.models.default.complete( + self._prompt.format( + {"user_input": self.user_input, "relevant_files": "TODO"} + ) + ) + return TextObservation(text=plan) + + +class WriteCommitStep(Step): + async def run(self, sdk: ContinueSDK): + pass + + +class ReviewCodeStep(Step): + async def run(self, sdk: ContinueSDK): + pass + + +class CleanupStep(Step): + async def run(self, sdk: ContinueSDK): + pass + + +class CommitPolicy(Policy): + user_input: str + + current_step: Literal["plan", "write", "review", "cleanup"] = "plan" + + def next(self, config: ContinueConfig, history: History) -> Step: + if history.get_current() is None: + return ( + PlanStep(user_input=self.user_input) + >> WriteCommitStep() + >> ReviewCodeStep() + >> CleanupStep() + ) diff --git a/server/continuedev/plugins/policies/default.py b/server/continuedev/plugins/policies/default.py new file mode 100644 index 00000000..574d2a1c --- /dev/null +++ b/server/continuedev/plugins/policies/default.py @@ -0,0 +1,85 @@ +from typing import Type, Union + +from ...core.config import ContinueConfig +from ...core.main import History, Policy, Step +from ...core.observation import UserInputObservation +from ..steps.chat import SimpleChatStep +from ..steps.custom_command import CustomCommandStep +from ..steps.main import EditHighlightedCodeStep +from ..steps.steps_on_startup import StepsOnStartupStep + + +def parse_slash_command(inp: str, config: ContinueConfig) -> Union[None, Step]: + """ + Parses a slash command, returning the command name and the rest of the input. + """ + if inp.startswith("/"): + command_name = inp.split(" ")[0].strip() + after_command = " ".join(inp.split(" ")[1:]) + + for slash_command in config.slash_commands: + if slash_command.name == command_name[1:]: + params = slash_command.params + params["user_input"] = after_command + try: + return slash_command.step(**params) + except TypeError as e: + raise Exception( + f"Incorrect params used for slash command '{command_name}': {e}" + ) + return None + + +def parse_custom_command(inp: str, config: ContinueConfig) -> Union[None, Step]: + command_name = inp.split(" ")[0].strip() + after_command = " ".join(inp.split(" ")[1:]) + for custom_cmd in config.custom_commands: + if custom_cmd.name == command_name[1:]: + slash_command = parse_slash_command(custom_cmd.prompt, config) + if slash_command is not None: + return slash_command + return CustomCommandStep( + name=custom_cmd.name, + description=custom_cmd.description, + prompt=custom_cmd.prompt, + user_input=after_command, + slash_command=command_name, + ) + return None + + +class DefaultPolicy(Policy): + default_step: Type[Step] = SimpleChatStep + default_params: dict = {} + + def next(self, config: ContinueConfig, history: History) -> Step: + # At the very start, run initial Steps specified in the config + if history.get_current() is None: + return StepsOnStartupStep() + + observation = history.get_current().observation + if observation is not None and isinstance(observation, UserInputObservation): + # This could be defined with ObservationTypePolicy. Ergonomics not right though. + user_input = observation.user_input + + slash_command = parse_slash_command(user_input, config) + if slash_command is not None: + if ( + getattr(slash_command, "user_input", None) is None + and history.get_current().step.user_input is not None + ): + history.get_current().step.user_input = ( + history.get_current().step.user_input.split()[0] + ) + return slash_command + + custom_command = parse_custom_command(user_input, config) + if custom_command is not None: + return custom_command + + if user_input.startswith("/edit"): + return EditHighlightedCodeStep(user_input=user_input[5:]) + + return self.default_step(**self.default_params) + + return None diff --git a/server/continuedev/plugins/policies/headless.py b/server/continuedev/plugins/policies/headless.py new file mode 100644 index 00000000..9fa0f3f2 --- /dev/null +++ b/server/continuedev/plugins/policies/headless.py @@ -0,0 +1,18 @@ +from ...core.config import ContinueConfig +from ...core.main import History, Policy, Step +from ...core.observation import TextObservation +from ...core.steps import ShellCommandsStep +from ...plugins.steps.on_traceback import DefaultOnTracebackStep + + +class HeadlessPolicy(Policy): + command: str + + def next(self, config: ContinueConfig, history: History) -> Step: + if history.get_current() is None: + return ShellCommandsStep(cmds=[self.command]) + observation = history.get_current().observation + if isinstance(observation, TextObservation): + return DefaultOnTracebackStep(output=observation.text) + + return None diff --git a/server/continuedev/plugins/recipes/AddTransformRecipe/README.md b/server/continuedev/plugins/recipes/AddTransformRecipe/README.md new file mode 100644 index 00000000..78d603a2 --- /dev/null +++ b/server/continuedev/plugins/recipes/AddTransformRecipe/README.md @@ -0,0 +1,9 @@ +# AddTransformRecipe
+
+Uses the Chess.com API example to show how to add map and filter Python transforms to a dlt pipeline.
+
+Background
+
+- https://dlthub.com/docs/general-usage/resource#filter-transform-and-pivot-data
+- https://dlthub.com/docs/customizations/customizing-pipelines/renaming_columns
+- https://dlthub.com/docs/customizations/customizing-pipelines/pseudonymizing_columns
diff --git a/server/continuedev/plugins/recipes/AddTransformRecipe/dlt_transform_docs.md b/server/continuedev/plugins/recipes/AddTransformRecipe/dlt_transform_docs.md new file mode 100644 index 00000000..864aea87 --- /dev/null +++ b/server/continuedev/plugins/recipes/AddTransformRecipe/dlt_transform_docs.md @@ -0,0 +1,142 @@ +# Customize resources + +## Filter, transform and pivot data + +You can attach any number of transformations that are evaluated on item per item basis to your resource. The available transformation types: + +- map - transform the data item (resource.add_map) +- filter - filter the data item (resource.add_filter) +- yield map - a map that returns iterator (so single row may generate many rows - resource.add_yield_map) + +Example: We have a resource that loads a list of users from an api endpoint. We want to customize it so: + +- we remove users with user_id == 'me' +- we anonymize user data + Here's our resource: + +```python +import dlt + +@dlt.resource(write_disposition='replace') +def users(): + ... + users = requests.get(...) + ... + yield users +``` + +Here's our script that defines transformations and loads the data. + +```python +from pipedrive import users + +def anonymize_user(user_data): + user_data['user_id'] = hash_str(user_data['user_id']) + user_data['user_email'] = hash_str(user_data['user_email']) + return user_data + +# add the filter and anonymize function to users resource and enumerate +for user in users().add_filter(lambda user: user['user_id'] != 'me').add_map(anonymize_user): +print(user) +``` + +Here is a more complex example of a filter transformation: + + # Renaming columns + ## Renaming columns by replacing the special characters + + In the example below, we create a dummy source with special characters in the name. We then write a function that we intend to apply to the resource to modify its output (i.e. replacing the German umlaut): replace_umlauts_in_dict_keys. + ```python + import dlt + + # create a dummy source with umlauts (special characters) in key names (um) + @dlt.source + def dummy_source(prefix: str = None): + @dlt.resource + def dummy_data(): + for _ in range(100): + yield {f'Objekt_{_}':{'Größe':_, 'Äquivalenzprüfung':True}} + return dummy_data(), + + def replace_umlauts_in_dict_keys(d): + # Replaces umlauts in dictionary keys with standard characters. + umlaut_map = {'ä': 'ae', 'ö': 'oe', 'ü': 'ue', 'ß': 'ss', 'Ä': 'Ae', 'Ö': 'Oe', 'Ü': 'Ue'} + result = {} + for k, v in d.items(): + new_key = ''.join(umlaut_map.get(c, c) for c in k) + if isinstance(v, dict): + result[new_key] = replace_umlauts_in_dict_keys(v) + else: + result[new_key] = v + return result + + # We can add the map function to the resource + + # 1. Create an instance of the source so you can edit it. + data_source = dummy_source() + + # 2. Modify this source instance's resource + data_source = data_source.dummy_data().add_map(replace_umlauts_in_dict_keys) + + # 3. Inspect your result + for row in data_source: + print(row) + + # {'Objekt_0': {'Groesse': 0, 'Aequivalenzpruefung': True}} + # ... + ``` + +Here is a more complex example of a map transformation: + +# Pseudonymizing columns + +## Pseudonymizing (or anonymizing) columns by replacing the special characters + +Pseudonymization is a deterministic way to hide personally identifiable info (PII), enabling us to consistently achieve the same mapping. If instead you wish to anonymize, you can delete the data, or replace it with a constant. In the example below, we create a dummy source with a PII column called 'name', which we replace with deterministic hashes (i.e. replacing the German umlaut). + +```python +import dlt +import hashlib + +@dlt.source +def dummy_source(prefix: str = None): + @dlt.resource + def dummy_data(): + for _ in range(3): + yield {'id':_, 'name': f'Jane Washington {_}'} + return dummy_data(), + +def pseudonymize_name(doc): + Pseudonmyisation is a deterministic type of PII-obscuring + Its role is to allow identifying users by their hash, without revealing the underlying info. + + # add a constant salt to generate + salt = 'WI@N57%zZrmk#88c' + salted_string = doc['name'] + salt + sh = hashlib.sha256() + sh.update(salted_string.encode()) + hashed_string = sh.digest().hex() + doc['name'] = hashed_string + return doc + + # run it as is + for row in dummy_source().dummy_data().add_map(pseudonymize_name): + print(row) + + #{'id': 0, 'name': '96259edb2b28b48bebce8278c550e99fbdc4a3fac8189e6b90f183ecff01c442'} + #{'id': 1, 'name': '92d3972b625cbd21f28782fb5c89552ce1aa09281892a2ab32aee8feeb3544a1'} + #{'id': 2, 'name': '443679926a7cff506a3b5d5d094dc7734861352b9e0791af5d39db5a7356d11a'} + + # Or create an instance of the data source, modify the resource and run the source. + + # 1. Create an instance of the source so you can edit it. + data_source = dummy_source() + # 2. Modify this source instance's resource + data_source = data_source.dummy_data().add_map(replace_umlauts_in_dict_keys) + # 3. Inspect your result + for row in data_source: + print(row) + + pipeline = dlt.pipeline(pipeline_name='example', destination='bigquery', dataset_name='normalized_data') + load_info = pipeline.run(data_source) +``` diff --git a/server/continuedev/plugins/recipes/AddTransformRecipe/main.py b/server/continuedev/plugins/recipes/AddTransformRecipe/main.py new file mode 100644 index 00000000..583cef1a --- /dev/null +++ b/server/continuedev/plugins/recipes/AddTransformRecipe/main.py @@ -0,0 +1,31 @@ +from textwrap import dedent + +from ....core.main import Step +from ....core.sdk import ContinueSDK +from ....core.steps import MessageStep, WaitForUserInputStep +from .steps import AddTransformStep, SetUpChessPipelineStep + + +class AddTransformRecipe(Step): + hide: bool = True + + async def run(self, sdk: ContinueSDK): + text_observation = await sdk.run_step( + MessageStep( + message=dedent( + """\ + This recipe will walk you through the process of adding a transform to a dlt pipeline that uses the chess.com API source. With the help of Continue, you will: + - Set up a dlt pipeline for the chess.com API + - Add a filter or map transform to the pipeline + - Run the pipeline and view the transformed data in a Streamlit app""" + ), + name="Add transformation to a dlt pipeline", + ) + >> SetUpChessPipelineStep() + >> WaitForUserInputStep( + prompt="How do you want to transform the Chess.com API data before loading it? For example, you could filter out games that ended in a draw." + ) + ) + await sdk.run_step( + AddTransformStep(transform_description=text_observation.text) + ) diff --git a/server/continuedev/plugins/recipes/AddTransformRecipe/steps.py b/server/continuedev/plugins/recipes/AddTransformRecipe/steps.py new file mode 100644 index 00000000..61638374 --- /dev/null +++ b/server/continuedev/plugins/recipes/AddTransformRecipe/steps.py @@ -0,0 +1,106 @@ +import os +from textwrap import dedent + +from ....core.main import Step +from ....core.sdk import ContinueSDK, Models +from ....core.steps import MessageStep +from ....libs.util.paths import find_data_file + +AI_ASSISTED_STRING = "(✨ AI-Assisted ✨)" + + +class SetUpChessPipelineStep(Step): + hide: bool = True + name: str = "Setup Chess.com API dlt Pipeline" + + async def describe(self, models: Models): + return "This step will create a new dlt pipeline that loads data from the chess.com API." + + async def run(self, sdk: ContinueSDK): + # running commands to get started when creating a new dlt pipeline + await sdk.run( + [ + "python3 -m venv .env", + "source .env/bin/activate", + "pip install dlt", + "dlt --non-interactive init chess duckdb", + "pip install -r requirements.txt", + "pip install pandas streamlit", # Needed for the pipeline show step later + ], + name="Set up Python environment", + description=dedent( + """\ + - Create a Python virtual environment: `python3 -m venv .env` + - Activate the virtual environment: `source .env/bin/activate` + - Install dlt: `pip install dlt` + - Create a new dlt pipeline called "chess" that loads data into a local DuckDB instance: `dlt init chess duckdb` + - Install the Python dependencies for the pipeline: `pip install -r requirements.txt`""" + ), + ) + + +class AddTransformStep(Step): + hide: bool = True + + # e.g. "Use the `python-chess` library to decode the moves in the game data" + transform_description: str + + async def run(self, sdk: ContinueSDK): + source_name = "chess" + filename = f"{source_name}_pipeline.py" + abs_filepath = os.path.join(sdk.ide.workspace_directory, filename) + + # Open the file and highlight the function to be edited + await sdk.ide.setFileOpen(abs_filepath) + + await sdk.run_step( + MessageStep( + message=dedent( + """\ + This step will customize your resource function with a transform of your choice: + - Add a filter or map transformation depending on your request + - Load the data into a local DuckDB instance + - Open up a Streamlit app for you to view the data""" + ), + name="Write transformation function", + ) + ) + + with open(find_data_file("dlt_transform_docs.md")) as f: + dlt_transform_docs = f.read() + + prompt = dedent( + f"""\ + Task: Write a transform function using the description below and then use `add_map` or `add_filter` from the `dlt` library to attach it a resource. + + Description: {self.transform_description} + + Here are some docs pages that will help you better understand how to use `dlt`. + + {dlt_transform_docs}""" + ) + + # edit the pipeline to add a transform function and attach it to a resource + await sdk.edit_file( + filename=filename, + prompt=prompt, + name=f"Writing transform function {AI_ASSISTED_STRING}", + ) + + await sdk.wait_for_user_confirmation( + "Press Continue to confirm that the changes are okay before we run the pipeline." + ) + + # run the pipeline and load the data + await sdk.run( + f"python3 {filename}", + name="Run the pipeline", + description=f"Running `python3 {filename}` to load the data into a local DuckDB instance", + ) + + # run a streamlit app to show the data + await sdk.run( + f"dlt pipeline {source_name}_pipeline show", + name="Show data in a Streamlit app", + description=f"Running `dlt pipeline {source_name} show` to show the data in a Streamlit app, where you can view and play with the data.", + ) diff --git a/server/continuedev/plugins/recipes/ContinueRecipeRecipe/README.md b/server/continuedev/plugins/recipes/ContinueRecipeRecipe/README.md new file mode 100644 index 00000000..df66104f --- /dev/null +++ b/server/continuedev/plugins/recipes/ContinueRecipeRecipe/README.md @@ -0,0 +1,7 @@ +# ContinueRecipeRecipe + +A recipe for building recipes! + +## How to use this recipe + +This recipe takes a single input, a description of the recipe to be built. diff --git a/server/continuedev/plugins/recipes/ContinueRecipeRecipe/main.py b/server/continuedev/plugins/recipes/ContinueRecipeRecipe/main.py new file mode 100644 index 00000000..3dff2e15 --- /dev/null +++ b/server/continuedev/plugins/recipes/ContinueRecipeRecipe/main.py @@ -0,0 +1,43 @@ +from textwrap import dedent + +from ....core.main import Step +from ....core.sdk import ContinueSDK +from ....plugins.steps.main import EditHighlightedCodeStep + + +class ContinueStepStep(Step): + name: str = "Write your own Continue Step." + prompt: str + + async def run(self, sdk: ContinueSDK): + await sdk.run_step( + EditHighlightedCodeStep( + user_input=dedent( + f"""\ + Here is an example of a Step that runs a command and then edits a file. + + ```python + from ...core.main import Step + from ...core.sdk import ContinueSDK + + class RunCommandAndEditFileStep(Step): + name: str = "Run a command and then edit a file." + command: str + file_path: str + prompt: str + + async def run(self, sdk: ContinueSDK): + await sdk.run([command]) + await sdk.edit_file(filename=self.file_path, prompt=self.prompt) + ``` + + Please edit the code to write your own Step that does the following: + + {self.prompt} + + It should be a subclass of Step as above, implementing the `run` method, and using pydantic attributes to define the parameters. + + """ + ) + ) + ) diff --git a/server/continuedev/plugins/recipes/CreatePipelineRecipe/README.md b/server/continuedev/plugins/recipes/CreatePipelineRecipe/README.md new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/server/continuedev/plugins/recipes/CreatePipelineRecipe/README.md diff --git a/server/continuedev/plugins/recipes/CreatePipelineRecipe/main.py b/server/continuedev/plugins/recipes/CreatePipelineRecipe/main.py new file mode 100644 index 00000000..56e6f055 --- /dev/null +++ b/server/continuedev/plugins/recipes/CreatePipelineRecipe/main.py @@ -0,0 +1,40 @@ +from textwrap import dedent + +from ....core.main import Step +from ....core.sdk import ContinueSDK +from ....core.steps import MessageStep, WaitForUserInputStep +from .steps import RunQueryStep, SetupPipelineStep, ValidatePipelineStep + + +class CreatePipelineRecipe(Step): + hide: bool = True + + async def run(self, sdk: ContinueSDK): + text_observation = await sdk.run_step( + MessageStep( + name="Building your first dlt pipeline", + message=dedent( + """\ + This recipe will walk you through the process of creating a dlt pipeline for your chosen data source. With the help of Continue, you will: + - Create a Python virtual environment with dlt installed + - Run `dlt init` to generate a pipeline template + - Write the code to call the API + - Add any required API keys to the `secrets.toml` file + - Test that the API call works + - Load the data into a local DuckDB instance + - Write a query to view the data""" + ), + ) + >> WaitForUserInputStep( + prompt="What API do you want to load data from? (e.g. weatherapi.com, chess.com)" + ) + ) + await sdk.run_step( + SetupPipelineStep(api_description=text_observation.text) + >> ValidatePipelineStep() + >> RunQueryStep() + >> MessageStep( + name="Congrats!", + message="You've successfully created your first dlt pipeline! 🎉", + ) + ) diff --git a/server/continuedev/plugins/recipes/CreatePipelineRecipe/steps.py b/server/continuedev/plugins/recipes/CreatePipelineRecipe/steps.py new file mode 100644 index 00000000..65e7182d --- /dev/null +++ b/server/continuedev/plugins/recipes/CreatePipelineRecipe/steps.py @@ -0,0 +1,243 @@ +import os +import time +from textwrap import dedent + +from ....core.main import Step +from ....core.sdk import ContinueSDK, Models +from ....core.steps import MessageStep +from ....models.filesystem import RangeInFile +from ....models.filesystem_edit import AddFile, FileEdit +from ....models.main import Range + +AI_ASSISTED_STRING = "(✨ AI-Assisted ✨)" + + +class SetupPipelineStep(Step): + hide: bool = True + name: str = "Setup dlt Pipeline" + + api_description: str # e.g. "I want to load data from the weatherapi.com API" + + async def describe(self, models: Models): + return dedent( + f"""\ + This step will create a new dlt pipeline that loads data from an API, as per your request: + {self.api_description} + """ + ) + + async def run(self, sdk: ContinueSDK): + sdk.context.set("api_description", self.api_description) + + source_name = ( + await sdk.models.summarize.complete( + f"Write a snake_case name for the data source described by {self.api_description}: " + ) + ).strip() + filename = f"{source_name}.py" + + # running commands to get started when creating a new dlt pipeline + await sdk.run( + [ + "python3 -m venv .env", + "source .env/bin/activate", + "pip install dlt", + f"dlt --non-interactive init {source_name} duckdb", + "pip install -r requirements.txt", + ], + description=dedent( + f"""\ + Running the following commands: + - `python3 -m venv .env`: Create a Python virtual environment + - `source .env/bin/activate`: Activate the virtual environment + - `pip install dlt`: Install dlt + - `dlt init {source_name} duckdb`: Create a new dlt pipeline called {source_name} that loads data into a local DuckDB instance + - `pip install -r requirements.txt`: Install the Python dependencies for the pipeline""" + ), + name="Setup Python environment", + ) + + # editing the resource function to call the requested API + resource_function_range = Range.from_shorthand(15, 0, 30, 0) + await sdk.ide.highlightCode( + RangeInFile( + filepath=os.path.join(await sdk.ide.getWorkspaceDirectory(), filename), + range=resource_function_range, + ), + "#ffa50033", + ) + + # sdk.set_loading_message("Writing code to call the API...") + await sdk.edit_file( + range=resource_function_range, + filename=filename, + prompt=f"Edit the resource function to call the API described by this: {self.api_description}. Do not move or remove the exit() call in __main__.", + name=f"Edit the resource function to call the API {AI_ASSISTED_STRING}", + ) + + time.sleep(1) + + # wait for user to put API key in secrets.toml + await sdk.ide.setFileOpen( + await sdk.ide.getWorkspaceDirectory() + "/.dlt/secrets.toml" + ) + await sdk.wait_for_user_confirmation( + "If this service requires an API key, please add it to the `secrets.toml` file and then press `Continue`." + ) + + sdk.context.set("source_name", source_name) + + +class ValidatePipelineStep(Step): + hide: bool = True + + async def run(self, sdk: ContinueSDK): + workspace_dir = await sdk.ide.getWorkspaceDirectory() + source_name = sdk.context.get("source_name") + filename = f"{source_name}.py" + + # await sdk.run_step(MessageStep(name="Validate the pipeline", message=dedent("""\ + # Next, we will validate that your dlt pipeline is working as expected: + # - Test that the API call works + # - Load the data into a local DuckDB instance + # - Write a query to view the data + # """))) + + # test that the API call works + output = await sdk.run( + f"python3 {filename}", + name="Test the pipeline", + description=f"Running `python3 {filename}` to test loading data from the API", + handle_error=False, + ) + + # If it fails, return the error + if "Traceback" in output or "SyntaxError" in output: + output = "Traceback" + output.split("Traceback")[-1] + file_content = await sdk.ide.readFile(os.path.join(workspace_dir, filename)) + suggestion = await sdk.models.summarize.complete( + dedent( + f"""\ + ```python + {file_content} + ``` + This above code is a dlt pipeline that loads data from an API. The function with the @resource decorator is responsible for calling the API and returning the data. While attempting to run the pipeline, the following error occurred: + + ```ascii + {output} + ``` + + This is a brief summary of the error followed by a suggestion on how it can be fixed by editing the resource function:""" + ) + ) + + api_documentation_url = await sdk.models.summarize.complete( + dedent( + f"""\ + The API I am trying to call is the '{sdk.context.get('api_description')}'. I tried calling it in the @resource function like this: + ```python + {file_content} + ``` + What is the URL for the API documentation that will help me learn how to make this call? Please format in markdown so I can click the link.""" + ) + ) + + sdk.raise_exception( + title=f"Error while running pipeline.\nFix the resource function in {filename} and rerun this step", + message=output, + with_step=MessageStep( + name=f"Suggestion to solve error {AI_ASSISTED_STRING}", + message=dedent( + f"""\ + {suggestion} + + {api_documentation_url} + + After you've fixed the code, click the retry button at the top of the Validate Pipeline step above.""" + ), + ), + ) + + # remove exit() from the main main function + await sdk.run_step( + MessageStep( + name="Remove early exit() from main function", + message="Remove the early exit() from the main function now that we are done testing and want the pipeline to load the data into DuckDB.", + ) + ) + + contents = await sdk.ide.readFile(os.path.join(workspace_dir, filename)) + replacement = "\n".join( + list(filter(lambda line: line.strip() != "exit()", contents.split("\n"))) + ) + await sdk.ide.applyFileSystemEdit( + FileEdit( + filepath=os.path.join(workspace_dir, filename), + replacement=replacement, + range=Range.from_entire_file(contents), + ) + ) + + # load the data into the DuckDB instance + await sdk.run( + f"python3 {filename}", + name="Load data into DuckDB", + description=f"Running python3 {filename} to load data into DuckDB", + ) + + tables_query_code = dedent( + f"""\ + import duckdb + + # connect to DuckDB instance + conn = duckdb.connect(database="{source_name}.duckdb") + + # list all tables + print(conn.sql("DESCRIBE"))""" + ) + + query_filename = os.path.join(workspace_dir, "query.py") + await sdk.apply_filesystem_edit( + AddFile(filepath=query_filename, content=tables_query_code), + name="Add query.py file", + description="Adding a file called `query.py` to the workspace that will run a test query on the DuckDB instance", + ) + + +class RunQueryStep(Step): + hide: bool = True + + async def run(self, sdk: ContinueSDK): + output = await sdk.run( + ".env/bin/python3 query.py", + name="Run test query", + description="Running `.env/bin/python3 query.py` to test that the data was loaded into DuckDB as expected", + handle_error=False, + ) + + if "Traceback" in output or "SyntaxError" in output: + suggestion = await sdk.models.summarize.complete( + dedent( + f"""\ + ```python + {await sdk.ide.readFile(os.path.join(sdk.ide.workspace_directory, "query.py"))} + ``` + This above code is a query that runs on the DuckDB instance. While attempting to run the query, the following error occurred: + + ```ascii + {output} + ``` + + This is a brief summary of the error followed by a suggestion on how it can be fixed:""" + ) + ) + + sdk.raise_exception( + title="Error while running query", + message=output, + with_step=MessageStep( + name=f"Suggestion to solve error {AI_ASSISTED_STRING}", + message=suggestion + + "\n\nIt is also very likely that no duckdb table was created, which can happen if the resource function did not yield any data. Please make sure that it is yielding data and then rerun this step.", + ), + ) diff --git a/server/continuedev/plugins/recipes/DDtoBQRecipe/README.md b/server/continuedev/plugins/recipes/DDtoBQRecipe/README.md new file mode 100644 index 00000000..d50324f7 --- /dev/null +++ b/server/continuedev/plugins/recipes/DDtoBQRecipe/README.md @@ -0,0 +1,3 @@ +# DDtoBQRecipe + +Move from using DuckDB to Google BigQuery as the destination for your `dlt` pipeline diff --git a/server/continuedev/plugins/recipes/DDtoBQRecipe/dlt_duckdb_to_bigquery_docs.md b/server/continuedev/plugins/recipes/DDtoBQRecipe/dlt_duckdb_to_bigquery_docs.md new file mode 100644 index 00000000..eb68e117 --- /dev/null +++ b/server/continuedev/plugins/recipes/DDtoBQRecipe/dlt_duckdb_to_bigquery_docs.md @@ -0,0 +1,85 @@ +### Credentials Missing: ConfigFieldMissingException + +You'll see this exception if `dlt` cannot find your bigquery credentials. In the exception below all of them ('project_id', 'private_key', 'client_email') are missing. The exception gives you also the list of all lookups for configuration performed - [here we explain how to read such list](run-a-pipeline.md#missing-secret-or-configuration-values). + +``` +dlt.common.configuration.exceptions.ConfigFieldMissingException: Following fields are missing: ['project_id', 'private_key', 'client_email'] in configuration with spec GcpServiceAccountCredentials + for field "project_id" config providers and keys were tried in following order: + In Environment Variables key WEATHERAPI__DESTINATION__BIGQUERY__CREDENTIALS__PROJECT_ID was not found. + In Environment Variables key WEATHERAPI__DESTINATION__CREDENTIALS__PROJECT_ID was not found. +``` + +The most common cases for the exception: + +1. The secrets are not in `secrets.toml` at all +2. The are placed in wrong section. For example the fragment below will not work: + +```toml +[destination.bigquery] +project_id = "project_id" # please set me up! +``` + +3. You run the pipeline script from the **different** folder from which it is saved. For example `python weatherapi_demo/weatherapi.py` will run the script from `weatherapi_demo` folder but the current working directory is folder above. This prevents `dlt` from finding `weatherapi_demo/.dlt/secrets.toml` and filling-in credentials. + +### Placeholders still in secrets.toml + +Here BigQuery complain that the format of the `private_key` is incorrect. Practically this most often happens if you forgot to replace the placeholders in `secrets.toml` with real values + +``` +<class 'dlt.destinations.exceptions.DestinationConnectionError'> +Connection with BigQuerySqlClient to dataset name weatherapi_data failed. Please check if you configured the credentials at all and provided the right credentials values. You can be also denied access or your internet connection may be down. The actual reason given is: No key could be detected. +``` + +### Bigquery not enabled + +[You must enable Bigquery API.](https://console.cloud.google.com/apis/dashboard) + +``` +<class 'google.api_core.exceptions.Forbidden'> +403 POST https://bigquery.googleapis.com/bigquery/v2/projects/bq-walkthrough/jobs?prettyPrint=false: BigQuery API has not been used in project 364286133232 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/bigquery.googleapis.com/overview?project=364286133232 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. + +Location: EU +Job ID: a5f84253-3c10-428b-b2c8-1a09b22af9b2 + [{'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'Google developers console API activation', 'url': 'https://console.developers.google.com/apis/api/bigquery.googleapis.com/overview?project=364286133232'}]}, {'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'SERVICE_DISABLED', 'domain': 'googleapis.com', 'metadata': {'service': 'bigquery.googleapis.com', 'consumer': 'projects/364286133232'}}] +``` + +### Lack of permissions to create jobs + +Add `BigQuery Job User` as described in the [destination page](../destinations/bigquery.md). + +``` +<class 'google.api_core.exceptions.Forbidden'> +403 POST https://bigquery.googleapis.com/bigquery/v2/projects/bq-walkthrough/jobs?prettyPrint=false: Access Denied: Project bq-walkthrough: User does not have bigquery.jobs.create permission in project bq-walkthrough. + +Location: EU +Job ID: c1476d2c-883c-43f7-a5fe-73db195e7bcd +``` + +### Lack of permissions to query/write data + +Add `BigQuery Data Editor` as described in the [destination page](../destinations/bigquery.md). + +``` +<class 'dlt.destinations.exceptions.DatabaseTransientException'> +403 Access Denied: Table bq-walkthrough:weatherapi_data._dlt_loads: User does not have permission to query table bq-walkthrough:weatherapi_data._dlt_loads, or perhaps it does not exist in location EU. + +Location: EU +Job ID: 299a92a3-7761-45dd-a433-79fdeb0c1a46 +``` + +### Lack of billing / BigQuery in sandbox mode + +`dlt` does not support BigQuery when project has no billing enabled. If you see a stack trace where following warning appears: + +``` +<class 'dlt.destinations.exceptions.DatabaseTransientException'> +403 Billing has not been enabled for this project. Enable billing at https://console.cloud.google.com/billing. DML queries are not allowed in the free tier. Set up a billing account to remove this restriction. +``` + +or + +``` +2023-06-08 16:16:26,769|[WARNING ]|8096|dlt|load.py|complete_jobs:198|Job for weatherapi_resource_83b8ac9e98_4_jsonl retried in load 1686233775.932288 with message {"error_result":{"reason":"billingNotEnabled","message":"Billing has not been enabled for this project. Enable billing at https://console.cloud.google.com/billing. Table expiration time must be less than 60 days while in sandbox mode."},"errors":[{"reason":"billingNotEnabled","message":"Billing has not been enabled for this project. Enable billing at https://console.cloud.google.com/billing. Table expiration time must be less than 60 days while in sandbox mode."}],"job_start":"2023-06-08T14:16:26.850000Z","job_end":"2023-06-08T14:16:26.850000Z","job_id":"weatherapi_resource_83b8ac9e98_4_jsonl"} +``` + +you must enable the billing. diff --git a/server/continuedev/plugins/recipes/DDtoBQRecipe/main.py b/server/continuedev/plugins/recipes/DDtoBQRecipe/main.py new file mode 100644 index 00000000..65149500 --- /dev/null +++ b/server/continuedev/plugins/recipes/DDtoBQRecipe/main.py @@ -0,0 +1,31 @@ +from textwrap import dedent + +from ....core.main import Step +from ....core.sdk import ContinueSDK +from ....core.steps import MessageStep +from .steps import LoadDataStep, SetUpChessPipelineStep, SwitchDestinationStep + +# Based on the following guide: +# https://github.com/dlt-hub/dlt/pull/392 + + +class DDtoBQRecipe(Step): + hide: bool = True + + async def run(self, sdk: ContinueSDK): + await sdk.run_step( + MessageStep( + name="Move from using DuckDB to Google BigQuery as the destination", + message=dedent( + """\ + This recipe will walk you through the process of moving from using DuckDB to Google BigQuery as the destination for your dlt pipeline. With the help of Continue, you will: + - Set up a dlt pipeline for the chess.com API + - Switch destination from DuckDB to Google BigQuery + - Add BigQuery credentials to your secrets.toml file + - Run the pipeline again to load data to BigQuery""" + ), + ) + >> SetUpChessPipelineStep() + >> SwitchDestinationStep() + >> LoadDataStep() + ) diff --git a/server/continuedev/plugins/recipes/DDtoBQRecipe/steps.py b/server/continuedev/plugins/recipes/DDtoBQRecipe/steps.py new file mode 100644 index 00000000..dfe25d9e --- /dev/null +++ b/server/continuedev/plugins/recipes/DDtoBQRecipe/steps.py @@ -0,0 +1,119 @@ +import os +from textwrap import dedent + +from ....core.main import Step +from ....core.sdk import ContinueSDK, Models +from ....core.steps import MessageStep +from ....libs.util.paths import find_data_file +from ....plugins.steps.find_and_replace import FindAndReplaceStep + +AI_ASSISTED_STRING = "(✨ AI-Assisted ✨)" + + +class SetUpChessPipelineStep(Step): + hide: bool = True + name: str = "Setup Chess.com API dlt Pipeline" + + async def describe(self, models: Models): + return "This step will create a new dlt pipeline that loads data from the chess.com API." + + async def run(self, sdk: ContinueSDK): + # running commands to get started when creating a new dlt pipeline + await sdk.run( + [ + "python3 -m venv .env", + "source .env/bin/activate", + "pip install dlt", + "dlt --non-interactive init chess duckdb", + "pip install -r requirements.txt", + ], + name="Set up Python environment", + description=dedent( + """\ + Running the following commands: + - `python3 -m venv .env`: Create a Python virtual environment + - `source .env/bin/activate`: Activate the virtual environment + - `pip install dlt`: Install dlt + - `dlt init chess duckdb`: Create a new dlt pipeline called "chess" that loads data into a local DuckDB instance + - `pip install -r requirements.txt`: Install the Python dependencies for the pipeline""" + ), + ) + + +class SwitchDestinationStep(Step): + hide: bool = True + + async def run(self, sdk: ContinueSDK): + # Switch destination from DuckDB to Google BigQuery + filepath = os.path.join(sdk.ide.workspace_directory, "chess_pipeline.py") + await sdk.run_step( + FindAndReplaceStep( + filepath=filepath, + pattern="destination='duckdb'", + replacement="destination='bigquery'", + ) + ) + + # Add BigQuery credentials to your secrets.toml file + template = dedent( + """\ + [destination.bigquery.credentials] + location = "US" # change the location of the data + project_id = "project_id" # please set me up! + private_key = "private_key" # please set me up! + client_email = "client_email" # please set me up!""" + ) + + # wait for user to put API key in secrets.toml + secrets_path = os.path.join(sdk.ide.workspace_directory, ".dlt/secrets.toml") + await sdk.ide.setFileOpen(secrets_path) + await sdk.append_to_file(secrets_path, template) + + # append template to bottom of secrets.toml + await sdk.wait_for_user_confirmation( + "Please add your GCP credentials to `secrets.toml` file and then press `Continue`" + ) + + +class LoadDataStep(Step): + name: str = "Load data to BigQuery" + hide: bool = True + + async def run(self, sdk: ContinueSDK): + # Run the pipeline again to load data to BigQuery + output = await sdk.run( + ".env/bin/python3 chess_pipeline.py", + name="Load data to BigQuery", + description="Running `.env/bin/python3 chess_pipeline.py` to load data to Google BigQuery", + ) + + if "Traceback" in output or "SyntaxError" in output: + with open(find_data_file("dlt_duckdb_to_bigquery_docs.md"), "r") as f: + docs = f.read() + + output = "Traceback" + output.split("Traceback")[-1] + suggestion = await sdk.models.default.complete( + dedent( + f"""\ + When trying to load data into BigQuery, the following error occurred: + + ```ascii + {output} + ``` + + Here is documentation describing common errors and their causes/solutions: + + {docs} + + This is a brief summary of the error followed by a suggestion on how it can be fixed:""" + ) + ) + + sdk.raise_exception( + title="Error while running query", + message=output, + with_step=MessageStep( + name=f"Suggestion to solve error {AI_ASSISTED_STRING}", + message=suggestion, + ), + ) diff --git a/server/continuedev/plugins/recipes/DeployPipelineAirflowRecipe/README.md b/server/continuedev/plugins/recipes/DeployPipelineAirflowRecipe/README.md new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/server/continuedev/plugins/recipes/DeployPipelineAirflowRecipe/README.md diff --git a/server/continuedev/plugins/recipes/DeployPipelineAirflowRecipe/main.py b/server/continuedev/plugins/recipes/DeployPipelineAirflowRecipe/main.py new file mode 100644 index 00000000..5b0bd320 --- /dev/null +++ b/server/continuedev/plugins/recipes/DeployPipelineAirflowRecipe/main.py @@ -0,0 +1,86 @@ +from textwrap import dedent + +from ....core.main import Step +from ....core.sdk import ContinueSDK +from ....core.steps import MessageStep +from ....plugins.steps.input.nl_multiselect import NLMultiselectStep +from .steps import DeployAirflowStep, RunPipelineStep, SetupPipelineStep + +# https://github.com/dlt-hub/dlt-deploy-template/blob/master/airflow-composer/dag_template.py +# https://www.notion.so/dlthub/Deploy-a-pipeline-with-Airflow-245fd1058652479494307ead0b5565f3 +# 1. What verified pipeline do you want to deploy with Airflow? +# 2. Set up selected verified pipeline +# 3. Deploy selected verified pipeline with Airflow +# 4. Set up Airflow locally? + + +class DeployPipelineAirflowRecipe(Step): + hide: bool = True + + async def run(self, sdk: ContinueSDK): + source_name = await sdk.run_step( + MessageStep( + name="Deploying a pipeline to Airflow", + message=dedent( + """\ + This recipe will show you how to deploy a pipeline to Airflow. With the help of Continue, you will: + - Select a dlt-verified pipeline + - Setup the pipeline + - Deploy it to Airflow + - Optionally, setup Airflow locally""" + ), + ) + >> NLMultiselectStep( + prompt=dedent( + """\ + Which verified pipeline do you want to deploy with Airflow? The options are: + - Asana + - Chess.com + - Facebook Ads + - GitHub + - Google Analytics + - Google Sheets + - HubSpot + - Jira + - Matomo + - Mux + - Notion + - Pipedrive + - Pokemon + - Salesforce + - Shopify + - Strapi + - Stripe + - SQL Database + - Workable + - Zendesk""" + ), + options=[ + "asana_dlt", + "chess", + "github", + "google_analytics", + "google_sheets", + "hubspot", + "matomo", + "pipedrive", + "shopify_dlt", + "strapi", + "zendesk", + "facebook_ads", + "jira", + "mux", + "notion", + "pokemon", + "salesforce", + "stripe_analytics", + "sql_database", + "workable", + ], + ) + ) + await sdk.run_step( + SetupPipelineStep(source_name=source_name) + >> RunPipelineStep(source_name=source_name) + >> DeployAirflowStep(source_name=source_name) + ) diff --git a/server/continuedev/plugins/recipes/DeployPipelineAirflowRecipe/steps.py b/server/continuedev/plugins/recipes/DeployPipelineAirflowRecipe/steps.py new file mode 100644 index 00000000..e4a932af --- /dev/null +++ b/server/continuedev/plugins/recipes/DeployPipelineAirflowRecipe/steps.py @@ -0,0 +1,125 @@ +import os +from textwrap import dedent + +from ....core.main import Step +from ....core.sdk import ContinueSDK, Models +from ....core.steps import MessageStep +from ....plugins.steps.find_and_replace import FindAndReplaceStep + +AI_ASSISTED_STRING = "(✨ AI-Assisted ✨)" + + +class SetupPipelineStep(Step): + hide: bool = True + name: str = "Setup dlt Pipeline" + + source_name: str + + async def describe(self, models: Models): + pass + + async def run(self, sdk: ContinueSDK): + await sdk.run( + [ + "python3 -m venv .env", + "source .env/bin/activate", + "pip install dlt", + f"dlt --non-interactive init {self.source_name} duckdb", + "pip install -r requirements.txt", + ], + description=dedent( + f"""\ + Running the following commands: + - `python3 -m venv .env`: Create a Python virtual environment + - `source .env/bin/activate`: Activate the virtual environment + - `pip install dlt`: Install dlt + - `dlt init {self.source_name} duckdb`: Create a new dlt pipeline called {self.source_name} that loads data into a local DuckDB instance + - `pip install -r requirements.txt`: Install the Python dependencies for the pipeline""" + ), + name="Setup Python environment", + ) + + +class RunPipelineStep(Step): + hide: bool = True + name: str = "Run dlt Pipeline" + + source_name: str + + async def describe(self, models: Models): + pass + + async def run(self, sdk: ContinueSDK): + await sdk.run( + [ + f"python3 {self.source_name}_pipeline.py", + ], + description=dedent( + f"""\ + Running the command `python3 {self.source_name}_pipeline.py to run the pipeline: """ + ), + name="Run dlt pipeline", + ) + + +class DeployAirflowStep(Step): + hide: bool = True + source_name: str + + async def run(self, sdk: ContinueSDK): + # Run dlt command to deploy pipeline to Airflow + await sdk.run( + [ + "git init", + f"dlt --non-interactive deploy {self.source_name}_pipeline.py airflow-composer", + ], + description="Running `dlt deploy airflow` to deploy the dlt pipeline to Airflow", + name="Deploy dlt pipeline to Airflow", + ) + + # Get filepaths, open the DAG file + directory = await sdk.ide.getWorkspaceDirectory() + pipeline_filepath = os.path.join(directory, f"{self.source_name}_pipeline.py") + dag_filepath = os.path.join( + directory, f"dags/dag_{self.source_name}_pipeline.py" + ) + + await sdk.ide.setFileOpen(dag_filepath) + + # Replace the pipeline name and dataset name + await sdk.run_step( + FindAndReplaceStep( + filepath=pipeline_filepath, + pattern="'pipeline_name'", + replacement=f"'{self.source_name}_pipeline'", + ) + ) + await sdk.run_step( + FindAndReplaceStep( + filepath=pipeline_filepath, + pattern="'dataset_name'", + replacement=f"'{self.source_name}_data'", + ) + ) + await sdk.run_step( + FindAndReplaceStep( + filepath=pipeline_filepath, + pattern="pipeline_or_source_script", + replacement=f"{self.source_name}_pipeline", + ) + ) + + # Prompt the user for the DAG schedule + # edit_dag_range = Range.from_shorthand(18, 0, 23, 0) + # await sdk.ide.highlightCode(range_in_file=RangeInFile(filepath=dag_filepath, range=edit_dag_range), color="#33993333") + # response = await sdk.run_step(WaitForUserInputStep(prompt="When would you like this Airflow DAG to run? (e.g. every day, every Monday, every 1st of the month, etc.)")) + # await sdk.edit_file(dag_filepath, prompt=f"Edit the DAG so that it runs at the following schedule: '{response.text}'", + # range=edit_dag_range) + + # Tell the user to check the schedule and fill in owner, email, other default_args + await sdk.run_step( + MessageStep( + message="Fill in the owner, email, and other default_args in the DAG file with your own personal information. Then the DAG will be ready to run!", + name="Fill in default_args", + ) + ) diff --git a/server/continuedev/plugins/recipes/README.md b/server/continuedev/plugins/recipes/README.md new file mode 100644 index 00000000..9860b0e2 --- /dev/null +++ b/server/continuedev/plugins/recipes/README.md @@ -0,0 +1,19 @@ +# This is a collaborative collection of Continue recipes + +A recipe is technically just a [Step](../steps/README.md), but is intended to be more complex, composed of multiple sub-steps. + +Recipes here will automatically be made available in the [Continue VS Code extension](https://marketplace.visualstudio.com/items?itemName=Continue.continue). + +The `recipes` folder contains all recipes, each with the same structure. **If you wish to create your own recipe, please do the following:** + +1. Create a new subfolder in `recipes`, with the name of your recipe (for example `MyNewRecipe`). +2. Make 2 files in this folder: 1) a `README.md` describing your recipe and how to use it and 2) a `main.py` including a single class with the name of your recipe (e.g. `MyNewRecipe`). +3. Write any utility code other than the main recipe class in a separate file, which you can import in `main.py`. Particularly if you decide to break the recipe into multiple sub-steps, try to keep these separate. + +# Existing Recipes + +`ContinueRecipeRecipe` - Write a Continue recipe with Continue. + +`CreatePipelineRecipe` - Build a dlt pipeline from scratch for an API of your choice. + +`WritePytestsRecipe` - Write Pytest unit tests in a folder adjacent to your Python file. diff --git a/server/continuedev/plugins/recipes/TemplateRecipe/README.md b/server/continuedev/plugins/recipes/TemplateRecipe/README.md new file mode 100644 index 00000000..91d1123b --- /dev/null +++ b/server/continuedev/plugins/recipes/TemplateRecipe/README.md @@ -0,0 +1,7 @@ +# TemplateRecipe + +This folder is a template that you can copy to create your own recipe. + +## How to use this recipe + +Explain here what users should know when using your recipe. What inputs does it have and what actions will it perform? diff --git a/server/continuedev/plugins/recipes/TemplateRecipe/main.py b/server/continuedev/plugins/recipes/TemplateRecipe/main.py new file mode 100644 index 00000000..01ae364d --- /dev/null +++ b/server/continuedev/plugins/recipes/TemplateRecipe/main.py @@ -0,0 +1,29 @@ +from typing import Coroutine + +from ....core.main import Observation, Step +from ....core.sdk import ContinueSDK, Models + + +class TemplateRecipe(Step): + """ + A simple recipe that appends a print statement to the currently open file. + Use this as a template to create your own! + """ + + # Parameters for the recipe + name: str + + # A title for the recipe, to be displayed in the GUI + title = "Template Recipe" + + # A description of what the recipe accomplished, to be displayed in the GUI + async def describe(self, models: Models) -> Coroutine[str, None, None]: + return f"Appended a statement to print `Hello, {self.name}!` at the end of the file." + + # The code executed when the recipe is run + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + visible_files = await sdk.ide.getVisibleFiles() + await sdk.edit_file( + filename=visible_files[0], + prompt=f"Append a statement to print `Hello, {self.name}!` at the end of the file.", + ) diff --git a/server/continuedev/plugins/recipes/WritePytestsRecipe/README.md b/server/continuedev/plugins/recipes/WritePytestsRecipe/README.md new file mode 100644 index 00000000..5ce33ecb --- /dev/null +++ b/server/continuedev/plugins/recipes/WritePytestsRecipe/README.md @@ -0,0 +1,7 @@ +# CreatePytestsRecipe + +A recipe for writing unit tests in Pytest. + +# How to use this recipe + +Call this recipe with a python file open that you would like to test. It will create tests in a `tests/` folder adjacent to the file with the test file given the same name prepended by `test_`. diff --git a/server/continuedev/plugins/recipes/WritePytestsRecipe/main.py b/server/continuedev/plugins/recipes/WritePytestsRecipe/main.py new file mode 100644 index 00000000..63edabc6 --- /dev/null +++ b/server/continuedev/plugins/recipes/WritePytestsRecipe/main.py @@ -0,0 +1,52 @@ +import os +from textwrap import dedent +from typing import Union + +from ....core.main import Step +from ....core.sdk import ContinueSDK +from ....models.filesystem_edit import AddDirectory, AddFile + + +class WritePytestsRecipe(Step): + for_filepath: Union[str, None] = None + user_input: str = "Write unit tests for this file." + + async def describe(self, models): + return f"Writing unit tests for {self.for_filepath}" + + async def run(self, sdk: ContinueSDK): + if self.for_filepath is None: + self.for_filepath = (await sdk.ide.getVisibleFiles())[0] + + filename = os.path.basename(self.for_filepath) + dirname = os.path.dirname(self.for_filepath) + + path_dir = os.path.join(dirname, "tests") + if not os.path.exists(path_dir): + await sdk.apply_filesystem_edit(AddDirectory(path=path_dir)) + + path = os.path.join(path_dir, f"test_{filename}") + if os.path.exists(path): + return None + + for_file_contents = await sdk.ide.readFile(self.for_filepath) + + prompt = dedent( + f"""\ + This is the file you will write unit tests for: + + ```python + {for_file_contents} + ``` + + Here are additional instructions: + + "{self.user_input}" + + Here is a complete set of pytest unit tests:""" + ) + tests = await sdk.models.summarize.complete(prompt) + + await sdk.apply_filesystem_edit(AddFile(filepath=path, content=tests)) + + return None diff --git a/server/continuedev/plugins/steps/README.md b/server/continuedev/plugins/steps/README.md new file mode 100644 index 00000000..a8cae90b --- /dev/null +++ b/server/continuedev/plugins/steps/README.md @@ -0,0 +1,50 @@ +# Steps + +Steps are the composable unit of action in Continue. They define a `run` method which has access to the entire `ContinueSDK`, allowing you to take actions inside the IDE, call language models, and more. In this folder you can find a number of good examples. + +## How to write a step + +a. Start by creating a subclass of `Step` + +You should first consider what will be the parameters of your recipe. These are defined as attributes in the Pydantic class. For example, if you wanted a "filepath" attribute that would look like this: + +```python +class HelloWorldStep(Step): + filepath: str + ... +``` + +b. Next, write the `run` method + +This method takes the ContinueSDK as a parameter, giving you all the tools you need to write your steps (if it's missing something, let us know, we'll add it!). You can write any code inside the run method; this is what will happen when your step is run, line for line. As an example, here's a step that will open a file and append "Hello World!": + +```python +class HelloWorldStep(Step): + filepath: str + + async def run(self, sdk: ContinueSDK): + await sdk.ide.setFileOpen(self.filepath) + await sdk.append_to_file(self.filepath, "Hello World!") +``` + +c. Finally, every Step is displayed with a description of what it has done + +If you'd like to override the default description of your step, which is just the class name, then implement the `describe` method. You can: + +- Return a static string +- Store state in a class attribute (prepend with a double underscore, which signifies (through Pydantic) that this is not a parameter for the Step, just internal state) during the run method, and then grab this in the describe method. +- Use state in conjunction with the `models` parameter of the describe method to autogenerate a description with a language model. For example, if you'd used an attribute called `__code_written` to store a string representing some code that was written, you could implement describe as `return models.summarize.complete(f"{self.\_\_code_written}\n\nSummarize the changes made in the above code.")`. + +Here's an example: + +```python +class HelloWorldStep(Step): + filepath: str + + async def run(self, sdk: ContinueSDK): + await sdk.ide.setFileOpen(self.filepath) + await sdk.append_to_file(self.filepath, "Hello World!") + + def describe(self, models: Models): + return f"Appended 'Hello World!' to {self.filepath}" +``` diff --git a/server/continuedev/plugins/steps/__init__.py b/server/continuedev/plugins/steps/__init__.py new file mode 100644 index 00000000..a181a956 --- /dev/null +++ b/server/continuedev/plugins/steps/__init__.py @@ -0,0 +1,13 @@ +# from .chroma import ( +# AnswerQuestionChroma, # noqa: F401 +# CreateCodebaseIndexChroma, # noqa: F401 +# EditFileChroma, # noqa: F401 +# ) +from .clear_history import ClearHistoryStep # noqa: F401 +from .cmd import GenerateShellCommandStep # noqa: F401 +from .comment_code import CommentCodeStep # noqa: F401 +from .help import HelpStep # noqa: F401 +from .main import EditHighlightedCodeStep # noqa: F401 +from .open_config import OpenConfigStep # noqa: F401 + +# from .share_session import ShareSessionStep # noqa: F401 diff --git a/server/continuedev/plugins/steps/chat.py b/server/continuedev/plugins/steps/chat.py new file mode 100644 index 00000000..1b0f76f9 --- /dev/null +++ b/server/continuedev/plugins/steps/chat.py @@ -0,0 +1,379 @@ +import html +import json +import os +from textwrap import dedent +from typing import Any, Coroutine, List + +import openai +from directory_tree import display_tree +from dotenv import load_dotenv +from pydantic import Field + +from ...core.main import ChatMessage, FunctionCall, Models, Step, step_to_json_schema +from ...core.sdk import ContinueSDK +from ...core.steps import MessageStep +from ...libs.llm.openai import OpenAI +from ...libs.llm.openai_free_trial import OpenAIFreeTrial +from ...libs.util.devdata import dev_data_logger +from ...libs.util.strings import remove_quotes_and_escapes +from ...libs.util.telemetry import posthog_logger +from .main import EditHighlightedCodeStep + +load_dotenv() +OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") +openai.api_key = OPENAI_API_KEY + +FREE_USAGE_STEP_NAME = "Please enter OpenAI API key" + + +def add_ellipsis(text: str, max_length: int = 200) -> str: + if len(text) > max_length: + return text[: max_length - 3] + "..." + return text + + +class SimpleChatStep(Step): + name: str = "Generating Response..." + manage_own_chat_context: bool = True + description: str = "" + messages: List[ChatMessage] = None + + async def run(self, sdk: ContinueSDK): + # Check if proxy server API key + if ( + isinstance(sdk.models.default, OpenAIFreeTrial) + and ( + sdk.models.default.api_key is None + or sdk.models.default.api_key.strip() == "" + ) + and len(list(filter(lambda x: not x.step.hide, sdk.history.timeline))) >= 10 + and len( + list( + filter( + lambda x: x.step.name == FREE_USAGE_STEP_NAME, + sdk.history.timeline, + ) + ) + ) + == 0 + ): + await sdk.run_step( + MessageStep( + name=FREE_USAGE_STEP_NAME, + message=dedent( + """\ + To make it easier to use Continue, you're getting limited free usage. When you have the chance, please enter your own OpenAI key in `~/.continue/config.py`. You can open the file by using the '/config' slash command in the text box below. + + Here's an example of how to edit the file: + ```python + ... + config=ContinueConfig( + ... + models=Models( + default=OpenAIFreeTrial(api_key="<API_KEY>", model="gpt-4"), + summarize=OpenAIFreeTrial(api_key="<API_KEY>", model="gpt-3.5-turbo") + ) + ) + ``` + + You can also learn more about customizations [here](https://continue.dev/docs/customization). + """ + ), + ) + ) + + messages = self.messages or await sdk.get_chat_context() + + generator = sdk.models.chat.stream_chat( + messages, temperature=sdk.config.temperature + ) + + posthog_logger.capture_event( + "model_use", + { + "model": sdk.models.default.model, + "provider": sdk.models.default.__class__.__name__, + }, + ) + dev_data_logger.capture( + "model_use", + { + "model": sdk.models.default.model, + "provider": sdk.models.default.__class__.__name__, + }, + ) + + async for chunk in generator: + if sdk.current_step_was_deleted(): + # So that the message doesn't disappear + self.hide = False + await sdk.update_ui() + break + + if "content" in chunk: + self.description += chunk["content"] + + # HTML unencode + end_size = len(chunk["content"]) - 6 + if "&" in self.description[-end_size:]: + self.description = self.description[:-end_size] + html.unescape( + self.description[-end_size:] + ) + + await sdk.update_ui() + + if sdk.config.disable_summaries: + self.name = "" + else: + self.name = "Generating title..." + await sdk.update_ui() + self.name = add_ellipsis( + remove_quotes_and_escapes( + await sdk.models.summarize.complete( + f'"{self.description}"\n\nPlease write a short title summarizing the message quoted above. Use no more than 10 words:', + max_tokens=20, + log=False, + ) + ), + 200, + ) + + await sdk.update_ui() + + self.chat_context.append( + ChatMessage(role="assistant", content=self.description, summary=self.name) + ) + + # TODO: Never actually closing. + await generator.aclose() + + +class AddFileStep(Step): + name: str = "Add File" + description = "Add a file to the workspace. Should always view the directory tree before this." + filename: str + file_contents: str + + async def describe( + self, models: Models + ) -> Coroutine[Any, Any, Coroutine[str, None, None]]: + return f"Added a file named `{self.filename}` to the workspace." + + async def run(self, sdk: ContinueSDK): + await sdk.add_file(self.filename, self.file_contents) + + await sdk.ide.setFileOpen( + os.path.join(sdk.ide.workspace_directory, self.filename) + ) + + +class DeleteFileStep(Step): + name: str = "Delete File" + description = "Delete a file from the workspace." + filename: str + + async def describe( + self, models: Models + ) -> Coroutine[Any, Any, Coroutine[str, None, None]]: + return f"Deleted a file named `{self.filename}` from the workspace." + + async def run(self, sdk: ContinueSDK): + await sdk.delete_file(self.filename) + + +class AddDirectoryStep(Step): + name: str = "Add Directory" + description = "Add a directory to the workspace." + directory_name: str + + async def describe( + self, models: Models + ) -> Coroutine[Any, Any, Coroutine[str, None, None]]: + return f"Added a directory named `{self.directory_name}` to the workspace." + + async def run(self, sdk: ContinueSDK): + try: + await sdk.add_directory(self.directory_name) + except FileExistsError: + self.description = f"Directory {self.directory_name} already exists." + + +class RunTerminalCommandStep(Step): + name: str = "Run Terminal Command" + description: str = "Run a terminal command." + command: str + + async def run(self, sdk: ContinueSDK): + self.description = f"Copy this command and run in your terminal:\n\n```bash\n{self.command}\n```" + + +class ViewDirectoryTreeStep(Step): + name: str = "View Directory Tree" + description: str = "View the directory tree to learn which folder and files exist. You should always do this before adding new files." + + async def describe( + self, models: Models + ) -> Coroutine[Any, Any, Coroutine[str, None, None]]: + return "Viewed the directory tree." + + async def run(self, sdk: ContinueSDK): + self.description = ( + f"```\n{display_tree(sdk.ide.workspace_directory, True, max_depth=2)}\n```" + ) + + +class EditFileStep(Step): + name: str = "Edit File" + description: str = "Edit a file in the workspace that is not currently open." + filename: str = Field(..., description="The name of the file to edit.") + instructions: str = Field(..., description="The instructions to edit the file.") + hide: bool = True + + async def run(self, sdk: ContinueSDK): + await sdk.edit_file(self.filename, self.instructions) + + +class ChatWithFunctions(Step): + user_input: str + functions: List[Step] = [ + AddFileStep(filename="", file_contents=""), + EditFileStep(filename="", instructions=""), + EditHighlightedCodeStep(user_input=""), + ViewDirectoryTreeStep(), + AddDirectoryStep(directory_name=""), + DeleteFileStep(filename=""), + RunTerminalCommandStep(command=""), + ] + name: str = "Input" + manage_own_chat_context: bool = True + description: str = "" + hide: bool = True + + async def run(self, sdk: ContinueSDK): + await sdk.update_ui() + + step_name_step_class_map = { + step.name.replace(" ", ""): step.__class__ for step in self.functions + } + + functions = [step_to_json_schema(function) for function in self.functions] + + self.chat_context.append( + ChatMessage(role="user", content=self.user_input, summary=self.user_input) + ) + + last_function_called_name = None + last_function_called_params = None + while True: + was_function_called = False + func_args = "" + func_name = "" + msg_content = "" + msg_step = None + + gpt350613 = OpenAI(model="gpt-3.5-turbo-0613") + await sdk.start_model(gpt350613) + + async for msg_chunk in gpt350613.stream_chat( + await sdk.get_chat_context(), functions=functions + ): + if sdk.current_step_was_deleted(): + return + + if "content" in msg_chunk and msg_chunk["content"] is not None: + msg_content += msg_chunk["content"] + # if last_function_called_index_in_history is not None: + # while sdk.history.timeline[last_function_called_index].step.hide: + # last_function_called_index += 1 + # sdk.history.timeline[last_function_called_index_in_history].step.description = msg_content + if msg_step is None: + msg_step = MessageStep( + name="Chat", message=msg_chunk["content"] + ) + await sdk.run_step(msg_step) + else: + msg_step.description = msg_content + await sdk.update_ui() + elif "function_call" in msg_chunk or func_name != "": + was_function_called = True + if "function_call" in msg_chunk: + if "arguments" in msg_chunk["function_call"]: + func_args += msg_chunk["function_call"]["arguments"] + if "name" in msg_chunk["function_call"]: + func_name += msg_chunk["function_call"]["name"] + + if not was_function_called: + self.chat_context.append( + ChatMessage( + role="assistant", content=msg_content, summary=msg_content + ) + ) + break + else: + if func_name == "python" and "python" not in step_name_step_class_map: + # GPT must be fine-tuned to believe this exists, but it doesn't always + func_name = "EditHighlightedCodeStep" + func_args = json.dumps({"user_input": self.user_input}) + # self.chat_context.append(ChatMessage( + # role="assistant", + # content=None, + # function_call=FunctionCall( + # name=func_name, + # arguments=func_args + # ), + # summary=f"Called function {func_name}" + # )) + # self.chat_context.append(ChatMessage( + # role="user", + # content="The 'python' function does not exist. Don't call it. Try again to call another function.", + # summary="'python' function does not exist." + # )) + # msg_step.hide = True + # continue + # Call the function, then continue to chat + func_args = "{}" if func_args == "" else func_args + try: + fn_call_params = json.loads(func_args) + except json.JSONDecodeError: + raise Exception("The model returned invalid JSON. Please try again") + self.chat_context.append( + ChatMessage( + role="assistant", + content=None, + function_call=FunctionCall(name=func_name, arguments=func_args), + summary=f"Called function {func_name}", + ) + ) + sdk.history.current_index + 1 + if func_name not in step_name_step_class_map: + raise Exception( + f"The model tried to call a function ({func_name}) that does not exist. Please try again." + ) + + # if func_name == "AddFileStep": + # step_to_run.hide = True + # self.description += f"\nAdded file `{func_args['filename']}`" + # elif func_name == "AddDirectoryStep": + # step_to_run.hide = True + # self.description += f"\nAdded directory `{func_args['directory_name']}`" + # else: + # self.description += f"\n`Running function {func_name}`\n\n" + if func_name == "EditHighlightedCodeStep": + fn_call_params["user_input"] = self.user_input + elif func_name == "EditFile": + fn_call_params["instructions"] = self.user_input + + step_to_run = step_name_step_class_map[func_name](**fn_call_params) + if ( + last_function_called_name is not None + and last_function_called_name == func_name + and last_function_called_params is not None + and last_function_called_params == fn_call_params + ): + # If it's calling the same function more than once in a row, it's probably looping and confused + return + last_function_called_name = func_name + last_function_called_params = fn_call_params + + await sdk.run_step(step_to_run) + await sdk.update_ui() diff --git a/server/continuedev/plugins/steps/chroma.py b/server/continuedev/plugins/steps/chroma.py new file mode 100644 index 00000000..f357a872 --- /dev/null +++ b/server/continuedev/plugins/steps/chroma.py @@ -0,0 +1,86 @@ +from textwrap import dedent +from typing import Coroutine, Union + +from ...core.main import Step +from ...core.observation import Observation +from ...core.sdk import ContinueSDK +from ...core.steps import EditFileStep +from ...libs.chroma.query import ChromaIndexManager + + +class CreateCodebaseIndexChroma(Step): + name: str = "Create Codebase Index" + hide: bool = True + + async def describe(self, llm) -> Coroutine[str, None, None]: + return "Indexing the codebase..." + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + index = ChromaIndexManager(await sdk.ide.getWorkspaceDirectory()) + if not index.check_index_exists(): + self.hide = False + index.create_codebase_index() + + +class AnswerQuestionChroma(Step): + question: str + _answer: Union[str, None] = None + name: str = "Answer Question" + + async def describe(self, llm) -> Coroutine[str, None, None]: + if self._answer is None: + return f"Answering the question: {self.question}" + else: + return self._answer + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + index = ChromaIndexManager(await sdk.ide.getWorkspaceDirectory()) + results = index.query_codebase_index(self.question) + + code_snippets = "" + + files = [] + for node in results.source_nodes: + resource_name = list(node.node.relationships.values())[0] + filepath = resource_name[: resource_name.index("::")] + files.append(filepath) + code_snippets += f"""{filepath}```\n{node.node.text}\n```\n\n""" + + prompt = dedent( + f"""Here are a few snippets of code that might be useful in answering the question: + + {code_snippets} + + Here is the question to answer: + + {self.question} + + Here is the answer:""" + ) + + answer = await sdk.models.summarize.complete(prompt) + # Make paths relative to the workspace directory + answer = answer.replace(await sdk.ide.getWorkspaceDirectory(), "") + + self._answer = answer + + await sdk.ide.setFileOpen(files[0]) + + +class EditFileChroma(Step): + request: str + hide: bool = True + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + index = ChromaIndexManager(await sdk.ide.getWorkspaceDirectory()) + results = index.query_codebase_index(self.request) + + resource_name = list(results.source_nodes[0].node.relationships.values())[0] + filepath = resource_name[: resource_name.index("::")] + + await sdk.run_step( + EditFileStep( + filepath=filepath, + prompt=f"Here is the code:\n\n{{code}}\n\nHere is the user request:\n\n{self.request}\n\nHere is the code after making the requested changes:\n", + ) + ) diff --git a/server/continuedev/plugins/steps/clear_history.py b/server/continuedev/plugins/steps/clear_history.py new file mode 100644 index 00000000..8f21518b --- /dev/null +++ b/server/continuedev/plugins/steps/clear_history.py @@ -0,0 +1,10 @@ +from ...core.main import Step +from ...core.sdk import ContinueSDK + + +class ClearHistoryStep(Step): + name: str = "Clear History" + hide: bool = True + + async def run(self, sdk: ContinueSDK): + await sdk.clear_history() diff --git a/server/continuedev/plugins/steps/cmd.py b/server/continuedev/plugins/steps/cmd.py new file mode 100644 index 00000000..a38f6323 --- /dev/null +++ b/server/continuedev/plugins/steps/cmd.py @@ -0,0 +1,30 @@ +from textwrap import dedent +from typing import Coroutine + +from ...core.main import Step +from ...core.observation import Observation +from ...core.sdk import ContinueSDK +from ...libs.util.strings import remove_quotes_and_escapes + + +class GenerateShellCommandStep(Step): + user_input: str + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + cmd = await sdk.models.default.complete( + dedent( + f"""\ + The user has made a request to run a shell command. Their description of what it should do is: + + "{self.user_input}" + + Please write a shell command that will do what the user requested. Your output should consist of only the command itself, without any explanation or example output. Do not use any newlines. Only output the command that when inserted into the terminal will do precisely what was requested. + """ + ) + ) + + cmd = remove_quotes_and_escapes(cmd.strip()).replace("\n", "").replace("\r", "") + + await sdk.ide.runCommand(cmd) + + self.description = f"Generated shell command: {cmd}" diff --git a/server/continuedev/plugins/steps/comment_code.py b/server/continuedev/plugins/steps/comment_code.py new file mode 100644 index 00000000..1eee791d --- /dev/null +++ b/server/continuedev/plugins/steps/comment_code.py @@ -0,0 +1,16 @@ +from ...core.main import ContinueSDK, Models, Step +from .main import EditHighlightedCodeStep + + +class CommentCodeStep(Step): + hide: bool = True + + async def describe(self, models: Models): + return "Writing comments" + + async def run(self, sdk: ContinueSDK): + await sdk.run_step( + EditHighlightedCodeStep( + user_input="Write comprehensive comments in the canonical format for every class and function" + ) + ) diff --git a/server/continuedev/plugins/steps/custom_command.py b/server/continuedev/plugins/steps/custom_command.py new file mode 100644 index 00000000..4128415b --- /dev/null +++ b/server/continuedev/plugins/steps/custom_command.py @@ -0,0 +1,29 @@ +from ...core.main import Step +from ...core.sdk import ContinueSDK, Models +from ...libs.util.templating import render_templated_string +from ..steps.chat import SimpleChatStep + + +class CustomCommandStep(Step): + name: str + prompt: str + user_input: str + slash_command: str + hide: bool = True + + async def describe(self, models: Models): + return self.prompt + + async def run(self, sdk: ContinueSDK): + task = render_templated_string(self.prompt) + + prompt_user_input = f"Task: {task}. Additional info: {self.user_input}" + messages = await sdk.get_chat_context() + # Find the last chat message with this slash command and replace it with the user input + for i in range(len(messages) - 1, -1, -1): + if messages[i].role == "user" and messages[i].content.startswith( + self.slash_command + ): + messages[i] = messages[i].copy(update={"content": prompt_user_input}) + break + await sdk.run_step(SimpleChatStep(messages=messages)) diff --git a/server/continuedev/plugins/steps/draft/abstract_method.py b/server/continuedev/plugins/steps/draft/abstract_method.py new file mode 100644 index 00000000..7ceefe9b --- /dev/null +++ b/server/continuedev/plugins/steps/draft/abstract_method.py @@ -0,0 +1,21 @@ +from ....core.main import Step +from ....core.sdk import ContinueSDK + + +class ImplementAbstractMethodStep(Step): + name: str = "Implement abstract method for all subclasses" + method_name: str + class_name: str + + async def run(self, sdk: ContinueSDK): + if sdk.lsp is None: + self.description = "Language Server Protocol is not enabled" + return + + implementations = await sdk.lsp.go_to_implementations(self.class_name) + + for implementation in implementations: + await sdk.edit_file( + range_in_files=[implementation.range_in_file], + prompt=f"Implement method `{self.method_name}` for this subclass of `{self.class_name}`", + ) diff --git a/server/continuedev/plugins/steps/draft/redux.py b/server/continuedev/plugins/steps/draft/redux.py new file mode 100644 index 00000000..83b5e592 --- /dev/null +++ b/server/continuedev/plugins/steps/draft/redux.py @@ -0,0 +1,50 @@ +from ....core.main import Step +from ....core.sdk import ContinueSDK +from ....core.steps import EditFileStep + + +class EditReduxStateStep(Step): + description: str # e.g. "I want to load data from the weatherapi.com API" + + async def run(self, sdk: ContinueSDK): + # Find the right file to edit + + # RootStore + store_filename = "" + sdk.run_step( + EditFileStep( + filename=store_filename, + prompt=f"Edit the root store to add a new slice for {self.description}", + ) + ) + store_file_contents = await sdk.ide.readFile(store_filename) + + # Selector + selector_filename = "" + sdk.run_step( + EditFileStep( + filepath=selector_filename, + prompt=f"Edit the selector to add a new property for {self.description}. The store looks like this: {store_file_contents}", + ) + ) + + # Reducer + reducer_filename = "" + sdk.run_step( + EditFileStep( + filepath=reducer_filename, + prompt=f"Edit the reducer to add a new property for {self.description}. The store looks like this: {store_file_contents}", + ) + ) + """ + Starts with implementing selector + 1. RootStore + 2. Selector + 3. Reducer or entire slice + + Need to first determine whether this is an: + 1. edit + 2. add new reducer and property in existing slice + 3. add whole new slice + 4. build redux from scratch + """ diff --git a/server/continuedev/plugins/steps/draft/typeorm.py b/server/continuedev/plugins/steps/draft/typeorm.py new file mode 100644 index 00000000..c79fa041 --- /dev/null +++ b/server/continuedev/plugins/steps/draft/typeorm.py @@ -0,0 +1,54 @@ +from textwrap import dedent + +from ....core.main import Step +from ....core.sdk import ContinueSDK + + +class CreateTableStep(Step): + sql_str: str + name: str = "Create a table in TypeORM" + + async def run(self, sdk: ContinueSDK): + # Write TypeORM entity + entity_name = self.sql_str.split(" ")[2].capitalize() + await sdk.edit_file( + f"src/entity/{entity_name}.ts", + dedent( + f"""\ + {self.sql_str} + + Write a TypeORM entity called {entity_name} for this table, importing as necessary:""" + ), + ) + + # Add entity to data-source.ts + await sdk.edit_file( + filepath="src/data-source.ts", prompt=f"Add the {entity_name} entity:" + ) + + # Generate blank migration for the entity + out = await sdk.run( + f"npx typeorm migration:create ./src/migration/Create{entity_name}Table" + ) + migration_filepath = out.text.split(" ")[1] + + # Wait for user input + await sdk.wait_for_user_confirmation("Fill in the migration?") + + # Fill in the migration + await sdk.edit_file( + migration_filepath, + dedent( + f"""\ + This is the table that was created: + + {self.sql_str} + + Fill in the migration for the table:""" + ), + ) + + # Run the migration + await sdk.run( + "npx typeorm-ts-node-commonjs migration:run -d ./src/data-source.ts" + ) diff --git a/server/continuedev/plugins/steps/feedback.py b/server/continuedev/plugins/steps/feedback.py new file mode 100644 index 00000000..df1142a1 --- /dev/null +++ b/server/continuedev/plugins/steps/feedback.py @@ -0,0 +1,14 @@ +from ...core.main import Models, Step +from ...core.sdk import ContinueSDK +from ...libs.util.telemetry import posthog_logger + + +class FeedbackStep(Step): + user_input: str + name = "Thanks for your feedback!" + + async def describe(self, models: Models): + return f"`{self.user_input}`\n\nWe'll see your feedback and make improvements as soon as possible. If you'd like to directly email us, you can contact [nate@continue.dev](mailto:nate@continue.dev?subject=Feedback%20On%20Continue)." + + async def run(self, sdk: ContinueSDK): + posthog_logger.capture_event("feedback", {"feedback": self.user_input}) diff --git a/server/continuedev/plugins/steps/find_and_replace.py b/server/continuedev/plugins/steps/find_and_replace.py new file mode 100644 index 00000000..287e286d --- /dev/null +++ b/server/continuedev/plugins/steps/find_and_replace.py @@ -0,0 +1,30 @@ +from ...core.main import Models, Step +from ...core.sdk import ContinueSDK +from ...models.filesystem_edit import FileEdit, Range + + +class FindAndReplaceStep(Step): + name: str = "Find and replace" + filepath: str + pattern: str + replacement: str + + async def describe(self, models: Models): + return f"Replaced all instances of `{self.pattern}` with `{self.replacement}` in `{self.filepath}`" + + async def run(self, sdk: ContinueSDK): + file_content = await sdk.ide.readFile(self.filepath) + while self.pattern in file_content: + start_index = file_content.index(self.pattern) + end_index = start_index + len(self.pattern) + await sdk.ide.applyFileSystemEdit( + FileEdit( + filepath=self.filepath, + range=Range.from_indices(file_content, start_index, end_index - 1), + replacement=self.replacement, + ) + ) + file_content = ( + file_content[:start_index] + self.replacement + file_content[end_index:] + ) + await sdk.ide.saveFile(self.filepath) diff --git a/server/continuedev/plugins/steps/help.py b/server/continuedev/plugins/steps/help.py new file mode 100644 index 00000000..148dddb8 --- /dev/null +++ b/server/continuedev/plugins/steps/help.py @@ -0,0 +1,70 @@ +from textwrap import dedent + +from ...core.main import ChatMessage, Step +from ...core.sdk import ContinueSDK +from ...libs.util.telemetry import posthog_logger + +help = dedent( + """\ + Continue is an open-source coding autopilot. It is a VS Code extension that brings the power of ChatGPT to your IDE. + + It gathers context for you and stores your interactions automatically, so that you can avoid copy/paste now and benefit from a customized Large Language Model (LLM) later. + + Continue can be used to... + 1. Edit chunks of code with specific instructions (e.g. "/edit migrate this digital ocean terraform file into one that works for GCP") + 2. Get answers to questions without switching windows (e.g. "how do I find running process on port 8000?") + 3. Generate files from scratch (e.g. "/edit Create a Python CLI tool that uses the posthog api to get events from DAUs") + + You tell Continue to edit a specific section of code by highlighting it. If you highlight multiple code sections, then it will only edit the one with the purple glow around it. You can switch which one has the purple glow by clicking the paint brush. + + If you don't highlight any code, then Continue will insert at the location of your cursor. + + Continue passes all of the sections of code you highlight, the code above and below the to-be edited highlighted code section, and all previous steps above input box as context to the LLM. + + You can use cmd+m (Mac) / ctrl+m (Windows) to open Continue. You can use cmd+shift+e / ctrl+shift+e to open file Explorer. You can add your own OpenAI API key to VS Code Settings with `cmd+,` + + If Continue is stuck loading, try using `cmd+shift+p` to open the command palette, search "Reload Window", and then select it. This will reload VS Code and Continue and often fixes issues. + + If you have feedback, please use /feedback to let us know how you would like to use Continue. We are excited to hear from you!""" +) + + +class HelpStep(Step): + name: str = "Help" + user_input: str + manage_own_chat_context: bool = True + description: str = "" + + async def run(self, sdk: ContinueSDK): + question = self.user_input + + if question.strip() == "": + self.description = help + else: + self.description = "The following output is generated by a language model, which may hallucinate. Type just '/help'to see a fixed answer. You can also learn more by reading [the docs](https://continue.dev/docs).\n\n" + prompt = dedent( + f""" + Information: + + {help} + + Instructions: + + Please us the information below to provide a succinct answer to the following question: {question} + + Do not cite any slash commands other than those you've been told about, which are: /edit and /feedback. Never refer or link to any URL.""" + ) + + self.chat_context.append( + ChatMessage(role="user", content=prompt, summary="Help") + ) + messages = await sdk.get_chat_context() + generator = sdk.models.default.stream_chat(messages) + async for chunk in generator: + if "content" in chunk: + self.description += chunk["content"] + await sdk.update_ui() + + posthog_logger.capture_event( + "help", {"question": question, "answer": self.description} + ) diff --git a/server/continuedev/plugins/steps/input/nl_multiselect.py b/server/continuedev/plugins/steps/input/nl_multiselect.py new file mode 100644 index 00000000..f4b5e7a6 --- /dev/null +++ b/server/continuedev/plugins/steps/input/nl_multiselect.py @@ -0,0 +1,32 @@ +from typing import List, Union + +from ....core.main import Step +from ....core.sdk import ContinueSDK +from ....core.steps import WaitForUserInputStep + + +class NLMultiselectStep(Step): + hide: bool = True + + prompt: str + options: List[str] + + async def run(self, sdk: ContinueSDK): + user_response = ( + await sdk.run_step(WaitForUserInputStep(prompt=self.prompt)) + ).text + + def extract_option(text: str) -> Union[str, None]: + for option in self.options: + if option in text: + return option + return None + + first_try = extract_option(user_response.lower()) + if first_try is not None: + return first_try + + gpt_parsed = await sdk.models.default.complete( + f"These are the available options are: [{', '.join(self.options)}]. The user requested {user_response}. This is the exact string from the options array that they selected:" + ) + return extract_option(gpt_parsed) or self.options[0] diff --git a/server/continuedev/plugins/steps/main.py b/server/continuedev/plugins/steps/main.py new file mode 100644 index 00000000..936fd7e0 --- /dev/null +++ b/server/continuedev/plugins/steps/main.py @@ -0,0 +1,422 @@ +import os +from textwrap import dedent +from typing import Coroutine, List, Optional, Union + +from pydantic import BaseModel, Field + +from ...core.main import ContinueCustomException, Step +from ...core.observation import Observation +from ...core.sdk import ContinueSDK, Models +from ...core.steps import DefaultModelEditCodeStep +from ...libs.llm.base import LLM +from ...libs.llm.prompt_utils import MarkdownStyleEncoderDecoder +from ...libs.util.calculate_diff import calculate_diff2 +from ...libs.util.logging import logger +from ...models.filesystem import RangeInFile, RangeInFileWithContents +from ...models.filesystem_edit import EditDiff, FileEdit +from ...models.main import Range, Traceback + + +class Policy(BaseModel): + pass + + +class RunPolicyUntilDoneStep(Step): + policy: "Policy" + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + next_step = self.policy.next(sdk.config, sdk.history) + while next_step is not None: + observation = await sdk.run_step(next_step) + next_step = self.policy.next(sdk.config, sdk.history) + return observation + + +class FasterEditHighlightedCodeStep(Step): + user_input: str + hide = True + _completion: str = "Edit Code" + _edit_diffs: Union[List[EditDiff], None] = None + _prompt: str = dedent( + """\ + You will be given code to edit in order to perfectly satisfy the user request. All the changes you make must be described as replacements, which you should format in the following way: + FILEPATH + <FILE_TO_EDIT> + REPLACE_ME + <CODE_TO_REPLACE> + REPLACE_WITH + <CODE_TO_REPLACE_WITH> + + where <CODE_TO_REPLACE> and <CODE_TO_REPLACE_WITH> can be multiple lines, but should be the minimum needed to make the edit. Be sure to maintain existing whitespace at the start of lines. + + For example, if you want to replace the code `x = 1` with `x = 2` in main.py, you would write: + FILEPATH + main.py + REPLACE_ME + x = 1 + REPLACE_WITH + x = 2 + If you wanted to delete the code + ``` + def sum(a, b): + return a + b + ``` + in main.py, you would write: + FILEPATH + main.py + REPLACE_ME + def sum(a, b): + return a + b + REPLACE_WITH + + You may need to make multiple edits; respond with exactly as many as needed. + + Below is the code before changes: + + {code} + + This is the user request: "{user_input}" + Here is the description of changes to make: +""" + ) + + async def describe(self, models: Models) -> Coroutine[str, None, None]: + return "Editing highlighted code" + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + range_in_files = await sdk.get_code_context(only_editing=True) + if len(range_in_files) == 0: + # Get the full contents of all visible files + files = await sdk.ide.getVisibleFiles() + contents = {} + for file in files: + contents[file] = await sdk.ide.readFile(file) + + range_in_files = [ + RangeInFileWithContents.from_entire_file(filepath, content) + for filepath, content in contents.items() + ] + + enc_dec = MarkdownStyleEncoderDecoder(range_in_files) + code_string = enc_dec.encode() + prompt = self._prompt.format(code=code_string, user_input=self.user_input) + + rif_dict = {} + for rif in range_in_files: + rif_dict[rif.filepath] = rif.contents + + completion = await sdk.models.summarize.complete(prompt) + + # Temporarily doing this to generate description. + self._prompt = prompt + self._completion = completion + logger.debug(completion) + + # ALTERNATIVE DECODING STEP HERE + raw_file_edits = [] + lines = completion.split("\n") + current_edit = {} + status = "FILEPATH" + for i in range(0, len(lines)): + line = lines[i] + if line == "FILEPATH": + if "FILEPATH" in current_edit: + raw_file_edits.append(current_edit) + current_edit = {} + status = "FILEPATH" + elif line == "REPLACE_ME": + status = "REPLACE_ME" + elif line == "REPLACE_WITH": + status = "REPLACE_WITH" + elif status == "FILEPATH": + current_edit["filepath"] = line + elif status == "REPLACE_ME": + if "replace_me" in current_edit: + current_edit["replace_me"] += "\n" + line + else: + current_edit["replace_me"] = line + elif status == "REPLACE_WITH": + if "replace_with" in current_edit: + current_edit["replace_with"] += "\n" + line + else: + current_edit["replace_with"] = line + if "filepath" in current_edit: + raw_file_edits.append(current_edit) + + file_edits = [] + for edit in raw_file_edits: + filepath = edit["filepath"] + replace_me = edit["replace_me"] + replace_with = edit["replace_with"] + file_edits.append( + FileEdit( + filepath=filepath, + range=Range.from_lines_snippet_in_file( + content=rif_dict[filepath], snippet=replace_me + ), + replacement=replace_with, + ) + ) + # ------------------------------ + + self._edit_diffs = [] + for file_edit in file_edits: + diff = await sdk.apply_filesystem_edit(file_edit) + self._edit_diffs.append(diff) + + for filepath in set([file_edit.filepath for file_edit in file_edits]): + await sdk.ide.saveFile(filepath) + await sdk.ide.setFileOpen(filepath) + + return None + + +class StarCoderEditHighlightedCodeStep(Step): + user_input: str + name: str = "Editing Code" + hide = False + _prompt: str = "<commit_before>{code}<commit_msg>{user_request}<commit_after>" + + _prompt_and_completion: str = "" + + async def describe(self, models: Models) -> Coroutine[str, None, None]: + return await models.summarize.complete( + f"{self._prompt_and_completion}\n\nPlease give brief a description of the changes made above using markdown bullet points:" + ) + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + range_in_files = await sdk.get_code_context(only_editing=True) + found_highlighted_code = len(range_in_files) > 0 + if not found_highlighted_code: + # Get the full contents of all visible files + files = await sdk.ide.getVisibleFiles() + contents = {} + for file in files: + contents[file] = await sdk.ide.readFile(file) + + range_in_files = [ + RangeInFileWithContents.from_entire_file(filepath, content) + for filepath, content in contents.items() + ] + + rif_dict = {} + for rif in range_in_files: + rif_dict[rif.filepath] = rif.contents + + for rif in range_in_files: + prompt = self._prompt.format( + code=rif.contents, user_request=self.user_input + ) + + if found_highlighted_code: + full_file_contents = await sdk.ide.readFile(rif.filepath) + segs = full_file_contents.split(rif.contents) + prompt = f"<file_prefix>{segs[0]}<file_suffix>{segs[1]}" + prompt + + completion = str(await sdk.models.starcoder.complete(prompt)) + eot_token = "<|endoftext|>" + completion = completion.removesuffix(eot_token) + + if found_highlighted_code: + rif.contents = segs[0] + rif.contents + segs[1] + completion = segs[0] + completion + segs[1] + + self._prompt_and_completion += prompt + completion + + edits = calculate_diff2( + rif.filepath, rif.contents, completion.removesuffix("\n") + ) + for edit in edits: + await sdk.ide.applyFileSystemEdit(edit) + + # await sdk.ide.applyFileSystemEdit( + # FileEdit(filepath=rif.filepath, range=rif.range, replacement=completion)) + await sdk.ide.saveFile(rif.filepath) + await sdk.ide.setFileOpen(rif.filepath) + + +def decode_escaped_path(path: str) -> str: + """We use a custom escaping scheme to record the full path of a file as a + corresponding basename, but withut URL encoding, because then the URI just gets + interpreted as a full path again.""" + return path.replace("$f$", "/").replace("$b$", "\\") + + +def encode_escaped_path(path: str) -> str: + """We use a custom escaping scheme to record the full path of a file as a + corresponding basename, but withut URL encoding, because then the URI just gets + interpreted as a full path again.""" + return path.replace("/", "$f$").replace("\\", "$b$") + + +class EditAlreadyEditedRangeStep(Step): + hide = True + model: Optional[LLM] = None + range_in_file: RangeInFile + + user_input: str + + _prompt = dedent( + """\ + You were previously asked to edit this code. The request was: + + "{prev_user_input}" + + And you generated this diff: + + {diff} + + Could you please re-edit this code to follow these secondary instructions? + + "{user_input}" + """ + ) + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + if os.path.basename(self.range_in_file.filepath) in os.listdir( + os.path.expanduser(os.path.join("~", ".continue", "diffs")) + ): + decoded_basename = decode_escaped_path( + os.path.basename(self.range_in_file.filepath) + ) + self.range_in_file.filepath = decoded_basename + + self.range_in_file.range = sdk.context.get("last_edit_range") + + if self.range_in_file.range.start == self.range_in_file.range.end: + self.range_in_file.range = Range.from_entire_file( + await sdk.ide.readFile(self.range_in_file.filepath) + ) + + await sdk.run_step( + DefaultModelEditCodeStep( + model=self.model, + user_input=self._prompt.format( + prev_user_input=sdk.context.get("last_edit_user_input"), + diff=sdk.context.get("last_edit_diff"), + user_input=self.user_input, + ), + range_in_files=[self.range_in_file], + ) + ) + + +class EditHighlightedCodeStep(Step): + user_input: str = Field( + ..., + title="User Input", + description="The natural language request describing how to edit the code", + ) + model: Optional[LLM] = None + hide = True + description: str = "Change the contents of the currently highlighted code or open file. You should call this function if the user asks seems to be asking for a code change." + + summary_prompt: Optional[str] = None + + async def describe(self, models: Models) -> Coroutine[str, None, None]: + return "Editing code" + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + range_in_files = sdk.get_code_context(only_editing=True) + + # If nothing highlighted, insert at the cursor if possible + if len(range_in_files) == 0: + highlighted_code = await sdk.ide.getHighlightedCode() + if highlighted_code is not None: + for rif in highlighted_code: + if rif.range.start == rif.range.end: + range_in_files.append( + RangeInFileWithContents.from_range_in_file(rif, "") + ) + + # If still no highlighted code, raise error + if len(range_in_files) == 0: + raise ContinueCustomException( + message="Please highlight some code and try again.", + title="No Code Selected (highlight and select with cmd+shift+M)", + ) + + # If all of the ranges are point ranges, only edit the last one + if all([rif.range.start == rif.range.end for rif in range_in_files]): + range_in_files = [range_in_files[-1]] + + range_in_files = list( + map( + lambda x: RangeInFile(filepath=x.filepath, range=x.range), + range_in_files, + ) + ) + + for range_in_file in range_in_files: + # Check whether re-editing + if ( + os.path.dirname(range_in_file.filepath) + == os.path.expanduser(os.path.join("~", ".continue", "diffs")) + or encode_escaped_path(range_in_file.filepath) + in os.listdir( + os.path.expanduser(os.path.join("~", ".continue", "diffs")) + ) + ) and sdk.context.get("last_edit_user_input") is not None: + await sdk.run_step( + EditAlreadyEditedRangeStep( + range_in_file=range_in_file, + user_input=self.user_input, + model=self.model, + ) + ) + return + + args = { + "user_input": self.user_input, + "range_in_files": range_in_files, + "model": self.model, + } + if self.summary_prompt: + args["summary_prompt"] = self.summary_prompt + + await sdk.run_step(DefaultModelEditCodeStep(**args)) + + +class UserInputStep(Step): + user_input: str + + +class SolveTracebackStep(Step): + traceback: Traceback + + async def describe(self, models: Models) -> Coroutine[str, None, None]: + return f"```\n{self.traceback.full_traceback}\n```" + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + prompt = dedent( + """I ran into this problem with my Python code: + + {traceback} + + Below are the files that might need to be fixed: + + {code} + + This is what the code should be in order to avoid the problem: + """ + ).format(traceback=self.traceback.full_traceback, code="{code}") + + range_in_files = [] + for frame in self.traceback.frames: + content = await sdk.ide.readFile(frame.filepath) + range_in_files.append(RangeInFile.from_entire_file(frame.filepath, content)) + + await sdk.run_step( + DefaultModelEditCodeStep(range_in_files=range_in_files, user_input=prompt) + ) + return None + + +class EmptyStep(Step): + hide: bool = True + + async def describe(self, models: Models) -> Coroutine[str, None, None]: + return "" + + async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]: + pass diff --git a/server/continuedev/plugins/steps/on_traceback.py b/server/continuedev/plugins/steps/on_traceback.py new file mode 100644 index 00000000..b72ce809 --- /dev/null +++ b/server/continuedev/plugins/steps/on_traceback.py @@ -0,0 +1,206 @@ +import os +from textwrap import dedent +from typing import Dict, List, Optional, Tuple + +from ...core.main import ChatMessage, ContinueCustomException, Step +from ...core.sdk import ContinueSDK +from ...core.steps import UserInputStep +from ...libs.util.filter_files import should_filter_path +from ...libs.util.traceback.traceback_parsers import ( + get_javascript_traceback, + get_python_traceback, + parse_python_traceback, +) +from ...models.filesystem import RangeInFile +from ...models.main import Range, Traceback, TracebackFrame +from .chat import SimpleChatStep + + +def extract_traceback_str(output: str) -> str: + tb = output.strip() + for tb_parser in [get_python_traceback, get_javascript_traceback]: + if parsed_tb := tb_parser(tb): + return parsed_tb + + +class DefaultOnTracebackStep(Step): + output: str + name: str = "Help With Traceback" + hide: bool = True + + async def find_relevant_files(self, sdk: ContinueSDK): + # Add context for any files in the traceback that are in the workspace + for line in self.output.split("\n"): + segs = line.split(" ") + for seg in segs: + if ( + seg.startswith(os.path.sep) + and os.path.exists(seg) # TODO: Use sdk.ide.fileExists + and os.path.commonprefix([seg, sdk.ide.workspace_directory]) + == sdk.ide.workspace_directory + ): + file_contents = await sdk.ide.readFile(seg) + self.chat_context.append( + ChatMessage( + role="user", + content=f"The contents of {seg}:\n```\n{file_contents}\n```", + summary="", + ) + ) + # TODO: The ideal is that these are added as context items, so then the user can see them + # And this function is where you can get arbitrarily fancy about adding context + + async def run(self, sdk: ContinueSDK): + if self.output.strip() == "": + raise ContinueCustomException( + title="No terminal open", + message="You must have a terminal open in order to automatically debug with Continue.", + ) + + if get_python_traceback(self.output) is not None and sdk.lsp is not None: + await sdk.run_step(SolvePythonTracebackStep(output=self.output)) + return + + tb = extract_traceback_str(self.output) or self.output[-8000:] + + await sdk.run_step( + UserInputStep( + user_input=f"""I got the following error, can you please help explain how to fix it?\n\n{tb}""", + ) + ) + await sdk.run_step(SimpleChatStep(name="Help With Traceback")) + + +def filter_frames(frames: List[TracebackFrame]) -> List[TracebackFrame]: + """Filter out frames that are not relevant to the user's code.""" + return list(filter(lambda x: should_filter_path(x.filepath), frames)) + + +def find_external_call( + frames: List[TracebackFrame], +) -> Optional[Tuple[TracebackFrame, TracebackFrame]]: + """Moving up from the bottom of the stack, if the frames are not user code, then find the last frame before it becomes user code.""" + if not should_filter_path(frames[-1].filepath): + # No external call, error comes directly from user code + return None + + for i in range(len(frames) - 2, -1, -1): + if not should_filter_path(frames[i].filepath): + return frames[i], frames[i + 1] + + +def get_func_source_for_frame(frame: Dict) -> str: + """Get the source for the function called in the frame.""" + pass + + +async def fetch_docs_for_external_call(external_call: Dict, next_frame: Dict) -> str: + """Fetch docs for the external call.""" + pass + + +class SolvePythonTracebackStep(Step): + output: str + name: str = "Solve Traceback" + hide: bool = True + + async def external_call_prompt( + self, sdk: ContinueSDK, external_call: Tuple[Dict, Dict], tb_string: str + ) -> str: + external_call, next_frame = external_call + source_line = external_call["source_line"] + external_func_source = get_func_source_for_frame(next_frame) + docs = await fetch_docs_for_external_call(external_call, next_frame) + + prompt = dedent( + f"""\ + I got the following error: + + {tb_string} + + I tried to call an external library like this: + + ```python + {source_line} + ``` + + This is the definition of the function I tried to call: + + ```python + {external_func_source} + ``` + + Here's the documentation for the external library I tried to call: + + {docs} + + Explain how to fix the error. + """ + ) + + return prompt + + async def normal_traceback_prompt( + self, sdk: ContinueSDK, tb: Traceback, tb_string: str + ) -> str: + function_bodies = await get_functions_from_traceback(tb, sdk) + + prompt = ( + "Here are the functions from the traceback (most recent call last):\n\n" + ) + for i, function_body in enumerate(function_bodies): + prompt += f'File "{tb.frames[i].filepath}", line {tb.frames[i].lineno}, in {tb.frames[i].function}\n\n```python\n{function_body or tb.frames[i].code}\n```\n\n' + + prompt += ( + "Here is the traceback:\n\n```\n" + + tb_string + + "\n```\n\nExplain how to fix the error." + ) + + return prompt + + async def run(self, sdk: ContinueSDK): + tb_string = get_python_traceback(self.output) + tb = parse_python_traceback(tb_string) + + if external_call := find_external_call(tb.frames): + prompt = await self.external_call_prompt(sdk, external_call, tb_string) + else: + prompt = await self.normal_traceback_prompt(sdk, tb, tb_string) + + await sdk.run_step( + UserInputStep( + user_input=prompt, + ) + ) + await sdk.run_step(SimpleChatStep(name="Help With Traceback")) + + +async def get_function_body(frame: TracebackFrame, sdk: ContinueSDK) -> Optional[str]: + """Get the function body from the traceback frame.""" + if sdk.lsp is None: + return None + + document_symbols = await sdk.lsp.document_symbol(frame.filepath) + for symbol in document_symbols: + if symbol.name == frame.function: + r = symbol.location.range + return await sdk.ide.readRangeInFile( + RangeInFile( + filepath=frame.filepath, + range=Range.from_shorthand( + r.start.line, r.start.character, r.end.line, r.end.character + ), + ) + ) + return None + + +async def get_functions_from_traceback(tb: Traceback, sdk: ContinueSDK) -> List[str]: + """Get the function bodies from the traceback.""" + function_bodies = [] + for frame in tb.frames: + if frame.function: + function_bodies.append(await get_function_body(frame, sdk)) + + return function_bodies diff --git a/server/continuedev/plugins/steps/open_config.py b/server/continuedev/plugins/steps/open_config.py new file mode 100644 index 00000000..c57939f8 --- /dev/null +++ b/server/continuedev/plugins/steps/open_config.py @@ -0,0 +1,17 @@ +from textwrap import dedent + +from ...core.main import Step +from ...core.sdk import ContinueSDK +from ...libs.util.paths import getConfigFilePath + + +class OpenConfigStep(Step): + name: str = "Open config" + + async def describe(self, models): + return dedent( + 'Read [the docs](https://continue.dev/docs/customization/overview) to learn more about how you can customize Continue using `"config.py"`.' + ) + + async def run(self, sdk: ContinueSDK): + await sdk.ide.setFileOpen(getConfigFilePath()) diff --git a/server/continuedev/plugins/steps/react.py b/server/continuedev/plugins/steps/react.py new file mode 100644 index 00000000..1b9bc265 --- /dev/null +++ b/server/continuedev/plugins/steps/react.py @@ -0,0 +1,44 @@ +from textwrap import dedent +from typing import List, Tuple, Union + +from ...core.main import Step +from ...core.sdk import ContinueSDK + + +class NLDecisionStep(Step): + user_input: str + default_step: Union[Step, None] = None + steps: List[Tuple[Step, str]] + + hide: bool = False + name: str = "Deciding what to do next" + + async def run(self, sdk: ContinueSDK): + step_descriptions = "\n".join( + [f"- {step[0].name}: {step[1]}" for step in self.steps] + ) + prompt = dedent( + f"""\ + The following steps are available, in the format "- [step name]: [step description]": + {step_descriptions} + + The user gave the following input: + + {self.user_input} + + Select the step which should be taken next to satisfy the user input. Say only the name of the selected step. You must choose one:""" + ) + + resp = (await sdk.models.summarize.complete(prompt)).lower() + + step_to_run = None + for step in self.steps: + if step[0].name.lower() in resp: + step_to_run = step[0] + + step_to_run = step_to_run or self.default_step or self.steps[0] + + self.hide = True + await sdk.update_ui() + + await sdk.run_step(step_to_run) diff --git a/server/continuedev/plugins/steps/refactor.py b/server/continuedev/plugins/steps/refactor.py new file mode 100644 index 00000000..56e9e09e --- /dev/null +++ b/server/continuedev/plugins/steps/refactor.py @@ -0,0 +1,136 @@ +import asyncio +from typing import List, Optional + +from ripgrepy import Ripgrepy + +from ...core.main import Step +from ...core.models import Models +from ...core.sdk import ContinueSDK +from ...libs.llm.prompts.edit import simplified_edit_prompt +from ...libs.util.ripgrep import get_rg_path +from ...libs.util.strings import remove_quotes_and_escapes, strip_code_block +from ...libs.util.templating import render_prompt_template +from ...models.filesystem import RangeInFile +from ...models.filesystem_edit import FileEdit +from ...models.main import PositionInFile, Range + + +class RefactorReferencesStep(Step): + name: str = "Refactor references of a symbol" + user_input: str + symbol_location: PositionInFile + + async def describe(self, models: Models): + return f"Renamed all instances of `{self.function_name}` to `{self.new_function_name}` in `{self.filepath}`" + + async def run(self, sdk: ContinueSDK): + while sdk.lsp is None or not sdk.lsp.ready: + await asyncio.sleep(0.1) + + references = await sdk.lsp.find_references( + self.symbol_location.position, self.symbol_location.filepath, False + ) + await sdk.run_step( + ParallelEditStep(user_input=self.user_input, range_in_files=references) + ) + + +class RefactorBySearchStep(Step): + name: str = "Refactor by search" + + pattern: str + user_input: str + + rg_path: Optional[str] = None + "Optional path to ripgrep executable" + + def get_range_for_result(self, result) -> RangeInFile: + pass + + async def run(self, sdk: ContinueSDK): + rg = Ripgrepy( + self.pattern, + sdk.ide.workspace_directory, + rg_path=self.rg_path or get_rg_path(), + ) + + results = rg.I().context(2).run() + range_in_files = [self.get_range_for_result(result) for result in results] + + await sdk.run_step( + ParallelEditStep(user_input=self.user_input, range_in_files=range_in_files) + ) + + +class ParallelEditStep(Step): + name: str = "Edit multiple ranges in parallel" + user_input: str + range_in_files: List[RangeInFile] + + hide: bool = True + + async def single_edit(self, sdk: ContinueSDK, range_in_file: RangeInFile): + # TODO: Can use folding info to get a more intuitively shaped range + expanded_range = await sdk.lsp.get_enclosing_folding_range(range_in_file) + if ( + expanded_range is None + or expanded_range.range.start.line != range_in_file.range.start.line + ): + expanded_range = Range.from_shorthand( + range_in_file.range.start.line, 0, range_in_file.range.end.line + 1, 0 + ) + else: + expanded_range = expanded_range.range + + new_rif = RangeInFile( + filepath=range_in_file.filepath, + range=expanded_range, + ) + code_to_edit = await sdk.ide.readRangeInFile(range_in_file=new_rif) + + # code_to_edit, common_whitespace = dedent_and_get_common_whitespace(code_to_edit) + + prompt = render_prompt_template( + simplified_edit_prompt, + history=[], + other_data={ + "code_to_edit": code_to_edit, + "user_input": self.user_input, + }, + ) + print(prompt + "\n\n-------------------\n\n") + + new_code = await sdk.models.edit.complete(prompt=prompt) + new_code = strip_code_block(remove_quotes_and_escapes(new_code)) + "\n" + # new_code = ( + # "\n".join([common_whitespace + line for line in new_code.split("\n")]) + # + "\n" + # ) + + print(new_code + "\n\n-------------------\n\n") + + await sdk.ide.applyFileSystemEdit( + FileEdit( + filepath=range_in_file.filepath, + range=expanded_range, + replacement=new_code, + ) + ) + + async def edit_file(self, sdk: ContinueSDK, filepath: str): + ranges_in_file = [ + range_in_file + for range_in_file in self.range_in_files + if range_in_file.filepath == filepath + ] + # Sort in reverse order so that we don't mess up the ranges + ranges_in_file.sort(key=lambda x: x.range.start.line, reverse=True) + for i in range(len(ranges_in_file)): + await self.single_edit(sdk=sdk, range_in_file=ranges_in_file[i]) + + async def run(self, sdk: ContinueSDK): + tasks = [] + for filepath in set([rif.filepath for rif in self.range_in_files]): + tasks.append(self.edit_file(sdk=sdk, filepath=filepath)) + + await asyncio.gather(*tasks) diff --git a/server/continuedev/plugins/steps/search_directory.py b/server/continuedev/plugins/steps/search_directory.py new file mode 100644 index 00000000..83516719 --- /dev/null +++ b/server/continuedev/plugins/steps/search_directory.py @@ -0,0 +1,84 @@ +import asyncio +import os +import re +from textwrap import dedent +from typing import List, Union + +from ...core.main import Step +from ...core.sdk import ContinueSDK +from ...libs.util.create_async_task import create_async_task +from ...models.filesystem import RangeInFile +from ...models.main import Range + +# Already have some code for this somewhere +IGNORE_DIRS = ["env", "venv", ".venv"] +IGNORE_FILES = [".env"] + + +def find_all_matches_in_dir(pattern: str, dirpath: str) -> List[RangeInFile]: + range_in_files = [] + for root, dirs, files in os.walk(dirpath): + dirname = os.path.basename(root) + if dirname.startswith(".") or dirname in IGNORE_DIRS: + continue # continue! + for file in files: + if file in IGNORE_FILES: + continue # pun intended + with open(os.path.join(root, file), "r") as f: + # Find the index of all occurrences of the pattern in the file. Use re. + file_content = f.read() + results = re.finditer(pattern, file_content) + range_in_files += [ + RangeInFile( + filepath=os.path.join(root, file), + range=Range.from_indices( + file_content, result.start(), result.end() + ), + ) + for result in results + ] + + return range_in_files + + +class WriteRegexPatternStep(Step): + user_request: str + + async def run(self, sdk: ContinueSDK): + # Ask the user for a regex pattern + pattern = await sdk.models.summarize.complete( + dedent( + f"""\ + This is the user request: + + {self.user_request} + + Please write either a regex pattern or just a string that be used with python's re module to find all matches requested by the user. It will be used as `re.findall(<PATTERN_YOU_WILL_WRITE>, file_content)`. Your output should be only the regex or string, nothing else:""" + ) + ) + + return pattern + + +class EditAllMatchesStep(Step): + pattern: str + user_request: str + directory: Union[str, None] = None + + async def run(self, sdk: ContinueSDK): + # Search all files for a given string + range_in_files = find_all_matches_in_dir( + self.pattern, self.directory or await sdk.ide.getWorkspaceDirectory() + ) + + tasks = [ + create_async_task( + sdk.edit_file( + range=range_in_file.range, + filename=range_in_file.filepath, + prompt=self.user_request, + ) + ) + for range_in_file in range_in_files + ] + await asyncio.gather(*tasks) diff --git a/server/continuedev/plugins/steps/setup_model.py b/server/continuedev/plugins/steps/setup_model.py new file mode 100644 index 00000000..87e52f1b --- /dev/null +++ b/server/continuedev/plugins/steps/setup_model.py @@ -0,0 +1,38 @@ +from ...core.main import Step +from ...core.sdk import ContinueSDK +from ...libs.util.paths import getConfigFilePath +from ...models.filesystem import RangeInFile +from ...models.main import Range + +MODEL_CLASS_TO_MESSAGE = { + "OpenAI": "Obtain your OpenAI API key from [here](https://platform.openai.com/account/api-keys) and paste it into the `api_key` field at config.models.default.api_key in `config.py`. Then reload the VS Code window for changes to take effect.", + "OpenAIFreeTrial": "To get started with OpenAI models, obtain your OpenAI API key from [here](https://platform.openai.com/account/api-keys) and paste it into the `api_key` field at config.models.default.api_key in `config.py`. Then reload the VS Code window for changes to take effect.", + "AnthropicLLM": "To get started with Anthropic, you first need to sign up for the beta [here](https://claude.ai/login) to obtain an API key. Once you have the key, paste it into the `api_key` field at config.models.default.api_key in `config.py`. Then reload the VS Code window for changes to take effect.", + "ReplicateLLM": "To get started with Replicate, sign up to obtain an API key [here](https://replicate.ai/), then paste it into the `api_key` field at config.models.default.api_key in `config.py`.", + "Ollama": "To get started with Ollama, download the app from [ollama.ai](https://ollama.ai/). Once it is downloaded, be sure to pull at least one model and use its name in the model field in config.py (e.g. `model='codellama'`).", + "GGML": "GGML models can be run locally using the `llama-cpp-python` library. To learn how to set up a local llama-cpp-python server, read [here](https://github.com/continuedev/ggml-server-example). Once it is started on port 8000, you're all set!", + "TogetherLLM": "To get started using models from Together, first obtain your Together API key from [here](https://together.ai). Paste it into the `api_key` field at config.models.default.api_key in `config.py`. Then, on their models page, press 'start' on the model of your choice and make sure the `model=` parameter in the config file for the `TogetherLLM` class reflects the name of this model. Finally, reload the VS Code window for changes to take effect.", + "LlamaCpp": "To get started with this model, clone the [`llama.cpp` repo](https://github.com/ggerganov/llama.cpp) and follow the instructions to set up the server [here](https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md#build). Any of the parameters described in the README can be passed to the `llama_cpp_args` field in the `LlamaCpp` class in `config.py`.", + "HuggingFaceInferenceAPI": "To get started with the HuggingFace Inference API, first deploy a model and obtain your API key from [here](https://huggingface.co/inference-api). Paste it into the `hf_token` field at config.models.default.hf_token in `config.py`. Finally, reload the VS Code window for changes to take effect.", + "GooglePaLMAPI": "To get started with the Google PaLM API, create an API key in Makersuite [here](https://makersuite.google.com/u/2/app/apikey), then paste it into the `api_key` field at config.models.default.api_key in `config.py`.", +} + + +class SetupModelStep(Step): + model_class: str + name: str = "Setup model in config.py" + + async def run(self, sdk: ContinueSDK): + await sdk.ide.setFileOpen(getConfigFilePath()) + self.description = MODEL_CLASS_TO_MESSAGE.get( + self.model_class, "Please finish setting up this model in `config.py`" + ) + + config_contents = await sdk.ide.readFile(getConfigFilePath()) + start = config_contents.find("default=") + len("default=") + end = config_contents.find("saved=") - 1 + range = Range.from_indices(config_contents, start, end) + range.end.line -= 1 + await sdk.ide.highlightCode( + RangeInFile(filepath=getConfigFilePath(), range=range) + ) diff --git a/server/continuedev/plugins/steps/share_session.py b/server/continuedev/plugins/steps/share_session.py new file mode 100644 index 00000000..1d68dc90 --- /dev/null +++ b/server/continuedev/plugins/steps/share_session.py @@ -0,0 +1,52 @@ +import json +import os +import time +from typing import Optional + +from ...core.main import FullState, Step +from ...core.sdk import ContinueSDK +from ...libs.util.paths import getGlobalFolderPath, getSessionFilePath +from ...server.session_manager import session_manager + + +class ShareSessionStep(Step): + session_id: Optional[str] = None + + async def run(self, sdk: ContinueSDK): + if self.session_id is None: + self.session_id = sdk.ide.session_id + + await session_manager.persist_session(self.session_id) + time.sleep(0.5) + + # Load the session data and format as a markdown file + session_filepath = getSessionFilePath(self.session_id) + with open(session_filepath, "r") as f: + session_state = FullState(**json.load(f)) + + import datetime + + date_created = datetime.datetime.fromtimestamp( + float(session_state.session_info.date_created) + ).strftime("%Y-%m-%d %H:%M:%S") + content = f"This is a session transcript from [Continue](https://continue.dev) on {date_created}.\n\n" + + for node in session_state.history.timeline[:-2]: + if node.step.hide: + continue # ay + + content += f"## {node.step.name}\n" + content += f"{node.step.description}\n\n" + + # Save to a markdown file + save_filepath = os.path.join( + getGlobalFolderPath(), f"{session_state.session_info.title}.md" + ) + + with open(save_filepath, "w") as f: + f.write(content) + + # Open the file + await sdk.ide.setFileOpen(save_filepath) + + self.description = f"The session transcript has been saved to a markdown file at {save_filepath}." diff --git a/server/continuedev/plugins/steps/steps_on_startup.py b/server/continuedev/plugins/steps/steps_on_startup.py new file mode 100644 index 00000000..58d56703 --- /dev/null +++ b/server/continuedev/plugins/steps/steps_on_startup.py @@ -0,0 +1,19 @@ +from ...core.main import Step +from ...core.sdk import ContinueSDK, Models + + +class StepsOnStartupStep(Step): + hide: bool = True + + async def describe(self, models: Models): + return "Running steps on startup" + + async def run(self, sdk: ContinueSDK): + steps_on_startup = sdk.config.steps_on_startup + + for step_type in steps_on_startup: + if isinstance(step_type, Step): + step = step_type + else: + step = step_type() + await sdk.run_step(step) diff --git a/server/continuedev/plugins/steps/welcome.py b/server/continuedev/plugins/steps/welcome.py new file mode 100644 index 00000000..ef1acfc1 --- /dev/null +++ b/server/continuedev/plugins/steps/welcome.py @@ -0,0 +1,40 @@ +import os +from textwrap import dedent + +from ...core.main import Step +from ...core.sdk import ContinueSDK, Models +from ...models.filesystem_edit import AddFile + + +class WelcomeStep(Step): + name: str = "Welcome to Continue!" + hide: bool = True + + async def describe(self, models: Models): + return "Welcome to Continue!" + + async def run(self, sdk: ContinueSDK): + continue_dir = os.path.expanduser("~/.continue") + filepath = os.path.join(continue_dir, "calculator.py") + if os.path.exists(filepath): + return + if not os.path.exists(continue_dir): + os.mkdir(continue_dir) + + await sdk.ide.applyFileSystemEdit( + AddFile( + filepath=filepath, + content=dedent( + """\ + \"\"\" + Welcome to Continue! To learn how to use it, delete this comment and try to use Continue for the following: + - "Write me a calculator class" + - Ask for a new method (e.g. "exp", "mod", "sqrt") + - Type /comment to write comments for the entire class + - Ask about how the class works, how to write it in another language, etc. + \"\"\"""" + ), + ) + ) + + # await sdk.ide.setFileOpen(filepath=filepath) |