@@ -36,6 +36,7 @@ type GenerateContentStreamEvent =
3636 ChannelMessage < GenerateContentStreamChannel > & {
3737 googleGenAIInput ?: Record < string , unknown > ;
3838 googleGenAIMetadata ?: Record < string , unknown > ;
39+ googleGenAIStartTime ?: number ;
3940 } ;
4041
4142type SpanState = {
@@ -184,12 +185,14 @@ export class GoogleGenAIPlugin extends BasePlugin {
184185 const params = event . arguments [ 0 ] ;
185186 streamEvent . googleGenAIInput = serializeInput ( params ) ;
186187 streamEvent . googleGenAIMetadata = extractMetadata ( params ) ;
188+ streamEvent . googleGenAIStartTime = getCurrentUnixTimestamp ( ) ;
187189 } ,
188190 asyncEnd : ( event ) => {
189191 const streamEvent = event as GenerateContentStreamEvent ;
190192 patchGoogleGenAIStreamingResult ( {
191193 input : streamEvent . googleGenAIInput ,
192194 metadata : streamEvent . googleGenAIMetadata ,
195+ startTime : streamEvent . googleGenAIStartTime ,
193196 result : streamEvent . result ,
194197 } ) ;
195198 } ,
@@ -277,9 +280,10 @@ function logErrorAndEndSpan<TChannel extends GenerateContentChannel>(
277280function patchGoogleGenAIStreamingResult ( args : {
278281 input : Record < string , unknown > | undefined ;
279282 metadata : Record < string , unknown > | undefined ;
283+ startTime : number | undefined ;
280284 result : unknown ;
281285} ) : boolean {
282- const { input, metadata, result } = args ;
286+ const { input, metadata, result, startTime } = args ;
283287
284288 if (
285289 ! input ||
@@ -296,7 +300,7 @@ function patchGoogleGenAIStreamingResult(args: {
296300 let firstTokenTime : number | null = null ;
297301 let finalized = false ;
298302 let span : Span | null = null ;
299- let startTime : number | null = null ;
303+ const requestStartTime = startTime ?? getCurrentUnixTimestamp ( ) ;
300304
301305 const ensureSpan = ( ) => {
302306 if ( ! span ) {
@@ -310,7 +314,6 @@ function patchGoogleGenAIStreamingResult(args: {
310314 metadata,
311315 } ,
312316 } ) ;
313- startTime = getCurrentUnixTimestamp ( ) ;
314317 }
315318
316319 return span ;
@@ -420,11 +423,11 @@ function patchGoogleGenAIStreamingResult(args: {
420423 chunks . push ( nextResult . value ) ;
421424 }
422425
423- if ( nextResult . done && startTime !== null ) {
426+ if ( nextResult . done ) {
424427 finalize ( {
425428 result : aggregateGenerateContentChunks (
426429 chunks ,
427- startTime ,
430+ requestStartTime ,
428431 firstTokenTime ,
429432 ) ,
430433 } ) ;
@@ -447,16 +450,13 @@ function patchGoogleGenAIStreamingResult(args: {
447450 ...returnArgs ,
448451 ) ) as IteratorResult < GoogleGenAIGenerateContentResponse > ;
449452 } finally {
450- if ( startTime !== null ) {
453+ if ( chunks . length > 0 ) {
451454 finalize ( {
452- result :
453- chunks . length > 0
454- ? aggregateGenerateContentChunks (
455- chunks ,
456- startTime ,
457- firstTokenTime ,
458- )
459- : undefined ,
455+ result : aggregateGenerateContentChunks (
456+ chunks ,
457+ requestStartTime ,
458+ firstTokenTime ,
459+ ) ,
460460 } ) ;
461461 } else {
462462 finalize ( { } ) ;
0 commit comments