@@ -28,19 +28,19 @@ export interface StreamPatchOptions<TChunk = unknown, TFinal = unknown> {
2828 * Called for each chunk as it's yielded.
2929 * Optional - if not provided, chunks are just collected.
3030 */
31- onChunk ?: ( chunk : TChunk ) => void ;
31+ onChunk ?: ( chunk : TChunk ) => void | Promise < void > ;
3232
3333 /**
3434 * Called when the stream completes successfully.
3535 * Receives all collected chunks.
3636 */
37- onComplete : ( chunks : TChunk [ ] ) => TFinal | void ;
37+ onComplete : ( chunks : TChunk [ ] ) => TFinal | void | Promise < TFinal | void > ;
3838
3939 /**
4040 * Called if the stream errors.
4141 * If not provided, errors are re-thrown after collection stops.
4242 */
43- onError ?: ( error : Error , chunks : TChunk [ ] ) => void ;
43+ onError ?: ( error : Error , chunks : TChunk [ ] ) => void | Promise < void > ;
4444
4545 /**
4646 * Filter to decide whether to collect a chunk.
@@ -135,7 +135,7 @@ export function patchStreamIfNeeded<TChunk = unknown, TFinal = unknown>(
135135 if ( ! completed ) {
136136 completed = true ;
137137 try {
138- options . onComplete ( chunks ) ;
138+ await options . onComplete ( chunks ) ;
139139 } catch ( error ) {
140140 console . error ( "Error in stream onComplete handler:" , error ) ;
141141 }
@@ -155,7 +155,7 @@ export function patchStreamIfNeeded<TChunk = unknown, TFinal = unknown>(
155155 // Call onChunk handler if provided
156156 if ( options . onChunk ) {
157157 try {
158- options . onChunk ( chunk ) ;
158+ await options . onChunk ( chunk ) ;
159159 } catch ( error ) {
160160 console . error ( "Error in stream onChunk handler:" , error ) ;
161161 }
@@ -170,7 +170,7 @@ export function patchStreamIfNeeded<TChunk = unknown, TFinal = unknown>(
170170 completed = true ;
171171 if ( options . onError ) {
172172 try {
173- options . onError (
173+ await options . onError (
174174 error instanceof Error ? error : new Error ( String ( error ) ) ,
175175 chunks ,
176176 ) ;
@@ -191,7 +191,7 @@ export function patchStreamIfNeeded<TChunk = unknown, TFinal = unknown>(
191191 completed = true ;
192192 // Stream was cancelled/returned early
193193 try {
194- options . onComplete ( chunks ) ;
194+ await options . onComplete ( chunks ) ;
195195 } catch ( error ) {
196196 console . error ( "Error in stream onComplete handler:" , error ) ;
197197 }
@@ -213,7 +213,7 @@ export function patchStreamIfNeeded<TChunk = unknown, TFinal = unknown>(
213213 : new Error ( String ( rawError ) ) ;
214214 if ( options . onError ) {
215215 try {
216- options . onError ( error , chunks ) ;
216+ await options . onError ( error , chunks ) ;
217217 } catch ( handlerError ) {
218218 console . error ( "Error in stream onError handler:" , handlerError ) ;
219219 }
0 commit comments