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 57 lines 2.01 KB
Raw
1 import { Effect } from 'effect';
2 import { Command, Completions } from 'effect/cli';
3
4 type Shell = 'bash' | 'zsh' | 'fish';
5
6 const sanitizeFishCompletionLine = (line: string): string => {
7 if (!line.startsWith('complete ')) {
8 return line;
9 }
10
11 const descriptionIndex = line.indexOf(' -d ');
12 if (descriptionIndex === -1) {
13 return line;
14 }
15
16 // Fish descriptions are optional. Dropping them avoids parse errors from
17 // multiline or quote-heavy command descriptions emitted by the upstream generator.
18 return line.slice(0, descriptionIndex).trimEnd();
19 };
20
21 const sanitizeFishCompletionLines = (lines: Array<string>): Array<string> =>
22 lines.map(sanitizeFishCompletionLine);
23
24 /**
25 * Converts a public `Command.Command` tree into the `Completions.CommandDescriptor`
26 * shape consumed by `effect/cli`'s completion-script generator.
27 *
28 * v4 has no like-for-like replacement for v3's rich `CommandDescriptor` walk:
29 * `Command.Command` only exposes `name` / `description` / `subcommands`
30 * publicly (see `effect/cli/Command`), not per-flag or per-argument
31 * metadata. Completions are therefore generated at the subcommand-name level
32 * only; flag/argument value completion is intentionally omitted until
33 * `effect/cli` exposes that metadata through a public API.
34 */
35 const toCompletionDescriptor = (command: Command.Command.Any): Completions.CommandDescriptor => ({
36 name: command.name,
37 description: command.description,
38 flags: [],
39 arguments: [],
40 subcommands: command.subcommands.flatMap(group => group.commands.map(toCompletionDescriptor)),
41 });
42
43 /**
44 * Generate a shell completion script for the given command tree and shell type.
45 */
46 export const getCompletionScript = (
47 command: Command.Command.Any,
48 shell: Shell
49 ): Effect.Effect<Array<string>> => {
50 const script = Completions.generate('composio', shell, toCompletionDescriptor(command));
51 const lines = script.split('\n');
52 return Effect.succeed(shell === 'fish' ? sanitizeFishCompletionLines(lines) : lines);
53 };
54
55 export const _test = {
56 sanitizeFishCompletionLine,
57 };