@@ -15,6 +15,7 @@ import {
1515 TypeValidationError ,
1616} from 'ai'
1717
18+ import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
1819import { getModelForRequest , markClaudeOAuthRateLimited , fetchClaudeOAuthResetTime } from './model-provider'
1920import { getValidClaudeOAuthCredentials } from '../credentials'
2021
@@ -184,7 +185,7 @@ export async function* promptAiSdkStream(
184185 onClaudeOAuthStatusChange ?: ( isActive : boolean ) => void
185186 } ,
186187) : ReturnType < PromptAiSdkStreamFn > {
187- const { logger } = params
188+ const { logger, trackEvent , userId , userInputId , model : requestedModel } = params
188189 const agentChunkMetadata =
189190 params . agentId != null ? { agentId : params . agentId } : undefined
190191
@@ -206,9 +207,20 @@ export async function* promptAiSdkStream(
206207 }
207208 const { model : aiSDKModel , isClaudeOAuth } = await getModelForRequest ( modelParams )
208209
209- // Notify about Claude OAuth usage
210- if ( isClaudeOAuth && params . onClaudeOAuthStatusChange ) {
211- params . onClaudeOAuthStatusChange ( true )
210+ // Track and notify about Claude OAuth usage
211+ if ( isClaudeOAuth ) {
212+ trackEvent ( {
213+ event : AnalyticsEvent . CLAUDE_OAUTH_REQUEST ,
214+ userId : userId ?? '' ,
215+ properties : {
216+ model : requestedModel ,
217+ userInputId,
218+ } ,
219+ logger,
220+ } )
221+ if ( params . onClaudeOAuthStatusChange ) {
222+ params . onClaudeOAuthStatusChange ( true )
223+ }
212224 }
213225
214226 const response = streamText ( {
@@ -404,6 +416,16 @@ export async function* promptAiSdkStream(
404416 { error : getErrorObject ( chunkValue . error ) } ,
405417 'Claude OAuth rate limited during stream, falling back to Codebuff backend' ,
406418 )
419+ // Track the rate limit event
420+ trackEvent ( {
421+ event : AnalyticsEvent . CLAUDE_OAUTH_RATE_LIMITED ,
422+ userId : userId ?? '' ,
423+ properties : {
424+ model : requestedModel ,
425+ userInputId,
426+ } ,
427+ logger,
428+ } )
407429 // Try to get the actual reset time from the quota API, fall back to default cooldown
408430 const credentials = await getValidClaudeOAuthCredentials ( )
409431 const resetTime = credentials ?. accessToken
@@ -433,6 +455,16 @@ export async function* promptAiSdkStream(
433455 { error : getErrorObject ( chunkValue . error ) } ,
434456 'Claude OAuth auth error during stream, falling back to Codebuff backend' ,
435457 )
458+ // Track the auth error event
459+ trackEvent ( {
460+ event : AnalyticsEvent . CLAUDE_OAUTH_AUTH_ERROR ,
461+ userId : userId ?? '' ,
462+ properties : {
463+ model : requestedModel ,
464+ userInputId,
465+ } ,
466+ logger,
467+ } )
436468 if ( params . onClaudeOAuthStatusChange ) {
437469 params . onClaudeOAuthStatusChange ( false )
438470 }
0 commit comments