1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! Multi-frame responses: the handler shape, and the writer that drains it
//! (#6286).
//!
//! Why: `trusty-memory`'s `POST /api/v1/chat` streams LLM tokens off a
//! `tokio::sync::mpsc::Receiver` wrapped in a `ReceiverStream`, and the
//! one-frame-per-connection contract [`super::handle_connection`] enforces
//! cannot carry that. #6287 deferred the multi-frame extension until a daemon
//! had a real cross-crate SSE consumer; chat is that consumer, and degrading
//! chat to a single buffered frame was ruled out. So the producer shape here is
//! the shape chat already has: a handler hands back the RECEIVER and goes on
//! filling the sender from wherever it likes.
//!
//! What, in the order a service uses them.
//! - [`RpcStreamItems`] is what a handler returns — an `mpsc::Receiver` of
//! per-item results. `tokio_stream::wrappers::ReceiverStream::new` takes the
//! same value, so a caller that already thinks in `Stream`s loses nothing.
//! - [`RpcStreamMethod`] is the object-safe handler trait, and
//! [`typed_stream_method`] builds one from an async function over the
//! caller's own request type.
//! - [`RpcOutcome`] is what [`super::RpcRouter::dispatch_streaming`] decides:
//! one response, or a stream to drain.
//! - [`write_stream`] drains that stream onto the socket in the frame contract
//! [`RpcStreamFrame`] documents.
//!
//! **Every stream ends in exactly one terminal frame.** `write_stream` has no
//! path that returns `Ok` without having written an `end` or an `error` frame:
//! a handler error becomes a terminal error frame, an item too large for the
//! frame budget becomes a terminal error frame, and a producer that drops its
//! sender without failing becomes a terminal `end`. A silently truncated stream
//! would be a Fail-Open branch — the client would read a partial answer as a
//! complete one — so the only way a stream ends without a terminal frame is a
//! write failure, which the client sees as a hang-up rather than as data.
//!
//! Test: `super::tests` — `stream_*`.
use PhantomData;
use async_trait;
use DeserializeOwned;
use ;
use mpsc;
use ;
/// The channel a streaming handler produces into.
///
/// An `Ok` item becomes one [`StreamPhase::Item`] frame; an `Err` becomes the
/// terminal error frame and stops the stream. Dropping the sender ends the
/// stream successfully.
///
/// [`StreamPhase::Item`]: super::StreamPhase
pub type RpcStreamItems = Receiver;
/// One streaming method's implementation (#6286).
///
/// Why: object-safe for the same reason [`RpcMethod`] is — the router holds a
/// heterogeneous set behind `Arc<dyn RpcStreamMethod>`. Most callers never name
/// it; [`typed_stream_method`] builds one from an ordinary async function.
/// What: opens the stream and returns its receiver. Returning `Err` here is the
/// "could not start" case — the caller never sent an item, so it becomes a
/// terminal error frame with zero items ahead of it.
///
/// [`RpcMethod`]: super::RpcMethod
///
/// Test: `stream_round_trips_many_frames_over_a_real_socket`,
/// `stream_reports_a_handler_error_as_a_terminal_frame`.
/// Adapter behind [`typed_stream_method`]; `PhantomData<fn(Req)>` keeps the
/// struct `Send + Sync` whatever `Req` is.
/// Build an [`RpcStreamMethod`] from an async function over the caller's own
/// request type.
///
/// Why: the streaming counterpart of [`typed_method`] — the caller names `Req`
/// and never touches `serde_json::Value` on the way in. Items stay `Value`
/// deliberately: a token stream is heterogeneous in practice (a text delta, a
/// usage record, a finish reason), and forcing one `Resp` type on every frame
/// would push that union into every service.
/// What: deserialises `params` into `Req` before `call` runs, reporting a decode
/// failure as [`RpcError::invalid_params`] exactly as the unary path does.
///
/// [`typed_method`]: super::typed_method
///
/// Test: `stream_reports_invalid_params_before_opening_the_stream`.
/// What one request frame turned out to need (#6286).
///
/// Why: [`super::RpcRouter::dispatch`] answers with a value, which is right for
/// a unary call and cannot express "many frames follow". This is the wider
/// return the connection handler needs, kept out of `dispatch` so every existing
/// caller of that function is untouched.
/// What: either the single response frame to write, or the id to stamp on each
/// frame plus the receiver to drain.
///
/// `#[non_exhaustive]`: a third outcome (a bidirectional exchange, say) would
/// otherwise be a breaking change.
///
/// Test: `stream_round_trips_many_frames_over_a_real_socket`,
/// `dispatch_streaming_answers_a_unary_request_unchanged`.
/// Drain `items` onto `writer` as newline-terminated stream frames.
///
/// Why: the write half of the contract [`RpcStreamFrame`] states, in one place,
/// so "what terminates a stream" is decided once rather than per service.
/// What: one [`StreamPhase::Item`] frame per `Ok` item, then exactly one
/// terminal frame — `end` when the producer finishes, `error` when it fails or
/// when an item does not fit `max_frame_bytes`. Returns whether the terminal
/// frame carried an error.
///
/// **`max_frame_bytes` is per frame, not per stream.** An item that would not
/// fit is refused rather than truncated, because the client applies the same
/// budget to each frame it reads and a frame it cannot buffer would desynchronise
/// the rest of the connection.
///
/// The writer is flushed after every frame. Batching would be cheaper and would
/// also defeat the point: a token stream the client sees only at completion is
/// the buffered response streaming exists to replace.
///
/// # Errors
///
/// Only the underlying write error. A handler failure is data, not an error
/// here — it leaves as the terminal error frame.
///
/// [`StreamPhase::Item`]: super::StreamPhase
///
/// Test: `stream_round_trips_many_frames_over_a_real_socket`,
/// `stream_reports_a_handler_error_as_a_terminal_frame`,
/// `stream_refuses_an_item_larger_than_the_frame_budget`,
/// `stream_survives_a_client_that_disconnects_mid_stream`.
pub async
/// [`crate::uds::encode_frame`] with its serde failure mapped to an io error,
/// so `write_stream` has one error type rather than two.
/// Encode one frame and push it, flushed, so the client sees it immediately.
async