centaurus-cli 2.4.0 → 2.5.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 (153) hide show
  1. package/README.md +151 -1
  2. package/dist/cli-adapter.d.ts +41 -2
  3. package/dist/cli-adapter.d.ts.map +1 -1
  4. package/dist/cli-adapter.js +407 -79
  5. package/dist/cli-adapter.js.map +1 -1
  6. package/dist/config/types.d.ts +23 -0
  7. package/dist/config/types.d.ts.map +1 -1
  8. package/dist/config/types.js +20 -0
  9. package/dist/config/types.js.map +1 -1
  10. package/dist/context/__tests__/command-detector.test.d.ts +14 -0
  11. package/dist/context/__tests__/command-detector.test.d.ts.map +1 -0
  12. package/dist/context/__tests__/command-detector.test.js +318 -0
  13. package/dist/context/__tests__/command-detector.test.js.map +1 -0
  14. package/dist/context/__tests__/context-manager.test.d.ts +16 -0
  15. package/dist/context/__tests__/context-manager.test.d.ts.map +1 -0
  16. package/dist/context/__tests__/context-manager.test.js +375 -0
  17. package/dist/context/__tests__/context-manager.test.js.map +1 -0
  18. package/dist/context/__tests__/error-handling.test.d.ts +15 -0
  19. package/dist/context/__tests__/error-handling.test.d.ts.map +1 -0
  20. package/dist/context/__tests__/error-handling.test.js +447 -0
  21. package/dist/context/__tests__/error-handling.test.js.map +1 -0
  22. package/dist/context/command-detector.d.ts +50 -0
  23. package/dist/context/command-detector.d.ts.map +1 -0
  24. package/dist/context/command-detector.js +72 -0
  25. package/dist/context/command-detector.js.map +1 -0
  26. package/dist/context/context-manager.d.ts +144 -0
  27. package/dist/context/context-manager.d.ts.map +1 -0
  28. package/dist/context/context-manager.js +487 -0
  29. package/dist/context/context-manager.js.map +1 -0
  30. package/dist/context/handlers/__tests__/docker-handler.test.d.ts +13 -0
  31. package/dist/context/handlers/__tests__/docker-handler.test.d.ts.map +1 -0
  32. package/dist/context/handlers/__tests__/docker-handler.test.js +285 -0
  33. package/dist/context/handlers/__tests__/docker-handler.test.js.map +1 -0
  34. package/dist/context/handlers/__tests__/ssh-handler.test.d.ts +13 -0
  35. package/dist/context/handlers/__tests__/ssh-handler.test.d.ts.map +1 -0
  36. package/dist/context/handlers/__tests__/ssh-handler.test.js +251 -0
  37. package/dist/context/handlers/__tests__/ssh-handler.test.js.map +1 -0
  38. package/dist/context/handlers/__tests__/wsl-handler.test.d.ts +7 -0
  39. package/dist/context/handlers/__tests__/wsl-handler.test.d.ts.map +1 -0
  40. package/dist/context/handlers/__tests__/wsl-handler.test.js +331 -0
  41. package/dist/context/handlers/__tests__/wsl-handler.test.js.map +1 -0
  42. package/dist/context/handlers/docker-handler.d.ts +111 -0
  43. package/dist/context/handlers/docker-handler.d.ts.map +1 -0
  44. package/dist/context/handlers/docker-handler.js +439 -0
  45. package/dist/context/handlers/docker-handler.js.map +1 -0
  46. package/dist/context/handlers/ssh-handler.d.ts +120 -0
  47. package/dist/context/handlers/ssh-handler.d.ts.map +1 -0
  48. package/dist/context/handlers/ssh-handler.js +523 -0
  49. package/dist/context/handlers/ssh-handler.js.map +1 -0
  50. package/dist/context/handlers/wsl-handler.d.ts +128 -0
  51. package/dist/context/handlers/wsl-handler.d.ts.map +1 -0
  52. package/dist/context/handlers/wsl-handler.js +590 -0
  53. package/dist/context/handlers/wsl-handler.js.map +1 -0
  54. package/dist/context/index.d.ts +8 -0
  55. package/dist/context/index.d.ts.map +1 -0
  56. package/dist/context/index.js +7 -0
  57. package/dist/context/index.js.map +1 -0
  58. package/dist/context/subshell-handler.d.ts +130 -0
  59. package/dist/context/subshell-handler.d.ts.map +1 -0
  60. package/dist/context/subshell-handler.js +5 -0
  61. package/dist/context/subshell-handler.js.map +1 -0
  62. package/dist/context/types.d.ts +70 -0
  63. package/dist/context/types.d.ts.map +1 -0
  64. package/dist/context/types.js +34 -0
  65. package/dist/context/types.js.map +1 -0
  66. package/dist/index.js +6 -0
  67. package/dist/index.js.map +1 -1
  68. package/dist/services/__tests__/ai-context-injector.test.d.ts +15 -0
  69. package/dist/services/__tests__/ai-context-injector.test.d.ts.map +1 -0
  70. package/dist/services/__tests__/ai-context-injector.test.js +326 -0
  71. package/dist/services/__tests__/ai-context-injector.test.js.map +1 -0
  72. package/dist/services/ai-context-injector.d.ts +41 -0
  73. package/dist/services/ai-context-injector.d.ts.map +1 -0
  74. package/dist/services/ai-context-injector.js +97 -0
  75. package/dist/services/ai-context-injector.js.map +1 -0
  76. package/dist/services/ai-service-client.d.ts +4 -1
  77. package/dist/services/ai-service-client.d.ts.map +1 -1
  78. package/dist/services/ai-service-client.js +5 -1
  79. package/dist/services/ai-service-client.js.map +1 -1
  80. package/dist/src/context/types.js +27 -0
  81. package/dist/src/services/ai-context-injector.js +96 -0
  82. package/dist/src/services/ai-service-client.js +270 -0
  83. package/dist/src/services/api-client.js +349 -0
  84. package/dist/src/tools/types.js +1 -0
  85. package/dist/src/types/index.js +1 -0
  86. package/dist/test/context/types.js +27 -0
  87. package/dist/test/services/__tests__/ai-context-injector.test.js +325 -0
  88. package/dist/test/services/ai-context-injector.js +96 -0
  89. package/dist/test/services/ai-service-client.js +270 -0
  90. package/dist/test/services/api-client.js +349 -0
  91. package/dist/test/tools/types.js +1 -0
  92. package/dist/test/types/index.js +1 -0
  93. package/dist/test-ai-context-injector.js +97 -0
  94. package/dist/test-ssh-handler.d.ts +8 -0
  95. package/dist/test-ssh-handler.d.ts.map +1 -0
  96. package/dist/test-ssh-handler.js +198 -0
  97. package/dist/test-ssh-handler.js.map +1 -0
  98. package/dist/tools/command.d.ts.map +1 -1
  99. package/dist/tools/command.js +123 -46
  100. package/dist/tools/command.js.map +1 -1
  101. package/dist/tools/file-ops.d.ts.map +1 -1
  102. package/dist/tools/file-ops.js +115 -48
  103. package/dist/tools/file-ops.js.map +1 -1
  104. package/dist/tools/types.d.ts +1 -0
  105. package/dist/tools/types.d.ts.map +1 -1
  106. package/dist/types/index.d.ts +41 -0
  107. package/dist/types/index.d.ts.map +1 -1
  108. package/dist/ui/components/App.d.ts +3 -0
  109. package/dist/ui/components/App.d.ts.map +1 -1
  110. package/dist/ui/components/App.js +213 -46
  111. package/dist/ui/components/App.js.map +1 -1
  112. package/dist/ui/components/Breadcrumbs.d.ts +12 -0
  113. package/dist/ui/components/Breadcrumbs.d.ts.map +1 -0
  114. package/dist/ui/components/Breadcrumbs.js +62 -0
  115. package/dist/ui/components/Breadcrumbs.js.map +1 -0
  116. package/dist/ui/components/CodeBlock.js +1 -1
  117. package/dist/ui/components/CodeBlock.js.map +1 -1
  118. package/dist/ui/components/DiffViewer.js +1 -1
  119. package/dist/ui/components/DiffViewer.js.map +1 -1
  120. package/dist/ui/components/FileViewerScreen.d.ts +14 -0
  121. package/dist/ui/components/FileViewerScreen.d.ts.map +1 -0
  122. package/dist/ui/components/FileViewerScreen.js +74 -0
  123. package/dist/ui/components/FileViewerScreen.js.map +1 -0
  124. package/dist/ui/components/InputBox.d.ts +2 -0
  125. package/dist/ui/components/InputBox.d.ts.map +1 -1
  126. package/dist/ui/components/InputBox.js +85 -41
  127. package/dist/ui/components/InputBox.js.map +1 -1
  128. package/dist/ui/components/MessageDisplay.d.ts.map +1 -1
  129. package/dist/ui/components/MessageDisplay.js +3 -28
  130. package/dist/ui/components/MessageDisplay.js.map +1 -1
  131. package/dist/ui/components/PasswordPrompt.d.ts +9 -0
  132. package/dist/ui/components/PasswordPrompt.d.ts.map +1 -0
  133. package/dist/ui/components/PasswordPrompt.js +20 -0
  134. package/dist/ui/components/PasswordPrompt.js.map +1 -0
  135. package/dist/ui/components/StatusBar.d.ts +2 -0
  136. package/dist/ui/components/StatusBar.d.ts.map +1 -1
  137. package/dist/ui/components/StatusBar.js +36 -1
  138. package/dist/ui/components/StatusBar.js.map +1 -1
  139. package/dist/ui/components/ToolExecutionMessage.d.ts.map +1 -1
  140. package/dist/ui/components/ToolExecutionMessage.js +13 -24
  141. package/dist/ui/components/ToolExecutionMessage.js.map +1 -1
  142. package/dist/ui/components/VersionUpdatePrompt.d.ts +10 -0
  143. package/dist/ui/components/VersionUpdatePrompt.d.ts.map +1 -0
  144. package/dist/ui/components/VersionUpdatePrompt.js +41 -0
  145. package/dist/ui/components/VersionUpdatePrompt.js.map +1 -0
  146. package/dist/utils/shell.d.ts.map +1 -1
  147. package/dist/utils/shell.js +38 -10
  148. package/dist/utils/shell.js.map +1 -1
  149. package/dist/utils/version-checker.d.ts +14 -0
  150. package/dist/utils/version-checker.d.ts.map +1 -0
  151. package/dist/utils/version-checker.js +63 -0
  152. package/dist/utils/version-checker.js.map +1 -0
  153. package/package.json +71 -69
