use crate::api::{Message, SystemPrompt};
use crate::config::SofosConfig;
#[derive(Clone)]
pub struct ConversationHistory {
messages: Vec<Message>,
system_prompt: Vec<SystemPrompt>,
config: SofosConfig,
}
impl ConversationHistory {
pub fn new() -> Self {
Self::with_features(false, false, None)
}
pub fn with_features(
has_morph: bool,
has_code_search: bool,
custom_instructions: Option<String>,
) -> Self {
let mut features = vec![
"1. Read files in the current project directory",
"2. Write/create files in the current project directory",
"3. List directory contents",
"4. Create directories",
"5. Search the web for information",
"6. Execute read-only bash commands (for testing code)",
"7. View images (user includes image path or URL in their message)",
];
if has_code_search {
features.push("8. Search code using ripgrep");
}
let edit_instruction = if has_morph {
"- When creating new files, use the write_file tool\n- When editing existing files, ALWAYS use the morph_edit_file tool (ultra-fast, 10,500+ tokens/sec)"
} else {
"- When creating or editing code, use the write_file tool"
};
let mut system_text = format!(
r#"You are Sofos, an AI coding assistant. You have access to tools that allow you to:
{}
When helping users:
- Be concise and practical
- Context interpretation: When users refer to "this code", "these files", or similar context-dependent terms without specifying a path, they mean the code in the current working directory
- ALWAYS explore first: Use list_directory to find files before trying to read them if you're unsure of their location
- Use your tools to read files before suggesting changes
{}
- Search the web when you need current information or documentation
- Execute bash commands safely with 3-tier permission system:
* Tier 1 (Allowed): Build tools (cargo, npm, python), read-only ops (ls, cat, grep) execute automatically
* Tier 2 (Forbidden): Destructive commands (rm, chmod, sudo) are always blocked
* Tier 3 (Ask): Unknown commands prompt user for permission
* Bash commands are ALWAYS sandboxed to workspace (no parent traversal, no absolute paths)
- Never run destructive or irreversible shell commands (e.g., rm -rf, rm, rmdir, dd, mkfs*, fdisk/parted, wipefs, chmod/chown -R on broad paths, truncate, :>, >/dev/sd*, kill -9 on system services).
Do not modify or delete files outside the working directory.
Prefer read-only commands and dry-runs; if a potentially destructive action seems necessary, stop and request explicit confirmation before proceeding.
- Explain your reasoning when using tools
Outside Workspace Access:
- read_file and list_directory CAN access absolute paths (like /path/to/file) and ~/paths IF the user has configured Read permissions
- To view files outside workspace, use read_file or list_directory with the absolute or ~/ path directly
- NEVER use bash commands (cat, ls, etc.) for outside workspace access - bash is always sandboxed to workspace
- Images: users can view images by including the path in their message (works for both workspace and permitted outside paths)
Image Vision:
- When users include image paths (.jpg, .png, .gif, .webp) or URLs in their message, you will see the image
- Local images: relative paths (in workspace) or absolute/~/ paths (if permitted in config)
- Web images: URLs starting with http:// or https://
- You do NOT need to use any tool to view images - they are automatically loaded and shown to you
- If asked to view an image, tell the user to include the image path or URL in their message
CRITICAL - Making Changes:
- NEVER make code changes or file modifications unless explicitly instructed by the user
- When the user asks for suggestions or improvements, DESCRIBE what you would change without implementing it
- Only implement changes when the user gives explicit approval (e.g., "do it", "implement that", "make the change")
- If unsure whether to implement or just suggest, always ask first
Testing after code changes:
- After editing code files (not comments, README, or documentation), ALWAYS test the changes using execute_bash
- Run appropriate build/test commands based on the project type:
* Rust: 'cargo build' and/or 'cargo test'
* JavaScript/TypeScript: 'npm run build' and/or 'npm test'
* Python: 'python -m pytest' or 'python -m unittest'
* Go: 'go build' and/or 'go test'
- If tests fail, fix the errors and test again
- Do NOT run tests for changes to: comments only, README.md, documentation files, or configuration files
Your goal is to help users with coding tasks efficiently and accurately.
Always use the metric system for all measurements. If the user uses other units, convert them and answer in metric.
Show imperial units only when the user explicitly asks for them."#,
features.join("\n"),
edit_instruction
);
if let Some(instructions) = custom_instructions {
system_text.push_str("\n\n");
system_text.push_str(&instructions);
}
Self {
messages: Vec::new(),
system_prompt: vec![SystemPrompt::new_cached_with_ttl(
system_text.to_string(),
None,
)],
config: SofosConfig::default(),
}
}
pub fn estimate_tokens(text: &str) -> usize {
(text.len() as f64 / 3.5).ceil() as usize
}
fn estimate_system_tokens(&self) -> usize {
self.system_prompt
.iter()
.map(|sp| Self::estimate_tokens(&sp.text))
.sum()
}
fn estimate_message_tokens(msg: &Message) -> usize {
use crate::api::{MessageContent, MessageContentBlock};
match &msg.content {
MessageContent::Text { content } => Self::estimate_tokens(content),
MessageContent::Blocks { content } => content
.iter()
.map(|block| match block {
MessageContentBlock::Text { text, .. } => Self::estimate_tokens(text),
MessageContentBlock::Thinking {
thinking,
signature,
..
} => Self::estimate_tokens(thinking) + Self::estimate_tokens(signature) + 10,
MessageContentBlock::Summary { summary, .. } => {
Self::estimate_tokens(summary) + 10
}
MessageContentBlock::ToolUse {
id, name, input, ..
} => {
let input_str = serde_json::to_string(input).unwrap_or_default();
Self::estimate_tokens(id)
+ Self::estimate_tokens(name)
+ Self::estimate_tokens(&input_str)
+ 10
}
MessageContentBlock::ToolResult {
tool_use_id,
content,
..
} => Self::estimate_tokens(tool_use_id) + Self::estimate_tokens(content) + 10,
MessageContentBlock::ServerToolUse {
id, name, input, ..
} => {
let input_str = serde_json::to_string(input).unwrap_or_default();
Self::estimate_tokens(id)
+ Self::estimate_tokens(name)
+ Self::estimate_tokens(&input_str)
+ 10
}
MessageContentBlock::WebSearchToolResult {
tool_use_id,
content,
..
} => {
let content_str = serde_json::to_string(content).unwrap_or_default();
Self::estimate_tokens(tool_use_id)
+ Self::estimate_tokens(&content_str)
+ 20
}
MessageContentBlock::Image { source, .. } => {
match source {
crate::api::ImageSource::Base64 { data, .. } => {
let estimated_bytes = data.len() * 3 / 4;
let estimated_pixels = estimated_bytes / 10;
(estimated_pixels / 750).max(100)
}
crate::api::ImageSource::Url { .. } => {
1000
}
}
}
})
.sum(),
}
}
pub fn estimate_total_tokens(&self) -> usize {
let system_tokens = self.estimate_system_tokens();
let message_tokens: usize = self
.messages
.iter()
.map(|m| Self::estimate_message_tokens(m))
.sum();
system_tokens + message_tokens
}
fn trim_if_needed(&mut self) {
if self.messages.len() > self.config.max_messages {
let remove_count = self.messages.len() - self.config.max_messages;
self.messages.drain(0..remove_count);
}
let mut total_tokens = self.estimate_total_tokens();
while total_tokens > self.config.max_context_tokens && self.messages.len() > 10 {
let removed_tokens = Self::estimate_message_tokens(&self.messages[0]);
self.messages.remove(0);
total_tokens -= removed_tokens;
}
if total_tokens > self.config.max_context_tokens && self.messages.len() <= 10 {
eprintln!(
"⚠️ Warning: Conversation approaching token limit ({} tokens). Consider starting a new session.",
total_tokens
);
}
}
pub fn needs_compaction(&self) -> bool {
let threshold =
(self.config.max_context_tokens as f64 * self.config.compaction_trigger_ratio) as usize;
self.estimate_total_tokens() > threshold
}
pub fn compaction_split_point(&self) -> usize {
let preserve = self.config.compaction_preserve_recent;
if self.messages.len() <= preserve + 5 {
return 0;
}
let mut split = self.messages.len().saturating_sub(preserve);
while split > 0 && self.messages[split].role != "user" {
split -= 1;
}
while split > 0 {
if let crate::api::MessageContent::Blocks { content } = &self.messages[split].content {
let has_tool_result = content.iter().any(|b| {
matches!(
b,
crate::api::MessageContentBlock::ToolResult { .. }
| crate::api::MessageContentBlock::WebSearchToolResult { .. }
)
});
if has_tool_result {
split -= 1;
continue;
}
}
break;
}
split
}
pub fn truncate_tool_results(&mut self, up_to: usize) {
let threshold = self.config.tool_result_truncate_threshold;
let keep_chars = 500;
for msg in self.messages[..up_to].iter_mut() {
if let crate::api::MessageContent::Blocks { content } = &mut msg.content {
for block in content.iter_mut() {
if let crate::api::MessageContentBlock::ToolResult {
content: result_text,
..
} = block
{
if result_text.len() > threshold {
let original_len = result_text.len();
let actual_keep = keep_chars.min(original_len / 3);
let start = &result_text[..actual_keep];
let end = &result_text[result_text.len() - actual_keep..];
*result_text = format!(
"{}\n...[truncated {} chars]...\n{}",
start, original_len, end
);
}
}
}
}
}
}
pub fn serialize_messages_for_summary(messages: &[Message]) -> String {
let mut parts = Vec::new();
for msg in messages {
let role_label = if msg.role == "user" {
"User"
} else {
"Assistant"
};
match &msg.content {
crate::api::MessageContent::Text { content } => {
parts.push(format!("{}: {}", role_label, content));
}
crate::api::MessageContent::Blocks { content } => {
for block in content {
match block {
crate::api::MessageContentBlock::Text { text, .. } => {
parts.push(format!("{}: {}", role_label, text));
}
crate::api::MessageContentBlock::ToolUse { name, input, .. } => {
let input_str = serde_json::to_string(input).unwrap_or_default();
let input_preview = if input_str.len() > 200 {
format!("{}...", &input_str[..200])
} else {
input_str
};
parts.push(format!("[Tool call: {}({})]", name, input_preview));
}
crate::api::MessageContentBlock::ToolResult { content, .. } => {
let preview = if content.len() > 300 {
format!("{}...", &content[..300])
} else {
content.clone()
};
parts.push(format!("[Tool result: {}]", preview));
}
crate::api::MessageContentBlock::Image { .. } => {
parts.push("[Image attached]".to_string());
}
_ => {}
}
}
}
}
}
parts.join("\n\n")
}
pub fn replace_with_summary(&mut self, summary: String, split_point: usize) {
if split_point == 0 || split_point > self.messages.len() {
return;
}
self.messages.drain(0..split_point);
let summary_msg = Message::user(format!(
"[Conversation Summary]\n\nThe following is a summary of our earlier conversation:\n\n{}",
summary
));
self.messages.insert(0, summary_msg);
}
pub fn fallback_trim(&mut self) {
self.trim_if_needed();
}
pub fn add_user_message(&mut self, content: String) {
self.messages.push(Message::user(content));
self.trim_if_needed();
}
pub fn add_user_with_blocks(&mut self, blocks: Vec<crate::api::MessageContentBlock>) {
self.messages.push(Message::user_with_blocks(blocks));
self.trim_if_needed();
}
pub fn add_assistant_with_blocks(&mut self, blocks: Vec<crate::api::MessageContentBlock>) {
self.messages.push(Message::assistant_with_blocks(blocks));
self.trim_if_needed();
}
pub fn add_tool_results(&mut self, results: Vec<crate::api::MessageContentBlock>) {
self.messages.push(Message::user_with_tool_results(results));
self.trim_if_needed();
}
pub fn messages(&self) -> &[Message] {
&self.messages
}
pub fn system_prompt(&self) -> &Vec<SystemPrompt> {
&self.system_prompt
}
pub fn clear(&mut self) {
self.messages.clear();
}
pub fn restore_messages(&mut self, messages: Vec<Message>) {
self.messages = messages;
self.trim_if_needed();
}
pub fn remove_last_message(&mut self) {
self.messages.pop();
}
pub fn _len(&self) -> usize {
self.messages.len()
}
pub fn _is_empty(&self) -> bool {
self.messages.is_empty()
}
}
impl Default for ConversationHistory {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::api::MessageContentBlock;
#[test]
fn test_message_limit_trimming() {
let mut history = ConversationHistory::new();
for i in 0..510 {
history.add_user_message(format!("Message {}", i));
}
assert_eq!(history.messages().len(), 500);
if let crate::api::MessageContent::Text { content } = &history.messages()[0].content {
assert_eq!(content, "Message 10");
}
}
#[test]
fn test_message_limit_with_blocks() {
let mut history = ConversationHistory::new();
for i in 0..260 {
history.add_user_message(format!("User {}", i));
history.add_assistant_with_blocks(vec![MessageContentBlock::Text {
text: format!("Assistant {}", i),
cache_control: None,
}]);
}
assert_eq!(history.messages().len(), 500);
}
#[test]
fn test_no_trimming_below_limit() {
let mut history = ConversationHistory::new();
for i in 0..20 {
history.add_user_message(format!("Message {}", i));
}
assert_eq!(history.messages().len(), 20);
}
#[test]
fn test_token_limit_trimming() {
let mut history = ConversationHistory::new();
history.config.max_context_tokens = 5000;
let large_content = "x".repeat(1000);
for i in 0..20 {
history.add_user_message(format!("{} {}", i, large_content));
}
assert!(history.messages().len() < 20);
assert!(history.messages().len() >= 10);
if let crate::api::MessageContent::Text { content } = &history.messages()[0].content {
assert!(!content.starts_with("0 "));
}
}
#[test]
fn test_token_estimation() {
let tokens = ConversationHistory::estimate_tokens("12345678901234567890123456789012345");
assert_eq!(tokens, 10);
let tokens = ConversationHistory::estimate_tokens("");
assert_eq!(tokens, 0);
}
#[test]
fn test_needs_compaction() {
let mut history = ConversationHistory::new();
history.config.max_context_tokens = 100_000;
history.config.compaction_trigger_ratio = 0.80;
history.add_user_message("hello".to_string());
assert!(!history.needs_compaction());
let large_content = "x".repeat(10_000);
for _ in 0..30 {
history.messages.push(Message::user(large_content.clone()));
}
assert!(history.needs_compaction());
}
#[test]
fn test_compaction_split_point() {
let mut history = ConversationHistory::new();
history.config.compaction_preserve_recent = 4;
for i in 0..10 {
history.messages.push(Message::user(format!("msg {}", i)));
}
let split = history.compaction_split_point();
assert_eq!(split, 6); }
#[test]
fn test_compaction_split_too_few_messages() {
let mut history = ConversationHistory::new();
history.config.compaction_preserve_recent = 20;
for i in 0..10 {
history.messages.push(Message::user(format!("msg {}", i)));
}
let split = history.compaction_split_point();
assert_eq!(split, 0); }
#[test]
fn test_truncate_tool_results() {
let mut history = ConversationHistory::new();
history.config.tool_result_truncate_threshold = 100;
let large_content = "x".repeat(500);
history.messages.push(Message::user_with_tool_results(vec![
MessageContentBlock::ToolResult {
tool_use_id: "id1".to_string(),
content: large_content,
cache_control: None,
},
]));
history.truncate_tool_results(1);
if let crate::api::MessageContent::Blocks { content } = &history.messages()[0].content {
if let MessageContentBlock::ToolResult { content, .. } = &content[0] {
assert!(content.contains("truncated"));
assert!(content.len() < 500); } else {
panic!("Expected ToolResult");
}
} else {
panic!("Expected Blocks");
}
}
#[test]
fn test_replace_with_summary() {
let mut history = ConversationHistory::new();
for i in 0..10 {
history.messages.push(Message::user(format!("msg {}", i)));
}
history.replace_with_summary("This is the summary".to_string(), 7);
assert_eq!(history.messages().len(), 4);
if let crate::api::MessageContent::Text { content } = &history.messages()[0].content {
assert!(content.contains("Conversation Summary"));
assert!(content.contains("This is the summary"));
}
}
#[test]
fn test_serialize_messages_for_summary() {
let messages = vec![
Message::user("Hello, help me with code".to_string()),
Message::assistant_with_blocks(vec![MessageContentBlock::Text {
text: "Sure, let me look at the files.".to_string(),
cache_control: None,
}]),
];
let serialized = ConversationHistory::serialize_messages_for_summary(&messages);
assert!(serialized.contains("User: Hello, help me with code"));
assert!(serialized.contains("Assistant: Sure, let me look at the files."));
}
}