vigthoria-cli 1.10.47 → 1.10.49

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 (60) hide show
  1. package/dist/commands/agent-session-menu.js +2 -8
  2. package/dist/commands/auth.js +51 -68
  3. package/dist/commands/bridge.js +42 -22
  4. package/dist/commands/cancel.js +15 -22
  5. package/dist/commands/chat.d.ts +3 -0
  6. package/dist/commands/chat.js +326 -295
  7. package/dist/commands/config.js +33 -73
  8. package/dist/commands/deploy.js +83 -123
  9. package/dist/commands/device.js +21 -61
  10. package/dist/commands/edit.js +32 -39
  11. package/dist/commands/explain.js +18 -25
  12. package/dist/commands/fork.d.ts +17 -0
  13. package/dist/commands/fork.js +164 -0
  14. package/dist/commands/generate.js +37 -44
  15. package/dist/commands/history.d.ts +17 -0
  16. package/dist/commands/history.js +113 -0
  17. package/dist/commands/hub.js +95 -102
  18. package/dist/commands/index.js +41 -46
  19. package/dist/commands/legion.js +146 -186
  20. package/dist/commands/preview.d.ts +55 -0
  21. package/dist/commands/preview.js +467 -0
  22. package/dist/commands/replay.d.ts +18 -0
  23. package/dist/commands/replay.js +156 -0
  24. package/dist/commands/repo.d.ts +97 -0
  25. package/dist/commands/repo.js +773 -0
  26. package/dist/commands/review.js +29 -36
  27. package/dist/commands/security.js +5 -12
  28. package/dist/commands/update.d.ts +9 -0
  29. package/dist/commands/update.js +201 -0
  30. package/dist/commands/wallet.js +28 -35
  31. package/dist/commands/workflow.js +13 -20
  32. package/dist/index.d.ts +21 -0
  33. package/dist/index.js +1652 -0
  34. package/dist/utils/api.d.ts +544 -0
  35. package/dist/utils/api.js +5486 -0
  36. package/dist/utils/brain-hub-client.js +1 -5
  37. package/dist/utils/bridge-client.js +11 -52
  38. package/dist/utils/cli-state.d.ts +54 -0
  39. package/dist/utils/cli-state.js +185 -0
  40. package/dist/utils/codebase-indexer.js +4 -41
  41. package/dist/utils/config.d.ts +82 -0
  42. package/dist/utils/config.js +269 -0
  43. package/dist/utils/context-ranker.js +15 -21
  44. package/dist/utils/desktop-bridge-client.d.ts +12 -0
  45. package/dist/utils/desktop-bridge-client.js +30 -0
  46. package/dist/utils/files.js +5 -42
  47. package/dist/utils/logger.js +42 -50
  48. package/dist/utils/persona.js +3 -8
  49. package/dist/utils/post-write-validator.js +26 -33
  50. package/dist/utils/project-memory.js +16 -23
  51. package/dist/utils/session.d.ts +118 -0
  52. package/dist/utils/session.js +423 -0
  53. package/dist/utils/task-display.js +13 -20
  54. package/dist/utils/tools.d.ts +269 -0
  55. package/dist/utils/tools.js +3450 -0
  56. package/dist/utils/workspace-brain-service.js +8 -45
  57. package/dist/utils/workspace-cache.js +18 -26
  58. package/dist/utils/workspace-stream.js +21 -63
  59. package/package.json +2 -1
  60. package/scripts/release/validate-no-go-gates.sh +7 -4
