% --- Streaming SSE parsing and event dispatch --- \begin{figure}[htbp] \centering \begin{tikzpicture}[vqfont] % Pipeline backbone \node[vqpurple, minimum height=1.5cm, text width=2.0cm] (prov) at (0,0) {\textbf{Provider}\\\vqsmall \texttt{/v1/messages}\\ streaming HTTP}; \node[vqblue, minimum height=1.5cm, text width=2.35cm] (buf) at (3.5,0) {\textbf{Byte buffer}\\\vqsmall append chunk;\\split on \texttt{\textbackslash n};\\\textit{keep partial line}}; \node[vqblue, minimum height=1.5cm, text width=2.2cm] (disp) at (7.05,0) {\textbf{Dispatch}\\\vqsmall parse \texttt{data:} line\\$\to$ JSON;\\switch on \texttt{type}}; % Right-hand output channels \node[vqgreen, text width=3.0cm] (txt) at (11.0, 2.55) {\textbf{Answer channel}\\\vqsmall \texttt{text\_delta} $\to$ accumulate $+$ stream}; \node[vqgreen, text width=3.0cm] (think) at (11.0, 1.05) {\textbf{Reasoning channel}\\\vqsmall \texttt{thinking\_delta} $\to$ separate panel}; \node[vqorange, text width=3.0cm] (tool) at (11.0,-0.65) {\textbf{Tool assembler}\\\vqsmall \texttt{start} $\cdot$ \texttt{input\_json\_delta}$^\ast$ $\cdot$ \texttt{stop} $\to$ args}; \node[vqred, text width=3.0cm] (tok) at (11.0,-2.35) {\textbf{Token ledger}\\\vqsmall \texttt{message\_start/}\allowbreak\texttt{delta/}\allowbreak\texttt{stop} $\to$ usage}; % Backbone arrows \draw[vqflow] (prov) -- node[vqtag, above]{raw bytes} (buf); \draw[vqflow] (buf) -- node[vqtag, above]{\texttt{data:} lines} (disp); % Fan-out \draw[vqarr] (disp.east) to[out=20,in=180] (txt.west); \draw[vqarr] (disp.east) to[out=8,in=180] (think.west); \draw[vqarr] (disp.east) to[out=-8,in=180] (tool.west); \draw[vqarr] (disp.east) to[out=-22,in=180] (tok.west); % Downstream consequences \draw[vqdash] (tool.south) to[out=-90,in=-90] node[vqtag, below, pos=0.5]{executable $\to$ loop} (disp.south); \end{tikzpicture} \caption{\textbf{Streaming transport and event dispatch.} The server reads the provider's Server-Sent Event stream as raw bytes, buffers them, and splits on newlines while \emph{retaining the trailing partial line} (frames do not align with packet boundaries). Each \texttt{data:} line is parsed to JSON and dispatched on its \texttt{type} onto four channels: answer text is accumulated and forwarded; private reasoning is forwarded to a separate panel; a tool call is assembled from a \texttt{content\_block\_start}, a sequence of \texttt{input\_json\_delta} fragments ($\ast$), and a closing \texttt{content\_block\_stop}, becoming executable only once the block closes; and usage events feed the token ledger. Heavy arrows are the byte backbone; thin arrows are per-event dispatch.} \label{fig:streaming} \end{figure}