@nordsym/apiclaw 1.3.6 → 1.3.8

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 (214) hide show
  1. package/README.md +422 -169
  2. package/convex/_generated/api.d.ts +16 -0
  3. package/convex/agents.ts +403 -0
  4. package/convex/billing.ts +651 -216
  5. package/convex/crons.ts +17 -0
  6. package/convex/directCall.ts +80 -0
  7. package/convex/earnProgress.ts +753 -0
  8. package/convex/email.ts +135 -82
  9. package/convex/feedback.ts +265 -0
  10. package/convex/http.ts +80 -4
  11. package/convex/logs.ts +304 -0
  12. package/convex/providerKeys.ts +289 -0
  13. package/convex/providers.ts +18 -0
  14. package/convex/schema.ts +185 -1
  15. package/convex/stripeActions.ts +512 -0
  16. package/convex/webhooks.ts +494 -0
  17. package/convex/workspaces.ts +158 -3
  18. package/dist/adapters/base.d.ts +112 -0
  19. package/dist/adapters/base.d.ts.map +1 -0
  20. package/dist/adapters/base.js +247 -0
  21. package/dist/adapters/base.js.map +1 -0
  22. package/dist/adapters/claude-desktop.d.ts +12 -0
  23. package/dist/adapters/claude-desktop.d.ts.map +1 -0
  24. package/dist/adapters/claude-desktop.js +36 -0
  25. package/dist/adapters/claude-desktop.js.map +1 -0
  26. package/dist/adapters/cline.d.ts +20 -0
  27. package/dist/adapters/cline.d.ts.map +1 -0
  28. package/dist/adapters/cline.js +77 -0
  29. package/dist/adapters/cline.js.map +1 -0
  30. package/dist/adapters/continue.d.ts +26 -0
  31. package/dist/adapters/continue.d.ts.map +1 -0
  32. package/dist/adapters/continue.js +68 -0
  33. package/dist/adapters/continue.js.map +1 -0
  34. package/dist/adapters/cursor.d.ts +12 -0
  35. package/dist/adapters/cursor.d.ts.map +1 -0
  36. package/dist/adapters/cursor.js +38 -0
  37. package/dist/adapters/cursor.js.map +1 -0
  38. package/dist/adapters/custom.d.ts +47 -0
  39. package/dist/adapters/custom.d.ts.map +1 -0
  40. package/dist/adapters/custom.js +146 -0
  41. package/dist/adapters/custom.js.map +1 -0
  42. package/dist/adapters/detect.d.ts +69 -0
  43. package/dist/adapters/detect.d.ts.map +1 -0
  44. package/dist/adapters/detect.js +158 -0
  45. package/dist/adapters/detect.js.map +1 -0
  46. package/dist/adapters/index.d.ts +21 -0
  47. package/dist/adapters/index.d.ts.map +1 -0
  48. package/dist/adapters/index.js +23 -0
  49. package/dist/adapters/index.js.map +1 -0
  50. package/dist/adapters/windsurf.d.ts +12 -0
  51. package/dist/adapters/windsurf.d.ts.map +1 -0
  52. package/dist/adapters/windsurf.js +39 -0
  53. package/dist/adapters/windsurf.js.map +1 -0
  54. package/dist/bin.d.ts +9 -0
  55. package/dist/bin.d.ts.map +1 -0
  56. package/dist/bin.js +19 -0
  57. package/dist/bin.js.map +1 -0
  58. package/dist/cli/commands/doctor.d.ts +34 -0
  59. package/dist/cli/commands/doctor.d.ts.map +1 -0
  60. package/dist/cli/commands/doctor.js +312 -0
  61. package/dist/cli/commands/doctor.js.map +1 -0
  62. package/dist/cli/commands/index.d.ts +9 -0
  63. package/dist/cli/commands/index.d.ts.map +1 -0
  64. package/dist/cli/commands/index.js +9 -0
  65. package/dist/cli/commands/index.js.map +1 -0
  66. package/dist/cli/commands/restore.d.ts +50 -0
  67. package/dist/cli/commands/restore.d.ts.map +1 -0
  68. package/dist/cli/commands/restore.js +260 -0
  69. package/dist/cli/commands/restore.js.map +1 -0
  70. package/dist/cli/commands/setup.d.ts +19 -0
  71. package/dist/cli/commands/setup.d.ts.map +1 -0
  72. package/dist/cli/commands/setup.js +206 -0
  73. package/dist/cli/commands/setup.js.map +1 -0
  74. package/dist/cli/commands/uninstall.d.ts +37 -0
  75. package/dist/cli/commands/uninstall.d.ts.map +1 -0
  76. package/dist/cli/commands/uninstall.js +189 -0
  77. package/dist/cli/commands/uninstall.js.map +1 -0
  78. package/dist/cli/index.d.ts +7 -0
  79. package/dist/cli/index.d.ts.map +1 -0
  80. package/dist/cli/index.js +97 -0
  81. package/dist/cli/index.js.map +1 -0
  82. package/dist/discovery.d.ts +6 -2
  83. package/dist/discovery.d.ts.map +1 -1
  84. package/dist/discovery.js +296 -2
  85. package/dist/discovery.js.map +1 -1
  86. package/dist/enterprise/env.d.ts +56 -0
  87. package/dist/enterprise/env.d.ts.map +1 -0
  88. package/dist/enterprise/env.js +124 -0
  89. package/dist/enterprise/env.js.map +1 -0
  90. package/dist/enterprise/index.d.ts +7 -0
  91. package/dist/enterprise/index.d.ts.map +1 -0
  92. package/dist/enterprise/index.js +7 -0
  93. package/dist/enterprise/index.js.map +1 -0
  94. package/dist/enterprise/script-generator.d.ts +32 -0
  95. package/dist/enterprise/script-generator.d.ts.map +1 -0
  96. package/dist/enterprise/script-generator.js +461 -0
  97. package/dist/enterprise/script-generator.js.map +1 -0
  98. package/dist/execute.d.ts +21 -0
  99. package/dist/execute.d.ts.map +1 -1
  100. package/dist/execute.js +231 -0
  101. package/dist/execute.js.map +1 -1
  102. package/dist/index.js +257 -7
  103. package/dist/index.js.map +1 -1
  104. package/dist/metered.d.ts +62 -0
  105. package/dist/metered.d.ts.map +1 -0
  106. package/dist/metered.js +81 -0
  107. package/dist/metered.js.map +1 -0
  108. package/dist/stripe.d.ts +62 -0
  109. package/dist/stripe.d.ts.map +1 -1
  110. package/dist/stripe.js +212 -0
  111. package/dist/stripe.js.map +1 -1
  112. package/dist/types.d.ts +29 -0
  113. package/dist/types.d.ts.map +1 -1
  114. package/dist/ui/colors.d.ts +111 -0
  115. package/dist/ui/colors.d.ts.map +1 -0
  116. package/dist/ui/colors.js +185 -0
  117. package/dist/ui/colors.js.map +1 -0
  118. package/dist/ui/errors.d.ts +69 -0
  119. package/dist/ui/errors.d.ts.map +1 -0
  120. package/dist/ui/errors.js +334 -0
  121. package/dist/ui/errors.js.map +1 -0
  122. package/dist/ui/index.d.ts +10 -0
  123. package/dist/ui/index.d.ts.map +1 -0
  124. package/dist/ui/index.js +14 -0
  125. package/dist/ui/index.js.map +1 -0
  126. package/dist/ui/prompts.d.ts +88 -0
  127. package/dist/ui/prompts.d.ts.map +1 -0
  128. package/dist/ui/prompts.js +295 -0
  129. package/dist/ui/prompts.js.map +1 -0
  130. package/dist/ui/spinner.d.ts +112 -0
  131. package/dist/ui/spinner.d.ts.map +1 -0
  132. package/dist/ui/spinner.js +229 -0
  133. package/dist/ui/spinner.js.map +1 -0
  134. package/dist/utils/backup.d.ts +48 -0
  135. package/dist/utils/backup.d.ts.map +1 -0
  136. package/dist/utils/backup.js +182 -0
  137. package/dist/utils/backup.js.map +1 -0
  138. package/dist/utils/config.d.ts +80 -0
  139. package/dist/utils/config.d.ts.map +1 -0
  140. package/dist/utils/config.js +221 -0
  141. package/dist/utils/config.js.map +1 -0
  142. package/dist/utils/os.d.ts +45 -0
  143. package/dist/utils/os.d.ts.map +1 -0
  144. package/dist/utils/os.js +106 -0
  145. package/dist/utils/os.js.map +1 -0
  146. package/dist/utils/paths.d.ts +38 -0
  147. package/dist/utils/paths.d.ts.map +1 -0
  148. package/dist/utils/paths.js +160 -0
  149. package/dist/utils/paths.js.map +1 -0
  150. package/docs/PRD-BILLING.md +226 -0
  151. package/docs/PRD-EARN-SYSTEM.md +261 -0
  152. package/docs/PRD-MCP-AUTO-SETUP.md +623 -0
  153. package/docs/PRD-final-polish.md +117 -0
  154. package/docs/PRD-mobile-responsive.md +56 -0
  155. package/docs/PRD-navigation-expansion.md +295 -0
  156. package/docs/PRD-stripe-billing.md +312 -0
  157. package/docs/PRD-workspace-cleanup.md +200 -0
  158. package/docs/enterprise-deployment.md +728 -0
  159. package/landing/next.config.mjs +14 -0
  160. package/landing/public/stats.json +4 -2
  161. package/landing/scripts/generate-stats.js +12 -0
  162. package/landing/src/app/api/billing/checkout/route.ts +109 -0
  163. package/landing/src/app/api/billing/payment-method/route.ts +118 -0
  164. package/landing/src/app/api/billing/portal/route.ts +64 -0
  165. package/landing/src/app/api/workspace-auth/magic-link/route.ts +6 -3
  166. package/landing/src/app/auth/verify/page.tsx +31 -9
  167. package/landing/src/app/docs/page.tsx +1 -1
  168. package/landing/src/app/earn/page.tsx +6 -6
  169. package/landing/src/app/join/page.tsx +49 -0
  170. package/landing/src/app/login/page.tsx +8 -2
  171. package/landing/src/app/page.tsx +81 -96
  172. package/landing/src/app/providers/dashboard/page.tsx +1 -1
  173. package/landing/src/app/providers/register/page.tsx +1 -1
  174. package/landing/src/app/workspace/page.tsx +3269 -534
  175. package/landing/src/components/CheckoutButton.tsx +188 -0
  176. package/landing/src/components/EarnCreditsTab.tsx +842 -0
  177. package/landing/src/components/Toast.tsx +84 -0
  178. package/landing/src/lib/stats.json +3 -1
  179. package/package.json +9 -2
  180. package/src/adapters/base.ts +363 -0
  181. package/src/adapters/claude-desktop.ts +41 -0
  182. package/src/adapters/cline.ts +88 -0
  183. package/src/adapters/continue.ts +91 -0
  184. package/src/adapters/cursor.ts +43 -0
  185. package/src/adapters/custom.ts +188 -0
  186. package/src/adapters/detect.ts +202 -0
  187. package/src/adapters/index.ts +47 -0
  188. package/src/adapters/windsurf.ts +44 -0
  189. package/src/bin.ts +19 -0
  190. package/src/cli/commands/doctor.ts +367 -0
  191. package/src/cli/commands/index.ts +9 -0
  192. package/src/cli/commands/restore.ts +333 -0
  193. package/src/cli/commands/setup.ts +276 -0
  194. package/src/cli/commands/uninstall.ts +240 -0
  195. package/src/cli/index.ts +107 -0
  196. package/src/discovery.ts +328 -3
  197. package/src/enterprise/env.ts +156 -0
  198. package/src/enterprise/index.ts +7 -0
  199. package/src/enterprise/script-generator.ts +481 -0
  200. package/src/execute.ts +256 -0
  201. package/src/index.ts +290 -7
  202. package/src/metered.ts +149 -0
  203. package/src/stripe.ts +253 -0
  204. package/src/types.ts +32 -0
  205. package/src/ui/colors.ts +219 -0
  206. package/src/ui/errors.ts +394 -0
  207. package/src/ui/index.ts +17 -0
  208. package/src/ui/prompts.ts +390 -0
  209. package/src/ui/spinner.ts +325 -0
  210. package/src/utils/backup.ts +224 -0
  211. package/src/utils/config.ts +315 -0
  212. package/src/utils/os.ts +124 -0
  213. package/src/utils/paths.ts +203 -0
  214. package/landing/tsconfig.tsbuildinfo +0 -1
