Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 50 additions & 59 deletions crates/synapse-infra/src/adapters/surrealdb_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
use surrealdb::engine::local::{Db, Mem, SurrealKv};
use surrealdb::sql::Thing;
use surrealdb::Surreal;
use surrealdb::opt::SurrealValue;
use synapse_core::{error::Error, error::Result, MemoryNode, MemoryPort, NodeType, SearchResult};
use std::sync::Arc;
use tokio::sync::OnceCell;
Expand All @@ -28,8 +29,10 @@ struct MemoryRecord {
loop_level: u8,
}

impl SurrealValue for MemoryRecord {}

/// Search result from SurrealDB vector query.
#[derive(Debug, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
struct VectorSearchResult {
id: Thing,
content: String,
Expand All @@ -46,6 +49,52 @@ struct VectorSearchResult {
distance: f32,
}

impl SurrealValue for VectorSearchResult {}

/// Internal record type for SurrealDB queries with ID.
#[derive(Debug, Serialize, Deserialize)]
struct RecordWithId {
id: Thing,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The PR description mentions considering RecordId instead of Thing for SurrealDB v3. While surrealdb::sql::Thing is still functional, surrealdb::RecordId offers a more type-safe and idiomatic representation for record IDs in modern SurrealDB usage. Migrating to RecordId here would improve type safety and future compatibility. This would involve changing the type of the id field.

Suggested change
id: Thing,
id: surrealdb::RecordId,

content: String,
layer: u8,
node_type: String,
created_at: i64,
updated_at: i64,
embedding: Vec<f32>,
metadata: String,
namespace: String,
source: String,
holographic_data: Option<Vec<u8>>,
loop_level: u8,
}

impl SurrealValue for RecordWithId {}

#[derive(Debug, Serialize, Deserialize)]
struct CountResult {
count: usize,
}

impl SurrealValue for CountResult {}

#[derive(Debug, Serialize, Deserialize)]
struct LayerResult {
id: Thing,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to RecordWithId, consider using surrealdb::RecordId instead of surrealdb::sql::Thing for the id field in LayerResult. This aligns with modern SurrealDB practices for improved type safety and consistency.

Suggested change
id: Thing,
id: surrealdb::RecordId,

content: String,
layer: u8,
node_type: String,
created_at: i64,
updated_at: i64,
embedding: Vec<f32>,
metadata: String,
namespace: String,
source: String,
holographic_data: Option<Vec<u8>>,
loop_level: u8,
}

impl SurrealValue for LayerResult {}

/// SurrealDB adapter for vector storage with graph support.
pub struct SurrealDbAdapter {
db: Arc<Surreal<Db>>,
Expand Down Expand Up @@ -392,22 +441,6 @@ impl MemoryPort for SurrealDbAdapter {
.await
.map_err(|e| Error::System(format!("Get by layer failed: {}", e)))?;

#[derive(Deserialize)]
struct LayerResult {
id: Thing,
content: String,
layer: u8,
node_type: String,
created_at: i64,
updated_at: i64,
embedding: Vec<f32>,
metadata: String,
namespace: String,
source: String,
holographic_data: Option<Vec<u8>>,
loop_level: u8,
}

let results: Vec<LayerResult> = response
.take(0)
.map_err(|e| Error::System(format!("Failed to parse layer results: {}", e)))?;
Expand Down Expand Up @@ -467,11 +500,6 @@ impl MemoryPort for SurrealDbAdapter {
.await
.map_err(|e| Error::System(format!("Count failed: {}", e)))?;

#[derive(Deserialize)]
struct CountResult {
count: usize,
}

let result: Option<CountResult> = response
.take(0)
.map_err(|e| Error::System(format!("Failed to parse count: {}", e)))?;
Expand Down Expand Up @@ -504,11 +532,6 @@ impl MemoryPort for SurrealDbAdapter {
.await
.map_err(|e| Error::System(format!("Count by layer failed: {}", e)))?;

#[derive(Deserialize)]
struct CountResult {
count: usize,
}

let result: Option<CountResult> = response
.take(0)
.map_err(|e| Error::System(format!("Failed to parse layer count: {}", e)))?;
Expand Down Expand Up @@ -542,22 +565,6 @@ impl MemoryPort for SurrealDbAdapter {
.await
.map_err(|e| Error::System(e.to_string()))?;

#[derive(Deserialize)]
struct RecordWithId {
id: Thing,
content: String,
layer: u8,
node_type: String,
created_at: i64,
updated_at: i64,
embedding: Vec<f32>,
metadata: String,
namespace: String,
source: String,
holographic_data: Option<Vec<u8>>,
loop_level: u8,
}

let record_result: surrealdb::Result<Option<RecordWithId>> = response.take(0);
let record = record_result.map_err(|e| Error::System(e.to_string()))?;

Expand Down Expand Up @@ -587,22 +594,6 @@ impl MemoryPort for SurrealDbAdapter {
let sql = "SELECT * FROM memory_node ORDER BY created_at DESC LIMIT 1";
let mut response = self.db.query(sql).await.map_err(|e| Error::System(e.to_string()))?;

#[derive(Deserialize)]
struct RecordWithId {
id: Thing,
content: String,
layer: u8,
node_type: String,
created_at: i64,
updated_at: i64,
embedding: Vec<f32>,
metadata: String,
namespace: String,
source: String,
holographic_data: Option<Vec<u8>>,
loop_level: u8,
}

let record_result: surrealdb::Result<Option<RecordWithId>> = response.take(0);
let record = record_result.map_err(|e| Error::System(e.to_string()))?;

Expand Down
18 changes: 12 additions & 6 deletions crates/synapse-infra/src/adapters/tokenomics_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use async_trait::async_trait;
use serde::Deserialize; // Serialize is unused
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use surrealdb::engine::local::{Db, Mem, SurrealKv};
use surrealdb::Surreal;
use surrealdb::opt::SurrealValue;
use tokio::sync::OnceCell;

use synapse_core::error::{Error, Result as SynapseResult};
use synapse_core::ports::TokenomicsRepository;
use synapse_core::tokenomics::structs::{RewardReceipt, UserProfile};

impl SurrealValue for UserProfile {}
impl SurrealValue for RewardReceipt {}

/// SurrealDB implementation of Tokenomics Persistence.
pub struct SurrealTokenomicsRepository {
db: Arc<Surreal<Db>>,
Expand Down Expand Up @@ -166,11 +170,6 @@ impl TokenomicsRepository for SurrealTokenomicsRepository {
.await
.map_err(|e| Error::System(format!("Failed to sum burned: {}", e)))?;

#[derive(Deserialize)]
struct SumResult {
total: u64,
}

let result: Option<SumResult> = response
.take(0)
.map_err(|e| Error::System(format!("Failed to parse sum: {}", e)))?;
Expand All @@ -179,6 +178,13 @@ impl TokenomicsRepository for SurrealTokenomicsRepository {
}
}

#[derive(Debug, Serialize, Deserialize)]
struct SumResult {
total: u64,
}

impl SurrealValue for SumResult {}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading