1
+import * as fs from 'node:fs';
2
+import * as path from 'node:path';
3
+import { Command, Args } from '@effect/cli';
4
+import { Effect, Option } from 'effect';
5
+import { ComposioCliUserConfig } from 'src/services/cli-user-config';
6
+import { TerminalUI } from 'src/services/terminal-ui';
7
+import { NodeOs } from 'src/services/node-os';
8
+import { CLI_EXPERIMENTAL_FEATURES } from 'src/constants';
9
+import { buildComposioCliSkill } from '../../../skills-src/composio-cli/index';
10
+import type { SkillFeatureFlag } from '../../../skills-src/composio-cli/reference-schema';
11
+
12
+const knownFeatures: readonly string[] = Object.values(CLI_EXPERIMENTAL_FEATURES);
13
+
14
+const featureArg = Args.text({ name: 'feature' }).pipe(
15
+ Args.withDescription(`Experimental feature name. Known features: ${knownFeatures.join(', ')}`),
16
+ Args.optional
17
+);
18
+
19
+const stateArg = Args.text({ name: 'state' }).pipe(
20
+ Args.withDescription('Set to "on" or "off"'),
21
+ Args.optional
22
+);
23
+
24
+const SKILL_NAME = 'composio-cli';
25
+
26
+/**
27
+ * Find every directory where the composio-cli skill is currently installed.
28
+ * Known locations:
29
+ * ~/.agents/skills/composio-cli (canonical install target)
30
+ * ~/.claude/skills/composio-cli (Claude Code — may be a symlink or a real dir)
31
+ *
32
+ * We resolve symlinks so we don't rebuild the same physical directory twice,
33
+ * but we also rebuild any real (non-symlink) copies so everything stays in sync.
34
+ */
35
+const discoverSkillRoots = (home: string): string[] => {
36
+ const candidates = [path.join(home, '.agents', 'skills'), path.join(home, '.claude', 'skills')];
37
+
38
+ const seen = new Set<string>();
39
+ const roots: string[] = [];
40
+
41
+ for (const parent of candidates) {
42
+ const skillDir = path.join(parent, SKILL_NAME);
43
+ try {
44
+ const stat = fs.lstatSync(skillDir);
45
+ if (stat.isSymbolicLink()) {
46
+ // Resolve the symlink target — we'll rebuild the real directory it points to
47
+ const realDir = fs.realpathSync(skillDir);
48
+ const realParent = path.dirname(realDir);
49
+ if (!seen.has(realParent)) {
50
+ seen.add(realParent);
51
+ roots.push(realParent);
52
+ }
53
+ } else if (stat.isDirectory()) {
54
+ if (!seen.has(parent)) {
55
+ seen.add(parent);
56
+ roots.push(parent);
57
+ }
58
+ }
59
+ } catch {
60
+ // Directory doesn't exist — skip
61
+ }
62
+ }
63
+
64
+ // Always include ~/.agents/skills as a fallback so there's at least one target
65
+ const agentSkillsRoot = path.join(home, '.agents', 'skills');
66
+ if (!seen.has(agentSkillsRoot)) {
67
+ roots.push(agentSkillsRoot);
68
+ }
69
+
70
+ return roots;
71
+};
72
+
73
+const rebuildSkill = Effect.gen(function* () {
74
+ const ui = yield* TerminalUI;
75
+ const os = yield* NodeOs;
76
+ const cliConfig = yield* ComposioCliUserConfig;
77
+ const home = os.homedir;
78
+
79
+ // Build the feature overrides from the current config
80
+ const featureOverrides: Partial<Record<SkillFeatureFlag, boolean>> = {};
81
+ for (const name of knownFeatures) {
82
+ featureOverrides[name as SkillFeatureFlag] = cliConfig.isExperimentalFeatureEnabled(name);
83
+ }
84
+
85
+ const roots = discoverSkillRoots(home);
86
+ for (const root of roots) {
87
+ buildComposioCliSkill({
88
+ channel: cliConfig.channel,
89
+ outputRoot: root,
90
+ featureOverrides,
91
+ });
92
+ }
93
+
94
+ yield* ui.log.step(
95
+ roots.length === 1
96
+ ? `Rebuilt skill in ${roots[0]}`
97
+ : `Rebuilt skill in ${roots.length} locations`
98
+ );
99
+});
100
+
101
+export const configExperimentalCmd = Command.make(
102
+ 'experimental',
103
+ { feature: featureArg, state: stateArg },
104
+ ({ feature, state }) =>
105
+ Effect.gen(function* () {
106
+ const ui = yield* TerminalUI;
107
+ const cliConfig = yield* ComposioCliUserConfig;
108
+
109
+ // No args: list all experimental features and their state
110
+ if (Option.isNone(feature)) {
111
+ const features = cliConfig.data.experimentalFeatures;
112
+ const lines: string[] = [];
113
+ for (const name of knownFeatures) {
114
+ const enabled = cliConfig.isExperimentalFeatureEnabled(name);
115
+ const configured = name in features;
116
+ const suffix = configured ? '' : ` (default, channel: ${cliConfig.channel})`;
117
+ lines.push(` ${name}: ${enabled ? 'on' : 'off'}${suffix}`);
118
+ }
119
+
120
+ // Show any custom features not in the known list
121
+ for (const [name, value] of Object.entries(features)) {
122
+ if (!knownFeatures.includes(name)) {
123
+ lines.push(` ${name}: ${value ? 'on' : 'off'}`);
124
+ }
125
+ }
126
+
127
+ if (lines.length === 0) {
128
+ yield* ui.log.info('No experimental features available.');
129
+ } else {
130
+ yield* ui.note(lines.join('\n'), 'Experimental Features');
131
+ yield* ui.output(JSON.stringify(features));
132
+ }
133
+ return;
134
+ }
135
+
136
+ const featureName = feature.value;
137
+
138
+ // Feature name only: show current state
139
+ if (Option.isNone(state)) {
140
+ const enabled = cliConfig.isExperimentalFeatureEnabled(featureName);
141
+ yield* ui.log.info(`${featureName}: ${enabled ? 'on' : 'off'}`);
142
+ yield* ui.output(enabled ? 'on' : 'off');
143
+ return;
144
+ }
145
+
146
+ const stateValue = state.value.toLowerCase();
147
+ if (stateValue !== 'on' && stateValue !== 'off') {
148
+ yield* ui.log.error(`Invalid state "${state.value}". Use "on" or "off".`);
149
+ return yield* Effect.fail(new Error(`Invalid state: ${state.value}`));
150
+ }
151
+
152
+ const enabled = stateValue === 'on';
153
+ yield* cliConfig.update({
154
+ experimentalFeatures: {
155
+ ...cliConfig.data.experimentalFeatures,
156
+ [featureName]: enabled,
157
+ },
158
+ });
159
+
160
+ yield* ui.log.success(`${featureName}: ${stateValue}`);
161
+
162
+ // Rebuild the skill so Claude Code picks up the new feature flags
163
+ yield* rebuildSkill.pipe(
164
+ Effect.catchAll(error =>
165
+ Effect.gen(function* () {
166
+ yield* Effect.logDebug('Skill rebuild failed:', error);
167
+ yield* ui.log.warn('Could not rebuild skill (non-fatal)');
168
+ })
169
+ )
170
+ );
171
+
172
+ yield* ui.output(stateValue);
173
+ })
174
+).pipe(
175
+ Command.withDescription(
176
+ [
177
+ 'View or toggle experimental feature flags.',
178
+ '',
179
+ 'Usage:',
180
+ ' composio config experimental List all features',
181
+ ' composio config experimental <feature> Show current state',
182
+ ' composio config experimental <feature> on|off Enable or disable',
183
+ '',
184
+ `Known features: ${knownFeatures.join(', ')}`,
185
+ ].join('\n')
186
+ )
187
+);