use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::model::{
deserialize_id, ColumnFilter, EntityProfile, EntityRelation, Explanation, Link, MemoryEdge,
MemoryNode, Recollection,
};
use crate::service::{canonical_entity_name, Metadata};
#[derive(Deserialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct RememberParams {
pub(super) fact: String,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) links: Vec<Link>,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) metadata: Option<Metadata>,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) ttl_seconds: Option<u64>,
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct RememberResult {
pub(super) id: u64,
pub(super) id_str: String,
}
#[derive(Deserialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct RecallParams {
pub(super) query: String,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) limit: Option<usize>,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) filter: Option<Metadata>,
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct RecollectionDto {
pub(super) id: u64,
pub(super) id_str: String,
pub(super) score: f32,
pub(super) content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) metadata: Option<Map<String, Value>>,
}
impl From<Recollection> for RecollectionDto {
fn from(memory: Recollection) -> Self {
Self {
id: memory.id,
id_str: memory.id.to_string(),
score: memory.score,
content: memory.content,
metadata: memory.metadata,
}
}
}
#[derive(Serialize, JsonSchema)]
pub(super) struct RecallResult {
pub(super) memories: Vec<RecollectionDto>,
}
impl RecallResult {
pub(super) fn new(memories: Vec<Recollection>) -> Self {
Self {
memories: memories.into_iter().map(RecollectionDto::from).collect(),
}
}
}
#[derive(Deserialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct RecallWhereParams {
pub(super) query: String,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) limit: Option<usize>,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) filters: Vec<ColumnFilter>,
}
#[derive(Deserialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct RecallFusedParams {
pub(super) query: String,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) limit: Option<usize>,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) filter: Option<Metadata>,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) hops: Option<usize>,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) graph_boost: Option<f64>,
pub(super) date_field: Option<String>,
}
#[derive(Serialize, JsonSchema)]
pub(super) struct RecallFusedResult {
pub(super) memories: Vec<RecollectionDto>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) dated_context: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) now: Option<String>,
}
impl RecallFusedResult {
pub(super) fn new(
memories: Vec<Recollection>,
dated_context: Option<String>,
now: Option<String>,
) -> Self {
Self {
memories: memories.into_iter().map(RecollectionDto::from).collect(),
dated_context,
now,
}
}
}
#[derive(Deserialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct RelateParams {
#[serde(deserialize_with = "deserialize_id")]
pub(super) from: u64,
#[serde(deserialize_with = "deserialize_id")]
pub(super) to: u64,
pub(super) relation: String,
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct RelateResult {
pub(super) edge_id: u64,
pub(super) edge_id_str: String,
}
#[derive(Deserialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct ForgetParams {
#[serde(deserialize_with = "deserialize_id")]
pub(super) id: u64,
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct ForgetResult {
pub(super) id: u64,
pub(super) id_str: String,
pub(super) found: bool,
}
#[derive(Deserialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct FeedbackParams {
#[serde(deserialize_with = "deserialize_id")]
pub(super) id: u64,
#[serde(deserialize_with = "super::wire::lenient")]
pub(super) success: bool,
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct FeedbackResult {
pub(super) id: u64,
pub(super) id_str: String,
pub(super) confidence: f32,
}
#[derive(Deserialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct WhyParams {
pub(super) decision: String,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) max_hops: Option<usize>,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) filter: Option<Metadata>,
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct MemoryNodeDto {
pub(super) id: u64,
pub(super) id_str: String,
pub(super) content: String,
pub(super) hop: usize,
}
impl From<MemoryNode> for MemoryNodeDto {
fn from(node: MemoryNode) -> Self {
Self {
id: node.id,
id_str: node.id.to_string(),
content: node.content,
hop: node.hop,
}
}
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct MemoryEdgeDto {
pub(super) from: u64,
pub(super) from_str: String,
pub(super) to: u64,
pub(super) to_str: String,
pub(super) relation: String,
}
impl From<MemoryEdge> for MemoryEdgeDto {
fn from(edge: MemoryEdge) -> Self {
Self {
from: edge.from,
from_str: edge.from.to_string(),
to: edge.to,
to_str: edge.to.to_string(),
relation: edge.relation,
}
}
}
#[derive(Deserialize, JsonSchema)]
pub(super) struct EntityParams {
pub(super) name: String,
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct EntityRelationDto {
pub(super) predicate: String,
pub(super) target_id: u64,
pub(super) target_id_str: String,
pub(super) target: String,
}
impl From<EntityRelation> for EntityRelationDto {
fn from(relation: EntityRelation) -> Self {
Self {
predicate: relation.predicate,
target_id: relation.target_id,
target_id_str: relation.target_id.to_string(),
target: relation.target,
}
}
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct EntityProfileDto {
pub(super) found: bool,
pub(super) id: u64,
pub(super) id_str: String,
pub(super) name: String,
pub(super) attributes: Metadata,
pub(super) relations: Vec<EntityRelationDto>,
}
impl EntityProfileDto {
pub(super) fn from_lookup(queried: &str, profile: Option<EntityProfile>) -> Self {
let Some(profile) = profile else {
return Self {
found: false,
id: 0,
id_str: "0".to_string(),
name: canonical_entity_name(queried),
attributes: Metadata::new(),
relations: Vec::new(),
};
};
Self {
found: true,
id: profile.id,
id_str: profile.id.to_string(),
name: profile.name,
attributes: profile.attributes,
relations: profile
.relations
.into_iter()
.map(EntityRelationDto::from)
.collect(),
}
}
}
#[derive(Serialize, JsonSchema)]
pub(super) struct ExplanationDto {
pub(super) nodes: Vec<MemoryNodeDto>,
pub(super) edges: Vec<MemoryEdgeDto>,
}
impl From<Explanation> for ExplanationDto {
fn from(explanation: Explanation) -> Self {
Self {
nodes: explanation
.nodes
.into_iter()
.map(MemoryNodeDto::from)
.collect(),
edges: explanation
.edges
.into_iter()
.map(MemoryEdgeDto::from)
.collect(),
}
}
}
#[derive(Deserialize, JsonSchema)]
pub(super) struct RememberExtractedParams {
pub(super) text: String,
#[serde(default, deserialize_with = "super::wire::lenient")]
pub(super) metadata: Option<Metadata>,
}
#[derive(Serialize, JsonSchema)]
#[schemars(transform = crate::schema::strip_int_formats)]
pub(super) struct RememberExtractedResult {
pub(super) ids: Vec<u64>,
pub(super) ids_str: Vec<String>,
}