use bytes::Bytes;
use futures_util::stream::{Stream, StreamExt};
use serde::Deserialize;
use serde_json::Value;
use crate::client::Client;
use crate::datasets::{push_pagination, Pagination};
use crate::errors::{Error, Result};
use crate::transport::Request;
use crate::types::{
AppendMessageRequest, AskRequest, AskResponse, ConversationMessage, CreateSessionRequest,
IntelligenceSession, MessageList, Metadata, SessionList, SessionMessage,
};
#[derive(Debug, Default, Clone)]
pub struct AskOptions {
pub dataset_id: Option<Value>,
pub top_k: Option<u32>,
pub conversation_history: Option<Vec<ConversationMessage>>,
pub include_sources: Option<bool>,
}
impl AskOptions {
pub fn with_dataset<S: Into<String>>(mut self, dataset_id: S) -> Self {
self.dataset_id = Some(Value::String(dataset_id.into()));
self
}
pub fn with_all_datasets(mut self) -> Self {
self.dataset_id = Some(Value::String("all".into()));
self
}
pub fn with_top_k(mut self, top_k: u32) -> Self {
self.top_k = Some(top_k);
self
}
pub fn with_sources(mut self, include: bool) -> Self {
self.include_sources = Some(include);
self
}
pub fn with_history(mut self, history: Vec<ConversationMessage>) -> Self {
self.conversation_history = Some(history);
self
}
}
#[derive(Clone)]
pub struct IntelligenceService {
client: Client,
}
impl IntelligenceService {
pub(crate) fn new(client: Client) -> Self {
Self { client }
}
pub async fn ask<S: Into<String>>(&self, query: S) -> Result<AskResponse> {
self.ask_with(query.into(), AskOptions::default()).await
}
pub async fn ask_with<S: Into<String>>(
&self,
query: S,
options: AskOptions,
) -> Result<AskResponse> {
let request = AskRequest {
query: query.into(),
dataset_id: options.dataset_id,
top_k: options.top_k,
conversation_history: options.conversation_history,
stream: false,
include_sources: options.include_sources,
};
let body = serde_json::to_value(&request)?;
self.client
.dispatcher()
.json(Request {
method: "POST".into(),
path: "/intelligence/query".into(),
body: Some(body),
..Default::default()
})
.await
}
pub async fn stream<S: Into<String>>(
&self,
query: S,
options: AskOptions,
) -> Result<AskStream> {
let request = AskRequest {
query: query.into(),
dataset_id: options.dataset_id,
top_k: options.top_k,
conversation_history: options.conversation_history,
stream: true,
include_sources: options.include_sources,
};
let body = serde_json::to_value(&request)?;
let stream = self
.client
.dispatcher()
.stream(Request {
method: "POST".into(),
path: "/intelligence/query".into(),
body: Some(body),
stream: true,
..Default::default()
})
.await?;
Ok(AskStream::new(stream))
}
pub async fn create_session<R: Into<CreateSessionRequest>>(
&self,
request: R,
) -> Result<IntelligenceSession> {
let body = serde_json::to_value(request.into())?;
self.client
.dispatcher()
.json(Request {
method: "POST".into(),
path: "/intelligence/sessions".into(),
body: Some(body),
..Default::default()
})
.await
}
pub async fn list_sessions<P: Into<Pagination>>(&self, pagination: P) -> Result<SessionList> {
let (limit, _offset) = pagination.into().resolve();
let mut req = Request {
method: "GET".into(),
path: "/intelligence/sessions".into(),
..Default::default()
};
push_pagination(&mut req.query, limit, 0);
self.client.dispatcher().json(req).await
}
pub async fn get_session(&self, session_id: &str) -> Result<IntelligenceSession> {
self.client
.dispatcher()
.json(Request {
method: "GET".into(),
path: format!("/intelligence/sessions/{session_id}"),
..Default::default()
})
.await
}
pub async fn delete_session(&self, session_id: &str) -> Result<()> {
self.client
.dispatcher()
.empty(Request {
method: "DELETE".into(),
path: format!("/intelligence/sessions/{session_id}"),
..Default::default()
})
.await
}
pub async fn append_message<S: Into<String>, C: Into<String>>(
&self,
session_id: &str,
role: S,
content: C,
) -> Result<SessionMessage> {
self.append_message_with(
session_id,
AppendMessageRequest {
role: role.into(),
content: content.into(),
metadata: None,
},
)
.await
}
pub async fn append_message_with(
&self,
session_id: &str,
request: AppendMessageRequest,
) -> Result<SessionMessage> {
let body = serde_json::to_value(&request)?;
self.client
.dispatcher()
.json(Request {
method: "POST".into(),
path: format!("/intelligence/sessions/{session_id}/messages"),
body: Some(body),
..Default::default()
})
.await
}
pub async fn list_messages<P: Into<Pagination>>(
&self,
session_id: &str,
pagination: P,
) -> Result<MessageList> {
let (limit, _offset) = pagination.into().resolve();
let mut req = Request {
method: "GET".into(),
path: format!("/intelligence/sessions/{session_id}/messages"),
..Default::default()
};
push_pagination(&mut req.query, limit, 0);
self.client.dispatcher().json(req).await
}
}
#[derive(Debug, Default, Clone)]
pub struct StreamEvent {
pub event: String,
pub chunk_type: String,
pub content: String,
pub metadata: Option<Metadata>,
pub raw: Vec<u8>,
}
#[derive(Debug, Default, Deserialize)]
struct StreamPayload {
#[serde(default)]
chunk_type: String,
#[serde(default)]
content: String,
#[serde(default)]
metadata: Option<Metadata>,
}
type ByteStream = Box<dyn Stream<Item = std::result::Result<Bytes, reqwest::Error>> + Send + Unpin>;
pub struct AskStream {
inner: ByteStream,
buffer: Vec<u8>,
done: bool,
pending_event: String,
pending_data: Vec<String>,
}
impl AskStream {
fn new(inner: ByteStream) -> Self {
Self {
inner,
buffer: Vec::new(),
done: false,
pending_event: String::new(),
pending_data: Vec::new(),
}
}
pub async fn next_event(&mut self) -> Result<Option<StreamEvent>> {
loop {
if let Some(event) = self.try_take_event() {
return Ok(Some(event));
}
if self.done {
if !self.pending_data.is_empty() {
let data = std::mem::take(&mut self.pending_data).join("\n");
let event = std::mem::take(&mut self.pending_event);
return Ok(Some(decode_event(event, data.into_bytes())));
}
return Ok(None);
}
match self.inner.next().await {
Some(Ok(chunk)) => {
self.buffer.extend_from_slice(&chunk);
}
Some(Err(e)) => return Err(Error::Http(e)),
None => {
self.done = true;
if !self.buffer.is_empty() {
let line = std::mem::take(&mut self.buffer);
self.consume_line(&line);
}
}
}
}
}
fn try_take_event(&mut self) -> Option<StreamEvent> {
loop {
let newline_pos = self.buffer.iter().position(|b| *b == b'\n')?;
let line: Vec<u8> = self.buffer.drain(..=newline_pos).collect();
let mut end = line.len();
if end > 0 && line[end - 1] == b'\n' {
end -= 1;
}
if end > 0 && line[end - 1] == b'\r' {
end -= 1;
}
let line = &line[..end];
if line.is_empty() {
if !self.pending_data.is_empty() {
let data = std::mem::take(&mut self.pending_data).join("\n");
let event = std::mem::take(&mut self.pending_event);
return Some(decode_event(event, data.into_bytes()));
}
self.pending_event.clear();
continue;
}
self.consume_line(line);
}
}
fn consume_line(&mut self, line: &[u8]) {
if line.is_empty() {
return;
}
if line[0] == b':' {
return;
}
if let Some(colon) = line.iter().position(|b| *b == b':') {
let field = &line[..colon];
let mut rest = &line[colon + 1..];
if !rest.is_empty() && rest[0] == b' ' {
rest = &rest[1..];
}
match field {
b"event" => {
self.pending_event = String::from_utf8_lossy(rest).into_owned();
}
b"data" => {
self.pending_data
.push(String::from_utf8_lossy(rest).into_owned());
}
_ => {}
}
}
}
}
fn decode_event(event: String, raw: Vec<u8>) -> StreamEvent {
let payload: StreamPayload = serde_json::from_slice(&raw).unwrap_or_default();
StreamEvent {
event,
chunk_type: payload.chunk_type,
content: payload.content,
metadata: payload.metadata,
raw,
}
}