1
+import type { CliCommandTelemetryContext, TrackEvent } from './types';
2
+import { ToolInputValidationError } from 'src/services/tool-input-validation';
3
+import { toolkitFromToolSlug } from 'src/utils/toolkit-from-tool-slug';
4
+
5
+export const CLI_ANALYTICS_EVENTS = {
6
+ CLI_COMMAND_INVOKED: 'CLI_COMMAND_INVOKED',
7
+ CLI_COMMAND_SUCCEEDED: 'CLI_COMMAND_SUCCEEDED',
8
+ CLI_COMMAND_FAILED: 'CLI_COMMAND_FAILED',
9
+ CLI_EXECUTE_INVOKED: 'CLI_EXECUTE_INVOKED',
10
+ CLI_EXECUTE_SUCCEEDED: 'CLI_EXECUTE_SUCCEEDED',
11
+ CLI_EXECUTE_FAILED: 'CLI_EXECUTE_FAILED',
12
+ CLI_SEARCH_INVOKED: 'CLI_SEARCH_INVOKED',
13
+ CLI_SEARCH_SUCCEEDED: 'CLI_SEARCH_SUCCEEDED',
14
+ CLI_SEARCH_FAILED: 'CLI_SEARCH_FAILED',
15
+ CLI_LINK_INVOKED: 'CLI_LINK_INVOKED',
16
+ CLI_LINK_SUCCEEDED: 'CLI_LINK_SUCCEEDED',
17
+ CLI_LINK_FAILED: 'CLI_LINK_FAILED',
18
+ CLI_LOGIN_INVOKED: 'CLI_LOGIN_INVOKED',
19
+ CLI_LOGIN_SUCCEEDED: 'CLI_LOGIN_SUCCEEDED',
20
+ CLI_LOGIN_FAILED: 'CLI_LOGIN_FAILED',
21
+ CLI_LOGOUT_INVOKED: 'CLI_LOGOUT_INVOKED',
22
+ CLI_LOGOUT_SUCCEEDED: 'CLI_LOGOUT_SUCCEEDED',
23
+ CLI_LOGOUT_FAILED: 'CLI_LOGOUT_FAILED',
24
+ CLI_PROXY_INVOKED: 'CLI_PROXY_INVOKED',
25
+ CLI_PROXY_SUCCEEDED: 'CLI_PROXY_SUCCEEDED',
26
+ CLI_PROXY_FAILED: 'CLI_PROXY_FAILED',
27
+ CLI_RUN_INVOKED: 'CLI_RUN_INVOKED',
28
+ CLI_RUN_SUCCEEDED: 'CLI_RUN_SUCCEEDED',
29
+ CLI_RUN_FAILED: 'CLI_RUN_FAILED',
30
+ CLI_TOOL_INVOCATION_VALIDATION_FAILED: 'CLI_TOOL_INVOCATION_VALIDATION_FAILED',
31
+ CLI_TOOL_INVOCATION_TOOL_NOT_FOUND: 'CLI_TOOL_INVOCATION_TOOL_NOT_FOUND',
32
+ CLI_TOOL_INVOCATION_FAILED: 'CLI_TOOL_INVOCATION_FAILED',
33
+} as const;
34
+
35
+const KNOWN_COMMAND_TOKENS = new Set([
36
+ 'version',
37
+ 'upgrade',
38
+ 'whoami',
39
+ 'login',
40
+ 'logout',
41
+ 'run',
42
+ 'install',
43
+ 'dev',
44
+ 'generate',
45
+ 'manage',
46
+ 'tools',
47
+ 'toolkits',
48
+ 'toolkit',
49
+ 'search',
50
+ 'execute',
51
+ 'link',
52
+ 'proxy',
53
+ 'artifacts',
54
+ 'cwd',
55
+ 'connected-accounts',
56
+ 'auth-configs',
57
+ 'triggers',
58
+ 'logs',
59
+ 'orgs',
60
+ 'projects',
61
+ 'info',
62
+ 'list',
63
+ 'create',
64
+ 'delete',
65
+ 'enable',
66
+ 'disable',
67
+ 'status',
68
+ 'listen',
69
+ 'switch',
70
+ 'py',
71
+ 'ts',
72
+]);
73
+
74
+const TOOL_NOT_FOUND_PATTERN = /\btool\b.*\bnot found\b/i;
75
+const getInvocationOrigin = (): string => process.env.COMPOSIO_CLI_INVOCATION_ORIGIN ?? 'cli';
76
+const getParentRunId = (): string | undefined => process.env.COMPOSIO_CLI_PARENT_RUN_ID;
77
+
78
+const extractCommandPath = (argv: ReadonlyArray<string>): string => {
79
+ const commandTokens: string[] = [];
80
+
81
+ for (const token of argv.slice(2)) {
82
+ if (!token || token.startsWith('-') || !KNOWN_COMMAND_TOKENS.has(token)) {
83
+ break;
84
+ }
85
+ commandTokens.push(token);
86
+ }
87
+
88
+ return commandTokens.length > 0 ? commandTokens.join(' ') : 'composio';
89
+};
90
+
91
+const extractFlagNames = (argv: ReadonlyArray<string>): ReadonlyArray<string> =>
92
+ [
93
+ ...new Set(
94
+ argv
95
+ .slice(2)
96
+ .filter(token => token.startsWith('-'))
97
+ .map(token => token.split('=')[0]!)
98
+ ),
99
+ ].sort();
100
+
101
+const argumentShape = (args: Record<string, unknown>) => {
102
+ const keys = Object.keys(args).sort();
103
+ return {
104
+ argument_key_count: keys.length,
105
+ argument_keys: keys.slice(0, 50),
106
+ };
107
+};
108
+
109
+const extractIssueLocations = (issues: ReadonlyArray<string>): ReadonlyArray<string> =>
110
+ [
111
+ ...new Set(
112
+ issues
113
+ .map(issue => issue.match(/^([^:]+):/u)?.[1]?.trim())
114
+ .filter((value): value is string => typeof value === 'string' && value.length > 0)
115
+ ),
116
+ ].slice(0, 20);
117
+
118
+const extractUnknownKeys = (issues: ReadonlyArray<string>): ReadonlyArray<string> =>
119
+ [
120
+ ...new Set(
121
+ issues
122
+ .map(issue => issue.match(/Unknown key "([^"]+)"/u)?.[1])
123
+ .filter((value): value is string => typeof value === 'string' && value.length > 0)
124
+ ),
125
+ ].slice(0, 20);
126
+
127
+const errorNameOf = (error: unknown): string =>
128
+ error instanceof Error && error.name ? error.name : 'UnknownError';
129
+
130
+const errorMessageOf = (error: unknown): string => {
131
+ if (error instanceof Error && error.message) return error.message.slice(0, 500);
132
+ if (typeof error === 'string') return error.slice(0, 500);
133
+ return 'Unknown error';
134
+};
135
+
136
+const isFlagPresent = (argv: ReadonlyArray<string>, ...flags: string[]): boolean =>
137
+ argv.slice(2).some(token => {
138
+ if (flags.includes(token)) return true;
139
+ return flags.some(flag => token.startsWith(`${flag}=`));
140
+ });
141
+
142
+const getFlagValue = (argv: ReadonlyArray<string>, ...flags: string[]): string | undefined => {
143
+ const args = argv.slice(2);
144
+ for (let index = 0; index < args.length; index += 1) {
145
+ const token = args[index];
146
+ if (!token) continue;
147
+
148
+ for (const flag of flags) {
149
+ if (token === flag) {
150
+ const next = args[index + 1];
151
+ return next && !next.startsWith('-') ? next : undefined;
152
+ }
153
+ if (token.startsWith(`${flag}=`)) {
154
+ return token.slice(flag.length + 1);
155
+ }
156
+ }
157
+ }
158
+ return undefined;
159
+};
160
+
161
+const getTrailingPositionals = (context: CliCommandTelemetryContext): ReadonlyArray<string> => {
162
+ const commandTokenCount =
163
+ context.commandPath === 'composio' ? 0 : context.commandPath.split(' ').length;
164
+ const args = context.argv.slice(2 + commandTokenCount);
165
+ const positionals: string[] = [];
166
+
167
+ for (let index = 0; index < args.length; index += 1) {
168
+ const token = args[index];
169
+ if (!token) continue;
170
+
171
+ if (token === '--') {
172
+ break;
173
+ }
174
+
175
+ if (token.startsWith('-')) {
176
+ const expectsValue = [
177
+ '--data',
178
+ '-d',
179
+ '--toolkits',
180
+ '--limit',
181
+ '--user-id',
182
+ '--project-name',
183
+ '--auth-config',
184
+ '--file',
185
+ '-f',
186
+ ].includes(token);
187
+ if (expectsValue) {
188
+ index += 1;
189
+ }
190
+ continue;
191
+ }
192
+
193
+ positionals.push(token);
194
+ }
195
+
196
+ return positionals;
197
+};
198
+
199
+const getExecuteCommandProperties = (context: CliCommandTelemetryContext) => {
200
+ const slug = getTrailingPositionals(context)[0];
201
+ return {
202
+ source: 'cli',
203
+ invocation_origin: getInvocationOrigin(),
204
+ parent_run_id: getParentRunId(),
205
+ parent_command: getParentRunId() ? 'run' : undefined,
206
+ cli_version: context.cliVersion,
207
+ command_path: context.commandPath,
208
+ duration_ms: Date.now() - context.startedAt,
209
+ surface: context.commandPath === 'execute' ? 'root' : 'manage',
210
+ tool_slug: slug,
211
+ tool_name: slug,
212
+ toolkit_slug: typeof slug === 'string' ? toolkitFromToolSlug(slug) : undefined,
213
+ dry_run: isFlagPresent(context.argv, '--dry-run'),
214
+ get_schema: isFlagPresent(context.argv, '--get-schema'),
215
+ has_data: isFlagPresent(context.argv, '--data', '-d'),
216
+ skip_connection_check: isFlagPresent(context.argv, '--skip-connection-check'),
217
+ skip_tool_params_check: isFlagPresent(context.argv, '--skip-tool-params-check'),
218
+ no_verify: isFlagPresent(context.argv, '--no-verify'),
219
+ };
220
+};
221
+
222
+const getSearchCommandProperties = (context: CliCommandTelemetryContext) => ({
223
+ source: 'cli',
224
+ invocation_origin: getInvocationOrigin(),
225
+ parent_run_id: getParentRunId(),
226
+ parent_command: getParentRunId() ? 'run' : undefined,
227
+ cli_version: context.cliVersion,
228
+ command_path: context.commandPath,
229
+ duration_ms: Date.now() - context.startedAt,
230
+ query: getTrailingPositionals(context)[0],
231
+ search_query: getTrailingPositionals(context)[0],
232
+ toolkits: getFlagValue(context.argv, '--toolkits'),
233
+ limit: getFlagValue(context.argv, '--limit'),
234
+});
235
+
236
+const getLinkCommandProperties = (context: CliCommandTelemetryContext) => {
237
+ const firstPositional = getTrailingPositionals(context)[0];
238
+ return {
239
+ source: 'cli',
240
+ invocation_origin: getInvocationOrigin(),
241
+ parent_run_id: getParentRunId(),
242
+ parent_command: getParentRunId() ? 'run' : undefined,
243
+ cli_version: context.cliVersion,
244
+ command_path: context.commandPath,
245
+ duration_ms: Date.now() - context.startedAt,
246
+ toolkit: getFlagValue(context.argv, '--toolkit') ?? firstPositional,
247
+ no_browser: isFlagPresent(context.argv, '--no-browser'),
248
+ no_wait: isFlagPresent(context.argv, '--no-wait'),
249
+ has_auth_config: isFlagPresent(context.argv, '--auth-config'),
250
+ };
251
+};
252
+
253
+const getRunCommandProperties = (context: CliCommandTelemetryContext) => ({
254
+ source: 'cli',
255
+ invocation_origin: getInvocationOrigin(),
256
+ run_id: context.runId,
257
+ cli_version: context.cliVersion,
258
+ command_path: context.commandPath,
259
+ duration_ms: Date.now() - context.startedAt,
260
+ debug: isFlagPresent(context.argv, '--debug'),
261
+ dry_run: isFlagPresent(context.argv, '--dry-run'),
262
+ skip_connection_check: isFlagPresent(context.argv, '--skip-connection-check'),
263
+ skip_tool_params_check: isFlagPresent(context.argv, '--skip-tool-params-check'),
264
+ no_verify: isFlagPresent(context.argv, '--no-verify'),
265
+ file_mode: isFlagPresent(context.argv, '--file', '-f'),
266
+ arg_count: Math.max(0, context.argv.length - 3),
267
+});
268
+
269
+const getLoginCommandProperties = (context: CliCommandTelemetryContext) => ({
270
+ source: 'cli',
271
+ invocation_origin: getInvocationOrigin(),
272
+ cli_version: context.cliVersion,
273
+ command_path: context.commandPath,
274
+ duration_ms: Date.now() - context.startedAt,
275
+ no_browser: isFlagPresent(context.argv, '--no-browser'),
276
+ no_wait: isFlagPresent(context.argv, '--no-wait'),
277
+ has_key: isFlagPresent(context.argv, '--key'),
278
+ skip_org_picker: isFlagPresent(context.argv, '--yes', '-y'),
279
+});
280
+
281
+const getLogoutCommandProperties = (context: CliCommandTelemetryContext) => ({
282
+ source: 'cli',
283
+ invocation_origin: getInvocationOrigin(),
284
+ cli_version: context.cliVersion,
285
+ command_path: context.commandPath,
286
+ duration_ms: Date.now() - context.startedAt,
287
+});
288
+
289
+const getProxyCommandProperties = (context: CliCommandTelemetryContext) => ({
290
+ source: 'cli',
291
+ invocation_origin: getInvocationOrigin(),
292
+ cli_version: context.cliVersion,
293
+ command_path: context.commandPath,
294
+ duration_ms: Date.now() - context.startedAt,
295
+ endpoint: getTrailingPositionals(context)[0],
296
+ toolkit: getFlagValue(context.argv, '--toolkit', '-t'),
297
+ method: getFlagValue(context.argv, '--method', '-X') ?? 'GET',
298
+ has_data: isFlagPresent(context.argv, '--data', '-d'),
299
+ header_count: context.argv.slice(2).filter(token => token === '--header' || token === '-H')
300
+ .length,
301
+ skip_connection_check: isFlagPresent(context.argv, '--skip-connection-check'),
302
+});
303
+
304
+const isExecuteCommand = (commandPath: string): boolean =>
305
+ commandPath === 'execute' || commandPath === 'manage tools execute';
306
+
307
+const isSearchCommand = (commandPath: string): boolean =>
308
+ commandPath === 'search' || commandPath === 'manage tools search';
309
+
310
+const isLinkCommand = (commandPath: string): boolean =>
311
+ commandPath === 'link' || commandPath === 'manage connected-accounts link';
312
+
313
+const isLoginCommand = (commandPath: string): boolean => commandPath === 'login';
314
+
315
+const isLogoutCommand = (commandPath: string): boolean => commandPath === 'logout';
316
+
317
+const isProxyCommand = (commandPath: string): boolean => commandPath === 'proxy';
318
+
319
+const isRunCommand = (commandPath: string): boolean => commandPath === 'run';
320
+
321
+const isGenericOnlyCommand = (commandPath: string): boolean =>
322
+ commandPath === 'composio' || commandPath.startsWith('dev');
323
+
324
+export const createCliCommandTelemetryContext = (
325
+ argv: ReadonlyArray<string>,
326
+ cliVersion: string
327
+): CliCommandTelemetryContext => ({
328
+ argv,
329
+ cliVersion,
330
+ commandPath: extractCommandPath(argv),
331
+ flagNames: extractFlagNames(argv),
332
+ startedAt: Date.now(),
333
+ runId:
334
+ extractCommandPath(argv) === 'run'
335
+ ? (process.env.COMPOSIO_CLI_PARENT_RUN_ID ?? crypto.randomUUID())
336
+ : undefined,
337
+});
338
+
339
+const getCliCommandInvokedEvent = (context: CliCommandTelemetryContext): TrackEvent => ({
340
+ name: CLI_ANALYTICS_EVENTS.CLI_COMMAND_INVOKED,
341
+ properties: {
342
+ source: 'cli',
343
+ invocation_origin: getInvocationOrigin(),
344
+ cli_version: context.cliVersion,
345
+ command_path: context.commandPath,
346
+ flag_names: context.flagNames,
347
+ arg_count: Math.max(0, context.argv.length - 2),
348
+ stdout_is_tty: Boolean(process.stdout.isTTY),
349
+ stderr_is_tty: Boolean(process.stderr.isTTY),
350
+ },
351
+});
352
+
353
+const getCliCommandSucceededEvent = (context: CliCommandTelemetryContext): TrackEvent => ({
354
+ name: CLI_ANALYTICS_EVENTS.CLI_COMMAND_SUCCEEDED,
355
+ properties: {
356
+ source: 'cli',
357
+ invocation_origin: getInvocationOrigin(),
358
+ cli_version: context.cliVersion,
359
+ command_path: context.commandPath,
360
+ duration_ms: Date.now() - context.startedAt,
361
+ flag_names: context.flagNames,
362
+ },
363
+});
364
+
365
+const getCliCommandFailedEvent = (
366
+ context: CliCommandTelemetryContext,
367
+ error: unknown
368
+): TrackEvent => ({
369
+ name: CLI_ANALYTICS_EVENTS.CLI_COMMAND_FAILED,
370
+ properties: {
371
+ source: 'cli',
372
+ invocation_origin: getInvocationOrigin(),
373
+ cli_version: context.cliVersion,
374
+ command_path: context.commandPath,
375
+ duration_ms: Date.now() - context.startedAt,
376
+ flag_names: context.flagNames,
377
+ error_name: errorNameOf(error),
378
+ error_message: errorMessageOf(error),
379
+ },
380
+});
381
+
382
+type SpecialLifecycleFamily = {
383
+ readonly match: (commandPath: string) => boolean;
384
+ readonly invokedEventName: string;
385
+ readonly succeededEventName: string;
386
+ readonly failedEventName: string;
387
+ readonly getProperties: (context: CliCommandTelemetryContext) => Record<string, unknown>;
388
+};
389
+
390
+const SPECIAL_LIFECYCLE_FAMILIES: ReadonlyArray<SpecialLifecycleFamily> = [
391
+ {
392
+ match: isExecuteCommand,
393
+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_INVOKED,
394
+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_SUCCEEDED,
395
+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_FAILED,
396
+ getProperties: getExecuteCommandProperties,
397
+ },
398
+ {
399
+ match: isSearchCommand,
400
+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_SEARCH_INVOKED,
401
+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_SEARCH_SUCCEEDED,
402
+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_SEARCH_FAILED,
403
+ getProperties: getSearchCommandProperties,
404
+ },
405
+ {
406
+ match: isLinkCommand,
407
+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_LINK_INVOKED,
408
+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_LINK_SUCCEEDED,
409
+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_LINK_FAILED,
410
+ getProperties: getLinkCommandProperties,
411
+ },
412
+ {
413
+ match: isLoginCommand,
414
+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGIN_INVOKED,
415
+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_LOGIN_SUCCEEDED,
416
+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGIN_FAILED,
417
+ getProperties: getLoginCommandProperties,
418
+ },
419
+ {
420
+ match: isLogoutCommand,
421
+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGOUT_INVOKED,
422
+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_LOGOUT_SUCCEEDED,
423
+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGOUT_FAILED,
424
+ getProperties: getLogoutCommandProperties,
425
+ },
426
+ {
427
+ match: isProxyCommand,
428
+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_PROXY_INVOKED,
429
+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_PROXY_SUCCEEDED,
430
+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_PROXY_FAILED,
431
+ getProperties: getProxyCommandProperties,
432
+ },
433
+ {
434
+ match: isRunCommand,
435
+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_RUN_INVOKED,
436
+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_RUN_SUCCEEDED,
437
+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_RUN_FAILED,
438
+ getProperties: getRunCommandProperties,
439
+ },
440
+];
441
+
442
+const getSpecialLifecycleFamily = (commandPath: string): SpecialLifecycleFamily | undefined =>
443
+ SPECIAL_LIFECYCLE_FAMILIES.find(family => family.match(commandPath));
444
+
445
+export const getPrimaryLifecycleInvokedEvent = (
446
+ context: CliCommandTelemetryContext
447
+): TrackEvent => {
448
+ const family = getSpecialLifecycleFamily(context.commandPath);
449
+ if (!family || isGenericOnlyCommand(context.commandPath)) {
450
+ return getCliCommandInvokedEvent(context);
451
+ }
452
+ return {
453
+ name: family.invokedEventName,
454
+ properties: family.getProperties(context),
455
+ };
456
+};
457
+
458
+export const getPrimaryLifecycleSucceededEvent = (
459
+ context: CliCommandTelemetryContext
460
+): TrackEvent => {
461
+ const family = getSpecialLifecycleFamily(context.commandPath);
462
+ if (!family || isGenericOnlyCommand(context.commandPath)) {
463
+ return getCliCommandSucceededEvent(context);
464
+ }
465
+ return {
466
+ name: family.succeededEventName,
467
+ properties: family.getProperties(context),
468
+ };
469
+};
470
+
471
+export const getPrimaryLifecycleFailedEvent = (
472
+ context: CliCommandTelemetryContext,
473
+ error: unknown
474
+): TrackEvent => {
475
+ const family = getSpecialLifecycleFamily(context.commandPath);
476
+ if (!family || isGenericOnlyCommand(context.commandPath)) {
477
+ return getCliCommandFailedEvent(context, error);
478
+ }
479
+ return {
480
+ name: family.failedEventName,
481
+ properties: {
482
+ ...family.getProperties(context),
483
+ error_name: errorNameOf(error),
484
+ error_message: errorMessageOf(error),
485
+ },
486
+ };
487
+};
488
+
489
+export const getToolExecuteValidationFailedEvent = (params: {
490
+ readonly toolSlug: string;
491
+ readonly args: Record<string, unknown>;
492
+ readonly error: ToolInputValidationError;
493
+ readonly surface: 'root' | 'manage' | 'dev';
494
+ readonly projectMode: 'consumer' | 'developer';
495
+ readonly stage: 'dry_run' | 'validation' | 'execution';
496
+}): TrackEvent => ({
497
+ name: CLI_ANALYTICS_EVENTS.CLI_TOOL_INVOCATION_VALIDATION_FAILED,
498
+ properties: {
499
+ source: 'cli',
500
+ invocation_origin: getInvocationOrigin(),
501
+ tool_slug: params.toolSlug,
502
+ toolkit_slug: toolkitFromToolSlug(params.toolSlug),
503
+ surface: params.surface,
504
+ project_mode: params.projectMode,
505
+ stage: params.stage,
506
+ issue_count: params.error.issues.length,
507
+ issue_locations: extractIssueLocations(params.error.issues),
508
+ unknown_keys: extractUnknownKeys(params.error.issues),
509
+ schema_path: params.error.schemaPath,
510
+ ...argumentShape(params.args),
511
+ },
512
+});
513
+
514
+export const isMaybeToolNotFoundError = (params: {
515
+ readonly message?: string;
516
+ readonly errorSlug?: string;
517
+ readonly status?: number;
518
+}): boolean =>
519
+ params.status === 404 ||
520
+ params.errorSlug?.toLowerCase().includes('notfound') === true ||
521
+ TOOL_NOT_FOUND_PATTERN.test(params.message ?? '');
522
+
523
+export const getToolExecuteToolNotFoundEvent = (params: {
524
+ readonly toolSlug: string;
525
+ readonly args: Record<string, unknown>;
526
+ readonly surface: 'root' | 'manage' | 'dev';
527
+ readonly projectMode: 'consumer' | 'developer';
528
+ readonly stage: 'schema_fetch' | 'dry_run' | 'execution';
529
+ readonly errorSlug?: string;
530
+ readonly status?: number;
531
+ readonly apiCode?: number;
532
+ readonly message?: string;
533
+}): TrackEvent => ({
534
+ name: CLI_ANALYTICS_EVENTS.CLI_TOOL_INVOCATION_TOOL_NOT_FOUND,
535
+ properties: {
536
+ source: 'cli',
537
+ invocation_origin: getInvocationOrigin(),
538
+ tool_slug: params.toolSlug,
539
+ toolkit_slug: toolkitFromToolSlug(params.toolSlug),
540
+ surface: params.surface,
541
+ project_mode: params.projectMode,
542
+ stage: params.stage,
543
+ error_slug: params.errorSlug,
544
+ http_status: params.status,
545
+ api_error_code: params.apiCode,
546
+ error_message: params.message?.slice(0, 500),
547
+ ...argumentShape(params.args),
548
+ },
549
+});
550
+
551
+export const getToolExecuteFailedEvent = (params: {
552
+ readonly toolSlug: string;
553
+ readonly args: Record<string, unknown>;
554
+ readonly surface: 'root' | 'manage' | 'dev';
555
+ readonly projectMode: 'consumer' | 'developer';
556
+ readonly stage: 'schema_fetch' | 'dry_run' | 'execution';
557
+ readonly errorSlug?: string;
558
+ readonly status?: number;
559
+ readonly apiCode?: number;
560
+ readonly message?: string;
561
+ readonly errorName?: string;
562
+ readonly isNoConnectionError?: boolean;
563
+}): TrackEvent => ({
564
+ name: CLI_ANALYTICS_EVENTS.CLI_TOOL_INVOCATION_FAILED,
565
+ properties: {
566
+ source: 'cli',
567
+ invocation_origin: getInvocationOrigin(),
568
+ tool_slug: params.toolSlug,
569
+ toolkit_slug: toolkitFromToolSlug(params.toolSlug),
570
+ surface: params.surface,
571
+ project_mode: params.projectMode,
572
+ stage: params.stage,
573
+ error_slug: params.errorSlug,
574
+ http_status: params.status,
575
+ api_error_code: params.apiCode,
576
+ error_name: params.errorName,
577
+ error_message: params.message?.slice(0, 500),
578
+ is_no_connection_error: Boolean(params.isNoConnectionError),
579
+ ...argumentShape(params.args),
580
+ },
581
+});