feat: ✨ FileTreeContextProvider

also pass workspace_dir: str to provide_context_items
This commit is contained in:
Nate Sesti 2023-07-29 23:02:23 -07:00
parent 17566c66e0
commit 8bd76be6c0
7 changed files with 58 additions and 10 deletions

View file

@ -75,7 +75,7 @@ class Autopilot(ContinueBaseModel):
HighlightedCodeContextProvider(ide=ide),
FileContextProvider(workspace_dir=ide.workspace_directory)
])
await autopilot.context_manager.load_index()
await autopilot.context_manager.load_index(ide.workspace_directory)
return autopilot

View file

@ -35,7 +35,7 @@ class ContextProvider(BaseModel):
return self.selected_items
@abstractmethod
async def provide_context_items(self) -> List[ContextItem]:
async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]:
"""
Provide documents for search index. This is run on startup.
@ -159,9 +159,9 @@ class ContextManager:
return cls(context_providers)
async def load_index(self):
async def load_index(self, workspace_dir: str):
for _, provider in self.context_providers.items():
context_items = await provider.provide_context_items()
context_items = await provider.provide_context_items(workspace_dir)
documents = [
{
"id": item.description.id.to_string(),

View file

@ -49,13 +49,12 @@ class FileContextProvider(ContextProvider):
"""
title = "file"
workspace_dir: str
ignore_patterns: List[str] = DEFAULT_IGNORE_DIRS + \
list(filter(lambda d: f"**/{d}", DEFAULT_IGNORE_DIRS))
async def provide_context_items(self) -> List[ContextItem]:
async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]:
filepaths = []
for root, dir_names, file_names in os.walk(self.workspace_dir):
for root, dir_names, file_names in os.walk(workspace_dir):
dir_names[:] = [d for d in dir_names if not any(
fnmatch(d, pattern) for pattern in self.ignore_patterns)]
for file_name in file_names:

View file

@ -0,0 +1,49 @@
import json
from typing import List
import os
import aiohttp
from ...core.main import ContextItem, ContextItemDescription, ContextItemId
from ...core.context import ContextProvider
def format_file_tree(startpath) -> str:
result = ""
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
result += '{}{}/'.format(indent, os.path.basename(root)) + "\n"
subindent = ' ' * 4 * (level + 1)
for f in files:
result += '{}{}'.format(subindent, f) + "\n"
return result
class FileTreeContextProvider(ContextProvider):
title = "tree"
workspace_dir: str = None
def _filetree_context_item(self):
return ContextItem(
content=format_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 [self._filetree_context_item()]
async def get_item(self, id: ContextItemId, query: str) -> ContextItem:
if not id.item_id == self.title:
raise Exception("Invalid item id")
return self._filetree_context_item()

View file

@ -15,7 +15,7 @@ class GitHubIssuesContextProvider(ContextProvider):
repo_name: str
auth_token: str
async def provide_context_items(self) -> List[ContextItem]:
async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]:
auth = Auth.Token(self.auth_token)
gh = Github(auth=auth)

View file

@ -42,7 +42,7 @@ class GoogleContextProvider(ContextProvider):
async with session.post(url, headers=headers, data=payload) as response:
return await response.text()
async def provide_context_items(self) -> List[ContextItem]:
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:

View file

@ -96,7 +96,7 @@ class HighlightedCodeContextProvider(ContextProvider):
hr.item.description.name = self._rif_to_name(
hr.rif, display_filename=basename)
async def provide_context_items(self) -> List[ContextItem]:
async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]:
return []
async def get_item(self, id: ContextItemId, query: str) -> ContextItem: