Skip to main content

magi_code/
context.rs

1#[cfg(test)]
2mod cache;
3mod code_mode;
4mod replay;
5mod replay_cache;
6mod tokens;
7
8#[cfg(test)]
9pub(crate) use cache::conversation_cache_material;
10#[cfg(test)]
11pub(crate) use replay::{
12    ConversationReplay, REPLAY_TOOL_RESULT_OUTPUT_CHAR_LIMIT, build_conversation_replay,
13    build_conversation_replay_with_limits, compact_historical_tool_output,
14};
15pub(crate) use replay::{
16    ConversationReplayLimits, REPLAY_JSONL_MAX_BYTES, REPLAY_JSONL_MAX_LINES,
17    build_conversation_replay_from_events,
18};
19pub(crate) use replay_cache::ConversationReplayCache;
20#[cfg(test)]
21pub(crate) use replay_cache::ReplayCacheMetrics;
22#[cfg(test)]
23pub(crate) use tokens::estimate_provider_request_input_tokens;
24pub(crate) use tokens::estimate_text_tokens;
25pub use tokens::{ContextBudget, ContextBudgetOverride};
26pub(crate) use tokens::{
27    ContextTokenCount, count_supported_provider_conversation_item_tokens,
28    project_provider_conversation_items_tokens, project_provider_request_input_tokens,
29    project_text_tokens, token_encoding_family_for_model,
30};
31
32#[cfg(test)]
33pub(crate) use crate::providers::{ChatMessage, ProviderConversationItem, ProviderToolResult};
34use crate::sessions::{SessionEvent, SessionEventKind};
35use serde_json::Value;
36#[cfg(test)]
37pub(crate) use serde_json::json;
38
39pub(crate) fn is_local_only_session_event(event: &SessionEvent) -> bool {
40    event.kind().is_some_and(SessionEventKind::is_local_only)
41}
42
43fn event_line(event: &SessionEvent) -> String {
44    let compact_payload = if let Some(text) = event.payload.get("text").and_then(Value::as_str) {
45        text.to_string()
46    } else if let Some(message) = event.payload.get("message").and_then(Value::as_str) {
47        message.to_string()
48    } else {
49        event.payload.to_string()
50    };
51    format!(
52        "{}: {}",
53        event.event_type,
54        compact_payload.replace('\n', "\\n")
55    )
56}
57
58pub(crate) fn canonical_json(value: &Value) -> String {
59    serde_json::to_string(value).unwrap_or_else(|_| "null".to_string())
60}
61
62#[cfg(test)]
63mod tests;