Skip to content
Merged
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
50 changes: 45 additions & 5 deletions core/bindings/node/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::types::*;
use dashmap::DashMap;
use engine_orchestrator::EngineOrchestrator;
use engine_orchestrator::search::FactId;
use engine_orchestrator::storage::{CandidateFactRow, EmbeddingRow, WriteAck};
use engine_orchestrator::storage::{CandidateFactRow, EmbeddingRow, RankedFact, WriteAck};
use napi::bindgen_prelude::*;
use napi::threadsafe_function::ThreadsafeFunction;
use napi_derive::napi;
Expand Down Expand Up @@ -138,12 +138,44 @@ impl MemoriEngine {
tokio::task::spawn_blocking(move || {
let req = serde_json::from_value(serde_json::to_value(&request).unwrap())
.map_err(|e| Error::from_reason(format!("Invalid retrieval request: {}", e)))?;
let results = inner
let results: Vec<RankedFact> = inner
.retrieve(req)
.map_err(|e| Error::from_reason(e.to_string()))?;
let napi_results: Vec<NapiRecallObject> =
serde_json::from_value(serde_json::to_value(&results).unwrap())
.map_err(|e| Error::from_reason(e.to_string()))?;
let napi_results = results
.into_iter()
.map(|r| {
let id = match r.id {
FactId::Int(n) => Either::A(n),
FactId::String(s) => Either::B(s),
};
let summaries = if r.summaries.is_empty() {
None
} else {
Some(
r.summaries
.into_iter()
.map(|s| NapiRecallSummary {
content: s["content"].as_str().unwrap_or("").to_string(),
date_created: s["date_created"]
.as_str()
.unwrap_or("")
.to_string(),
entity_fact_id: fact_id_from_json(&s["entity_fact_id"]),
fact_id: fact_id_from_json(&s["fact_id"]),
})
.collect(),
)
};
NapiRecallObject {
id,
content: r.content,
rank_score: Some(r.rank_score as f64),
similarity: Some(r.similarity as f64),
date_created: Some(r.date_created),
summaries,
}
})
.collect();
Ok(napi_results)
})
.await
Expand Down Expand Up @@ -200,3 +232,11 @@ impl MemoriEngine {
self.inner.shutdown();
}
}

fn fact_id_from_json(v: &serde_json::Value) -> Option<Either<i64, String>> {
match v {
serde_json::Value::Number(n) => n.as_i64().map(Either::A),
serde_json::Value::String(s) => Some(Either::B(s.clone())),
_ => None,
}
}
8 changes: 3 additions & 5 deletions core/bindings/node/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ pub struct NapiRetrievalRequest {
}

#[napi(object)]
#[derive(Serialize, Deserialize)]
pub struct NapiRecallSummary {
pub content: String,
pub date_created: String,
pub entity_fact_id: Option<i64>,
pub fact_id: Option<i64>,
pub entity_fact_id: Option<Either<i64, String>>,
pub fact_id: Option<Either<i64, String>>,
}

#[napi(object)]
#[derive(Serialize, Deserialize)]
pub struct NapiRecallObject {
pub id: i64,
pub id: Either<i64, String>,
pub content: String,
pub rank_score: Option<f64>,
pub similarity: Option<f64>,
Expand Down
4 changes: 2 additions & 2 deletions memori-ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion memori-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@memorilabs/memori",
"version": "0.1.16-beta",
"version": "0.1.17-beta",
"description": "The official TypeScript SDK for Memori",
"type": "module",
"main": "./dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions memori-ts/src/core/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ export class NativeEngine {
summaries: r.summaries?.map((s) => ({
content: s.content,
date_created: s.dateCreated,
entity_fact_id: s.entityFactId as number,
fact_id: s.factId as number,
entity_fact_id: s.entityFactId as number | string,
fact_id: s.factId as number | string,
})),
}));
}
Expand Down
6 changes: 3 additions & 3 deletions memori-ts/src/native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface NapiMessage {
}

export interface NapiRecallObject {
id: number;
id: number | string;
content: string;
rankScore?: number;
similarity?: number;
Expand All @@ -72,8 +72,8 @@ export interface NapiRecallObject {
export interface NapiRecallSummary {
content: string;
dateCreated: string;
entityFactId?: number;
factId?: number;
entityFactId?: number | string;
factId?: number | string;
}

export interface NapiRetrievalRequest {
Expand Down
12 changes: 6 additions & 6 deletions memori-ts/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface RetrievalRequest {
* @internal
*/
export interface RecallObject {
id: number;
id: number | string;
content: string;
rank_score?: number;
similarity?: number;
Expand All @@ -26,16 +26,16 @@ export interface RecallObject {
* Single row from the native N-API `retrieve` response (camelCase before mapping to `RecallObject`).
*/
export interface NapiRecallRow {
id: number;
id: number | string;
content: string;
rankScore?: number;
similarity?: number;
dateCreated?: string;
summaries?: Array<{
content: string;
dateCreated: string;
entityFactId?: number;
factId?: number;
entityFactId?: number | string;
factId?: number | string;
}>;
}

Expand All @@ -51,8 +51,8 @@ export type RecallItem = string | RecallObject;
export interface RecallSummary {
content: string;
date_created: string;
entity_fact_id: number;
fact_id: number;
entity_fact_id: number | string;
fact_id: number | string;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion memori-ts/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function formatSummariesFromFacts(facts: ParsedFact[]): string[] {
function attachRawSummariesToFacts(facts: RecallItem[], summaries: RecallSummary[]): RecallItem[] {
if (summaries.length === 0) return facts;

const summariesByFactId = new Map<number, RecallSummary[]>();
const summariesByFactId = new Map<number | string, RecallSummary[]>();

for (const summary of summaries) {
const summaryFactId = summary.entity_fact_id;
Expand Down
Loading