From b36449660146e931f3ec40dd6820a35ffef4fd95 Mon Sep 17 00:00:00 2001 From: oudecheng <13802883547@139.com> Date: Wed, 20 May 2026 15:13:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=94=9F=E6=88=90?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=8E=AF=E5=A2=83=E4=BF=A1=E6=81=AF=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E8=AF=8D=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=9C=A8=E5=AD=90=E4=BB=A3=E7=90=86=E6=8F=90=E7=A4=BA=E8=AF=8D?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E4=B8=AD=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agent/mod.rs | 3 ++- src/agent/system_prompt.rs | 25 +++++++++++++++++++++++++ src/gateway/prompt.rs | 22 ++-------------------- src/tools/task/prompt.rs | 10 +++++++--- src/tools/task/runtime.rs | 1 + 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/agent/mod.rs b/src/agent/mod.rs index 1150691..ec41da3 100644 --- a/src/agent/mod.rs +++ b/src/agent/mod.rs @@ -9,5 +9,6 @@ pub use agent_loop::{ pub use context_compressor::ContextCompressor; pub use runtime_config::AgentRuntimeConfig; pub use system_prompt::{ - CompositeSystemPromptProvider, SystemPrompt, SystemPromptContext, SystemPromptProvider, + CompositeSystemPromptProvider, generate_system_env_prompt, SystemPrompt, SystemPromptContext, + SystemPromptProvider, }; diff --git a/src/agent/system_prompt.rs b/src/agent/system_prompt.rs index 04ebd62..00f48f9 100644 --- a/src/agent/system_prompt.rs +++ b/src/agent/system_prompt.rs @@ -70,6 +70,31 @@ impl SystemPromptProvider for CompositeSystemPromptProvider { } } +/// 生成系统环境信息提示词 +/// 供主智能体和子智能体共享使用 +pub fn generate_system_env_prompt(config: &crate::config::LLMProviderConfig) -> String { + use std::env; + use std::env::consts::{ARCH, OS}; + + let os_name = match OS { + "windows" => "Windows", + "linux" => "Linux", + "macos" => "macOS", + "freebsd" => "FreeBSD", + _ => OS, + }; + + let shell = env::var("SHELL").unwrap_or_else(|_| "unknown".to_string()); + let cwd = env::current_dir() + .map(|p| p.display().to_string()) + .unwrap_or_else(|_| "unknown".to_string()); + + format!( + "## 系统环境\n- 操作系统: {}\n- 架构: {}\n- Shell: {}\n- 当前工作目录: {}\n- 模型提供商: {}\n- 模型: {}", + os_name, ARCH, shell, cwd, config.name, config.model_id + ) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/gateway/prompt.rs b/src/gateway/prompt.rs index 98bbdce..f68d7d7 100644 --- a/src/gateway/prompt.rs +++ b/src/gateway/prompt.rs @@ -1,4 +1,3 @@ -use std::env; use std::fs; use std::path::{Path, PathBuf}; @@ -104,26 +103,9 @@ fn strip_comments_and_whitespace(content: &str) -> String { } /// 生成系统环境信息提示词 +/// 使用 agent 模块的共享实现 pub(crate) fn generate_system_environment_prompt(config: &LLMProviderConfig) -> String { - use std::env::consts::{ARCH, OS}; - - let os_name = match OS { - "windows" => "Windows", - "linux" => "Linux", - "macos" => "macOS", - "freebsd" => "FreeBSD", - _ => OS, - }; - - let shell = env::var("SHELL").unwrap_or_else(|_| "unknown".to_string()); - let cwd = env::current_dir() - .map(|p| p.display().to_string()) - .unwrap_or_else(|_| "unknown".to_string()); - - format!( - "## 系统环境\n- 操作系统: {}\n- 架构: {}\n- Shell: {}\n- 当前工作目录: {}\n- 模型提供商: {}\n- 模型: {}", - os_name, ARCH, shell, cwd, config.name, config.model_id - ) + crate::agent::generate_system_env_prompt(config) } fn persist_memory_summary(path: &Path, markdown_body: &str) -> Result<(), AgentError> { diff --git a/src/tools/task/prompt.rs b/src/tools/task/prompt.rs index 8e73353..8547218 100644 --- a/src/tools/task/prompt.rs +++ b/src/tools/task/prompt.rs @@ -1,19 +1,23 @@ use super::types::SubagentType; +use crate::config::LLMProviderConfig; /// 子代理系统提示词构建器 pub struct SubagentPromptBuilder; impl SubagentPromptBuilder { - /// 构建子代理系统提示词 + /// 构建子代理系统提示词(包含系统环境信息) pub fn build( subagent_type: SubagentType, description: &str, _prompt: &str, + config: &LLMProviderConfig, ) -> String { - match subagent_type { + let base_prompt = match subagent_type { SubagentType::General => Self::build_general_prompt(description), SubagentType::Explore => Self::build_explore_prompt(description), - } + }; + let env_info = crate::agent::generate_system_env_prompt(config); + format!("{}\n\n{}", base_prompt, env_info) } /// 构建恢复任务的提示词 diff --git a/src/tools/task/runtime.rs b/src/tools/task/runtime.rs index 6c3db11..530efe1 100644 --- a/src/tools/task/runtime.rs +++ b/src/tools/task/runtime.rs @@ -281,6 +281,7 @@ impl SubAgentRuntime for DefaultSubAgentRuntime { task.subagent_type, &task.description, &task.prompt, + &self.provider_config, ); // 5. 创建子代理