Skip to content

Commit c3b83f8

Browse files
committed
refactor(cli): ls in CLI mode now use the DB adapter layer.
1 parent bfc8404 commit c3b83f8

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

src/vectorcode/database/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def get_database_connector(config: Config) -> DatabaseConnectorBase:
1616
"""
1717
cls: Optional[Type[DatabaseConnectorBase]] = None
1818

19+
if not config.db_type.endswith("Connector"):
20+
config.db_type = f"{config.db_type}Connector"
21+
1922
match config.db_type:
2023
case "ChromaDB0Connector":
2124
from vectorcode.database.chroma0 import ChromaDB0Connector

src/vectorcode/database/chroma0.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from chromadb.errors import InvalidCollectionException
2121
from tree_sitter import Point
2222

23-
import vectorcode.subcommands.query.types as vectorcode_query_types
2423
from vectorcode.chunking import Chunk, TreeSitterChunker
2524
from vectorcode.cli_utils import Config, LockManager, expand_path
2625
from vectorcode.common import get_embedding_function
@@ -34,21 +33,22 @@
3433
VectoriseStats,
3534
)
3635
from vectorcode.database.utils import get_collection_id, hash_file
36+
from vectorcode.subcommands.query import types
3737
from vectorcode.subcommands.vectorise import get_uuid
3838

3939
_logger = logging.getLogger(name=__name__)
4040

4141

4242
def __convert_chroma_query_results(
4343
chroma_result: QueryResult, queries: list[str]
44-
) -> list[vectorcode_query_types.QueryResult]:
44+
) -> list[types.QueryResult]:
4545
"""Convert chromadb query result to in-house query results"""
4646
assert chroma_result["documents"] is not None
4747
assert chroma_result["distances"] is not None
4848
assert chroma_result["metadatas"] is not None
4949
assert chroma_result["ids"] is not None
5050

51-
chroma_results_list: list[vectorcode_query_types.QueryResult] = []
51+
chroma_results_list: list[types.QueryResult] = []
5252
for q_i in range(len(queries)):
5353
q = queries[q_i]
5454
documents = chroma_result["documents"][q_i]
@@ -64,7 +64,7 @@ def __convert_chroma_query_results(
6464
if meta.get("path"):
6565
chunk.path = str(meta["path"])
6666
chroma_results_list.append(
67-
vectorcode_query_types.QueryResult(
67+
types.QueryResult(
6868
chunk=chunk,
6969
path=str(meta.get("path", "")),
7070
query=(q,),
@@ -391,11 +391,12 @@ async def list_collections(self):
391391
result: list[CollectionInfo] = []
392392
for col_name in await client.list_collections():
393393
col = await client.get_collection(col_name)
394-
col_counts = await self.list(col_name)
394+
project_root = str(col.metadata.get("path"))
395+
col_counts = await self.list(project_root)
395396
result.append(
396397
CollectionInfo(
397398
id=col_name,
398-
path=str(col.metadata.get("path")),
399+
path=project_root,
399400
embedding_function=col.metadata.get(
400401
"embedding_function",
401402
Config().embedding_function, # fallback to default
@@ -413,9 +414,7 @@ async def list(self, collection_path, what=None) -> CollectionContent:
413414
Otherwise, this method may populate only one of them to save waiting time.
414415
"""
415416
content = CollectionContent()
416-
collection = await self._create_or_get_collection(
417-
get_collection_id(collection_path)
418-
)
417+
collection = await self._create_or_get_collection((collection_path))
419418
raw_content = await collection.get(
420419
include=[
421420
IncludeEnum.metadatas,

src/vectorcode/database/types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ class CollectionInfo:
6767
chunk_count: int = 0
6868
metadata: dict[str, Any] = field(default_factory=dict)
6969

70+
def to_dict(self) -> dict[str, int | str]:
71+
return {
72+
"project-root": self.path,
73+
"size": self.chunk_count,
74+
"num_files": self.file_count,
75+
"collection_name": self.id,
76+
"embedding_function": self.embedding_function,
77+
}
78+
7079

7180
@dataclass
7281
class FileInCollection:

src/vectorcode/subcommands/ls.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from chromadb.api.types import IncludeEnum
99

1010
from vectorcode.cli_utils import Config, cleanup_path
11-
from vectorcode.common import ClientManager, get_collections
11+
from vectorcode.common import get_collections
12+
from vectorcode.database import get_database_connector
1213

1314
logger = logging.getLogger(name=__name__)
1415

@@ -36,34 +37,34 @@ async def get_collection_list(client: AsyncClientAPI) -> list[dict]:
3637

3738

3839
async def ls(configs: Config) -> int:
39-
async with ClientManager().get_client(configs) as client:
40-
result: list[dict] = await get_collection_list(client)
41-
logger.info(f"Found the following collections: {result}")
40+
result = [
41+
i.to_dict() for i in await get_database_connector(configs).list_collections()
42+
]
4243

43-
if configs.pipe:
44-
print(json.dumps(result))
45-
else:
46-
table = []
47-
for meta in result:
48-
project_root = meta["project-root"]
49-
if os.environ.get("HOME"):
50-
project_root = project_root.replace(os.environ["HOME"], "~")
51-
row = [
52-
project_root,
53-
meta["size"],
54-
meta["num_files"],
55-
meta["embedding_function"],
56-
]
57-
table.append(row)
58-
print(
59-
tabulate.tabulate(
60-
table,
61-
headers=[
62-
"Project Root",
63-
"Collection Size",
64-
"Number of Files",
65-
"Embedding Function",
66-
],
67-
)
44+
if configs.pipe:
45+
print(json.dumps(result))
46+
else:
47+
table = []
48+
for meta in result:
49+
project_root = str(meta["project-root"])
50+
if os.environ.get("HOME"):
51+
project_root = project_root.replace(os.environ["HOME"], "~")
52+
row = [
53+
project_root,
54+
meta["size"],
55+
meta["num_files"],
56+
meta["embedding_function"],
57+
]
58+
table.append(row)
59+
print(
60+
tabulate.tabulate(
61+
table,
62+
headers=[
63+
"Project Root",
64+
"Number of Embeddings",
65+
"Number of Files",
66+
"Embedding Function",
67+
],
6868
)
69-
return 0
69+
)
70+
return 0

0 commit comments

Comments
 (0)