feat: 添加生成系统环境信息提示词功能,并在会话历史中注入该提示词
This commit is contained in:
parent
181740559b
commit
0ea98c6e8e
@ -1,7 +1,9 @@
|
|||||||
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use crate::agent::AgentError;
|
use crate::agent::AgentError;
|
||||||
|
use crate::config::LLMProviderConfig;
|
||||||
use crate::platform::atomic_rename;
|
use crate::platform::atomic_rename;
|
||||||
|
|
||||||
pub(crate) const DEFAULT_AGENT_PROMPT: &str = include_str!("default_agent_prompt.md");
|
pub(crate) const DEFAULT_AGENT_PROMPT: &str = include_str!("default_agent_prompt.md");
|
||||||
@ -100,6 +102,29 @@ fn strip_comments_and_whitespace(content: &str) -> String {
|
|||||||
.join("\n")
|
.join("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 生成系统环境信息提示词
|
||||||
|
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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fn persist_memory_summary(path: &Path, markdown_body: &str) -> Result<(), AgentError> {
|
fn persist_memory_summary(path: &Path, markdown_body: &str) -> Result<(), AgentError> {
|
||||||
let trimmed = markdown_body.trim();
|
let trimmed = markdown_body.trim();
|
||||||
if trimmed.is_empty() {
|
if trimmed.is_empty() {
|
||||||
|
|||||||
@ -141,6 +141,7 @@ impl Session {
|
|||||||
prompt_injector,
|
prompt_injector,
|
||||||
conversations,
|
conversations,
|
||||||
skill_events,
|
skill_events,
|
||||||
|
provider_config,
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,12 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use crate::agent::AgentError;
|
use crate::agent::AgentError;
|
||||||
use crate::bus::ChatMessage;
|
use crate::bus::ChatMessage;
|
||||||
|
use crate::config::LLMProviderConfig;
|
||||||
use crate::storage::{
|
use crate::storage::{
|
||||||
ConversationRepository, SessionRecord, SkillEventRepository, persistent_session_id,
|
ConversationRepository, SessionRecord, SkillEventRepository, persistent_session_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::prompt::generate_system_environment_prompt;
|
||||||
use super::prompt_injector::PromptInjector;
|
use super::prompt_injector::PromptInjector;
|
||||||
|
|
||||||
fn preview_text(content: &str, max_chars: usize) -> String {
|
fn preview_text(content: &str, max_chars: usize) -> String {
|
||||||
@ -24,6 +26,7 @@ pub(crate) struct SessionHistory {
|
|||||||
prompt_injector: PromptInjector,
|
prompt_injector: PromptInjector,
|
||||||
conversations: Arc<dyn ConversationRepository>,
|
conversations: Arc<dyn ConversationRepository>,
|
||||||
skill_events: Arc<dyn SkillEventRepository>,
|
skill_events: Arc<dyn SkillEventRepository>,
|
||||||
|
provider_config: LLMProviderConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SessionHistory {
|
impl SessionHistory {
|
||||||
@ -32,6 +35,7 @@ impl SessionHistory {
|
|||||||
prompt_injector: PromptInjector,
|
prompt_injector: PromptInjector,
|
||||||
conversations: Arc<dyn ConversationRepository>,
|
conversations: Arc<dyn ConversationRepository>,
|
||||||
skill_events: Arc<dyn SkillEventRepository>,
|
skill_events: Arc<dyn SkillEventRepository>,
|
||||||
|
provider_config: LLMProviderConfig,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
channel_name: channel_name.into(),
|
channel_name: channel_name.into(),
|
||||||
@ -40,6 +44,7 @@ impl SessionHistory {
|
|||||||
prompt_injector,
|
prompt_injector,
|
||||||
conversations,
|
conversations,
|
||||||
skill_events,
|
skill_events,
|
||||||
|
provider_config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,9 +264,19 @@ impl SessionHistory {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 注入 Agent Prompt
|
||||||
let prompt_injector = self.prompt_injector.clone();
|
let prompt_injector = self.prompt_injector.clone();
|
||||||
prompt_injector.ensure_initial_prompt(history_is_empty, |message| {
|
prompt_injector.ensure_initial_prompt(history_is_empty, |message| {
|
||||||
self.append_persisted_message(chat_id, message)
|
self.append_persisted_message(chat_id, message)
|
||||||
})
|
})?;
|
||||||
|
|
||||||
|
// 注入系统环境提示词
|
||||||
|
let env_prompt = generate_system_environment_prompt(&self.provider_config);
|
||||||
|
self.append_persisted_message(
|
||||||
|
chat_id,
|
||||||
|
ChatMessage::system(env_prompt),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user