feat(backend): modèle unifié streaming/progress/events provider-agnostic + pont app-tauri — foundation #156 (QA verte)

This commit is contained in:
2026-08-06 13:13:17 +02:00
parent 40fa551f32
commit 918116664c
14 changed files with 630 additions and 59 deletions

View File

@ -30,7 +30,9 @@ use backend::stream::ReplayOutputBridge;
use tauri::ipc::Channel;
use domain::ids::SessionId;
use domain::ports::ReplyEvent;
use domain::ports::{
ReplyEvent, ReplyProgress, ReplyProgressKind, ReplyProgressSource, ReplyProgressStage,
};
use crate::dto::ReplyChunk;
use crate::stream::TauriChannelSink;
@ -136,6 +138,13 @@ fn reply_chunk_bytes(chunk: &ReplyChunk) -> usize {
ReplyChunk::UserPrompt { text } => text.len(),
ReplyChunk::TextDelta { text } => text.len(),
ReplyChunk::ToolActivity { label } => label.len(),
ReplyChunk::Progress { progress } => {
progress.label.len()
+ progress.text.as_ref().map_or(0, String::len)
+ progress.provider.as_ref().map_or(0, String::len)
+ progress.native_event.as_ref().map_or(0, String::len)
+ progress.tool_name.as_ref().map_or(0, String::len)
}
ReplyChunk::Final { content } => content.len(),
ReplyChunk::Error { message } => message.len(),
}
@ -154,8 +163,20 @@ fn reply_chunk_bytes(chunk: &ReplyChunk) -> usize {
#[must_use]
pub fn chunk_from_event(event: ReplyEvent) -> Option<ReplyChunk> {
match event {
ReplyEvent::Progress { progress } => Some(ReplyChunk::Progress {
progress: progress.into(),
}),
ReplyEvent::TextDelta { text } => Some(ReplyChunk::TextDelta { text }),
ReplyEvent::ToolActivity { label } => Some(ReplyChunk::ToolActivity { label }),
ReplyEvent::ToolActivity { label } => Some(ReplyChunk::Progress {
progress: ReplyProgress::new(
ReplyProgressSource::ProviderNative,
ReplyProgressKind::Tool,
ReplyProgressStage::Info,
label.clone(),
)
.with_tool_name(label)
.into(),
}),
ReplyEvent::Error { message } => Some(ReplyChunk::Error { message }),
ReplyEvent::Final { content } => {
if content.trim().is_empty() {
@ -166,7 +187,16 @@ pub fn chunk_from_event(event: ReplyEvent) -> Option<ReplyChunk> {
Some(ReplyChunk::Final { content })
}
}
ReplyEvent::Announcement { .. } => None,
ReplyEvent::Announcement { text } => Some(ReplyChunk::Progress {
progress: ReplyProgress::new(
ReplyProgressSource::ProviderNative,
ReplyProgressKind::Message,
ReplyProgressStage::Delta,
"message intermédiaire",
)
.with_text(text)
.into(),
}),
ReplyEvent::Heartbeat => None,
ReplyEvent::RateLimited { .. } => None,
}

View File

@ -2195,14 +2195,32 @@ pub async fn agent_send(
},
);
// Open the turn stream with a live progress tap. The returned `ReplyStream`
// remains the authoritative drain to `Final`; the tap is best-effort
// observability for provider-native/local progress produced while `send` is
// still running.
let (tap_tx, tap_rx) = std::sync::mpsc::channel();
let tap_bridge = std::sync::Arc::clone(&state.chat_bridge);
let tap_sid = sid;
let tap_pump = std::thread::spawn(move || {
for event in tap_rx {
let Some(chunk) = crate::chat::chunk_from_event(event) else {
continue;
};
if matches!(chunk, ReplyChunk::Final { .. } | ReplyChunk::Error { .. }) {
continue;
}
let _ = tap_bridge.send_output(&tap_sid, chunk);
}
});
// Open the turn stream. A start failure leaves the just-registered channel in
// place (the cell stays attached, ready for a retry) — mirrors the PTY pump,
// which only unregisters on a hard subscribe failure; here the session is
// still live, so we keep the attach and surface the error.
let stream = session
.send(&prompt_for_model)
.await
.map_err(|e| ErrorDto::from(AppError::from(e)))?;
let stream_result = session.send_with_tap(&prompt_for_model, tap_tx).await;
let _ = tap_pump.join();
let stream = stream_result.map_err(|e| ErrorDto::from(AppError::from(e)))?;
// Drain the blocking reply iterator on a dedicated OS thread (the stream is a
// synchronous `Iterator`, exactly like the PTY byte stream). It runs to the