Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
feat(cli): add telemetry worker and improve tool execution UX (#2993)
Rahul Tarak3b41c475b367
diff --git a/ts/packages/cli/src/analytics/dispatch.ts b/ts/packages/cli/src/analytics/dispatch.tsnew file mode 100644index 000000000..4bf9814a0--- /dev/null+++ b/ts/packages/cli/src/analytics/dispatch.ts@@ -0,0 +1,365 @@+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import process from 'node:process';+import { spawn } from 'node:child_process';+import { Effect } from 'effect';+import type { AnalyticsEnvelope, TrackEvent } from './types';+import * as constants from 'src/constants';++const INTERNAL_ANALYTICS_WORKER_FLAG = '__analytics-worker';+const COMPOSIO_DIR = '.composio';+const ANALYTICS_STATE_FILE_NAME = 'analytics.json';+const CONSUMER_SHORT_TERM_CACHE_FILE_NAME = 'consumer-short-term-cache.json';+const CLI_ANALYTICS_PATH = '/api/v3/cli/analytics';+const TELEMETRY_DEBUG_ENV_VAR = 'COMPOSIO_CLI_TELEMETRY_DEBUG';++const truthy = (value: string | undefined): boolean =>+ value === '1' || value === 'true' || value === 'yes' || value === 'on';++const isTelemetryDebugEnabled = (): boolean => truthy(process.env[TELEMETRY_DEBUG_ENV_VAR]);++const telemetryDebugLog = (label: string, payload: Record<string, unknown>) => {+ if (!isTelemetryDebugEnabled()) {+ return;+ }++ process.stderr.write(+ `[telemetry-debug] ${JSON.stringify(+ {+ label,+ ...payload,+ },+ null,+ 2+ )}\n`+ );+};++const analyticsDir = () => path.join(os.homedir(), COMPOSIO_DIR);+const analyticsStatePath = () => path.join(analyticsDir(), ANALYTICS_STATE_FILE_NAME);+const userConfigPath = () => path.join(analyticsDir(), constants.USER_CONFIG_FILE_NAME);+const cacheDir = () =>+ process.env.COMPOSIO_CACHE_DIR?.trim() ||+ process.env.CACHE_DIR?.trim() ||+ path.join(os.homedir(), constants.USER_COMPOSIO_DIR);+const consumerShortTermCachePath = () => path.join(cacheDir(), CONSUMER_SHORT_TERM_CACHE_FILE_NAME);++type ConsumerShortTermCacheState = Record<+ string,+ {+ readonly probablyMyCliSessionsByCwdHash?: Record<+ string,+ {+ readonly id: string;+ readonly expiresAt: string;+ }+ >;+ }+>;++const ensureAnalyticsDir = () => {+ fs.mkdirSync(analyticsDir(), { recursive: true });+};++const encodeBase64Url = (value: string): string => {+ const bytes = new TextEncoder().encode(value);+ let binary = '';+ for (const byte of bytes) {+ binary += String.fromCharCode(byte);+ }+ return btoa(binary).replace(/\+/gu, '-').replace(/\//gu, '_').replace(/=+$/u, '');+};++const decodeBase64Url = (value: string): string => {+ const normalized = value.replace(/-/gu, '+').replace(/_/gu, '/');+ const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, '=');+ const binary = atob(padded);+ const bytes = Uint8Array.from(binary, char => char.charCodeAt(0));+ return new TextDecoder().decode(bytes);+};++const djb2Hash = (value: string): number => {+ let hash = 5381;+ for (let index = 0; index < value.length; index += 1) {+ hash = (hash * 33) ^ value.charCodeAt(index);+ }+ return Math.abs(hash >>> 0);+};++const hashString = (value: string): string => djb2Hash(value).toString(16).padStart(8, '0');++const getOrCreateInstallId = (): string => {+ try {+ ensureAnalyticsDir();+ const filePath = analyticsStatePath();+ if (fs.existsSync(filePath)) {+ const raw = fs.readFileSync(filePath, 'utf8');+ const parsed = JSON.parse(raw) as { install_id?: unknown };+ if (typeof parsed.install_id === 'string' && parsed.install_id.length > 0) {+ return parsed.install_id;+ }+ }+ const installId = crypto.randomUUID();+ fs.writeFileSync(+ filePath,+ JSON.stringify(+ {+ install_id: installId,+ created_at: new Date().toISOString(),+ },+ null,+ 2+ ),+ 'utf8'+ );+ return installId;+ } catch {+ return crypto.randomUUID();+ }+};++const getHashedApiKeyDistinctId = (): string | null => {+ const envApiKey = process.env.COMPOSIO_USER_API_KEY?.trim();+ if (envApiKey) {+ return `user_${hashString(envApiKey)}`;+ }++ try {+ const raw = fs.readFileSync(userConfigPath(), 'utf8');+ const parsed = JSON.parse(raw) as { api_key?: unknown };+ if (typeof parsed.api_key === 'string' && parsed.api_key.trim().length > 0) {+ return `user_${hashString(parsed.api_key.trim())}`;+ }+ } catch {+ // Ignore user config read failures.+ }++ return null;+};++const getDistinctId = (): string => getHashedApiKeyDistinctId() ?? `anon_${getOrCreateInstallId()}`;++const getUserApiKey = (): string | null => {+ const envApiKey = process.env.COMPOSIO_USER_API_KEY?.trim();+ if (envApiKey) {+ return envApiKey;+ }++ try {+ const raw = fs.readFileSync(userConfigPath(), 'utf8');+ const parsed = JSON.parse(raw) as { api_key?: unknown };+ if (typeof parsed.api_key === 'string' && parsed.api_key.trim().length > 0) {+ return parsed.api_key.trim();+ }+ } catch {+ // Ignore user config read failures.+ }++ return null;+};++const cwdHash = (cwd: string): string => djb2Hash(cwd).toString(36);++const getCurrentCwdSessionId = (): string | undefined => {+ try {+ const raw = fs.readFileSync(consumerShortTermCachePath(), 'utf8');+ const parsed = JSON.parse(raw) as ConsumerShortTermCacheState;+ const currentCwdHash = cwdHash(process.cwd());+ const now = Date.now();+ let best: { id: string; expiresAtMs: number } | undefined;++ for (const entry of Object.values(parsed)) {+ const session = entry.probablyMyCliSessionsByCwdHash?.[currentCwdHash];+ if (!session?.id) continue;+ const expiresAtMs = Date.parse(session.expiresAt);+ if (!Number.isFinite(expiresAtMs) || expiresAtMs <= now) continue;+ if (!best || expiresAtMs > best.expiresAtMs) {+ best = { id: session.id, expiresAtMs };+ }+ }++ return best?.id;+ } catch {+ return undefined;+ }+};++const withCliSessionId = (event: TrackEvent): TrackEvent => {+ if (!event) return event;+ const cliSessionId = getCurrentCwdSessionId();+ if (!cliSessionId) return event;+ return {+ ...event,+ properties: {+ ...(event.properties ?? {}),+ cli_session_id: cliSessionId,+ },+ };+};++const readApiBaseUrl = (): string | null => {+ const envBaseUrl = process.env.COMPOSIO_BASE_URL?.trim();+ if (envBaseUrl) {+ return envBaseUrl.replace(/\/+$/u, '');+ }++ try {+ const raw = fs.readFileSync(userConfigPath(), 'utf8');+ const parsed = JSON.parse(raw) as { base_url?: unknown };+ if (typeof parsed.base_url === 'string' && parsed.base_url.trim().length > 0) {+ return parsed.base_url.trim().replace(/\/+$/u, '');+ }+ } catch {+ // Ignore user config read failures.+ }++ return null;+};++const getAnalyticsEndpoint = (): string | null => {+ const baseUrl = readApiBaseUrl();+ return baseUrl ? `${baseUrl}${CLI_ANALYTICS_PATH}` : null;+};++const shouldDisableAnalytics = (): boolean =>+ truthy(process.env.COMPOSIO_CLI_TELEMETRY_DISABLED) ||+ truthy(process.env.TELEMETRY_DISABLED) ||+ truthy(process.env.COMPOSIO_DISABLE_TELEMETRY) ||+ process.env.NODE_ENV === 'test' ||+ process.env.CI === 'true';++const getWorkerSpawnArgs = (encodedPayload: string): { command: string; args: string[] } => {+ const maybeScriptPath = process.argv[1];+ const scriptPathLooksReal =+ typeof maybeScriptPath === 'string' &&+ maybeScriptPath.length > 0 &&+ fs.existsSync(maybeScriptPath) &&+ /\.(?:[cm]?[jt]s|mjs|mts|cts)$/u.test(maybeScriptPath);++ return scriptPathLooksReal+ ? {+ command: process.execPath,+ args: [maybeScriptPath, INTERNAL_ANALYTICS_WORKER_FLAG, encodedPayload],+ }+ : {+ command: process.execPath,+ args: [INTERNAL_ANALYTICS_WORKER_FLAG, encodedPayload],+ };+};++const captureToComposioAnalytics = async (envelope: AnalyticsEnvelope): Promise<void> => {+ const endpoint = getAnalyticsEndpoint();+ if (!endpoint || shouldDisableAnalytics()) {+ telemetryDebugLog('delivery_skipped', {+ reason: shouldDisableAnalytics() ? 'disabled' : 'missing_endpoint',+ endpoint,+ eventName: envelope.event,+ });+ return;+ }++ const userApiKey = getUserApiKey();+ const response = await fetch(endpoint, {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ 'x-composio-analytics-source': 'cli',+ ...(userApiKey ? { 'x-user-api-key': userApiKey } : {}),+ },+ body: JSON.stringify(envelope),+ });+ const responseBody =+ !response.ok && isTelemetryDebugEnabled() ? await response.text() : undefined;++ telemetryDebugLog(response.ok ? 'delivery_succeeded' : 'delivery_failed', {+ endpoint,+ eventName: envelope.event,+ status: response.status,+ ok: response.ok,+ responseBody: responseBody?.slice(0, 1000),+ });+};++export const trackCliEvent = (event: TrackEvent): void => {+ const endpoint = getAnalyticsEndpoint();+ if (!event) {+ return;+ }++ if (shouldDisableAnalytics() || !endpoint) {+ telemetryDebugLog('skip', {+ reason: shouldDisableAnalytics() ? 'disabled' : 'missing_endpoint',+ eventName: event.name,+ endpoint,+ });+ return;+ }++ try {+ const enrichedEvent = withCliSessionId(event);+ if (!enrichedEvent) {+ return;+ }+ const installId = getOrCreateInstallId();+ const distinctId = getDistinctId();+ const envelope: AnalyticsEnvelope = {+ event: enrichedEvent.name,+ ...(enrichedEvent.properties ? { properties: enrichedEvent.properties } : {}),+ sentAt: new Date().toISOString(),+ source: 'cli',+ distinctId,+ installId,+ };+ telemetryDebugLog('enqueue', {+ endpoint,+ envelope,+ });+ const encodedPayload = encodeBase64Url(JSON.stringify(envelope));+ const { command, args } = getWorkerSpawnArgs(encodedPayload);+ const child = spawn(command, args, {+ detached: true,+ stdio: isTelemetryDebugEnabled() ? ['ignore', 'ignore', 'inherit'] : 'ignore',+ env: {+ ...process.env,+ COMPOSIO_CLI_ANALYTICS_WORKER: '1',+ },+ });+ child.unref();+ } catch {+ // Analytics must never break CLI execution.+ }+};++export const trackCliEventEffect = (event: TrackEvent) => Effect.sync(() => trackCliEvent(event));++const getAnalyticsWorkerFlagIndex = (argv: ReadonlyArray<string>): number =>+ argv.findIndex(token => token === INTERNAL_ANALYTICS_WORKER_FLAG);++export const isAnalyticsWorkerInvocation = (argv: ReadonlyArray<string>): boolean =>+ getAnalyticsWorkerFlagIndex(argv) >= 0;++export const runAnalyticsWorkerFromArgv = async (argv: ReadonlyArray<string>): Promise<void> => {+ const flagIndex = getAnalyticsWorkerFlagIndex(argv);+ const encodedPayload = flagIndex >= 0 ? argv[flagIndex + 1] : undefined;+ if (!encodedPayload) {+ return;+ }++ try {+ const decoded = decodeBase64Url(encodedPayload);+ const envelope = JSON.parse(decoded) as AnalyticsEnvelope;+ if (typeof envelope?.event !== 'string' || envelope.event.length === 0) {+ return;+ }+ await captureToComposioAnalytics(envelope);+ } catch (error) {+ telemetryDebugLog('delivery_error', {+ error:+ error instanceof Error+ ? { name: error.name, message: error.message }+ : { message: String(error) },+ });+ // Analytics must never break CLI execution.+ }+};diff --git a/ts/packages/cli/src/analytics/events.ts b/ts/packages/cli/src/analytics/events.tsnew file mode 100644index 000000000..8d30fe2c1--- /dev/null+++ b/ts/packages/cli/src/analytics/events.ts@@ -0,0 +1,581 @@+import type { CliCommandTelemetryContext, TrackEvent } from './types';+import { ToolInputValidationError } from 'src/services/tool-input-validation';+import { toolkitFromToolSlug } from 'src/utils/toolkit-from-tool-slug';++export const CLI_ANALYTICS_EVENTS = {+ CLI_COMMAND_INVOKED: 'CLI_COMMAND_INVOKED',+ CLI_COMMAND_SUCCEEDED: 'CLI_COMMAND_SUCCEEDED',+ CLI_COMMAND_FAILED: 'CLI_COMMAND_FAILED',+ CLI_EXECUTE_INVOKED: 'CLI_EXECUTE_INVOKED',+ CLI_EXECUTE_SUCCEEDED: 'CLI_EXECUTE_SUCCEEDED',+ CLI_EXECUTE_FAILED: 'CLI_EXECUTE_FAILED',+ CLI_SEARCH_INVOKED: 'CLI_SEARCH_INVOKED',+ CLI_SEARCH_SUCCEEDED: 'CLI_SEARCH_SUCCEEDED',+ CLI_SEARCH_FAILED: 'CLI_SEARCH_FAILED',+ CLI_LINK_INVOKED: 'CLI_LINK_INVOKED',+ CLI_LINK_SUCCEEDED: 'CLI_LINK_SUCCEEDED',+ CLI_LINK_FAILED: 'CLI_LINK_FAILED',+ CLI_LOGIN_INVOKED: 'CLI_LOGIN_INVOKED',+ CLI_LOGIN_SUCCEEDED: 'CLI_LOGIN_SUCCEEDED',+ CLI_LOGIN_FAILED: 'CLI_LOGIN_FAILED',+ CLI_LOGOUT_INVOKED: 'CLI_LOGOUT_INVOKED',+ CLI_LOGOUT_SUCCEEDED: 'CLI_LOGOUT_SUCCEEDED',+ CLI_LOGOUT_FAILED: 'CLI_LOGOUT_FAILED',+ CLI_PROXY_INVOKED: 'CLI_PROXY_INVOKED',+ CLI_PROXY_SUCCEEDED: 'CLI_PROXY_SUCCEEDED',+ CLI_PROXY_FAILED: 'CLI_PROXY_FAILED',+ CLI_RUN_INVOKED: 'CLI_RUN_INVOKED',+ CLI_RUN_SUCCEEDED: 'CLI_RUN_SUCCEEDED',+ CLI_RUN_FAILED: 'CLI_RUN_FAILED',+ CLI_TOOL_INVOCATION_VALIDATION_FAILED: 'CLI_TOOL_INVOCATION_VALIDATION_FAILED',+ CLI_TOOL_INVOCATION_TOOL_NOT_FOUND: 'CLI_TOOL_INVOCATION_TOOL_NOT_FOUND',+ CLI_TOOL_INVOCATION_FAILED: 'CLI_TOOL_INVOCATION_FAILED',+} as const;++const KNOWN_COMMAND_TOKENS = new Set([+ 'version',+ 'upgrade',+ 'whoami',+ 'login',+ 'logout',+ 'run',+ 'install',+ 'dev',+ 'generate',+ 'manage',+ 'tools',+ 'toolkits',+ 'toolkit',+ 'search',+ 'execute',+ 'link',+ 'proxy',+ 'artifacts',+ 'cwd',+ 'connected-accounts',+ 'auth-configs',+ 'triggers',+ 'logs',+ 'orgs',+ 'projects',+ 'info',+ 'list',+ 'create',+ 'delete',+ 'enable',+ 'disable',+ 'status',+ 'listen',+ 'switch',+ 'py',+ 'ts',+]);++const TOOL_NOT_FOUND_PATTERN = /\btool\b.*\bnot found\b/i;+const getInvocationOrigin = (): string => process.env.COMPOSIO_CLI_INVOCATION_ORIGIN ?? 'cli';+const getParentRunId = (): string | undefined => process.env.COMPOSIO_CLI_PARENT_RUN_ID;++const extractCommandPath = (argv: ReadonlyArray<string>): string => {+ const commandTokens: string[] = [];++ for (const token of argv.slice(2)) {+ if (!token || token.startsWith('-') || !KNOWN_COMMAND_TOKENS.has(token)) {+ break;+ }+ commandTokens.push(token);+ }++ return commandTokens.length > 0 ? commandTokens.join(' ') : 'composio';+};++const extractFlagNames = (argv: ReadonlyArray<string>): ReadonlyArray<string> =>+ [+ ...new Set(+ argv+ .slice(2)+ .filter(token => token.startsWith('-'))+ .map(token => token.split('=')[0]!)+ ),+ ].sort();++const argumentShape = (args: Record<string, unknown>) => {+ const keys = Object.keys(args).sort();+ return {+ argument_key_count: keys.length,+ argument_keys: keys.slice(0, 50),+ };+};++const extractIssueLocations = (issues: ReadonlyArray<string>): ReadonlyArray<string> =>+ [+ ...new Set(+ issues+ .map(issue => issue.match(/^([^:]+):/u)?.[1]?.trim())+ .filter((value): value is string => typeof value === 'string' && value.length > 0)+ ),+ ].slice(0, 20);++const extractUnknownKeys = (issues: ReadonlyArray<string>): ReadonlyArray<string> =>+ [+ ...new Set(+ issues+ .map(issue => issue.match(/Unknown key "([^"]+)"/u)?.[1])+ .filter((value): value is string => typeof value === 'string' && value.length > 0)+ ),+ ].slice(0, 20);++const errorNameOf = (error: unknown): string =>+ error instanceof Error && error.name ? error.name : 'UnknownError';++const errorMessageOf = (error: unknown): string => {+ if (error instanceof Error && error.message) return error.message.slice(0, 500);+ if (typeof error === 'string') return error.slice(0, 500);+ return 'Unknown error';+};++const isFlagPresent = (argv: ReadonlyArray<string>, ...flags: string[]): boolean =>+ argv.slice(2).some(token => {+ if (flags.includes(token)) return true;+ return flags.some(flag => token.startsWith(`${flag}=`));+ });++const getFlagValue = (argv: ReadonlyArray<string>, ...flags: string[]): string | undefined => {+ const args = argv.slice(2);+ for (let index = 0; index < args.length; index += 1) {+ const token = args[index];+ if (!token) continue;++ for (const flag of flags) {+ if (token === flag) {+ const next = args[index + 1];+ return next && !next.startsWith('-') ? next : undefined;+ }+ if (token.startsWith(`${flag}=`)) {+ return token.slice(flag.length + 1);+ }+ }+ }+ return undefined;+};++const getTrailingPositionals = (context: CliCommandTelemetryContext): ReadonlyArray<string> => {+ const commandTokenCount =+ context.commandPath === 'composio' ? 0 : context.commandPath.split(' ').length;+ const args = context.argv.slice(2 + commandTokenCount);+ const positionals: string[] = [];++ for (let index = 0; index < args.length; index += 1) {+ const token = args[index];+ if (!token) continue;++ if (token === '--') {+ break;+ }++ if (token.startsWith('-')) {+ const expectsValue = [+ '--data',+ '-d',+ '--toolkits',+ '--limit',+ '--user-id',+ '--project-name',+ '--auth-config',+ '--file',+ '-f',+ ].includes(token);+ if (expectsValue) {+ index += 1;+ }+ continue;+ }++ positionals.push(token);+ }++ return positionals;+};++const getExecuteCommandProperties = (context: CliCommandTelemetryContext) => {+ const slug = getTrailingPositionals(context)[0];+ return {+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ parent_run_id: getParentRunId(),+ parent_command: getParentRunId() ? 'run' : undefined,+ cli_version: context.cliVersion,+ command_path: context.commandPath,+ duration_ms: Date.now() - context.startedAt,+ surface: context.commandPath === 'execute' ? 'root' : 'manage',+ tool_slug: slug,+ tool_name: slug,+ toolkit_slug: typeof slug === 'string' ? toolkitFromToolSlug(slug) : undefined,+ dry_run: isFlagPresent(context.argv, '--dry-run'),+ get_schema: isFlagPresent(context.argv, '--get-schema'),+ has_data: isFlagPresent(context.argv, '--data', '-d'),+ skip_connection_check: isFlagPresent(context.argv, '--skip-connection-check'),+ skip_tool_params_check: isFlagPresent(context.argv, '--skip-tool-params-check'),+ no_verify: isFlagPresent(context.argv, '--no-verify'),+ };+};++const getSearchCommandProperties = (context: CliCommandTelemetryContext) => ({+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ parent_run_id: getParentRunId(),+ parent_command: getParentRunId() ? 'run' : undefined,+ cli_version: context.cliVersion,+ command_path: context.commandPath,+ duration_ms: Date.now() - context.startedAt,+ query: getTrailingPositionals(context)[0],+ search_query: getTrailingPositionals(context)[0],+ toolkits: getFlagValue(context.argv, '--toolkits'),+ limit: getFlagValue(context.argv, '--limit'),+});++const getLinkCommandProperties = (context: CliCommandTelemetryContext) => {+ const firstPositional = getTrailingPositionals(context)[0];+ return {+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ parent_run_id: getParentRunId(),+ parent_command: getParentRunId() ? 'run' : undefined,+ cli_version: context.cliVersion,+ command_path: context.commandPath,+ duration_ms: Date.now() - context.startedAt,+ toolkit: getFlagValue(context.argv, '--toolkit') ?? firstPositional,+ no_browser: isFlagPresent(context.argv, '--no-browser'),+ no_wait: isFlagPresent(context.argv, '--no-wait'),+ has_auth_config: isFlagPresent(context.argv, '--auth-config'),+ };+};++const getRunCommandProperties = (context: CliCommandTelemetryContext) => ({+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ run_id: context.runId,+ cli_version: context.cliVersion,+ command_path: context.commandPath,+ duration_ms: Date.now() - context.startedAt,+ debug: isFlagPresent(context.argv, '--debug'),+ dry_run: isFlagPresent(context.argv, '--dry-run'),+ skip_connection_check: isFlagPresent(context.argv, '--skip-connection-check'),+ skip_tool_params_check: isFlagPresent(context.argv, '--skip-tool-params-check'),+ no_verify: isFlagPresent(context.argv, '--no-verify'),+ file_mode: isFlagPresent(context.argv, '--file', '-f'),+ arg_count: Math.max(0, context.argv.length - 3),+});++const getLoginCommandProperties = (context: CliCommandTelemetryContext) => ({+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ cli_version: context.cliVersion,+ command_path: context.commandPath,+ duration_ms: Date.now() - context.startedAt,+ no_browser: isFlagPresent(context.argv, '--no-browser'),+ no_wait: isFlagPresent(context.argv, '--no-wait'),+ has_key: isFlagPresent(context.argv, '--key'),+ skip_org_picker: isFlagPresent(context.argv, '--yes', '-y'),+});++const getLogoutCommandProperties = (context: CliCommandTelemetryContext) => ({+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ cli_version: context.cliVersion,+ command_path: context.commandPath,+ duration_ms: Date.now() - context.startedAt,+});++const getProxyCommandProperties = (context: CliCommandTelemetryContext) => ({+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ cli_version: context.cliVersion,+ command_path: context.commandPath,+ duration_ms: Date.now() - context.startedAt,+ endpoint: getTrailingPositionals(context)[0],+ toolkit: getFlagValue(context.argv, '--toolkit', '-t'),+ method: getFlagValue(context.argv, '--method', '-X') ?? 'GET',+ has_data: isFlagPresent(context.argv, '--data', '-d'),+ header_count: context.argv.slice(2).filter(token => token === '--header' || token === '-H')+ .length,+ skip_connection_check: isFlagPresent(context.argv, '--skip-connection-check'),+});++const isExecuteCommand = (commandPath: string): boolean =>+ commandPath === 'execute' || commandPath === 'manage tools execute';++const isSearchCommand = (commandPath: string): boolean =>+ commandPath === 'search' || commandPath === 'manage tools search';++const isLinkCommand = (commandPath: string): boolean =>+ commandPath === 'link' || commandPath === 'manage connected-accounts link';++const isLoginCommand = (commandPath: string): boolean => commandPath === 'login';++const isLogoutCommand = (commandPath: string): boolean => commandPath === 'logout';++const isProxyCommand = (commandPath: string): boolean => commandPath === 'proxy';++const isRunCommand = (commandPath: string): boolean => commandPath === 'run';++const isGenericOnlyCommand = (commandPath: string): boolean =>+ commandPath === 'composio' || commandPath.startsWith('dev');++export const createCliCommandTelemetryContext = (+ argv: ReadonlyArray<string>,+ cliVersion: string+): CliCommandTelemetryContext => ({+ argv,+ cliVersion,+ commandPath: extractCommandPath(argv),+ flagNames: extractFlagNames(argv),+ startedAt: Date.now(),+ runId:+ extractCommandPath(argv) === 'run'+ ? (process.env.COMPOSIO_CLI_PARENT_RUN_ID ?? crypto.randomUUID())+ : undefined,+});++const getCliCommandInvokedEvent = (context: CliCommandTelemetryContext): TrackEvent => ({+ name: CLI_ANALYTICS_EVENTS.CLI_COMMAND_INVOKED,+ properties: {+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ cli_version: context.cliVersion,+ command_path: context.commandPath,+ flag_names: context.flagNames,+ arg_count: Math.max(0, context.argv.length - 2),+ stdout_is_tty: Boolean(process.stdout.isTTY),+ stderr_is_tty: Boolean(process.stderr.isTTY),+ },+});++const getCliCommandSucceededEvent = (context: CliCommandTelemetryContext): TrackEvent => ({+ name: CLI_ANALYTICS_EVENTS.CLI_COMMAND_SUCCEEDED,+ properties: {+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ cli_version: context.cliVersion,+ command_path: context.commandPath,+ duration_ms: Date.now() - context.startedAt,+ flag_names: context.flagNames,+ },+});++const getCliCommandFailedEvent = (+ context: CliCommandTelemetryContext,+ error: unknown+): TrackEvent => ({+ name: CLI_ANALYTICS_EVENTS.CLI_COMMAND_FAILED,+ properties: {+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ cli_version: context.cliVersion,+ command_path: context.commandPath,+ duration_ms: Date.now() - context.startedAt,+ flag_names: context.flagNames,+ error_name: errorNameOf(error),+ error_message: errorMessageOf(error),+ },+});++type SpecialLifecycleFamily = {+ readonly match: (commandPath: string) => boolean;+ readonly invokedEventName: string;+ readonly succeededEventName: string;+ readonly failedEventName: string;+ readonly getProperties: (context: CliCommandTelemetryContext) => Record<string, unknown>;+};++const SPECIAL_LIFECYCLE_FAMILIES: ReadonlyArray<SpecialLifecycleFamily> = [+ {+ match: isExecuteCommand,+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_INVOKED,+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_SUCCEEDED,+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_FAILED,+ getProperties: getExecuteCommandProperties,+ },+ {+ match: isSearchCommand,+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_SEARCH_INVOKED,+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_SEARCH_SUCCEEDED,+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_SEARCH_FAILED,+ getProperties: getSearchCommandProperties,+ },+ {+ match: isLinkCommand,+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_LINK_INVOKED,+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_LINK_SUCCEEDED,+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_LINK_FAILED,+ getProperties: getLinkCommandProperties,+ },+ {+ match: isLoginCommand,+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGIN_INVOKED,+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_LOGIN_SUCCEEDED,+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGIN_FAILED,+ getProperties: getLoginCommandProperties,+ },+ {+ match: isLogoutCommand,+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGOUT_INVOKED,+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_LOGOUT_SUCCEEDED,+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_LOGOUT_FAILED,+ getProperties: getLogoutCommandProperties,+ },+ {+ match: isProxyCommand,+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_PROXY_INVOKED,+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_PROXY_SUCCEEDED,+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_PROXY_FAILED,+ getProperties: getProxyCommandProperties,+ },+ {+ match: isRunCommand,+ invokedEventName: CLI_ANALYTICS_EVENTS.CLI_RUN_INVOKED,+ succeededEventName: CLI_ANALYTICS_EVENTS.CLI_RUN_SUCCEEDED,+ failedEventName: CLI_ANALYTICS_EVENTS.CLI_RUN_FAILED,+ getProperties: getRunCommandProperties,+ },+];++const getSpecialLifecycleFamily = (commandPath: string): SpecialLifecycleFamily | undefined =>+ SPECIAL_LIFECYCLE_FAMILIES.find(family => family.match(commandPath));++export const getPrimaryLifecycleInvokedEvent = (+ context: CliCommandTelemetryContext+): TrackEvent => {+ const family = getSpecialLifecycleFamily(context.commandPath);+ if (!family || isGenericOnlyCommand(context.commandPath)) {+ return getCliCommandInvokedEvent(context);+ }+ return {+ name: family.invokedEventName,+ properties: family.getProperties(context),+ };+};++export const getPrimaryLifecycleSucceededEvent = (+ context: CliCommandTelemetryContext+): TrackEvent => {+ const family = getSpecialLifecycleFamily(context.commandPath);+ if (!family || isGenericOnlyCommand(context.commandPath)) {+ return getCliCommandSucceededEvent(context);+ }+ return {+ name: family.succeededEventName,+ properties: family.getProperties(context),+ };+};++export const getPrimaryLifecycleFailedEvent = (+ context: CliCommandTelemetryContext,+ error: unknown+): TrackEvent => {+ const family = getSpecialLifecycleFamily(context.commandPath);+ if (!family || isGenericOnlyCommand(context.commandPath)) {+ return getCliCommandFailedEvent(context, error);+ }+ return {+ name: family.failedEventName,+ properties: {+ ...family.getProperties(context),+ error_name: errorNameOf(error),+ error_message: errorMessageOf(error),+ },+ };+};++export const getToolExecuteValidationFailedEvent = (params: {+ readonly toolSlug: string;+ readonly args: Record<string, unknown>;+ readonly error: ToolInputValidationError;+ readonly surface: 'root' | 'manage' | 'dev';+ readonly projectMode: 'consumer' | 'developer';+ readonly stage: 'dry_run' | 'validation' | 'execution';+}): TrackEvent => ({+ name: CLI_ANALYTICS_EVENTS.CLI_TOOL_INVOCATION_VALIDATION_FAILED,+ properties: {+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ tool_slug: params.toolSlug,+ toolkit_slug: toolkitFromToolSlug(params.toolSlug),+ surface: params.surface,+ project_mode: params.projectMode,+ stage: params.stage,+ issue_count: params.error.issues.length,+ issue_locations: extractIssueLocations(params.error.issues),+ unknown_keys: extractUnknownKeys(params.error.issues),+ schema_path: params.error.schemaPath,+ ...argumentShape(params.args),+ },+});++export const isMaybeToolNotFoundError = (params: {+ readonly message?: string;+ readonly errorSlug?: string;+ readonly status?: number;+}): boolean =>+ params.status === 404 ||+ params.errorSlug?.toLowerCase().includes('notfound') === true ||+ TOOL_NOT_FOUND_PATTERN.test(params.message ?? '');++export const getToolExecuteToolNotFoundEvent = (params: {+ readonly toolSlug: string;+ readonly args: Record<string, unknown>;+ readonly surface: 'root' | 'manage' | 'dev';+ readonly projectMode: 'consumer' | 'developer';+ readonly stage: 'schema_fetch' | 'dry_run' | 'execution';+ readonly errorSlug?: string;+ readonly status?: number;+ readonly apiCode?: number;+ readonly message?: string;+}): TrackEvent => ({+ name: CLI_ANALYTICS_EVENTS.CLI_TOOL_INVOCATION_TOOL_NOT_FOUND,+ properties: {+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ tool_slug: params.toolSlug,+ toolkit_slug: toolkitFromToolSlug(params.toolSlug),+ surface: params.surface,+ project_mode: params.projectMode,+ stage: params.stage,+ error_slug: params.errorSlug,+ http_status: params.status,+ api_error_code: params.apiCode,+ error_message: params.message?.slice(0, 500),+ ...argumentShape(params.args),+ },+});++export const getToolExecuteFailedEvent = (params: {+ readonly toolSlug: string;+ readonly args: Record<string, unknown>;+ readonly surface: 'root' | 'manage' | 'dev';+ readonly projectMode: 'consumer' | 'developer';+ readonly stage: 'schema_fetch' | 'dry_run' | 'execution';+ readonly errorSlug?: string;+ readonly status?: number;+ readonly apiCode?: number;+ readonly message?: string;+ readonly errorName?: string;+ readonly isNoConnectionError?: boolean;+}): TrackEvent => ({+ name: CLI_ANALYTICS_EVENTS.CLI_TOOL_INVOCATION_FAILED,+ properties: {+ source: 'cli',+ invocation_origin: getInvocationOrigin(),+ tool_slug: params.toolSlug,+ toolkit_slug: toolkitFromToolSlug(params.toolSlug),+ surface: params.surface,+ project_mode: params.projectMode,+ stage: params.stage,+ error_slug: params.errorSlug,+ http_status: params.status,+ api_error_code: params.apiCode,+ error_name: params.errorName,+ error_message: params.message?.slice(0, 500),+ is_no_connection_error: Boolean(params.isNoConnectionError),+ ...argumentShape(params.args),+ },+});diff --git a/ts/packages/cli/src/analytics/types.ts b/ts/packages/cli/src/analytics/types.tsnew file mode 100644index 000000000..d43f51b0b--- /dev/null+++ b/ts/packages/cli/src/analytics/types.ts@@ -0,0 +1,22 @@+export type TrackEvent = {+ readonly name: string;+ readonly properties?: Record<string, unknown>;+} | null;++export type AnalyticsEnvelope = {+ readonly event: NonNullable<TrackEvent>['name'];+ readonly properties?: NonNullable<TrackEvent>['properties'];+ readonly sentAt: string;+ readonly source: 'cli';+ readonly distinctId: string;+ readonly installId: string;+};++export type CliCommandTelemetryContext = {+ readonly argv: ReadonlyArray<string>;+ readonly cliVersion: string;+ readonly commandPath: string;+ readonly flagNames: ReadonlyArray<string>;+ readonly startedAt: number;+ readonly runId?: string;+};diff --git a/ts/packages/cli/src/bin.ts b/ts/packages/cli/src/bin.tsindex e729dbddc..0517d764b 100644--- a/ts/packages/cli/src/bin.ts+++ b/ts/packages/cli/src/bin.ts@@ -1,243 +1,26 @@import process from 'node:process';-import { Cause, Console, Effect, Exit, HashMap, Layer, Logger, Option } from 'effect';-import { captureErrors, prettyPrintFromCapturedErrors } from 'effect-errors/index';-import { CliConfig, CommandDescriptor, HelpDoc, Usage, ValidationError } from '@effect/cli';-import { FetchHttpClient } from '@effect/platform';-import { BunContext, BunRuntime, BunFileSystem } from '@effect/platform-bun';-import type { Teardown } from '@effect/platform/Runtime';-import { rootCommand, runWithConfig } from 'src/commands';-import * as constants from 'src/constants';-import { ComposioCliConfig } from 'src/cli-config';-import { BaseConfigProviderLive, ConfigLive, extendConfigProvider } from 'src/services/config';-import {- ComposioClientSingleton,- ComposioSessionRepository,- ComposioToolkitsRepository,-} from 'src/services/composio-clients';-import { ComposioToolkitsRepositoryCached } from 'src/services/composio-clients-cached';-import { NodeOs } from 'src/services/node-os';-import { NodeProcess } from 'src/services/node-process';-import { JsPackageManagerDetector } from 'src/services/js-package-manager-detector';-import { ComposioUserContextLive as _ComposioUserContextLive } from 'src/services/user-context';-import { UpgradeBinary } from 'src/services/upgrade-binary';-import { TerminalUILive } from 'src/services/terminal-ui';-import { TriggersRealtime } from 'src/services/triggers-realtime';-import { ToolsExecutorLive as _ToolsExecutorLive } from 'src/services/tools-executor';-import { ProjectContext } from 'src/services/project-context';-import { ProjectEnvironmentDetector } from 'src/services/project-environment-detector';-import { CommandRunner } from 'src/services/command-runner';-import { StdinLive } from 'src/services/stdin';-import { showUpdateNotice, checkForUpdateInBackground } from 'src/services/update-check';-import { mapOnlyComposioOverrideError } from 'src/services/composio-error-overrides';+import { isAnalyticsWorkerInvocation, runAnalyticsWorkerFromArgv } from 'src/analytics/dispatch';-/**- * Concrete Effect layer compositions for the Composio CLI runtime.- *- * ┌─── The service to be created- * │ ┌─── The possible error- * │ │ ┌─── The required dependencies- * ▼ ▼ ▼- * Layer<RequirementsOut, Error, RequirementsIn>- */+const TELEMETRY_DEBUG_FLAG = '--telemetry-debug';+const CLI_TELEMETRY_DEBUG_ENV_VAR = 'COMPOSIO_CLI_TELEMETRY_DEBUG';-// eslint-disable-next-line @typescript-eslint/no-explicit-any-type RequiredLayer = Layer.Layer<any, any, never>;--/**- * Service layer that configures the CLI appearance and default command options.- */-export const CliConfigLive = CliConfig.layer(ComposioCliConfig) satisfies RequiredLayer;--export const ComposioUserContextLive = Layer.provide(- _ComposioUserContextLive,- Layer.mergeAll(BunFileSystem.layer, NodeOs.Default)-) satisfies RequiredLayer;--export const ComposioSessionRepositoryLive = Layer.provide(- ComposioSessionRepository.Default,- Layer.mergeAll(BunFileSystem.layer, NodeOs.Default)-) satisfies RequiredLayer;--export const ComposioToolkitsRepositoryLive = Layer.provide(- ComposioToolkitsRepository.Default,- Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, ConfigLive)-) satisfies RequiredLayer;--export const ComposioToolkitsRepositoryCachedLive = Layer.provide(- ComposioToolkitsRepositoryCached,- ComposioToolkitsRepositoryLive-) satisfies RequiredLayer;--export const UpgradeBinaryLive = Layer.provide(- UpgradeBinary.Default,- Layer.mergeAll(BunFileSystem.layer, FetchHttpClient.layer)-) satisfies RequiredLayer;--export const TriggersRealtimeLive = Layer.provide(- TriggersRealtime.Default,- Layer.mergeAll(BunFileSystem.layer, NodeOs.Default)-) satisfies RequiredLayer;--export const ComposioClientSingletonLive = Layer.provide(- ComposioClientSingleton.Default,- Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, ConfigLive)-) satisfies RequiredLayer;--export const ToolsExecutorLive = Layer.provide(- _ToolsExecutorLive,- ComposioClientSingletonLive-) satisfies RequiredLayer;--export const ProjectContextLive = Layer.provide(- ProjectContext.Default,- Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, NodeProcess.Default)-) satisfies RequiredLayer;--const layers = Layer.mergeAll(- CliConfigLive.pipe(Layer.provide(ConfigLive)),- NodeOs.Default,- NodeProcess.Default,- UpgradeBinaryLive,- ComposioUserContextLive,- ComposioSessionRepositoryLive,- ComposioClientSingletonLive, // Expose ComposioClientSingleton for commands that use Tool Router directly- ComposioToolkitsRepositoryCachedLive, // Use the cached layer instead of the regular one- ToolsExecutorLive,- JsPackageManagerDetector.Default,- ProjectEnvironmentDetector.Default,- CommandRunner.Default,- TriggersRealtimeLive,- ProjectContextLive,- BunContext.layer,- BunFileSystem.layer,- StdinLive,- TerminalUILive,- Logger.pretty-) satisfies RequiredLayer;--export const teardown: Teardown = <E, A>(exit: Exit.Exit<E, A>, onExit: (code: number) => void) => {- const shouldFail = Exit.isFailure(exit) && !Cause.isInterruptedOnly(exit.cause);- const errorCode = Number(process.exitCode ?? 1);- onExit(shouldFail ? errorCode : 0);-};--const runWithArgs = Effect.flatMap(runWithConfig, run => run(process.argv)) satisfies Effect.Effect<- void,- unknown,- unknown->;--const collectValueOptionNamesFromUsage = (usage: Usage.Usage, acc: Set<string>) => {- switch (usage._tag) {- case 'Named': {- if (Option.isSome(usage.acceptedValues)) {- for (const name of usage.names) {- if (name.startsWith('-')) {- acc.add(name);- }- }- }- return;- }- case 'Optional':- case 'Repeated': {- collectValueOptionNamesFromUsage(usage.usage, acc);- return;- }- case 'Alternation':- case 'Concat': {- collectValueOptionNamesFromUsage(usage.left, acc);- collectValueOptionNamesFromUsage(usage.right, acc);- return;- }- case 'Mixed':- case 'Empty': {- return;- }+const stripTelemetryDebugFlag = (argv: ReadonlyArray<string>): string[] => {+ const normalizedArgv = [...argv];+ const flagIndex = normalizedArgv.indexOf(TELEMETRY_DEBUG_FLAG);+ if (flagIndex < 0) {+ return normalizedArgv;}-};--const valueOptionNames = (() => {- const names = new Set<string>();- const visited = new Set<CommandDescriptor.Command<unknown>>();- const visit = (command: CommandDescriptor.Command<unknown>) => {- if (visited.has(command)) {- return;- }- visited.add(command);- collectValueOptionNamesFromUsage(CommandDescriptor.getUsage(command), names);- for (const [, subcommand] of HashMap.toEntries(CommandDescriptor.getSubcommands(command))) {- visit(subcommand);- }- };- visit(rootCommand.descriptor);- return names;-})();--/**- * Upgrade hint — runs outside the Effect runtime, never blocks or throws.- * showUpdateNotice() reads a cached file (~1 ms) and prints to stderr.- * checkForUpdateInBackground() fires a non-blocking fetch to GitHub.- */-showUpdateNotice();-checkForUpdateInBackground();-/**- * CLI entrypoint, which:- * - runs the Effect runtime and sets up its runtime environment- * - collects and displays errors- */-runWithArgs.pipe(- Effect.scoped,- Effect.mapError(error =>- ValidationError.isValidationError(error) ? error : mapOnlyComposioOverrideError({ error })- ),- // @effect/cli already prints validation errors (missing args, invalid flags, etc.)- // via its own printDocs before re-failing. Swallow the re-thrown error to avoid- // routing it through the generic error box which would dump raw JSON.- // When a flag is passed without its required value (e.g. `--query` instead of- // `--query "text"`), @effect/cli reports it as "unknown argument" — add a tip.- Effect.catchIf(ValidationError.isValidationError, error => {- const text = HelpDoc.toAnsiText(error.error).trim();- const flagMatch = text.match(/Received unknown argument: '(-{1,2}[\w-]+)'/);- if (flagMatch && valueOptionNames.has(flagMatch[1])) {- return Console.error(`Tip: ${flagMatch[1]} requires a value, e.g. ${flagMatch[1]} "value"`);- }- return Effect.void;- }),- Effect.withSpan('composio-cli', {- attributes: {- name: constants.APP_NAME,- filename: 'src/bin.ts',- },- }),- Effect.sandbox,- Effect.catchAll(- Effect.fn(function* (cause) {- const captured = yield* captureErrors(cause, {- stripCwd: true,- });- const filteredErrors = captured.errors.filter(- error => error.errorType !== 'ToolExecutionError'- );-- if (captured.interrupted || filteredErrors.length > 0) {- const message = prettyPrintFromCapturedErrors(- { ...captured, errors: filteredErrors },- {- hideStackTrace: true,- stripCwd: true,- enabled: true,- }- );+ normalizedArgv.splice(flagIndex, 1);+ process.env[CLI_TELEMETRY_DEBUG_ENV_VAR] = 'true';+ return normalizedArgv;+};- yield* Console.error(message);- process.exitCode = 1;- }- })- ),- Effect.provide(layers),- Effect.withConfigProvider(extendConfigProvider(BaseConfigProviderLive)),- effect =>- (BunRuntime.runMain({ teardown }) as (e: Effect.Effect<void, unknown, unknown>) => void)(effect)-);+if (isAnalyticsWorkerInvocation(process.argv)) {+ void runAnalyticsWorkerFromArgv(process.argv).finally(() => {+ process.exit(0);+ });+} else {+ process.argv = stripTelemetryDebugFlag(process.argv);+ void import('./cli-main');+}diff --git a/ts/packages/cli/src/cli-main.ts b/ts/packages/cli/src/cli-main.tsnew file mode 100644index 000000000..2d1bbf431--- /dev/null+++ b/ts/packages/cli/src/cli-main.ts@@ -0,0 +1,237 @@+import process from 'node:process';+import { Cause, Console, Effect, Exit, HashMap, Layer, Logger, Option } from 'effect';+import { captureErrors, prettyPrintFromCapturedErrors } from 'effect-errors/index';+import { CliConfig, CommandDescriptor, HelpDoc, Usage, ValidationError } from '@effect/cli';+import { FetchHttpClient } from '@effect/platform';+import { BunContext, BunRuntime, BunFileSystem } from '@effect/platform-bun';+import type { Teardown } from '@effect/platform/Runtime';+import { rootCommand, runWithConfig } from 'src/commands';+import * as constants from 'src/constants';+import { ComposioCliConfig } from 'src/cli-config';+import { BaseConfigProviderLive, ConfigLive, extendConfigProvider } from 'src/services/config';+import {+ ComposioClientSingleton,+ ComposioSessionRepository,+ ComposioToolkitsRepository,+} from 'src/services/composio-clients';+import { ComposioToolkitsRepositoryCached } from 'src/services/composio-clients-cached';+import { NodeOs } from 'src/services/node-os';+import { NodeProcess } from 'src/services/node-process';+import { JsPackageManagerDetector } from 'src/services/js-package-manager-detector';+import { ComposioUserContextLive as _ComposioUserContextLive } from 'src/services/user-context';+import { UpgradeBinary } from 'src/services/upgrade-binary';+import { TerminalUILive } from 'src/services/terminal-ui';+import { TriggersRealtime } from 'src/services/triggers-realtime';+import { ToolsExecutorLive as _ToolsExecutorLive } from 'src/services/tools-executor';+import { ProjectContext } from 'src/services/project-context';+import { ProjectEnvironmentDetector } from 'src/services/project-environment-detector';+import { CommandRunner } from 'src/services/command-runner';+import { StdinLive } from 'src/services/stdin';+import { showUpdateNotice, checkForUpdateInBackground } from 'src/services/update-check';+import {+ createCliCommandTelemetryContext,+ getPrimaryLifecycleFailedEvent,+ getPrimaryLifecycleInvokedEvent,+ getPrimaryLifecycleSucceededEvent,+} from 'src/analytics/events';+import { trackCliEvent, trackCliEventEffect } from 'src/analytics/dispatch';+import { mapOnlyComposioOverrideError } from 'src/services/composio-error-overrides';++// eslint-disable-next-line @typescript-eslint/no-explicit-any+type RequiredLayer = Layer.Layer<any, any, never>;++export const CliConfigLive = CliConfig.layer(ComposioCliConfig) satisfies RequiredLayer;++export const ComposioUserContextLive = Layer.provide(+ _ComposioUserContextLive,+ Layer.mergeAll(BunFileSystem.layer, NodeOs.Default)+) satisfies RequiredLayer;++export const ComposioSessionRepositoryLive = Layer.provide(+ ComposioSessionRepository.Default,+ Layer.mergeAll(BunFileSystem.layer, NodeOs.Default)+) satisfies RequiredLayer;++export const ComposioToolkitsRepositoryLive = Layer.provide(+ ComposioToolkitsRepository.Default,+ Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, ConfigLive)+) satisfies RequiredLayer;++export const ComposioToolkitsRepositoryCachedLive = Layer.provide(+ ComposioToolkitsRepositoryCached,+ ComposioToolkitsRepositoryLive+) satisfies RequiredLayer;++export const UpgradeBinaryLive = Layer.provide(+ UpgradeBinary.Default,+ Layer.mergeAll(BunFileSystem.layer, FetchHttpClient.layer)+) satisfies RequiredLayer;++export const TriggersRealtimeLive = Layer.provide(+ TriggersRealtime.Default,+ Layer.mergeAll(BunFileSystem.layer, NodeOs.Default)+) satisfies RequiredLayer;++export const ComposioClientSingletonLive = Layer.provide(+ ComposioClientSingleton.Default,+ Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, ConfigLive)+) satisfies RequiredLayer;++export const ToolsExecutorLive = Layer.provide(+ _ToolsExecutorLive,+ ComposioClientSingletonLive+) satisfies RequiredLayer;++export const ProjectContextLive = Layer.provide(+ ProjectContext.Default,+ Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, NodeProcess.Default)+) satisfies RequiredLayer;++const layers = Layer.mergeAll(+ CliConfigLive.pipe(Layer.provide(ConfigLive)),+ NodeOs.Default,+ NodeProcess.Default,+ UpgradeBinaryLive,+ ComposioUserContextLive,+ ComposioSessionRepositoryLive,+ ComposioClientSingletonLive,+ ComposioToolkitsRepositoryCachedLive,+ ToolsExecutorLive,+ JsPackageManagerDetector.Default,+ ProjectEnvironmentDetector.Default,+ CommandRunner.Default,+ TriggersRealtimeLive,+ ProjectContextLive,+ BunContext.layer,+ BunFileSystem.layer,+ StdinLive,+ TerminalUILive,+ Logger.pretty+) satisfies RequiredLayer;++export const teardown: Teardown = <E, A>(exit: Exit.Exit<E, A>, onExit: (code: number) => void) => {+ const shouldFail = Exit.isFailure(exit) && !Cause.isInterruptedOnly(exit.cause);+ const errorCode = Number(process.exitCode ?? 1);+ onExit(shouldFail ? errorCode : 0);+};++const runWithArgs = Effect.flatMap(runWithConfig, run => run(process.argv)) satisfies Effect.Effect<+ void,+ unknown,+ unknown+>;++const commandTelemetryContext = createCliCommandTelemetryContext(+ process.argv,+ constants.APP_VERSION+);+if (commandTelemetryContext.commandPath === 'run' && commandTelemetryContext.runId) {+ process.env.COMPOSIO_CLI_PARENT_RUN_ID = commandTelemetryContext.runId;+}++const collectValueOptionNamesFromUsage = (usage: Usage.Usage, acc: Set<string>) => {+ switch (usage._tag) {+ case 'Named': {+ if (Option.isSome(usage.acceptedValues)) {+ for (const name of usage.names) {+ if (name.startsWith('-')) {+ acc.add(name);+ }+ }+ }+ return;+ }+ case 'Optional':+ case 'Repeated': {+ collectValueOptionNamesFromUsage(usage.usage, acc);+ return;+ }+ case 'Alternation':+ case 'Concat': {+ collectValueOptionNamesFromUsage(usage.left, acc);+ collectValueOptionNamesFromUsage(usage.right, acc);+ return;+ }+ case 'Mixed':+ case 'Empty': {+ return;+ }+ }+};++const valueOptionNames = (() => {+ const names = new Set<string>();+ const visited = new Set<CommandDescriptor.Command<unknown>>();+ const visit = (command: CommandDescriptor.Command<unknown>) => {+ if (visited.has(command)) {+ return;+ }+ visited.add(command);+ collectValueOptionNamesFromUsage(CommandDescriptor.getUsage(command), names);+ for (const [, subcommand] of HashMap.toEntries(CommandDescriptor.getSubcommands(command))) {+ visit(subcommand);+ }+ };+ visit(rootCommand.descriptor);+ return names;+})();++showUpdateNotice();+checkForUpdateInBackground();+trackCliEvent(getPrimaryLifecycleInvokedEvent(commandTelemetryContext));++runWithArgs.pipe(+ Effect.scoped,+ Effect.mapError(error =>+ ValidationError.isValidationError(error) ? error : mapOnlyComposioOverrideError({ error })+ ),+ Effect.tap(() => trackCliEventEffect(getPrimaryLifecycleSucceededEvent(commandTelemetryContext))),+ Effect.tapErrorCause(cause =>+ trackCliEventEffect(+ getPrimaryLifecycleFailedEvent(commandTelemetryContext, Cause.squash(cause))+ )+ ),+ Effect.catchIf(ValidationError.isValidationError, error => {+ const text = HelpDoc.toAnsiText(error.error).trim();+ const flagMatch = text.match(/Received unknown argument: '(-{1,2}[\w-]+)'/);+ if (flagMatch && valueOptionNames.has(flagMatch[1])) {+ return Console.error(`Tip: ${flagMatch[1]} requires a value, e.g. ${flagMatch[1]} "value"`);+ }+ return Effect.void;+ }),+ Effect.withSpan('composio-cli', {+ attributes: {+ name: constants.APP_NAME,+ filename: 'src/bin.ts',+ },+ }),+ Effect.sandbox,+ Effect.catchAll(+ Effect.fn(function* (cause) {+ const captured = yield* captureErrors(cause, {+ stripCwd: true,+ });+ const filteredErrors = captured.errors.filter(+ error => error.errorType !== 'ToolExecutionError'+ );+ if (captured.interrupted || filteredErrors.length > 0) {+ const message = prettyPrintFromCapturedErrors(+ { ...captured, errors: filteredErrors },+ {+ hideStackTrace: true,+ stripCwd: true,+ enabled: true,+ }+ ).trim();+ if (message.length > 0) {+ yield* Console.error(message);+ process.exitCode = 1;+ }+ }+ })+ ),+ Effect.provide(layers),+ Effect.withConfigProvider(extendConfigProvider(BaseConfigProviderLive)),+ effect =>+ (BunRuntime.runMain({ teardown }) as (e: Effect.Effect<void, unknown, unknown>) => void)(effect)+);diff --git a/ts/packages/cli/src/commands/run.cmd.ts b/ts/packages/cli/src/commands/run.cmd.tsindex d54981038..9338c3297 100644--- a/ts/packages/cli/src/commands/run.cmd.ts+++ b/ts/packages/cli/src/commands/run.cmd.ts@@ -160,6 +160,7 @@ type RunHelperContext = {readonly baseURL?: string;readonly webURL?: string;readonly orgId?: string;+ readonly runId?: string;readonly consumerUserId?: string;readonly consumerProjectId?: string;readonly consumerProjectName?: string;@@ -484,6 +485,8 @@ const buildRunBaseHelpersSource = (): ReadonlyArray<string> => [' ...(helperContext.apiKey ? { COMPOSIO_USER_API_KEY: helperContext.apiKey } : {}),',' ...(helperContext.baseURL ? { COMPOSIO_BASE_URL: helperContext.baseURL } : {}),',' ...(helperContext.webURL ? { COMPOSIO_WEB_URL: helperContext.webURL } : {}),',+ ' COMPOSIO_CLI_INVOCATION_ORIGIN: "run",',+ ' ...(helperContext.runId ? { COMPOSIO_CLI_PARENT_RUN_ID: helperContext.runId } : {}),',' ...(sharedRunOutputDir ? { COMPOSIO_RUN_OUTPUT_DIR: sharedRunOutputDir } : {}),',' ...(perfDebugEnabled ? { COMPOSIO_PERF_DEBUG: "1" } : {}),',' ...(toolDebugEnabled ? { COMPOSIO_TOOL_DEBUG: "1" } : {}),',@@ -891,6 +894,7 @@ export const runCmd = Command.make('run', {Command.withHandler(({ file, dryRun, debug, skipConnectionCheck, skipToolParamsCheck, noVerify, args }) =>Effect.gen(function* () {+ const runId = process.env.COMPOSIO_CLI_PARENT_RUN_ID ?? crypto.randomUUID();const perfDebug = isPerfDebugEnabled();const toolDebug = isToolDebugEnabled();if (Option.isNone(file)) {@@ -906,6 +910,7 @@ export const runCmd = Command.make('run', {const helperContext: RunHelperContext = {...(yield* resolveRunHelperContext()),+ runId,master: detectMaster(),perfDebug,toolDebug,@@ -941,6 +946,7 @@ export const runCmd = Command.make('run', {env: {...process.env,BUN_BE_BUN: '1',+ COMPOSIO_CLI_PARENT_RUN_ID: runId,...(perfDebug ? { COMPOSIO_PERF_DEBUG: '1' } : {}),...(toolDebug ? { COMPOSIO_TOOL_DEBUG: '1' } : {}),},diff --git a/ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts b/ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.tsindex 772d5eb03..1a1d87414 100644--- a/ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts+++ b/ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts@@ -4,6 +4,7 @@ import { Effect, Option, Either, Exit, Fiber, Cause } from 'effect';import { encodingForModel } from 'js-tiktoken';import { redact } from 'src/ui/redact';import { parseJsonIsh } from 'src/utils/parse-json-ish';+import { toolkitFromToolSlug } from 'src/utils/toolkit-from-tool-slug';import { requireAuth } from 'src/effects/require-auth';import { resolveOptionalTextInput } from 'src/effects/resolve-optional-text-input';import {@@ -21,6 +22,13 @@ import type { ToolExecuteParams, ToolExecuteResponse } from 'src/services/tools-import { ComposioToolkitsRepository } from 'src/services/composio-clients';import { ComposioUserContext } from 'src/services/user-context';import { ProjectContext } from 'src/services/project-context';+import { trackCliEventEffect } from 'src/analytics/dispatch';+import {+ getToolExecuteFailedEvent,+ getToolExecuteToolNotFoundEvent,+ getToolExecuteValidationFailedEvent,+ isMaybeToolNotFoundError,+} from 'src/analytics/events';import { handleHttpServerError } from 'src/effects/handle-http-error';import { formatToolInputParameters } from '../format';import { ComposioClientSingleton } from 'src/services/composio-clients';@@ -42,6 +50,7 @@ import { storeCliSessionArtifact } from 'src/services/cli-session-artifacts';import {ComposioNoActiveConnectionError,mapComposioError,+ normalizeCliError,} from 'src/services/composio-error-overrides';const slug = Args.text({ name: 'slug' }).pipe(@@ -108,14 +117,6 @@ const parseArguments = (raw: string) =>return parsed as Record<string, unknown>;});-const toolkitFromToolSlug = (toolSlug: string): string | undefined => {- const idx = toolSlug.indexOf('_');- if (idx <= 0) return toolSlug.toLowerCase();- const prefix = toolSlug.slice(0, idx).toLowerCase();- if (prefix === 'composio') return undefined;- return prefix;-};-const connectionTips = (toolSlug: string, surface: 'root' | 'manage' | 'dev') => {const toolkit = toolkitFromToolSlug(toolSlug);const executeStep =@@ -253,6 +254,79 @@ const prepareExecuteOutput = (};});+const emitExecuteFailureTelemetry = (params: {+ readonly toolSlug: string;+ readonly args: Record<string, unknown>;+ readonly error: unknown;+ readonly surface: 'root' | 'manage' | 'dev';+ readonly projectMode: 'consumer' | 'developer';+ readonly stage: 'schema_fetch' | 'dry_run' | 'validation' | 'execution';+ readonly mappedError?: ReturnType<typeof mapComposioError>;+}) =>+ Effect.gen(function* () {+ const normalized = params.mappedError?.normalized ?? normalizeCliError(params.error);++ if (normalized instanceof ToolInputValidationError) {+ yield* trackCliEventEffect(+ getToolExecuteValidationFailedEvent({+ toolSlug: params.toolSlug,+ args: params.args,+ error: normalized,+ surface: params.surface,+ projectMode: params.projectMode,+ stage: params.stage === 'dry_run' ? 'dry_run' : 'validation',+ })+ );+ return;+ }++ const mapped =+ params.mappedError ??+ mapComposioError({+ error: params.error,+ toolSlug: params.toolSlug,+ });+ const apiDetails = mapped.apiDetails;+ const message = mapped.message;+ const errorSlug = mapped.slugValue;+ const status = apiDetails?.status;+ const apiCode = apiDetails?.code;+ const isNoConnectionError =+ normalized instanceof ComposioNoActiveConnectionError || Boolean(mapped.override);++ const event = isMaybeToolNotFoundError({+ message,+ errorSlug,+ status,+ })+ ? getToolExecuteToolNotFoundEvent({+ toolSlug: params.toolSlug,+ args: params.args,+ surface: params.surface,+ projectMode: params.projectMode,+ stage: params.stage === 'validation' ? 'execution' : params.stage,+ errorSlug,+ status,+ apiCode,+ message,+ })+ : getToolExecuteFailedEvent({+ toolSlug: params.toolSlug,+ args: params.args,+ surface: params.surface,+ projectMode: params.projectMode,+ stage: params.stage === 'validation' ? 'execution' : params.stage,+ errorSlug,+ status,+ apiCode,+ message,+ errorName: normalized instanceof Error ? normalized.name : undefined,+ isNoConnectionError,+ });++ yield* trackCliEventEffect(event);+ });+export const showToolsExecuteInputHelp = (toolSlug: string) =>Effect.gen(function* () {if (!(yield* requireAuth)) return;@@ -299,12 +373,26 @@ export const showToolsExecuteInputHelp = (toolSlug: string) =>const handleExecutionError = (ui: TerminalUI,error: unknown,- context: { toolSlug: string; surface: 'root' | 'manage' | 'dev' }+ context: {+ toolSlug: string;+ args: Record<string, unknown>;+ surface: 'root' | 'manage' | 'dev';+ projectMode: 'consumer' | 'developer';+ stage: 'schema_fetch' | 'dry_run' | 'validation' | 'execution';+ }) =>Effect.gen(function* () {const mapped = mapComposioError({ error, toolSlug: context.toolSlug });const normalized = mapped.normalized;if (normalized instanceof ToolInputValidationError) {+ yield* emitExecuteFailureTelemetry({+ toolSlug: context.toolSlug,+ args: context.args,+ error: normalized,+ surface: context.surface,+ projectMode: context.projectMode,+ stage: context.stage,+ });yield* ui.log.error(`Input validation failed for ${context.toolSlug}`);yield* ui.note([`Schema: ${normalized.schemaPath}`, ...normalized.issues.map(issue => `- ${issue}`)].join(@@ -318,6 +406,16 @@ const handleExecutionError = (const apiDetails = mapped.apiDetails;const slugValue = mapped.slugValue;+ yield* emitExecuteFailureTelemetry({+ toolSlug: context.toolSlug,+ args: context.args,+ error,+ surface: context.surface,+ projectMode: context.projectMode,+ stage: context.stage,+ mappedError: mapped,+ });+if (normalized instanceof ComposioNoActiveConnectionError) {yield* ui.log.error(mapped.message);if (toolkitFromToolSlug(context.toolSlug)) {@@ -709,9 +807,11 @@ const runConnectedToolkitFailFast = (params: {}});+// eslint-disable-next-line max-lines-per-functionconst runExecuteWithSpinner = (params: {readonly slug: string;readonly surface: 'root' | 'manage' | 'dev';+ readonly projectMode: 'consumer' | 'developer';readonly dryRun: boolean;readonly ui: TerminalUI;readonly executor: ToolsExecutor;@@ -759,9 +859,35 @@ const runExecuteWithSpinner = (params: {(yield* getOrFetchToolInputDefinition(params.slug, {orgId: params.resolvedProject.orgId,projectId: params.resolvedProject.projectId,- })));+ }).pipe(+ Effect.tapError(error =>+ emitExecuteFailureTelemetry({+ toolSlug: params.slug,+ args: params.args,+ error,+ surface: params.surface,+ projectMode: params.projectMode,+ stage: 'dry_run',+ })+ )+ )));if (definition) {- yield* validateToolInputArgumentsWithDefinition(params.slug, params.args, definition);+ yield* validateToolInputArgumentsWithDefinition(+ params.slug,+ params.args,+ definition+ ).pipe(+ Effect.tapError(error =>+ emitExecuteFailureTelemetry({+ toolSlug: params.slug,+ args: params.args,+ error,+ surface: params.surface,+ projectMode: params.projectMode,+ stage: 'dry_run',+ })+ )+ );}const summary: DryRunSummary = {successful: true,@@ -819,7 +945,10 @@ const runExecuteWithSpinner = (params: {yield* spinner.error();const summary = yield* handleExecutionError(params.ui, resultEither.left, {toolSlug: params.slug,+ args: params.args,surface: params.surface,+ projectMode: params.projectMode,+ stage: 'execution',});yield* params.ui.output(JSON.stringify({ successful: false, ...summary }, ciRedactReplacer, 2)@@ -865,7 +994,10 @@ const runExecuteWithSpinner = (params: {const summary = yield* handleExecutionError(params.ui, result.error ?? result, {toolSlug: params.slug,+ args: params.args,surface: params.surface,+ projectMode: params.projectMode,+ stage: 'execution',});yield* params.ui.output(JSON.stringify(result, ciRedactReplacer, 2));return yield* Effect.fail(new ToolExecutionError(summary.error));@@ -937,7 +1069,18 @@ const runToolsExecute = (params: RunToolsExecuteParams) =>const definition = yield* getOrFetchToolInputDefinition(params.slug, {orgId: context.resolvedProject.orgId,projectId: context.resolvedProject.projectId,- });+ }).pipe(+ Effect.tapError(error =>+ emitExecuteFailureTelemetry({+ toolSlug: params.slug,+ args: context.args,+ error,+ surface: params.surface,+ projectMode: params.projectMode,+ stage: 'schema_fetch',+ })+ )+ );yield* emitCachedSchema(context.ui, params.slug, definition);return;}@@ -966,6 +1109,7 @@ const runToolsExecute = (params: RunToolsExecuteParams) =>yield* runExecuteWithSpinner({slug: params.slug,surface: params.surface,+ projectMode: params.projectMode,dryRun: params.dryRun,ui: context.ui,executor: context.executor,diff --git a/ts/packages/cli/src/utils/toolkit-from-tool-slug.ts b/ts/packages/cli/src/utils/toolkit-from-tool-slug.tsnew file mode 100644index 000000000..cefad19a2--- /dev/null+++ b/ts/packages/cli/src/utils/toolkit-from-tool-slug.ts@@ -0,0 +1,6 @@+export const toolkitFromToolSlug = (toolSlug: string): string | undefined => {+ const idx = toolSlug.indexOf('_');+ if (idx <= 0) return toolSlug.toLowerCase();+ const prefix = toolSlug.slice(0, idx).toLowerCase();+ return prefix === 'composio' ? undefined : prefix;+};