centaurus-cli 2.5.2 → 2.5.3

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 (246) hide show
  1. package/AUTH_FLOW.md +138 -0
  2. package/CONFIG_GUIDE.md +249 -0
  3. package/dist/config/models.d.ts +1 -1
  4. package/dist/config/models.d.ts.map +1 -1
  5. package/dist/config/models.js +2 -0
  6. package/dist/config/models.js.map +1 -1
  7. package/dist/ui/components/App.d.ts.map +1 -1
  8. package/dist/ui/components/App.js +104 -62
  9. package/dist/ui/components/App.js.map +1 -1
  10. package/dist/ui/components/FontRecommendation.d.ts +1 -0
  11. package/dist/ui/components/FontRecommendation.d.ts.map +1 -0
  12. package/dist/ui/components/FontRecommendation.js +1 -0
  13. package/dist/ui/components/FontRecommendation.js.map +1 -0
  14. package/dist/ui/components/InputBox.d.ts.map +1 -1
  15. package/dist/ui/components/InputBox.js +3 -2
  16. package/dist/ui/components/InputBox.js.map +1 -1
  17. package/dist/ui/components/MarkdownRenderer.d.ts.map +1 -1
  18. package/dist/ui/components/MarkdownRenderer.js +34 -9
  19. package/dist/ui/components/MarkdownRenderer.js.map +1 -1
  20. package/dist/ui/components/MessageDisplay.js +2 -2
  21. package/dist/ui/components/StreamingMessageDisplay.d.ts.map +1 -1
  22. package/dist/ui/components/StreamingMessageDisplay.js +3 -3
  23. package/dist/ui/components/StreamingMessageDisplay.js.map +1 -1
  24. package/dist/ui/components/ThinkingDisplay.d.ts +13 -0
  25. package/dist/ui/components/ThinkingDisplay.d.ts.map +1 -0
  26. package/dist/ui/components/ThinkingDisplay.js +41 -0
  27. package/dist/ui/components/ThinkingDisplay.js.map +1 -0
  28. package/dist/utils/version-checker.d.ts.map +1 -1
  29. package/dist/utils/version-checker.js +3 -31
  30. package/dist/utils/version-checker.js.map +1 -1
  31. package/package.json +5 -5
  32. package/dist/ai/provider-factory.d.ts +0 -6
  33. package/dist/ai/provider-factory.d.ts.map +0 -1
  34. package/dist/ai/provider-factory.js +0 -27
  35. package/dist/ai/provider-factory.js.map +0 -1
  36. package/dist/ai/providers/base.d.ts +0 -25
  37. package/dist/ai/providers/base.d.ts.map +0 -1
  38. package/dist/ai/providers/base.js +0 -9
  39. package/dist/ai/providers/base.js.map +0 -1
  40. package/dist/ai/providers/gemini.d.ts +0 -34
  41. package/dist/ai/providers/gemini.d.ts.map +0 -1
  42. package/dist/ai/providers/gemini.js +0 -146
  43. package/dist/ai/providers/gemini.js.map +0 -1
  44. package/dist/commands/view-duplication-logs.d.ts +0 -5
  45. package/dist/commands/view-duplication-logs.d.ts.map +0 -1
  46. package/dist/commands/view-duplication-logs.js +0 -14
  47. package/dist/commands/view-duplication-logs.js.map +0 -1
  48. package/dist/context/__tests__/command-detector.test.d.ts +0 -14
  49. package/dist/context/__tests__/command-detector.test.d.ts.map +0 -1
  50. package/dist/context/__tests__/command-detector.test.js +0 -318
  51. package/dist/context/__tests__/command-detector.test.js.map +0 -1
  52. package/dist/context/__tests__/context-manager.test.d.ts +0 -16
  53. package/dist/context/__tests__/context-manager.test.d.ts.map +0 -1
  54. package/dist/context/__tests__/context-manager.test.js +0 -375
  55. package/dist/context/__tests__/context-manager.test.js.map +0 -1
  56. package/dist/context/__tests__/error-handling.test.d.ts +0 -15
  57. package/dist/context/__tests__/error-handling.test.d.ts.map +0 -1
  58. package/dist/context/__tests__/error-handling.test.js +0 -447
  59. package/dist/context/__tests__/error-handling.test.js.map +0 -1
  60. package/dist/context/handlers/__tests__/docker-handler.test.d.ts +0 -13
  61. package/dist/context/handlers/__tests__/docker-handler.test.d.ts.map +0 -1
  62. package/dist/context/handlers/__tests__/docker-handler.test.js +0 -285
  63. package/dist/context/handlers/__tests__/docker-handler.test.js.map +0 -1
  64. package/dist/context/handlers/__tests__/ssh-handler.test.d.ts +0 -13
  65. package/dist/context/handlers/__tests__/ssh-handler.test.d.ts.map +0 -1
  66. package/dist/context/handlers/__tests__/ssh-handler.test.js +0 -251
  67. package/dist/context/handlers/__tests__/ssh-handler.test.js.map +0 -1
  68. package/dist/context/handlers/__tests__/wsl-handler.test.d.ts +0 -7
  69. package/dist/context/handlers/__tests__/wsl-handler.test.d.ts.map +0 -1
  70. package/dist/context/handlers/__tests__/wsl-handler.test.js +0 -331
  71. package/dist/context/handlers/__tests__/wsl-handler.test.js.map +0 -1
  72. package/dist/index-custom.d.ts +0 -3
  73. package/dist/index-custom.d.ts.map +0 -1
  74. package/dist/index-custom.js +0 -65
  75. package/dist/index-custom.js.map +0 -1
  76. package/dist/prompts/system-prompt.d.ts +0 -47
  77. package/dist/prompts/system-prompt.d.ts.map +0 -1
  78. package/dist/prompts/system-prompt.js +0 -377
  79. package/dist/prompts/system-prompt.js.map +0 -1
  80. package/dist/providers/GoogleProvider.d.ts +0 -26
  81. package/dist/providers/GoogleProvider.d.ts.map +0 -1
  82. package/dist/providers/GoogleProvider.js +0 -313
  83. package/dist/providers/GoogleProvider.js.map +0 -1
  84. package/dist/providers/Provider.d.ts +0 -114
  85. package/dist/providers/Provider.d.ts.map +0 -1
  86. package/dist/providers/Provider.js +0 -44
  87. package/dist/providers/Provider.js.map +0 -1
  88. package/dist/services/__tests__/ai-context-injector.test.d.ts +0 -15
  89. package/dist/services/__tests__/ai-context-injector.test.d.ts.map +0 -1
  90. package/dist/services/__tests__/ai-context-injector.test.js +0 -326
  91. package/dist/services/__tests__/ai-context-injector.test.js.map +0 -1
  92. package/dist/src/context/types.js +0 -27
  93. package/dist/src/services/ai-context-injector.js +0 -96
  94. package/dist/src/services/ai-service-client.js +0 -270
  95. package/dist/src/services/api-client.js +0 -349
  96. package/dist/src/tools/types.js +0 -1
  97. package/dist/src/types/index.js +0 -1
  98. package/dist/test/context/types.js +0 -27
  99. package/dist/test/services/__tests__/ai-context-injector.test.js +0 -325
  100. package/dist/test/services/ai-context-injector.js +0 -96
  101. package/dist/test/services/ai-service-client.js +0 -270
  102. package/dist/test/services/api-client.js +0 -349
  103. package/dist/test/tools/types.js +0 -1
  104. package/dist/test/types/index.js +0 -1
  105. package/dist/test-ai-context-injector.js +0 -97
  106. package/dist/tests/automated-verification.d.ts +0 -27
  107. package/dist/tests/automated-verification.d.ts.map +0 -1
  108. package/dist/tests/automated-verification.js +0 -359
  109. package/dist/tests/automated-verification.js.map +0 -1
  110. package/dist/tests/integration-tests.d.ts +0 -50
  111. package/dist/tests/integration-tests.d.ts.map +0 -1
  112. package/dist/tests/integration-tests.js +0 -648
  113. package/dist/tests/integration-tests.js.map +0 -1
  114. package/dist/tools/file-ops-test.d.ts +0 -6
  115. package/dist/tools/file-ops-test.d.ts.map +0 -1
  116. package/dist/tools/file-ops-test.js +0 -197
  117. package/dist/tools/file-ops-test.js.map +0 -1
  118. package/dist/ui/DisplayHistory.d.ts +0 -53
  119. package/dist/ui/DisplayHistory.d.ts.map +0 -1
  120. package/dist/ui/DisplayHistory.js +0 -82
  121. package/dist/ui/DisplayHistory.js.map +0 -1
  122. package/dist/ui/clack-ui.d.ts +0 -83
  123. package/dist/ui/clack-ui.d.ts.map +0 -1
  124. package/dist/ui/clack-ui.js +0 -304
  125. package/dist/ui/clack-ui.js.map +0 -1
  126. package/dist/ui/components/DisplayItemRenderer.d.ts +0 -18
  127. package/dist/ui/components/DisplayItemRenderer.d.ts.map +0 -1
  128. package/dist/ui/components/DisplayItemRenderer.js +0 -53
  129. package/dist/ui/components/DisplayItemRenderer.js.map +0 -1
  130. package/dist/ui/components/DynamicMessage.d.ts +0 -13
  131. package/dist/ui/components/DynamicMessage.d.ts.map +0 -1
  132. package/dist/ui/components/DynamicMessage.js +0 -27
  133. package/dist/ui/components/DynamicMessage.js.map +0 -1
  134. package/dist/ui/components/FileViewerScreen.d.ts +0 -14
  135. package/dist/ui/components/FileViewerScreen.d.ts.map +0 -1
  136. package/dist/ui/components/FileViewerScreen.js +0 -74
  137. package/dist/ui/components/FileViewerScreen.js.map +0 -1
  138. package/dist/ui/components/ScrollableContent.d.ts +0 -7
  139. package/dist/ui/components/ScrollableContent.d.ts.map +0 -1
  140. package/dist/ui/components/ScrollableContent.js +0 -6
  141. package/dist/ui/components/ScrollableContent.js.map +0 -1
  142. package/dist/ui/components/ScrollableMessageList.d.ts +0 -10
  143. package/dist/ui/components/ScrollableMessageList.d.ts.map +0 -1
  144. package/dist/ui/components/ScrollableMessageList.js +0 -133
  145. package/dist/ui/components/ScrollableMessageList.js.map +0 -1
  146. package/dist/ui/components/ScrollableScreen.d.ts +0 -9
  147. package/dist/ui/components/ScrollableScreen.d.ts.map +0 -1
  148. package/dist/ui/components/ScrollableScreen.js +0 -22
  149. package/dist/ui/components/ScrollableScreen.js.map +0 -1
  150. package/dist/ui/components/StaticMessageHistory.d.ts +0 -14
  151. package/dist/ui/components/StaticMessageHistory.d.ts.map +0 -1
  152. package/dist/ui/components/StaticMessageHistory.js +0 -19
  153. package/dist/ui/components/StaticMessageHistory.js.map +0 -1
  154. package/dist/ui/components/code-block.d.ts +0 -10
  155. package/dist/ui/components/code-block.d.ts.map +0 -1
  156. package/dist/ui/components/code-block.js +0 -74
  157. package/dist/ui/components/code-block.js.map +0 -1
  158. package/dist/ui/components/confirm-prompt.d.ts +0 -12
  159. package/dist/ui/components/confirm-prompt.d.ts.map +0 -1
  160. package/dist/ui/components/confirm-prompt.js +0 -104
  161. package/dist/ui/components/confirm-prompt.js.map +0 -1
  162. package/dist/ui/components/diff-viewer.d.ts +0 -9
  163. package/dist/ui/components/diff-viewer.d.ts.map +0 -1
  164. package/dist/ui/components/diff-viewer.js +0 -57
  165. package/dist/ui/components/diff-viewer.js.map +0 -1
  166. package/dist/ui/components/input-box.d.ts +0 -18
  167. package/dist/ui/components/input-box.d.ts.map +0 -1
  168. package/dist/ui/components/input-box.js +0 -157
  169. package/dist/ui/components/input-box.js.map +0 -1
  170. package/dist/ui/components/keyboard-help.d.ts +0 -7
  171. package/dist/ui/components/keyboard-help.d.ts.map +0 -1
  172. package/dist/ui/components/keyboard-help.js +0 -43
  173. package/dist/ui/components/keyboard-help.js.map +0 -1
  174. package/dist/ui/components/loading-indicator.d.ts +0 -3
  175. package/dist/ui/components/loading-indicator.d.ts.map +0 -1
  176. package/dist/ui/components/loading-indicator.js +0 -42
  177. package/dist/ui/components/loading-indicator.js.map +0 -1
  178. package/dist/ui/components/message-display.d.ts +0 -7
  179. package/dist/ui/components/message-display.d.ts.map +0 -1
  180. package/dist/ui/components/message-display.js +0 -104
  181. package/dist/ui/components/message-display.js.map +0 -1
  182. package/dist/ui/components/misc.d.ts +0 -28
  183. package/dist/ui/components/misc.d.ts.map +0 -1
  184. package/dist/ui/components/misc.js +0 -128
  185. package/dist/ui/components/misc.js.map +0 -1
  186. package/dist/ui/components/select-prompt.d.ts +0 -13
  187. package/dist/ui/components/select-prompt.d.ts.map +0 -1
  188. package/dist/ui/components/select-prompt.js +0 -42
  189. package/dist/ui/components/select-prompt.js.map +0 -1
  190. package/dist/ui/components/status-bar.d.ts +0 -11
  191. package/dist/ui/components/status-bar.d.ts.map +0 -1
  192. package/dist/ui/components/status-bar.js +0 -47
  193. package/dist/ui/components/status-bar.js.map +0 -1
  194. package/dist/ui/components/tool-execution.d.ts +0 -3
  195. package/dist/ui/components/tool-execution.d.ts.map +0 -1
  196. package/dist/ui/components/tool-execution.js +0 -374
  197. package/dist/ui/components/tool-execution.js.map +0 -1
  198. package/dist/ui/components/tool-result.d.ts +0 -11
  199. package/dist/ui/components/tool-result.d.ts.map +0 -1
  200. package/dist/ui/components/tool-result.js +0 -58
  201. package/dist/ui/components/tool-result.js.map +0 -1
  202. package/dist/ui/components/welcome-banner.d.ts +0 -3
  203. package/dist/ui/components/welcome-banner.d.ts.map +0 -1
  204. package/dist/ui/components/welcome-banner.js +0 -46
  205. package/dist/ui/components/welcome-banner.js.map +0 -1
  206. package/dist/ui/hooks/useDisplayHistory.d.ts +0 -13
  207. package/dist/ui/hooks/useDisplayHistory.d.ts.map +0 -1
  208. package/dist/ui/hooks/useDisplayHistory.js +0 -45
  209. package/dist/ui/hooks/useDisplayHistory.js.map +0 -1
  210. package/dist/ui/render-engine.d.ts +0 -69
  211. package/dist/ui/render-engine.d.ts.map +0 -1
  212. package/dist/ui/render-engine.js +0 -197
  213. package/dist/ui/render-engine.js.map +0 -1
  214. package/dist/ui/terminal/TerminalRenderer.d.ts +0 -84
  215. package/dist/ui/terminal/TerminalRenderer.d.ts.map +0 -1
  216. package/dist/ui/terminal/TerminalRenderer.js +0 -154
  217. package/dist/ui/terminal/TerminalRenderer.js.map +0 -1
  218. package/dist/ui/terminal/TerminalUI.d.ts +0 -139
  219. package/dist/ui/terminal/TerminalUI.d.ts.map +0 -1
  220. package/dist/ui/terminal/TerminalUI.js +0 -430
  221. package/dist/ui/terminal/TerminalUI.js.map +0 -1
  222. package/dist/ui/terminal/VirtualChatBuffer.d.ts +0 -32
  223. package/dist/ui/terminal/VirtualChatBuffer.d.ts.map +0 -1
  224. package/dist/ui/terminal/VirtualChatBuffer.js +0 -37
  225. package/dist/ui/terminal/VirtualChatBuffer.js.map +0 -1
  226. package/dist/ui/terminal-kit-base.d.ts +0 -117
  227. package/dist/ui/terminal-kit-base.d.ts.map +0 -1
  228. package/dist/ui/terminal-kit-base.js +0 -188
  229. package/dist/ui/terminal-kit-base.js.map +0 -1
  230. package/dist/ui/utils/duplication-detector.d.ts +0 -32
  231. package/dist/ui/utils/duplication-detector.d.ts.map +0 -1
  232. package/dist/ui/utils/duplication-detector.js +0 -227
  233. package/dist/ui/utils/duplication-detector.js.map +0 -1
  234. package/dist/ui/utils/duplication-logger.d.ts +0 -21
  235. package/dist/ui/utils/duplication-logger.d.ts.map +0 -1
  236. package/dist/ui/utils/duplication-logger.js +0 -85
  237. package/dist/ui/utils/duplication-logger.js.map +0 -1
  238. package/dist/ui/utils/terminal-scanner.d.ts +0 -19
  239. package/dist/ui/utils/terminal-scanner.d.ts.map +0 -1
  240. package/dist/ui/utils/terminal-scanner.js +0 -217
  241. package/dist/ui/utils/terminal-scanner.js.map +0 -1
  242. package/dist/version.d.ts +0 -2
  243. package/dist/version.d.ts.map +0 -1
  244. package/dist/version.js +0 -3
  245. package/dist/version.js.map +0 -1
  246. package/scripts/generate-version.js +0 -25
