pub mod section;
pub mod cache;
pub mod context;
pub mod orchestrator;
pub mod preprocess;
pub mod dump;
pub mod constants;
pub use section::{PromptSection, SectionContent, SectionBuilder};
pub use cache::{SectionCache, CacheKey, CachedEntry, CacheStats, global_cache, clear_global_cache, estimate_tokens};
pub use context::{ContextInjector, UserContext, SystemContext, ProjectType};
pub use orchestrator::{PromptOrchestrator, PromptProfile, PromptBuilder, AssembledPrompt, CACHE_BOUNDARY};
pub use preprocess::{PreProcessHook, ProcessResult, SkillPattern, WorkflowTrigger, preprocess};
pub use dump::{PromptDumper, DumpEntry, PromptAnalysis, DumpFileAnalysis, read_dump_file, analyze_dump_file};
pub use constants::*;
pub use orchestrator::PromptProfile as LegacyPromptProfile;
#[derive(Debug, Clone)]
pub struct OverviewContext {
pub project_name: String,
pub project_type: String,
pub directory_structure: String,
pub config_files: Vec<(String, String)>, pub readme: Option<String>,
pub source_files: Vec<(String, String)>, }
impl Default for OverviewContext {
fn default() -> Self {
Self {
project_name: String::new(),
project_type: String::new(),
directory_structure: String::new(),
config_files: Vec::new(),
readme: None,
source_files: Vec::new(),
}
}
}
pub fn build_overview_prompt(context: &OverviewContext) -> String {
let mut prompt = String::new();
prompt.push_str("你是一个项目分析专家,请根据提供的项目信息生成项目概览文档(MATRIX.md)。\n\n");
prompt.push_str("要求:\n");
prompt.push_str("- 使用 Markdown 格式\n");
prompt.push_str("- 简洁明了,突出重点\n");
prompt.push_str("- 包含项目定位、技术栈、架构要点\n");
prompt.push_str("- 便于 AI 助手快速理解项目\n");
prompt.push_str("\n---\n\n");
prompt.push_str(&format!("项目名称: {}\n", context.project_name));
prompt.push_str(&format!("项目类型: {}\n\n", context.project_type));
prompt.push_str("## 目录结构\n\n");
prompt.push_str("```\n");
prompt.push_str(&context.directory_structure);
prompt.push_str("```\n\n");
if !context.config_files.is_empty() {
prompt.push_str("## 配置文件\n\n");
for (filename, content) in &context.config_files {
prompt.push_str(&format!("### {}\n\n", filename));
prompt.push_str("```\n");
prompt.push_str(content);
prompt.push_str("\n```\n\n");
}
}
if let Some(readme) = &context.readme {
prompt.push_str("## README.md (开头部分)\n\n");
prompt.push_str(readme);
prompt.push_str("\n\n");
}
if !context.source_files.is_empty() {
prompt.push_str("## 关键源文件\n\n");
for (filename, content) in &context.source_files {
prompt.push_str(&format!("### {}\n\n", filename));
prompt.push_str("```\n");
prompt.push_str(content);
prompt.push_str("\n```\n\n");
}
}
prompt.push_str("---\n\n");
prompt.push_str("请根据以上信息生成 MATRIX.md 文档内容:\n");
prompt
}
use crate::skills::Skill;
use std::path::PathBuf;
fn join_lines(items: &[String]) -> String {
items.join("\n")
}
pub fn build_system_prompt(
profile: &PromptProfile,
skills: &[Skill],
project_overview: Option<&str>,
memory_summary: Option<&str>,
) -> String {
build_system_prompt_with_workflows(profile, skills, project_overview, memory_summary, None, None)
}
pub fn build_system_prompt_with_workflows(
profile: &PromptProfile,
skills: &[Skill],
project_overview: Option<&str>,
memory_summary: Option<&str>,
project_path: Option<&PathBuf>,
lsp_servers: Option<&[crate::lsp::LspServerInfo]>,
) -> String {
let has_codegraph = project_path
.map(|p| crate::tools::codegraph::should_inject_codegraph_tools(p))
.unwrap_or(false);
let mut builder = PromptBuilder::new(
project_path.cloned().unwrap_or_else(|| std::env::current_dir().unwrap())
)
.profile(*profile)
.no_context();
for (name, content) in constants::get_static_sections(has_codegraph) {
builder = builder.add_static(name, content);
}
for (name, content) in constants::get_memory_sections() {
builder = builder.add_static(name, content);
}
let mut orchestrator = builder.build();
let assembled = orchestrator.assemble();
let mut parts = vec![assembled.prompt];
let tools_prompt = crate::tools::generate_tools_prompt_with_path(project_path);
parts.push(tools_prompt);
if !skills.is_empty() {
let skills_lines: Vec<String> = skills.iter()
.map(|s| {
if let Some(trigger) = &s.trigger {
format!(" - {}: {}\n 触发场景: {}", s.name, s.description, trigger)
} else {
format!(" - {}: {}", s.name, s.description)
}
})
.collect();
let skills_section = format!(
"[AVAILABLE SKILLS]\n可用技能(使用 skill 工具调用):\n\n{}",
join_lines(&skills_lines)
);
parts.push(skills_section);
}
if let Some(path) = project_path {
let registry = crate::workflow::WorkflowRegistry::new(Some(path));
if !registry.is_empty() {
let workflows_lines: Vec<String> = registry.list().iter()
.map(|w| {
let desc = w.description.as_deref().unwrap_or(&w.name);
if w.required_inputs.is_empty() {
format!(" - {}: {}", w.id, desc)
} else {
format!(" - {}: {} (需要输入: {})", w.id, desc, w.required_inputs.join(", "))
}
})
.collect();
let workflows_section = format!(
"[AVAILABLE WORKFLOWS]\n可执行的自动化流程(使用 workflow_run 工具调用):\n\n{}",
join_lines(&workflows_lines)
);
parts.push(workflows_section);
}
}
if let Some(overview) = project_overview {
parts.push(format!("[PROJECT CONTEXT]\n{}", overview));
}
if let Some(memory) = memory_summary {
parts.push(format!(
"{}\n{}\n\n{}",
MEMORY_SUMMARY_HEADER,
MEMORY_USAGE_INSTRUCTIONS,
memory
));
}
if let Some(servers) = lsp_servers {
if let Some(lsp_section) = crate::prompt::constants::get_lsp_section(servers) {
parts.push(lsp_section);
}
}
parts.join("\n\n")
}