Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import * as FileSystem from 'effect/FileSystem'; |
| 2 | import * as Path from 'effect/Path'; |
| 3 | import { Cause, Context, Effect, Layer } from 'effect'; |
| 4 | import { trackCliEventEffect } from 'src/analytics/dispatch'; |
| 5 | import { captureServices } from 'src/effects/capture-services'; |
| 6 | import { |
| 7 | getPluginHintShownEvent, |
| 8 | getPrimaryLifecycleFailedEvent, |
| 9 | getPrimaryLifecycleInvokedEvent, |
| 10 | getPrimaryLifecycleSucceededEvent, |
| 11 | } from 'src/analytics/events'; |
| 12 | import type { CliCommandTelemetryContext, TrackEvent } from 'src/analytics/types'; |
| 13 | import { APP_VERSION } from 'src/constants'; |
| 14 | import type { AgentHost } from './agent-host'; |
| 15 | import { NodeOs } from './node-os'; |
| 16 | import { TerminalUI } from './terminal-ui'; |
| 17 | |
| 18 | /** The plugin hint (`src/services/plugin-hint.ts`) was printed for a command. */ |
| 19 | export interface PluginHintShown { |
| 20 | readonly invocationOrigin: string; |
| 21 | /** The command the hint was printed for, as `commandPathOf` gives it. */ |
| 22 | readonly commandPath: string; |
| 23 | readonly agentHost: AgentHost; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Where an invocation reports its command's lifecycle, once when the command starts and once |
| 28 | * when it succeeds or fails, and what its invocation hooks showed. Every method is best-effort |
| 29 | * and never fails. |
| 30 | */ |
| 31 | export interface CommandLifecycleTelemetryShape { |
| 32 | readonly invoked: (context: CliCommandTelemetryContext) => Effect.Effect<void>; |
| 33 | readonly succeeded: (context: CliCommandTelemetryContext) => Effect.Effect<void>; |
| 34 | readonly failed: ( |
| 35 | context: CliCommandTelemetryContext, |
| 36 | cause: Cause.Cause<unknown> |
| 37 | ) => Effect.Effect<void>; |
| 38 | /** Tracks `CLI_PLUGIN_HINT_SHOWN`. */ |
| 39 | readonly pluginHintShown: (shown: PluginHintShown) => Effect.Effect<void>; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Tracks the command's primary lifecycle events (its family's events, or the generic |
| 44 | * `CLI_COMMAND_*` ones) and the plugin hint's event through the analytics dispatcher. |
| 45 | */ |
| 46 | export class CommandLifecycleTelemetry extends Context.Service< |
| 47 | CommandLifecycleTelemetry, |
| 48 | CommandLifecycleTelemetryShape |
| 49 | >()('services/CommandLifecycleTelemetry') { |
| 50 | static readonly Default: Layer.Layer< |
| 51 | CommandLifecycleTelemetry, |
| 52 | never, |
| 53 | FileSystem.FileSystem | Path.Path | NodeOs | TerminalUI |
| 54 | > = Layer.effect( |
| 55 | CommandLifecycleTelemetry, |
| 56 | Effect.gen(function* () { |
| 57 | const services = yield* captureServices(FileSystem.FileSystem, Path.Path, NodeOs, TerminalUI); |
| 58 | const track = (event: TrackEvent) => |
| 59 | trackCliEventEffect(event).pipe(Effect.provideContext(services)); |
| 60 | return CommandLifecycleTelemetry.of({ |
| 61 | invoked: context => track(getPrimaryLifecycleInvokedEvent(context)), |
| 62 | succeeded: context => track(getPrimaryLifecycleSucceededEvent(context)), |
| 63 | failed: (context, cause) => |
| 64 | track(getPrimaryLifecycleFailedEvent(context, Cause.squash(cause))), |
| 65 | pluginHintShown: shown => |
| 66 | track(getPluginHintShownEvent({ ...shown, cliVersion: APP_VERSION })), |
| 67 | }); |
| 68 | }) |
| 69 | ); |
| 70 | } |