1
import { cliAnalyticsProvenance } from 'src/services/client-provenance';
2
-import { Option, Schema } from 'effect';
2
+import { Option } from 'effect';
3
import { CliError } from 'effect/cli';
4
import type { CliCommandTelemetryContext, TelemetryFamily, TrackEvent } from './types';
5
import { type CommandIdentity, commandPathOf } from 'src/invocation/command-identity';
171
3702, // ComposioTools_ValidationError
172
]);
173
174
-const extractFlagNames = (argv: ReadonlyArray<string>): ReadonlyArray<string> =>
175
- [
176
- ...new Set(
177
- argv
178
- .slice(2)
179
- .filter(token => token.startsWith('-'))
180
- .map(token => token.split('=')[0]!)
181
- ),
182
- ].sort();
183
-
174
const argumentShape = (args: Record<string, unknown>) => {
175
const keys = Object.keys(args).sort();
176
return {
213
return {};
214
};
215
226
-const isFlagPresent = (argv: ReadonlyArray<string>, ...flags: string[]): boolean =>
227
- argv.slice(2).some(token => {
228
- if (flags.includes(token)) return true;
229
- return flags.some(flag => token.startsWith(`${flag}=`));
230
- });
216
+const commandProperties = (context: CliCommandTelemetryContext) => ({
217
+ ...context.options,
218
+ source: 'cli',
219
+ invocation_origin: context.invocationOrigin,
220
+ cli_version: context.cliVersion,
221
+ command_path: context.commandPath,
222
+ duration_ms: Date.now() - context.startedAt,
223
+});
224
232
-const getFlagValue = (argv: ReadonlyArray<string>, ...flags: string[]): string | undefined => {
233
- const args = argv.slice(2);
234
- for (let index = 0; index < args.length; index += 1) {
235
- const token = args[index];
236
- if (!token) continue;
237
-
238
- for (const flag of flags) {
239
- if (token === flag) {
240
- const next = args[index + 1];
241
- return next && !next.startsWith('-') ? next : undefined;
242
- }
243
- if (token.startsWith(`${flag}=`)) {
244
- return token.slice(flag.length + 1);
245
- }
246
- }
247
- }
248
- return undefined;
249
-};
225
+const parentProperties = (context: CliCommandTelemetryContext) => ({
226
+ ...commandProperties(context),
227
+ parent_run_id: context.parentRunId,
228
+ parent_command: context.parentRunId ? 'run' : undefined,
229
+});
230
231
const getExecuteCommandProperties = (context: CliCommandTelemetryContext) => {
232
const slug = context.argument;
233
return {
254
- source: 'cli',
255
- invocation_origin: context.invocationOrigin,
256
- parent_run_id: context.parentRunId,
257
- parent_command: context.parentRunId ? 'run' : undefined,
258
- cli_version: context.cliVersion,
259
- command_path: context.commandPath,
260
- duration_ms: Date.now() - context.startedAt,
234
+ ...parentProperties(context),
235
surface: context.commandPath === 'execute' ? 'root' : 'dev',
236
tool_slug: slug,
237
tool_name: slug,
238
toolkit_slug:
239
context.toolkitSlug ??
240
(typeof slug === 'string' ? guessToolkitFromToolSlug(slug) : undefined),
267
- dry_run: isFlagPresent(context.argv, '--dry-run'),
268
- get_schema: isFlagPresent(context.argv, '--get-schema'),
269
- has_data: isFlagPresent(context.argv, '--data', '-d'),
270
- skip_connection_check: isFlagPresent(context.argv, '--skip-connection-check'),
271
- skip_tool_params_check: isFlagPresent(context.argv, '--skip-tool-params-check'),
272
- skip_checks: isFlagPresent(context.argv, '--skip-checks'),
241
};
242
};
243
276
-// Search queries are user-authored free text, so only their shape is recorded.
277
-const getSearchCommandProperties = (context: CliCommandTelemetryContext) => {
278
- const query = context.argument ?? '';
279
- return {
280
- source: 'cli',
281
- invocation_origin: context.invocationOrigin,
282
- parent_run_id: context.parentRunId,
283
- parent_command: context.parentRunId ? 'run' : undefined,
284
- cli_version: context.cliVersion,
285
- command_path: context.commandPath,
286
- duration_ms: Date.now() - context.startedAt,
287
- query_length: query.length,
288
- query_term_count: query.split(/\s+/u).filter(Boolean).length,
289
- toolkits: getFlagValue(context.argv, '--toolkits'),
290
- limit: getFlagValue(context.argv, '--limit'),
291
- };
292
-};
293
-
294
-const getLinkCommandProperties = (context: CliCommandTelemetryContext) => {
295
- return {
296
- source: 'cli',
297
- invocation_origin: context.invocationOrigin,
298
- parent_run_id: context.parentRunId,
299
- parent_command: context.parentRunId ? 'run' : undefined,
300
- cli_version: context.cliVersion,
301
- command_path: context.commandPath,
302
- duration_ms: Date.now() - context.startedAt,
303
- toolkit: context.argument,
304
- no_browser: isFlagPresent(context.argv, '--no-browser'),
305
- no_wait: isFlagPresent(context.argv, '--no-wait'),
306
- has_auth_config: isFlagPresent(context.argv, '--auth-config'),
307
- };
308
-};
244
+const getLinkCommandProperties = (context: CliCommandTelemetryContext) => ({
245
+ ...parentProperties(context),
246
+ toolkit: context.argument,
247
+});
248
249
const getRunCommandProperties = (context: CliCommandTelemetryContext) => ({
311
- source: 'cli',
312
- invocation_origin: context.invocationOrigin,
250
+ ...commandProperties(context),
251
run_id: context.runId,
314
- cli_version: context.cliVersion,
315
- command_path: context.commandPath,
316
- duration_ms: Date.now() - context.startedAt,
317
- debug: isFlagPresent(context.argv, '--debug'),
318
- dry_run: isFlagPresent(context.argv, '--dry-run'),
319
- skip_connection_check: isFlagPresent(context.argv, '--skip-connection-check'),
320
- skip_tool_params_check: isFlagPresent(context.argv, '--skip-tool-params-check'),
321
- skip_checks: isFlagPresent(context.argv, '--skip-checks'),
322
- file_mode: isFlagPresent(context.argv, '--file', '-f'),
323
- arg_count: Math.max(0, context.argv.length - 3),
324
-});
325
-
326
-const getInstallCommandProperties = (context: CliCommandTelemetryContext) => ({
327
- source: 'cli',
328
- invocation_origin: context.invocationOrigin,
329
- cli_version: context.cliVersion,
330
- command_path: context.commandPath,
331
- duration_ms: Date.now() - context.startedAt,
332
- completions: isFlagPresent(context.argv, '--completions'),
333
- no_completions: isFlagPresent(context.argv, '--no-completions'),
252
});
253
254
const getSetupCommandProperties = (context: CliCommandTelemetryContext) => ({
337
- source: 'cli',
338
- invocation_origin: context.invocationOrigin,
339
- cli_version: context.cliVersion,
340
- command_path: context.commandPath,
341
- duration_ms: Date.now() - context.startedAt,
342
- operation: isFlagPresent(context.argv, '--uninstall') ? 'uninstall' : 'setup',
343
- target: getFlagValue(context.argv, '--target') ?? 'auto',
344
- yes: isFlagPresent(context.argv, '--yes', '-y'),
345
- if_present: isFlagPresent(context.argv, '--if-present'),
255
+ ...commandProperties(context),
256
stdout_is_tty: context.stdoutIsTTY,
257
});
258
349
-const getLoginCommandProperties = (context: CliCommandTelemetryContext) => ({
350
- source: 'cli',
351
- invocation_origin: context.invocationOrigin,
352
- cli_version: context.cliVersion,
353
- command_path: context.commandPath,
354
- duration_ms: Date.now() - context.startedAt,
355
- no_browser: isFlagPresent(context.argv, '--no-browser'),
356
- no_wait: isFlagPresent(context.argv, '--no-wait'),
357
- has_key: isFlagPresent(context.argv, '--key'),
358
- skip_org_picker: isFlagPresent(context.argv, '--yes', '-y'),
359
-});
360
-
361
-const getLogoutCommandProperties = (context: CliCommandTelemetryContext) => ({
362
- source: 'cli',
363
- invocation_origin: context.invocationOrigin,
364
- cli_version: context.cliVersion,
365
- command_path: context.commandPath,
366
- duration_ms: Date.now() - context.startedAt,
367
-});
368
-
369
-const getProxyCommandProperties = (context: CliCommandTelemetryContext) => ({
370
- source: 'cli',
371
- invocation_origin: context.invocationOrigin,
372
- cli_version: context.cliVersion,
373
- command_path: context.commandPath,
374
- duration_ms: Date.now() - context.startedAt,
375
- has_endpoint: typeof context.argument === 'string',
376
- toolkit: getFlagValue(context.argv, '--toolkit', '-t'),
377
- method: getFlagValue(context.argv, '--method', '-X') ?? 'GET',
378
- has_data: isFlagPresent(context.argv, '--data', '-d'),
379
- header_count: context.argv.slice(2).filter(token => token === '--header' || token === '-H')
380
- .length,
381
- skip_connection_check: isFlagPresent(context.argv, '--skip-connection-check'),
382
-});
383
-
384
-const firstOf = (values: ReadonlyArray<string>) => values[0];
385
-
386
-/**
387
- * How each family that describes an input argument finds it in the input `effect/cli` parsed for
388
- * the command (see `CliCommandTelemetryContext.argument`).
389
- */
390
-const FAMILY_ARGUMENTS: Partial<
391
- Record<TelemetryFamily, (input: unknown) => Option.Option<string>>
392
-> = {
393
- execute: input =>
394
- Option.map(
395
- Schema.decodeUnknownOption(Schema.Struct({ slug: Schema.String }))(input),
396
- ({ slug }) => slug
397
- ),
398
- search: input =>
399
- Option.flatMap(
400
- Schema.decodeUnknownOption(Schema.Struct({ query: Schema.Array(Schema.String) }))(input),
401
- ({ query }) => Option.fromNullishOr(firstOf(query))
402
- ),
403
- link: input =>
404
- Option.flatMap(
405
- Schema.decodeUnknownOption(Schema.Struct({ toolkit: Schema.Option(Schema.String) }))(input),
406
- ({ toolkit }) => toolkit
407
- ),
408
- proxy: input =>
409
- Option.map(
410
- Schema.decodeUnknownOption(Schema.Struct({ endpoint: Schema.String }))(input),
411
- ({ endpoint }) => endpoint
412
- ),
413
-};
414
-
415
-const familyArgument = (
416
- family: TelemetryFamily,
417
- input: Option.Option<unknown>
418
-): string | undefined =>
419
- Option.getOrUndefined(
420
- Option.flatMap(input, parsed =>
421
- Option.flatMap(Option.fromNullishOr(FAMILY_ARGUMENTS[family]), argument => argument(parsed))
422
- )
423
- );
424
-
259
/**
260
* Builds the telemetry context of the command `identity` names, with the input `effect/cli`
261
* parsed for it (none for help, version, and parse failures).
281
parentRunId: invocation.parentRunId,
282
commandPath: commandPathOf(identity),
283
family,
450
- argument: familyArgument(family, params.input),
451
- flagNames: extractFlagNames(argv),
284
+ ...Option.getOrElse(
285
+ Option.flatMap(params.input, input =>
286
+ Option.map(Option.fromNullishOr(identity.meta.telemetryInput), project => project(input))
287
+ ),
288
+ () => ({ options: {}, flagNames: [] })
289
+ ),
290
stdoutIsTTY: terminal.stdoutIsTTY,
291
stderrIsTTY: terminal.stderrIsTTY,
292
startedAt: params.startedAt,
352
invokedEventName: CLI_ANALYTICS_EVENTS.CLI_SEARCH_INVOKED,
353
succeededEventName: CLI_ANALYTICS_EVENTS.CLI_SEARCH_SUCCEEDED,
354
failedEventName: CLI_ANALYTICS_EVENTS.CLI_SEARCH_FAILED,
517
- getProperties: getSearchCommandProperties,
355
+ getProperties: parentProperties,
356
},
357
link: {
358
invokedEventName: CLI_ANALYTICS_EVENTS.CLI_LINK_INVOKED,
364
invokedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGIN_INVOKED,
365
succeededEventName: CLI_ANALYTICS_EVENTS.CLI_LOGIN_SUCCEEDED,
366
failedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGIN_FAILED,
529
- getProperties: getLoginCommandProperties,
367
+ getProperties: commandProperties,
368
},
369
logout: {
370
invokedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGOUT_INVOKED,
371
succeededEventName: CLI_ANALYTICS_EVENTS.CLI_LOGOUT_SUCCEEDED,
372
failedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGOUT_FAILED,
535
- getProperties: getLogoutCommandProperties,
373
+ getProperties: commandProperties,
374
},
375
proxy: {
376
invokedEventName: CLI_ANALYTICS_EVENTS.CLI_PROXY_INVOKED,
377
succeededEventName: CLI_ANALYTICS_EVENTS.CLI_PROXY_SUCCEEDED,
378
failedEventName: CLI_ANALYTICS_EVENTS.CLI_PROXY_FAILED,
541
- getProperties: getProxyCommandProperties,
379
+ getProperties: commandProperties,
380
},
381
run: {
382
invokedEventName: CLI_ANALYTICS_EVENTS.CLI_RUN_INVOKED,
388
invokedEventName: CLI_ANALYTICS_EVENTS.CLI_INSTALL_INVOKED,
389
succeededEventName: CLI_ANALYTICS_EVENTS.CLI_INSTALL_SUCCEEDED,
390
failedEventName: CLI_ANALYTICS_EVENTS.CLI_INSTALL_FAILED,
553
- getProperties: getInstallCommandProperties,
391
+ getProperties: commandProperties,
392
},
393
setup: {
394
invokedEventName: CLI_ANALYTICS_EVENTS.CLI_SETUP_INVOKED,