use std::error::Error;
use std::fmt;
use async_trait::async_trait;
use gadk_genai::{Blob, Content};
use crate::LlmResponse;
#[derive(Debug, Clone, PartialEq)]
pub enum RealtimeInput {
Blob(Blob),
ActivityStart,
ActivityEnd,
AudioStreamEnd,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LiveConnErrorKind {
ConnectionLost,
Closed {
code: u16,
},
Other,
}
#[derive(Debug)]
pub struct LiveConnError {
pub kind: LiveConnErrorKind,
pub message: String,
pub source: Option<Box<dyn Error + Send + Sync>>,
}
const RESUMABLE_TEXT: [&str; 4] = ["broken pipe", "connection reset", "EOF", "GoAway"];
const CLOSE_POLICY_VIOLATION: u16 = 1008;
impl LiveConnError {
pub fn new(kind: LiveConnErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
source: None,
}
}
pub fn with_source(mut self, source: impl Into<Box<dyn Error + Send + Sync>>) -> Self {
self.source = Some(source.into());
self
}
pub fn eof() -> Self {
Self::new(LiveConnErrorKind::ConnectionLost, "EOF")
}
pub fn is_resumable(&self) -> bool {
match self.kind {
LiveConnErrorKind::ConnectionLost => true,
LiveConnErrorKind::Closed { code } if code == CLOSE_POLICY_VIOLATION => true,
_ => RESUMABLE_TEXT.iter().any(|t| self.message.contains(t)),
}
}
}
impl fmt::Display for LiveConnError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl Error for LiveConnError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.source.as_deref().map(|e| e as &(dyn Error + 'static))
}
}
#[async_trait]
pub trait LiveConnection: Send + Sync {
async fn send_history(&self, history: &[Content]) -> Result<(), LiveConnError>;
async fn send_content(&self, content: Content) -> Result<(), LiveConnError>;
async fn send_realtime(&self, input: RealtimeInput) -> Result<(), LiveConnError>;
async fn recv(&self) -> Result<Option<LlmResponse>, LiveConnError>;
async fn close(&self) -> Result<(), LiveConnError>;
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use std::any::Any;
use std::error::Error;
use futures::stream::BoxStream;
use gadk_genai::LiveConnectConfig;
use super::{LiveConnError, LiveConnErrorKind};
use crate::{Llm, LlmRequest, LlmResponse, ModelError};
struct NoLive;
impl Llm for NoLive {
fn name(&self) -> &str {
"no-live"
}
fn generate_content(
&self,
_req: LlmRequest,
_stream: bool,
) -> BoxStream<'static, Result<LlmResponse, ModelError>> {
Box::pin(futures::stream::empty())
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[tokio::test]
async fn live_default_connect_live_is_unsupported() {
let m: &dyn Llm = &NoLive;
let Err(err) = m.connect_live(LiveConnectConfig::default()).await else {
panic!("the default connect_live must fail");
};
assert!(matches!(err, ModelError::Other(_)), "{err:?}");
assert_eq!(err.to_string(), "model does not support live connection");
}
fn other(msg: &str) -> LiveConnError {
LiveConnError::new(LiveConnErrorKind::Other, msg)
}
#[test]
fn live_is_resumable_follows_go_is_resumable() {
let cases: Vec<(&str, LiveConnError, bool)> = vec![
("eof", LiveConnError::eof(), true),
(
"connection lost",
LiveConnError::new(
LiveConnErrorKind::ConnectionLost,
"failed to send message: IO error: x",
),
true,
),
(
"close 1008",
LiveConnError::new(
LiveConnErrorKind::Closed { code: 1008 },
"websocket: close 1008 (policy violation)",
),
true,
),
(
"close 1002",
LiveConnError::new(
LiveConnErrorKind::Closed { code: 1002 },
"websocket: close 1002 (protocol error): fatal",
),
false,
),
(
"close 1002 with EOF in reason",
LiveConnError::new(
LiveConnErrorKind::Closed { code: 1002 },
"websocket: close 1002 (protocol error): unexpected EOF",
),
true,
),
(
"close 1002 with 1008 in reason",
LiveConnError::new(
LiveConnErrorKind::Closed { code: 1002 },
"websocket: close 1002 (protocol error): error 1008",
),
false,
),
("broken pipe text", other("write: broken pipe"), true),
(
"connection reset text",
other("read: connection reset by peer"),
true,
),
(
"EOF text",
other(
"failed to receive message: websocket: close 1006 (abnormal closure): unexpected EOF",
),
true,
),
("GoAway text", other("GoAway received"), true),
(
"1008 in a port is not a code",
other(
"failed to receive message: read tcp 127.0.0.1:41008->127.0.0.1:42207: i/o timeout",
),
false,
),
(
"application error",
other("model refused the request"),
false,
),
];
for (name, err, want) in cases {
assert_eq!(err.is_resumable(), want, "{name}: {err}");
}
}
#[test]
fn live_display_is_message_and_source_is_kept() {
let src = std::io::Error::new(std::io::ErrorKind::BrokenPipe, "pipe");
let err = LiveConnError::new(
LiveConnErrorKind::ConnectionLost,
"failed to send message: pipe",
)
.with_source(src);
assert_eq!(err.to_string(), "failed to send message: pipe");
assert_eq!(err.kind, LiveConnErrorKind::ConnectionLost);
let source = err.source().expect("source kept");
assert!(source.downcast_ref::<std::io::Error>().is_some());
let eof = LiveConnError::eof();
assert_eq!(
(eof.kind, eof.message.as_str()),
(LiveConnErrorKind::ConnectionLost, "EOF")
);
assert!(eof.source.is_none());
}
}