Skip to content

Commit 2ee71f4

Browse files
committed
Track events for claude oauth
1 parent d04ca99 commit 2ee71f4

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

common/src/constants/analytics-events.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ export enum AnalyticsEvent {
128128
TOKEN_COUNT_VALIDATION_ERROR = 'api.token_count_validation_error',
129129
TOKEN_COUNT_ERROR = 'api.token_count_error',
130130

131+
// Claude OAuth
132+
CLAUDE_OAUTH_REQUEST = 'sdk.claude_oauth_request',
133+
CLAUDE_OAUTH_RATE_LIMITED = 'sdk.claude_oauth_rate_limited',
134+
CLAUDE_OAUTH_AUTH_ERROR = 'sdk.claude_oauth_auth_error',
135+
131136
// Common
132137
FLUSH_FAILED = 'common.flush_failed',
133138
}

sdk/src/impl/llm.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
TypeValidationError,
1616
} from 'ai'
1717

18+
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
1819
import { getModelForRequest, markClaudeOAuthRateLimited, fetchClaudeOAuthResetTime } from './model-provider'
1920
import { 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

Comments
 (0)