use std::collections::HashMap;
use std::fmt::Write;
use std::path::Path;
use regex::Regex;
use std::sync::LazyLock;
#[derive(rust_embed::RustEmbed)]
#[folder = "src/prompt"]
struct PromptAssets;
pub(crate) static TEMPLATE_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?-u)\{\{(\w+)\}\}").expect("TEMPLATE_RE must compile"));
fn asset_text(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes).replace("\r\n", "\n")
}
#[must_use]
pub(crate) fn prompt_asset(asset_key: &str) -> Option<String> {
PromptAssets::get(asset_key).map(|file| asset_text(file.data.as_ref()))
}
#[must_use]
pub(crate) fn load_prompt(asset_key: &str) -> String {
prompt_asset(asset_key).unwrap_or_else(|| {
panic!(
"Embedded prompt '{asset_key}' not found. \
Create the file at src/prompt/{asset_key} and rebuild."
)
})
}
#[must_use]
pub(crate) fn media_delivery() -> String {
load_prompt("context/media_delivery.md").trim().to_owned()
}
#[must_use]
pub(crate) fn load_prompt_sections(asset_key: &str) -> Vec<String> {
split_sections(&load_prompt(asset_key))
}
#[must_use]
pub(crate) fn deal_sections(sections: &[String], count: usize, k: usize) -> String {
sections
.iter()
.skip(k)
.step_by(count)
.map(String::as_str)
.collect::<Vec<_>>()
.join("\n\n")
}
fn split_sections(text: &str) -> Vec<String> {
text.split("\n---\n")
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string)
.collect()
}
fn append_file_section(out: &mut String, name: &str, content: &str) {
let trimmed = content.trim();
if trimmed.is_empty() {
return;
}
let _ = writeln!(out, "--- {name} ---\n");
push_truncated(out, trimmed);
}
pub(crate) const MEMORY_FILE: &str = "MEMORY.md";
pub(crate) const MAX_WORKSPACE_FILE_CHARS: usize = 10_000;
const WORKSPACE_FILES: &[&str] = &[
"README.md",
"BOOTSTRAP.md",
MEMORY_FILE,
"CLAUDE.md",
"AGENTS.md",
"AGENTS.local.md",
"CLAUDE.local.md",
".cursorrules",
"copilot-instructions.md",
".github/copilot-instructions.md",
];
pub(crate) async fn build_workspace_context(workspace: &Path, personal_memory: bool) -> String {
let mut out = String::new();
for &filename in WORKSPACE_FILES {
if filename == MEMORY_FILE && !personal_memory {
continue;
}
let path = workspace.join(filename);
if let Ok(raw) = tokio::fs::read_to_string(&path).await {
append_file_section(&mut out, filename, &raw);
}
}
for (rel_path, content) in discover_claude_rules(workspace).await {
append_file_section(&mut out, &rel_path, &content);
}
out
}
pub(crate) fn wrap_workspace_context(content: &str) -> String {
let trimmed = content.trim();
if trimmed.is_empty() {
return String::new();
}
format!("\n<workspace-context>\n{trimmed}\n</workspace-context>\n")
}
async fn build_general_workspace_context(ws: &crate::Workspace) -> String {
let stored = match crate::workspace::WORKSPACES.get() {
Some(store) => store.get_general_context(&ws.name).await.ok().flatten(),
None => None,
};
let content = match stored {
Some(ctx) => ctx,
None => build_workspace_context(ws.as_path(), false).await,
};
wrap_workspace_context(&content)
}
pub(crate) async fn prepend_general_context(
messages: &mut Vec<crate::ChatMessage>,
ws: &crate::Workspace,
) {
let context = build_general_workspace_context(ws).await;
if !context.is_empty() {
messages.insert(0, crate::ChatMessage::system(&context));
}
}
pub(crate) fn format_ticket_block(ticket: &crate::pipeline::board::Ticket) -> String {
let mut comments = String::new();
if !ticket.comments.is_empty() {
let _ = writeln!(comments);
let _ = writeln!(comments, "### Comments ({})", ticket.comments.len());
let _ = writeln!(comments);
for comment in &ticket.comments {
let ts = format_local_timestamp(&comment.created_at);
let _ = writeln!(comments, "**{}** ({}):", comment.role, ts);
let _ = writeln!(comments, "{}", comment.content);
let _ = writeln!(comments);
let _ = writeln!(comments, "---");
let _ = writeln!(comments);
}
}
substitute(
&load_prompt("context/ticket.md"),
&[
("{{ticket_id}}", &ticket.id),
("{{ticket_title}}", &ticket.title),
("{{ticket_reporter}}", &ticket.reporter),
("{{ticket_priority}}", &format!("P{}", ticket.priority)),
("{{ticket_description}}", &ticket.description),
("{{ticket_comments}}", &comments),
],
)
}
fn format_local_timestamp(iso_str: &str) -> String {
crate::db::parse_utc_timestamp(iso_str).map_or_else(
|e| {
tracing::warn!(iso_str = %iso_str, error = %e, "Failed to parse timestamp, falling back to raw string");
iso_str.to_string()
},
|dt| {
dt.with_timezone(&chrono::Local)
.format("%Y-%m-%d %H:%M:%S")
.to_string()
},
)
}
fn truncation_point(text: &str) -> Option<usize> {
text.char_indices()
.nth(MAX_WORKSPACE_FILE_CHARS)
.map(|(idx, _)| idx)
}
pub(crate) fn exceeds_workspace_file_limit(text: &str) -> bool {
truncation_point(text).is_some()
}
fn push_truncated(out: &mut String, text: &str) {
if let Some(idx) = truncation_point(text) {
out.push_str(&text[..idx]);
let _ = writeln!(
out,
"\n\n{}\n",
substitute(
&load_prompt("context/truncation_notice.md"),
&[("{{max_chars}}", &MAX_WORKSPACE_FILE_CHARS.to_string())],
),
);
} else {
out.push_str(text);
out.push_str("\n\n");
}
}
async fn discover_claude_rules(workspace: &Path) -> Vec<(String, String)> {
let rules_dir = workspace.join(".claude").join("rules");
let Ok(mut entries) = tokio::fs::read_dir(&rules_dir).await else {
return Vec::new();
};
let mut files = Vec::new();
while let Ok(Some(entry)) = entries.next_entry().await {
let path = entry.path();
if path.extension().is_some_and(|e| e == "md") {
files.push(path);
}
}
files.sort_by(|a, b| a.file_name().cmp(&b.file_name()));
let mut rules = Vec::new();
for path in files {
if let Ok(content) = tokio::fs::read_to_string(&path).await {
let rel_path = path
.strip_prefix(workspace)
.unwrap_or(&path)
.display()
.to_string();
rules.push((rel_path, content));
}
}
rules
}
pub(crate) fn substitute(template: &str, replacements: &[(&str, &str)]) -> String {
let map: HashMap<&str, &str> = replacements.iter().copied().collect();
TEMPLATE_RE
.replace_all(template, |caps: ®ex::Captures| {
let whole = caps
.get(0)
.expect("capture group 0 always matches")
.as_str();
if let Some(val) = map.get(whole) {
(*val).to_owned()
} else {
tracing::warn!(
template_var = %whole,
"prompt substitution: no replacement provided for '{whole}' — \
literal text will appear in the prompt output. \
Check that the variable name in the template matches a \
replacement key at the call site."
);
whole.to_owned()
}
})
.into_owned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn substitute_cases() {
type Pair = (&'static str, &'static str);
type Case = (&'static str, &'static str, &'static [Pair], &'static str);
let cases: &[Case] = &[
(
"both keys replaced",
"Hello {{name}}, your {{item}} is ready.",
&[("{{name}}", "Alice"), ("{{item}}", "order")],
"Hello Alice, your order is ready.",
),
(
"unknown key preserved",
"Hello {{name}}, here is {{missing}} key.",
&[("{{name}}", "Alice")],
"Hello Alice, here is {{missing}} key.",
),
("empty template", "", &[("{{key}}", "value")], ""),
("no replacements", "Hello {{name}}!", &[], "Hello {{name}}!"),
];
for (case, template, replacements, expected) in cases {
assert_eq!(
substitute(template, replacements),
*expected,
"case '{case}'"
);
}
}
#[test]
fn substitute_no_cascade() {
let result = substitute(
"First: {{a}}, Second: {{b}}",
&[("{{a}}", "value-{{b}}"), ("{{b}}", "actual-b")],
);
assert_eq!(result, "First: value-{{b}}, Second: actual-b");
}
#[tokio::test]
async fn discover_claude_rules_finds_only_readable_md_files() {
let dir = tempfile::tempdir().unwrap();
let rules_dir = dir.path().join(".claude").join("rules");
std::fs::create_dir_all(&rules_dir).unwrap();
std::fs::write(rules_dir.join("testing.md"), "Test content").unwrap();
std::fs::write(rules_dir.join("style.md"), "Style rules").unwrap();
std::fs::write(rules_dir.join("notes.txt"), "irrelevant").unwrap();
std::fs::create_dir(rules_dir.join("broken.md")).unwrap();
let rules = discover_claude_rules(dir.path()).await;
let paths: Vec<&str> = rules.iter().map(|(p, _)| p.as_str()).collect();
assert_eq!(
paths,
[".claude/rules/style.md", ".claude/rules/testing.md"],
"only the readable .md rules are discovered"
);
}
#[tokio::test]
async fn discover_claude_rules_missing_dir() {
let dir = tempfile::tempdir().unwrap();
let rules = discover_claude_rules(dir.path()).await;
assert!(rules.is_empty());
}
#[tokio::test]
async fn discover_claude_rules_returns_sorted_order() {
let dir = tempfile::tempdir().unwrap();
let rules_dir = dir.path().join(".claude").join("rules");
std::fs::create_dir_all(&rules_dir).unwrap();
for name in ["zebra", "alpha", "middle"] {
std::fs::write(
rules_dir.join(format!("{name}.md")),
format!("{name} rules"),
)
.unwrap();
}
let rules = discover_claude_rules(dir.path()).await;
let paths: Vec<&str> = rules.iter().map(|(p, _)| p.as_str()).collect();
assert_eq!(
paths,
[
".claude/rules/alpha.md",
".claude/rules/middle.md",
".claude/rules/zebra.md",
]
);
}
#[test]
fn the_file_limit_counts_characters() {
let fits = "ё".repeat(MAX_WORKSPACE_FILE_CHARS);
assert_eq!(
fits.len(),
MAX_WORKSPACE_FILE_CHARS * 2,
"two bytes per char"
);
assert!(!exceeds_workspace_file_limit(&fits));
assert!(exceeds_workspace_file_limit(&format!("{fits}ё")));
}
}