Expand description
Multi-codec compression engine for M2M Protocol.
This module provides the core compression functionality with multiple algorithms optimized for different content types and sizes.
§Algorithms
| Algorithm | Wire Prefix | Best For |
|---|---|---|
M2M | #M2M|1| | All content (100% JSON fidelity) |
TokenNative | #TK| | Legacy token-based compression |
Brotli | #M2M[v3.0]|DATA: | Large repetitive content (>1KB) |
None | (passthrough) | Small content (<100 bytes) |
§M2M Wire Format v1
The new M2M wire format provides:
- 100% JSON fidelity: Original JSON is perfectly reconstructed
- Header extraction: Routing info available without decompression
- Cost estimation: Token counts and cost in headers
- Optional encryption: HMAC or AEAD security modes
§Wire Format Examples
// M2M v1 format (default)
#M2M|1|<fixed_header><routing_header><compressed_payload>
// Legacy formats (still supported for decoding)
#TK|C|<varint_tokens>
#M2M[v3.0]|DATA:<base64_brotli>§Usage
ⓘ
use m2m::codec::{CodecEngine, Algorithm};
use m2m::codec::m2m::M2MCodec;
// New M2M codec (recommended)
let m2m_codec = M2MCodec::new();
let encoded = m2m_codec.encode(json)?;
let decoded = m2m_codec.decode(&encoded)?; // 100% fidelity
// Or use CodecEngine for auto-selection
let engine = CodecEngine::new();
let result = engine.compress(content, Algorithm::M2M)?;
let original = engine.decompress(&result.data)?;Re-exports§
Modules§
- m2m
- M2M Wire Format v1 - Binary protocol with 100% JSON fidelity.
Structs§
- Brotli
Codec - Brotli codec
- Codec
Engine - Codec engine with automatic algorithm selection
- Compression
Result - Result of compression operation
- Content
Analysis - Content characteristics for algorithm selection
- Dictionary
Codec - Dictionary codec using pattern matching
- M3Chat
Request - Chat completion request in M3 format
- M3Codec
- M3 Codec for schema-aware compression
- M3Message
- A single chat message in M3 format
- Streaming
Codec - Streaming codec for SSE compression
- Streaming
Decompressor - Streaming decompressor for expanding abbreviated SSE
- Streaming
Stats - Statistics from streaming compression
- Token
Codec - Token compressor using pattern replacement and key/value abbreviation
- Token
Native Codec - Token-native compression codec
Enums§
- Algorithm
- Available compression algorithms
- SseEvent
- SSE event types
- Streaming
Mode - Streaming compression mode
Constants§
- M3_
PREFIX - M3 wire format prefix
Statics§
- KEY_
ABBREV - Key abbreviations (JSON keys -> short form)
- KEY_
EXPAND - Reverse key mapping (short form -> full key)
- MODEL_
ABBREV - Model name abbreviations
- MODEL_
EXPAND - Reverse model mapping
- PATTERN_
ABBREV - High-frequency patterns for token-efficient compression
- PATTERN_
EXPAND - Reverse pattern mapping for decompression
- ROLE_
ABBREV - Role abbreviations
- ROLE_
EXPAND - Reverse role mapping
Functions§
- detect_
algorithm - Detect the compression algorithm used in a message
- is_
default_ value - Check if a value is a default that can be removed
- is_
m2m_ format - Check if content is in M2M compressed format