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 110 lines 4.03 KB
Raw
1 import { Console, Context, Effect } from 'effect';
2 import { CliConfig, CliOutput, Command, GlobalFlag } from 'effect/cli';
3 import type { CommandInputError, NotLoggedInError } from 'src/commands/errors';
4 import type { CommandSurface } from 'src/commands/surface';
5 import { beginAtCommandPath } from 'src/invocation/command-identity';
6 import type { ComposioUserContext } from 'src/services/user-context';
7 import { withRegisteredSubcommands } from 'src/commands/registration';
8
9 export const RootHelpMarker = Context.Reference<boolean>('commands/RootHelpMarker', {
10 defaultValue: () => false,
11 });
12
13 export class RootHelpContext extends Context.Service<
14 RootHelpContext,
15 {
16 readonly command: Command.Command.Any;
17 readonly version: string;
18 }
19 >()('commands/RootHelpContext') {}
20
21 /** Keep a short root overview; the framework generates every usage, flag, and subcommand. */
22 export const helpFormatter = () => {
23 const formatter = CliOutput.defaultFormatter();
24 return {
25 ...formatter,
26 formatVersion: (_name: string, version: string) => version,
27 formatHelpDoc: (doc: Parameters<typeof formatter.formatHelpDoc>[0]) => {
28 const text = formatter.formatHelpDoc(doc);
29 return Context.get(doc.annotations, RootHelpMarker)
30 ? `${text}\n\nUse \`composio <command> --help\` for command details.\nDocumentation: https://docs.composio.dev`
31 : text;
32 },
33 } satisfies CliOutput.Formatter;
34 };
35
36 export const showCommandHelp = (parts: ReadonlyArray<string> = []) =>
37 Effect.gen(function* () {
38 const { command, version } = yield* RootHelpContext;
39 const { builtIns } = yield* CliConfig.CliConfig;
40 const console = yield* Console.Console;
41 yield* GlobalFlag.Help.run(true, {
42 command,
43 commandPath: [command.name, ...parts],
44 version,
45 builtIns,
46 }).pipe(
47 Effect.provideService(Console.Console, {
48 ...console,
49 log: (...args) => console.info(...args),
50 })
51 );
52 });
53
54 /** Derive help aliases from the real command tree, letting Effect validate names and suggest fixes. */
55 export const buildHelpCommand = (commands: ReadonlyArray<Command.Command.Any>) => {
56 const makeHelpCommand = (
57 name: string,
58 path: ReadonlyArray<string>,
59 subcommands: ReadonlyArray<Command.Command.Any>,
60 description: string | undefined
61 ): Command.Command<
62 string,
63 Record<never, never>,
64 Record<never, never>,
65 CommandInputError | NotLoggedInError,
66 RootHelpContext | ComposioUserContext | CommandSurface
67 > =>
68 Command.make(name, {}, () => showCommandHelp(path)).pipe(
69 Command.withDescription(description ?? 'Show command help.'),
70 withRegisteredSubcommands(
71 subcommands.map(child => {
72 const help = makeHelpCommand(
73 child.name,
74 [...path, child.name],
75 child.subcommands.flatMap(group => group.commands),
76 child.description
77 );
78 const aliased = child.alias ? help.pipe(Command.withAlias(child.alias)) : help;
79 return child.unlisted ? aliased.pipe(Command.unlisted) : aliased;
80 })
81 )
82 );
83 return makeHelpCommand('help', [], commands, 'Show help for a command.');
84 };
85
86 /**
87 * `--help`: begins the command whose help it shows (no handler runs, so nothing else records
88 * it). A help flag on the help alias describes the requested command.
89 */
90 export const Help = GlobalFlag.Action({
91 flag: GlobalFlag.Help.flag,
92 run: (value, context) => {
93 const commandPath =
94 context.commandPath[1] === 'help'
95 ? [context.commandPath[0], ...context.commandPath.slice(2)]
96 : context.commandPath;
97 return beginAtCommandPath(context.command, commandPath).pipe(
98 Effect.andThen(GlobalFlag.Help.run(value, { ...context, commandPath }))
99 );
100 },
101 });
102
103 /** `--version`: begins the command it was given on (no handler runs), then prints the version. */
104 export const Version = GlobalFlag.Action({
105 flag: GlobalFlag.Version.flag,
106 run: (value, context) =>
107 beginAtCommandPath(context.command, context.commandPath).pipe(
108 Effect.andThen(GlobalFlag.Version.run(value, context))
109 ),
110 });