m2m/codec/mod.rs
1//! Multi-codec compression engine for M2M Protocol.
2//!
3//! This module provides the core compression functionality with multiple
4//! algorithms optimized for different content types and sizes.
5//!
6//! # Algorithms
7//!
8//! | Algorithm | Wire Prefix | Best For |
9//! |--------------|----------------------|---------------------------------|
10//! | [`M2M`] | `#M2M\|1\|` | All content (100% JSON fidelity)|
11//! | [`TokenNative`] | `#TK\|` | Legacy token-based compression |
12//! | [`Brotli`] | `#M2M[v3.0]\|DATA:` | Large repetitive content (>1KB) |
13//! | [`None`] | (passthrough) | Small content (<100 bytes) |
14//!
15//! # M2M Wire Format v1
16//!
17//! The new M2M wire format provides:
18//! - **100% JSON fidelity**: Original JSON is perfectly reconstructed
19//! - **Header extraction**: Routing info available without decompression
20//! - **Cost estimation**: Token counts and cost in headers
21//! - **Optional encryption**: HMAC or AEAD security modes
22//!
23//! # Wire Format Examples
24//!
25//! ```text
26//! // M2M v1 format (default)
27//! #M2M|1|<fixed_header><routing_header><compressed_payload>
28//!
29//! // Legacy formats (still supported for decoding)
30//! #TK|C|<varint_tokens>
31//! #M2M[v3.0]|DATA:<base64_brotli>
32//! ```
33//!
34//! # Usage
35//!
36//! ```rust,ignore
37//! use m2m::codec::{CodecEngine, Algorithm};
38//! use m2m::codec::m2m::M2MCodec;
39//!
40//! // New M2M codec (recommended)
41//! let m2m_codec = M2MCodec::new();
42//! let encoded = m2m_codec.encode(json)?;
43//! let decoded = m2m_codec.decode(&encoded)?; // 100% fidelity
44//!
45//! // Or use CodecEngine for auto-selection
46//! let engine = CodecEngine::new();
47//! let result = engine.compress(content, Algorithm::M2M)?;
48//! let original = engine.decompress(&result.data)?;
49//! ```
50//!
51//! [`M2M`]: Algorithm::M2M
52//! [`TokenNative`]: Algorithm::TokenNative
53//! [`Brotli`]: Algorithm::Brotli
54//! [`None`]: Algorithm::None
55
56mod algorithm;
57mod brotli;
58mod dictionary;
59mod engine;
60pub mod m2m;
61mod m3;
62mod streaming;
63mod tables;
64mod token;
65mod token_native;
66
67pub use algorithm::{Algorithm, CompressionResult};
68pub use brotli::BrotliCodec;
69pub use dictionary::DictionaryCodec;
70pub use engine::{CodecEngine, ContentAnalysis};
71pub use m2m::{M2MCodec, M2MFrame};
72pub use m3::{M3ChatRequest, M3Codec, M3Message, M3_PREFIX};
73pub use streaming::{
74 SseEvent, StreamingCodec, StreamingDecompressor, StreamingMode, StreamingStats,
75};
76pub use tables::{
77 is_default_value, KEY_ABBREV, KEY_EXPAND, MODEL_ABBREV, MODEL_EXPAND, PATTERN_ABBREV,
78 PATTERN_EXPAND, ROLE_ABBREV, ROLE_EXPAND,
79};
80pub use token::TokenCodec;
81pub use token_native::TokenNativeCodec;
82
83/// Check if content is in M2M compressed format
84pub fn is_m2m_format(content: &str) -> bool {
85 content.starts_with("#M2M|1|") // M2M v1 format (default)
86 || content.starts_with("#TK|") // TokenNative
87 || content.starts_with("#M2M[v3.0]|") // Brotli
88}
89
90/// Detect the compression algorithm used in a message
91pub fn detect_algorithm(content: &str) -> Option<Algorithm> {
92 Algorithm::from_prefix(content)
93}