From fc0152c654165519da8d5497deedb33ecc317357 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sun, 19 Nov 2023 09:38:55 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20wip,=20fixing=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/continuedev/continuedev.pyi | 9 ++++- .../libs/index/chunkers/chunk_directory.py | 34 ++++++++++++++----- server/src/lib.rs | 7 +++- server/src/sync/mod.rs | 13 ++++--- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/server/continuedev/continuedev.pyi b/server/continuedev/continuedev.pyi index 18fb9beac..e26793021 100644 --- a/server/continuedev/continuedev.pyi +++ b/server/continuedev/continuedev.pyi @@ -2,4 +2,11 @@ from typing import List, Tuple def sync_results( dir: str, branch: str -) -> Tuple[List[Tuple[str, str]], List[Tuple[str, str]]]: ... +) -> Tuple[ + List[Tuple[str, str]], + List[Tuple[str, str]], + List[Tuple[str, str]], + List[Tuple[str, str]], +]: + """Returns lists of files that need (compute, delete, add label, remove label) with each item being a (path, hash) tuple""" + ... diff --git a/server/continuedev/libs/index/chunkers/chunk_directory.py b/server/continuedev/libs/index/chunkers/chunk_directory.py index 9db8bdf98..c3306f961 100644 --- a/server/continuedev/libs/index/chunkers/chunk_directory.py +++ b/server/continuedev/libs/index/chunkers/chunk_directory.py @@ -2,15 +2,13 @@ import asyncio import os from typing import AsyncGenerator, Callable, Generator, List, Optional, Tuple -from .ignore import should_ignore_file_factory - -from ....server.protocols.ide_protocol import AbstractIdeProtocolServer - -from .chunk import Chunk -from . import chunk_document -from .fast_index import stream_files_to_update from continuedev.continuedev import sync_results +from ....server.protocols.ide_protocol import AbstractIdeProtocolServer +from . import chunk_document +from .chunk import Chunk +from .fast_index import stream_files_to_update +from .ignore import should_ignore_file_factory MAX_SIZE_IN_CHARS = 50_000 @@ -85,9 +83,27 @@ async def stream_chunk_directory( def local_stream_chunk_directory( - workspace_dir: str, - max_chunk_size: int, + workspace_dir: str, max_chunk_size: int, branch: str ) -> Generator[Tuple[Optional[Chunk], float], None, None]: + (compute, delete, add_label, remove_label) = sync_results(workspace_dir, branch) + + for filepath, digest in compute: + # Ignore if the file is too large (cutoff is 10MB) + if os.path.getsize(filepath) > 10_000_000: + continue + + try: + contents = open(filepath, "r").read() + except Exception as e: + print(e, filepath) + continue + + if contents.strip() == "": + continue + + for chunk in chunk_document(filepath, contents, max_chunk_size, digest): + yield (chunk, 0.0) + for (add_filepath, add_digest), (del_filepath, del_digest) in sync_results( workspace_dir, branch ): diff --git a/server/src/lib.rs b/server/src/lib.rs index 7c25def44..fd1697f82 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -7,7 +7,12 @@ mod sync; fn sync_results( dir: String, branch: String, -) -> PyResult<(Vec<(String, String)>, Vec<(String, String)>)> { +) -> PyResult<( + Vec<(String, String)>, + Vec<(String, String)>, + Vec<(String, String)>, + Vec<(String, String)>, +)> { return sync::sync(Path::new(&dir), Some(&branch)) .map_err(|err| PyErr::new::(format!("{}", err))); } diff --git a/server/src/sync/mod.rs b/server/src/sync/mod.rs index f6a6e219f..eb3dd0206 100644 --- a/server/src/sync/mod.rs +++ b/server/src/sync/mod.rs @@ -1,11 +1,10 @@ mod merkle; use homedir::get_my_home; -use ignore; use merkle::{build_walk, compute_tree_for_dir, diff, hash_string, ObjectHash}; use std::{ collections::HashMap, fs::{File, OpenOptions}, - io::{BufRead, BufReader, Read, Seek, SeekFrom, Write}, + io::{Read, Seek, SeekFrom, Write}, path::{Path, PathBuf}, time::{SystemTime, UNIX_EPOCH}, }; @@ -212,7 +211,7 @@ impl IndexCache { // TODO: You could add_bulk, remove_bulk if this gets slow fn read_rev_tags(&self, hash: [u8; ITEM_SIZE]) -> HashMap> { - let mut rev_tags_path = IndexCache::rev_tags_path(hash); + let rev_tags_path = IndexCache::rev_tags_path(hash); let mut rev_tags_file = OpenOptions::new() .read(true) .write(true) @@ -221,14 +220,14 @@ impl IndexCache { .unwrap(); let mut contents = String::new(); rev_tags_file.read_to_string(&mut contents).unwrap(); - let mut rev_tags: HashMap> = + let rev_tags: HashMap> = serde_json::from_str(&contents).unwrap_or(HashMap::new()); return rev_tags; } fn write_rev_tags(&self, hash: [u8; ITEM_SIZE], rev_tags: HashMap>) { - let mut rev_tags_path = IndexCache::rev_tags_path(hash); + let rev_tags_path = IndexCache::rev_tags_path(hash); let mut rev_tags_file = OpenOptions::new() .read(true) .write(true) @@ -253,7 +252,7 @@ impl IndexCache { let tag_str = self.tag_str(); let hash_str = hash_string(item.hash); if !rev_tags.contains_key(hash_str.as_str()) { - rev_tags.insert(hash_str, Vec::new()); + rev_tags.insert(hash_str.clone(), Vec::new()); } rev_tags.get_mut(hash_str.as_str()).unwrap().push(tag_str); self.write_rev_tags(item.hash, rev_tags); @@ -281,7 +280,7 @@ impl IndexCache { let tag_str = self.tag_str(); let hash_str = hash_string(item.hash); if rev_tags.contains_key(hash_str.as_str()) { - let mut tags = rev_tags.get_mut(hash_str.as_str()).unwrap(); + let tags = rev_tags.get_mut(hash_str.as_str()).unwrap(); let index = tags.iter().position(|x| *x == tag_str).unwrap(); tags.remove(index); if tags.len() == 0 {