conva_ai/
base.rs

1use std::pin::Pin;
2
3use crate::{hosts::ConvaAIHost, response::ConvaAIResponse};
4use async_trait::async_trait;
5use futures_util::Stream;
6use reqwest::Error;
7use serde_json::json;
8
9#[derive(Debug, Clone)]
10pub struct BaseClient {
11    pub copilot_id: String,
12    pub api_key: String,
13    pub copilot_version: String,
14    pub host: ConvaAIHost,
15    pub keep_conversation_history: bool,
16    pub domain: String,
17    pub history: String,
18}
19
20impl BaseClient {
21    pub fn new(
22        copilot_id: &str,
23        copilot_version: &str,
24        api_key: &str,
25        host: Option<ConvaAIHost>,
26    ) -> Self {
27        Self {
28            copilot_id: String::from(copilot_id),
29            api_key: String::from(api_key),
30            copilot_version: String::from(copilot_version),
31            host: match host {
32                Some(host) => host,
33                None => ConvaAIHost::Prod,
34            },
35            keep_conversation_history: true,
36            domain: String::from(""),
37            history: json!({}).to_string(),
38        }
39    }
40
41    pub fn use_history(&mut self, use_history: &bool) {
42        self.keep_conversation_history = *use_history;
43    }
44
45    pub fn clear_history(&mut self) {
46        self.history = "".to_string();
47    }
48}
49
50pub type EventStream = Pin<Box<dyn Stream<Item = Result<ConvaAIResponse, Error>> + Send>>;
51
52#[async_trait]
53pub trait AsyncConvaAI {
54    fn init(copilot_id: &str, copilot_version: &str, api_key: &str) -> Self;
55    fn new(
56        copilot_id: &str,
57        copilot_version: &str,
58        api_key: &str,
59        host: Option<ConvaAIHost>,
60    ) -> Self;
61    fn use_history(&mut self, use_history: &bool);
62    fn clear_history(&mut self);
63    async fn invoke_capability(
64        mut self,
65        query: String,
66        stream: bool,
67        capability_group: String,
68    ) -> Result<EventStream, Error>;
69}