feat(backend): modèle unifié streaming/progress/events provider-agnostic + pont app-tauri — foundation #156 (QA verte)
This commit is contained in:
@ -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,
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -18,7 +18,9 @@ use app_tauri_lib::chat::{
|
||||
};
|
||||
use app_tauri_lib::dto::ReplyChunk;
|
||||
use domain::ids::SessionId;
|
||||
use domain::ports::ReplyEvent;
|
||||
use domain::ports::{
|
||||
ReplyEvent, ReplyProgress, ReplyProgressKind, ReplyProgressSource, ReplyProgressStage,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Builds a `Channel<ReplyChunk>` whose sent chunks are recorded into `sink`.
|
||||
@ -62,6 +64,9 @@ fn 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)
|
||||
}
|
||||
ReplyChunk::Final { content } => content.len(),
|
||||
ReplyChunk::Error { message } => message.len(),
|
||||
}
|
||||
@ -81,13 +86,41 @@ fn chunk_from_event_maps_text_delta() {
|
||||
|
||||
#[test]
|
||||
fn chunk_from_event_maps_tool_activity() {
|
||||
let Some(ReplyChunk::Progress { progress }) = chunk_from_event(ReplyEvent::ToolActivity {
|
||||
label: "reads file".into(),
|
||||
}) else {
|
||||
panic!("tool activity must map to canonical progress")
|
||||
};
|
||||
assert_eq!(progress.label, "reads file");
|
||||
assert_eq!(
|
||||
chunk_from_event(ReplyEvent::ToolActivity {
|
||||
label: "reads file".into()
|
||||
}),
|
||||
Some(ReplyChunk::ToolActivity {
|
||||
label: "reads file".into()
|
||||
})
|
||||
progress.kind,
|
||||
app_tauri_lib::dto::ReplyProgressKindDto::Tool
|
||||
);
|
||||
assert_eq!(
|
||||
progress.source,
|
||||
app_tauri_lib::dto::ReplyProgressSourceDto::ProviderNative
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chunk_from_event_maps_canonical_progress() {
|
||||
let event = ReplyEvent::Progress {
|
||||
progress: ReplyProgress::new(
|
||||
ReplyProgressSource::IdeaLocal,
|
||||
ReplyProgressKind::Mcp,
|
||||
ReplyProgressStage::Started,
|
||||
"idea_ask_agent",
|
||||
)
|
||||
.with_tool_name("idea_ask_agent"),
|
||||
};
|
||||
let Some(ReplyChunk::Progress { progress }) = chunk_from_event(event) else {
|
||||
panic!("progress event must map to progress chunk")
|
||||
};
|
||||
assert_eq!(progress.label, "idea_ask_agent");
|
||||
assert_eq!(progress.kind, app_tauri_lib::dto::ReplyProgressKindDto::Mcp);
|
||||
assert_eq!(
|
||||
progress.source,
|
||||
app_tauri_lib::dto::ReplyProgressSourceDto::IdeaLocal
|
||||
);
|
||||
}
|
||||
|
||||
@ -357,7 +390,18 @@ fn scrollback_accumulates_every_routed_chunk_in_order() {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
bridge.scrollback(&session),
|
||||
bridge
|
||||
.scrollback(&session)
|
||||
.into_iter()
|
||||
.map(|chunk| match chunk {
|
||||
ReplyChunk::Progress { progress } => {
|
||||
ReplyChunk::ToolActivity {
|
||||
label: progress.label,
|
||||
}
|
||||
}
|
||||
other => other,
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
vec![
|
||||
delta("x"),
|
||||
ReplyChunk::ToolActivity {
|
||||
|
||||
@ -8,7 +8,8 @@
|
||||
|
||||
use app_tauri_lib::dto::{
|
||||
CellKind, ChatAttachmentDto, ChatAttachmentInputDto, ImportChatAttachmentsRequestDto,
|
||||
ImportChatAttachmentsResponseDto, ReattachChatDto, ReplyChunk, TerminalSessionDto,
|
||||
ImportChatAttachmentsResponseDto, ReattachChatDto, ReplyChunk, ReplyProgressDto,
|
||||
ReplyProgressKindDto, ReplyProgressSourceDto, ReplyProgressStageDto, TerminalSessionDto,
|
||||
};
|
||||
use application::{LaunchAgentOutput, StructuredSessionDescriptor};
|
||||
use domain::project::ProjectPath;
|
||||
@ -48,6 +49,37 @@ fn reply_chunk_tool_activity_serialises_exact_camel_case() {
|
||||
assert_eq!(v, json!({ "kind": "toolActivity", "label": "reads file" }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reply_chunk_progress_serialises_exact_camel_case() {
|
||||
let v = serde_json::to_value(ReplyChunk::Progress {
|
||||
progress: ReplyProgressDto {
|
||||
source: ReplyProgressSourceDto::IdeaLocal,
|
||||
kind: ReplyProgressKindDto::Mcp,
|
||||
stage: ReplyProgressStageDto::Started,
|
||||
label: "idea_ask_agent".into(),
|
||||
text: Some("vers QA".into()),
|
||||
provider: None,
|
||||
native_event: None,
|
||||
tool_name: Some("idea_ask_agent".into()),
|
||||
},
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
v,
|
||||
json!({
|
||||
"kind": "progress",
|
||||
"progress": {
|
||||
"source": "ideaLocal",
|
||||
"kind": "mcp",
|
||||
"stage": "started",
|
||||
"label": "idea_ask_agent",
|
||||
"text": "vers QA",
|
||||
"toolName": "idea_ask_agent"
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reply_chunk_final_serialises_exact_camel_case() {
|
||||
let v = serde_json::to_value(ReplyChunk::Final {
|
||||
@ -77,6 +109,18 @@ fn reply_chunk_round_trips_through_json_for_every_variant() {
|
||||
ReplyChunk::ToolActivity {
|
||||
label: "runs".into(),
|
||||
},
|
||||
ReplyChunk::Progress {
|
||||
progress: ReplyProgressDto {
|
||||
source: ReplyProgressSourceDto::ProviderNative,
|
||||
kind: ReplyProgressKindDto::Turn,
|
||||
stage: ReplyProgressStageDto::Started,
|
||||
label: "tour démarré".into(),
|
||||
text: None,
|
||||
provider: Some("codex".into()),
|
||||
native_event: Some("turn.started".into()),
|
||||
tool_name: None,
|
||||
},
|
||||
},
|
||||
ReplyChunk::Final {
|
||||
content: "y".into(),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user