-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(llm): add real-time token streaming through provider chain #2872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from all commits
dc98e73
f3167ac
e1a776a
76b2ba6
becfbc8
2bc1127
d006d9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -625,6 +625,31 @@ impl<'a> LoopDelegate for ChatDelegate<'a> { | |
| } | ||
|
|
||
| let llm_call_start = std::time::Instant::now(); | ||
|
|
||
| // Wire up real-time token streaming to the channel layer. | ||
| // Bounded channel bounds memory usage when the consumer (channel | ||
| // layer) is slower than the LLM; producer drops chunks on overflow | ||
| // via `try_send`. | ||
| { | ||
| let (chunk_tx, mut chunk_rx) = | ||
| tokio::sync::mpsc::channel::<String>(256); | ||
| let channels = Arc::clone(&self.agent.channels); | ||
| let channel_name = self.message.channel.clone(); | ||
| let metadata = self.message.metadata.clone(); | ||
| tokio::spawn(async move { | ||
| while let Some(chunk) = chunk_rx.recv().await { | ||
| let _ = channels | ||
| .send_status( | ||
| &channel_name, | ||
| crate::channels::StatusUpdate::StreamChunk(chunk), | ||
| &metadata, | ||
| ) | ||
| .await; | ||
| } | ||
| }); | ||
| reason_ctx.chunk_sender = Some(chunk_tx); | ||
| } | ||
|
Comment on lines
+629
to
+651
|
||
|
|
||
| let output = match reasoning.respond_with_tools(reason_ctx).await { | ||
| Ok(output) => output, | ||
| Err(crate::error::LlmError::ContextLengthExceeded { used, limit }) => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -329,6 +329,26 @@ impl LlmProvider for FailoverProvider { | |
| Ok(response) | ||
| } | ||
|
|
||
| async fn complete_stream( | ||
| &self, | ||
| request: CompletionRequest, | ||
| on_chunk: &mut (dyn FnMut(String) + Send), | ||
| ) -> Result<CompletionResponse, LlmError> { | ||
| self.providers[self.last_used.load(Ordering::Relaxed)] | ||
| .complete_stream(request, on_chunk) | ||
| .await | ||
| } | ||
|
Comment on lines
+332
to
+340
|
||
|
|
||
| async fn complete_with_tools_stream( | ||
| &self, | ||
| request: ToolCompletionRequest, | ||
| on_chunk: &mut (dyn FnMut(String) + Send), | ||
| ) -> Result<ToolCompletionResponse, LlmError> { | ||
| self.providers[self.last_used.load(Ordering::Relaxed)] | ||
| .complete_with_tools_stream(request, on_chunk) | ||
| .await | ||
| } | ||
|
|
||
| fn active_model_name(&self) -> String { | ||
| self.providers[self.last_used.load(Ordering::Relaxed)].active_model_name() | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The streaming bridge uses a hard-coded channel capacity of
256. If this value is expected to be tuned (or kept consistent with other buffering limits), consider extracting it to a named constant or config to avoid a magic number here and make operational tuning easier.