Skip to main content

lean_ctx_proxy/
compress_shared.rs

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