/*
* Ebb Memory Engine API
*
* Production cognitive memory engine and multi-transport gateway for autonomous AI agents. Features continuous mass-dilated logarithmic decay, Hebbian co-activation networks, off-heap mmap storage, and unified enterprise security. Canonical public HTTP contract for Ebb client SDKs. Generated from the hosted FastAPI application. Excludes internal and admin routes (`/api/v1/admin/_**`, `/metrics`, `/mcp`). Protected operations require `Authorization: Bearer <token>` (security scheme `HTTPBearer`). The same routes accept an optional `X-API-Key` header and `X-Ebb-Namespace` to select a namespace. Error responses use the `ErrorResponse` envelope (`code`, `message`, `details`) plus FastAPI's `HTTPValidationError` for 422. SDK subset of the Ebb HTTP contract. Includes Memory, Tasks, Events, and Health. Omits admin, runtime config, provider probes, registration, password reset, and SSO.
*
* The version of the OpenAPI document: 0.2.0
* Contact: maintainers@spectrayan.com
* Generated by: https://openapi-generator.tech
*/
use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};
/// struct for typed errors of method [`stream_events`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum StreamEventsError {
Status422(models::HttpValidationError),
UnknownValue(serde_json::Value),
}
/// Stream live memory and ingestion lifecycle events over SSE with Last-Event-ID replay.
pub async fn stream_events(configuration: &configuration::Configuration, last_event_id: Option<&str>, event_types: Option<Vec<String>>, namespace: Option<&str>, namespaces: Option<Vec<String>>, last_event_id2: Option<&str>, token: Option<&str>) -> Result<models::StreamEventsResponse200, Error<StreamEventsError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_query_last_event_id = last_event_id;
let p_query_event_types = event_types;
let p_query_namespace = namespace;
let p_query_namespaces = namespaces;
let p_header_last_event_id = last_event_id2;
let p_query_token = token;
let uri_str = format!("{}/api/v1/events/stream", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = p_query_last_event_id {
req_builder = req_builder.query(&[("last_event_id", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_event_types {
req_builder = match "multi" {
"multi" => req_builder.query(¶m_value.into_iter().map(|p| ("event_types".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("event_types", ¶m_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
}
if let Some(ref param_value) = p_query_namespace {
req_builder = req_builder.query(&[("namespace", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_namespaces {
req_builder = match "multi" {
"multi" => req_builder.query(¶m_value.into_iter().map(|p| ("namespaces".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("namespaces", ¶m_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
}
if let Some(ref param_value) = p_query_token {
req_builder = req_builder.query(&[("token", ¶m_value.to_string())]);
}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(param_value) = p_header_last_event_id {
req_builder = req_builder.header("Last-Event-ID", param_value.to_string());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let value = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.header("X-API-Key", value);
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::StreamEventsResponse200`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::StreamEventsResponse200`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<StreamEventsError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}