The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(storage): add import support module for storage backends
Steven Enamakel81f095aa9c4f
diff --git a/crates/openhuman-core/src/modules/storage_import_support.rs b/crates/openhuman-core/src/modules/storage_import_support.rsnew file mode 100644index 0000000000..b698983d00--- /dev/null+++ b/crates/openhuman-core/src/modules/storage_import_support.rs@@ -0,0 +1,144 @@+//! What the one-time legacy importers (`storage_import_*`) share.+//!+//! Each importer moves a module's pre-storage files into the module's own+//! scope of the installed backend, once per source directory, never touching+//! the files. The shared parts are opening that scope, the host-only marker+//! that records a finished import, and a write that never replaces a record+//! the module has since written.++use std::path::Path;+use std::sync::Arc;++use anyhow::{anyhow, Result};+use serde_json::{json, Value};+use sha2::Digest;+use tinystoragedrivers::{CollectionSpec, DocumentStore, ErrorKind, Precondition, ScopedStorage};++/// The module's scope of the installed backend, or `None` when the process+/// runs without a storage backend (the module is then not using this storage).+pub(super) fn module_scope(module_id: &str) -> Result<Option<ScopedStorage>> {+ let Some(backend) = crate::storage::installed() else {+ return Ok(None);+ };+ let scope = super::storage_host::current_module_scope(module_id)+ .map_err(|error| anyhow!("resolve the module scope: {:?}", error.kind()))?;+ let scoped = backend+ .for_scope(&scope)+ .map_err(|error| anyhow!("open the module scope: {:?}", error.kind()))?;+ Ok(Some(scoped))+}++/// A stable marker id for one source path: `<prefix>@<16 hex of sha256(path)>`.+pub(super) fn marker_id(prefix: &str, source: &Path) -> String {+ let digest = sha2::Sha256::digest(source.as_os_str().as_encoded_bytes());+ format!("{prefix}@{}", &hex::encode(digest)[..16])+}++/// Declares `collection` (host-only: not in any module's grant) and reports+/// whether it already holds the marker `id`.+pub(super) async fn already_imported(+ docs: &Arc<dyn DocumentStore>,+ collection: &str,+ id: &str,+) -> Result<bool> {+ docs.ensure_collection(&CollectionSpec::new(collection))+ .await+ .map_err(|error| anyhow!("declare the marker collection: {:?}", error.kind()))?;+ Ok(docs+ .get(collection, id)+ .await+ .map_err(|error| anyhow!("read the marker: {:?}", error.kind()))?+ .is_some())+}++/// Records that the import of `id` finished. A marker another attempt wrote+/// first is left alone.+pub(super) async fn mark_imported(+ docs: &Arc<dyn DocumentStore>,+ collection: &str,+ id: &str,+ mut summary: Value,+) -> Result<()> {+ summary["at"] = json!(now_ms());+ match docs+ .put(collection, id, summary, Precondition::Absent)+ .await+ {+ Ok(_) => Ok(()),+ Err(error) if matches!(error.kind(), ErrorKind::Conflict | ErrorKind::AlreadyExists) => {+ Ok(())+ }+ Err(error) => Err(anyhow!("write the marker: {:?}", error.kind())),+ }+}++/// Writes `doc` only when `id` is absent; `true` when it was written.+pub(super) async fn put_absent(+ docs: &Arc<dyn DocumentStore>,+ collection: &str,+ id: &str,+ doc: Value,+) -> Result<bool> {+ match docs.put(collection, id, doc, Precondition::Absent).await {+ Ok(_) => Ok(true),+ Err(error) if matches!(error.kind(), ErrorKind::Conflict | ErrorKind::AlreadyExists) => {+ Ok(false)+ }+ Err(error) => Err(anyhow!("write {collection}: {:?}", error.kind())),+ }+}++/// Wall clock in milliseconds since the Unix epoch.+pub(super) fn now_ms() -> u64 {+ std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .unwrap_or_default()+ .as_millis()+ .min(u128::from(u64::MAX)) as u64+}++/// Parses the non-empty lines of a JSONL file; a line that does not parse is+/// skipped (and counted) rather than failing the whole file.+pub(super) fn parse_jsonl(text: &str) -> (Vec<Value>, usize) {+ let mut values = Vec::new();+ let mut skipped = 0;+ for line in text.lines().map(str::trim).filter(|line| !line.is_empty()) {+ match serde_json::from_str::<Value>(line) {+ Ok(value) if value.is_object() => values.push(value),+ _ => skipped += 1,+ }+ }+ (values, skipped)+}++/// Appends `values` to an empty `stream` in batches; a stream that already+/// holds records is left alone so a retry cannot duplicate them. Returns the+/// number appended.+pub(super) async fn append_if_empty(+ scoped: &ScopedStorage,+ stream: &str,+ values: Vec<Value>,+) -> Result<usize> {+ const BATCH: usize = 500;+ if values.is_empty() {+ return Ok(0);+ }+ let streams = scoped.streams();+ let held = streams+ .len(stream)+ .await+ .map_err(|error| anyhow!("read stream length: {:?}", error.kind()))?;+ if held != 0 {+ return Ok(0);+ }+ let total = values.len();+ let mut rest = values;+ while !rest.is_empty() {+ let tail = rest.split_off(rest.len().min(BATCH));+ streams+ .append_batch(stream, std::mem::replace(&mut rest, tail))+ .await+ .map_err(|error| anyhow!("append to a stream: {:?}", error.kind()))?;+ }+ Ok(total)+}