sphereai-cli 1.0.0

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 (70) hide show
  1. package/README.md +529 -0
  2. package/dist/commands/auth.d.ts +3 -0
  3. package/dist/commands/auth.d.ts.map +1 -0
  4. package/dist/commands/auth.js +194 -0
  5. package/dist/commands/auth.js.map +1 -0
  6. package/dist/commands/execute.d.ts +3 -0
  7. package/dist/commands/execute.d.ts.map +1 -0
  8. package/dist/commands/execute.js +165 -0
  9. package/dist/commands/execute.js.map +1 -0
  10. package/dist/commands/files.d.ts +3 -0
  11. package/dist/commands/files.d.ts.map +1 -0
  12. package/dist/commands/files.js +235 -0
  13. package/dist/commands/files.js.map +1 -0
  14. package/dist/commands/models.d.ts +3 -0
  15. package/dist/commands/models.d.ts.map +1 -0
  16. package/dist/commands/models.js +70 -0
  17. package/dist/commands/models.js.map +1 -0
  18. package/dist/commands/prompts.d.ts +3 -0
  19. package/dist/commands/prompts.d.ts.map +1 -0
  20. package/dist/commands/prompts.js +72 -0
  21. package/dist/commands/prompts.js.map +1 -0
  22. package/dist/index.d.ts +3 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +103 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/services/api-client.d.ts +16 -0
  27. package/dist/services/api-client.d.ts.map +1 -0
  28. package/dist/services/api-client.js +125 -0
  29. package/dist/services/api-client.js.map +1 -0
  30. package/dist/services/auth-service.d.ts +28 -0
  31. package/dist/services/auth-service.d.ts.map +1 -0
  32. package/dist/services/auth-service.js +171 -0
  33. package/dist/services/auth-service.js.map +1 -0
  34. package/dist/services/cli-api-client.d.ts +20 -0
  35. package/dist/services/cli-api-client.d.ts.map +1 -0
  36. package/dist/services/cli-api-client.js +83 -0
  37. package/dist/services/cli-api-client.js.map +1 -0
  38. package/dist/services/config-service.d.ts +43 -0
  39. package/dist/services/config-service.d.ts.map +1 -0
  40. package/dist/services/config-service.js +97 -0
  41. package/dist/services/config-service.js.map +1 -0
  42. package/dist/services/file-tracking-service.d.ts +62 -0
  43. package/dist/services/file-tracking-service.d.ts.map +1 -0
  44. package/dist/services/file-tracking-service.js +207 -0
  45. package/dist/services/file-tracking-service.js.map +1 -0
  46. package/dist/types/api-types.d.ts +67 -0
  47. package/dist/types/api-types.d.ts.map +1 -0
  48. package/dist/types/api-types.js +10 -0
  49. package/dist/types/api-types.js.map +1 -0
  50. package/dist/utils/errors.d.ts +35 -0
  51. package/dist/utils/errors.d.ts.map +1 -0
  52. package/dist/utils/errors.js +82 -0
  53. package/dist/utils/errors.js.map +1 -0
  54. package/dist/utils/file-walker.d.ts +31 -0
  55. package/dist/utils/file-walker.d.ts.map +1 -0
  56. package/dist/utils/file-walker.js +118 -0
  57. package/dist/utils/file-walker.js.map +1 -0
  58. package/dist/utils/formatters.d.ts +35 -0
  59. package/dist/utils/formatters.d.ts.map +1 -0
  60. package/dist/utils/formatters.js +101 -0
  61. package/dist/utils/formatters.js.map +1 -0
  62. package/dist/utils/ignore-parser.d.ts +21 -0
  63. package/dist/utils/ignore-parser.d.ts.map +1 -0
  64. package/dist/utils/ignore-parser.js +170 -0
  65. package/dist/utils/ignore-parser.js.map +1 -0
  66. package/dist/utils/sphereignore.d.ts +34 -0
  67. package/dist/utils/sphereignore.d.ts.map +1 -0
  68. package/dist/utils/sphereignore.js +148 -0
  69. package/dist/utils/sphereignore.js.map +1 -0
  70. package/package.json +48 -0
