Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/code_assistant/resources/compaction_prompt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<system-compaction>
The conversation history is nearing the model's context window limit. Provide a thorough summary that allows resuming the task without the earlier messages. Include:
- The current objectives or tasks.
- Key actions taken so far and their outcomes.
- Important files, commands, or decisions that matter for continuing.
- Outstanding questions or follow-up work that still needs attention.
Respond with plain text only.
</system-compaction>
65 changes: 25 additions & 40 deletions crates/code_assistant/src/acp/agent.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use agent_client_protocol as acp;
#[allow(unused_imports)]
use anyhow::Context;
use anyhow::Result;
use serde_json::{json, Map as JsonMap, Value as JsonValue};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -321,14 +323,12 @@ impl acp::Agent for ACPAgentImpl {

let session_id = {
let mut manager = session_manager.lock().await;
let session_model_config = SessionModelConfig::new(model_name.clone());
manager
.create_session_with_config(
None,
Some(session_config),
Some(SessionModelConfig {
model_name: model_name.clone(),
record_path: None,
}),
Some(session_model_config),
)
.map_err(|e| {
tracing::error!("Failed to create session: {}", e);
Expand All @@ -344,13 +344,11 @@ impl acp::Agent for ACPAgentImpl {
{
if model_info.selection_changed {
let mut manager = session_manager.lock().await;
if let Err(err) = manager.set_session_model_config(
&session_id,
Some(SessionModelConfig {
model_name: model_info.selected_model_name.clone(),
record_path: None,
}),
) {
let fallback_model_config =
SessionModelConfig::new(model_info.selected_model_name.clone());
if let Err(err) =
manager.set_session_model_config(&session_id, Some(fallback_model_config))
{
tracing::error!(
error = ?err,
"ACP: Failed to persist fallback model selection for session {}",
Expand Down Expand Up @@ -446,16 +444,12 @@ impl acp::Agent for ACPAgentImpl {
.map(|config| config.model_name.as_str()),
) {
if model_info.selection_changed {
let record_path = stored_model_config
.as_ref()
.and_then(|config| config.record_path.clone());
let mut manager = session_manager.lock().await;
let fallback_model_config =
SessionModelConfig::new(model_info.selected_model_name.clone());
if let Err(err) = manager.set_session_model_config(
&arguments.session_id.0,
Some(SessionModelConfig {
model_name: model_info.selected_model_name.clone(),
record_path,
}),
Some(fallback_model_config),
) {
tracing::error!(
error = ?err,
Expand Down Expand Up @@ -541,10 +535,7 @@ impl acp::Agent for ACPAgentImpl {

let session_model_config = match config_result {
Ok(Some(config)) => config,
Ok(None) => SessionModelConfig {
model_name: model_name.clone(),
record_path: None,
},
Ok(None) => SessionModelConfig::new(model_name.clone()),
Err(e) => {
let error_msg = format!(
"Failed to load session model configuration for session {}: {e}",
Expand All @@ -565,6 +556,7 @@ impl acp::Agent for ACPAgentImpl {
&model_name_for_prompt,
playback_path,
fast_playback,
None,
)
.await
{
Expand Down Expand Up @@ -813,27 +805,20 @@ impl acp::Agent for ACPAgentImpl {
manager.get_session_model_config(&session_id.0)
};

let record_path = match existing_config {
Ok(Some(config)) => config.record_path,
Ok(None) => None,
Err(err) => {
tracing::error!(
error = ?err,
"ACP: Failed to read existing session model configuration"
);
return Err(acp::Error::internal_error());
}
};
if let Err(err) = existing_config {
tracing::error!(
error = ?err,
"ACP: Failed to read existing session model configuration"
);
return Err(acp::Error::internal_error());
}

{
let mut manager = session_manager.lock().await;
if let Err(err) = manager.set_session_model_config(
&session_id.0,
Some(SessionModelConfig {
model_name: display_name.clone(),
record_path,
}),
) {
let new_model_config = SessionModelConfig::new(display_name.clone());
if let Err(err) =
manager.set_session_model_config(&session_id.0, Some(new_model_config))
{
tracing::error!(
error = ?err,
"ACP: Failed to persist session model selection"
Expand Down
7 changes: 7 additions & 0 deletions crates/code_assistant/src/acp/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ pub fn fragment_to_content_block(fragment: &DisplayFragment) -> acp::ContentBloc
text: text.clone(),
meta: None,
}),
DisplayFragment::CompactionDivider { summary } => {
acp::ContentBlock::Text(acp::TextContent {
annotations: None,
text: format!("Conversation compacted:\n{summary}"),
meta: None,
})
}
DisplayFragment::Image { media_type, data } => {
acp::ContentBlock::Image(acp::ImageContent {
annotations: None,
Expand Down
5 changes: 5 additions & 0 deletions crates/code_assistant/src/acp/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ impl UserInterface for ACPUserUI {
// Events that don't translate to ACP
UiEvent::UpdateMemory { .. }
| UiEvent::SetMessages { .. }
| UiEvent::DisplayCompactionSummary { .. }
| UiEvent::StreamingStarted(_)
| UiEvent::StreamingStopped { .. }
| UiEvent::RefreshChatList
Expand Down Expand Up @@ -653,6 +654,10 @@ impl UserInterface for ACPUserUI {
let content = fragment_to_content_block(fragment);
self.queue_session_update(acp::SessionUpdate::AgentMessageChunk { content });
}
DisplayFragment::CompactionDivider { .. } => {
let content = fragment_to_content_block(fragment);
self.queue_session_update(acp::SessionUpdate::AgentMessageChunk { content });
}
DisplayFragment::ThinkingText(_) => {
let content = fragment_to_content_block(fragment);
self.queue_session_update(acp::SessionUpdate::AgentThoughtChunk { content });
Expand Down
2 changes: 1 addition & 1 deletion crates/code_assistant/src/agent/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl FileStatePersistence {
);
let json = std::fs::read_to_string(&self.state_file_path)?;
let mut session: ChatSession = serde_json::from_str(&json)?;
session.ensure_config();
session.ensure_config()?;

info!(
"Loaded agent state with {} messages",
Expand Down
Loading