feat(agent): add AI summarization for conversation history compacting
This commit is contained in:
@@ -232,6 +232,8 @@ fn handle_tick(state: &mut AppStateRest) {
|
||||
if let Some(ref mut rt) = state.session_runtime {
|
||||
rt.usage.tokens_in = rt.usage.tokens_in.saturating_add(tokens_in);
|
||||
rt.usage.tokens_out = rt.usage.tokens_out.saturating_add(tokens_out);
|
||||
rt.usage.last_tokens_in = tokens_in;
|
||||
rt.usage.last_tokens_out = tokens_out;
|
||||
}
|
||||
}
|
||||
TurnEvent::ReviewUsage {
|
||||
@@ -436,19 +438,21 @@ fn handle_abort_turn(state: &mut AppStateRest) {
|
||||
}
|
||||
|
||||
fn handle_compact(state: &mut AppStateRest) {
|
||||
tracing::info!("compacting conversation");
|
||||
const KEEP_HEAD: usize = 2; // system prompt + tool definitions
|
||||
const KEEP_TAIL: usize = 10; // recent conversation messages
|
||||
tracing::info!("compacting conversation with AI summarization");
|
||||
let provider_name = &state.settings.provider;
|
||||
let provider_cfg = state.app_config.providers.get(provider_name).cloned();
|
||||
let api_key = state.settings.api_keys.get(provider_name).cloned().unwrap_or_default();
|
||||
let model = state.settings.model.clone();
|
||||
let api_base = provider_cfg.map(|cfg| cfg.api_base.clone());
|
||||
|
||||
let client = zesdex_infrastructure::llm::LlmClient::new(api_key, model, api_base);
|
||||
|
||||
if let Some(ref mut rt) = state.session_runtime {
|
||||
if rt.messages.len() > KEEP_HEAD + KEEP_TAIL {
|
||||
let tail = rt.messages.split_off(rt.messages.len() - KEEP_TAIL);
|
||||
let head: Vec<_> = rt.messages.drain(..KEEP_HEAD.min(rt.messages.len())).collect();
|
||||
rt.messages = head;
|
||||
rt.messages.extend(tail);
|
||||
if let Ok(()) = zesdex_infrastructure::agent::compact_messages_with_ai(&mut rt.messages, &client) {
|
||||
let msg_count = rt.messages.len();
|
||||
state.push_transcript(ChatMessageDisplay::new(
|
||||
RoleWrapper::System,
|
||||
format!("Conversation compacted to {msg_count} messages."),
|
||||
format!("Conversation compacted via AI Summarizer to {msg_count} messages."),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,6 +155,8 @@ pub fn apply_action(state: &mut crate::state::AppStateRest, action: Action) {
|
||||
if let Some(ref mut rt) = state.session_runtime {
|
||||
rt.usage.tokens_in = rt.usage.tokens_in.saturating_add(tokens_in);
|
||||
rt.usage.tokens_out = rt.usage.tokens_out.saturating_add(tokens_out);
|
||||
rt.usage.last_tokens_in = tokens_in;
|
||||
rt.usage.last_tokens_out = tokens_out;
|
||||
rt.usage.api_calls = rt.usage.api_calls.saturating_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user