From 1e69fa3bd1839111d41e0464d979556f68c3fb76 Mon Sep 17 00:00:00 2001 From: xiaoxixi Date: Mon, 11 May 2026 18:01:25 +0800 Subject: [PATCH] feat: enhance memory section guide and improve search tool descriptions for clarity --- src/agent/system_prompt.rs | 23 ++++++++++++++++++++--- src/tools/content_search.rs | 2 +- src/tools/file_search.rs | 19 ++++++++++++++----- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/agent/system_prompt.rs b/src/agent/system_prompt.rs index 9eca740..22d35de 100644 --- a/src/agent/system_prompt.rs +++ b/src/agent/system_prompt.rs @@ -300,7 +300,7 @@ impl PromptSection for RuntimeSection { } } -/// Injects relevant knowledge memories into the system prompt. +/// Injects memory system guide and relevant knowledge memories into the system prompt. pub struct MemorySection; impl PromptSection for MemorySection { @@ -309,11 +309,28 @@ impl PromptSection for MemorySection { } fn build(&self, ctx: &PromptContext<'_>) -> String { + let guide = r#"## 记忆系统 + +### 记忆类别 +- **Knowledge(知识)**:长期存储的事实、偏好、模式、洞察。会被注入到每轮系统提示词中。 +- **Timeline(时间线)**:历史会话摘要,可通过 timeline_recall 工具主动召回。 + +### 记忆工具 +- **memory_recall**:搜索知识记忆。参数 query 是关键词列表(空格分隔),返回相关事实、偏好、洞察。 +- **timeline_recall**:搜索历史会话摘要。可选 session_id 参数限定特定会话。 + +### 主动记忆 +遇到以下情况时应主动使用 `memory_store` 记忆: +- 用户明确表达的偏好(如编程语言、工具选择) +- 重要的项目事实(如使用的框架、架构决策) +- 值得记录的经验和教训 +"#; + match ctx.memory_context { Some(context) if !context.is_empty() => { - format!("## 记忆上下文\n\n{}", context) + format!("{}\n\n### 记忆上下文\n\n{}", guide, context) } - _ => String::new(), + _ => guide.to_string(), } } } diff --git a/src/tools/content_search.rs b/src/tools/content_search.rs index 8cabd81..96823aa 100644 --- a/src/tools/content_search.rs +++ b/src/tools/content_search.rs @@ -59,7 +59,7 @@ impl Tool for ContentSearchTool { } fn description(&self) -> &str { - "Search file contents by regex or text pattern. Uses ripgrep (rg) if available, falls back to grep, then pure Rust." + "Search file contents by regex or text pattern. Internally uses ripgrep (rg) for fast searching — if rg is not available, falls back to grep, then pure Rust. Supports context lines, file filtering, and case-sensitivity options." } fn parameters_schema(&self) -> serde_json::Value { diff --git a/src/tools/file_search.rs b/src/tools/file_search.rs index 93f5bf7..0399a6d 100644 --- a/src/tools/file_search.rs +++ b/src/tools/file_search.rs @@ -56,7 +56,7 @@ impl Tool for FileSearchTool { } fn description(&self) -> &str { - "Search for files by glob pattern (e.g. '*.rs', 'test_*.rs'). Uses fd if available, falls back to find, then pure Rust." + "Search for files by glob pattern (e.g. '*.rs', 'test_*.rs'). Internally uses fd (fast find) for efficient searching — if fd is not available, falls back to the find command, then pure Rust glob matching." } fn parameters_schema(&self) -> serde_json::Value { @@ -130,11 +130,19 @@ impl FileSearchTool { case_sensitive: bool, max_results: usize, ) -> anyhow::Result> { - if which::which("fd").is_ok() { - match self.search_with_fd(pattern, dir, case_sensitive, max_results).await { + let fd_cmd = if which::which("fd").is_ok() { + "fd" + } else if which::which("fdfind").is_ok() { + "fdfind" + } else { + "" + }; + + if !fd_cmd.is_empty() { + match self.search_with_fd(pattern, dir, case_sensitive, max_results, fd_cmd).await { Ok(lines) if !lines.is_empty() => return Ok(lines), Ok(_) => {}, - Err(e) => tracing::warn!("fd failed: {}, falling back", e), + Err(e) => tracing::warn!("{} failed: {}, falling back", fd_cmd, e), } } @@ -156,8 +164,9 @@ impl FileSearchTool { dir: &str, case_sensitive: bool, max_results: usize, + fd_cmd: &str, ) -> anyhow::Result> { - let mut cmd = Command::new("fd"); + let mut cmd = Command::new(fd_cmd); cmd.arg("--search-path").arg(dir) .arg("--glob").arg(pattern) .arg("--color").arg("never")