Skip to content

Commit

Permalink
Update version of rusty-buddy and improve directory handling
Browse files Browse the repository at this point in the history
Motivate the version change from "1.1.1" to "1.1.2" for the `rusty-buddy` package to incorporate new features or fixes. Replace `WalkDir` with `WalkBuilder` to allow applying standard `.gitignore` rules when listing directories, enhancing performance and usability. Add a function `truncate_to_max_bytes` to ensure that directory listings do not exceed a specified byte limit, preventing potential overflows in processing and improving system stability. Overall, these changes improve the codebase's functionality and maintainability.
  • Loading branch information
Christian Stolz committed Oct 14, 2024
1 parent c3bd44c commit 65c5672
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rusty-buddy"
version = "1.1.1"
version = "1.1.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
25 changes: 21 additions & 4 deletions src/cli/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ use crate::persona::{get_internal_persona_configs, Persona};
use crate::provider::ollama::ollama_interface::OllamaInterface;
use crate::provider::openai::openai_interface::OpenAIInterface;
use dotenvy::dotenv;
use ignore::WalkBuilder;
use log::warn;
use std::error::Error;
use std::io::Write;
use std::{env, fs};
use walkdir::WalkDir;

/// This function represents the entry point of the init command.
/// It initializes the configuration based on user choice of AI backend (OpenAI or Ollama),
Expand Down Expand Up @@ -114,6 +115,19 @@ pub async fn run_init_command() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn truncate_to_max_bytes(s: &str, max_bytes: usize) -> &str {
if s.len() <= max_bytes {
s
} else {
warn!("Truncating to {} bytes.", max_bytes);
let mut end = max_bytes;
while !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}
}

// Function to recommend a persona
async fn recommend_persona(
dir_listing: String,
Expand All @@ -128,10 +142,10 @@ async fn recommend_persona(
};

let mut chat_service = ChatService::new(backend, Box::new(storage), persona.clone(), None);

let trunced_dir_listing = truncate_to_max_bytes(&dir_listing, 500_000);
let prompt = format!(
"Analyze the following directory structure:\n{}\n\nChoose the most suitable persona from this list: {:?}. Just answer with one value from that list. No explanation needed.",
dir_listing, personas
trunced_dir_listing, personas
);

let response = chat_service
Expand Down Expand Up @@ -174,7 +188,10 @@ fn write_openai_key_to_env_file(openai_key: &str) -> Result<(), Box<dyn Error>>

// Function to list directory
fn get_directory_listing(path: &str) -> String {
WalkDir::new(path)
let walker = WalkBuilder::new(path)
.standard_filters(true) // Apply standard .gitignore rules
.build();
walker
.into_iter()
.filter_map(Result::ok)
.map(|entry| entry.path().display().to_string())
Expand Down

0 comments on commit 65c5672

Please sign in to comment.