package/README.md ADDED
@@ -0,0 +1,529 @@
1
+ # SphereAI CLI
2
+
3
+ A command-line interface tool for interacting with the SphereAI API.
4
+
5
+ ## Features
6
+
7
+ - **Key-Based Authentication** - Authenticate using UserChannelKey (recommended)
8
+ - **OAuth2 Authentication** - Secure browser-based authentication flow with local callback server (legacy)
9
+ - **JWT Token Management** - Secure token storage with automatic user info extraction from token payload
10
+ - **File Tracking** - Git-like file tracking system with .sphereignore support
11
+ - **Multiple Output Formats** - JSON and table formats for all commands
12
+ - **Environment Override** - Support for environment variables and command-line options
13
+ - **Offline User Info** - User information extracted from JWT token (no API calls needed)
14
+
15
+ ## Installation
16
+
17
+ ### From npm (Global Installation)
18
+
19
+ ```bash
20
+ npm install -g sphereai-cli
21
+ ```
22
+
23
+ ### From Source
24
+
25
+ ```bash
26
+ # Clone the repository
27
+ git clone <repository-url>
28
+ cd SphereAI.CLI
29
+
30
+ # Install dependencies
31
+ npm install
32
+
33
+ # Build the project
34
+ npm run build
35
+
36
+ # Link globally for local development
37
+ npm link
38
+ ```
39
+
40
+ ## Quick Start
41
+
42
+ ### 1. Configure API URLs
43
+
44
+ Set the required API URLs using environment variables:
45
+
46
+ ```bash
47
+ # Main API URL (for prompts and other resources)
48
+ export SPHEREAI_API_URL=https://api.develop.sphereai.nava.com.br
49
+
50
+ # CLI API URL (for key-based authentication)
51
+ export SPHEREAI_CLI_API_URL=https://cli.api.develop.sphereai.nava.com.br
52
+ ```
53
+
54
+ Or create a `.env` file in your project directory:
55
+
56
+ ```bash
57
+ # Main API URL (for prompts and other resources after authentication)
58
+ SPHEREAI_API_URL=https://api.develop.sphereai.nava.com.br
59
+
60
+ # CLI API URL (for key-based authentication)
61
+ SPHEREAI_CLI_API_URL=https://cli.api.develop.sphereai.nava.com.br
62
+
63
+ # OAuth2 URL (for legacy OAuth authentication)
64
+ SPHEREAI_AUTH_URL=http://localhost:3000/login?callback=http://localhost:3001/callback
65
+ SPHEREAI_OAUTH_CALLBACK_PORT=3001
66
+ ```
67
+
68
+ ### 2. Authentication
69
+
70
+ #### Option A: UserChannelKey Authentication (Recommended)
71
+
72
+ Authenticate using your UserChannelKey:
73
+
74
+ ```bash
75
+ sphereai auth login YOUR_USER_CHANNEL_KEY
76
+ ```
77
+
78
+ This will:
79
+ 1. Send your key to the CLI API endpoint `https://cli.api.develop.sphereai.nava.com.br/api/v1/Auth`
80
+ 2. Receive a JWT token and user information
81
+ 3. Save the token securely to `~/.config/sphereai-cli/config.json`
82
+ 4. Display user and channel information
83
+
84
+ Example response:
85
+ ```
86
+ ✔ Successfully authenticated!
87
+ ✓ Logged in as: John Doe
88
+ ℹ Channel: Production Channel
89
+ ℹ Token expires in: 3600 seconds
90
+ ℹ Token expiration: 1/13/2025, 10:00:00 AM
91
+ ```
92
+
93
+ #### Option B: OAuth2 Flow (Legacy)
94
+
95
+ For legacy OAuth2 authentication:
96
+
97
+ ```bash
98
+ export SPHEREAI_AUTH_URL=https://your-auth-url.com/authorize
99
+ sphereai auth login-oauth
100
+ ```
101
+
102
+ This will:
103
+ 1. Open your browser to the authorization page
104
+ 2. Start a local callback server on port 3001
105
+ 3. Save the token securely to `~/.config/sphereai-cli/config.json`
106
+
107
+ #### Option C: Manual Token Setting
108
+
109
+ If you already have a JWT token:
110
+
111
+ ```bash
112
+ sphereai auth set-token YOUR_JWT_TOKEN
113
+ ```
114
+
115
+ ### 3. Verify Authentication
116
+
117
+ ```bash
118
+ sphereai auth whoami
119
+ ```
120
+
121
+ This command extracts your user information directly from the JWT token payload (no API call required), displaying:
122
+ - User ID
123
+ - Channel ID
124
+ - Name
125
+ - Channel Name
126
+ - Role
127
+ - Account creation date
128
+
129
+ ## Commands
130
+
131
+ ### Authentication Commands
132
+
133
+ | Command | Description |
134
+ |---------|-------------|
135
+ | `sphereai auth login <key>` | Authenticate using UserChannelKey (recommended) |
136
+ | `sphereai auth login-oauth` | Authenticate using OAuth2 flow (legacy) |
137
+ | `sphereai auth logout` | Clear stored credentials |
138
+ | `sphereai auth whoami` | Display current user information |
139
+ | `sphereai auth set-token <token>` | Manually set authentication token |
140
+ | `sphereai auth config` | Display current configuration |
141
+
142
+ #### Authentication Examples
143
+
144
+ ```bash
145
+ # Authenticate with UserChannelKey (recommended)
146
+ sphereai auth login YOUR_USER_CHANNEL_KEY
147
+
148
+ # OAuth2 login (legacy - requires SPHEREAI_AUTH_URL)
149
+ sphereai auth login-oauth
150
+
151
+ # Set token manually
152
+ sphereai auth set-token YOUR_JWT_TOKEN
153
+
154
+ # Check current user
155
+ sphereai auth whoami
156
+
157
+ # Check current user (with JSON output)
158
+ sphereai auth whoami --output json
159
+
160
+ # View configuration
161
+ sphereai auth config
162
+
163
+ # Logout (clear stored token)
164
+ sphereai auth logout
165
+ ```
166
+
167
+ ### File Management Commands
168
+
169
+ | Command | Description |
170
+ |---------|-------------|
171
+ | `sphereai files add [path]` | Add files to tracking (defaults to current directory) |
172
+ | `sphereai files list` | List all tracked files |
173
+ | `sphereai files remove <path>` | Remove a file from tracking |
174
+ | `sphereai files clear` | Clear all tracked files |
175
+ | `sphereai files status` | Show status of tracked files |
176
+ | `sphereai files ignore` | Show .sphereignore patterns |
177
+
178
+ #### File Management Examples
179
+
180
+ ```bash
181
+ # Add all files from current directory (respects .sphereignore)
182
+ sphereai files add .
183
+
184
+ # Add a specific file
185
+ sphereai files add src/index.ts
186
+
187
+ # Add files from a specific directory
188
+ sphereai files add src/
189
+
190
+ # List tracked files
191
+ sphereai files list
192
+
193
+ # List tracked files with details
194
+ sphereai files list --verbose
195
+
196
+ # Show status of tracked files
197
+ sphereai files status
198
+
199
+ # Remove a file from tracking
200
+ sphereai files remove src/index.ts
201
+
202
+ # Clear all tracked files (with confirmation)
203
+ sphereai files clear --force
204
+
205
+ # Show .sphereignore patterns
206
+ sphereai files ignore
207
+
208
+ # Reload .sphereignore patterns
209
+ sphereai files ignore --reload
210
+ ```
211
+
212
+ #### .sphereignore File
213
+
214
+ The `.sphereignore` file works similar to `.gitignore`. Create a `.sphereignore` file in your project root to specify patterns for files that should be ignored when adding files to tracking.
215
+
216
+ **Example .sphereignore:**
217
+
218
+ ```gitignore
219
+ # Dependencies
220
+ node_modules/
221
+
222
+ # Build outputs
223
+ dist/
224
+ build/
225
+
226
+ # Environment variables
227
+ .env
228
+ .env.*
229
+
230
+ # Logs
231
+ *.log
232
+
233
+ # IDE
234
+ .vscode/
235
+ .idea/
236
+
237
+ # OS files
238
+ .DS_Store
239
+ ```
240
+
241
+ **Pattern Syntax:**
242
+
243
+ - Use `#` for comments
244
+ - Directory patterns: `node_modules/`
245
+ - Wildcard patterns: `*.log`, `*.env.*`
246
+ - Exact matches: `.DS_Store`
247
+
248
+ If no `.sphereignore` file exists, the following default patterns are used:
249
+
250
+ - `node_modules`, `.git`, `dist`, `build`
251
+ - `.env`, `.env.*`, `*.log`
252
+ - `.DS_Store`, `coverage`, `.vscode`, `.idea`
253
+
254
+ ## Global Options
255
+
256
+ All commands support these global options:
257
+
258
+ | Option | Description | Environment Variable |
259
+ |--------|-------------|---------------------|
260
+ | `--token <token>` | Override stored authentication token | `SPHEREAI_TOKEN` |
261
+ | `--output <format>` | Output format: `json` or `table` | - |
262
+
263
+ ## Environment Variables
264
+
265
+ | Variable | Description | Required | Default |
266
+ |----------|-------------|----------|---------|
267
+ | `SPHEREAI_API_URL` | Main API URL for resources (prompts, etc.) | Yes | - |
268
+ | `SPHEREAI_CLI_API_URL` | CLI API URL for key-based authentication | Yes (for login) | - |
269
+ | `SPHEREAI_TOKEN` | Override stored authentication token | No | - |
270
+ | `SPHEREAI_AUTH_URL` | OAuth2 authorization URL (legacy) | No | - |
271
+ | `SPHEREAI_OAUTH_CALLBACK_PORT` | OAuth2 callback port (legacy) | No | 3001 |
272
+
273
+ ### Examples
274
+
275
+ ```bash
276
+ # Set required environment variables
277
+ export SPHEREAI_API_URL="https://api.develop.sphereai.nava.com.br"
278
+ export SPHEREAI_CLI_API_URL="https://cli.api.develop.sphereai.nava.com.br"
279
+
280
+ # Use custom token
281
+ sphereai --token YOUR_TOKEN auth whoami
282
+
283
+ # JSON output
284
+ sphereai auth whoami --output json
285
+ ```
286
+
287
+ ## Configuration
288
+
289
+ ### Configuration File
290
+
291
+ The CLI stores configuration in: `~/.config/sphereai-cli/config.json`
292
+
293
+ This file contains:
294
+ - JWT authentication token
295
+ - API base URL
296
+ - OAuth2 authorization URL
297
+ - OAuth callback port (default: 3000)
298
+
299
+ **Note**: User information is extracted from the JWT token payload when needed, so it's always up-to-date with your current token.
300
+
301
+ ### View Configuration
302
+
303
+ ```bash
304
+ sphereai auth config
305
+ ```
306
+
307
+ ### Clear Configuration
308
+
309
+ ```bash
310
+ sphereai auth logout
311
+ ```
312
+
313
+ ## Output Formats
314
+
315
+ ### Table Format (Default)
316
+
317
+ Human-readable formatted tables with colors.
318
+
319
+ ```bash
320
+ sphereai auth whoami
321
+ ```
322
+
323
+ ### JSON Format
324
+
325
+ Machine-readable JSON output for scripting and automation.
326
+
327
+ ```bash
328
+ sphereai auth whoami --output json
329
+ ```
330
+
331
+ ## Error Handling
332
+
333
+ The CLI provides clear error messages for common issues:
334
+
335
+ - **401 Unauthorized**: Authentication required or token expired
336
+ - **403 Forbidden**: Insufficient permissions
337
+ - **404 Not Found**: Resource does not exist
338
+ - **400 Bad Request**: Validation errors with detailed field information
339
+ - **500 Internal Server Error**: Server-side issues
340
+
341
+ ### Debug Mode
342
+
343
+ Enable debug output for troubleshooting:
344
+
345
+ ```bash
346
+ DEBUG=1 sphereai auth whoami
347
+ ```
348
+
349
+ ## Examples
350
+
351
+ ### Complete Authentication Workflow
352
+
353
+ ```bash
354
+ # 1. Set API URLs (in .env file or environment variables)
355
+ export SPHEREAI_API_URL=https://api.develop.sphereai.nava.com.br
356
+ export SPHEREAI_CLI_API_URL=https://cli.api.develop.sphereai.nava.com.br
357
+
358
+ # 2. Authenticate using UserChannelKey
359
+ sphereai auth login YOUR_USER_CHANNEL_KEY
360
+
361
+ # 3. Check authentication
362
+ sphereai auth whoami
363
+
364
+ # 4. View configuration
365
+ sphereai auth config
366
+
367
+ # 5. Use JSON output
368
+ sphereai auth whoami --output json
369
+
370
+ # 6. Logout when done
371
+ sphereai auth logout
372
+ ```
373
+
374
+ ### Scripting with JSON Output
375
+
376
+ ```bash
377
+ #!/bin/bash
378
+
379
+ # Get current user info as JSON
380
+ user=$(sphereai auth whoami --output json)
381
+
382
+ # Parse with jq (requires jq to be installed)
383
+ user_id=$(echo "$user" | jq -r '.id')
384
+ user_email=$(echo "$user" | jq -r '.email')
385
+ user_role=$(echo "$user" | jq -r '.role')
386
+
387
+ echo "User ID: $user_id"
388
+ echo "Email: $user_email"
389
+ echo "Role: $user_role"
390
+ ```
391
+
392
+ ## Development
393
+
394
+ ### Prerequisites
395
+
396
+ - Node.js >= 14.0.0
397
+ - npm or yarn
398
+
399
+ ### Setup
400
+
401
+ ```bash
402
+ # Install dependencies
403
+ npm install
404
+
405
+ # Build
406
+ npm run build
407
+
408
+ # Development mode (watch for changes)
409
+ npm run dev
410
+
411
+ # Link for local testing
412
+ npm link
413
+ ```
414
+
415
+ ### Project Structure
416
+
417
+ ```
418
+ src/
419
+ ├── commands/ # CLI command implementations
420
+ │ ├── auth.ts # Authentication commands
421
+ │ ├── prompts.ts # Prompt management commands
422
+ │ └── files.ts # File tracking commands
423
+ ├── services/ # Core services
424
+ │ ├── api-client.ts # HTTP client wrapper
425
+ │ ├── cli-api-client.ts # CLI auth API client
426
+ │ ├── auth-service.ts # OAuth2 flow handler
427
+ │ ├── config-service.ts # Configuration management
428
+ │ └── file-tracking-service.ts # File tracking management
429
+ ├── types/ # TypeScript type definitions
430
+ │ └── api-types.ts # API type definitions
431
+ ├── utils/ # Utility functions
432
+ │ ├── formatters.ts # Output formatting
433
+ │ ├── errors.ts # Error handling
434
+ │ └── sphereignore.ts # .sphereignore parser
435
+ └── index.ts # CLI entry point
436
+ ```
437
+
438
+ ### Building
439
+
440
+ ```bash
441
+ # Clean build directory
442
+ npm run clean
443
+
444
+ # Compile TypeScript
445
+ npm run build
446
+
447
+ # The compiled output will be in the dist/ directory
448
+ ```
449
+
450
+ ## Troubleshooting
451
+
452
+ ### Port Already in Use
453
+
454
+ If OAuth2 callback port (3000) is in use:
455
+
456
+ ```bash
457
+ # The error message will suggest closing other instances
458
+ # Or you can change the port in config (future enhancement)
459
+ ```
460
+
461
+ ### Token Expired
462
+
463
+ ```bash
464
+ # Re-authenticate
465
+ sphereai auth login --email user@example.com --password secret
466
+ ```
467
+
468
+ ### Authentication Issues
469
+
470
+ ```bash
471
+ # Check current configuration
472
+ sphereai auth config
473
+
474
+ # Clear all authentication data and reconfigure
475
+ sphereai auth logout
476
+ sphereai auth set-auth-url https://your-auth-url.com/authorize
477
+ sphereai auth login
478
+ ```
479
+
480
+ ### Clear Configuration
481
+
482
+ ```bash
483
+ # Logout to clear all stored data
484
+ sphereai auth logout
485
+ ```
486
+
487
+ ## Contributing
488
+
489
+ 1. Fork the repository
490
+ 2. Create a feature branch
491
+ 3. Make your changes
492
+ 4. Run tests and build
493
+ 5. Submit a pull request
494
+
495
+ ## License
496
+
497
+ ISC
498
+
499
+ ## Support
500
+
501
+ For issues and questions, please refer to your SphereAI system administrator.
502
+
503
+ ## Security
504
+
505
+ ### JWT Token Storage
506
+ The CLI stores JWT tokens in your local configuration file (`~/.config/sphereai-cli/config.json`). The token contains encoded user information that is decoded locally when needed.
507
+
508
+ ### Token Claims
509
+ The JWT token includes the following claims that are used by the CLI:
510
+ - `sub` - User ID
511
+ - `customer_id` - Customer ID
512
+ - `unique_name` - User name
513
+ - `email` - User email
514
+ - `role` - User role (Admin/User)
515
+ - `iat` - Issued at timestamp
516
+ - `exp` - Expiration timestamp
517
+
518
+ The `whoami` command decodes these claims locally without making API calls, providing instant user information display.
519
+
520
+ ## Changelog
521
+
522
+ ### Version 1.0.0
523
+ - Initial release
524
+ - OAuth2 authentication support with browser-based flow
525
+ - JWT token-based user information (decoded locally)
526
+ - Credential-based authentication
527
+ - Token management (set, view, clear)
528
+ - JSON and table output formats
529
+ - Comprehensive error handling with user-friendly messages
@@ -0,0 +1,3 @@
1
+ import { Command } from 'commander';
2
+ export declare function registerAuthCommands(program: Command): void;
3
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/commands/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AASpC,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAyM3D"}