use std::collections::HashMap;
use std::pin::Pin;
use futures_util::Stream;
use serde_json::Value;
use crate::dialectic_stream::DialecticStream;
use crate::error::Result;
use crate::types::dialectic::{DialecticOptions, ReasoningLevel};
use crate::types::message::MessageSearchOptions;
use crate::types::pagination::Page;
use crate::types::peer::{PeerConfig, PeerContext};
use crate::types::session::{Session, SessionListOptions};
use super::conclusion::ConclusionScope;
use super::iter::{BlockingIter, collect_all_pages};
use super::runtime::block_on;
#[derive(Clone)]
pub struct Peer {
inner: crate::Peer,
}
impl std::fmt::Debug for Peer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Peer")
.field("id", &self.inner.id())
.finish()
}
}
impl Peer {
pub(crate) fn new(inner: crate::Peer) -> Self {
Self { inner }
}
#[must_use]
pub fn id(&self) -> &str {
self.inner.id()
}
#[must_use]
pub fn metadata(&self) -> Option<HashMap<String, Value>> {
self.inner.metadata()
}
#[must_use]
pub fn configuration(&self) -> Option<PeerConfig> {
self.inner.configuration()
}
pub fn refresh(&self) -> Result<()> {
block_on(self.inner.refresh())
}
pub fn get_metadata(&self) -> Result<HashMap<String, Value>> {
block_on(self.inner.get_metadata())
}
pub fn set_metadata(&self, metadata: HashMap<String, Value>) -> Result<()> {
block_on(self.inner.set_metadata(metadata))
}
pub fn get_configuration(&self) -> Result<PeerConfig> {
block_on(self.inner.get_configuration())
}
pub fn set_configuration(&self, config: &PeerConfig) -> Result<()> {
block_on(self.inner.set_configuration(config))
}
pub fn get_configuration_raw(&self) -> Result<HashMap<String, Value>> {
block_on(self.inner.get_configuration_raw())
}
pub fn set_configuration_raw(&self, config: HashMap<String, Value>) -> Result<()> {
block_on(self.inner.set_configuration_raw(config))
}
pub fn update(&self, metadata: HashMap<String, Value>) -> Result<()> {
block_on(self.inner.update(metadata))
}
pub fn chat(&self, query: &str) -> Result<Option<String>> {
block_on(self.inner.chat(query))
}
pub fn chat_with_options(&self, options: &DialecticOptions) -> Result<Option<String>> {
block_on(self.inner.chat_with_options(options))
}
#[must_use]
pub fn chat_stream(&self, query: impl Into<String>) -> BlockingChatStreamBuilder {
BlockingChatStreamBuilder {
inner: self.inner.chat_stream(query),
}
}
pub fn representation(&self) -> Result<String> {
block_on(self.inner.representation())
}
#[must_use]
pub fn representation_builder(&self) -> BlockingRepresentationBuilder {
BlockingRepresentationBuilder {
inner: self.inner.representation_builder(),
}
}
#[must_use]
pub fn context_builder(&self) -> BlockingContextBuilder {
BlockingContextBuilder {
inner: self.inner.context_builder(),
}
}
pub fn context(&self) -> Result<PeerContext> {
block_on(self.inner.context())
}
#[deprecated(since = "0.1.1", note = "use `Peer::context_builder()` instead")]
#[allow(deprecated)]
pub fn context_with_target(&self, target: &str) -> Result<PeerContext> {
block_on(self.inner.context_with_target(target))
}
#[deprecated(since = "0.1.1", note = "use `Peer::context_builder()` instead")]
#[allow(deprecated)]
pub fn context_with_options(
&self,
options: &crate::types::peer::PeerContextOptions,
) -> Result<PeerContext> {
block_on(self.inner.context_with_options(options))
}
pub fn sessions(&self) -> Result<Vec<Session>> {
block_on(async {
let page = self.inner.sessions().await?;
collect_all_pages(page).await
})
}
pub fn sessions_with_options(&self, options: &SessionListOptions) -> Result<Page<Session>> {
block_on(self.inner.sessions_with_options(options))
}
pub fn search(&self, query: &str) -> Result<Vec<crate::Message>> {
block_on(self.inner.search(query))
}
pub fn search_with_options(
&self,
options: &MessageSearchOptions,
) -> Result<Vec<crate::Message>> {
block_on(self.inner.search_with_options(options))
}
pub fn get_card(&self) -> Result<Option<Vec<String>>> {
block_on(self.inner.get_card())
}
pub fn get_card_with_target(&self, target: &str) -> Result<Option<Vec<String>>> {
block_on(self.inner.get_card_with_target(target))
}
pub fn set_card(&self, card: Vec<String>) -> Result<Option<Vec<String>>> {
block_on(self.inner.set_card(card))
}
pub fn set_card_with_target(
&self,
card: Vec<String>,
target: &str,
) -> Result<Option<Vec<String>>> {
block_on(self.inner.set_card_with_target(card, target))
}
#[must_use]
pub fn conclusions(&self) -> ConclusionScope {
ConclusionScope::new(self.inner.conclusions())
}
#[must_use]
pub fn conclusions_of(&self, target: impl Into<String>) -> ConclusionScope {
ConclusionScope::new(self.inner.conclusions_of(target))
}
#[must_use]
pub fn message(&self, content: impl Into<String>) -> crate::peer::MessageBuilder {
self.inner.message(content)
}
}
pub struct BlockingChatStreamBuilder {
inner: crate::peer::ChatStreamBuilder,
}
impl BlockingChatStreamBuilder {
#[must_use]
pub fn target(self, target: impl Into<String>) -> Self {
Self {
inner: self.inner.target(target),
}
}
#[must_use]
pub fn session(self, session_id: impl Into<String>) -> Self {
Self {
inner: self.inner.session(session_id),
}
}
#[must_use]
pub fn reasoning_level(self, level: ReasoningLevel) -> Self {
Self {
inner: self.inner.reasoning_level(level),
}
}
pub fn send(self) -> Result<ChatStreamIterator> {
let stream = block_on(self.inner.send())?;
Ok(ChatStreamIterator {
inner: BlockingIter::new(stream),
})
}
}
#[allow(clippy::type_complexity)]
pub struct ChatStreamIterator {
inner: BlockingIter<DialecticStream<Pin<Box<dyn Stream<Item = Result<String>> + Send>>>>,
}
impl ChatStreamIterator {
#[must_use]
pub fn final_response(&self) -> &crate::FinalResponse {
self.inner.stream().final_response()
}
#[must_use]
pub fn is_complete(&self) -> bool {
self.inner.stream().is_complete()
}
}
impl Iterator for ChatStreamIterator {
type Item = Result<String>;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
pub struct BlockingRepresentationBuilder {
inner: crate::peer::RepresentationBuilder,
}
impl BlockingRepresentationBuilder {
#[must_use]
pub fn session_id(self, val: impl Into<String>) -> Self {
Self {
inner: self.inner.session_id(val),
}
}
#[must_use]
pub fn target(self, val: impl Into<String>) -> Self {
Self {
inner: self.inner.target(val),
}
}
#[must_use]
pub fn search_query(self, val: impl Into<String>) -> Self {
Self {
inner: self.inner.search_query(val),
}
}
#[must_use]
pub fn search_top_k(self, val: u32) -> Self {
Self {
inner: self.inner.search_top_k(val),
}
}
#[must_use]
pub fn search_max_distance(self, val: f64) -> Self {
Self {
inner: self.inner.search_max_distance(val),
}
}
#[must_use]
pub fn include_most_frequent(self, val: bool) -> Self {
Self {
inner: self.inner.include_most_frequent(val),
}
}
#[must_use]
pub fn max_conclusions(self, val: u32) -> Self {
Self {
inner: self.inner.max_conclusions(val),
}
}
pub fn send(self) -> Result<String> {
block_on(self.inner.send())
}
}
pub struct BlockingContextBuilder {
inner: crate::peer::ContextBuilder,
}
impl BlockingContextBuilder {
#[must_use]
pub fn target(self, val: impl Into<String>) -> Self {
Self {
inner: self.inner.target(val),
}
}
#[must_use]
pub fn summary(self, val: bool) -> Self {
Self {
inner: self.inner.summary(val),
}
}
#[must_use]
pub fn limit_to_session(self, val: bool) -> Self {
Self {
inner: self.inner.limit_to_session(val),
}
}
#[must_use]
pub fn max_conclusions(self, val: u32) -> Self {
Self {
inner: self.inner.max_conclusions(val),
}
}
#[must_use]
pub fn search_query(self, val: impl Into<String>) -> Self {
Self {
inner: self.inner.search_query(val),
}
}
#[must_use]
pub fn search_top_k(self, val: u32) -> Self {
Self {
inner: self.inner.search_top_k(val),
}
}
#[must_use]
pub fn search_max_distance(self, val: f64) -> Self {
Self {
inner: self.inner.search_max_distance(val),
}
}
#[must_use]
pub fn include_most_frequent(self, val: bool) -> Self {
Self {
inner: self.inner.include_most_frequent(val),
}
}
pub fn send(self) -> Result<PeerContext> {
block_on(self.inner.send())
}
}
impl std::fmt::Debug for BlockingChatStreamBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BlockingChatStreamBuilder")
.finish_non_exhaustive()
}
}
impl std::fmt::Debug for ChatStreamIterator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ChatStreamIterator").finish_non_exhaustive()
}
}
impl std::fmt::Debug for BlockingRepresentationBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BlockingRepresentationBuilder")
.finish_non_exhaustive()
}
}
impl std::fmt::Debug for BlockingContextBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BlockingContextBuilder")
.finish_non_exhaustive()
}
}