Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 43 lines 1.86 KB
Raw
1 import { Option } from 'effect';
2 import type { Command } from 'effect/cli';
3 import type { CommandTelemetryInput } from './types';
4 import { withCommandMeta } from 'src/invocation/command-identity';
5
6 type Value = string | number | boolean | undefined;
7 type Field = { readonly value: Value; readonly flags?: ReadonlyArray<string> };
8 const value = (value: Value): Field => ({ value });
9 const flag = (name: string, enabled: boolean): Field => ({
10 value: enabled,
11 flags: enabled ? [name] : [],
12 });
13 const present = (input: Option.Option<string> | undefined) =>
14 input !== undefined && Option.isSome(input);
15 const optional = (input: Option.Option<string> | undefined) =>
16 input === undefined ? undefined : Option.getOrUndefined(input);
17
18 /** One allowlist supplies both safe properties and canonical enabled flag names. */
19 const fields = (
20 input: Readonly<Record<string, Field>>,
21 argument?: string
22 ): CommandTelemetryInput => ({
23 argument,
24 options: Object.fromEntries(
25 Object.entries(input)
26 .filter(([, field]) => field.value !== undefined)
27 .map(([key, field]) => [key, field.value])
28 ),
29 flagNames: [...new Set(Object.values(input).flatMap(field => field.flags ?? []))].sort(),
30 });
31
32 /** Select only measured fields. Neither values nor flag names are taken from raw argv. */
33 export const telemetry = { fields, value, flag, present, optional };
34
35 /** Bind a privacy projection to the same command whose parser owns its input type. */
36 export const withCommandTelemetry = <Name extends string, Input, ContextInput, E, R>(
37 command: Command.Command<Name, Input, ContextInput, E, R>,
38 project: (input: NoInfer<Input>) => CommandTelemetryInput
39 ): Command.Command<Name, Input, ContextInput, E, R> =>
40 withCommandMeta({
41 // Command annotations erase Input. Only this command's parsed input reaches this callback.
42 telemetryInput: input => project(input as Input),
43 })(command);