Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Compress context
let context = "Your large LLM context here...";
let compressor = ZlibCompressor;
let module = MemoryModule::new(context, &compressor).await?;
let module = MemoryModule::new(context, &compressor, None).await?;

// Serialize
let json = module.to_json()?;
Expand Down
31 changes: 23 additions & 8 deletions src/compression.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
use super::{Compressor, Expander, StreamlinerError};
use flate2::{Compression, write::ZlibEncoder, read::ZlibDecoder};
use std::io::{Write, Read};
use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression};
use std::io::{Read, Write};

/// Zlib-based compression implementation
pub struct ZlibCompressor;

#[async_trait::async_trait]
impl Compressor for ZlibCompressor {
async fn compress(&'async_trait self, context: &'async_trait str) -> Result<Vec<u8>, StreamlinerError> {
async fn compress(
&'async_trait self,
context: &'async_trait str,
) -> Result<Vec<u8>, StreamlinerError> {
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
encoder.write_all(context.as_bytes())?;
Ok(encoder.finish()?)
}

fn algorithm(&self) -> &'static str {
"zlib"
}
}

/// Zlib-based expansion implementation
pub struct ZlibExpander;

#[async_trait::async_trait]
impl Expander for ZlibExpander {
async fn expand(&'async_trait self, compressed: &'async_trait [u8]) -> Result<String, StreamlinerError> {
async fn expand(
&'async_trait self,
compressed: &'async_trait [u8],
) -> Result<String, StreamlinerError> {
let mut decoder = ZlibDecoder::new(compressed);
let mut output = String::new();
decoder.read_to_string(&mut output)
decoder
.read_to_string(&mut output)
.map_err(|e| StreamlinerError::ExpansionError(e.to_string()))?;
Ok(output)
}

fn algorithm(&self) -> &'static str {
"zlib"
}
}

#[cfg(test)]
Expand All @@ -37,10 +52,10 @@ mod tests {
let compressor = ZlibCompressor;
let expander = ZlibExpander;
let original = "test context";

let compressed = compressor.compress(original).await.unwrap();
let expanded = expander.expand(&compressed).await.unwrap();

assert_eq!(original, expanded);
}
}
}
Loading