baseguard 1.0.3 → 1.0.5

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 (169) hide show
  1. package/.baseguardrc.example.json +63 -63
  2. package/.eslintrc.json +24 -24
  3. package/.prettierrc +7 -7
  4. package/CHANGELOG.md +195 -195
  5. package/DEPLOYMENT.md +624 -624
  6. package/DEPLOYMENT_CHECKLIST.md +239 -239
  7. package/DEPLOYMENT_SUMMARY_v1.0.2.md +202 -202
  8. package/QUICK_START.md +134 -134
  9. package/README.md +488 -488
  10. package/RELEASE_NOTES_v1.0.2.md +434 -434
  11. package/bin/base.js +627 -627
  12. package/dist/ai/fix-manager.d.ts.map +1 -1
  13. package/dist/ai/fix-manager.js +1 -1
  14. package/dist/ai/fix-manager.js.map +1 -1
  15. package/dist/ai/gemini-analyzer.d.ts.map +1 -1
  16. package/dist/ai/gemini-analyzer.js +29 -35
  17. package/dist/ai/gemini-analyzer.js.map +1 -1
  18. package/dist/ai/gemini-code-fixer.d.ts.map +1 -1
  19. package/dist/ai/gemini-code-fixer.js +58 -58
  20. package/dist/ai/gemini-code-fixer.js.map +1 -1
  21. package/dist/ai/jules-implementer.d.ts +3 -0
  22. package/dist/ai/jules-implementer.d.ts.map +1 -1
  23. package/dist/ai/jules-implementer.js +63 -32
  24. package/dist/ai/jules-implementer.js.map +1 -1
  25. package/dist/ai/unified-code-fixer.js.map +1 -1
  26. package/dist/commands/check.d.ts.map +1 -1
  27. package/dist/commands/check.js +1 -1
  28. package/dist/commands/check.js.map +1 -1
  29. package/dist/commands/config.js +2 -1
  30. package/dist/commands/config.js.map +1 -1
  31. package/dist/commands/fix.d.ts.map +1 -1
  32. package/dist/commands/fix.js +48 -15
  33. package/dist/commands/fix.js.map +1 -1
  34. package/dist/core/api-key-manager.js +2 -2
  35. package/dist/core/api-key-manager.js.map +1 -1
  36. package/dist/core/baseguard.d.ts +1 -0
  37. package/dist/core/baseguard.d.ts.map +1 -1
  38. package/dist/core/baseguard.js +13 -10
  39. package/dist/core/baseguard.js.map +1 -1
  40. package/dist/core/baseline-checker.d.ts.map +1 -1
  41. package/dist/core/baseline-checker.js +8 -5
  42. package/dist/core/baseline-checker.js.map +1 -1
  43. package/dist/core/configuration-recovery.d.ts.map +1 -1
  44. package/dist/core/configuration-recovery.js +1 -1
  45. package/dist/core/configuration-recovery.js.map +1 -1
  46. package/dist/core/debug-logger.d.ts.map +1 -1
  47. package/dist/core/debug-logger.js +1 -1
  48. package/dist/core/debug-logger.js.map +1 -1
  49. package/dist/core/error-handler.d.ts.map +1 -1
  50. package/dist/core/error-handler.js +2 -1
  51. package/dist/core/error-handler.js.map +1 -1
  52. package/dist/core/gitignore-manager.js +5 -5
  53. package/dist/core/graceful-degradation-manager.d.ts.map +1 -1
  54. package/dist/core/graceful-degradation-manager.js +16 -16
  55. package/dist/core/graceful-degradation-manager.js.map +1 -1
  56. package/dist/core/lazy-loader.d.ts.map +1 -1
  57. package/dist/core/lazy-loader.js +9 -2
  58. package/dist/core/lazy-loader.js.map +1 -1
  59. package/dist/core/memory-manager.d.ts +0 -3
  60. package/dist/core/memory-manager.d.ts.map +1 -1
  61. package/dist/core/memory-manager.js.map +1 -1
  62. package/dist/core/parser-worker.d.ts +2 -0
  63. package/dist/core/parser-worker.d.ts.map +1 -0
  64. package/dist/core/parser-worker.js +19 -0
  65. package/dist/core/parser-worker.js.map +1 -0
  66. package/dist/core/startup-optimizer.d.ts.map +1 -1
  67. package/dist/core/startup-optimizer.js +4 -8
  68. package/dist/core/startup-optimizer.js.map +1 -1
  69. package/dist/core/system-error-handler.d.ts.map +1 -1
  70. package/dist/core/system-error-handler.js.map +1 -1
  71. package/dist/git/automation-engine.d.ts.map +1 -1
  72. package/dist/git/automation-engine.js +5 -4
  73. package/dist/git/automation-engine.js.map +1 -1
  74. package/dist/git/github-manager.d.ts.map +1 -1
  75. package/dist/git/github-manager.js.map +1 -1
  76. package/dist/git/hook-manager.js +5 -5
  77. package/dist/git/hook-manager.js.map +1 -1
  78. package/dist/parsers/parser-manager.d.ts.map +1 -1
  79. package/dist/parsers/parser-manager.js +1 -1
  80. package/dist/parsers/parser-manager.js.map +1 -1
  81. package/dist/parsers/svelte-parser.js +1 -1
  82. package/dist/parsers/svelte-parser.js.map +1 -1
  83. package/dist/parsers/vanilla-parser.d.ts.map +1 -1
  84. package/dist/parsers/vanilla-parser.js.map +1 -1
  85. package/dist/parsers/vue-parser.d.ts.map +1 -1
  86. package/dist/parsers/vue-parser.js.map +1 -1
  87. package/dist/ui/components.d.ts +1 -1
  88. package/dist/ui/components.d.ts.map +1 -1
  89. package/dist/ui/components.js +11 -11
  90. package/dist/ui/components.js.map +1 -1
  91. package/dist/ui/terminal-header.js +14 -14
  92. package/package.json +105 -105
  93. package/src/ai/__tests__/gemini-analyzer.test.ts +180 -180
  94. package/src/ai/agentkit-orchestrator.ts +533 -533
  95. package/src/ai/fix-manager.ts +362 -362
  96. package/src/ai/gemini-analyzer.ts +665 -671
  97. package/src/ai/gemini-code-fixer.ts +539 -540
  98. package/src/ai/index.ts +3 -3
  99. package/src/ai/jules-implementer.ts +504 -460
  100. package/src/ai/unified-code-fixer.ts +347 -347
  101. package/src/commands/automation.ts +343 -343
  102. package/src/commands/check.ts +298 -299
  103. package/src/commands/config.ts +584 -583
  104. package/src/commands/fix.ts +269 -238
  105. package/src/commands/index.ts +6 -6
  106. package/src/commands/init.ts +155 -155
  107. package/src/commands/status.ts +306 -306
  108. package/src/core/api-key-manager.ts +298 -298
  109. package/src/core/baseguard.ts +757 -756
  110. package/src/core/baseline-checker.ts +566 -563
  111. package/src/core/cache-manager.ts +271 -271
  112. package/src/core/configuration-recovery.ts +672 -673
  113. package/src/core/configuration.ts +595 -595
  114. package/src/core/debug-logger.ts +590 -590
  115. package/src/core/directory-filter.ts +420 -420
  116. package/src/core/error-handler.ts +518 -517
  117. package/src/core/file-processor.ts +337 -337
  118. package/src/core/gitignore-manager.ts +168 -168
  119. package/src/core/graceful-degradation-manager.ts +596 -596
  120. package/src/core/index.ts +16 -16
  121. package/src/core/lazy-loader.ts +317 -307
  122. package/src/core/memory-manager.ts +290 -295
  123. package/src/core/parser-worker.ts +33 -0
  124. package/src/core/startup-optimizer.ts +246 -255
  125. package/src/core/system-error-handler.ts +755 -756
  126. package/src/git/automation-engine.ts +361 -361
  127. package/src/git/github-manager.ts +190 -192
  128. package/src/git/hook-manager.ts +210 -210
  129. package/src/git/index.ts +3 -3
  130. package/src/index.ts +7 -7
  131. package/src/parsers/feature-validator.ts +558 -558
  132. package/src/parsers/index.ts +7 -7
  133. package/src/parsers/parser-manager.ts +418 -419
  134. package/src/parsers/parser.ts +25 -25
  135. package/src/parsers/react-parser-optimized.ts +160 -160
  136. package/src/parsers/react-parser.ts +358 -358
  137. package/src/parsers/svelte-parser.ts +510 -510
  138. package/src/parsers/vanilla-parser.ts +685 -686
  139. package/src/parsers/vue-parser.ts +476 -478
  140. package/src/types/index.ts +95 -95
  141. package/src/ui/components.ts +567 -567
  142. package/src/ui/help.ts +192 -192
  143. package/src/ui/index.ts +3 -3
  144. package/src/ui/prompts.ts +680 -680
  145. package/src/ui/terminal-header.ts +58 -58
  146. package/test-build.js +40 -40
  147. package/test-config-commands.js +55 -55
  148. package/test-header-simple.js +32 -32
  149. package/test-terminal-header.js +11 -11
  150. package/test-ui.js +28 -28
  151. package/tests/e2e/baseguard.e2e.test.ts +515 -515
  152. package/tests/e2e/cross-platform.e2e.test.ts +419 -419
  153. package/tests/e2e/git-integration.e2e.test.ts +486 -486
  154. package/tests/fixtures/react-project/package.json +13 -13
  155. package/tests/fixtures/react-project/src/App.css +75 -75
  156. package/tests/fixtures/react-project/src/App.tsx +76 -76
  157. package/tests/fixtures/svelte-project/package.json +10 -10
  158. package/tests/fixtures/svelte-project/src/App.svelte +368 -368
  159. package/tests/fixtures/vanilla-project/index.html +75 -75
  160. package/tests/fixtures/vanilla-project/script.js +330 -330
  161. package/tests/fixtures/vanilla-project/styles.css +358 -358
  162. package/tests/fixtures/vue-project/package.json +11 -11
  163. package/tests/fixtures/vue-project/src/App.vue +215 -215
  164. package/tmp-smoke/.baseguard/backups/config-2026-02-19T12-04-11-067Z-auto.json +30 -0
  165. package/tmp-smoke/src/bad.css +3 -0
  166. package/tsconfig.json +34 -34
  167. package/vitest.config.ts +11 -11
  168. package/dist/terminal-header.d.ts +0 -12
  169. package/dist/terminal-header.js +0 -45