@@ -1,270 +0,0 @@
1
- /**
2
- * AI Service Client
3
- *
4
- * Handles communication with the backend AI proxy service for streaming
5
- * AI chat requests. Replaces direct Gemini SDK usage in the CLI.
6
- */
7
- import { apiClient } from './api-client.js';
8
- import { readFileSync, existsSync } from 'fs';
9
- import { join } from 'path';
10
- import { homedir } from 'os';
11
- /**
12
- * AI Service Client for streaming chat requests to backend
13
- */
14
- export class AIServiceClient {
15
- constructor() {
16
- this.maxRetries = 3;
17
- this.retryDelay = 1000; // Start with 1 second
18
- // Don't set baseURL yet - lazy load it when first used
19
- // This allows environment variables to be loaded first
20
- this.baseURL = '';
21
- }
22
- /**
23
- * Get the base URL for API requests
24
- * Lazy-loaded to ensure environment variables are loaded first
25
- */
26
- getBaseURL() {
27
- if (!this.baseURL) {
28
- // Use production URL by default, only use localhost in development mode
29
- this.baseURL = process.env.DEV_MODE === 'true'
30
- ? 'http://localhost:3002/api'
31
- : (process.env.BACKEND_URL || 'https://centaurus-backend-354715948975.asia-south1.run.app/api');
32
- }
33
- return this.baseURL;
34
- }
35
- /**
36
- * Stream chat request to backend AI proxy
37
- *
38
- * @param model - The AI model to use (e.g., 'gemini-2.5-flash')
39
- * @param messages - Conversation history including system, user, assistant, and tool messages
40
- * @param tools - Available tool schemas for the AI to use
41
- * @param environmentContext - Optional environment context (OS, shell, cwd, etc.)
42
- * @param mode - Optional mode (default, plan, command)
43
- * @yields Stream chunks (text, tool calls, done, or error events)
44
- */
45
- async *streamChat(model, messages, tools, environmentContext, mode) {
46
- // Build request payload
47
- const payload = {
48
- model,
49
- messages,
50
- tools,
51
- stream: true,
52
- environmentContext,
53
- mode,
54
- };
55
- // Get authentication token from api client
56
- if (!apiClient.isAuthenticated()) {
57
- yield {
58
- type: 'error',
59
- message: 'Authentication required. Please sign in.',
60
- code: 'AUTH_REQUIRED',
61
- };
62
- return;
63
- }
64
- // Retry logic for transient errors
65
- let lastError = null;
66
- for (let attempt = 0; attempt < this.maxRetries; attempt++) {
67
- try {
68
- // Make fetch request to backend AI endpoint
69
- const response = await fetch(`${this.getBaseURL()}/ai/chat`, {
70
- method: 'POST',
71
- headers: {
72
- 'Content-Type': 'application/json',
73
- 'Authorization': `Bearer ${this.getSessionToken()}`,
74
- },
75
- body: JSON.stringify(payload),
76
- signal: AbortSignal.timeout(60000), // 60 second timeout
77
- });
78
- // Check for HTTP errors
79
- if (!response.ok) {
80
- const errorText = await response.text();
81
- let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
82
- let errorCode = 'HTTP_ERROR';
83
- try {
84
- const errorData = JSON.parse(errorText);
85
- if (errorData.error) {
86
- errorMessage = errorData.error.message || errorMessage;
87
- errorCode = errorData.error.code || errorCode;
88
- }
89
- }
90
- catch {
91
- // If response is not JSON, use the text as message
92
- if (errorText) {
93
- errorMessage = errorText;
94
- }
95
- }
96
- // Handle specific error codes
97
- if (response.status === 401) {
98
- yield {
99
- type: 'error',
100
- message: 'Session expired. Please sign in again.',
101
- code: 'AUTH_REQUIRED',
102
- };
103
- return;
104
- }
105
- if (response.status === 429) {
106
- // Rate limit - don't retry immediately
107
- yield {
108
- type: 'error',
109
- message: 'Service temporarily unavailable due to high demand. Please try again in a moment.',
110
- code: 'RATE_LIMIT',
111
- };
112
- return;
113
- }
114
- if (response.status === 504) {
115
- // Timeout error - retryable
116
- lastError = {
117
- type: 'error',
118
- message: 'Request timed out. Retrying...',
119
- code: 'TIMEOUT',
120
- };
121
- if (attempt < this.maxRetries - 1) {
122
- await this.sleep(this.retryDelay * Math.pow(2, attempt));
123
- continue;
124
- }
125
- }
126
- // For other errors, yield and return
127
- yield {
128
- type: 'error',
129
- message: errorMessage,
130
- code: errorCode,
131
- };
132
- return;
133
- }
134
- // Check if response body exists
135
- if (!response.body) {
136
- yield {
137
- type: 'error',
138
- message: 'No response body from backend',
139
- code: 'NO_RESPONSE_BODY',
140
- };
141
- return;
142
- }
143
- // Parse SSE stream - if successful, we're done
144
- yield* this.parseSSEStream(response.body);
145
- return;
146
- }
147
- catch (error) {
148
- // Handle network errors
149
- if (error.name === 'TypeError' && error.message.includes('fetch')) {
150
- lastError = {
151
- type: 'error',
152
- message: 'Backend service is unreachable. Please check your connection.',
153
- code: 'NETWORK_ERROR',
154
- };
155
- }
156
- else if (error.name === 'AbortError' || error.name === 'TimeoutError') {
157
- lastError = {
158
- type: 'error',
159
- message: 'Request timed out. Please try again.',
160
- code: 'TIMEOUT',
161
- };
162
- }
163
- else {
164
- lastError = {
165
- type: 'error',
166
- message: error.message || 'Unknown error occurred',
167
- code: error.code || 'UNKNOWN_ERROR',
168
- };
169
- }
170
- // Retry for transient errors
171
- if (this.isRetryableError(lastError.code) && attempt < this.maxRetries - 1) {
172
- await this.sleep(this.retryDelay * Math.pow(2, attempt));
173
- continue;
174
- }
175
- // If not retryable or max retries reached, yield error and return
176
- break;
177
- }
178
- }
179
- // If we get here, we've exhausted retries
180
- if (lastError) {
181
- yield lastError;
182
- }
183
- }
184
- /**
185
- * Check if an error code is retryable
186
- */
187
- isRetryableError(code) {
188
- const retryableCodes = ['NETWORK_ERROR', 'TIMEOUT', 'UNKNOWN_ERROR'];
189
- return retryableCodes.includes(code);
190
- }
191
- /**
192
- * Sleep for specified milliseconds
193
- */
194
- sleep(ms) {
195
- return new Promise(resolve => setTimeout(resolve, ms));
196
- }
197
- /**
198
- * Get session token from apiClient
199
- * This is a workaround since sessionToken is private
200
- */
201
- getSessionToken() {
202
- // Read session token from the same location apiClient uses
203
- const configPath = join(homedir(), '.centaurus', 'session.json');
204
- try {
205
- if (existsSync(configPath)) {
206
- const data = readFileSync(configPath, 'utf-8');
207
- const session = JSON.parse(data);
208
- return session.sessionToken || '';
209
- }
210
- }
211
- catch (error) {
212
- // Return empty string if unable to read
213
- }
214
- return '';
215
- }
216
- /**
217
- * Parse Server-Sent Events stream from response body
218
- *
219
- * @param body - ReadableStream from fetch response
220
- * @yields Parsed stream chunks
221
- */
222
- async *parseSSEStream(body) {
223
- const reader = body.getReader();
224
- const decoder = new TextDecoder();
225
- let buffer = '';
226
- try {
227
- while (true) {
228
- const { done, value } = await reader.read();
229
- if (done) {
230
- break;
231
- }
232
- // Decode chunk and add to buffer
233
- buffer += decoder.decode(value, { stream: true });
234
- // Process complete lines in buffer
235
- const lines = buffer.split('\n');
236
- // Keep the last incomplete line in buffer
237
- buffer = lines.pop() || '';
238
- for (const line of lines) {
239
- // SSE format: "data: {json}"
240
- if (line.startsWith('data: ')) {
241
- const dataStr = line.slice(6); // Remove "data: " prefix
242
- // Skip empty data lines
243
- if (!dataStr.trim()) {
244
- continue;
245
- }
246
- try {
247
- const chunk = JSON.parse(dataStr);
248
- yield chunk;
249
- // Stop if we receive a done or error event
250
- if (chunk.type === 'done' || chunk.type === 'error') {
251
- return;
252
- }
253
- }
254
- catch (error) {
255
- // Skip malformed JSON
256
- console.error('Failed to parse SSE data:', dataStr);
257
- }
258
- }
259
- // SSE event type line: "event: chunk"
260
- // We don't need to process these separately since the data contains the type
261
- }
262
- }
263
- }
264
- finally {
265
- reader.releaseLock();
266
- }
267
- }
268
- }
269
- // Export singleton instance
270
- export const aiServiceClient = new AIServiceClient();
@@ -1,349 +0,0 @@
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();
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,27 +0,0 @@
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
- }