use std::sync::Arc;
use futures::StreamExt;
use objectiveai_sdk::cli::output::{Handle, Notification, Output};
fn apply_agent_id_arg(
http: &mut objectiveai_sdk::HttpClient,
agent_id_arg: Option<String>,
) {
if http.agent_id.is_none() {
if let Some(id) = agent_id_arg {
http.agent_id = Some(Arc::new(id));
}
}
}
pub async fn call_unary<Req, Resp>(
cli_config: &crate::Config,
handle: &Handle,
method: reqwest::Method,
path: &str,
body: Option<Req>,
agent_id_arg: Option<String>,
) -> Result<(), crate::error::Error>
where
Req: serde::Serialize + Send,
Resp: serde::de::DeserializeOwned + serde::Serialize + Send + 'static,
{
let (_client, mut config) = crate::config::read(cli_config).await?;
let mut http = super::client::build_http_client(&mut config);
apply_agent_id_arg(&mut http, agent_id_arg);
let response: Resp = http.send_unary(method, path, body).await?;
Output::<Resp>::Notification(Notification { agent_id: None, value: response })
.emit(handle)
.await;
Ok(())
}
pub async fn call_unary_no_response<Req>(
cli_config: &crate::Config,
handle: &Handle,
method: reqwest::Method,
path: &str,
body: Option<Req>,
agent_id_arg: Option<String>,
) -> Result<(), crate::error::Error>
where
Req: serde::Serialize + Send,
{
let (_client, mut config) = crate::config::read(cli_config).await?;
let mut http = super::client::build_http_client(&mut config);
apply_agent_id_arg(&mut http, agent_id_arg);
http.send_unary_no_response(method, path, body).await?;
Output::<serde_json::Value>::Notification(Notification {
agent_id: None,
value: serde_json::Value::Null,
})
.emit(handle)
.await;
Ok(())
}
pub async fn call_streaming<Req, Chunk>(
cli_config: &crate::Config,
handle: &Handle,
method: reqwest::Method,
path: &str,
body: Option<Req>,
agent_id_arg: Option<String>,
) -> Result<(), crate::error::Error>
where
Req: serde::Serialize + Send,
Chunk: serde::de::DeserializeOwned + serde::Serialize + Send + 'static,
{
let (_client, mut config) = crate::config::read(cli_config).await?;
let mut http = super::client::build_http_client(&mut config);
apply_agent_id_arg(&mut http, agent_id_arg);
let stream = http
.send_streaming::<Chunk, _, _>(method, path.to_string(), body)
.await?;
let mut stream = std::pin::pin!(stream);
while let Some(result) = stream.next().await {
let chunk = result?;
Output::<Chunk>::Notification(Notification { agent_id: None, value: chunk })
.emit(handle)
.await;
}
Ok(())
}
pub async fn call_streaming_ws<Req, Chunk>(
cli_config: &crate::Config,
handle: &Handle,
method: reqwest::Method,
path: &str,
body: Req,
agent_id_arg: Option<String>,
) -> Result<(), crate::error::Error>
where
Req: serde::Serialize + Send + 'static,
Chunk: serde::de::DeserializeOwned + serde::Serialize + Send + 'static,
{
let (_client, mut config) = crate::config::read(cli_config).await?;
let mut http = super::client::build_http_client(&mut config);
apply_agent_id_arg(&mut http, agent_id_arg);
let conduit = super::conduit::build_handler(&mut config);
let (stream, _notifier) = http
.send_streaming_ws::<Chunk, _, _, _>(method, path.to_string(), body, conduit)
.await?;
let mut stream = std::pin::pin!(stream);
while let Some(result) = stream.next().await {
let chunk = result?;
Output::<Chunk>::Notification(Notification { agent_id: None, value: chunk })
.emit(handle)
.await;
}
Ok(())
}