@nordsym/apiclaw 1.3.7 → 1.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (203) hide show
  1. package/README.md +436 -200
  2. package/convex/_generated/api.d.ts +4 -0
  3. package/convex/agents.ts +403 -0
  4. package/convex/directCall.ts +80 -0
  5. package/convex/earnProgress.ts +753 -0
  6. package/convex/logs.ts +17 -0
  7. package/convex/providerKeys.ts +82 -2
  8. package/convex/schema.ts +71 -2
  9. package/convex/workspaces.ts +84 -2
  10. package/dist/adapters/base.d.ts +112 -0
  11. package/dist/adapters/base.d.ts.map +1 -0
  12. package/dist/adapters/base.js +247 -0
  13. package/dist/adapters/base.js.map +1 -0
  14. package/dist/adapters/claude-desktop.d.ts +12 -0
  15. package/dist/adapters/claude-desktop.d.ts.map +1 -0
  16. package/dist/adapters/claude-desktop.js +36 -0
  17. package/dist/adapters/claude-desktop.js.map +1 -0
  18. package/dist/adapters/cline.d.ts +20 -0
  19. package/dist/adapters/cline.d.ts.map +1 -0
  20. package/dist/adapters/cline.js +77 -0
  21. package/dist/adapters/cline.js.map +1 -0
  22. package/dist/adapters/continue.d.ts +26 -0
  23. package/dist/adapters/continue.d.ts.map +1 -0
  24. package/dist/adapters/continue.js +68 -0
  25. package/dist/adapters/continue.js.map +1 -0
  26. package/dist/adapters/cursor.d.ts +12 -0
  27. package/dist/adapters/cursor.d.ts.map +1 -0
  28. package/dist/adapters/cursor.js +38 -0
  29. package/dist/adapters/cursor.js.map +1 -0
  30. package/dist/adapters/custom.d.ts +47 -0
  31. package/dist/adapters/custom.d.ts.map +1 -0
  32. package/dist/adapters/custom.js +146 -0
  33. package/dist/adapters/custom.js.map +1 -0
  34. package/dist/adapters/detect.d.ts +69 -0
  35. package/dist/adapters/detect.d.ts.map +1 -0
  36. package/dist/adapters/detect.js +158 -0
  37. package/dist/adapters/detect.js.map +1 -0
  38. package/dist/adapters/index.d.ts +21 -0
  39. package/dist/adapters/index.d.ts.map +1 -0
  40. package/dist/adapters/index.js +23 -0
  41. package/dist/adapters/index.js.map +1 -0
  42. package/dist/adapters/windsurf.d.ts +12 -0
  43. package/dist/adapters/windsurf.d.ts.map +1 -0
  44. package/dist/adapters/windsurf.js +39 -0
  45. package/dist/adapters/windsurf.js.map +1 -0
  46. package/dist/bin.d.ts +9 -0
  47. package/dist/bin.d.ts.map +1 -0
  48. package/dist/bin.js +19 -0
  49. package/dist/bin.js.map +1 -0
  50. package/dist/cli/commands/doctor.d.ts +34 -0
  51. package/dist/cli/commands/doctor.d.ts.map +1 -0
  52. package/dist/cli/commands/doctor.js +312 -0
  53. package/dist/cli/commands/doctor.js.map +1 -0
  54. package/dist/cli/commands/index.d.ts +9 -0
  55. package/dist/cli/commands/index.d.ts.map +1 -0
  56. package/dist/cli/commands/index.js +9 -0
  57. package/dist/cli/commands/index.js.map +1 -0
  58. package/dist/cli/commands/mcp-install.d.ts +14 -0
  59. package/dist/cli/commands/mcp-install.d.ts.map +1 -0
  60. package/dist/cli/commands/mcp-install.js +246 -0
  61. package/dist/cli/commands/mcp-install.js.map +1 -0
  62. package/dist/cli/commands/restore.d.ts +50 -0
  63. package/dist/cli/commands/restore.d.ts.map +1 -0
  64. package/dist/cli/commands/restore.js +260 -0
  65. package/dist/cli/commands/restore.js.map +1 -0
  66. package/dist/cli/commands/setup.d.ts +19 -0
  67. package/dist/cli/commands/setup.d.ts.map +1 -0
  68. package/dist/cli/commands/setup.js +206 -0
  69. package/dist/cli/commands/setup.js.map +1 -0
  70. package/dist/cli/commands/uninstall.d.ts +37 -0
  71. package/dist/cli/commands/uninstall.d.ts.map +1 -0
  72. package/dist/cli/commands/uninstall.js +189 -0
  73. package/dist/cli/commands/uninstall.js.map +1 -0
  74. package/dist/cli/index.d.ts +7 -0
  75. package/dist/cli/index.d.ts.map +1 -0
  76. package/dist/cli/index.js +105 -0
  77. package/dist/cli/index.js.map +1 -0
  78. package/dist/discovery.d.ts +6 -2
  79. package/dist/discovery.d.ts.map +1 -1
  80. package/dist/discovery.js +296 -2
  81. package/dist/discovery.js.map +1 -1
  82. package/dist/enterprise/env.d.ts +56 -0
  83. package/dist/enterprise/env.d.ts.map +1 -0
  84. package/dist/enterprise/env.js +124 -0
  85. package/dist/enterprise/env.js.map +1 -0
  86. package/dist/enterprise/index.d.ts +7 -0
  87. package/dist/enterprise/index.d.ts.map +1 -0
  88. package/dist/enterprise/index.js +7 -0
  89. package/dist/enterprise/index.js.map +1 -0
  90. package/dist/enterprise/script-generator.d.ts +32 -0
  91. package/dist/enterprise/script-generator.d.ts.map +1 -0
  92. package/dist/enterprise/script-generator.js +461 -0
  93. package/dist/enterprise/script-generator.js.map +1 -0
  94. package/dist/execute.d.ts +21 -0
  95. package/dist/execute.d.ts.map +1 -1
  96. package/dist/execute.js +231 -0
  97. package/dist/execute.js.map +1 -1
  98. package/dist/index.js +79 -7
  99. package/dist/index.js.map +1 -1
  100. package/dist/stripe.d.ts +1 -1
  101. package/dist/stripe.js +1 -1
  102. package/dist/stripe.js.map +1 -1
  103. package/dist/types.d.ts +29 -0
  104. package/dist/types.d.ts.map +1 -1
  105. package/dist/ui/colors.d.ts +111 -0
  106. package/dist/ui/colors.d.ts.map +1 -0
  107. package/dist/ui/colors.js +185 -0
  108. package/dist/ui/colors.js.map +1 -0
  109. package/dist/ui/errors.d.ts +69 -0
  110. package/dist/ui/errors.d.ts.map +1 -0
  111. package/dist/ui/errors.js +334 -0
  112. package/dist/ui/errors.js.map +1 -0
  113. package/dist/ui/index.d.ts +10 -0
  114. package/dist/ui/index.d.ts.map +1 -0
  115. package/dist/ui/index.js +14 -0
  116. package/dist/ui/index.js.map +1 -0
  117. package/dist/ui/prompts.d.ts +88 -0
  118. package/dist/ui/prompts.d.ts.map +1 -0
  119. package/dist/ui/prompts.js +295 -0
  120. package/dist/ui/prompts.js.map +1 -0
  121. package/dist/ui/spinner.d.ts +112 -0
  122. package/dist/ui/spinner.d.ts.map +1 -0
  123. package/dist/ui/spinner.js +229 -0
  124. package/dist/ui/spinner.js.map +1 -0
  125. package/dist/utils/backup.d.ts +48 -0
  126. package/dist/utils/backup.d.ts.map +1 -0
  127. package/dist/utils/backup.js +182 -0
  128. package/dist/utils/backup.js.map +1 -0
  129. package/dist/utils/config.d.ts +80 -0
  130. package/dist/utils/config.d.ts.map +1 -0
  131. package/dist/utils/config.js +221 -0
  132. package/dist/utils/config.js.map +1 -0
  133. package/dist/utils/os.d.ts +45 -0
  134. package/dist/utils/os.d.ts.map +1 -0
  135. package/dist/utils/os.js +106 -0
  136. package/dist/utils/os.js.map +1 -0
  137. package/dist/utils/paths.d.ts +38 -0
  138. package/dist/utils/paths.d.ts.map +1 -0
  139. package/dist/utils/paths.js +160 -0
  140. package/dist/utils/paths.js.map +1 -0
  141. package/docs/PRD-BILLING.md +226 -0
  142. package/docs/PRD-EARN-SYSTEM.md +261 -0
  143. package/docs/PRD-MCP-AUTO-SETUP.md +623 -0
  144. package/docs/enterprise-deployment.md +728 -0
  145. package/landing/next.config.mjs +14 -0
  146. package/landing/public/book/index.html +24 -5
  147. package/landing/public/demo-product.jpg +0 -0
  148. package/landing/public/stats.json +4 -2
  149. package/landing/scripts/generate-stats.js +13 -0
  150. package/landing/src/app/api/og/route.tsx +1 -3
  151. package/landing/src/app/api/workspace-auth/magic-link/route.ts +6 -3
  152. package/landing/src/app/auth/verify/page.tsx +11 -4
  153. package/landing/src/app/docs/page.tsx +1 -1
  154. package/landing/src/app/join/page.tsx +49 -0
  155. package/landing/src/app/layout.tsx +1 -1
  156. package/landing/src/app/login/page.tsx +7 -1
  157. package/landing/src/app/page.tsx +50 -38
  158. package/landing/src/app/providers/register/page.tsx +1 -1
  159. package/landing/src/app/workspace/page.tsx +484 -806
  160. package/landing/src/components/CheckoutButton.tsx +1 -1
  161. package/landing/src/components/EarnCreditsTab.tsx +842 -0
  162. package/landing/src/components/HeroTabs.tsx +2 -2
  163. package/landing/src/components/demo/PhoneDemo.tsx +423 -0
  164. package/landing/src/components/demo/index.ts +1 -0
  165. package/landing/src/lib/stats.json +3 -1
  166. package/landing/tailwind.config.ts +10 -0
  167. package/package.json +9 -2
  168. package/src/adapters/base.ts +363 -0
  169. package/src/adapters/claude-desktop.ts +41 -0
  170. package/src/adapters/cline.ts +88 -0
  171. package/src/adapters/continue.ts +91 -0
  172. package/src/adapters/cursor.ts +43 -0
  173. package/src/adapters/custom.ts +188 -0
  174. package/src/adapters/detect.ts +202 -0
  175. package/src/adapters/index.ts +47 -0
  176. package/src/adapters/windsurf.ts +44 -0
  177. package/src/bin.ts +19 -0
  178. package/src/cli/commands/doctor.ts +367 -0
  179. package/src/cli/commands/index.ts +9 -0
  180. package/src/cli/commands/mcp-install.ts +291 -0
  181. package/src/cli/commands/restore.ts +333 -0
  182. package/src/cli/commands/setup.ts +276 -0
  183. package/src/cli/commands/uninstall.ts +240 -0
  184. package/src/cli/index.ts +116 -0
  185. package/src/discovery.ts +328 -3
  186. package/src/enterprise/env.ts +156 -0
  187. package/src/enterprise/index.ts +7 -0
  188. package/src/enterprise/script-generator.ts +481 -0
  189. package/src/execute.ts +256 -0
  190. package/src/index.ts +85 -7
  191. package/src/stripe.ts +1 -1
  192. package/src/types.ts +32 -0
  193. package/src/ui/colors.ts +219 -0
  194. package/src/ui/errors.ts +394 -0
  195. package/src/ui/index.ts +17 -0
  196. package/src/ui/prompts.ts +390 -0
  197. package/src/ui/spinner.ts +325 -0
  198. package/src/utils/backup.ts +224 -0
  199. package/src/utils/config.ts +315 -0
  200. package/src/utils/os.ts +124 -0
  201. package/src/utils/paths.ts +203 -0
  202. package/STATUS.md +0 -160
  203. package/landing/tsconfig.tsbuildinfo +0 -1