@@ -1,298 +1,298 @@
1
- import { ConfigurationManager } from './configuration.js';
2
- import { UIComponents } from '../ui/components.js';
3
-
4
- /**
5
- * API key validation patterns and utilities
6
- */
7
- export class ApiKeyManager {
8
- /**
9
- * Validate Jules API key format
10
- */
11
- static validateJulesApiKey(apiKey: string): { valid: boolean; error?: string } {
12
- if (!apiKey || typeof apiKey !== 'string') {
13
- return { valid: false, error: 'API key is required' };
14
- }
15
-
16
- const trimmed = apiKey.trim();
17
-
18
- if (trimmed.length < 10) {
19
- return { valid: false, error: 'API key seems too short' };
20
- }
21
-
22
- if (trimmed.length > 200) {
23
- return { valid: false, error: 'API key seems too long' };
24
- }
25
-
26
- // Basic format validation - Jules keys might have specific patterns
27
- if (!/^[A-Za-z0-9_\-\.]+$/.test(trimmed)) {
28
- return { valid: false, error: 'API key contains invalid characters' };
29
- }
30
-
31
- return { valid: true };
32
- }
33
-
34
- /**
35
- * Validate Gemini API key format
36
- */
37
- static validateGeminiApiKey(apiKey: string): { valid: boolean; error?: string } {
38
- if (!apiKey || typeof apiKey !== 'string') {
39
- return { valid: false, error: 'API key is required' };
40
- }
41
-
42
- const trimmed = apiKey.trim();
43
-
44
- if (trimmed.length < 30) {
45
- return { valid: false, error: 'Gemini API key seems too short' };
46
- }
47
-
48
- if (trimmed.length > 100) {
49
- return { valid: false, error: 'Gemini API key seems too long' };
50
- }
51
-
52
- // Gemini API keys typically start with "AIza"
53
- if (!trimmed.startsWith('AIza')) {
54
- return { valid: false, error: 'Gemini API keys typically start with "AIza"' };
55
- }
56
-
57
- // Basic format validation
58
- if (!/^[A-Za-z0-9_\-]+$/.test(trimmed)) {
59
- return { valid: false, error: 'API key contains invalid characters' };
60
- }
61
-
62
- return { valid: true };
63
- }
64
-
65
- /**
66
- * Test Jules API key connectivity
67
- */
68
- static async testJulesApiKey(apiKey: string): Promise<{ success: boolean; error?: string }> {
69
- try {
70
- // Validate format first
71
- const validation = this.validateJulesApiKey(apiKey);
72
- if (!validation.valid) {
73
- return { success: false, error: validation.error };
74
- }
75
-
76
- // Test connectivity with a simple API call
77
- const response = await fetch('https://jules.googleapis.com/v1alpha/sessions', {
78
- method: 'GET',
79
- headers: {
80
- 'X-Goog-Api-Key': apiKey.trim(),
81
- 'Content-Type': 'application/json'
82
- }
83
- });
84
-
85
- if (response.status === 401) {
86
- return { success: false, error: 'Invalid API key or insufficient permissions' };
87
- }
88
-
89
- if (response.status === 403) {
90
- return { success: false, error: 'API key does not have required permissions' };
91
- }
92
-
93
- if (response.status === 429) {
94
- return { success: false, error: 'Rate limit exceeded. Please try again later' };
95
- }
96
-
97
- if (!response.ok && response.status !== 404) {
98
- return { success: false, error: `API error: ${response.status} ${response.statusText}` };
99
- }
100
-
101
- // 404 is acceptable for a test call to sessions endpoint
102
- return { success: true };
103
-
104
- } catch (error) {
105
- if (error instanceof Error) {
106
- if (error.message.includes('fetch')) {
107
- return { success: false, error: 'Network error. Please check your internet connection' };
108
- }
109
- return { success: false, error: error.message };
110
- }
111
- return { success: false, error: 'Unknown error occurred during API test' };
112
- }
113
- }
114
-
115
- /**
116
- * Test Gemini API key connectivity
117
- */
118
- static async testGeminiApiKey(apiKey: string): Promise<{ success: boolean; error?: string }> {
119
- try {
120
- // Validate format first
121
- const validation = this.validateGeminiApiKey(apiKey);
122
- if (!validation.valid) {
123
- return { success: false, error: validation.error };
124
- }
125
-
126
- // Test connectivity with a simple API call
127
- const response = await fetch('https://generativelanguage.googleapis.com/v1beta/models', {
128
- method: 'GET',
129
- headers: {
130
- 'x-goog-api-key': apiKey.trim()
131
- }
132
- });
133
-
134
- if (response.status === 401) {
135
- return { success: false, error: 'Invalid API key' };
136
- }
137
-
138
- if (response.status === 403) {
139
- return { success: false, error: 'API key does not have required permissions' };
140
- }
141
-
142
- if (response.status === 429) {
143
- return { success: false, error: 'Rate limit exceeded. Please try again later' };
144
- }
145
-
146
- if (!response.ok) {
147
- return { success: false, error: `API error: ${response.status} ${response.statusText}` };
148
- }
149
-
150
- // Verify we can parse the response
151
- const data = await response.json();
152
- if (!data.models || !Array.isArray(data.models)) {
153
- return { success: false, error: 'Unexpected API response format' };
154
- }
155
-
156
- return { success: true };
157
-
158
- } catch (error) {
159
- if (error instanceof Error) {
160
- if (error.message.includes('fetch')) {
161
- return { success: false, error: 'Network error. Please check your internet connection' };
162
- }
163
- return { success: false, error: error.message };
164
- }
165
- return { success: false, error: 'Unknown error occurred during API test' };
166
- }
167
- }
168
-
169
- /**
170
- * Store API keys securely in configuration
171
- */
172
- static async storeApiKeys(keys: { jules?: string; gemini?: string }): Promise<void> {
173
- const config = await ConfigurationManager.load();
174
-
175
- if (keys.jules) {
176
- config.apiKeys.jules = keys.jules.trim();
177
- }
178
-
179
- if (keys.gemini) {
180
- config.apiKeys.gemini = keys.gemini.trim();
181
- }
182
-
183
- await ConfigurationManager.save(config);
184
- }
185
-
186
- /**
187
- * Get stored API keys
188
- */
189
- static async getStoredApiKeys(): Promise<{ jules: string | null; gemini: string | null }> {
190
- const config = await ConfigurationManager.load();
191
- return {
192
- jules: config.apiKeys.jules,
193
- gemini: config.apiKeys.gemini
194
- };
195
- }
196
-
197
- /**
198
- * Check if API keys are configured
199
- */
200
- static async hasApiKeys(): Promise<{ jules: boolean; gemini: boolean }> {
201
- const keys = await this.getStoredApiKeys();
202
- return {
203
- jules: !!keys.jules,
204
- gemini: !!keys.gemini
205
- };
206
- }
207
-
208
- /**
209
- * Clear stored API keys
210
- */
211
- static async clearApiKeys(): Promise<void> {
212
- const config = await ConfigurationManager.load();
213
- config.apiKeys.jules = null;
214
- config.apiKeys.gemini = null;
215
- await ConfigurationManager.save(config);
216
- }
217
-
218
- /**
219
- * Validate and test all stored API keys
220
- */
221
- static async validateStoredKeys(): Promise<{
222
- jules: { valid: boolean; error?: string };
223
- gemini: { valid: boolean; error?: string };
224
- }> {
225
- const keys = await this.getStoredApiKeys();
226
-
227
- const results = {
228
- jules: { valid: false, error: 'No API key configured' },
229
- gemini: { valid: false, error: 'No API key configured' }
230
- };
231
-
232
- if (keys.jules) {
233
- const julesResult = await this.testJulesApiKey(keys.jules);
234
- results.jules = { valid: julesResult.success, error: julesResult.error || '' };
235
- }
236
-
237
- if (keys.gemini) {
238
- const geminiResult = await this.testGeminiApiKey(keys.gemini);
239
- results.gemini = { valid: geminiResult.success, error: geminiResult.error || '' };
240
- }
241
-
242
- return results;
243
- }
244
-
245
- /**
246
- * Get API key status for display
247
- */
248
- static async getApiKeyStatus(): Promise<{
249
- jules: 'configured' | 'missing' | 'invalid';
250
- gemini: 'configured' | 'missing' | 'invalid';
251
- }> {
252
- const keys = await this.getStoredApiKeys();
253
- const status: {
254
- jules: 'configured' | 'missing' | 'invalid';
255
- gemini: 'configured' | 'missing' | 'invalid';
256
- } = {
257
- jules: 'missing',
258
- gemini: 'missing'
259
- };
260
-
261
- if (keys.jules) {
262
- const validation = this.validateJulesApiKey(keys.jules);
263
- status.jules = validation.valid ? 'configured' : 'invalid';
264
- }
265
-
266
- if (keys.gemini) {
267
- const validation = this.validateGeminiApiKey(keys.gemini);
268
- status.gemini = validation.valid ? 'configured' : 'invalid';
269
- }
270
-
271
- return status;
272
- }
273
-
274
- /**
275
- * Show helpful error messages for API failures
276
- */
277
- static showApiErrorHelp(service: 'jules' | 'gemini', error: string): void {
278
- UIComponents.showErrorBox(`${service.toUpperCase()} API Error: ${error}`);
279
-
280
- console.log('\nTroubleshooting steps:');
281
-
282
- if (service === 'jules') {
283
- UIComponents.showList([
284
- 'Verify your Jules API key at https://jules.google.com',
285
- 'Check that the Jules GitHub app is installed on your repository',
286
- 'Ensure your API key has the required permissions',
287
- 'Try regenerating your API key if the issue persists'
288
- ]);
289
- } else {
290
- UIComponents.showList([
291
- 'Verify your Gemini API key at https://aistudio.google.com/app/apikey',
292
- 'Check that the Gemini API is enabled in your Google Cloud project',
293
- 'Ensure you have sufficient quota remaining',
294
- 'Try regenerating your API key if the issue persists'
295
- ]);
296
- }
297
- }
298
- }
1
+ import { ConfigurationManager } from './configuration.js';
2
+ import { UIComponents } from '../ui/components.js';
3
+
4
+ /**
5
+ * API key validation patterns and utilities
6
+ */
7
+ export class ApiKeyManager {
8
+ /**
9
+ * Validate Jules API key format
10
+ */
11
+ static validateJulesApiKey(apiKey: string): { valid: boolean; error?: string } {
12
+ if (!apiKey || typeof apiKey !== 'string') {
13
+ return { valid: false, error: 'API key is required' };
14
+ }
15
+
16
+ const trimmed = apiKey.trim();
17
+
18
+ if (trimmed.length < 10) {
19
+ return { valid: false, error: 'API key seems too short' };
20
+ }
21
+
22
+ if (trimmed.length > 200) {
23
+ return { valid: false, error: 'API key seems too long' };
24
+ }
25
+
26
+ // Basic format validation - Jules keys might have specific patterns
27
+ if (!/^[A-Za-z0-9_.-]+$/.test(trimmed)) {
28
+ return { valid: false, error: 'API key contains invalid characters' };
29
+ }
30
+
31
+ return { valid: true };
32
+ }
33
+
34
+ /**
35
+ * Validate Gemini API key format
36
+ */
37
+ static validateGeminiApiKey(apiKey: string): { valid: boolean; error?: string } {
38
+ if (!apiKey || typeof apiKey !== 'string') {
39
+ return { valid: false, error: 'API key is required' };
40
+ }
41
+
42
+ const trimmed = apiKey.trim();
43
+
44
+ if (trimmed.length < 30) {
45
+ return { valid: false, error: 'Gemini API key seems too short' };
46
+ }
47
+
48
+ if (trimmed.length > 100) {
49
+ return { valid: false, error: 'Gemini API key seems too long' };
50
+ }
51
+
52
+ // Gemini API keys typically start with "AIza"
53
+ if (!trimmed.startsWith('AIza')) {
54
+ return { valid: false, error: 'Gemini API keys typically start with "AIza"' };
55
+ }
56
+
57
+ // Basic format validation
58
+ if (!/^[A-Za-z0-9_-]+$/.test(trimmed)) {
59
+ return { valid: false, error: 'API key contains invalid characters' };
60
+ }
61
+
62
+ return { valid: true };
63
+ }
64
+
65
+ /**
66
+ * Test Jules API key connectivity
67
+ */
68
+ static async testJulesApiKey(apiKey: string): Promise<{ success: boolean; error?: string }> {
69
+ try {
70
+ // Validate format first
71
+ const validation = this.validateJulesApiKey(apiKey);
72
+ if (!validation.valid) {
73
+ return { success: false, error: validation.error };
74
+ }
75
+
76
+ // Test connectivity with a simple API call
77
+ const response = await fetch('https://jules.googleapis.com/v1alpha/sessions', {
78
+ method: 'GET',
79
+ headers: {
80
+ 'X-Goog-Api-Key': apiKey.trim(),
81
+ 'Content-Type': 'application/json'
82
+ }
83
+ });
84
+
85
+ if (response.status === 401) {
86
+ return { success: false, error: 'Invalid API key or insufficient permissions' };
87
+ }
88
+
89
+ if (response.status === 403) {
90
+ return { success: false, error: 'API key does not have required permissions' };
91
+ }
92
+
93
+ if (response.status === 429) {
94
+ return { success: false, error: 'Rate limit exceeded. Please try again later' };
95
+ }
96
+
97
+ if (!response.ok && response.status !== 404) {
98
+ return { success: false, error: `API error: ${response.status} ${response.statusText}` };
99
+ }
100
+
101
+ // 404 is acceptable for a test call to sessions endpoint
102
+ return { success: true };
103
+
104
+ } catch (error) {
105
+ if (error instanceof Error) {
106
+ if (error.message.includes('fetch')) {
107
+ return { success: false, error: 'Network error. Please check your internet connection' };
108
+ }
109
+ return { success: false, error: error.message };
110
+ }
111
+ return { success: false, error: 'Unknown error occurred during API test' };
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Test Gemini API key connectivity
117
+ */
118
+ static async testGeminiApiKey(apiKey: string): Promise<{ success: boolean; error?: string }> {
119
+ try {
120
+ // Validate format first
121
+ const validation = this.validateGeminiApiKey(apiKey);
122
+ if (!validation.valid) {
123
+ return { success: false, error: validation.error };
124
+ }
125
+
126
+ // Test connectivity with a simple API call
127
+ const response = await fetch('https://generativelanguage.googleapis.com/v1beta/models', {
128
+ method: 'GET',
129
+ headers: {
130
+ 'x-goog-api-key': apiKey.trim()
131
+ }
132
+ });
133
+
134
+ if (response.status === 401) {
135
+ return { success: false, error: 'Invalid API key' };
136
+ }
137
+
138
+ if (response.status === 403) {
139
+ return { success: false, error: 'API key does not have required permissions' };
140
+ }
141
+
142
+ if (response.status === 429) {
143
+ return { success: false, error: 'Rate limit exceeded. Please try again later' };
144
+ }
145
+
146
+ if (!response.ok) {
147
+ return { success: false, error: `API error: ${response.status} ${response.statusText}` };
148
+ }
149
+
150
+ // Verify we can parse the response
151
+ const data = await response.json();
152
+ if (!data.models || !Array.isArray(data.models)) {
153
+ return { success: false, error: 'Unexpected API response format' };
154
+ }
155
+
156
+ return { success: true };
157
+
158
+ } catch (error) {
159
+ if (error instanceof Error) {
160
+ if (error.message.includes('fetch')) {
161
+ return { success: false, error: 'Network error. Please check your internet connection' };
162
+ }
163
+ return { success: false, error: error.message };
164
+ }
165
+ return { success: false, error: 'Unknown error occurred during API test' };
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Store API keys securely in configuration
171
+ */
172
+ static async storeApiKeys(keys: { jules?: string; gemini?: string }): Promise<void> {
173
+ const config = await ConfigurationManager.load();
174
+
175
+ if (keys.jules) {
176
+ config.apiKeys.jules = keys.jules.trim();
177
+ }
178
+
179
+ if (keys.gemini) {
180
+ config.apiKeys.gemini = keys.gemini.trim();
181
+ }
182
+
183
+ await ConfigurationManager.save(config);
184
+ }
185
+
186
+ /**
187
+ * Get stored API keys
188
+ */
189
+ static async getStoredApiKeys(): Promise<{ jules: string | null; gemini: string | null }> {
190
+ const config = await ConfigurationManager.load();
191
+ return {
192
+ jules: config.apiKeys.jules,
193
+ gemini: config.apiKeys.gemini
194
+ };
195
+ }
196
+
197
+ /**
198
+ * Check if API keys are configured
199
+ */
200
+ static async hasApiKeys(): Promise<{ jules: boolean; gemini: boolean }> {
201
+ const keys = await this.getStoredApiKeys();
202
+ return {
203
+ jules: !!keys.jules,
204
+ gemini: !!keys.gemini
205
+ };
206
+ }
207
+
208
+ /**
209
+ * Clear stored API keys
210
+ */
211
+ static async clearApiKeys(): Promise<void> {
212
+ const config = await ConfigurationManager.load();
213
+ config.apiKeys.jules = null;
214
+ config.apiKeys.gemini = null;
215
+ await ConfigurationManager.save(config);
216
+ }
217
+
218
+ /**
219
+ * Validate and test all stored API keys
220
+ */
221
+ static async validateStoredKeys(): Promise<{
222
+ jules: { valid: boolean; error?: string };
223
+ gemini: { valid: boolean; error?: string };
224
+ }> {
225
+ const keys = await this.getStoredApiKeys();
226
+
227
+ const results = {
228
+ jules: { valid: false, error: 'No API key configured' },
229
+ gemini: { valid: false, error: 'No API key configured' }
230
+ };
231
+
232
+ if (keys.jules) {
233
+ const julesResult = await this.testJulesApiKey(keys.jules);
234
+ results.jules = { valid: julesResult.success, error: julesResult.error || '' };
235
+ }
236
+
237
+ if (keys.gemini) {
238
+ const geminiResult = await this.testGeminiApiKey(keys.gemini);
239
+ results.gemini = { valid: geminiResult.success, error: geminiResult.error || '' };
240
+ }
241
+
242
+ return results;
243
+ }
244
+
245
+ /**
246
+ * Get API key status for display
247
+ */
248
+ static async getApiKeyStatus(): Promise<{
249
+ jules: 'configured' | 'missing' | 'invalid';
250
+ gemini: 'configured' | 'missing' | 'invalid';
251
+ }> {
252
+ const keys = await this.getStoredApiKeys();
253
+ const status: {
254
+ jules: 'configured' | 'missing' | 'invalid';
255
+ gemini: 'configured' | 'missing' | 'invalid';
256
+ } = {
257
+ jules: 'missing',
258
+ gemini: 'missing'
259
+ };
260
+
261
+ if (keys.jules) {
262
+ const validation = this.validateJulesApiKey(keys.jules);
263
+ status.jules = validation.valid ? 'configured' : 'invalid';
264
+ }
265
+
266
+ if (keys.gemini) {
267
+ const validation = this.validateGeminiApiKey(keys.gemini);
268
+ status.gemini = validation.valid ? 'configured' : 'invalid';
269
+ }
270
+
271
+ return status;
272
+ }
273
+
274
+ /**
275
+ * Show helpful error messages for API failures
276
+ */
277
+ static showApiErrorHelp(service: 'jules' | 'gemini', error: string): void {
278
+ UIComponents.showErrorBox(`${service.toUpperCase()} API Error: ${error}`);
279
+
280
+ console.log('\nTroubleshooting steps:');
281
+
282
+ if (service === 'jules') {
283
+ UIComponents.showList([
284
+ 'Verify your Jules API key at https://jules.google.com',
285
+ 'Check that the Jules GitHub app is installed on your repository',
286
+ 'Ensure your API key has the required permissions',
287
+ 'Try regenerating your API key if the issue persists'
288
+ ]);
289
+ } else {
290
+ UIComponents.showList([
291
+ 'Verify your Gemini API key at https://aistudio.google.com/app/apikey',
292
+ 'Check that the Gemini API is enabled in your Google Cloud project',
293
+ 'Ensure you have sufficient quota remaining',
294
+ 'Try regenerating your API key if the issue persists'
295
+ ]);
296
+ }
297
+ }
298
+ }