Skip to content

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

feat(notifications): add document and import notification stores

Steven Enamakelef1f614a72bb
diff --git a/crates/openhuman-core/src/desktop/notifications/store_documents.rs b/crates/openhuman-core/src/desktop/notifications/store_documents.rs
index 56e2cbddd4..fd9b662eb1 100644
--- a/crates/openhuman-core/src/desktop/notifications/store_documents.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store_documents.rs
@@ -385,12 +385,12 @@ impl Docs {
reason: &str,
) -> Result<bool> {
let (score, action, reason) = (f64::from(score), action.to_string(), reason.to_string());
- let scored_at = Utc::now().to_rfc3339();
+ let scored_ms = Utc::now().timestamp_millis();
self.set(id, move |next| {
next["importance_score"] = json!(score);
next["triage_action"] = json!(action);
next["triage_reason"] = json!(reason);
- next["scored_at"] = json!(scored_at);
+ next["scored_ms"] = json!(scored_ms);
})
}
@@ -468,6 +468,7 @@ impl Docs {
pub(super) fn get_settings(&self, provider: &str) -> Result<NotificationSettings> {
let id = key_of(provider)?;
+ let provider = provider.to_string();
self.0.run(|docs| async move {
let defaults = NotificationSettings {
provider: provider.clone(),
@@ -490,7 +491,7 @@ impl Docs {
.get("route_to_orchestrator")
.and_then(Value::as_bool)
.unwrap_or(defaults.route_to_orchestrator),
- provider: provider.to_string(),
+ provider,
})
})
}
@@ -507,7 +508,6 @@ impl Docs {
.context("[notifications::store] serialize core notification failed")?,
"timestamp_ms": event.timestamp_ms,
"read": false,
- "created_at": Utc::now().to_rfc3339(),
});
self.0.run(|docs| async move {
match docs.put(CORE, &id, doc, Precondition::Absent).await {
diff --git a/crates/openhuman-core/src/desktop/notifications/store_import.rs b/crates/openhuman-core/src/desktop/notifications/store_import.rs
index 14a8ee16b8..1ee1d0e11b 100644
--- a/crates/openhuman-core/src/desktop/notifications/store_import.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store_import.rs
@@ -23,7 +23,7 @@ use serde_json::json;
mod store_rows;
use store_rows::rows_to_notifications;
-use super::store_documents::{core_id, to_doc, CORE, LOCAL_WORKSPACE, NOTIFICATIONS, SETTINGS};
+use super::storage_schema::{LEGACY_CORE, LEGACY_NOTIFICATIONS, LEGACY_SETTINGS};
use crate::config::Config;
use crate::storage::import::{table_exists, ImportDoc};
@@ -121,6 +121,40 @@ pub(crate) fn with_connection<T>(
f(&conn)
}
+/// The v1 core-notification document id: the workspace is length-prefixed.
+pub(crate) fn legacy_core_id(workspace: &str, event_id: &str) -> String {
+ format!("{}:{workspace}/{event_id}", workspace.len())
+}
+
+/// A notification in its v1 document shape (`received_at`, `scored_at`
+/// formatted times), which the owner's step converts.
+fn legacy_doc(n: &IntegrationNotification) -> serde_json::Value {
+ let mut doc = serde_json::Map::new();
+ doc.insert("provider".into(), json!(n.provider));
+ doc.insert("title".into(), json!(n.title));
+ doc.insert("body".into(), json!(n.body));
+ doc.insert("raw_payload".into(), json!(n.raw_payload.to_string()));
+ doc.insert("status".into(), json!(n.status.as_str()));
+ doc.insert("received_at".into(), json!(n.received_at.to_rfc3339()));
+ doc.insert(
+ "received_ms".into(),
+ json!(n.received_at.timestamp_millis()),
+ );
+ let optional = [
+ ("account_id", n.account_id.as_ref().map(|v| json!(v))),
+ ("importance_score", n.importance_score.map(|v| json!(v))),
+ ("triage_action", n.triage_action.as_ref().map(|v| json!(v))),
+ ("triage_reason", n.triage_reason.as_ref().map(|v| json!(v))),
+ ("scored_at", n.scored_at.map(|v| json!(v.to_rfc3339()))),
+ ];
+ for (field, value) in optional {
+ if let Some(value) = value {
+ doc.insert(field.into(), value);
+ }
+ }
+ serde_json::Value::Object(doc)
+}
+
/// Reads every legacy row.
pub(crate) fn read(config: &Config) -> Result<Vec<ImportDoc>> {
read_for(config, LOCAL_WORKSPACE)
@@ -148,9 +182,9 @@ fn read_rows(conn: &Connection, workspace: &str) -> Result<Vec<ImportDoc>> {
.context("[notifications::import] query integration_notifications")?;
for notification in rows_to_notifications(rows)? {
docs.push(ImportDoc {
- collection: NOTIFICATIONS,
+ collection: LEGACY_NOTIFICATIONS,
id: notification.id.clone(),
- doc: to_doc(&notification),
+ doc: legacy_doc(&notification),
});
}
}
@@ -163,7 +197,7 @@ fn read_rows(conn: &Connection, workspace: &str) -> Result<Vec<ImportDoc>> {
.context("[notifications::import] prepare notification_settings")?;
let rows = stmt.query_map([], |row| {
Ok(ImportDoc {
- collection: SETTINGS,
+ collection: LEGACY_SETTINGS,
id: row.get(0)?,
doc: json!({
"enabled": row.get::<_, i64>(1)? != 0,
@@ -186,8 +220,8 @@ fn read_rows(conn: &Connection, workspace: &str) -> Result<Vec<ImportDoc>> {
let rows = stmt.query_map([], |row| {
let id: String = row.get(0)?;
Ok(ImportDoc {
- collection: CORE,
- id: core_id(workspace, &id),
+ collection: LEGACY_CORE,
+ id: legacy_core_id(workspace, &id),
doc: json!({
"workspace": workspace,
"payload": row.get::<_, String>(1)?,