MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / trigger_compaction

Method trigger_compaction

crates/opencode-session/src/prompt.rs:2543–2589  ·  view source on GitHub ↗
(session: &mut Session, messages: &[SessionMessage])

Source from the content-addressed store, hash-verified

2541 }
2542
2543 fn trigger_compaction(session: &mut Session, messages: &[SessionMessage]) -> Option<String> {
2544 let total_messages = messages.len();
2545 if total_messages < 10 {
2546 return None;
2547 }
2548
2549 let keep_count = total_messages / 2;
2550 let summary_parts: Vec<String> = messages
2551 .iter()
2552 .take(keep_count)
2553 .flat_map(|m| m.parts.iter())
2554 .filter_map(|p| match &p.part_type {
2555 PartType::Text { text, .. } => Some(text.clone()),
2556 _ => None,
2557 })
2558 .collect();
2559
2560 let summary = format!(
2561 "Compacted {} messages. Summary: {}...",
2562 total_messages - keep_count,
2563 summary_parts
2564 .join(" ")
2565 .chars()
2566 .take(500)
2567 .collect::<String>()
2568 );
2569
2570 // Persist the compaction summary as a Compaction part on a new assistant message.
2571 // This mirrors the TS behavior where compaction creates an assistant message with
2572 // summary=true and a compaction part, so that filter_compacted_messages can find it.
2573 let mut compaction_msg = SessionMessage::assistant(session.id.clone());
2574 compaction_msg.parts.push(crate::MessagePart {
2575 id: format!("prt_{}", uuid::Uuid::new_v4()),
2576 part_type: PartType::Compaction {
2577 summary: summary.clone(),
2578 },
2579 created_at: chrono::Utc::now(),
2580 message_id: None,
2581 });
2582 session.messages.push(compaction_msg);
2583
2584 // Set the compacting timestamp on the session.
2585 session.time.compacting = Some(chrono::Utc::now().timestamp_millis());
2586 session.touch();
2587
2588 Some(summary)
2589 }
2590}
2591
2592impl Default for SessionPrompt {

Callers

nothing calls this directly

Calls 2

cloneMethod · 0.45
touchMethod · 0.45

Tested by

no test coverage detected