Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(store_import): add import support for notifications, task sources, and devices

Steven Enamakel20c1eb1a1ac1
diff --git a/crates/openhuman-core/src/desktop/notifications/store_import.rs b/crates/openhuman-core/src/desktop/notifications/store_import.rs
new file mode 100644
index 0000000000..7a96fa34af
--- /dev/null
+++ b/crates/openhuman-core/src/desktop/notifications/store_import.rs
@@ -0,0 +1,108 @@
+//! One-shot import of the legacy `notifications.db` tables into the document
+//! layout (`store_documents.rs`).
+//!
+//! | Legacy table | Collection | Id |
+//! | --- | --- | --- |
+//! | `integration_notifications` | `integration_notifications` | row id |
+//! | `notification_settings` | `notification_settings` | provider |
+//! | `core_notifications` | `core_notifications` | `core_id(workspace, id)` |
+//!
+//! Documents come from the store's own `to_doc` and field names. The legacy
+//! `core_notifications` rows carry no workspace, so they take this
+//! workspace's directory, which is the one value a later read filters on.
+//! (The content-dedup window needs no import: it lasts a minute.) The driver
+//! ([`crate::storage::local`]) writes the documents and then renames the
+//! tables to `_legacy_<name>`.
+
+use anyhow::{Context, Result};
+use rusqlite::Connection;
+use serde_json::json;
+
+use super::store_documents::{core_id, to_doc, CORE, NOTIFICATIONS, SETTINGS};
+use crate::config::Config;
+use crate::storage::local::{table_exists, ImportDoc};
+
+/// The legacy tables this import retires.
+pub(super) const TABLES: &[&str] = &[
+ "integration_notifications",
+ "notification_settings",
+ "core_notifications",
+];
+
+/// Reads every legacy row.
+pub(super) fn read(config: &Config) -> Result<Vec<ImportDoc>> {
+ let workspace = config.workspace_dir.to_string_lossy().into_owned();
+ super::store::with_connection(config, |conn| read_rows(conn, &workspace))
+}
+
+fn read_rows(conn: &Connection, workspace: &str) -> Result<Vec<ImportDoc>> {
+ let mut docs = Vec::new();
+ if table_exists(conn, "integration_notifications")? {
+ let mut stmt = conn
+ .prepare(
+ "SELECT id, provider, account_id, title, body, raw_payload,
+ importance_score, triage_action, triage_reason, status,
+ received_at, scored_at
+ FROM integration_notifications ORDER BY rowid",
+ )
+ .context("[notifications::import] prepare integration_notifications")?;
+ let rows = stmt
+ .query(())
+ .context("[notifications::import] query integration_notifications")?;
+ for notification in super::store::rows_to_notifications(rows)? {
+ docs.push(ImportDoc {
+ collection: NOTIFICATIONS,
+ id: notification.id.clone(),
+ doc: to_doc(&notification),
+ });
+ }
+ }
+ if table_exists(conn, "notification_settings")? {
+ let mut stmt = conn
+ .prepare(
+ "SELECT provider, enabled, importance_threshold, route_to_orchestrator
+ FROM notification_settings ORDER BY rowid",
+ )
+ .context("[notifications::import] prepare notification_settings")?;
+ let rows = stmt.query_map([], |row| {
+ Ok(ImportDoc {
+ collection: SETTINGS,
+ id: row.get(0)?,
+ doc: json!({
+ "enabled": row.get::<_, i64>(1)? != 0,
+ "importance_threshold": row.get::<_, f64>(2)?,
+ "route_to_orchestrator": row.get::<_, i64>(3)? != 0,
+ }),
+ })
+ })?;
+ for row in rows {
+ docs.push(row.context("[notifications::import] decode a notification_settings row")?);
+ }
+ }
+ if table_exists(conn, "core_notifications")? {
+ let mut stmt = conn
+ .prepare(
+ "SELECT id, payload, timestamp_ms, read, created_at
+ FROM core_notifications ORDER BY rowid",
+ )
+ .context("[notifications::import] prepare core_notifications")?;
+ let rows = stmt.query_map([], |row| {
+ let id: String = row.get(0)?;
+ Ok(ImportDoc {
+ collection: CORE,
+ id: core_id(workspace, &id),
+ doc: json!({
+ "workspace": workspace,
+ "payload": row.get::<_, String>(1)?,
+ "timestamp_ms": row.get::<_, i64>(2)?,
+ "read": row.get::<_, i64>(3)? != 0,
+ "created_at": row.get::<_, String>(4)?,
+ }),
+ })
+ })?;
+ for row in rows {
+ docs.push(row.context("[notifications::import] decode a core_notifications row")?);
+ }
+ }
+ Ok(docs)
+}
diff --git a/crates/openhuman-core/src/integrations/task_sources/store_import.rs b/crates/openhuman-core/src/integrations/task_sources/store_import.rs
new file mode 100644
index 0000000000..e8e8e09998
--- /dev/null
+++ b/crates/openhuman-core/src/integrations/task_sources/store_import.rs
@@ -0,0 +1,92 @@
+//! One-shot import of the legacy `sources.db` tables into the document layout
+//! (`store_documents.rs`): `task_sources` becomes one `task_sources` document
+//! per source (the store's own `to_doc`) and `ingested_tasks` one
+//! `ingested_tasks` document per dedup-ledger entry, with `ingested_ms`
+//! derived from `ingested_at`. A source row that no longer decodes is logged
+//! and left in the retired table instead of stopping the import. The driver
+//! ([`crate::storage::local`]) writes the documents and then renames the
+//! tables to `_legacy_<name>`.
+
+use anyhow::{Context, Result};
+use chrono::DateTime;
+use rusqlite::Connection;
+use serde_json::{json, Map, Value};
+
+use super::store_documents::{ingested_id, to_doc, INGESTED, SOURCES};
+use crate::config::Config;
+use crate::storage::local::{table_exists, ImportDoc};
+
+/// The legacy tables this import retires.
+pub(super) const TABLES: &[&str] = &["task_sources", "ingested_tasks"];
+
+/// Reads every legacy row, after bringing an old schema forward.
+pub(super) fn read(config: &Config) -> Result<Vec<ImportDoc>> {
+ super::store::with_connection(config, read_rows)
+}
+
+fn read_rows(conn: &Connection) -> Result<Vec<ImportDoc>> {
+ let mut docs = Vec::new();
+ if table_exists(conn, "task_sources")? {
+ let mut stmt = conn
+ .prepare(&format!(
+ "{} ORDER BY created_at ASC, id ASC",
+ super::store::SELECT_SOURCE_COLUMNS
+ ))
+ .context("[task_sources::import] prepare task_sources")?;
+ let rows = stmt
+ .query_map([], super::store::map_source_row)
+ .context("[task_sources::import] query task_sources")?;
+ for row in rows {
+ match row {
+ Ok(source) => docs.push(ImportDoc {
+ collection: SOURCES,
+ id: source.id.clone(),
+ doc: to_doc(&source)?,
+ }),
+ Err(error) => tracing::warn!(
+ "[task_sources::import] skipping an undecodable source row: {error}"
+ ),
+ }
+ }
+ }
+ if table_exists(conn, "ingested_tasks")? {
+ let mut stmt = conn
+ .prepare(
+ "SELECT source_id, external_id, content_hash, title, payload, ingested_at
+ FROM ingested_tasks ORDER BY rowid",
+ )
+ .context("[task_sources::import] prepare ingested_tasks")?;
+ let rows = stmt
+ .query_map([], |row| {
+ let source_id: String = row.get(0)?;
+ let external_id: String = row.get(1)?;
+ let ingested_at: String = row.get(5)?;
+ let mut doc = Map::new();
+ doc.insert("source_id".into(), json!(source_id));
+ doc.insert("external_id".into(), json!(external_id));
+ doc.insert("content_hash".into(), json!(row.get::<_, String>(2)?));
+ if let Some(title) = row.get::<_, Option<String>>(3)? {
+ doc.insert("title".into(), json!(title));
+ }
+ if let Some(payload) = row.get::<_, Option<String>>(4)? {
+ doc.insert("payload".into(), json!(payload));
+ }
+ doc.insert(
+ "ingested_ms".into(),
+ json!(DateTime::parse_from_rfc3339(&ingested_at)
+ .map_or(0, |at| at.timestamp_millis())),
+ );
+ doc.insert("ingested_at".into(), json!(ingested_at));
+ Ok(ImportDoc {
+ collection: INGESTED,
+ id: ingested_id(&source_id, &external_id),
+ doc: Value::Object(doc),
+ })
+ })
+ .context("[task_sources::import] query ingested_tasks")?;
+ for row in rows {
+ docs.push(row.context("[task_sources::import] decode an ingested_tasks row")?);
+ }
+ }
+ Ok(docs)
+}
diff --git a/crates/openhuman-core/src/security/devices/store_import.rs b/crates/openhuman-core/src/security/devices/store_import.rs
new file mode 100644
index 0000000000..fa257fccd0
--- /dev/null
+++ b/crates/openhuman-core/src/security/devices/store_import.rs
@@ -0,0 +1,55 @@
+//! One-shot import of the legacy `devices.db` `paired_devices` table into the
+//! document layout (`store_documents.rs`): one `paired_devices` document per
+//! `channel_id`, with the columns as fields and `revoked` as a boolean.
+//! `shared_secret_encrypted` was never written by the store and is not
+//! carried over. The driver ([`crate::storage::local`]) writes the documents
+//! and then renames the table to `_legacy_paired_devices`.
+
+use anyhow::{Context, Result};
+use rusqlite::Connection;
+use serde_json::{json, Value};
+
+use super::store_documents::DEVICES;
+use crate::config::Config;
+use crate::storage::local::{table_exists, ImportDoc};
+
+/// The legacy tables this import retires.
+pub(super) const TABLES: &[&str] = &["paired_devices"];
+
+/// Reads every legacy row.
+pub(super) fn read(config: &Config) -> Result<Vec<ImportDoc>> {
+ super::store::with_connection(config, read_rows)
+}
+
+fn read_rows(conn: &Connection) -> Result<Vec<ImportDoc>> {
+ if !table_exists(conn, "paired_devices")? {
+ return Ok(Vec::new());
+ }
+ let mut stmt = conn
+ .prepare(
+ "SELECT channel_id, label, device_pubkey, core_session_token_hash,
+ created_at, last_seen_at, revoked
+ FROM paired_devices ORDER BY rowid",
+ )
+ .context("[devices::import] prepare paired_devices")?;
+ let rows = stmt
+ .query_map([], |row| {
+ Ok(ImportDoc {
+ collection: DEVICES,
+ id: row.get(0)?,
+ doc: json!({
+ "label": row.get::<_, String>(1)?,
+ "device_pubkey": row.get::<_, String>(2)?,
+ "core_session_token_hash": row.get::<_, String>(3)?,
+ "created_at": row.get::<_, String>(4)?,
+ "last_seen_at": row
+ .get::<_, Option<String>>(5)?
+ .map_or(Value::Null, Value::String),
+ "revoked": row.get::<_, i64>(6)? != 0,
+ }),
+ })
+ })
+ .context("[devices::import] query paired_devices")?;
+ rows.map(|row| row.context("[devices::import] decode a paired_devices row"))
+ .collect()
+}