diff --git a/mindflow/cli/commands/index.py b/mindflow/cli/commands/index.py index b9286f4..47b9fcb 100644 --- a/mindflow/cli/commands/index.py +++ b/mindflow/cli/commands/index.py @@ -10,4 +10,4 @@ @click.argument("document_paths", type=str, nargs=-1, required=True) @click.option("--refresh", is_flag=True, default=False) def index(document_paths: List[str], refresh: bool) -> None: - run_index(document_paths, refresh) + print(run_index(document_paths, refresh)) diff --git a/mindflow/core/commands/chat.py b/mindflow/core/commands/chat.py index 98006e7..55f315f 100644 --- a/mindflow/core/commands/chat.py +++ b/mindflow/core/commands/chat.py @@ -17,7 +17,7 @@ ) -def run_chat(document_paths: List[str], user_query: str): +def run_chat(document_paths: List[str], user_query: str) -> str: settings = Settings() completion_model = settings.mindflow_models.query.model diff --git a/mindflow/core/commands/delete.py b/mindflow/core/commands/delete.py index cc28a7e..4228309 100644 --- a/mindflow/core/commands/delete.py +++ b/mindflow/core/commands/delete.py @@ -10,27 +10,24 @@ from mindflow.core.resolving.resolve import resolve_paths_to_document_references -def run_delete(document_paths: List[str]): +def run_delete(document_paths: List[str]) -> str: """Delete documents from MindFlow index.""" - document_references: List[DocumentReference] = resolve_paths_to_document_references( - document_paths - ) - document_ids = [ - document_id - for document_id in [ - get_document_id(document_reference.path, document_reference.document_type) - for document_reference in document_references - ] - if document_id is not None - ] - if not (documents := Document.load_bulk_ignore_missing(document_ids)): - return "No documents to delete" + document_references: List[DocumentReference] = resolve_paths_to_document_references(document_paths) + + document_ids = [document_id for document_id in [get_document_id(document_reference.path, document_reference.document_type) for document_reference in document_references] if document_id is not None] + + if not document_ids: + return "No document IDs resolved. Nothing to delete." + + documents = Document.load_bulk_ignore_missing(document_ids) + if not documents: + return "No documents found to delete." document_chunk_ids = get_document_chunk_ids(documents) if not DocumentChunk.load_bulk_ignore_missing(document_chunk_ids): - return "No documents to delete" + return "No document chunks found to delete." Document.delete_bulk(document_ids) DocumentChunk.delete_bulk(document_chunk_ids) - return "Documents deleted" + return True, "Documents and associated chunks deleted successfully." diff --git a/mindflow/core/commands/gen.py b/mindflow/core/commands/gen.py index 223496e..327df52 100644 --- a/mindflow/core/commands/gen.py +++ b/mindflow/core/commands/gen.py @@ -16,7 +16,7 @@ from mindflow.core.token_counting import get_token_count_of_messages_for_model -def run_code_generation(output_path: str, prompt: str): +def run_code_generation(output_path: str, prompt: str) -> str: settings = Settings() completion_model = settings.mindflow_models.query.model diff --git a/mindflow/core/commands/git/commit.py b/mindflow/core/commands/git/commit.py index 53f47e4..19a36c6 100644 --- a/mindflow/core/commands/git/commit.py +++ b/mindflow/core/commands/git/commit.py @@ -45,4 +45,6 @@ def run_commit( + list(args) ) - return execute_command_and_print_without_trace(["git", "commit", "-m"] + [message_overwrite] + list(args)) + return execute_command_and_print_without_trace( + ["git", "commit", "-m"] + [message_overwrite] + list(args) + ) diff --git a/mindflow/core/commands/git/diff.py b/mindflow/core/commands/git/diff.py index 6c510ec..26714a4 100644 --- a/mindflow/core/commands/git/diff.py +++ b/mindflow/core/commands/git/diff.py @@ -26,7 +26,9 @@ def run_diff(args: Tuple[str], detailed: bool = True) -> Optional[str]: settings = Settings() completion_model: ConfiguredModel = settings.mindflow_models.query.model - diff_result = execute_command_and_print_without_trace(["git", "diff"] + list(args)).strip() + diff_result = execute_command_and_print_without_trace( + ["git", "diff"] + list(args) + ).strip() if not diff_result: return None diff --git a/mindflow/core/commands/index.py b/mindflow/core/commands/index.py index 2df9d57..7ffe063 100644 --- a/mindflow/core/commands/index.py +++ b/mindflow/core/commands/index.py @@ -26,7 +26,7 @@ from mindflow.core.token_counting import get_token_count_of_text_for_model -def run_index(document_paths: List[str], verbose: bool = True) -> None: +def run_index(document_paths: List[str]) -> str: settings = Settings() completion_model: ConfiguredModel = settings.mindflow_models.index.model embedding_model: ConfiguredModel = settings.mindflow_models.embedding.model @@ -40,15 +40,15 @@ def run_index(document_paths: List[str], verbose: bool = True) -> None: document_references, completion_model ) ): - if verbose: - print("No documents to index") - return + return "No documents to index" print_total_size_of_documents(indexable_documents) print_total_tokens_and_ask_to_continue(indexable_documents, completion_model) index_documents(indexable_documents, completion_model, embedding_model) + return "Successfully indexed documents" + def print_total_size_of_documents(documents: List[Document]): print( @@ -155,14 +155,14 @@ def get_indexable_document( return None document_text_bytes = document_text.encode("utf-8") - doc_hash = hashlib.sha256(document_text_bytes).hexdigest() + document_hash = hashlib.sha256(document_text_bytes).hexdigest() - if document and document.id == doc_hash: + if document and document.id == document_hash: return None return Document( { - "id": doc_hash, + "id": document_hash, "path": document_reference.path, "document_type": document_reference.document_type, "num_chunks": document.num_chunks if document else 0, diff --git a/mindflow/core/commands/query.py b/mindflow/core/commands/query.py index e5440aa..63b38da 100644 --- a/mindflow/core/commands/query.py +++ b/mindflow/core/commands/query.py @@ -24,7 +24,7 @@ from mindflow.core.token_counting import get_token_count_of_text_for_model -def run_query(document_paths: List[str], query: str): +def run_query(document_paths: List[str], query: str) -> str: """Query files, folders, and websites.""" settings = Settings() completion_model = settings.mindflow_models.query.model @@ -52,10 +52,7 @@ def run_query(document_paths: List[str], query: str): top_k=100, ) ): - print( - "No index for requested hashes. Please generate index for passed content." - ) - sys.exit(1) + return "No index for requested hashes. Please generate index for passed content." document_selection_batch: List[Tuple[str, DocumentChunk]] = [ (document_hash_to_path[document_chunk.id.split("_")[0]], document_chunk) diff --git a/mindflow/core/file_processing/git.py b/mindflow/core/file_processing/git.py index a7efcf3..8f28496 100644 --- a/mindflow/core/file_processing/git.py +++ b/mindflow/core/file_processing/git.py @@ -5,6 +5,7 @@ from typing import List from typing import Union + def is_path_within_git_repo(path: Union[str, os.PathLike]) -> bool: try: output = subprocess.run(