feat: Stream chat message (#5498)

* chore: stream message

* chore: stream message

* chore: fix streaming

* chore: fix clippy
This commit is contained in:
Nathan.fooo
2024-06-09 14:02:32 +08:00
committed by GitHub
parent 94060a0a99
commit bb3e9d5bd8
46 changed files with 1691 additions and 870 deletions

View File

@ -8,7 +8,7 @@ edition = "2018"
crate-type = ["cdylib", "rlib"]
[dependencies]
chrono = { workspace = true, default-features = false, features = ["clock"] }
chrono = { workspace = true, default-features = false, features = ["clock"] }
bytes = { version = "1.5" }
pin-project = "1.1.3"
futures-core = { version = "0.3" }
@ -21,6 +21,8 @@ tempfile = "3.8.1"
validator = "0.16.0"
tracing.workspace = true
atomic_refcell = "0.1"
allo-isolate = { version = "^0.1", features = ["catch-unwind"], optional = true }
futures = "0.3.30"
[dev-dependencies]
rand = "0.8.5"
@ -28,7 +30,8 @@ futures = "0.3.30"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
zip = { version = "0.6.6", features = ["deflate"] }
brotli = { version = "3.4.0", optional = true }
brotli = { version = "3.4.0", optional = true }
[features]
compression = ["brotli"]
compression = ["brotli"]
isolate_flutter = ["allo-isolate"]

View File

@ -0,0 +1,44 @@
use allo_isolate::{IntoDart, Isolate};
use futures::Sink;
use pin_project::pin_project;
use std::pin::Pin;
use std::task::{Context, Poll};
#[pin_project]
pub struct IsolateSink {
isolate: Isolate,
}
impl IsolateSink {
pub fn new(isolate: Isolate) -> Self {
Self { isolate }
}
}
impl<T> Sink<T> for IsolateSink
where
T: IntoDart,
{
type Error = ();
fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
let this = self.project();
if this.isolate.post(item) {
Ok(())
} else {
Err(())
}
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
}

View File

@ -19,6 +19,8 @@ if_wasm! {
}
}
#[cfg(feature = "isolate_flutter")]
pub mod isolate_stream;
pub mod priority_task;
pub mod ref_map;
pub mod util;