feat: add internal ZCLAW kernel crates to git tracking
This commit is contained in:
54
crates/zclaw-runtime/src/stream.rs
Normal file
54
crates/zclaw-runtime/src/stream.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
//! Streaming utilities
|
||||
|
||||
use tokio::sync::mpsc;
|
||||
use zclaw_types::Result;
|
||||
|
||||
/// Stream event for LLM responses
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum StreamEvent {
|
||||
/// Text delta received
|
||||
TextDelta(String),
|
||||
/// Thinking delta received
|
||||
ThinkingDelta(String),
|
||||
/// Tool use started
|
||||
ToolUseStart { id: String, name: String },
|
||||
/// Tool use input chunk
|
||||
ToolUseInput { id: String, chunk: String },
|
||||
/// Tool use completed
|
||||
ToolUseEnd { id: String, input: serde_json::Value },
|
||||
/// Response completed
|
||||
Complete { input_tokens: u32, output_tokens: u32 },
|
||||
/// Error occurred
|
||||
Error(String),
|
||||
}
|
||||
|
||||
/// Stream sender wrapper
|
||||
pub struct StreamSender {
|
||||
tx: mpsc::Sender<StreamEvent>,
|
||||
}
|
||||
|
||||
impl StreamSender {
|
||||
pub fn new(tx: mpsc::Sender<StreamEvent>) -> Self {
|
||||
Self { tx }
|
||||
}
|
||||
|
||||
pub async fn send_text(&self, delta: impl Into<String>) -> Result<()> {
|
||||
self.tx.send(StreamEvent::TextDelta(delta.into())).await.ok();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_thinking(&self, delta: impl Into<String>) -> Result<()> {
|
||||
self.tx.send(StreamEvent::ThinkingDelta(delta.into())).await.ok();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_complete(&self, input_tokens: u32, output_tokens: u32) -> Result<()> {
|
||||
self.tx.send(StreamEvent::Complete { input_tokens, output_tokens }).await.ok();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_error(&self, error: impl Into<String>) -> Result<()> {
|
||||
self.tx.send(StreamEvent::Error(error.into())).await.ok();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user