Skip to main content

lean_ctx/proxy/
compress_shared.rs

1//! Provider-neutral classification for content blocks compressed in place.
2
3use serde_json::Value;
4
5use super::tool_kind::{self, ToolResultKind};
6
7/// Semantic type of a provider content block.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum ToolKind {
10    ToolUse,
11    ToolResult,
12    Text,
13    Image,
14    Other,
15}
16
17/// Classify the common content-block types used by provider request shapes.
18pub fn classify_tool_kind(content: &Value) -> ToolKind {
19    match content.get("type").and_then(Value::as_str) {
20        Some("tool_use" | "function_call") => ToolKind::ToolUse,
21        Some("tool_result" | "function_call_output") => ToolKind::ToolResult,
22        Some("text" | "input_text" | "output_text") => ToolKind::Text,
23        Some("image" | "image_url" | "input_image") => ToolKind::Image,
24        _ => ToolKind::Other,
25    }
26}
27
28/// Whether this block kind carries text that may be compressed in place.
29pub const fn should_compress_content(kind: ToolKind) -> bool {
30    matches!(kind, ToolKind::Text)
31}
32
33/// Resolve a tool name to its result kind, preserving the unknown-tool fallback.
34pub fn tool_result_kind(tool_name: Option<&str>) -> ToolResultKind {
35    tool_name.map_or(ToolResultKind::Other, tool_kind::classify_tool_name)
36}