Skip to content

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

feat(notifications): route store calls through document backend

Steven Enamakel09cd1fb09b4f
diff --git a/crates/openhuman-core/src/desktop/notifications/store.rs b/crates/openhuman-core/src/desktop/notifications/store.rs
index 36733b83a8..080e281991 100644
--- a/crates/openhuman-core/src/desktop/notifications/store.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store.rs
@@ -1,5 +1,8 @@
//! SQLite persistence for `IntegrationNotification` records.
//!
+//! With a storage backend configured ([`crate::storage`]) every function
+//! here is served from the document port instead (`store_documents.rs`).
+//!
//! Uses a synchronous `rusqlite::Connection` opened per call, following the
//! same `with_connection` pattern as the cron domain.
@@ -106,6 +109,9 @@ fn with_connection<T>(config: &Config, f: impl FnOnce(&Connection) -> Result<T>)
/// Persist a new notification to the store.
pub fn insert(config: &Config, n: &IntegrationNotification) -> Result<()> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.insert(n, false).map(|_| ());
+ }
with_connection(config, |conn| {
conn.execute(
"INSERT INTO integration_notifications
@@ -144,6 +150,9 @@ pub fn insert(config: &Config, n: &IntegrationNotification) -> Result<()> {
/// event is ignored (no duplicates). Returns `true` when a new row was written,
/// `false` when an event with the same id already existed.
pub fn insert_core_notification(config: &Config, event: &CoreNotificationEvent) -> Result<bool> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.insert_core_notification(event);
+ }
with_connection(config, |conn| {
let payload = serde_json::to_string(event)
.context("[notifications::store] serialize core notification failed")?;
@@ -172,6 +181,9 @@ pub fn list_core_notifications(
only_unread: bool,
limit: usize,
) -> Result<Vec<CoreNotificationEvent>> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.list_core_notifications(only_unread, limit);
+ }
with_connection(config, |conn| {
let sql = if only_unread {
"SELECT payload FROM core_notifications WHERE read = 0
@@ -207,6 +219,9 @@ pub fn list_core_notifications(
/// Mark a persisted core notification as read so it isn't re-surfaced on the
/// next sync-down. Returns `true` when a row was updated.
pub fn mark_core_notification_read(config: &Config, id: &str) -> Result<bool> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.mark_core_notification_read(id);
+ }
with_connection(config, |conn| {
let affected = conn
.execute(
@@ -220,6 +235,9 @@ pub fn mark_core_notification_read(config: &Config, id: &str) -> Result<bool> {
/// Count unread persisted core notifications.
pub fn unread_core_notification_count(config: &Config) -> Result<i64> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.unread_core_notification_count();
+ }
with_connection(config, |conn| {
let count: i64 = conn
.query_row(
@@ -236,6 +254,9 @@ pub fn unread_core_notification_count(config: &Config) -> Result<i64> {
///
/// Returns `true` when inserted, `false` when skipped as duplicate.
pub fn insert_if_not_recent(config: &Config, n: &IntegrationNotification) -> Result<bool> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.insert(n, true);
+ }
with_connection(config, |conn| {
conn.execute_batch("BEGIN IMMEDIATE")
.context("[notifications::store] begin insert_if_not_recent tx failed")?;
@@ -313,6 +334,9 @@ pub fn list(
provider_filter: Option<&str>,
min_score: Option<f32>,
) -> Result<Vec<IntegrationNotification>> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.list(limit, offset, provider_filter, min_score);
+ }
with_connection(config, |conn| {
// Build a dynamic query instead of relying on nullable-aware WHERE
// logic so the SQL stays readable for future contributors.
@@ -360,6 +384,13 @@ pub fn update_triage(
action: &str,
reason: &str,
) -> Result<()> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.update_triage(id, score, action, reason).map(|found| {
+ if !found {
+ tracing::warn!(id = %id, action = %action, "[notifications::store] update_triage matched no rows");
+ }
+ });
+ }
with_connection(config, |conn| {
let now = Utc::now().to_rfc3339();
let updated = conn
@@ -393,6 +424,13 @@ pub fn update_triage(
/// Transition a notification from `unread` to `read`.
pub fn mark_read(config: &Config, id: &str) -> Result<()> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.set_status(id, NotificationStatus::Read).map(|found| {
+ if !found {
+ tracing::warn!(id = %id, "[notifications::store] mark_read matched no rows");
+ }
+ });
+ }
with_connection(config, |conn| {
let updated = conn
.execute(
@@ -414,6 +452,9 @@ pub fn mark_read(config: &Config, id: &str) -> Result<()> {
/// Count unread notifications.
pub fn unread_count(config: &Config) -> Result<i64> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.unread_count();
+ }
with_connection(config, |conn| {
let count: i64 = conn
.query_row(
@@ -435,6 +476,9 @@ pub fn exists_recent(
title: &str,
body: &str,
) -> Result<bool> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.exists_recent(provider, account_id, title, body);
+ }
with_connection(config, |conn| {
let count: i64 = match account_id {
Some(aid) => conn.query_row(
@@ -463,6 +507,9 @@ pub fn exists_recent(
///
/// Returns `true` when at least one row matched and was updated.
pub fn mark_dismissed(config: &Config, id: &str) -> Result<bool> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.set_status(id, NotificationStatus::Dismissed);
+ }
with_connection(config, |conn| {
let updated = conn
.execute(
@@ -484,6 +531,9 @@ pub fn mark_dismissed(config: &Config, id: &str) -> Result<bool> {
///
/// Returns `true` when at least one row matched and was updated.
pub fn mark_acted(config: &Config, id: &str) -> Result<bool> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.set_status(id, NotificationStatus::Acted);
+ }
with_connection(config, |conn| {
let updated = conn
.execute(
@@ -503,6 +553,9 @@ pub fn mark_acted(config: &Config, id: &str) -> Result<bool> {
/// Return aggregate statistics for the notification intelligence pipeline.
pub fn stats(config: &Config) -> Result<super::types::NotificationStats> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.stats();
+ }
use std::collections::HashMap;
with_connection(config, |conn| {
let total: i64 = conn
@@ -587,6 +640,9 @@ pub fn stats(config: &Config) -> Result<super::types::NotificationStats> {
/// Upsert provider-level notification settings.
pub fn upsert_settings(config: &Config, settings: &NotificationSettings) -> Result<()> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.upsert_settings(settings);
+ }
with_connection(config, |conn| {
conn.execute(
"INSERT INTO notification_settings (provider, enabled, importance_threshold, route_to_orchestrator)
@@ -609,6 +665,9 @@ pub fn upsert_settings(config: &Config, settings: &NotificationSettings) -> Resu
/// Read provider-level notification settings with defaults when missing.
pub fn get_settings(config: &Config, provider: &str) -> Result<NotificationSettings> {
+ if let Some(docs) = super::store_documents::current()? {
+ return docs.get_settings(provider);
+ }
with_connection(config, |conn| {
let mut stmt = conn
.prepare(
diff --git a/crates/openhuman-core/src/desktop/notifications/store_documents.rs b/crates/openhuman-core/src/desktop/notifications/store_documents.rs
index aefbf8355f..7b70652b96 100644
--- a/crates/openhuman-core/src/desktop/notifications/store_documents.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store_documents.rs
@@ -235,16 +235,17 @@ impl Docs {
if limit == 0 {
return Ok(Vec::new());
}
- let mut filter = Filter::All;
- if let Some(provider) = provider {
- filter = filter.and(Filter::eq("provider", provider));
- }
- if let Some(score) = min_score {
- filter = filter.and(
+ let filter = [
+ provider.map(|provider| Filter::eq("provider", provider)),
+ min_score.map(|score| {
Filter::exists("importance_score", false)
- .or(Filter::gte("importance_score", f64::from(score))),
- );
- }
+ .or(Filter::gte("importance_score", f64::from(score)))
+ }),
+ ]
+ .into_iter()
+ .flatten()
+ .reduce(Filter::and)
+ .unwrap_or(Filter::All);
let query = Query::filter(filter)
.sort(Sort::desc("received_ms"))
.limit(offset.saturating_add(limit));