@@ -22,8 +22,8 @@ import {
2222} from '@google/genai' ;
2323import { AuthType , ContentGenerator } from './contentGenerator.js' ;
2424import OpenAI from 'openai' ;
25- import { logApiResponse } from '../telemetry/loggers.js' ;
26- import { ApiResponseEvent } from '../telemetry/types.js' ;
25+ import { logApiError , logApiResponse } from '../telemetry/loggers.js' ;
26+ import { ApiErrorEvent , ApiResponseEvent } from '../telemetry/types.js' ;
2727import { Config } from '../config/config.js' ;
2828import { openaiLogger } from '../utils/openaiLogger.js' ;
2929
@@ -263,6 +263,7 @@ export class OpenAIContentGenerator implements ContentGenerator {
263263
264264 // Log API response event for UI telemetry
265265 const responseEvent = new ApiResponseEvent (
266+ response . responseId || 'unknown' ,
266267 this . model ,
267268 durationMs ,
268269 userPromptId ,
@@ -291,41 +292,21 @@ export class OpenAIContentGenerator implements ContentGenerator {
291292 ? error . message
292293 : String ( error ) ;
293294
294- // Estimate token usage even when there's an error
295- // This helps track costs and usage even for failed requests
296- let estimatedUsage ;
297- try {
298- const tokenCountResult = await this . countTokens ( {
299- contents : request . contents ,
300- model : this . model ,
301- } ) ;
302- estimatedUsage = {
303- promptTokenCount : tokenCountResult . totalTokens ,
304- candidatesTokenCount : 0 , // No completion tokens since request failed
305- totalTokenCount : tokenCountResult . totalTokens ,
306- } ;
307- } catch {
308- // If token counting also fails, provide a minimal estimate
309- const contentStr = JSON . stringify ( request . contents ) ;
310- const estimatedTokens = Math . ceil ( contentStr . length / 4 ) ;
311- estimatedUsage = {
312- promptTokenCount : estimatedTokens ,
313- candidatesTokenCount : 0 ,
314- totalTokenCount : estimatedTokens ,
315- } ;
316- }
317-
318- // Log API error event for UI telemetry with estimated usage
319- const errorEvent = new ApiResponseEvent (
295+ // Log API error event for UI telemetry
296+ const errorEvent = new ApiErrorEvent (
297+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
298+ ( error as any ) . requestID || 'unknown' ,
320299 this . model ,
300+ errorMessage ,
321301 durationMs ,
322302 userPromptId ,
323303 this . config . getContentGeneratorConfig ( ) ?. authType ,
324- estimatedUsage ,
325- undefined ,
326- errorMessage ,
304+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
305+ ( error as any ) . type ,
306+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
307+ ( error as any ) . code ,
327308 ) ;
328- logApiResponse ( this . config , errorEvent ) ;
309+ logApiError ( this . config , errorEvent ) ;
329310
330311 // Log error interaction if enabled
331312 if ( this . config . getContentGeneratorConfig ( ) ?. enableOpenAILogging ) {
@@ -414,6 +395,7 @@ export class OpenAIContentGenerator implements ContentGenerator {
414395
415396 // Log API response event for UI telemetry
416397 const responseEvent = new ApiResponseEvent (
398+ responses [ responses . length - 1 ] ?. responseId || 'unknown' ,
417399 this . model ,
418400 durationMs ,
419401 userPromptId ,
@@ -445,40 +427,21 @@ export class OpenAIContentGenerator implements ContentGenerator {
445427 ? error . message
446428 : String ( error ) ;
447429
448- // Estimate token usage even when there's an error in streaming
449- let estimatedUsage ;
450- try {
451- const tokenCountResult = await this . countTokens ( {
452- contents : request . contents ,
453- model : this . model ,
454- } ) ;
455- estimatedUsage = {
456- promptTokenCount : tokenCountResult . totalTokens ,
457- candidatesTokenCount : 0 , // No completion tokens since request failed
458- totalTokenCount : tokenCountResult . totalTokens ,
459- } ;
460- } catch {
461- // If token counting also fails, provide a minimal estimate
462- const contentStr = JSON . stringify ( request . contents ) ;
463- const estimatedTokens = Math . ceil ( contentStr . length / 4 ) ;
464- estimatedUsage = {
465- promptTokenCount : estimatedTokens ,
466- candidatesTokenCount : 0 ,
467- totalTokenCount : estimatedTokens ,
468- } ;
469- }
470-
471- // Log API error event for UI telemetry with estimated usage
472- const errorEvent = new ApiResponseEvent (
430+ // Log API error event for UI telemetry
431+ const errorEvent = new ApiErrorEvent (
432+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
433+ ( error as any ) . requestID || 'unknown' ,
473434 this . model ,
435+ errorMessage ,
474436 durationMs ,
475437 userPromptId ,
476438 this . config . getContentGeneratorConfig ( ) ?. authType ,
477- estimatedUsage ,
478- undefined ,
479- errorMessage ,
439+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
440+ ( error as any ) . type ,
441+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
442+ ( error as any ) . code ,
480443 ) ;
481- logApiResponse ( this . config , errorEvent ) ;
444+ logApiError ( this . config , errorEvent ) ;
482445
483446 // Log error interaction if enabled
484447 if ( this . config . getContentGeneratorConfig ( ) ?. enableOpenAILogging ) {
@@ -518,40 +481,21 @@ export class OpenAIContentGenerator implements ContentGenerator {
518481 ? error . message
519482 : String ( error ) ;
520483
521- // Estimate token usage even when there's an error in streaming setup
522- let estimatedUsage ;
523- try {
524- const tokenCountResult = await this . countTokens ( {
525- contents : request . contents ,
526- model : this . model ,
527- } ) ;
528- estimatedUsage = {
529- promptTokenCount : tokenCountResult . totalTokens ,
530- candidatesTokenCount : 0 , // No completion tokens since request failed
531- totalTokenCount : tokenCountResult . totalTokens ,
532- } ;
533- } catch {
534- // If token counting also fails, provide a minimal estimate
535- const contentStr = JSON . stringify ( request . contents ) ;
536- const estimatedTokens = Math . ceil ( contentStr . length / 4 ) ;
537- estimatedUsage = {
538- promptTokenCount : estimatedTokens ,
539- candidatesTokenCount : 0 ,
540- totalTokenCount : estimatedTokens ,
541- } ;
542- }
543-
544- // Log API error event for UI telemetry with estimated usage
545- const errorEvent = new ApiResponseEvent (
484+ // Log API error event for UI telemetry
485+ const errorEvent = new ApiErrorEvent (
486+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
487+ ( error as any ) . requestID || 'unknown' ,
546488 this . model ,
489+ errorMessage ,
547490 durationMs ,
548491 userPromptId ,
549492 this . config . getContentGeneratorConfig ( ) ?. authType ,
550- estimatedUsage ,
551- undefined ,
552- errorMessage ,
493+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
494+ ( error as any ) . type ,
495+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
496+ ( error as any ) . code ,
553497 ) ;
554- logApiResponse ( this . config , errorEvent ) ;
498+ logApiError ( this . config , errorEvent ) ;
555499
556500 // Allow subclasses to suppress error logging for specific scenarios
557501 if ( ! this . shouldSuppressErrorLogging ( error , request ) ) {
0 commit comments