This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathtelemetry.ts
More file actions
535 lines (463 loc) · 16.2 KB
/
Copy pathtelemetry.ts
File metadata and controls
535 lines (463 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import { z } from "zod"
import { providerNames } from "./provider-settings.js"
import { clineMessageSchema } from "./message.js"
/**
* TelemetrySetting
*/
export const telemetrySettings = ["unset", "enabled", "disabled"] as const
export const telemetrySettingsSchema = z.enum(telemetrySettings)
export type TelemetrySetting = z.infer<typeof telemetrySettingsSchema>
/**
* TelemetryEventName
*/
export enum TelemetryEventName {
TASK_CREATED = "Task Created",
TASK_RESTARTED = "Task Reopened",
TASK_COMPLETED = "Task Completed",
TASK_MESSAGE = "Task Message",
TASK_CONVERSATION_MESSAGE = "Conversation Message",
LLM_COMPLETION = "LLM Completion",
MODE_SWITCH = "Mode Switched",
MODE_SELECTOR_OPENED = "Mode Selector Opened",
TOOL_USED = "Tool Used",
CHECKPOINT_CREATED = "Checkpoint Created",
CHECKPOINT_RESTORED = "Checkpoint Restored",
CHECKPOINT_DIFFED = "Checkpoint Diffed",
TAB_SHOWN = "Tab Shown",
MODE_SETTINGS_CHANGED = "Mode Setting Changed",
CUSTOM_MODE_CREATED = "Custom Mode Created",
CONTEXT_CONDENSED = "Context Condensed",
SLIDING_WINDOW_TRUNCATION = "Sliding Window Truncation",
CODE_ACTION_USED = "Code Action Used",
PROMPT_ENHANCED = "Prompt Enhanced",
TITLE_BUTTON_CLICKED = "Title Button Clicked",
AUTHENTICATION_INITIATED = "Authentication Initiated",
MARKETPLACE_ITEM_INSTALLED = "Marketplace Item Installed",
MARKETPLACE_ITEM_REMOVED = "Marketplace Item Removed",
MARKETPLACE_TAB_VIEWED = "Marketplace Tab Viewed",
MARKETPLACE_INSTALL_BUTTON_CLICKED = "Marketplace Install Button Clicked",
SHARE_BUTTON_CLICKED = "Share Button Clicked",
SHARE_ORGANIZATION_CLICKED = "Share Organization Clicked",
SHARE_PUBLIC_CLICKED = "Share Public Clicked",
SHARE_CONNECT_TO_CLOUD_CLICKED = "Share Connect To Cloud Clicked",
ACCOUNT_CONNECT_CLICKED = "Account Connect Clicked",
ACCOUNT_CONNECT_SUCCESS = "Account Connect Success",
ACCOUNT_LOGOUT_CLICKED = "Account Logout Clicked",
ACCOUNT_LOGOUT_SUCCESS = "Account Logout Success",
FEATURED_PROVIDER_CLICKED = "Featured Provider Clicked",
UPSELL_DISMISSED = "Upsell Dismissed",
UPSELL_CLICKED = "Upsell Clicked",
SCHEMA_VALIDATION_ERROR = "Schema Validation Error",
DIFF_APPLICATION_ERROR = "Diff Application Error",
SHELL_INTEGRATION_ERROR = "Shell Integration Error",
CONSECUTIVE_MISTAKE_ERROR = "Consecutive Mistake Error",
CODE_INDEX_ERROR = "Code Index Error",
TELEMETRY_SETTINGS_CHANGED = "Telemetry Settings Changed",
MODEL_CACHE_EMPTY_RESPONSE = "Model Cache Empty Response",
READ_FILE_LEGACY_FORMAT_USED = "Read File Legacy Format Used",
}
/**
* TelemetryProperties
*/
export const staticAppPropertiesSchema = z.object({
appName: z.string(),
appVersion: z.string(),
vscodeVersion: z.string(),
platform: z.string(),
editorName: z.string(),
hostname: z.string().optional(),
})
export type StaticAppProperties = z.infer<typeof staticAppPropertiesSchema>
export const dynamicAppPropertiesSchema = z.object({
language: z.string(),
mode: z.string(),
})
export type DynamicAppProperties = z.infer<typeof dynamicAppPropertiesSchema>
export const cloudAppPropertiesSchema = z.object({
cloudIsAuthenticated: z.boolean().optional(),
})
export type CloudAppProperties = z.infer<typeof cloudAppPropertiesSchema>
export const appPropertiesSchema = z.object({
...staticAppPropertiesSchema.shape,
...dynamicAppPropertiesSchema.shape,
...cloudAppPropertiesSchema.shape,
})
export type AppProperties = z.infer<typeof appPropertiesSchema>
export const taskPropertiesSchema = z.object({
taskId: z.string().optional(),
parentTaskId: z.string().optional(),
apiProvider: z.enum(providerNames).optional(),
modelId: z.string().optional(),
diffStrategy: z.string().optional(),
isSubtask: z.boolean().optional(),
todos: z
.object({
total: z.number(),
completed: z.number(),
inProgress: z.number(),
pending: z.number(),
})
.optional(),
})
export type TaskProperties = z.infer<typeof taskPropertiesSchema>
export const gitPropertiesSchema = z.object({
repositoryUrl: z.string().optional(),
repositoryName: z.string().optional(),
defaultBranch: z.string().optional(),
})
export type GitProperties = z.infer<typeof gitPropertiesSchema>
export const telemetryPropertiesSchema = z.object({
...appPropertiesSchema.shape,
...taskPropertiesSchema.shape,
...gitPropertiesSchema.shape,
})
export type TelemetryProperties = z.infer<typeof telemetryPropertiesSchema>
/**
* TelemetryEvent
*/
export type TelemetryEvent = {
event: TelemetryEventName
// eslint-disable-next-line @typescript-eslint/no-explicit-any
properties?: Record<string, any>
}
/**
* RooCodeTelemetryEvent
*/
export const rooCodeTelemetryEventSchema = z.discriminatedUnion("type", [
z.object({
type: z.enum([
TelemetryEventName.TASK_CREATED,
TelemetryEventName.TASK_RESTARTED,
TelemetryEventName.TASK_COMPLETED,
TelemetryEventName.TASK_CONVERSATION_MESSAGE,
TelemetryEventName.MODE_SWITCH,
TelemetryEventName.MODE_SELECTOR_OPENED,
TelemetryEventName.TOOL_USED,
TelemetryEventName.CHECKPOINT_CREATED,
TelemetryEventName.CHECKPOINT_RESTORED,
TelemetryEventName.CHECKPOINT_DIFFED,
TelemetryEventName.CODE_ACTION_USED,
TelemetryEventName.PROMPT_ENHANCED,
TelemetryEventName.TITLE_BUTTON_CLICKED,
TelemetryEventName.AUTHENTICATION_INITIATED,
TelemetryEventName.MARKETPLACE_ITEM_INSTALLED,
TelemetryEventName.MARKETPLACE_ITEM_REMOVED,
TelemetryEventName.MARKETPLACE_TAB_VIEWED,
TelemetryEventName.MARKETPLACE_INSTALL_BUTTON_CLICKED,
TelemetryEventName.SHARE_BUTTON_CLICKED,
TelemetryEventName.SHARE_ORGANIZATION_CLICKED,
TelemetryEventName.SHARE_PUBLIC_CLICKED,
TelemetryEventName.SHARE_CONNECT_TO_CLOUD_CLICKED,
TelemetryEventName.ACCOUNT_CONNECT_CLICKED,
TelemetryEventName.ACCOUNT_CONNECT_SUCCESS,
TelemetryEventName.ACCOUNT_LOGOUT_CLICKED,
TelemetryEventName.ACCOUNT_LOGOUT_SUCCESS,
TelemetryEventName.FEATURED_PROVIDER_CLICKED,
TelemetryEventName.UPSELL_DISMISSED,
TelemetryEventName.UPSELL_CLICKED,
TelemetryEventName.SCHEMA_VALIDATION_ERROR,
TelemetryEventName.DIFF_APPLICATION_ERROR,
TelemetryEventName.SHELL_INTEGRATION_ERROR,
TelemetryEventName.CONSECUTIVE_MISTAKE_ERROR,
TelemetryEventName.CODE_INDEX_ERROR,
TelemetryEventName.MODEL_CACHE_EMPTY_RESPONSE,
TelemetryEventName.CONTEXT_CONDENSED,
TelemetryEventName.SLIDING_WINDOW_TRUNCATION,
TelemetryEventName.TAB_SHOWN,
TelemetryEventName.MODE_SETTINGS_CHANGED,
TelemetryEventName.CUSTOM_MODE_CREATED,
TelemetryEventName.READ_FILE_LEGACY_FORMAT_USED,
]),
properties: telemetryPropertiesSchema,
}),
z.object({
type: z.literal(TelemetryEventName.TELEMETRY_SETTINGS_CHANGED),
properties: z.object({
...telemetryPropertiesSchema.shape,
previousSetting: telemetrySettingsSchema,
newSetting: telemetrySettingsSchema,
}),
}),
z.object({
type: z.literal(TelemetryEventName.TASK_MESSAGE),
properties: z.object({
...telemetryPropertiesSchema.shape,
taskId: z.string(),
message: clineMessageSchema,
}),
}),
z.object({
type: z.literal(TelemetryEventName.LLM_COMPLETION),
properties: z.object({
...telemetryPropertiesSchema.shape,
inputTokens: z.number(),
outputTokens: z.number(),
cacheReadTokens: z.number().optional(),
cacheWriteTokens: z.number().optional(),
cost: z.number().optional(),
}),
}),
])
export type RooCodeTelemetryEvent = z.infer<typeof rooCodeTelemetryEventSchema>
/**
* TelemetryEventSubscription
*/
export type TelemetryEventSubscription =
| { type: "include"; events: TelemetryEventName[] }
| { type: "exclude"; events: TelemetryEventName[] }
/**
* TelemetryPropertiesProvider
*/
export interface TelemetryPropertiesProvider {
getTelemetryProperties(): Promise<TelemetryProperties>
}
/**
* TelemetryClient
*/
export interface TelemetryClient {
subscription?: TelemetryEventSubscription
setProvider(provider: TelemetryPropertiesProvider): void
capture(options: TelemetryEvent): Promise<void>
captureException(error: Error, additionalProperties?: Record<string, unknown>): Promise<void>
updateTelemetryState(isOptedIn: boolean): void
isTelemetryEnabled(): boolean
shutdown(): Promise<void>
}
/**
* Expected API error codes that should not be reported to telemetry.
* These are normal/expected errors that users can't do much about.
*/
export const EXPECTED_API_ERROR_CODES = new Set([
402, // Payment required - billing issues
429, // Rate limit - expected when hitting API limits
])
/**
* Patterns in error messages that indicate expected errors (rate limits, etc.)
* These are checked when no numeric error code is available.
*/
const EXPECTED_ERROR_MESSAGE_PATTERNS = [
/^429\b/, // Message starts with "429"
/rate limit/i, // Contains "rate limit" (case insensitive)
]
/**
* Interface representing the error structure from OpenAI SDK.
* OpenAI SDK errors (APIError, AuthenticationError, RateLimitError, etc.)
* have a numeric `status` property and may contain nested error metadata.
*
* @see https://github.com/openai/openai-node/blob/master/src/error.ts
*/
interface OpenAISdkError {
/** HTTP status code of the error response */
status: number
/** Optional error code (may be numeric or string) */
code?: number | string
/** Primary error message */
message: string
/** Nested error object containing additional details from the API response */
error?: {
message?: string
metadata?: {
/** Raw error message from upstream provider (e.g., OpenRouter upstream errors) */
raw?: string
}
}
}
/**
* Type guard to check if an error object is an OpenAI SDK error.
* OpenAI SDK errors (APIError and subclasses) have: status, code, message properties.
*/
function isOpenAISdkError(error: unknown): error is OpenAISdkError {
return (
typeof error === "object" &&
error !== null &&
"status" in error &&
typeof (error as OpenAISdkError).status === "number"
)
}
/**
* Extracts the HTTP status code from an error object.
* Supports OpenAI SDK errors that have a status property.
* @param error - The error to extract status from
* @returns The status code if available, undefined otherwise
*/
export function getErrorStatusCode(error: unknown): number | undefined {
if (isOpenAISdkError(error)) {
return error.status
}
return undefined
}
/**
* Extracts a message from a JSON payload embedded in an error string.
* Handles cases like "503 {"error":{"message":"actual error message"}}"
* or just '{"error":{"message":"actual error message"}}'
*
* @param message - The message string that may contain JSON
* @returns The extracted message from the JSON payload, or undefined if not found
*/
export function extractMessageFromJsonPayload(message: string): string | undefined {
// Find the first occurrence of '{' which may indicate JSON content
const jsonStartIndex = message.indexOf("{")
if (jsonStartIndex === -1) {
return undefined
}
const potentialJson = message.slice(jsonStartIndex)
try {
const parsed = JSON.parse(potentialJson)
// Handle structure: {"error":{"message":"..."}} or {"error":{"code":"","message":"..."}}
if (parsed?.error?.message && typeof parsed.error.message === "string") {
return parsed.error.message
}
// Handle structure: {"message":"..."}
if (parsed?.message && typeof parsed.message === "string") {
return parsed.message
}
} catch {
// JSON parsing failed - not valid JSON
}
return undefined
}
/**
* Extracts the most descriptive error message from an error object.
* Prioritizes nested metadata (upstream provider errors) over the standard message.
* Also handles JSON payloads embedded in error messages.
* @param error - The error to extract message from
* @returns The best available error message, or undefined if not extractable
*/
export function getErrorMessage(error: unknown): string | undefined {
let message: string | undefined
if (isOpenAISdkError(error)) {
// Prioritize nested metadata which may contain upstream provider details
message = error.error?.metadata?.raw || error.error?.message || error.message
} else if (error instanceof Error) {
// Handle standard Error objects (including ApiProviderError)
message = error.message
} else if (typeof error === "object" && error !== null && "message" in error) {
// Handle plain objects with a message property
const msgValue = (error as { message: unknown }).message
if (typeof msgValue === "string") {
message = msgValue
}
}
if (!message) {
return undefined
}
// If the message contains JSON, try to extract the message from it
const extractedMessage = extractMessageFromJsonPayload(message)
if (extractedMessage) {
return extractedMessage
}
return message
}
/**
* Helper to check if an API error should be reported to telemetry.
* Filters out expected errors like rate limits by checking both error codes and messages.
* @param errorCode - The HTTP error code (if available)
* @param errorMessage - The error message (if available)
* @returns true if the error should be reported, false if it should be filtered out
*/
export function shouldReportApiErrorToTelemetry(errorCode?: number, errorMessage?: string): boolean {
// Check numeric error code
if (errorCode !== undefined && EXPECTED_API_ERROR_CODES.has(errorCode)) {
return false
}
// Check error message for expected patterns (e.g., "429 Rate limit exceeded")
if (errorMessage) {
for (const pattern of EXPECTED_ERROR_MESSAGE_PATTERNS) {
if (pattern.test(errorMessage)) {
return false
}
}
}
return true
}
/**
* Generic API provider error class for structured error tracking via PostHog.
* Can be reused by any API provider.
*/
export class ApiProviderError extends Error {
constructor(
message: string,
public readonly provider: string,
public readonly modelId: string,
public readonly operation: string,
public readonly errorCode?: number,
) {
super(message)
this.name = "ApiProviderError"
}
}
/**
* Type guard to check if an error is an ApiProviderError.
* Used by telemetry to automatically extract structured properties.
*/
export function isApiProviderError(error: unknown): error is ApiProviderError {
return (
error instanceof Error &&
error.name === "ApiProviderError" &&
"provider" in error &&
"modelId" in error &&
"operation" in error
)
}
/**
* Extracts properties from an ApiProviderError for telemetry.
* Returns the structured properties that can be merged with additionalProperties.
*/
export function extractApiProviderErrorProperties(error: ApiProviderError): Record<string, unknown> {
return {
provider: error.provider,
modelId: error.modelId,
operation: error.operation,
...(error.errorCode !== undefined && { errorCode: error.errorCode }),
}
}
/**
* Reason why the consecutive mistake limit was reached.
*/
export type ConsecutiveMistakeReason = "no_tools_used" | "tool_repetition" | "unknown"
/**
* Error class for "Roo is having trouble" consecutive mistake scenarios.
* Triggered when the task reaches the configured consecutive mistake limit.
* Used for structured exception tracking via PostHog.
*/
export class ConsecutiveMistakeError extends Error {
constructor(
message: string,
public readonly taskId: string,
public readonly consecutiveMistakeCount: number,
public readonly consecutiveMistakeLimit: number,
public readonly reason: ConsecutiveMistakeReason = "unknown",
public readonly provider?: string,
public readonly modelId?: string,
) {
super(message)
this.name = "ConsecutiveMistakeError"
}
}
/**
* Type guard to check if an error is a ConsecutiveMistakeError.
* Used by telemetry to automatically extract structured properties.
*/
export function isConsecutiveMistakeError(error: unknown): error is ConsecutiveMistakeError {
return (
error instanceof Error &&
error.name === "ConsecutiveMistakeError" &&
"taskId" in error &&
"consecutiveMistakeCount" in error &&
"consecutiveMistakeLimit" in error
)
}
/**
* Extracts properties from a ConsecutiveMistakeError for telemetry.
* Returns the structured properties that can be merged with additionalProperties.
*/
export function extractConsecutiveMistakeErrorProperties(error: ConsecutiveMistakeError): Record<string, unknown> {
return {
taskId: error.taskId,
consecutiveMistakeCount: error.consecutiveMistakeCount,
consecutiveMistakeLimit: error.consecutiveMistakeLimit,
reason: error.reason,
...(error.provider !== undefined && { provider: error.provider }),
...(error.modelId !== undefined && { modelId: error.modelId }),
}
}