use super::types::{Document, KnowledgeStats, ListOptions, SearchOptions, SearchResult};
use crate::error::ForgeError;
use async_trait::async_trait;
#[async_trait]
pub trait KnowledgeClient: Send + Sync {
async fn search(
&self,
query: &str,
options: SearchOptions,
) -> Result<Vec<SearchResult>, ForgeError>;
async fn upload(&self, documents: Vec<Document>) -> Result<Vec<String>, ForgeError>;
async fn get(&self, id: &str) -> Result<Option<Document>, ForgeError>;
async fn list(&self, options: ListOptions) -> Result<Vec<Document>, ForgeError>;
async fn delete(&self, id: &str) -> Result<bool, ForgeError>;
async fn update(&self, document: Document) -> Result<bool, ForgeError>;
async fn stats(&self) -> Result<KnowledgeStats, ForgeError>;
async fn clear(&self, namespace: Option<&str>) -> Result<usize, ForgeError>;
}
pub struct SyncKnowledgeClient<C: KnowledgeClient> {
inner: C,
runtime: tokio::runtime::Runtime,
}
impl<C: KnowledgeClient> SyncKnowledgeClient<C> {
pub fn new(client: C) -> Result<Self, ForgeError> {
let runtime = tokio::runtime::Runtime::new()
.map_err(|e| ForgeError::internal(format!("Failed to create runtime: {}", e)))?;
Ok(Self {
inner: client,
runtime,
})
}
pub fn search(
&self,
query: &str,
options: SearchOptions,
) -> Result<Vec<SearchResult>, ForgeError> {
self.runtime.block_on(self.inner.search(query, options))
}
pub fn upload(&self, documents: Vec<Document>) -> Result<Vec<String>, ForgeError> {
self.runtime.block_on(self.inner.upload(documents))
}
pub fn get(&self, id: &str) -> Result<Option<Document>, ForgeError> {
self.runtime.block_on(self.inner.get(id))
}
pub fn list(&self, options: ListOptions) -> Result<Vec<Document>, ForgeError> {
self.runtime.block_on(self.inner.list(options))
}
pub fn delete(&self, id: &str) -> Result<bool, ForgeError> {
self.runtime.block_on(self.inner.delete(id))
}
pub fn update(&self, document: Document) -> Result<bool, ForgeError> {
self.runtime.block_on(self.inner.update(document))
}
pub fn stats(&self) -> Result<KnowledgeStats, ForgeError> {
self.runtime.block_on(self.inner.stats())
}
pub fn clear(&self, namespace: Option<&str>) -> Result<usize, ForgeError> {
self.runtime.block_on(self.inner.clear(namespace))
}
pub fn inner(&self) -> &C {
&self.inner
}
}