@@ -8,6 +8,7 @@ import type { DroidMcpServerConfig } from './mcp.js';
88import type {
99 InitializeSessionRequestParams ,
1010 McpServerConfig ,
11+ OutputFormat ,
1112 SessionTag ,
1213} from './schemas/client.js' ;
1314import type {
@@ -23,9 +24,11 @@ import type { ToolSelectionOverrides } from './schemas/shared.js';
2324import {
2425 convertNotificationToStreamMessage ,
2526 DroidMessageType ,
27+ isDefaultStreamMessage ,
28+ isInternalMessage ,
2629 StreamStateTracker ,
2730} from './stream.js' ;
28- import type { DroidMessage } from './stream.js' ;
31+ import type { DroidStreamEvent , InternalDroidMessage } from './stream.js' ;
2932import { ProcessTransport } from './transport.js' ;
3033import type { DroidClientTransport , ProcessTransportOptions } from './types.js' ;
3134
@@ -52,14 +55,27 @@ export function extractInnerNotification(
5255}
5356
5457export class MessageBridge {
55- private readonly _queue : DroidMessage [ ] = [ ] ;
58+ private readonly _queue : DroidStreamEvent [ ] = [ ] ;
5659 private readonly _onDone : ( ( ) => void ) | undefined ;
5760 private _resolveWaiting : ( ( ) => void ) | null = null ;
5861 private _done = false ;
59- private readonly _stateTracker = new StreamStateTracker ( ) ;
62+ private readonly _stateTracker : StreamStateTracker ;
6063
61- constructor ( onDone ?: ( ) => void ) {
64+ constructor (
65+ onDone ?: ( ) => void ,
66+ private readonly _options : {
67+ includePartialMessages ?: boolean ;
68+ sessionId ?: string ;
69+ startedAt ?: number ;
70+ outputFormat ?: OutputFormat ;
71+ } = { }
72+ ) {
6273 this . _onDone = onDone ;
74+ this . _stateTracker = new StreamStateTracker ( {
75+ sessionId : _options . sessionId ,
76+ startedAt : _options . startedAt ,
77+ hasOutputFormat : _options . outputFormat !== undefined ,
78+ } ) ;
6379 }
6480
6581 readonly notificationHandler = (
@@ -75,11 +91,15 @@ export class MessageBridge {
7591
7692 for ( const msg of messages ) {
7793 const { message, additional } = this . _stateTracker . processMessage ( msg ) ;
78- this . _enqueue ( message ) ;
94+ if ( message && this . _shouldYield ( message ) ) {
95+ this . _enqueue ( message ) ;
96+ }
7997
8098 for ( const extra of additional ) {
81- this . _enqueue ( extra ) ;
82- if ( extra . type === DroidMessageType . TurnComplete ) {
99+ if ( this . _shouldYield ( extra ) ) {
100+ this . _enqueue ( extra ) ;
101+ }
102+ if ( extra . type === DroidMessageType . Result ) {
83103 this . _signalDone ( ) ;
84104 }
85105 }
@@ -90,13 +110,13 @@ export class MessageBridge {
90110 this . _signalDone ( ) ;
91111 }
92112
93- async * messages ( ) : AsyncGenerator < DroidMessage , void , undefined > {
113+ async * messages ( ) : AsyncGenerator < DroidStreamEvent , void , undefined > {
94114 while ( true ) {
95115 while ( this . _queue . length > 0 ) {
96116 const msg = this . _queue . shift ( ) ! ;
97117 yield msg ;
98118
99- if ( msg . type === DroidMessageType . TurnComplete ) {
119+ if ( msg . type === DroidMessageType . Result ) {
100120 return ;
101121 }
102122 }
@@ -111,7 +131,18 @@ export class MessageBridge {
111131 }
112132 }
113133
114- private _enqueue ( msg : DroidMessage ) : void {
134+ private _shouldYield (
135+ message : InternalDroidMessage
136+ ) : message is DroidStreamEvent {
137+ if ( isInternalMessage ( message ) ) {
138+ return false ;
139+ }
140+ return this . _options . includePartialMessages
141+ ? true
142+ : isDefaultStreamMessage ( message ) ;
143+ }
144+
145+ private _enqueue ( msg : DroidStreamEvent ) : void {
115146 this . _queue . push ( msg ) ;
116147 if ( this . _resolveWaiting ) {
117148 const resolve = this . _resolveWaiting ;
0 commit comments