Skip to content

Commit f3b0cd2

Browse files
authored
Merge pull request #856 from smallcloudai/agents_md
feat: read agents.md always
2 parents 0ec70dd + f787fab commit f3b0cd2

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

refact-agent/engine/src/scratchpads/chat_utils_prompts.rs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::sync::Arc;
44
use std::path::PathBuf;
55
use tokio::sync::RwLock as ARwLock;
66

7-
use crate::call_validation;
7+
use crate::call_validation::{self, ContextFile};
8+
use crate::files_correction::get_active_project_path;
89
use crate::global_context::GlobalContext;
910
use crate::http::http_post_json;
1011
use crate::http::routers::v1::system_prompt::{PrependSystemPromptPost, PrependSystemPromptResponse};
@@ -283,10 +284,79 @@ pub async fn prepend_the_right_system_prompt_and_maybe_more_initial_messages(
283284
).await;
284285
},
285286
}
287+
288+
match get_agents_md(&gcx).await {
289+
Ok(agent_md) => {
290+
const IMPORTANTER_ROLES: [&str; 2] = ["system", "developer"];
291+
stream_back_to_user.push_in_json(serde_json::json!(agent_md));
292+
let pos_to_insert = if messages.is_empty() || !IMPORTANTER_ROLES.contains(&messages[0].role.as_str()) {
293+
0
294+
} else { 1 };
295+
messages.insert(pos_to_insert, agent_md);
296+
},
297+
Err(e) => {
298+
tracing::warn!("Failed to read AGENTS.md: {}", e);
299+
},
300+
}
301+
286302
tracing::info!("\n\nSYSTEM PROMPT MIXER chat_mode={:?}\n{:#?}", chat_meta.chat_mode, messages);
287303
messages
288304
}
289305

306+
/// Reads AGENTS.md file from the active repository or nothing if it does not exist.
307+
async fn get_agents_md(gcx: &Arc<ARwLock<GlobalContext>>) -> Result<ChatMessage, String> {
308+
let act_proj_path = get_active_project_path(gcx.clone()).await
309+
.ok_or("No active project path found")?;
310+
311+
let found_agents_md = {
312+
let mut found_agents_md = vec![];
313+
314+
for parent in act_proj_path.ancestors() {
315+
let mut files_in_dir = tokio::fs::read_dir(parent)
316+
.await
317+
.map_err(|e| format!("Failed to read directory {}: {}", act_proj_path.display(), e))?;
318+
319+
while let Ok(Some(entry)) = files_in_dir.next_entry().await {
320+
let file_name = entry.file_name().to_string_lossy().to_string();
321+
if file_name.to_lowercase() == "agents.md" {
322+
found_agents_md.push((parent, file_name));
323+
}
324+
}
325+
}
326+
327+
if found_agents_md.is_empty() {
328+
return Err("AGENTS.md not found in the active project directory".to_string());
329+
}
330+
331+
found_agents_md
332+
};
333+
334+
let mut context_files = vec![];
335+
336+
for (path, file_name) in found_agents_md {
337+
let content = tokio::fs::read_to_string(&path.join(&file_name))
338+
.await
339+
.map_err(|e| format!("Failed to read AGENTS.md: {}", e))?;
340+
341+
let context_file = ContextFile {
342+
file_name,
343+
file_content: content.clone(),
344+
line1: 0,
345+
line2: content.lines().count(),
346+
symbols: vec![],
347+
gradient_type: 0,
348+
usefulness: 100.0,
349+
};
350+
351+
context_files.push(context_file);
352+
}
353+
354+
let context_files = serde_json::to_string(&context_files)
355+
.map_err(|e| format!("Failed to serialize context file: {}", e))?;
356+
357+
Ok(ChatMessage::new("context_file".to_string(), context_files))
358+
}
359+
290360
pub async fn prepend_system_prompt_and_maybe_more_initial_messages_from_remote(
291361
gcx: Arc<ARwLock<GlobalContext>>,
292362
messages: &[call_validation::ChatMessage],

0 commit comments

Comments
 (0)