@@ -0,0 +1,367 @@
1
+ /**
2
+ * Doctor Command
3
+ * Health check for APIClaw installation and MCP client configurations
4
+ */
5
+
6
+ import { existsSync } from 'fs';
7
+ import { execSync } from 'child_process';
8
+ import { getAllClients, getClientConfig, MCPClient } from '../../utils/paths.js';
9
+ import { readConfig, hasApiclawConfig } from '../../utils/config.js';
10
+ import { detectOS, getOSDisplayName } from '../../utils/os.js';
11
+ import { getApiUrl, readEnvConfig, ENV_VARS } from '../../enterprise/env.js';
12
+
13
+ export interface DoctorResult {
14
+ healthy: boolean;
15
+ checks: CheckResult[];
16
+ summary: string;
17
+ }
18
+
19
+ export interface CheckResult {
20
+ category: string;
21
+ name: string;
22
+ status: 'pass' | 'fail' | 'warn' | 'skip';
23
+ message: string;
24
+ details?: string;
25
+ }
26
+
27
+ /**
28
+ * Check Node.js availability and version
29
+ */
30
+ function checkNode(): CheckResult {
31
+ try {
32
+ const version = execSync('node --version', { encoding: 'utf-8' }).trim();
33
+ const major = parseInt(version.replace('v', '').split('.')[0], 10);
34
+
35
+ if (major < 18) {
36
+ return {
37
+ category: 'System',
38
+ name: 'Node.js',
39
+ status: 'warn',
40
+ message: `${version} (recommend v18+)`,
41
+ };
42
+ }
43
+
44
+ return {
45
+ category: 'System',
46
+ name: 'Node.js',
47
+ status: 'pass',
48
+ message: version,
49
+ };
50
+ } catch {
51
+ return {
52
+ category: 'System',
53
+ name: 'Node.js',
54
+ status: 'fail',
55
+ message: 'Not found',
56
+ details: 'Node.js is required. Install from https://nodejs.org',
57
+ };
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Check npm availability
63
+ */
64
+ function checkNpm(): CheckResult {
65
+ try {
66
+ const version = execSync('npm --version', { encoding: 'utf-8' }).trim();
67
+ return {
68
+ category: 'System',
69
+ name: 'npm',
70
+ status: 'pass',
71
+ message: `v${version}`,
72
+ };
73
+ } catch {
74
+ return {
75
+ category: 'System',
76
+ name: 'npm',
77
+ status: 'fail',
78
+ message: 'Not found',
79
+ details: 'npm is required for npx to work',
80
+ };
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Check npx availability
86
+ */
87
+ function checkNpx(): CheckResult {
88
+ try {
89
+ execSync('npx --version', { encoding: 'utf-8', stdio: 'pipe' });
90
+ return {
91
+ category: 'System',
92
+ name: 'npx',
93
+ status: 'pass',
94
+ message: 'Available',
95
+ };
96
+ } catch {
97
+ return {
98
+ category: 'System',
99
+ name: 'npx',
100
+ status: 'fail',
101
+ message: 'Not found',
102
+ details: 'npx is required for MCP server execution',
103
+ };
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Check MCP client configuration
109
+ */
110
+ function checkClient(client: MCPClient, serverName = 'apiclaw'): CheckResult {
111
+ const config = getClientConfig(client);
112
+ const configPath = config.configPath;
113
+
114
+ // Check if config file exists
115
+ if (!existsSync(configPath)) {
116
+ // Check if config directory exists (client might be installed but not configured)
117
+ const dirExists = existsSync(config.configDir);
118
+
119
+ if (dirExists) {
120
+ return {
121
+ category: 'MCP Clients',
122
+ name: config.displayName,
123
+ status: 'warn',
124
+ message: 'Installed but not configured',
125
+ details: `Config file: ${configPath}`,
126
+ };
127
+ }
128
+
129
+ return {
130
+ category: 'MCP Clients',
131
+ name: config.displayName,
132
+ status: 'skip',
133
+ message: 'Not installed',
134
+ };
135
+ }
136
+
137
+ // Read and check config
138
+ const readResult = readConfig(configPath);
139
+
140
+ if (!readResult.success) {
141
+ return {
142
+ category: 'MCP Clients',
143
+ name: config.displayName,
144
+ status: 'fail',
145
+ message: 'Invalid config',
146
+ details: readResult.error,
147
+ };
148
+ }
149
+
150
+ // Check if APIClaw is configured
151
+ if (readResult.config && hasApiclawConfig(readResult.config, serverName)) {
152
+ return {
153
+ category: 'MCP Clients',
154
+ name: config.displayName,
155
+ status: 'pass',
156
+ message: 'Configured',
157
+ };
158
+ }
159
+
160
+ return {
161
+ category: 'MCP Clients',
162
+ name: config.displayName,
163
+ status: 'warn',
164
+ message: 'APIClaw not configured',
165
+ details: `Run: npx @nordsym/apiclaw setup --client ${client}`,
166
+ };
167
+ }
168
+
169
+ /**
170
+ * Check API connectivity
171
+ */
172
+ async function checkConnectivity(): Promise<CheckResult> {
173
+ const apiUrl = getApiUrl();
174
+ const testUrl = `${apiUrl}/health`;
175
+
176
+ try {
177
+ const controller = new AbortController();
178
+ const timeout = setTimeout(() => controller.abort(), 5000);
179
+
180
+ const response = await fetch(testUrl, {
181
+ method: 'GET',
182
+ signal: controller.signal,
183
+ });
184
+
185
+ clearTimeout(timeout);
186
+
187
+ if (response.ok) {
188
+ return {
189
+ category: 'Connectivity',
190
+ name: 'API Server',
191
+ status: 'pass',
192
+ message: `${apiUrl} reachable`,
193
+ };
194
+ }
195
+
196
+ return {
197
+ category: 'Connectivity',
198
+ name: 'API Server',
199
+ status: 'warn',
200
+ message: `${apiUrl} returned ${response.status}`,
201
+ };
202
+ } catch (error) {
203
+ // For now, skip connectivity check if fetch fails (might be offline mode)
204
+ return {
205
+ category: 'Connectivity',
206
+ name: 'API Server',
207
+ status: 'skip',
208
+ message: 'Could not reach API (offline?)',
209
+ details: `URL: ${testUrl}`,
210
+ };
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Check environment variables
216
+ */
217
+ function checkEnvVars(): CheckResult[] {
218
+ const envConfig = readEnvConfig();
219
+ const results: CheckResult[] = [];
220
+
221
+ if (envConfig.workspace) {
222
+ results.push({
223
+ category: 'Environment',
224
+ name: ENV_VARS.WORKSPACE,
225
+ status: 'pass',
226
+ message: envConfig.workspace,
227
+ });
228
+ }
229
+
230
+ if (envConfig.apiUrl) {
231
+ results.push({
232
+ category: 'Environment',
233
+ name: ENV_VARS.API_URL,
234
+ status: 'pass',
235
+ message: envConfig.apiUrl,
236
+ });
237
+ }
238
+
239
+ if (envConfig.disableTelemetry) {
240
+ results.push({
241
+ category: 'Environment',
242
+ name: ENV_VARS.DISABLE_TELEMETRY,
243
+ status: 'pass',
244
+ message: 'true',
245
+ });
246
+ }
247
+
248
+ return results;
249
+ }
250
+
251
+ /**
252
+ * Run all health checks
253
+ */
254
+ export async function runDoctor(options: { serverName?: string } = {}): Promise<DoctorResult> {
255
+ const checks: CheckResult[] = [];
256
+ const serverName = options.serverName || 'apiclaw';
257
+
258
+ // System checks
259
+ checks.push(checkNode());
260
+ checks.push(checkNpm());
261
+ checks.push(checkNpx());
262
+
263
+ // Client checks
264
+ for (const client of getAllClients()) {
265
+ checks.push(checkClient(client, serverName));
266
+ }
267
+
268
+ // Connectivity check
269
+ checks.push(await checkConnectivity());
270
+
271
+ // Environment checks
272
+ checks.push(...checkEnvVars());
273
+
274
+ // Calculate health status
275
+ const failures = checks.filter(c => c.status === 'fail');
276
+ const warnings = checks.filter(c => c.status === 'warn');
277
+ const passes = checks.filter(c => c.status === 'pass');
278
+
279
+ let healthy = failures.length === 0;
280
+ let summary: string;
281
+
282
+ if (failures.length > 0) {
283
+ summary = `${failures.length} issue(s) found`;
284
+ } else if (warnings.length > 0) {
285
+ summary = `All systems operational (${warnings.length} warning(s))`;
286
+ } else {
287
+ summary = 'All systems operational āœ“';
288
+ }
289
+
290
+ return { healthy, checks, summary };
291
+ }
292
+
293
+ /**
294
+ * Format doctor results for display
295
+ */
296
+ export function formatDoctorOutput(result: DoctorResult): string {
297
+ const lines: string[] = [];
298
+
299
+ lines.push('');
300
+ lines.push('šŸ” APIClaw Health Check');
301
+ lines.push('========================');
302
+ lines.push('');
303
+
304
+ // Group checks by category
305
+ const categories = new Map<string, CheckResult[]>();
306
+
307
+ for (const check of result.checks) {
308
+ const existing = categories.get(check.category) || [];
309
+ existing.push(check);
310
+ categories.set(check.category, existing);
311
+ }
312
+
313
+ // Format each category
314
+ for (const [category, checks] of categories) {
315
+ // Skip empty categories
316
+ if (checks.length === 0) continue;
317
+
318
+ lines.push(`${category}:`);
319
+
320
+ for (const check of checks) {
321
+ const icon = getStatusIcon(check.status);
322
+ lines.push(` ${icon} ${check.name} - ${check.message}`);
323
+
324
+ if (check.details && (check.status === 'fail' || check.status === 'warn')) {
325
+ lines.push(` ${check.details}`);
326
+ }
327
+ }
328
+
329
+ lines.push('');
330
+ }
331
+
332
+ // Summary
333
+ lines.push(`Status: ${result.summary}`);
334
+ lines.push('');
335
+
336
+ return lines.join('\n');
337
+ }
338
+
339
+ /**
340
+ * Get icon for status
341
+ */
342
+ function getStatusIcon(status: CheckResult['status']): string {
343
+ switch (status) {
344
+ case 'pass': return 'āœ“';
345
+ case 'fail': return 'āœ—';
346
+ case 'warn': return '⚠';
347
+ case 'skip': return 'ā—‹';
348
+ }
349
+ }
350
+
351
+ /**
352
+ * Doctor command handler
353
+ */
354
+ export async function doctorCommand(options: { serverName?: string; json?: boolean } = {}): Promise<void> {
355
+ const result = await runDoctor(options);
356
+
357
+ if (options.json) {
358
+ console.log(JSON.stringify(result, null, 2));
359
+ } else {
360
+ console.log(formatDoctorOutput(result));
361
+ }
362
+
363
+ // Exit with error code if unhealthy
364
+ if (!result.healthy) {
365
+ process.exit(1);
366
+ }
367
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * CLI Commands Index
3
+ * Exports all command handlers
4
+ */
5
+
6
+ export * from './setup.js';
7
+ export * from './doctor.js';
8
+ export * from './restore.js';
9
+ export * from './uninstall.js';
@@ -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
+ }