1
import process from 'node:process';
2
import { Effect, Option } from 'effect';
3
-import { Command } from '@effect/cli';
3
+import { Command, HelpDoc, ValidationError } from '@effect/cli';
4
import { $defaultCmd } from './$default.cmd';
5
import { getVersion } from 'src/effects/version';
6
import { versionCmd } from './version.cmd';
76
return $defaultCmd.pipe(Command.withSubcommands(subcommands as any));
77
};
78
79
+const formatSubcommandChoices = (choices: ReadonlyArray<string>) =>
80
+ choices.map(choice => `'${choice}'`).join(', ');
81
+
82
+/**
83
+ * Internal shape of an `@effect/cli` command descriptor. The public
84
+ * `CommandDescriptor.getSubcommands` helper flattens nested subcommand trees
85
+ * and only returns the *parent* Standard node for each branch, which means we
86
+ * can't use it to walk deeper than one level. Walking the `_tag`-based
87
+ * internal tree directly is the only way to recover the full descendable
88
+ * structure without losing subcommand context on descent.
89
+ */
90
+type InternalDescriptor =
91
+ | { _tag: 'Standard'; name: string }
92
+ | { _tag: 'GetUserInput'; name: string }
93
+ | { _tag: 'Map'; command: InternalDescriptor }
94
+ | {
95
+ _tag: 'Subcommands';
96
+ parent: InternalDescriptor;
97
+ children: ReadonlyArray<InternalDescriptor>;
98
+ };
99
+
100
+type UnwrappedDescriptor = Exclude<InternalDescriptor, { _tag: 'Map' }>;
101
+
102
+const unwrapMaps = (cmd: InternalDescriptor): UnwrappedDescriptor => {
103
+ let current: InternalDescriptor = cmd;
104
+ while (current._tag === 'Map') {
105
+ current = current.command;
106
+ }
107
+ return current;
108
+};
109
+
110
+const getCommandName = (cmd: InternalDescriptor): string => {
111
+ const unwrapped = unwrapMaps(cmd);
112
+ if (unwrapped._tag === 'Subcommands') {
113
+ return getCommandName(unwrapped.parent);
114
+ }
115
+ return unwrapped.name;
116
+};
117
+
118
+const getChildEntries = (
119
+ cmd: InternalDescriptor
120
+): ReadonlyArray<readonly [string, InternalDescriptor]> => {
121
+ const unwrapped = unwrapMaps(cmd);
122
+ if (unwrapped._tag !== 'Subcommands') {
123
+ return [];
124
+ }
125
+ return unwrapped.children.map(child => [getCommandName(child), child] as const);
126
+};
127
+
128
+const findNestedSubcommandMismatch = (
129
+ argv: ReadonlyArray<string>,
130
+ rootCommand: ReturnType<typeof buildRootCommand>
131
+): ReturnType<typeof ValidationError.commandMismatch> | undefined => {
132
+ const args = argv.slice(2);
133
+ let current: InternalDescriptor = rootCommand.descriptor as unknown as InternalDescriptor;
134
+ const path = ['composio'];
135
+
136
+ for (const token of args) {
137
+ if (!token || token === '--' || token === '--help' || token === '-h' || token.startsWith('-')) {
138
+ return undefined;
139
+ }
140
+
141
+ const children = getChildEntries(current);
142
+ if (children.length === 0) {
143
+ return undefined;
144
+ }
145
+
146
+ const match = children.find(([name]) => name === token);
147
+ if (match) {
148
+ current = match[1];
149
+ path.push(token);
150
+ continue;
151
+ }
152
+
153
+ if (path.length === 1) {
154
+ return undefined;
155
+ }
156
+
157
+ const available = children.map(([name]) => name).sort();
158
+ return ValidationError.commandMismatch(
159
+ HelpDoc.p(
160
+ `Invalid subcommand for ${path.join(' ')} - use one of ${formatSubcommandChoices(available)}`
161
+ )
162
+ );
163
+ }
164
+
165
+ return undefined;
166
+};
167
+
168
const ROOT_INSTALL_SKILL_FLAGS = ['--instal-skill', '--install-skill'] as const;
80
-const SKILL_INSTALL_TARGETS = ['claude', 'codex', 'openclaw'] as const satisfies ReadonlyArray<
81
- SkillInstallTarget
82
->;
169
+const SKILL_INSTALL_TARGETS = [
170
+ 'claude',
171
+ 'codex',
172
+ 'openclaw',
173
+] as const satisfies ReadonlyArray<SkillInstallTarget>;
174
175
type RootInstallSkillRequest =
176
| {
213
if (!isSkillInstallTarget(target)) {
214
return {
215
_tag: 'error',
125
- message:
126
- 'Invalid target for --instal-skill. Expected one of: claude, codex, openclaw.',
216
+ message: 'Invalid target for --instal-skill. Expected one of: claude, codex, openclaw.',
217
};
218
}
219
return { _tag: 'parsed', target };
224
if (!isSkillInstallTarget(target)) {
225
return {
226
_tag: 'error',
137
- message:
138
- 'Invalid target for --instal-skill. Expected one of: claude, codex, openclaw.',
227
+ message: 'Invalid target for --instal-skill. Expected one of: claude, codex, openclaw.',
228
};
229
}
230
return { _tag: 'parsed', skillName, target };
464
if (subHelp) {
465
return printSubcommandHelp(subHelp, visibility);
466
}
467
+ const nestedMismatch = findNestedSubcommandMismatch(normalizedArgv, rootCommand);
468
+ if (nestedMismatch) {
469
+ return Effect.fail(nestedMismatch);
470
+ }
471
const parallelExecute = runParallelToolsExecuteFromArgv(normalizedArgv);
472
if (parallelExecute) {
473
return parallelExecute;