Skip to main content

just_deepseek/
stream.rs

1//! Stream type for DeepSeek chat completion SSE chunks.
2
3use std::{
4    fmt,
5    pin::Pin,
6    task::{Context, Poll},
7};
8
9use futures_core::Stream;
10use just_common::error::TransportError;
11use just_common::transport::sse::JsonEventStream;
12
13use crate::types::chat::ChatCompletionChunk;
14
15/// Stream of DeepSeek chat-completion SSE chunks.
16pub struct ChatCompletionStream {
17    inner: JsonEventStream<ChatCompletionChunk>,
18}
19
20impl ChatCompletionStream {
21    /// Creates a stream from an SSE HTTP response.
22    pub fn from_response(response: reqwest::Response) -> Result<Self, TransportError> {
23        Ok(Self {
24            inner: JsonEventStream::from_response(response)?,
25        })
26    }
27}
28
29impl fmt::Debug for ChatCompletionStream {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        f.debug_struct("ChatCompletionStream")
32            .finish_non_exhaustive()
33    }
34}
35
36impl Stream for ChatCompletionStream {
37    type Item = Result<ChatCompletionChunk, TransportError>;
38
39    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
40        Pin::new(&mut self.inner).poll_next(cx)
41    }
42}