package/dist/index.js ADDED
@@ -0,0 +1,1652 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Vigthoria CLI - AI-Powered Terminal Coding Assistant
4
+ *
5
+ * Usage:
6
+ * vigthoria chat - Start interactive chat
7
+ * vigthoria edit <file> - Edit a file with AI assistance
8
+ * vigthoria generate <desc> - Generate code from description
9
+ * vigthoria explain <file> - Explain code in a file
10
+ * vigthoria fix <file> - Fix issues in a file
11
+ * vigthoria review <file> - Review code quality
12
+ * vigthoria login - Authenticate with Vigthoria
13
+ * vigthoria config - Configure settings
14
+ * vigthoria hub - Discover & activate API modules
15
+ * vigthoria workflow - Manage repeatable VigFlow workflows
16
+ * vigthoria operator - Start BMAD operator mode
17
+ */
18
+ import { Command } from 'commander';
19
+ import { ChatCommand } from './commands/chat.js';
20
+ import { EditCommand } from './commands/edit.js';
21
+ import { GenerateCommand } from './commands/generate.js';
22
+ import { ExplainCommand } from './commands/explain.js';
23
+ import { handleLogin, handleLogout, statusAction } from './commands/auth.js';
24
+ import { ConfigCommand } from './commands/config.js';
25
+ import { ReviewCommand } from './commands/review.js';
26
+ import { HubCommand } from './commands/hub.js';
27
+ import { RepoCommand } from './commands/repo.js';
28
+ import { DeployCommand } from './commands/deploy.js';
29
+ import { BridgeCommand } from './commands/bridge.js';
30
+ import { DeviceCommand } from './commands/device.js';
31
+ import { WorkflowCommand } from './commands/workflow.js';
32
+ import { PreviewCommand } from './commands/preview.js';
33
+ import { LegionCommand } from './commands/legion.js';
34
+ import { HistoryCommand } from './commands/history.js';
35
+ import { ReplayCommand } from './commands/replay.js';
36
+ import { ForkCommand } from './commands/fork.js';
37
+ import { CancelCommand } from './commands/cancel.js';
38
+ import { SecurityCommand } from './commands/security.js';
39
+ import { Config } from './utils/config.js';
40
+ import { Logger, CH } from './utils/logger.js';
41
+ import chalk from 'chalk';
42
+ import * as fs from 'fs';
43
+ import * as path from 'path';
44
+ import * as os from 'os';
45
+ import { fileURLToPath } from 'url';
46
+ import { createHash } from 'crypto';
47
+ import axios from 'axios';
48
+ // ESM shim — TypeScript emits ESM under "type": "module", so __dirname
49
+ // is not available natively. Restore the CommonJS-style helper so the
50
+ // pre-existing package-discovery logic continues to work unchanged.
51
+ const __filename = fileURLToPath(import.meta.url);
52
+ const __dirname = path.dirname(__filename);
53
+ import { APIClient, sanitizeUserFacingErrorText } from './utils/api.js';
54
+ import { getCliStateFile, isOfflineMode, isSafeNpmPackageSpec, isUpdateCheckSuppressed, readCachedLatestVersion, readGatewayPreflightCache, writeCachedLatestVersion, writeGatewayPreflightCache, } from './utils/cli-state.js';
55
+ function isApiError(error) {
56
+ return Boolean(error &&
57
+ typeof error === 'object' &&
58
+ typeof error.status === 'number' &&
59
+ typeof error.message === 'string' &&
60
+ typeof error.code === 'string');
61
+ }
62
+ function getInvokedBinaryName() {
63
+ const executable = process.argv[1] || 'vigthoria';
64
+ return path.basename(executable, path.extname(executable)).toLowerCase();
65
+ }
66
+ // Get version from package.json dynamically
67
+ function getPackageMetadataPaths() {
68
+ return [
69
+ path.join(__dirname, '..', 'package.json'),
70
+ path.join(__dirname, '..', '..', 'package.json'),
71
+ path.join(process.cwd(), 'package.json'),
72
+ path.join(process.cwd(), 'node_modules', 'vigthoria-cli', 'package.json'),
73
+ // Also check global npm paths on Windows
74
+ path.join(process.env.APPDATA || '', 'npm', 'node_modules', 'vigthoria-cli', 'package.json'),
75
+ ];
76
+ }
77
+ function getVersion() {
78
+ try {
79
+ for (const p of getPackageMetadataPaths()) {
80
+ if (fs.existsSync(p)) {
81
+ const pkg = JSON.parse(fs.readFileSync(p, 'utf8'));
82
+ if (pkg.name === 'vigthoria-cli') {
83
+ return pkg.version;
84
+ }
85
+ }
86
+ }
87
+ }
88
+ catch (e) {
89
+ const message = e instanceof Error ? e.message : String(e);
90
+ if (process.env.VIGTHORIA_DEBUG_VERSION === '1') {
91
+ console.error(chalk.gray(`Unable to read package version: ${message}`));
92
+ }
93
+ }
94
+ return '0.0.0';
95
+ }
96
+ export function validateReleaseMetadata() {
97
+ try {
98
+ const packagePath = getPackageMetadataPaths().find((candidate) => fs.existsSync(candidate));
99
+ if (!packagePath) {
100
+ return false;
101
+ }
102
+ const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
103
+ const packageDir = path.dirname(packagePath);
104
+ const readmePath = path.join(packageDir, 'README.md');
105
+ if (!fs.existsSync(readmePath)) {
106
+ return false;
107
+ }
108
+ const readme = fs.readFileSync(readmePath, 'utf8');
109
+ const bins = pkg.bin && typeof pkg.bin === 'object' ? pkg.bin : {};
110
+ const requiredReadmePhrases = [
111
+ 'npm install -g vigthoria-cli',
112
+ 'curl -fsSL https://cli.vigthoria.io/install.sh | bash',
113
+ 'irm https://cli.vigthoria.io/install.ps1 | iex',
114
+ 'vigthoria login',
115
+ 'vigthoria chat',
116
+ 'vig c',
117
+ 'vigthoria edit',
118
+ 'vigthoria update',
119
+ 'vigthoria doctor',
120
+ ];
121
+ return (pkg.name === 'vigthoria-cli' &&
122
+ typeof pkg.version === 'string' &&
123
+ pkg.description === 'Vigthoria Coder CLI - AI-powered terminal coding assistant' &&
124
+ pkg.main === 'dist/index.js' &&
125
+ bins.vigthoria === 'dist/index.js' &&
126
+ bins.vig === 'dist/index.js' &&
127
+ bins['vigthoria-chat'] === 'dist/index.js' &&
128
+ requiredReadmePhrases.every((phrase) => readme.includes(phrase)));
129
+ }
130
+ catch (error) {
131
+ const message = error instanceof Error ? error.message : String(error);
132
+ if (process.env.VIGTHORIA_DEBUG_VERSION === '1') {
133
+ console.error(chalk.gray(`Release metadata validation failed: ${message}`));
134
+ }
135
+ return false;
136
+ }
137
+ }
138
+ function compareVersions(v1, v2) {
139
+ const parts1 = v1.split('.').map(Number);
140
+ const parts2 = v2.split('.').map(Number);
141
+ for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
142
+ const p1 = parts1[i] || 0;
143
+ const p2 = parts2[i] || 0;
144
+ if (p1 < p2)
145
+ return -1;
146
+ if (p1 > p2)
147
+ return 1;
148
+ }
149
+ return 0;
150
+ }
151
+ const VERSION = getVersion();
152
+ const VIGTHORIA_DEFAULT_MANIFEST_URL = process.env.VIGTHORIA_UPDATE_MANIFEST_URL || "https://coder.vigthoria.io/releases/manifest.json";
153
+ function resolveManifestEntry(manifest, channel) {
154
+ const normalized = String(channel || 'stable').trim() || 'stable';
155
+ if (manifest.channels && manifest.channels[normalized]) {
156
+ return manifest.channels[normalized];
157
+ }
158
+ const direct = manifest[normalized];
159
+ if (direct && typeof direct === 'object') {
160
+ return direct;
161
+ }
162
+ if (normalized === 'stable') {
163
+ const rootVersion = typeof manifest.version === 'string' ? manifest.version : '';
164
+ const rootUrl = typeof manifest.url === 'string' ? manifest.url : '';
165
+ const rootSha = typeof manifest.sha256 === 'string' ? manifest.sha256 : undefined;
166
+ if (rootVersion && rootUrl) {
167
+ return { version: rootVersion, url: rootUrl, sha256: rootSha };
168
+ }
169
+ }
170
+ return null;
171
+ }
172
+ async function downloadFile(url, targetPath) {
173
+ const response = await axios.get(url, {
174
+ responseType: 'arraybuffer',
175
+ timeout: 20000,
176
+ maxRedirects: 5,
177
+ validateStatus: (status) => status >= 200 && status < 300,
178
+ });
179
+ fs.writeFileSync(targetPath, Buffer.from(response.data));
180
+ }
181
+ function sha256File(filePath) {
182
+ const hash = createHash('sha256');
183
+ const data = fs.readFileSync(filePath);
184
+ hash.update(data);
185
+ return hash.digest('hex');
186
+ }
187
+ async function installGlobalPackageWithNpm(packageSpec) {
188
+ const { execFileSync, execSync } = await import('child_process');
189
+ const attempts = [];
190
+ if (process.platform === 'win32') {
191
+ // First-choice: cmd.exe /c npm — resolves npm.cmd shims on any Windows Node setup
192
+ const comspec = process.env.COMSPEC || 'cmd.exe';
193
+ attempts.push({
194
+ label: 'cmd.exe /c npm',
195
+ command: comspec,
196
+ args: ['/c', 'npm', 'install', '-g', packageSpec],
197
+ });
198
+ const npmExecPath = process.env.npm_execpath;
199
+ if (npmExecPath && fs.existsSync(npmExecPath)) {
200
+ attempts.push({
201
+ label: 'node+npm_execpath',
202
+ command: process.execPath,
203
+ args: [npmExecPath, 'install', '-g', packageSpec],
204
+ });
205
+ }
206
+ try {
207
+ const resolvedNpmCmd = execSync('where npm.cmd', {
208
+ encoding: 'utf8',
209
+ stdio: ['pipe', 'pipe', 'pipe'],
210
+ windowsHide: true,
211
+ }).split(/\r?\n/).map((line) => line.trim()).find(Boolean);
212
+ if (resolvedNpmCmd) {
213
+ attempts.push({
214
+ label: 'where npm.cmd',
215
+ command: resolvedNpmCmd,
216
+ args: ['install', '-g', packageSpec],
217
+ });
218
+ }
219
+ }
220
+ catch {
221
+ // Fall through to generic attempts below.
222
+ }
223
+ attempts.push({
224
+ label: 'npm.cmd',
225
+ command: 'npm.cmd',
226
+ args: ['install', '-g', packageSpec],
227
+ });
228
+ // Last-resort Windows shell fallback for PATH/cmd shim edge-cases.
229
+ attempts.push({
230
+ label: 'npm via shell',
231
+ command: 'npm',
232
+ args: ['install', '-g', packageSpec],
233
+ shell: true,
234
+ });
235
+ }
236
+ else {
237
+ let resolvedNpm = '';
238
+ try {
239
+ resolvedNpm = execSync('which npm', {
240
+ encoding: 'utf8',
241
+ stdio: ['pipe', 'pipe', 'pipe'],
242
+ }).trim();
243
+ }
244
+ catch {
245
+ // Keep resolvedNpm empty and use plain `npm` below.
246
+ }
247
+ attempts.push({
248
+ label: resolvedNpm ? 'which npm' : 'npm',
249
+ command: resolvedNpm || 'npm',
250
+ args: ['install', '-g', packageSpec],
251
+ });
252
+ }
253
+ let lastError = null;
254
+ for (const attempt of attempts) {
255
+ try {
256
+ console.log(chalk.gray(`[update] installer executable: ${attempt.command}${attempt.shell ? ' (shell)' : ''}`));
257
+ execFileSync(attempt.command, attempt.args, {
258
+ stdio: 'inherit',
259
+ windowsHide: true,
260
+ shell: attempt.shell === true,
261
+ });
262
+ return;
263
+ }
264
+ catch (error) {
265
+ lastError = error;
266
+ const code = error?.code || error?.status || 'unknown';
267
+ console.error(chalk.yellow(`[update] install attempt failed via ${attempt.label}: ${code}`));
268
+ }
269
+ }
270
+ const finalCode = lastError?.code || lastError?.status || 'unknown';
271
+ throw new Error(`Unable to launch npm installer after ${attempts.length} attempt(s). Last error: ${finalCode}`);
272
+ }
273
+ function renderUpdateBanner(latestVersion) {
274
+ if (compareVersions(latestVersion, '1.4.0') >= 0 && compareVersions(VERSION, '1.4.0') < 0) {
275
+ console.log(chalk.red.bold(`\n${CH.warnEmoji} SECURITY UPDATE AVAILABLE`));
276
+ console.log(chalk.red(` Version ${VERSION} has security vulnerabilities.`));
277
+ console.log(chalk.yellow(` Please update to ${latestVersion} immediately:`));
278
+ console.log(chalk.white.bold(' npm install -g vigthoria-cli@latest\n'));
279
+ }
280
+ else {
281
+ console.log(chalk.yellow(`\n${CH.warnEmoji} Update available: ${VERSION} -> ${latestVersion}`));
282
+ console.log(chalk.gray(' Run `vigthoria update` to install\n'));
283
+ }
284
+ }
285
+ // Check for updates quietly on startup. Cached (24 h) so we never block
286
+ // startup on `npm view`. Honours VIGTHORIA_OFFLINE / VIGTHORIA_NO_UPDATE_CHECK.
287
+ async function checkForUpdatesQuietly() {
288
+ if (isUpdateCheckSuppressed()) {
289
+ return;
290
+ }
291
+ const cached = readCachedLatestVersion();
292
+ if (cached) {
293
+ if (compareVersions(cached, VERSION) > 0) {
294
+ renderUpdateBanner(cached);
295
+ }
296
+ return;
297
+ }
298
+ // Cache miss: probe npm registry in the background, with a hard 5 s
299
+ // timeout. Result is cached for 24 h so subsequent runs are instant.
300
+ try {
301
+ const { execSync } = await import('child_process');
302
+ const npmVersion = execSync('npm view vigthoria-cli version', {
303
+ encoding: 'utf8',
304
+ timeout: 5000,
305
+ stdio: ['pipe', 'pipe', 'pipe'],
306
+ windowsHide: true,
307
+ }).trim();
308
+ if (!npmVersion) {
309
+ return;
310
+ }
311
+ writeCachedLatestVersion(npmVersion);
312
+ if (compareVersions(npmVersion, VERSION) > 0) {
313
+ renderUpdateBanner(npmVersion);
314
+ }
315
+ }
316
+ catch {
317
+ // Network or npm failure should never block CLI; do not cache failure.
318
+ }
319
+ }
320
+ const VIGTHORIA_GATEWAY_AUTH_FAILURE_MESSAGE = 'Vigthoria Gate way user authentification failed. Please log out and login again.';
321
+ function resolveRequestedCommand(argv) {
322
+ for (let i = 2; i < argv.length; i++) {
323
+ const token = String(argv[i] || '').trim();
324
+ if (!token || token.startsWith('-'))
325
+ continue;
326
+ return token;
327
+ }
328
+ return '';
329
+ }
330
+ function isAuthProtectedCommand(command) {
331
+ const protectedCommands = new Set([
332
+ 'chat', 'c', 'chat-resume',
333
+ 'agent', 'a', 'operator', 'op',
334
+ 'edit', 'e', 'generate', 'g', 'explain', 'x', 'fix', 'f', 'review', 'r',
335
+ 'workflow', 'flow', 'hub', 'marketplace', 'deploy', 'host', 'preview', 'device',
336
+ 'legion', 'history', 'runs', 'replay', 'fork', 'cancel',
337
+ 'repo', 'repository',
338
+ ]);
339
+ return protectedCommands.has(command);
340
+ }
341
+ async function enforceGatewayAuthSession(config, logger, jsonOutputRequested) {
342
+ if (!config.isAuthenticated()) {
343
+ return true;
344
+ }
345
+ const explicitEnvToken = Boolean(process.env.VIGTHORIA_TOKEN || process.env.VIGTHORIA_AUTH_TOKEN);
346
+ // Offline mode disables every outbound preflight call.
347
+ if (isOfflineMode()) {
348
+ return true;
349
+ }
350
+ // Short-lived cache so back-to-back CLI invocations don't re-probe. Never use
351
+ // a cached success when an explicit env token overrides the stored session.
352
+ if (!explicitEnvToken) {
353
+ const cached = readGatewayPreflightCache();
354
+ if (cached === true) {
355
+ return true;
356
+ }
357
+ }
358
+ const api = new APIClient(config, logger);
359
+ try {
360
+ const tokenCheck = await api.validateToken();
361
+ if (!tokenCheck.valid) {
362
+ writeGatewayPreflightCache(false);
363
+ if (jsonOutputRequested) {
364
+ process.exitCode = 1;
365
+ console.log(JSON.stringify({ success: false, error: VIGTHORIA_GATEWAY_AUTH_FAILURE_MESSAGE }, null, 2));
366
+ }
367
+ else {
368
+ console.log(chalk.red(VIGTHORIA_GATEWAY_AUTH_FAILURE_MESSAGE));
369
+ }
370
+ return false;
371
+ }
372
+ writeGatewayPreflightCache(true);
373
+ return true;
374
+ }
375
+ catch {
376
+ if (explicitEnvToken) {
377
+ writeGatewayPreflightCache(false);
378
+ if (jsonOutputRequested) {
379
+ process.exitCode = 1;
380
+ console.log(JSON.stringify({ success: false, error: VIGTHORIA_GATEWAY_AUTH_FAILURE_MESSAGE }, null, 2));
381
+ }
382
+ else {
383
+ console.log(chalk.red(VIGTHORIA_GATEWAY_AUTH_FAILURE_MESSAGE));
384
+ }
385
+ return false;
386
+ }
387
+ // Network or transient backend outages should not be misclassified as auth failure.
388
+ return true;
389
+ }
390
+ finally {
391
+ api.destroy();
392
+ }
393
+ }
394
+ function scrubMessageForUser(message) {
395
+ try {
396
+ return sanitizeUserFacingErrorText(message) || message;
397
+ }
398
+ catch {
399
+ return message;
400
+ }
401
+ }
402
+ function normalizeCliError(error) {
403
+ if (error instanceof Error) {
404
+ const extended = error;
405
+ const responseMessage = typeof extended.response?.data?.error === 'string'
406
+ ? extended.response.data.error
407
+ : typeof extended.response?.data?.message === 'string'
408
+ ? extended.response.data.message
409
+ : undefined;
410
+ const rawMessage = responseMessage || extended.message || 'An unexpected CLI error occurred.';
411
+ return {
412
+ message: scrubMessageForUser(rawMessage),
413
+ code: extended.code,
414
+ status: extended.status || extended.statusCode || extended.response?.status,
415
+ details: extended.details || extended.response?.data,
416
+ };
417
+ }
418
+ if (error && typeof error === 'object') {
419
+ const value = error;
420
+ const rawMessage = typeof value.message === 'string' && value.message.trim() ? value.message : 'An unexpected CLI error occurred.';
421
+ return {
422
+ message: scrubMessageForUser(rawMessage),
423
+ code: typeof value.code === 'string' || typeof value.code === 'number' ? value.code : undefined,
424
+ status: typeof value.status === 'number' ? value.status : undefined,
425
+ details: value.details,
426
+ };
427
+ }
428
+ const rawMessage = typeof error === 'string' && error.trim() ? error : 'An unexpected CLI error occurred.';
429
+ return {
430
+ message: scrubMessageForUser(rawMessage),
431
+ };
432
+ }
433
+ function formatCliError(error, jsonOutputRequested = false) {
434
+ const normalized = normalizeCliError(error);
435
+ if (jsonOutputRequested) {
436
+ return JSON.stringify({ success: false, error: normalized.message, code: normalized.code, status: normalized.status, details: normalized.details }, null, 2);
437
+ }
438
+ const parts = [chalk.red('Error:'), normalized.message];
439
+ if (normalized.code)
440
+ parts.push(chalk.gray(`[${normalized.code}]`));
441
+ if (normalized.status)
442
+ parts.push(chalk.gray(`(HTTP ${normalized.status})`));
443
+ return parts.join(' ');
444
+ }
445
+ function reportCliError(error, jsonOutputRequested = false) {
446
+ const output = formatCliError(error, jsonOutputRequested);
447
+ if (jsonOutputRequested) {
448
+ console.log(output);
449
+ }
450
+ else {
451
+ console.error(output);
452
+ }
453
+ }
454
+ function handleFatalCliError(error, jsonOutputRequested = false) {
455
+ reportCliError(error, jsonOutputRequested);
456
+ process.exitCode = 1;
457
+ }
458
+ let errorHandlersInstalled = false;
459
+ export function setupErrorHandlers() {
460
+ if (errorHandlersInstalled) {
461
+ return;
462
+ }
463
+ errorHandlersInstalled = true;
464
+ process.on('unhandledRejection', (reason) => {
465
+ const rejection = reason instanceof Error ? reason : new Error(String(reason || 'Unhandled promise rejection'));
466
+ handleFatalCliError(rejection, process.argv.includes('--json'));
467
+ process.exit(1);
468
+ });
469
+ process.on('uncaughtException', (error) => {
470
+ handleFatalCliError(error, process.argv.includes('--json'));
471
+ });
472
+ process.on('warning', (warning) => {
473
+ if (process.env.VIGTHORIA_DEBUG === '1') {
474
+ reportCliError(warning, process.argv.includes('--json'));
475
+ }
476
+ });
477
+ }
478
+ export async function main(args) {
479
+ const program = new Command();
480
+ const config = new Config();
481
+ const logger = new Logger();
482
+ const invokedBinaryName = getInvokedBinaryName();
483
+ const argv = [...args];
484
+ const firstArg = argv[2];
485
+ const jsonOutputRequested = argv.includes('--json');
486
+ const directPromptRequested = argv.includes('--prompt') || argv.includes('-P');
487
+ const helpOrVersionRequested = argv.some((token) => token === '--help' || token === '-h' || token === '--version' || token === '-V' ||
488
+ token === 'help' || token === 'version');
489
+ const isLegionCortexRequest = invokedBinaryName === 'vigthoria' && argv[2] === 'legion' && argv.includes('--cortex');
490
+ if (invokedBinaryName === 'vigthoria-chat') {
491
+ const knownCommands = new Set([
492
+ 'chat', 'chat-resume', 'agent', 'edit', 'generate', 'explain', 'fix', 'review', 'cancel',
493
+ 'hub', 'repo', 'deploy', 'operator', 'workflow', 'flow', 'device', 'security', 'vsec', 'login', 'logout', 'status', 'config', 'update',
494
+ '--help', '-h', '--version', '-V', 'help', 'version',
495
+ ]);
496
+ if (!firstArg || firstArg.startsWith('-') || !knownCommands.has(firstArg)) {
497
+ argv.splice(2, 0, 'chat');
498
+ }
499
+ }
500
+ const requestedCommand = resolveRequestedCommand(argv);
501
+ // Skip gateway JWT auth when running on-server with a service key (e.g., Cortex on-box).
502
+ // The service key is checked by Hyper Loop directly — no user session needed.
503
+ const hasServiceKey = !!(process.env.HYPERLOOP_SERVICE_KEY ||
504
+ process.env.V3_SERVICE_KEY);
505
+ if (!hasServiceKey && !isLegionCortexRequest && requestedCommand && isAuthProtectedCommand(requestedCommand)) {
506
+ const authOk = await enforceGatewayAuthSession(config, logger, jsonOutputRequested);
507
+ if (!authOk) {
508
+ process.exitCode = 1;
509
+ return;
510
+ }
511
+ }
512
+ // Banner - Fixed alignment with proper padding
513
+ const boxWidth = 61; // Inner content width
514
+ const titleText = 'VIGTHORIA CLI - AI-Powered Coding Assistant';
515
+ const versionText = `Version ${VERSION}`;
516
+ // Calculate padding for centering
517
+ const titlePad = Math.floor((boxWidth - 4 - titleText.length) / 2);
518
+ const versionPad = Math.floor((boxWidth - 4 - versionText.length) / 2);
519
+ if (process.env.VIGTHORIA_NO_BANNER !== '1' &&
520
+ !jsonOutputRequested &&
521
+ !directPromptRequested &&
522
+ !helpOrVersionRequested) {
523
+ console.log(chalk.cyan(CH.dTl + CH.dH.repeat(boxWidth) + CH.dTr));
524
+ console.log(chalk.cyan(CH.dV + ' '.repeat(boxWidth) + CH.dV));
525
+ console.log(chalk.cyan(CH.dV) + ' '.repeat(titlePad) + chalk.bold.white('VIGTHORIA CLI') + chalk.cyan(' - AI-Powered Coding Assistant') + ' '.repeat(boxWidth - titlePad - titleText.length) + chalk.cyan(CH.dV));
526
+ console.log(chalk.cyan(CH.dV) + ' '.repeat(versionPad) + chalk.gray(versionText) + ' '.repeat(boxWidth - versionPad - versionText.length) + chalk.cyan(CH.dV));
527
+ console.log(chalk.cyan(CH.dV + ' '.repeat(boxWidth) + CH.dV));
528
+ console.log(chalk.cyan(CH.dBl + CH.dH.repeat(boxWidth) + CH.dBr));
529
+ console.log();
530
+ }
531
+ // Check for updates in background (don't wait). Skip for JSON / direct
532
+ // prompt / help / version invocations to keep them deterministic.
533
+ if (!isUpdateCheckSuppressed() &&
534
+ !jsonOutputRequested &&
535
+ !directPromptRequested &&
536
+ !helpOrVersionRequested) {
537
+ void checkForUpdatesQuietly();
538
+ }
539
+ program
540
+ .name(invokedBinaryName === 'vigthoria-chat' ? 'vigthoria-chat' : 'vigthoria')
541
+ .description('AI-powered terminal coding assistant for Vigthoria Coder subscribers')
542
+ .version(VERSION);
543
+ // Chat command - Interactive mode (Agent mode is default for best results)
544
+ program
545
+ .command('chat')
546
+ .alias('c')
547
+ .description('Start interactive chat with Vigthoria AI')
548
+ .option('-m, --model <model>', 'Select AI model (agent, code, code-35b, code-9b, balanced, balanced-4b, cloud, ultra)')
549
+ .option('-p, --project <path>', 'Set project context path')
550
+ .option('--new-project [name]', 'Create or use a managed local workspace folder when no --project path is given')
551
+ .option('-a, --agent', 'Enable agentic mode (default: true for best quality)', true)
552
+ .option('--no-agent', 'Disable agentic mode (simple chat only)')
553
+ .option('-r, --resume', 'Resume last session for this project', false)
554
+ .option('--prompt <text>', 'Run a single prompt directly and exit')
555
+ .option('-w, --workflow <selector>', 'Route prompts through a named or explicit VigFlow workflow target')
556
+ .option('--json', 'Emit machine-readable JSON output for direct prompt runs', false)
557
+ .option('--auto-approve', 'Auto-approve agent actions (dangerous!)', false)
558
+ .option('--bridge <url>', 'Connect to Vigthoria Commando Bridge for remote admin observability')
559
+ .action(async (options) => {
560
+ const chat = new ChatCommand(config, logger);
561
+ await chat.run({
562
+ model: options.model,
563
+ project: options.project,
564
+ projectProvided: typeof options.project === 'string' && options.project.trim().length > 0,
565
+ newProject: options.newProject,
566
+ agent: options.agent,
567
+ workflow: options.workflow,
568
+ json: options.json,
569
+ autoApprove: options.autoApprove,
570
+ resume: options.resume,
571
+ prompt: options.prompt,
572
+ bridge: options.bridge,
573
+ });
574
+ });
575
+ program
576
+ .command('chat-resume')
577
+ .description('Resume the latest chat session for the current or specified project')
578
+ .option('-m, --model <model>', 'Select AI model (agent, code, code-35b, code-9b, balanced, balanced-4b, cloud, ultra)')
579
+ .option('-p, --project <path>', 'Set project context path')
580
+ .option('--new-project [name]', 'Create or use a managed local workspace folder when no --project path is given')
581
+ .option('-a, --agent', 'Enable agentic mode (default: true for best quality)', true)
582
+ .option('--no-agent', 'Disable agentic mode (simple chat only)')
583
+ .option('--prompt <text>', 'Run a single prompt directly and exit after resuming context')
584
+ .option('-w, --workflow <selector>', 'Route prompts through a named or explicit VigFlow workflow target')
585
+ .option('--json', 'Emit machine-readable JSON output for direct prompt runs', false)
586
+ .option('--auto-approve', 'Auto-approve agent actions (dangerous!)', false)
587
+ .option('--bridge <url>', 'Connect to Vigthoria Commando Bridge for remote admin observability')
588
+ .action(async (options) => {
589
+ const chat = new ChatCommand(config, logger);
590
+ await chat.run({
591
+ model: options.model,
592
+ project: options.project,
593
+ projectProvided: typeof options.project === 'string' && options.project.trim().length > 0,
594
+ newProject: options.newProject,
595
+ agent: options.agent,
596
+ workflow: options.workflow,
597
+ json: options.json,
598
+ autoApprove: options.autoApprove,
599
+ resume: true,
600
+ prompt: options.prompt,
601
+ bridge: options.bridge,
602
+ });
603
+ });
604
+ // Agent command - Agentic mode (Vigthoria Autonomous)
605
+ // Uses Vigthoria v3 Code 35B or Vigthoria Cloud for complex tasks
606
+ program
607
+ .command('agent')
608
+ .alias('a')
609
+ .description('Start agentic mode - AI can read/write files, run commands')
610
+ .option('-m, --model <model>', 'Select AI model (agent, code, cloud, ultra)')
611
+ .option('-p, --project <path>', 'Set project context path')
612
+ .option('--new-project [name]', 'Create or use a managed local workspace folder when no --project path is given')
613
+ .option('--prompt <text>', 'Run a single agent prompt directly and exit')
614
+ .option('-w, --workflow <selector>', 'Run the prompt through a named or explicit VigFlow workflow target')
615
+ .option('--json', 'Emit machine-readable JSON output for direct prompt runs', false)
616
+ .option('--auto-approve', 'Auto-approve all actions (dangerous!)', false)
617
+ .option('--bridge <url>', 'Connect to Vigthoria Commando Bridge for remote admin observability')
618
+ .action(async (options) => {
619
+ const chat = new ChatCommand(config, logger);
620
+ await chat.run({
621
+ model: options.model,
622
+ project: options.project,
623
+ projectProvided: typeof options.project === 'string' && options.project.trim().length > 0,
624
+ newProject: options.newProject,
625
+ agent: true,
626
+ operator: false,
627
+ workflow: options.workflow,
628
+ json: options.json,
629
+ autoApprove: options.autoApprove,
630
+ prompt: options.prompt,
631
+ bridge: options.bridge,
632
+ });
633
+ });
634
+ program
635
+ .command('operator')
636
+ .alias('op')
637
+ .description('Start BMAD operator mode for infrastructure and system workflows')
638
+ .option('-m, --model <model>', 'Select operator model (code, agent, cloud)')
639
+ .option('-p, --project <path>', 'Set project context path')
640
+ .option('--new-project [name]', 'Create or use a managed local workspace folder when no --project path is given')
641
+ .option('--prompt <text>', 'Run a single operator prompt directly and exit')
642
+ .option('-w, --workflow <selector>', 'Run the prompt through a named or explicit VigFlow workflow target')
643
+ .option('--save-plan', 'Save the completed BMAD plan into VigFlow for rerun and audit', false)
644
+ .option('--json', 'Emit machine-readable JSON output for direct prompt runs', false)
645
+ .option('--bridge <url>', 'Connect to Vigthoria Commando Bridge for remote admin observability')
646
+ .action(async (options) => {
647
+ const chat = new ChatCommand(config, logger);
648
+ await chat.run({
649
+ model: options.model,
650
+ project: options.project,
651
+ projectProvided: typeof options.project === 'string' && options.project.trim().length > 0,
652
+ newProject: options.newProject,
653
+ agent: false,
654
+ operator: true,
655
+ workflow: options.workflow,
656
+ savePlan: options.savePlan,
657
+ json: options.json,
658
+ prompt: options.prompt,
659
+ bridge: options.bridge,
660
+ });
661
+ });
662
+ program
663
+ .command('bridge')
664
+ .description('Inspect local DevTools Bridge availability for browser debugging tasks')
665
+ .command('status')
666
+ .description('Show live DevTools Bridge status')
667
+ .action(async () => {
668
+ const bridge = new BridgeCommand(config, logger);
669
+ await bridge.status();
670
+ });
671
+ const deviceCommand = program
672
+ .command('device')
673
+ .alias('adb')
674
+ .description('Android Developer Bridge commands via local ADB');
675
+ deviceCommand
676
+ .command('status')
677
+ .description('Show ADB availability and connected Android devices')
678
+ .option('--json', 'Emit JSON output', false)
679
+ .action(async (options) => {
680
+ const device = new DeviceCommand(config, logger);
681
+ await device.status(options);
682
+ });
683
+ deviceCommand
684
+ .command('list')
685
+ .description('List connected Android devices')
686
+ .option('--json', 'Emit JSON output', false)
687
+ .action(async (options) => {
688
+ const device = new DeviceCommand(config, logger);
689
+ await device.list(options);
690
+ });
691
+ deviceCommand
692
+ .command('screenshot')
693
+ .description('Capture a screenshot from an Android device')
694
+ .option('-d, --device <id>', 'ADB device id')
695
+ .option('-o, --output <path>', 'Output PNG path')
696
+ .action(async (options) => {
697
+ const device = new DeviceCommand(config, logger);
698
+ await device.screenshot(options);
699
+ });
700
+ deviceCommand
701
+ .command('install <apk>')
702
+ .description('Install or update an APK on an Android device')
703
+ .option('-d, --device <id>', 'ADB device id')
704
+ .option('--json', 'Emit JSON output', false)
705
+ .action(async (apk, options) => {
706
+ const device = new DeviceCommand(config, logger);
707
+ await device.install(apk, options);
708
+ });
709
+ deviceCommand
710
+ .command('launch <package>')
711
+ .description('Launch an Android app by package name')
712
+ .option('-d, --device <id>', 'ADB device id')
713
+ .option('-a, --activity <activity>', 'Activity class, for example .MainActivity')
714
+ .option('--json', 'Emit JSON output', false)
715
+ .action(async (packageName, options) => {
716
+ const device = new DeviceCommand(config, logger);
717
+ await device.launch(packageName, options);
718
+ });
719
+ deviceCommand
720
+ .command('logs')
721
+ .description('Read Android logcat output')
722
+ .option('-d, --device <id>', 'ADB device id')
723
+ .option('-n, --lines <count>', 'Number of log lines for snapshot mode', '250')
724
+ .option('-f, --follow', 'Follow logcat until interrupted', false)
725
+ .option('--json', 'Emit JSON output for snapshot mode', false)
726
+ .action(async (options) => {
727
+ const device = new DeviceCommand(config, logger);
728
+ await device.logs(options);
729
+ });
730
+ deviceCommand
731
+ .command('tcpip')
732
+ .description('Enable wireless ADB tcpip mode on the selected USB-connected device')
733
+ .option('-d, --device <id>', 'ADB device id')
734
+ .option('-p, --port <port>', 'Wireless ADB port', '5555')
735
+ .option('--json', 'Emit JSON output', false)
736
+ .action(async (options) => {
737
+ const device = new DeviceCommand(config, logger);
738
+ await device.tcpip(options);
739
+ });
740
+ deviceCommand
741
+ .command('connect <address>')
742
+ .description('Connect to a wireless ADB device, for example 192.168.1.30:5555')
743
+ .option('--json', 'Emit JSON output', false)
744
+ .action(async (address, options) => {
745
+ const device = new DeviceCommand(config, logger);
746
+ await device.connect(address, options);
747
+ });
748
+ deviceCommand
749
+ .command('disconnect [address]')
750
+ .description('Disconnect a wireless ADB device')
751
+ .option('--json', 'Emit JSON output', false)
752
+ .action(async (address, options) => {
753
+ const device = new DeviceCommand(config, logger);
754
+ await device.disconnect(address, options);
755
+ });
756
+ deviceCommand.action(async () => {
757
+ const device = new DeviceCommand(config, logger);
758
+ await device.status({});
759
+ });
760
+ // Security command group - Security DevOps scans and fix plans
761
+ const securityCommand = program
762
+ .command('security')
763
+ .alias('vsec')
764
+ .description('Run security scans, scores, and fix plans via Vigthoria MCP security tools');
765
+ securityCommand
766
+ .command('scan')
767
+ .description('Scan project for security issues')
768
+ .option('-d, --dir <path>', 'Directory to scan (default: current directory)')
769
+ .option('--json', 'Emit JSON output', false)
770
+ .action(async (options) => {
771
+ const security = new SecurityCommand(config, logger);
772
+ await security.scan({ dir: options.dir, json: options.json });
773
+ });
774
+ securityCommand
775
+ .command('score')
776
+ .description('Calculate project security score')
777
+ .option('-d, --dir <path>', 'Directory to score (default: current directory)')
778
+ .option('--json', 'Emit JSON output', false)
779
+ .action(async (options) => {
780
+ const security = new SecurityCommand(config, logger);
781
+ await security.score({ dir: options.dir, json: options.json });
782
+ });
783
+ securityCommand
784
+ .command('fix')
785
+ .description('Generate security fix plan')
786
+ .option('-d, --dir <path>', 'Directory to inspect (default: current directory)')
787
+ .option('-i, --issue-id <id>', 'Single issue id to target')
788
+ .option('--apply', 'Request auto-apply mode (safe mode still requires explicit patching)', false)
789
+ .option('--json', 'Emit JSON output', false)
790
+ .action(async (options) => {
791
+ const security = new SecurityCommand(config, logger);
792
+ await security.fix({ dir: options.dir, issueId: options.issueId, apply: options.apply, json: options.json });
793
+ });
794
+ // Edit command - Edit files with AI
795
+ program
796
+ .command('edit <file>')
797
+ .alias('e')
798
+ .description('Edit a file with AI assistance')
799
+ .option('-i, --instruction <text>', 'Editing instruction')
800
+ .option('-m, --model <model>', 'Select AI model', 'code')
801
+ .option('--apply', 'Automatically apply changes without confirmation', false)
802
+ .action(async (file, options) => {
803
+ const edit = new EditCommand(config, logger);
804
+ await edit.run(file, options);
805
+ });
806
+ // Generate command - Generate code
807
+ program
808
+ .command('generate <description>')
809
+ .alias('g')
810
+ .description('Generate code from description')
811
+ .option('-l, --language <lang>', 'Target language (auto-detected from description, or specify: javascript, typescript, python, html, etc.)')
812
+ .option('-o, --output <file>', 'Output file path')
813
+ .option('-m, --model <model>', 'Select AI model', 'code')
814
+ .option('-p, --pro', 'Senior Developer Mode: plan, generate, quality check (recommended)', false)
815
+ .action(async (description, options) => {
816
+ const generate = new GenerateCommand(config, logger);
817
+ await generate.run(description, options);
818
+ });
819
+ // Explain command - Explain code
820
+ program
821
+ .command('explain <file>')
822
+ .alias('x')
823
+ .description('Explain code in a file')
824
+ .option('-l, --lines <range>', 'Line range (e.g., 1-50)')
825
+ .option('-d, --detail <level>', 'Detail level (brief, normal, detailed)', 'normal')
826
+ .action(async (file, options) => {
827
+ const explain = new ExplainCommand(config, logger);
828
+ await explain.run(file, options);
829
+ });
830
+ // Fix command - Fix code issues
831
+ program
832
+ .command('fix [file]')
833
+ .alias('f')
834
+ .description('Fix issues in a file')
835
+ .option('-t, --type <type>', 'Fix type (bugs, style, security, performance)', 'bugs')
836
+ .option('--apply', 'Automatically apply fixes', false)
837
+ .action(async (file, options) => {
838
+ if (!file) {
839
+ logger.error('Usage: vigthoria fix <file> [--type bugs|style|security|performance] [--apply]');
840
+ process.exitCode = 1;
841
+ return;
842
+ }
843
+ const edit = new EditCommand(config, logger);
844
+ await edit.fix(file, options);
845
+ });
846
+ // Review command - Code review
847
+ program
848
+ .command('review <file>')
849
+ .alias('r')
850
+ .description('Review code quality')
851
+ .option('-f, --format <format>', 'Output format (text, json, markdown)', 'text')
852
+ .action(async (file, options) => {
853
+ const review = new ReviewCommand(config, logger);
854
+ await review.run(file, options);
855
+ });
856
+ const workflowCommand = program
857
+ .command('workflow')
858
+ .alias('flow')
859
+ .description('List, instantiate, and run repeatable VigFlow workflows');
860
+ workflowCommand
861
+ .command('templates')
862
+ .alias('catalog')
863
+ .description('List available VigFlow templates')
864
+ .option('-c, --category <category>', 'Filter templates by category')
865
+ .option('-s, --search <query>', 'Search template names, descriptions, and tags')
866
+ .option('--json', 'Emit machine-readable JSON output', false)
867
+ .action(async (options) => {
868
+ const workflow = new WorkflowCommand(config, logger);
869
+ await workflow.templates(options);
870
+ });
871
+ workflowCommand
872
+ .command('list')
873
+ .alias('ls')
874
+ .description('List workflows created for the current account')
875
+ .option('--json', 'Emit machine-readable JSON output', false)
876
+ .action(async (options) => {
877
+ const workflow = new WorkflowCommand(config, logger);
878
+ await workflow.list(options);
879
+ });
880
+ workflowCommand
881
+ .command('use-template <templateId>')
882
+ .description('Create a workflow from a built-in or saved template')
883
+ .option('-n, --name <name>', 'Override the workflow name')
884
+ .option('-v, --variables <json>', 'Template variables as a JSON object')
885
+ .option('--json', 'Emit machine-readable JSON output', false)
886
+ .action(async (templateId, options) => {
887
+ const workflow = new WorkflowCommand(config, logger);
888
+ await workflow.useTemplate(templateId, options);
889
+ });
890
+ workflowCommand
891
+ .command('run <workflowId>')
892
+ .description('Run a workflow immediately')
893
+ .option('-d, --data <json>', 'Execution input data as a JSON object')
894
+ .option('--no-brain', 'Do not attach local Project Brain context to this workflow run')
895
+ .option('--json', 'Emit machine-readable JSON output', false)
896
+ .action(async (workflowId, options) => {
897
+ const workflow = new WorkflowCommand(config, logger);
898
+ await workflow.run(workflowId, options);
899
+ });
900
+ workflowCommand
901
+ .command('status <executionId>')
902
+ .description('Get the current or final status of a workflow execution')
903
+ .option('--no-brain', 'Do not remember this workflow status in local Project Brain')
904
+ .option('--json', 'Emit machine-readable JSON output', false)
905
+ .action(async (executionId, options) => {
906
+ const workflow = new WorkflowCommand(config, logger);
907
+ await workflow.status(executionId, options);
908
+ });
909
+ workflowCommand.action(async () => {
910
+ const workflow = new WorkflowCommand(config, logger);
911
+ await workflow.templates({});
912
+ });
913
+ // ==================== HUB / MARKETPLACE COMMANDS ====================
914
+ // Hub command - Discover and activate API modules
915
+ const hubCommand = program
916
+ .command('hub')
917
+ .alias('marketplace')
918
+ .description('Discover, search, and activate Vigthoria API modules');
919
+ hubCommand
920
+ .command('discover')
921
+ .alias('d')
922
+ .description('Interactive module discovery - find the right APIs for your project')
923
+ .action(async () => {
924
+ const hub = new HubCommand(config, logger);
925
+ await hub.discover();
926
+ });
927
+ hubCommand
928
+ .command('list')
929
+ .alias('ls')
930
+ .description('List all available API modules')
931
+ .option('-c, --category <category>', 'Filter by category (payments, communication, ai, creative, media)')
932
+ .action(async (options) => {
933
+ const hub = new HubCommand(config, logger);
934
+ await hub.list(options);
935
+ });
936
+ hubCommand
937
+ .command('search <query>')
938
+ .alias('find')
939
+ .description('Semantic search for modules (e.g., "generate background music for my app")')
940
+ .action(async (query) => {
941
+ const hub = new HubCommand(config, logger);
942
+ await hub.search(query);
943
+ });
944
+ hubCommand
945
+ .command('activate <module>')
946
+ .alias('enable')
947
+ .description('Activate a module for your API key (enables pay-as-you-go)')
948
+ .action(async (module) => {
949
+ const hub = new HubCommand(config, logger);
950
+ await hub.activate(module);
951
+ });
952
+ hubCommand
953
+ .command('active')
954
+ .description('Show your currently active modules')
955
+ .action(async () => {
956
+ const hub = new HubCommand(config, logger);
957
+ await hub.active();
958
+ });
959
+ hubCommand
960
+ .command('info <module>')
961
+ .alias('details')
962
+ .description('Get detailed information about a module')
963
+ .action(async (module) => {
964
+ const hub = new HubCommand(config, logger);
965
+ await hub.info(module);
966
+ });
967
+ // Default hub action shows discover
968
+ hubCommand.action(async () => {
969
+ const hub = new HubCommand(config, logger);
970
+ await hub.discover();
971
+ });
972
+ // ==================== REPO COMMANDS ====================
973
+ // Repo command - Push/Pull projects to/from Vigthoria Repository
974
+ const repoCommand = program
975
+ .command('repo')
976
+ .alias('repository')
977
+ .description('Push and pull projects to/from your Vigthoria Repository');
978
+ repoCommand
979
+ .command('push [path]')
980
+ .alias('upload')
981
+ .description('Push current or specified project to Vigthoria Repo')
982
+ .option('-n, --name <projectName>', 'Project name override for the remote repo')
983
+ .option('-v, --visibility <type>', 'Set visibility (private, restricted, public)', 'private')
984
+ .option('-d, --description <text>', 'Project description')
985
+ .option('-f, --force', 'Overwrite existing project', false)
986
+ .option('-y, --yes', 'Run non-interactively with provided/default values', false)
987
+ .option('--open-in <engine>', 'After push, open project in engine: shop, visual, game')
988
+ .option('--browser', 'Open result URL in default browser', false)
989
+ .action(async (pathArg, options) => {
990
+ const repo = new RepoCommand(config, logger);
991
+ await repo.push({
992
+ path: pathArg,
993
+ name: options.name,
994
+ visibility: options.visibility,
995
+ description: options.description,
996
+ force: options.force,
997
+ yes: options.yes
998
+ });
999
+ if (options.openIn) {
1000
+ const engine = options.openIn;
1001
+ if (!['shop', 'visual', 'game'].includes(engine)) {
1002
+ logger.warn(`Unknown engine "${engine}". Use: shop, visual, game`);
1003
+ }
1004
+ else {
1005
+ await repo.openIn(engine, options.name || undefined, { browser: options.browser });
1006
+ }
1007
+ }
1008
+ });
1009
+ repoCommand
1010
+ .command('pull <name>')
1011
+ .alias('download')
1012
+ .description('Pull a project from your Vigthoria Repo')
1013
+ .option('-o, --output <path>', 'Output directory path')
1014
+ .option('-f, --force', 'Overwrite existing directory', false)
1015
+ .action(async (name, options) => {
1016
+ const repo = new RepoCommand(config, logger);
1017
+ await repo.pull(name, {
1018
+ output: options.output,
1019
+ force: options.force
1020
+ });
1021
+ });
1022
+ repoCommand
1023
+ .command('list')
1024
+ .alias('ls')
1025
+ .description('List all your projects in Vigthoria Repo')
1026
+ .option('-v, --visibility <type>', 'Filter by visibility (private, restricted, public)')
1027
+ .action(async (options) => {
1028
+ const repo = new RepoCommand(config, logger);
1029
+ await repo.list({ visibility: options.visibility });
1030
+ });
1031
+ repoCommand
1032
+ .command('status')
1033
+ .description('Show sync status of current project')
1034
+ .action(async () => {
1035
+ const repo = new RepoCommand(config, logger);
1036
+ await repo.status();
1037
+ });
1038
+ repoCommand
1039
+ .command('share <name>')
1040
+ .description('Generate a shareable link for a project')
1041
+ .option('-e, --expires <duration>', 'Link expiration (e.g., 7d, 24h, 30m)', '7d')
1042
+ .action(async (name, options) => {
1043
+ const repo = new RepoCommand(config, logger);
1044
+ await repo.share(name, { expires: options.expires });
1045
+ });
1046
+ repoCommand
1047
+ .command('delete <name>')
1048
+ .alias('rm')
1049
+ .description('Remove a project from your Vigthoria Repo')
1050
+ .action(async (name) => {
1051
+ const repo = new RepoCommand(config, logger);
1052
+ await repo.delete(name);
1053
+ });
1054
+ repoCommand
1055
+ .command('clone <url>')
1056
+ .description('Clone a public project from Vigthoria Repo')
1057
+ .option('-o, --output <path>', 'Output directory path')
1058
+ .option('-f, --force', 'Overwrite existing directory', false)
1059
+ .action(async (url, options) => {
1060
+ const repo = new RepoCommand(config, logger);
1061
+ await repo.clone(url, {
1062
+ output: options.output,
1063
+ force: options.force
1064
+ });
1065
+ });
1066
+ repoCommand
1067
+ .command('open-in <engine> [projectName]')
1068
+ .description('Open a Vigthoria Repo project in the Shop Engine, Visual Editor, or Gaming Engine')
1069
+ .option('--browser', 'Open result URL in default browser', false)
1070
+ .option('--shop-id <id>', 'Target shop ID (for shop engine)', 'default')
1071
+ .addHelpText('after', `
1072
+ Examples:
1073
+ vigthoria repo open-in shop my-store
1074
+ vigthoria repo open-in visual my-website
1075
+ vigthoria repo open-in game my-game --browser`)
1076
+ .action(async (engine, projectName, options) => {
1077
+ if (!['shop', 'visual', 'game'].includes(engine)) {
1078
+ logger.error(`Unknown engine "${engine}". Valid options: shop, visual, game`);
1079
+ process.exit(1);
1080
+ }
1081
+ const repo = new RepoCommand(config, logger);
1082
+ await repo.openIn(engine, projectName, {
1083
+ browser: options.browser,
1084
+ shopId: options.shopId
1085
+ });
1086
+ });
1087
+ // Default repo action shows help with available subcommands
1088
+ repoCommand.action(() => {
1089
+ repoCommand.outputHelp();
1090
+ });
1091
+ // ==================== DEPLOY COMMANDS ====================
1092
+ // Deploy command - Host projects on Vigthoria
1093
+ const deployCommand = program
1094
+ .command('deploy')
1095
+ .alias('host')
1096
+ .description('Deploy and host your project on Vigthoria infrastructure');
1097
+ deployCommand
1098
+ .command('preview')
1099
+ .description('Deploy to free preview URL')
1100
+ .option('-p, --project <path>', 'Project directory path', process.cwd())
1101
+ .action(async (options) => {
1102
+ const deploy = new DeployCommand(config, logger);
1103
+ await deploy.deployToPreview(options.project);
1104
+ });
1105
+ deployCommand
1106
+ .command('subdomain <name>')
1107
+ .description('Deploy to yourname.vigthoria.io')
1108
+ .option('-p, --project <path>', 'Project directory path', process.cwd())
1109
+ .action(async (name, options) => {
1110
+ const deploy = new DeployCommand(config, logger);
1111
+ await deploy.deployToSubdomain(name, options.project);
1112
+ });
1113
+ deployCommand
1114
+ .command('custom <domain>')
1115
+ .description('Deploy to your custom domain')
1116
+ .option('-p, --project <path>', 'Project directory path', process.cwd())
1117
+ .action(async (domain, options) => {
1118
+ const deploy = new DeployCommand(config, logger);
1119
+ await deploy.deployToCustomDomain(domain, options.project);
1120
+ });
1121
+ deployCommand
1122
+ .command('list')
1123
+ .alias('ls')
1124
+ .description('List all your deployments')
1125
+ .action(async () => {
1126
+ const deploy = new DeployCommand(config, logger);
1127
+ await deploy.list();
1128
+ });
1129
+ deployCommand
1130
+ .command('plans')
1131
+ .description('Show hosting plans and pricing')
1132
+ .action(async () => {
1133
+ const deploy = new DeployCommand(config, logger);
1134
+ await deploy.showPlans();
1135
+ });
1136
+ deployCommand
1137
+ .command('status [domain]')
1138
+ .description('Check deployment status')
1139
+ .action(async (domain) => {
1140
+ const deploy = new DeployCommand(config, logger);
1141
+ await deploy.status(domain);
1142
+ });
1143
+ deployCommand
1144
+ .command('verify <domain>')
1145
+ .description('Verify DNS configuration for custom domain')
1146
+ .action(async (domain) => {
1147
+ const deploy = new DeployCommand(config, logger);
1148
+ await deploy.verify(domain);
1149
+ });
1150
+ deployCommand
1151
+ .command('remove <domain>')
1152
+ .alias('rm')
1153
+ .description('Remove a deployment')
1154
+ .action(async (domain) => {
1155
+ const deploy = new DeployCommand(config, logger);
1156
+ await deploy.remove(domain);
1157
+ });
1158
+ // Default deploy action shows interactive wizard
1159
+ deployCommand
1160
+ .option('-s, --subdomain <name>', 'Deploy to subdomain')
1161
+ .option('-d, --domain <domain>', 'Deploy to custom domain')
1162
+ .option('-p, --project <path>', 'Project directory path', process.cwd())
1163
+ .action(async (options) => {
1164
+ const deploy = new DeployCommand(config, logger);
1165
+ await deploy.deploy({
1166
+ subdomain: options.subdomain,
1167
+ domain: options.domain,
1168
+ project: options.project
1169
+ });
1170
+ });
1171
+ // ==================== PREVIEW COMMAND ====================
1172
+ program
1173
+ .command('preview')
1174
+ .description('Preview project locally with visual diffs and proof validation')
1175
+ .option('-p, --project <path>', 'Project directory path', process.cwd())
1176
+ .option('-e, --entry <file>', 'Entry HTML file (auto-detected if omitted)')
1177
+ .option('--port <number>', 'Local server port', parseInt)
1178
+ .option('--no-open', 'Do not auto-open browser')
1179
+ .option('--diff', 'Show consolidated diff of recent agent changes')
1180
+ .option('--proof', 'Run Template Service preview gate and persist proof bundle')
1181
+ .option('--screenshot', 'Capture screenshot via Puppeteer')
1182
+ .action(async (options) => {
1183
+ const preview = new PreviewCommand(config, logger);
1184
+ await preview.run({
1185
+ project: options.project,
1186
+ entry: options.entry,
1187
+ port: options.port,
1188
+ open: options.open,
1189
+ diff: options.diff,
1190
+ proof: options.proof,
1191
+ screenshot: options.screenshot,
1192
+ });
1193
+ });
1194
+ // ==================== LEGION COMMAND ====================
1195
+ program
1196
+ .command('legion [request]')
1197
+ .description('Run parallel tasks via Hyper Loop Legion orchestrator')
1198
+ .option('--workers', 'List available Legion workers')
1199
+ .option('--status', 'Show Legion infrastructure status')
1200
+ .option('--cortex', 'Vigthoria Cortex: maximum intelligence execution')
1201
+ .option('--approve', 'Auto-approve Cortex execution prompt')
1202
+ .option('--no-approve', 'Require interactive approval before execution')
1203
+ .option('--auto-charge', 'Attempt direct VigCoin top-up when Cortex balance is low')
1204
+ .option('--plan-only', 'Run Cortex estimator only; do not execute Legion job')
1205
+ .option('--force-budget', 'Allow execution when estimated budget exceeds the hard safe-stop ceiling')
1206
+ .option('--ignore-preflight', 'Bypass mandatory local preflight checks (no warranty)')
1207
+ .option('--speed', 'Enable speed mode (allows optional role skips when convergence is detected)')
1208
+ .option('--repro-cmd <command>', 'Local reproduction/preflight command to validate before cloud spend')
1209
+ .option('--expect-repro-fail', 'Require repro command to fail (non-zero) before proceeding')
1210
+ .option('--models <csv>', 'Comma-separated model IDs to constrain Cortex selection')
1211
+ .option('-t, --timeout <seconds>', 'Override Legion execution timeout in seconds (defaults to server policy)')
1212
+ .option('-w, --worker <name>', 'Execute a specific worker')
1213
+ .option('-p, --project <path>', 'Project directory', process.cwd())
1214
+ .action(async (request, options) => {
1215
+ const legion = new LegionCommand(config, logger);
1216
+ const parsedTimeout = Number.parseInt(String(options.timeout || ''), 10);
1217
+ await legion.run(request, {
1218
+ workers: options.workers,
1219
+ status: options.status,
1220
+ cortex: Boolean(options.cortex),
1221
+ approve: options.approve,
1222
+ noApprove: options.approve === false,
1223
+ autoCharge: options.autoCharge,
1224
+ planOnly: options.planOnly,
1225
+ forceBudget: options.forceBudget,
1226
+ ignorePreflight: options.ignorePreflight,
1227
+ speed: options.speed,
1228
+ reproCmd: options.reproCmd,
1229
+ expectReproFail: options.expectReproFail,
1230
+ models: options.models,
1231
+ timeoutSec: Number.isFinite(parsedTimeout) && parsedTimeout > 0 ? parsedTimeout : undefined,
1232
+ worker: options.worker,
1233
+ project: options.project,
1234
+ });
1235
+ });
1236
+ // ==================== REPLAY & FORK COMMANDS ====================
1237
+ program
1238
+ .command('history')
1239
+ .alias('runs')
1240
+ .description('List recent V3 agent runs')
1241
+ .option('-n, --limit <count>', 'Number of runs to show', '20')
1242
+ .option('-p, --project <path>', 'Project directory', process.cwd())
1243
+ .option('--json', 'Machine-readable JSON output', false)
1244
+ .action(async (options) => {
1245
+ const history = new HistoryCommand(config, logger);
1246
+ await history.run({ limit: parseInt(options.limit, 10) || 20, json: options.json, project: options.project });
1247
+ });
1248
+ program
1249
+ .command('replay <runId>')
1250
+ .description('Replay events from a V3 agent run step-by-step')
1251
+ .option('-s, --speed <ms>', 'Delay between events in ms', '200')
1252
+ .option('-p, --project <path>', 'Project directory', process.cwd())
1253
+ .option('--json', 'Machine-readable JSON output', false)
1254
+ .action(async (runId, options) => {
1255
+ const replay = new ReplayCommand(config, logger);
1256
+ await replay.run(runId, { speed: parseInt(options.speed, 10) || 200, json: options.json, project: options.project });
1257
+ });
1258
+ program
1259
+ .command('fork <runId> [message]')
1260
+ .description('Fork from an existing V3 agent run with new instructions')
1261
+ .option('-e, --event-index <index>', 'Fork from specific event index', '0')
1262
+ .option('-p, --project <path>', 'Project directory', process.cwd())
1263
+ .option('--json', 'Machine-readable JSON output', false)
1264
+ .action(async (runId, message, options) => {
1265
+ const fork = new ForkCommand(config, logger);
1266
+ await fork.run(runId, message || '', { eventIndex: parseInt(options.eventIndex, 10) || 0, project: options.project, json: options.json });
1267
+ });
1268
+ program
1269
+ .command('cancel [contextId]')
1270
+ .description('Cancel an in-flight V3 agent run (by context_id), or use --all to cancel every active run')
1271
+ .option('-a, --all', 'Cancel all currently-active runs', false)
1272
+ .option('-l, --list', 'List active runs without cancelling', false)
1273
+ .option('--json', 'Machine-readable JSON output', false)
1274
+ .action(async (contextId, options) => {
1275
+ const cancel = new CancelCommand(config, logger);
1276
+ await cancel.run(contextId, { all: options.all, list: options.list, json: options.json });
1277
+ });
1278
+ // ==================== AUTH COMMANDS ====================
1279
+ // Auth commands
1280
+ program
1281
+ .command('login')
1282
+ .description('Login to Vigthoria Coder (prompts for credentials when run without flags)')
1283
+ .option('-t, --token <token>', 'API token for token-based authentication')
1284
+ .option('-e, --email <email>', 'Account email for credential-based login')
1285
+ .option('-p, --password <password>', 'Account password for credential-based login')
1286
+ .option('--device', 'Use OAuth device flow (requires server support)')
1287
+ .action(async (options) => {
1288
+ await handleLogin(options);
1289
+ });
1290
+ program
1291
+ .command('logout')
1292
+ .description('Logout from Vigthoria Coder')
1293
+ .action(async () => {
1294
+ await handleLogout(null);
1295
+ });
1296
+ program
1297
+ .command('status')
1298
+ .description('Show authentication and subscription status')
1299
+ .action(async () => {
1300
+ await statusAction();
1301
+ });
1302
+ program
1303
+ .command('doctor')
1304
+ .description('Run local Vigthoria CLI diagnostics (no network calls unless --check-api)')
1305
+ .option('--check-api', 'Also probe Vigthoria API reachability', false)
1306
+ .option('--json', 'Emit machine-readable JSON output', false)
1307
+ .action(async (options) => {
1308
+ const apiUrl = String(config.get('apiUrl') || 'https://coder.vigthoria.io').replace(/\/$/, '');
1309
+ const modelsApiUrl = String(config.get('modelsApiUrl') || 'https://api.vigthoria.io').replace(/\/$/, '');
1310
+ const offline = isOfflineMode();
1311
+ const updateSuppressed = isUpdateCheckSuppressed();
1312
+ const subscription = config.get('subscription') || { plan: null, status: null, expiresAt: null };
1313
+ const report = {
1314
+ cliVersion: VERSION,
1315
+ nodeVersion: process.version,
1316
+ platform: `${process.platform} ${process.arch}`,
1317
+ cwd: process.cwd(),
1318
+ homeDir: os.homedir(),
1319
+ configPath: config.getConfigPath(),
1320
+ stateFile: getCliStateFile(),
1321
+ apiUrl,
1322
+ modelsApiUrl,
1323
+ loggedIn: config.isAuthenticated(),
1324
+ subscriptionPlan: subscription.plan || null,
1325
+ subscriptionStatus: subscription.status || null,
1326
+ offlineMode: offline,
1327
+ updateCheckSuppressed: updateSuppressed,
1328
+ envOverrides: {
1329
+ VIGTHORIA_API_URL: process.env.VIGTHORIA_API_URL || null,
1330
+ VIGTHORIA_V3_AGENT_URL: process.env.VIGTHORIA_V3_AGENT_URL || null,
1331
+ VIGTHORIA_OFFLINE: process.env.VIGTHORIA_OFFLINE || null,
1332
+ VIGTHORIA_TOKEN: process.env.VIGTHORIA_TOKEN ? '<set>' : null,
1333
+ HYPERLOOP_SERVICE_KEY: process.env.HYPERLOOP_SERVICE_KEY ? '<set>' : null,
1334
+ V3_SERVICE_KEY: process.env.V3_SERVICE_KEY ? '<set>' : null,
1335
+ },
1336
+ };
1337
+ if (options.checkApi && !offline) {
1338
+ try {
1339
+ const probe = await axios.get(`${apiUrl}/api/health`, { timeout: 4000, validateStatus: () => true });
1340
+ report.apiHealth = probe.status >= 200 && probe.status < 400 ? 'online' : `status ${probe.status}`;
1341
+ }
1342
+ catch (error) {
1343
+ report.apiHealth = `unreachable (${error.message})`;
1344
+ }
1345
+ try {
1346
+ const probe = await axios.get(`${modelsApiUrl}/health`, { timeout: 4000, validateStatus: () => true });
1347
+ report.modelsApiHealth = probe.status >= 200 && probe.status < 400 ? 'online' : `status ${probe.status}`;
1348
+ }
1349
+ catch (error) {
1350
+ report.modelsApiHealth = `unreachable (${error.message})`;
1351
+ }
1352
+ }
1353
+ if (options.json) {
1354
+ console.log(JSON.stringify(report, null, 2));
1355
+ }
1356
+ else {
1357
+ console.log(chalk.bold('Vigthoria CLI diagnostics'));
1358
+ for (const [key, value] of Object.entries(report)) {
1359
+ if (value && typeof value === 'object') {
1360
+ console.log(chalk.gray(`- ${key}:`));
1361
+ for (const [subKey, subVal] of Object.entries(value)) {
1362
+ console.log(chalk.gray(` - ${subKey}: ${subVal ?? '(none)'}`));
1363
+ }
1364
+ }
1365
+ else {
1366
+ console.log(chalk.gray(`- ${key}: ${value ?? '(none)'}`));
1367
+ }
1368
+ }
1369
+ if (!options.checkApi) {
1370
+ console.log(chalk.gray('\nTip: pass --check-api to verify API reachability.'));
1371
+ }
1372
+ }
1373
+ process.exitCode = 0;
1374
+ });
1375
+ // Config command
1376
+ program
1377
+ .command('config')
1378
+ .description('Configure Vigthoria CLI settings')
1379
+ .option('-s, --set <key=value>', 'Set a configuration value')
1380
+ .option('-g, --get <key>', 'Get a configuration value')
1381
+ .option('-l, --list', 'List all settings')
1382
+ .option('-r, --reset', 'Reset to defaults')
1383
+ .action(async (options) => {
1384
+ const configCmd = new ConfigCommand(config, logger);
1385
+ await configCmd.run(options);
1386
+ });
1387
+ // Update command - Check for and install updates
1388
+ program
1389
+ .command('update')
1390
+ .alias('upgrade')
1391
+ .description('Check for updates and upgrade Vigthoria CLI. Default manifest: https://coder.vigthoria.io/releases/manifest.json')
1392
+ .option('-c, --check', 'Only check for updates, don\'t install')
1393
+ .option('-f, --from <target>', 'Install update from npm spec, local .tgz path, or URL')
1394
+ .option('-m, --manifest <url>', 'Update manifest URL for server-driven releases')
1395
+ .option('--channel <name>', 'Release channel to use from manifest (default: stable)', 'stable')
1396
+ .option('--allow-downgrade', 'Allow installing an older version from custom update source')
1397
+ .action(async (options) => {
1398
+ const { execSync } = await import('child_process');
1399
+ const updateTarget = typeof options.from === 'string' ? options.from.trim() : '';
1400
+ const manifestUrl = typeof options.manifest === 'string' ? options.manifest.trim() : VIGTHORIA_DEFAULT_MANIFEST_URL.trim();
1401
+ const channel = typeof options.channel === 'string' ? options.channel.trim() : 'stable';
1402
+ const allowDowngrade = !!options.allowDowngrade;
1403
+ const gitPackageSpec = 'git+https://market.vigthoria.io/vigthoria/vigthoria-cli.git';
1404
+ if (updateTarget) {
1405
+ if (!isSafeNpmPackageSpec(updateTarget)) {
1406
+ console.error(chalk.red('Refusing to run installer with unsafe package spec.'));
1407
+ console.error(chalk.gray('Allowed characters: A-Z a-z 0-9 . _ - / @ : + \\ ='));
1408
+ process.exitCode = 1;
1409
+ return;
1410
+ }
1411
+ try {
1412
+ if (options.check) {
1413
+ console.log(chalk.cyan(`Update source configured: ${updateTarget}`));
1414
+ console.log(chalk.gray('Run `vigthoria update --from <target>` to install from this source'));
1415
+ return;
1416
+ }
1417
+ console.log(chalk.cyan(`Installing update from ${updateTarget}...`));
1418
+ await installGlobalPackageWithNpm(updateTarget);
1419
+ console.log(chalk.green('Update installed successfully'));
1420
+ console.log(chalk.gray('Please restart the CLI to use the new version'));
1421
+ return;
1422
+ }
1423
+ catch (error) {
1424
+ console.error(chalk.red('Failed to install update from target:'), error.message);
1425
+ console.log(chalk.gray(`Try manually: npm install -g ${updateTarget}`));
1426
+ process.exitCode = 1;
1427
+ return;
1428
+ }
1429
+ }
1430
+ if (manifestUrl) {
1431
+ if (isOfflineMode()) {
1432
+ console.log(chalk.yellow('Offline mode (VIGTHORIA_OFFLINE=1): skipping manifest update.'));
1433
+ return;
1434
+ }
1435
+ try {
1436
+ console.log(chalk.cyan(`Checking manifest channel ${channel}...`));
1437
+ const response = await axios.get(manifestUrl, {
1438
+ timeout: 10000,
1439
+ maxRedirects: 5,
1440
+ validateStatus: (status) => status >= 200 && status < 300,
1441
+ });
1442
+ const entry = resolveManifestEntry(response.data || {}, channel);
1443
+ if (!entry || !entry.version || !entry.url) {
1444
+ console.error(chalk.red(`Manifest missing valid release entry for channel: ${channel}`));
1445
+ console.log(chalk.gray('Expected: channels.<channel>.version and channels.<channel>.url'));
1446
+ process.exitCode = 1;
1447
+ return;
1448
+ }
1449
+ const currentVersion = VERSION;
1450
+ const comparison = compareVersions(entry.version, currentVersion);
1451
+ if (!allowDowngrade && comparison <= 0) {
1452
+ console.log(chalk.green(`You are running the latest version for channel ${channel} (${currentVersion})`));
1453
+ return;
1454
+ }
1455
+ console.log(chalk.yellow(`Update available from manifest: ${currentVersion} -> ${entry.version} (${channel})`));
1456
+ if (entry.notes) {
1457
+ console.log(chalk.gray(` ${entry.notes}`));
1458
+ }
1459
+ if (options.check) {
1460
+ console.log(chalk.gray(`Install with: vigthoria update --manifest ${manifestUrl} --channel ${channel}`));
1461
+ return;
1462
+ }
1463
+ const tmpFile = path.join(os.tmpdir(), `vigthoria-update-${Date.now()}.tgz`);
1464
+ try {
1465
+ console.log(chalk.cyan('Downloading release package...'));
1466
+ await downloadFile(entry.url, tmpFile);
1467
+ if (entry.sha256) {
1468
+ const expected = entry.sha256.toLowerCase();
1469
+ const actual = sha256File(tmpFile).toLowerCase();
1470
+ if (actual !== expected) {
1471
+ console.error(chalk.red('Release checksum verification failed'));
1472
+ console.error(chalk.red(`Expected: ${expected}`));
1473
+ console.error(chalk.red(`Actual: ${actual}`));
1474
+ process.exitCode = 1;
1475
+ return;
1476
+ }
1477
+ console.log(chalk.green('Checksum verification passed'));
1478
+ }
1479
+ else {
1480
+ console.log(chalk.yellow.bold('WARNING: manifest entry has no sha256.'));
1481
+ console.log(chalk.yellow(' The release artifact will be installed without integrity verification.'));
1482
+ console.log(chalk.gray(' Set --manifest to a trusted source, or supply sha256 in the manifest entry.'));
1483
+ }
1484
+ console.log(chalk.cyan('Installing update...'));
1485
+ await installGlobalPackageWithNpm(tmpFile);
1486
+ console.log(chalk.green(`Updated to version ${entry.version}`));
1487
+ console.log(chalk.gray('Please restart the CLI to use the new version'));
1488
+ return;
1489
+ }
1490
+ finally {
1491
+ if (fs.existsSync(tmpFile)) {
1492
+ try {
1493
+ fs.unlinkSync(tmpFile);
1494
+ }
1495
+ catch { }
1496
+ }
1497
+ }
1498
+ }
1499
+ catch (error) {
1500
+ console.error(chalk.red('Failed to process manifest update:'), error.message);
1501
+ console.log(chalk.gray('Falling back to npm/git update channels...'));
1502
+ }
1503
+ }
1504
+ if (isOfflineMode()) {
1505
+ console.log(chalk.yellow('Offline mode (VIGTHORIA_OFFLINE=1): skipping update check.'));
1506
+ return;
1507
+ }
1508
+ console.log(chalk.cyan('Checking for updates...'));
1509
+ try {
1510
+ // Get latest version from npm - cross-platform
1511
+ const latestVersion = execSync('npm view vigthoria-cli version', {
1512
+ encoding: 'utf8',
1513
+ stdio: ['pipe', 'pipe', 'pipe'],
1514
+ windowsHide: true,
1515
+ }).trim();
1516
+ const currentVersion = VERSION;
1517
+ // Use semantic version comparison (1.6.0 > 1.5.9)
1518
+ const comparison = compareVersions(latestVersion, currentVersion);
1519
+ if (comparison <= 0) {
1520
+ console.log(chalk.green(`You are running the latest version (${currentVersion})`));
1521
+ return;
1522
+ }
1523
+ console.log(chalk.yellow(`Update available: ${currentVersion} -> ${latestVersion}`));
1524
+ if (options.check) {
1525
+ console.log(chalk.gray('Run `vigthoria update` to install the update'));
1526
+ return;
1527
+ }
1528
+ console.log(chalk.cyan('Installing update from npm registry...'));
1529
+ await installGlobalPackageWithNpm('vigthoria-cli@latest');
1530
+ console.log(chalk.green(`Updated to version ${latestVersion}`));
1531
+ console.log(chalk.gray('Please restart the CLI to use the new version'));
1532
+ }
1533
+ catch (error) {
1534
+ console.error(chalk.red('Failed to check/install via npm registry:'), error.message);
1535
+ if (options.check) {
1536
+ console.log(chalk.gray(`npm registry check failed; fallback install target is ${gitPackageSpec}`));
1537
+ return;
1538
+ }
1539
+ try {
1540
+ console.log(chalk.cyan('Attempting git package fallback...'));
1541
+ await installGlobalPackageWithNpm(gitPackageSpec);
1542
+ console.log(chalk.green('Updated via git package fallback'));
1543
+ console.log(chalk.gray('Please restart the CLI to use the new version'));
1544
+ }
1545
+ catch (fallbackError) {
1546
+ console.error(chalk.red('Fallback update also failed:'), fallbackError.message);
1547
+ console.log(chalk.gray('Try manually: npm install -g vigthoria-cli@latest'));
1548
+ console.log(chalk.gray(`Or: npm install -g ${gitPackageSpec}`));
1549
+ process.exitCode = 1;
1550
+ }
1551
+ }
1552
+ });
1553
+ // Hyper Loop command alias (maps to legion --status for checklist gate 6.9)
1554
+ program
1555
+ .command('hyper-loop')
1556
+ .description('Hyper Loop Legion orchestrator commands')
1557
+ .command('status')
1558
+ .description('Show Hyper Loop Legion infrastructure status')
1559
+ .action(async () => {
1560
+ const legion = new LegionCommand(config, logger);
1561
+ await legion.run(undefined, {
1562
+ status: true,
1563
+ workers: false,
1564
+ cortex: false,
1565
+ approve: false,
1566
+ noApprove: true,
1567
+ planOnly: false,
1568
+ models: undefined,
1569
+ worker: undefined,
1570
+ project: process.cwd(),
1571
+ });
1572
+ });
1573
+ // DevTools command alias (maps to bridge for checklist gate 6.12)
1574
+ program
1575
+ .command('devtools')
1576
+ .description('DevTools Bridge commands for browser debugging')
1577
+ .command('connect')
1578
+ .description('Connect to DevTools Bridge')
1579
+ .action(async () => {
1580
+ const bridge = new BridgeCommand(config, logger);
1581
+ await bridge.status();
1582
+ });
1583
+ // Init command - Initialize project
1584
+ program
1585
+ .command('init')
1586
+ .description('Initialize Vigthoria in current project')
1587
+ .action(async () => {
1588
+ const configCmd = new ConfigCommand(config, logger);
1589
+ await configCmd.init();
1590
+ });
1591
+ const codingCommandDefinitions = [
1592
+ { name: 'edit', description: 'Edit code by describing the desired change', instruction: 'Edit the project according to this request' },
1593
+ { name: 'generate', description: 'Generate code from a prompt', instruction: 'Generate code for this request' },
1594
+ { name: 'explain', description: 'Explain code, errors, or project behavior', instruction: 'Explain this request clearly and practically' },
1595
+ { name: 'review', description: 'Review code and suggest concrete improvements', instruction: 'Review the project or code for this request' },
1596
+ { name: 'fix', description: 'Fix bugs, build failures, or test failures', instruction: 'Fix this issue in the project' },
1597
+ ];
1598
+ for (const commandDefinition of codingCommandDefinitions) {
1599
+ program
1600
+ .command(commandDefinition.name)
1601
+ .description(commandDefinition.description)
1602
+ .argument('[request...]', 'Request text for the coding assistant')
1603
+ .option('-m, --model <model>', 'Model to use', 'code')
1604
+ .option('-p, --project <path>', 'Project directory', process.cwd())
1605
+ .action(async (requestParts = [], options) => {
1606
+ const requestText = requestParts.join(' ').trim();
1607
+ const chat = new ChatCommand(config, logger);
1608
+ await chat.run({
1609
+ model: options.model || 'code',
1610
+ project: options.project || process.cwd(),
1611
+ prompt: requestText ? `${commandDefinition.instruction}: ${requestText}` : commandDefinition.instruction,
1612
+ });
1613
+ });
1614
+ }
1615
+ try {
1616
+ // Default to chat if no command
1617
+ if (args.length === 2) {
1618
+ const chat = new ChatCommand(config, logger);
1619
+ await chat.run({ model: 'code', project: process.cwd() });
1620
+ process.exitCode = 0;
1621
+ return;
1622
+ }
1623
+ await program.parseAsync(args);
1624
+ if (process.exitCode === undefined || process.exitCode === 0) {
1625
+ process.exitCode = 0;
1626
+ }
1627
+ }
1628
+ catch (error) {
1629
+ reportCliError(error, jsonOutputRequested);
1630
+ process.exitCode = 1;
1631
+ }
1632
+ }
1633
+ setupErrorHandlers();
1634
+ async function bootstrapCli() {
1635
+ try {
1636
+ await main(process.argv);
1637
+ }
1638
+ catch (err) {
1639
+ handleFatalCliError(err, process.argv.includes('--json'));
1640
+ process.exit(1);
1641
+ }
1642
+ }
1643
+ void (async () => {
1644
+ try {
1645
+ await bootstrapCli();
1646
+ }
1647
+ catch (err) {
1648
+ handleFatalCliError(err, process.argv.includes('--json'));
1649
+ process.exit(1);
1650
+ }
1651
+ })();
1652
+ export const __cliErrorHandlingReady = true;