@@ -0,0 +1,349 @@
1
+ /**
2
+ * API Client Service for Centaurus CLI
3
+ *
4
+ * Handles all communication with the backend REST API including:
5
+ * - Authentication and session management
6
+ * - Conversation and message operations
7
+ * - User settings management
8
+ * - API key storage and retrieval
9
+ */
10
+ import axios from 'axios';
11
+ import { readFileSync, writeFileSync, mkdirSync, existsSync, unlinkSync } from 'fs';
12
+ import { join } from 'path';
13
+ import { homedir } from 'os';
14
+ /**
15
+ * API Client class for communicating with the backend service
16
+ */
17
+ class ApiClient {
18
+ constructor() {
19
+ this.client = null;
20
+ this.sessionToken = null;
21
+ // Set up session storage path: ~/.centaurus/session.json
22
+ this.configDir = join(homedir(), '.centaurus');
23
+ this.configPath = join(this.configDir, 'session.json');
24
+ // Load existing session if available
25
+ this.loadSession();
26
+ // Don't create axios client yet - wait until first use
27
+ // This allows environment variables to be loaded first
28
+ }
29
+ /**
30
+ * Get or create the axios client instance
31
+ * This is lazy-loaded to ensure environment variables are loaded first
32
+ */
33
+ getClient() {
34
+ if (!this.client) {
35
+ // Create axios instance with base configuration
36
+ // Use production URL by default, only use localhost in development mode
37
+ const baseURL = process.env.DEV_MODE === 'true'
38
+ ? 'http://localhost:3002/api'
39
+ : (process.env.BACKEND_URL || 'https://centaurus-backend-354715948975.asia-south1.run.app/api');
40
+ this.client = axios.create({
41
+ baseURL,
42
+ timeout: 30000,
43
+ headers: {
44
+ 'Content-Type': 'application/json',
45
+ },
46
+ });
47
+ // Request interceptor: Add Authorization header if session token exists
48
+ this.getClient().interceptors.request.use((config) => {
49
+ if (this.sessionToken) {
50
+ config.headers.Authorization = `Bearer ${this.sessionToken}`;
51
+ }
52
+ return config;
53
+ }, (error) => {
54
+ return Promise.reject(error);
55
+ });
56
+ // Response interceptor: Handle 401 errors (expired/invalid session)
57
+ this.getClient().interceptors.response.use((response) => response, async (error) => {
58
+ if (error.response?.status === 401) {
59
+ // Clear invalid session
60
+ this.clearSession();
61
+ // Create a more user-friendly error
62
+ const authError = new Error('Session expired. Please sign in again.');
63
+ authError.name = 'AuthenticationError';
64
+ throw authError;
65
+ }
66
+ // For other errors, extract message from API response if available
67
+ if (error.response?.data) {
68
+ const apiError = error.response.data;
69
+ if (apiError.error) {
70
+ const customError = new Error(apiError.error.message);
71
+ customError.name = apiError.error.code;
72
+ throw customError;
73
+ }
74
+ }
75
+ throw error;
76
+ });
77
+ }
78
+ return this.client;
79
+ }
80
+ /**
81
+ * Load session token from local config file
82
+ */
83
+ loadSession() {
84
+ try {
85
+ if (existsSync(this.configPath)) {
86
+ const data = readFileSync(this.configPath, 'utf-8');
87
+ const session = JSON.parse(data);
88
+ this.sessionToken = session.sessionToken || null;
89
+ }
90
+ }
91
+ catch (error) {
92
+ // If there's any error reading the session, just start fresh
93
+ this.sessionToken = null;
94
+ }
95
+ }
96
+ /**
97
+ * Save session token to local config file
98
+ */
99
+ saveSession(token, expiresAt) {
100
+ try {
101
+ // Ensure config directory exists
102
+ if (!existsSync(this.configDir)) {
103
+ mkdirSync(this.configDir, { recursive: true });
104
+ }
105
+ // Save session data
106
+ const sessionData = {
107
+ sessionToken: token,
108
+ expiresAt: expiresAt || null,
109
+ savedAt: new Date().toISOString(),
110
+ };
111
+ writeFileSync(this.configPath, JSON.stringify(sessionData, null, 2), 'utf-8');
112
+ this.sessionToken = token;
113
+ }
114
+ catch (error) {
115
+ console.error('Failed to save session:', error);
116
+ throw new Error('Failed to save session locally');
117
+ }
118
+ }
119
+ /**
120
+ * Clear session token from memory and local storage
121
+ */
122
+ clearSession() {
123
+ this.sessionToken = null;
124
+ try {
125
+ if (existsSync(this.configPath)) {
126
+ unlinkSync(this.configPath);
127
+ }
128
+ }
129
+ catch (error) {
130
+ // Ignore errors when clearing session
131
+ }
132
+ }
133
+ /**
134
+ * Check if user is authenticated
135
+ */
136
+ isAuthenticated() {
137
+ return this.sessionToken !== null;
138
+ }
139
+ // ==================== Authentication Methods ====================
140
+ /**
141
+ * Initialize Google OAuth flow
142
+ * @param redirectUri - The URI to redirect to after OAuth
143
+ * @returns OAuth URL and state parameter
144
+ */
145
+ async initGoogleAuth(redirectUri) {
146
+ const response = await this.getClient().post('/auth/google/init', { redirectUri });
147
+ return response.data.data;
148
+ }
149
+ /**
150
+ * Complete Google OAuth authentication
151
+ * @param code - Authorization code from Google
152
+ * @param state - State parameter for CSRF protection
153
+ * @returns Session token and user information
154
+ */
155
+ async authenticate(code, state) {
156
+ const response = await this.getClient().post('/auth/google/callback', { code, state });
157
+ const authData = response.data.data;
158
+ this.saveSession(authData.sessionToken, authData.expiresAt);
159
+ return authData;
160
+ }
161
+ /**
162
+ * Set session token directly (used when receiving token from web app)
163
+ * @param sessionToken - The session token to save
164
+ * @param user - User information
165
+ */
166
+ setSessionToken(sessionToken, user) {
167
+ // Calculate expiration (30 days from now)
168
+ const expiresAt = new Date();
169
+ expiresAt.setDate(expiresAt.getDate() + 30);
170
+ this.saveSession(sessionToken, expiresAt.toISOString());
171
+ }
172
+ /**
173
+ * Refresh the current session token
174
+ * @returns New session token and expiration
175
+ */
176
+ async refreshSession() {
177
+ const response = await this.getClient().post('/auth/refresh');
178
+ const refreshData = response.data.data;
179
+ this.saveSession(refreshData.sessionToken, refreshData.expiresAt);
180
+ return refreshData;
181
+ }
182
+ /**
183
+ * Logout and invalidate current session
184
+ */
185
+ async logout() {
186
+ try {
187
+ await this.getClient().post('/auth/logout');
188
+ }
189
+ finally {
190
+ // Always clear local session, even if API call fails
191
+ this.clearSession();
192
+ }
193
+ }
194
+ /**
195
+ * Get current authenticated user profile
196
+ * @returns User profile information
197
+ */
198
+ async getCurrentUser() {
199
+ const response = await this.getClient().get('/auth/me');
200
+ return response.data.data;
201
+ }
202
+ // ==================== Conversation Methods ====================
203
+ /**
204
+ * Create a new conversation
205
+ * @param data - Conversation creation parameters
206
+ * @returns Created conversation
207
+ */
208
+ async createConversation(data) {
209
+ const response = await this.getClient().post('/conversations', data);
210
+ return response.data.data;
211
+ }
212
+ /**
213
+ * Get all conversations for the authenticated user
214
+ * @param params - Pagination and filter parameters
215
+ * @returns List of conversations with pagination metadata
216
+ */
217
+ async getConversations(params) {
218
+ const queryParams = {
219
+ page: params?.page || 1,
220
+ limit: params?.limit || 20,
221
+ };
222
+ if (params?.includeArchived !== undefined) {
223
+ queryParams.includeArchived = params.includeArchived;
224
+ }
225
+ if (params?.tags && params.tags.length > 0) {
226
+ queryParams.tags = params.tags.join(',');
227
+ }
228
+ const response = await this.getClient().get('/conversations', { params: queryParams });
229
+ return response.data;
230
+ }
231
+ /**
232
+ * Get a specific conversation by ID
233
+ * @param conversationId - The conversation ID
234
+ * @returns Conversation details
235
+ */
236
+ async getConversation(conversationId) {
237
+ const response = await this.getClient().get(`/conversations/${conversationId}`);
238
+ return response.data.data;
239
+ }
240
+ /**
241
+ * Update a conversation
242
+ * @param conversationId - The conversation ID
243
+ * @param data - Fields to update
244
+ * @returns Updated conversation
245
+ */
246
+ async updateConversation(conversationId, data) {
247
+ const response = await this.getClient().put(`/conversations/${conversationId}`, data);
248
+ return response.data.data;
249
+ }
250
+ /**
251
+ * Delete (archive) a conversation
252
+ * @param conversationId - The conversation ID
253
+ */
254
+ async deleteConversation(conversationId) {
255
+ await this.getClient().delete(`/conversations/${conversationId}`);
256
+ }
257
+ // ==================== Message Methods ====================
258
+ /**
259
+ * Add a message to a conversation
260
+ * @param conversationId - The conversation ID
261
+ * @param message - Message data
262
+ * @returns Created message
263
+ */
264
+ async addMessage(conversationId, message) {
265
+ const response = await this.getClient().post(`/conversations/${conversationId}/messages`, message);
266
+ return response.data.data;
267
+ }
268
+ /**
269
+ * Get all messages for a conversation
270
+ * @param conversationId - The conversation ID
271
+ * @param params - Pagination parameters
272
+ * @returns List of messages with pagination metadata
273
+ */
274
+ async getMessages(conversationId, params) {
275
+ const queryParams = {
276
+ page: params?.page || 1,
277
+ limit: params?.limit || 50,
278
+ };
279
+ const response = await this.getClient().get(`/conversations/${conversationId}/messages`, { params: queryParams });
280
+ return response.data;
281
+ }
282
+ // ==================== Settings Methods ====================
283
+ /**
284
+ * Get user settings
285
+ * @returns User settings object
286
+ */
287
+ async getSettings() {
288
+ const response = await this.getClient().get('/settings');
289
+ return response.data.data;
290
+ }
291
+ /**
292
+ * Update user settings
293
+ * @param settings - Settings to update (partial update supported)
294
+ * @returns Updated settings
295
+ */
296
+ async updateSettings(settings) {
297
+ const response = await this.getClient().put('/settings', settings);
298
+ return response.data.data;
299
+ }
300
+ // ==================== API Key Methods ====================
301
+ /**
302
+ * Store an encrypted API key
303
+ * @param data - API key data including provider, name, and key value
304
+ * @returns API key metadata (without the actual key)
305
+ */
306
+ async storeApiKey(data) {
307
+ const response = await this.getClient().post('/api-keys', data);
308
+ return response.data.data;
309
+ }
310
+ /**
311
+ * Get all API keys (metadata only, not actual keys)
312
+ * @returns List of API key metadata
313
+ */
314
+ async getApiKeys() {
315
+ const response = await this.getClient().get('/api-keys');
316
+ return response.data.data;
317
+ }
318
+ /**
319
+ * Decrypt and retrieve an API key for a specific provider
320
+ * @param provider - The provider name
321
+ * @returns Decrypted API key
322
+ */
323
+ async decryptApiKey(provider) {
324
+ const response = await this.getClient().get(`/api-keys/${provider}/decrypt`);
325
+ return response.data.data;
326
+ }
327
+ /**
328
+ * Delete an API key
329
+ * @param keyId - The API key ID
330
+ */
331
+ async deleteApiKey(keyId) {
332
+ await this.getClient().delete(`/api-keys/${keyId}`);
333
+ }
334
+ // ==================== Health Check ====================
335
+ /**
336
+ * Check backend service health
337
+ * @returns Health status information
338
+ */
339
+ async healthCheck() {
340
+ // Health endpoint is at root level, not under /api
341
+ // So we need to construct the full URL manually
342
+ const baseURL = process.env.BACKEND_API_URL || 'http://localhost:3000/api';
343
+ const healthURL = baseURL.replace('/api', '/health');
344
+ const response = await axios.get(healthURL);
345
+ return response.data.data;
346
+ }
347
+ }
348
+ // Export singleton instance
349
+ export const apiClient = new ApiClient();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Core types for the subshell context management system
3
+ */
4
+ /**
5
+ * Error thrown when a subshell connection fails
6
+ */
7
+ export class SubshellConnectionError extends Error {
8
+ constructor(type, reason, recoverable) {
9
+ super(`${type} connection failed: ${reason}`);
10
+ this.type = type;
11
+ this.reason = reason;
12
+ this.recoverable = recoverable;
13
+ this.name = 'SubshellConnectionError';
14
+ }
15
+ }
16
+ /**
17
+ * Error thrown when a subshell command execution fails
18
+ */
19
+ export class SubshellExecutionError extends Error {
20
+ constructor(command, reason, exitCode) {
21
+ super(`Command execution failed: ${reason}`);
22
+ this.command = command;
23
+ this.reason = reason;
24
+ this.exitCode = exitCode;
25
+ this.name = 'SubshellExecutionError';
26
+ }
27
+ }