@@ -0,0 +1,333 @@
1
+ /**
2
+ * Restore Command
3
+ * Rollback MCP client configs from backups
4
+ */
5
+
6
+ import { existsSync } from 'fs';
7
+ import {
8
+ listBackups,
9
+ restoreBackup as doRestore,
10
+ getLatestBackup,
11
+ formatBackupInfo,
12
+ BackupInfo
13
+ } from '../../utils/backup.js';
14
+ import { getAllClients, getClientConfig, MCPClient, parseClientArg } from '../../utils/paths.js';
15
+
16
+ export interface RestoreOptions {
17
+ client?: string; // Specific client to restore
18
+ backup?: string; // Specific backup file path
19
+ list?: boolean; // List available backups
20
+ all?: boolean; // Restore all clients
21
+ interactive?: boolean; // Interactive mode (future)
22
+ dryRun?: boolean; // Show what would be done
23
+ }
24
+
25
+ export interface RestoreResult {
26
+ success: boolean;
27
+ client: string;
28
+ message: string;
29
+ backupUsed?: string;
30
+ preRestoreBackup?: string;
31
+ }
32
+
33
+ /**
34
+ * List all available backups for a client
35
+ */
36
+ function listClientBackups(client: MCPClient): BackupInfo[] {
37
+ const config = getClientConfig(client);
38
+ return listBackups(config.configPath);
39
+ }
40
+
41
+ /**
42
+ * List all backups across all clients
43
+ */
44
+ export function listAllBackups(): Map<MCPClient, BackupInfo[]> {
45
+ const results = new Map<MCPClient, BackupInfo[]>();
46
+
47
+ for (const client of getAllClients()) {
48
+ const backups = listClientBackups(client);
49
+ if (backups.length > 0) {
50
+ results.set(client, backups);
51
+ }
52
+ }
53
+
54
+ return results;
55
+ }
56
+
57
+ /**
58
+ * Format backup list for display
59
+ */
60
+ export function formatBackupList(backups: Map<MCPClient, BackupInfo[]>): string {
61
+ const lines: string[] = [];
62
+
63
+ lines.push('');
64
+ lines.push('šŸ“¦ Available Backups');
65
+ lines.push('====================');
66
+ lines.push('');
67
+
68
+ if (backups.size === 0) {
69
+ lines.push('No backups found.');
70
+ lines.push('');
71
+ lines.push('Backups are created automatically when you run setup.');
72
+ lines.push('');
73
+ return lines.join('\n');
74
+ }
75
+
76
+ for (const [client, clientBackups] of backups) {
77
+ const config = getClientConfig(client);
78
+ lines.push(`${config.displayName}:`);
79
+
80
+ for (let i = 0; i < clientBackups.length; i++) {
81
+ const backup = clientBackups[i];
82
+ const marker = i === 0 ? ' (latest)' : '';
83
+ lines.push(` ${i + 1}. ${formatBackupInfo(backup)}${marker}`);
84
+ }
85
+
86
+ lines.push('');
87
+ }
88
+
89
+ lines.push('Usage:');
90
+ lines.push(' npx @nordsym/apiclaw restore # Restore latest for all');
91
+ lines.push(' npx @nordsym/apiclaw restore --client claude # Restore latest for Claude');
92
+ lines.push(' npx @nordsym/apiclaw restore --backup <path> # Restore specific backup');
93
+ lines.push('');
94
+
95
+ return lines.join('\n');
96
+ }
97
+
98
+ /**
99
+ * Restore a specific client to its latest backup
100
+ */
101
+ export function restoreClient(client: MCPClient): RestoreResult {
102
+ const config = getClientConfig(client);
103
+ const configPath = config.configPath;
104
+
105
+ // Check if config exists
106
+ if (!existsSync(configPath)) {
107
+ return {
108
+ success: false,
109
+ client: config.displayName,
110
+ message: 'No config file to restore',
111
+ };
112
+ }
113
+
114
+ // Get latest backup
115
+ const latest = getLatestBackup(configPath);
116
+
117
+ if (!latest) {
118
+ return {
119
+ success: false,
120
+ client: config.displayName,
121
+ message: 'No backups available',
122
+ };
123
+ }
124
+
125
+ // Perform restore
126
+ const result = doRestore(latest.path, configPath);
127
+
128
+ if (result.success) {
129
+ return {
130
+ success: true,
131
+ client: config.displayName,
132
+ message: 'Restored successfully',
133
+ backupUsed: latest.filename,
134
+ preRestoreBackup: result.backupPath || undefined,
135
+ };
136
+ }
137
+
138
+ return {
139
+ success: false,
140
+ client: config.displayName,
141
+ message: result.error || 'Unknown error',
142
+ };
143
+ }
144
+
145
+ /**
146
+ * Restore from a specific backup file
147
+ */
148
+ export function restoreFromFile(backupPath: string, client?: MCPClient): RestoreResult {
149
+ // Validate backup exists
150
+ if (!existsSync(backupPath)) {
151
+ return {
152
+ success: false,
153
+ client: client ? getClientConfig(client).displayName : 'Unknown',
154
+ message: `Backup file not found: ${backupPath}`,
155
+ };
156
+ }
157
+
158
+ // If client not specified, try to detect from backup path
159
+ if (!client) {
160
+ for (const c of getAllClients()) {
161
+ const config = getClientConfig(c);
162
+ if (backupPath.includes(config.configDir)) {
163
+ client = c;
164
+ break;
165
+ }
166
+ }
167
+ }
168
+
169
+ if (!client) {
170
+ return {
171
+ success: false,
172
+ client: 'Unknown',
173
+ message: 'Could not determine target client. Use --client to specify.',
174
+ };
175
+ }
176
+
177
+ const config = getClientConfig(client);
178
+ const result = doRestore(backupPath, config.configPath);
179
+
180
+ if (result.success) {
181
+ return {
182
+ success: true,
183
+ client: config.displayName,
184
+ message: 'Restored successfully',
185
+ backupUsed: backupPath,
186
+ preRestoreBackup: result.backupPath || undefined,
187
+ };
188
+ }
189
+
190
+ return {
191
+ success: false,
192
+ client: config.displayName,
193
+ message: result.error || 'Unknown error',
194
+ };
195
+ }
196
+
197
+ /**
198
+ * Restore all clients to their latest backups
199
+ */
200
+ export function restoreAll(): RestoreResult[] {
201
+ const results: RestoreResult[] = [];
202
+
203
+ for (const client of getAllClients()) {
204
+ const config = getClientConfig(client);
205
+
206
+ // Skip clients without backups
207
+ const backups = listClientBackups(client);
208
+ if (backups.length === 0) {
209
+ continue;
210
+ }
211
+
212
+ results.push(restoreClient(client));
213
+ }
214
+
215
+ return results;
216
+ }
217
+
218
+ /**
219
+ * Format restore results for display
220
+ */
221
+ export function formatRestoreResults(results: RestoreResult[]): string {
222
+ const lines: string[] = [];
223
+
224
+ lines.push('');
225
+ lines.push('šŸ”„ Restore Results');
226
+ lines.push('==================');
227
+ lines.push('');
228
+
229
+ if (results.length === 0) {
230
+ lines.push('No clients had backups to restore.');
231
+ lines.push('');
232
+ return lines.join('\n');
233
+ }
234
+
235
+ for (const result of results) {
236
+ const icon = result.success ? 'āœ“' : 'āœ—';
237
+ lines.push(`${icon} ${result.client}: ${result.message}`);
238
+
239
+ if (result.backupUsed) {
240
+ lines.push(` Restored from: ${result.backupUsed}`);
241
+ }
242
+
243
+ if (result.preRestoreBackup) {
244
+ lines.push(` Pre-restore backup: ${result.preRestoreBackup}`);
245
+ }
246
+ }
247
+
248
+ lines.push('');
249
+
250
+ const successful = results.filter(r => r.success).length;
251
+ if (successful > 0) {
252
+ lines.push(`āœ“ Restored ${successful} client(s). Restart your MCP clients to apply changes.`);
253
+ }
254
+
255
+ lines.push('');
256
+
257
+ return lines.join('\n');
258
+ }
259
+
260
+ /**
261
+ * Restore command handler
262
+ */
263
+ export async function restoreCommand(options: RestoreOptions = {}): Promise<void> {
264
+ // List mode
265
+ if (options.list) {
266
+ const backups = listAllBackups();
267
+ console.log(formatBackupList(backups));
268
+ return;
269
+ }
270
+
271
+ // Dry run mode
272
+ if (options.dryRun) {
273
+ console.log('\nšŸ” Dry run - would restore:\n');
274
+
275
+ if (options.backup) {
276
+ console.log(` From file: ${options.backup}`);
277
+ } else if (options.client) {
278
+ const client = parseClientArg(options.client);
279
+ if (client) {
280
+ const latest = getLatestBackup(getClientConfig(client).configPath);
281
+ console.log(` ${getClientConfig(client).displayName}: ${latest?.filename || 'No backup'}`);
282
+ }
283
+ } else {
284
+ const backups = listAllBackups();
285
+ for (const [client, clientBackups] of backups) {
286
+ console.log(` ${getClientConfig(client).displayName}: ${clientBackups[0]?.filename || 'No backup'}`);
287
+ }
288
+ }
289
+
290
+ console.log('\nRun without --dry-run to perform restore.\n');
291
+ return;
292
+ }
293
+
294
+ // Restore from specific file
295
+ if (options.backup) {
296
+ const client = options.client ? parseClientArg(options.client) : undefined;
297
+ const result = restoreFromFile(options.backup, client || undefined);
298
+ console.log(formatRestoreResults([result]));
299
+
300
+ if (!result.success) {
301
+ process.exit(1);
302
+ }
303
+ return;
304
+ }
305
+
306
+ // Restore specific client
307
+ if (options.client) {
308
+ const client = parseClientArg(options.client);
309
+
310
+ if (!client) {
311
+ console.error(`\nāœ— Unknown client: ${options.client}\n`);
312
+ console.error('Valid clients: claude, cursor, windsurf, cline, continue\n');
313
+ process.exit(1);
314
+ }
315
+
316
+ const result = restoreClient(client);
317
+ console.log(formatRestoreResults([result]));
318
+
319
+ if (!result.success) {
320
+ process.exit(1);
321
+ }
322
+ return;
323
+ }
324
+
325
+ // Restore all clients with backups
326
+ const results = restoreAll();
327
+ console.log(formatRestoreResults(results));
328
+
329
+ const failures = results.filter(r => !r.success);
330
+ if (failures.length > 0) {
331
+ process.exit(1);
332
+ }
333
+ }
@@ -0,0 +1,276 @@
1
+ /**
2
+ * Setup Command
3
+ * Configures APIClaw for MCP clients
4
+ */
5
+
6
+ import { existsSync } from 'fs';
7
+ import { detectOS, getOSDisplayName } from '../../utils/os.js';
8
+ import {
9
+ getAllClients,
10
+ getConfigPath,
11
+ getClientConfig,
12
+ parseClientArg,
13
+ MCPClient,
14
+ ClientPathConfig
15
+ } from '../../utils/paths.js';
16
+ import {
17
+ readConfig,
18
+ writeConfig,
19
+ mergeApiclawConfig,
20
+ mergeApiclawContinueConfig,
21
+ hasApiclawConfig,
22
+ isContinueFormat,
23
+ MCPConfig,
24
+ ContinueConfig
25
+ } from '../../utils/config.js';
26
+ import { createBackup, getLatestBackup } from '../../utils/backup.js';
27
+
28
+ export interface SetupOptions {
29
+ client?: string;
30
+ config?: string;
31
+ workspace?: string;
32
+ dryRun?: boolean;
33
+ force?: boolean;
34
+ wizard?: boolean;
35
+ all?: boolean;
36
+ verbose?: boolean;
37
+ }
38
+
39
+ interface SetupResult {
40
+ client: string;
41
+ success: boolean;
42
+ message: string;
43
+ backupPath?: string | null;
44
+ skipped?: boolean;
45
+ }
46
+
47
+ /**
48
+ * Detect which MCP clients are installed
49
+ */
50
+ function detectInstalledClients(): MCPClient[] {
51
+ const clients = getAllClients();
52
+ const installed: MCPClient[] = [];
53
+
54
+ for (const client of clients) {
55
+ const configPath = getConfigPath(client);
56
+ // Check if config exists OR if parent directory exists (client might be installed but not configured)
57
+ if (existsSync(configPath)) {
58
+ installed.push(client);
59
+ }
60
+ }
61
+
62
+ return installed;
63
+ }
64
+
65
+ /**
66
+ * Check if a client's config directory structure exists (might not have config yet)
67
+ */
68
+ function clientMightBeInstalled(client: MCPClient): boolean {
69
+ const config = getClientConfig(client);
70
+ const configDir = config.configDir;
71
+
72
+ // Check a few levels up to see if the app is installed
73
+ const pathParts = configDir.split('/').filter(Boolean);
74
+ for (let i = pathParts.length; i >= Math.max(0, pathParts.length - 3); i--) {
75
+ const checkPath = '/' + pathParts.slice(0, i).join('/');
76
+ if (existsSync(checkPath)) {
77
+ return true;
78
+ }
79
+ }
80
+
81
+ return false;
82
+ }
83
+
84
+ /**
85
+ * Setup a single client
86
+ */
87
+ async function setupClient(
88
+ client: MCPClient | 'custom',
89
+ configPath: string,
90
+ options: SetupOptions
91
+ ): Promise<SetupResult> {
92
+ const displayName = client === 'custom' ? 'Custom' : getClientConfig(client as MCPClient).displayName;
93
+
94
+ if (options.verbose) {
95
+ console.log(`\nšŸ“ Configuring ${displayName}...`);
96
+ console.log(` Config path: ${configPath}`);
97
+ }
98
+
99
+ // Read existing config
100
+ const readResult = readConfig(configPath);
101
+
102
+ if (!readResult.success) {
103
+ return {
104
+ client: displayName,
105
+ success: false,
106
+ message: readResult.error || 'Failed to read config',
107
+ };
108
+ }
109
+
110
+ const existingConfig = readResult.config!;
111
+
112
+ // Check if APIClaw is already configured
113
+ if (hasApiclawConfig(existingConfig) && !options.force) {
114
+ if (options.verbose) {
115
+ console.log(` ā­ļø APIClaw already configured (use --force to update)`);
116
+ }
117
+ return {
118
+ client: displayName,
119
+ success: true,
120
+ message: 'Already configured',
121
+ skipped: true,
122
+ };
123
+ }
124
+
125
+ // Merge APIClaw config
126
+ let newConfig: MCPConfig | ContinueConfig;
127
+
128
+ if (client === 'continue' || isContinueFormat(existingConfig)) {
129
+ newConfig = mergeApiclawContinueConfig(existingConfig as ContinueConfig, {
130
+ workspace: options.workspace,
131
+ force: options.force,
132
+ });
133
+ } else {
134
+ newConfig = mergeApiclawConfig(existingConfig as MCPConfig, {
135
+ workspace: options.workspace,
136
+ force: options.force,
137
+ });
138
+ }
139
+
140
+ // Dry run - show what would happen
141
+ if (options.dryRun) {
142
+ console.log(`\nšŸ“‹ Would configure ${displayName}:`);
143
+ console.log(` Path: ${configPath}`);
144
+ console.log(` Changes:`);
145
+ if (isContinueFormat(newConfig)) {
146
+ const continueConfig = newConfig as ContinueConfig;
147
+ const apiclaw = continueConfig.mcpServers?.find(s => s.name === 'apiclaw');
148
+ console.log(JSON.stringify(apiclaw, null, 4).split('\n').map(l => ` ${l}`).join('\n'));
149
+ } else {
150
+ const mcpConfig = newConfig as MCPConfig;
151
+ console.log(JSON.stringify({ apiclaw: mcpConfig.mcpServers?.apiclaw }, null, 4).split('\n').map(l => ` ${l}`).join('\n'));
152
+ }
153
+
154
+ return {
155
+ client: displayName,
156
+ success: true,
157
+ message: 'Dry run - no changes made',
158
+ skipped: true,
159
+ };
160
+ }
161
+
162
+ // Write config
163
+ const writeResult = writeConfig(configPath, newConfig, true);
164
+
165
+ if (!writeResult.success) {
166
+ return {
167
+ client: displayName,
168
+ success: false,
169
+ message: writeResult.error || 'Failed to write config',
170
+ };
171
+ }
172
+
173
+ return {
174
+ client: displayName,
175
+ success: true,
176
+ message: readResult.isNew ? 'Created new config' : 'Updated existing config',
177
+ backupPath: writeResult.backupPath,
178
+ };
179
+ }
180
+
181
+ /**
182
+ * Main setup command handler
183
+ */
184
+ export async function setupCommand(options: SetupOptions): Promise<void> {
185
+ const os = detectOS();
186
+ const osName = getOSDisplayName();
187
+
188
+ console.log('\nšŸš€ APIClaw MCP Auto-Setup\n');
189
+ console.log(`Platform: ${osName}\n`);
190
+
191
+ // Determine which clients to configure
192
+ let clientsToSetup: Array<{ client: MCPClient | 'custom'; path: string }> = [];
193
+
194
+ if (options.config) {
195
+ // Custom config path
196
+ clientsToSetup.push({ client: 'custom', path: options.config });
197
+ } else if (options.client) {
198
+ // Specific client
199
+ const client = parseClientArg(options.client);
200
+
201
+ if (!client) {
202
+ console.error(`āŒ Unknown client: ${options.client}`);
203
+ console.log(' Supported clients: claude-desktop, cursor, windsurf, cline, continue');
204
+ process.exit(1);
205
+ }
206
+
207
+ clientsToSetup.push({ client, path: getConfigPath(client) });
208
+ } else {
209
+ // Auto-detect or all
210
+ console.log('šŸ” Detecting MCP clients...\n');
211
+
212
+ const allClients = getAllClients();
213
+ const detected = detectInstalledClients();
214
+
215
+ for (const client of allClients) {
216
+ const config = getClientConfig(client);
217
+ const isDetected = detected.includes(client);
218
+ const icon = isDetected ? 'āœ“' : 'āœ—';
219
+ const status = isDetected ? 'found' : 'not found';
220
+ console.log(` ${icon} ${config.displayName} ${status}`);
221
+
222
+ if (isDetected || options.all) {
223
+ clientsToSetup.push({ client, path: config.configPath });
224
+ }
225
+ }
226
+
227
+ console.log('');
228
+
229
+ if (clientsToSetup.length === 0) {
230
+ console.log('āš ļø No MCP clients detected. Use --client to specify manually.');
231
+ console.log(' Example: npx @nordsym/apiclaw setup --client claude-desktop');
232
+ process.exit(0);
233
+ }
234
+ }
235
+
236
+ // Setup each client
237
+ const results: SetupResult[] = [];
238
+
239
+ for (const { client, path } of clientsToSetup) {
240
+ const result = await setupClient(client, path, options);
241
+ results.push(result);
242
+
243
+ const icon = result.success ? (result.skipped ? 'ā­ļø' : 'āœ“') : 'āœ—';
244
+ console.log(`${icon} ${result.client}: ${result.message}`);
245
+
246
+ if (result.backupPath && options.verbose) {
247
+ console.log(` Backup: ${result.backupPath}`);
248
+ }
249
+ }
250
+
251
+ // Summary
252
+ const succeeded = results.filter(r => r.success && !r.skipped).length;
253
+ const skipped = results.filter(r => r.skipped).length;
254
+ const failed = results.filter(r => !r.success).length;
255
+
256
+ console.log('\n' + '═'.repeat(50));
257
+
258
+ if (failed === 0) {
259
+ if (options.dryRun) {
260
+ console.log('\nāœ… Dry run complete! Use without --dry-run to apply changes.\n');
261
+ } else if (succeeded > 0) {
262
+ console.log('\nāœ… APIClaw configured successfully!\n');
263
+ console.log('Next steps:');
264
+ console.log(' 1. Restart your AI coding assistant');
265
+ console.log(' 2. Ask your agent: "List available APIs"\n');
266
+ console.log('Need help? https://docs.apiclaw.com/setup\n');
267
+ } else if (skipped === results.length) {
268
+ console.log('\nāœ… APIClaw already configured in all clients.\n');
269
+ console.log('Use --force to reconfigure.\n');
270
+ }
271
+ } else {
272
+ console.log(`\nāš ļø Setup completed with ${failed} error(s).\n`);
273
+ console.log('For troubleshooting, visit: https://docs.apiclaw.com/setup/troubleshooting\n');
274
+ process.exit(1);
275
+ }
276
+ }