Module codec

Module codec 

Source
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

AlgorithmWire PrefixBest 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§

pub use m2m::M2MCodec;
pub use m2m::M2MFrame;

Modules§

m2m
M2M Wire Format v1 - Binary protocol with 100% JSON fidelity.

Structs§

BrotliCodec
Brotli codec
CodecEngine
Codec engine with automatic algorithm selection
CompressionResult
Result of compression operation
ContentAnalysis
Content characteristics for algorithm selection
DictionaryCodec
Dictionary codec using pattern matching
M3ChatRequest
Chat completion request in M3 format
M3Codec
M3 Codec for schema-aware compression
M3Message
A single chat message in M3 format
StreamingCodec
Streaming codec for SSE compression
StreamingDecompressor
Streaming decompressor for expanding abbreviated SSE
StreamingStats
Statistics from streaming compression
TokenCodec
Token compressor using pattern replacement and key/value abbreviation
TokenNativeCodec
Token-native compression codec

Enums§

Algorithm
Available compression algorithms
SseEvent
SSE event types
StreamingMode
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