feat: add save_item_from_mcp functionality to core services

Co-authored-by: aider (openai/andrew/openrouter/google/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-25 12:48:10 -03:00
parent da59401ca7
commit afe23aaa40
4 changed files with 128 additions and 92 deletions

View File

@@ -3,6 +3,7 @@ use crate::core::compression_service::CompressionService;
use crate::core::error::CoreError;
use crate::core::meta_service::MetaService;
use crate::core::types::{ItemWithContent, ItemWithMeta};
use crate::meta_plugin::{get_meta_plugin, MetaPlugin, MetaPluginType};
use crate::db::{self, Meta};
use crate::compression_engine::{get_compression_engine, CompressionType};
use crate::modes::common::settings_compression_type;
@@ -194,4 +195,69 @@ impl ItemService {
self.get_item(conn, item_id)
}
pub fn save_item_from_mcp(
&self,
content: &[u8],
tags: &Vec<String>,
metadata: &HashMap<String, String>,
conn: &mut Connection,
) -> Result<ItemWithMeta, CoreError> {
let compression_type = CompressionType::LZ4;
let compression_engine = get_compression_engine(compression_type.clone())?;
let tx = conn.transaction()?;
let item_id;
let mut item;
{
item = db::create_item(&tx, compression_type.clone())?;
item_id = item.id.unwrap();
// Add tags
for tag in tags {
db::add_tag(&tx, item_id, tag)?;
}
// Add custom metadata
for (key, value) in metadata {
db::add_meta(&tx, item_id, key, value)?;
}
}
let mut item_path = self.data_path.clone();
item_path.push(item_id.to_string());
let mut writer = compression_engine.create(item_path.clone())?;
writer.write_all(content)?;
drop(writer);
let plugin_types = vec![
MetaPluginType::FileMime,
MetaPluginType::FileEncoding,
MetaPluginType::Binary,
MetaPluginType::LineCount,
MetaPluginType::WordCount,
MetaPluginType::DigestSha256,
MetaPluginType::Uid,
MetaPluginType::User,
MetaPluginType::Hostname,
];
let mut plugins: Vec<Box<dyn MetaPlugin>> =
plugin_types.iter().map(|p| get_meta_plugin(p.clone())).collect();
self.meta_service
.initialize_plugins(&mut plugins, &tx, item_id);
self.meta_service
.process_chunk(&mut plugins, content, &tx);
self.meta_service.finalize_plugins(&mut plugins, &tx);
item.size = Some(content.len() as i64);
db::update_item(&tx, item.clone())?;
tx.commit()?;
self.get_item(conn, item_id)
}
}