baseguard 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 (244) hide show
  1. package/.eslintrc.json +25 -0
  2. package/.prettierrc +8 -0
  3. package/README.md +94 -0
  4. package/bin/base.js +494 -0
  5. package/dist/ai/fix-manager.d.ts +67 -0
  6. package/dist/ai/fix-manager.d.ts.map +1 -0
  7. package/dist/ai/fix-manager.js +326 -0
  8. package/dist/ai/fix-manager.js.map +1 -0
  9. package/dist/ai/gemini-analyzer.d.ts +116 -0
  10. package/dist/ai/gemini-analyzer.d.ts.map +1 -0
  11. package/dist/ai/gemini-analyzer.js +572 -0
  12. package/dist/ai/gemini-analyzer.js.map +1 -0
  13. package/dist/ai/index.d.ts +4 -0
  14. package/dist/ai/index.d.ts.map +1 -0
  15. package/dist/ai/index.js +5 -0
  16. package/dist/ai/index.js.map +1 -0
  17. package/dist/ai/jules-implementer.d.ts +115 -0
  18. package/dist/ai/jules-implementer.d.ts.map +1 -0
  19. package/dist/ai/jules-implementer.js +387 -0
  20. package/dist/ai/jules-implementer.js.map +1 -0
  21. package/dist/commands/automation.d.ts +5 -0
  22. package/dist/commands/automation.d.ts.map +1 -0
  23. package/dist/commands/automation.js +305 -0
  24. package/dist/commands/automation.js.map +1 -0
  25. package/dist/commands/check.d.ts +9 -0
  26. package/dist/commands/check.d.ts.map +1 -0
  27. package/dist/commands/check.js +113 -0
  28. package/dist/commands/check.js.map +1 -0
  29. package/dist/commands/config.d.ts +11 -0
  30. package/dist/commands/config.d.ts.map +1 -0
  31. package/dist/commands/config.js +324 -0
  32. package/dist/commands/config.js.map +1 -0
  33. package/dist/commands/fix.d.ts +9 -0
  34. package/dist/commands/fix.d.ts.map +1 -0
  35. package/dist/commands/fix.js +207 -0
  36. package/dist/commands/fix.js.map +1 -0
  37. package/dist/commands/index.d.ts +6 -0
  38. package/dist/commands/index.d.ts.map +1 -0
  39. package/dist/commands/index.js +7 -0
  40. package/dist/commands/index.js.map +1 -0
  41. package/dist/commands/init.d.ts +9 -0
  42. package/dist/commands/init.d.ts.map +1 -0
  43. package/dist/commands/init.js +125 -0
  44. package/dist/commands/init.js.map +1 -0
  45. package/dist/core/api-key-manager.d.ts +83 -0
  46. package/dist/core/api-key-manager.d.ts.map +1 -0
  47. package/dist/core/api-key-manager.js +244 -0
  48. package/dist/core/api-key-manager.js.map +1 -0
  49. package/dist/core/baseguard.d.ts +46 -0
  50. package/dist/core/baseguard.d.ts.map +1 -0
  51. package/dist/core/baseguard.js +132 -0
  52. package/dist/core/baseguard.js.map +1 -0
  53. package/dist/core/baseline-checker.d.ts +63 -0
  54. package/dist/core/baseline-checker.d.ts.map +1 -0
  55. package/dist/core/baseline-checker.js +502 -0
  56. package/dist/core/baseline-checker.js.map +1 -0
  57. package/dist/core/cache-manager.d.ts +88 -0
  58. package/dist/core/cache-manager.d.ts.map +1 -0
  59. package/dist/core/cache-manager.js +213 -0
  60. package/dist/core/cache-manager.js.map +1 -0
  61. package/dist/core/configuration.d.ts +140 -0
  62. package/dist/core/configuration.d.ts.map +1 -0
  63. package/dist/core/configuration.js +474 -0
  64. package/dist/core/configuration.js.map +1 -0
  65. package/dist/core/directory-filter.d.ts +90 -0
  66. package/dist/core/directory-filter.d.ts.map +1 -0
  67. package/dist/core/directory-filter.js +319 -0
  68. package/dist/core/directory-filter.js.map +1 -0
  69. package/dist/core/error-handler.d.ts +110 -0
  70. package/dist/core/error-handler.d.ts.map +1 -0
  71. package/dist/core/error-handler.js +392 -0
  72. package/dist/core/error-handler.js.map +1 -0
  73. package/dist/core/file-processor.d.ts +80 -0
  74. package/dist/core/file-processor.d.ts.map +1 -0
  75. package/dist/core/file-processor.js +259 -0
  76. package/dist/core/file-processor.js.map +1 -0
  77. package/dist/core/gitignore-manager.d.ts +44 -0
  78. package/dist/core/gitignore-manager.d.ts.map +1 -0
  79. package/dist/core/gitignore-manager.js +147 -0
  80. package/dist/core/gitignore-manager.js.map +1 -0
  81. package/dist/core/index.d.ts +13 -0
  82. package/dist/core/index.d.ts.map +1 -0
  83. package/dist/core/index.js +13 -0
  84. package/dist/core/index.js.map +1 -0
  85. package/dist/core/lazy-loader.d.ts +68 -0
  86. package/dist/core/lazy-loader.d.ts.map +1 -0
  87. package/dist/core/lazy-loader.js +260 -0
  88. package/dist/core/lazy-loader.js.map +1 -0
  89. package/dist/core/memory-manager.d.ts +1 -0
  90. package/dist/core/memory-manager.d.ts.map +1 -0
  91. package/dist/core/memory-manager.js +2 -0
  92. package/dist/core/memory-manager.js.map +1 -0
  93. package/dist/core/startup-optimizer.d.ts +45 -0
  94. package/dist/core/startup-optimizer.d.ts.map +1 -0
  95. package/dist/core/startup-optimizer.js +140 -0
  96. package/dist/core/startup-optimizer.js.map +1 -0
  97. package/dist/git/automation-engine.d.ts +58 -0
  98. package/dist/git/automation-engine.d.ts.map +1 -0
  99. package/dist/git/automation-engine.js +318 -0
  100. package/dist/git/automation-engine.js.map +1 -0
  101. package/dist/git/github-manager.d.ts +71 -0
  102. package/dist/git/github-manager.d.ts.map +1 -0
  103. package/dist/git/github-manager.js +226 -0
  104. package/dist/git/github-manager.js.map +1 -0
  105. package/dist/git/hook-manager.d.ts +43 -0
  106. package/dist/git/hook-manager.d.ts.map +1 -0
  107. package/dist/git/hook-manager.js +191 -0
  108. package/dist/git/hook-manager.js.map +1 -0
  109. package/dist/git/index.d.ts +4 -0
  110. package/dist/git/index.d.ts.map +1 -0
  111. package/dist/git/index.js +5 -0
  112. package/dist/git/index.js.map +1 -0
  113. package/dist/index.d.ts +8 -0
  114. package/dist/index.d.ts.map +1 -0
  115. package/dist/index.js +9 -0
  116. package/dist/index.js.map +1 -0
  117. package/dist/parsers/feature-validator.d.ts +60 -0
  118. package/dist/parsers/feature-validator.d.ts.map +1 -0
  119. package/dist/parsers/feature-validator.js +483 -0
  120. package/dist/parsers/feature-validator.js.map +1 -0
  121. package/dist/parsers/index.d.ts +8 -0
  122. package/dist/parsers/index.d.ts.map +1 -0
  123. package/dist/parsers/index.js +9 -0
  124. package/dist/parsers/index.js.map +1 -0
  125. package/dist/parsers/parser-manager.d.ts +103 -0
  126. package/dist/parsers/parser-manager.d.ts.map +1 -0
  127. package/dist/parsers/parser-manager.js +321 -0
  128. package/dist/parsers/parser-manager.js.map +1 -0
  129. package/dist/parsers/parser.d.ts +23 -0
  130. package/dist/parsers/parser.d.ts.map +1 -0
  131. package/dist/parsers/parser.js +6 -0
  132. package/dist/parsers/parser.js.map +1 -0
  133. package/dist/parsers/react-parser.d.ts +22 -0
  134. package/dist/parsers/react-parser.d.ts.map +1 -0
  135. package/dist/parsers/react-parser.js +307 -0
  136. package/dist/parsers/react-parser.js.map +1 -0
  137. package/dist/parsers/svelte-parser.d.ts +33 -0
  138. package/dist/parsers/svelte-parser.d.ts.map +1 -0
  139. package/dist/parsers/svelte-parser.js +408 -0
  140. package/dist/parsers/svelte-parser.js.map +1 -0
  141. package/dist/parsers/vanilla-parser.d.ts +31 -0
  142. package/dist/parsers/vanilla-parser.d.ts.map +1 -0
  143. package/dist/parsers/vanilla-parser.js +590 -0
  144. package/dist/parsers/vanilla-parser.js.map +1 -0
  145. package/dist/parsers/vue-parser.d.ts +9 -0
  146. package/dist/parsers/vue-parser.d.ts.map +1 -0
  147. package/dist/parsers/vue-parser.js +16 -0
  148. package/dist/parsers/vue-parser.js.map +1 -0
  149. package/dist/terminal-header.d.ts +12 -0
  150. package/dist/terminal-header.js +45 -0
  151. package/dist/types/index.d.ts +83 -0
  152. package/dist/types/index.d.ts.map +1 -0
  153. package/dist/types/index.js +5 -0
  154. package/dist/types/index.js.map +1 -0
  155. package/dist/ui/components.d.ts +133 -0
  156. package/dist/ui/components.d.ts.map +1 -0
  157. package/dist/ui/components.js +482 -0
  158. package/dist/ui/components.js.map +1 -0
  159. package/dist/ui/help.d.ts +11 -0
  160. package/dist/ui/help.d.ts.map +1 -0
  161. package/dist/ui/help.js +161 -0
  162. package/dist/ui/help.js.map +1 -0
  163. package/dist/ui/index.d.ts +5 -0
  164. package/dist/ui/index.d.ts.map +1 -0
  165. package/dist/ui/index.js +5 -0
  166. package/dist/ui/index.js.map +1 -0
  167. package/dist/ui/prompts.d.ts +63 -0
  168. package/dist/ui/prompts.d.ts.map +1 -0
  169. package/dist/ui/prompts.js +611 -0
  170. package/dist/ui/prompts.js.map +1 -0
  171. package/dist/ui/terminal-header.d.ts +13 -0
  172. package/dist/ui/terminal-header.d.ts.map +1 -0
  173. package/dist/ui/terminal-header.js +46 -0
  174. package/dist/ui/terminal-header.js.map +1 -0
  175. package/package.json +80 -0
  176. package/src/ai/__tests__/gemini-analyzer.test.ts +181 -0
  177. package/src/ai/fix-manager.ts +362 -0
  178. package/src/ai/gemini-analyzer.ts +671 -0
  179. package/src/ai/index.ts +4 -0
  180. package/src/ai/jules-implementer.ts +459 -0
  181. package/src/commands/automation.ts +344 -0
  182. package/src/commands/check.ts +299 -0
  183. package/src/commands/config.ts +365 -0
  184. package/src/commands/fix.ts +234 -0
  185. package/src/commands/index.ts +6 -0
  186. package/src/commands/init.ts +142 -0
  187. package/src/commands/status.ts +0 -0
  188. package/src/core/api-key-manager.ts +298 -0
  189. package/src/core/baseguard.ts +742 -0
  190. package/src/core/baseline-checker.ts +563 -0
  191. package/src/core/cache-manager.ts +270 -0
  192. package/src/core/configuration-recovery.ts +676 -0
  193. package/src/core/configuration.ts +559 -0
  194. package/src/core/debug-logger.ts +590 -0
  195. package/src/core/directory-filter.ts +421 -0
  196. package/src/core/error-handler.ts +517 -0
  197. package/src/core/file-processor.ts +331 -0
  198. package/src/core/gitignore-manager.ts +169 -0
  199. package/src/core/graceful-degradation-manager.ts +596 -0
  200. package/src/core/index.ts +13 -0
  201. package/src/core/lazy-loader.ts +307 -0
  202. package/src/core/logger.ts +0 -0
  203. package/src/core/memory-manager.ts +294 -0
  204. package/src/core/startup-optimizer.ts +173 -0
  205. package/src/core/system-error-handler.ts +746 -0
  206. package/src/git/automation-engine.ts +361 -0
  207. package/src/git/github-manager.ts +260 -0
  208. package/src/git/hook-manager.ts +210 -0
  209. package/src/git/index.ts +4 -0
  210. package/src/index.ts +8 -0
  211. package/src/parsers/feature-validator.ts +559 -0
  212. package/src/parsers/index.ts +8 -0
  213. package/src/parsers/parser-manager.ts +419 -0
  214. package/src/parsers/parser.ts +26 -0
  215. package/src/parsers/react-parser-optimized.ts +161 -0
  216. package/src/parsers/react-parser.ts +359 -0
  217. package/src/parsers/svelte-parser.ts +506 -0
  218. package/src/parsers/vanilla-parser.ts +682 -0
  219. package/src/parsers/vue-parser.ts +472 -0
  220. package/src/types/index.ts +92 -0
  221. package/src/ui/components.ts +567 -0
  222. package/src/ui/help.ts +193 -0
  223. package/src/ui/index.ts +4 -0
  224. package/src/ui/prompts.ts +688 -0
  225. package/src/ui/terminal-header.ts +59 -0
  226. package/test-config-commands.js +56 -0
  227. package/test-header-simple.js +33 -0
  228. package/test-terminal-header.js +12 -0
  229. package/test-ui.js +29 -0
  230. package/tests/e2e/baseguard.e2e.test.ts +516 -0
  231. package/tests/e2e/cross-platform.e2e.test.ts +420 -0
  232. package/tests/e2e/git-integration.e2e.test.ts +487 -0
  233. package/tests/fixtures/react-project/package.json +14 -0
  234. package/tests/fixtures/react-project/src/App.css +76 -0
  235. package/tests/fixtures/react-project/src/App.tsx +77 -0
  236. package/tests/fixtures/svelte-project/package.json +11 -0
  237. package/tests/fixtures/svelte-project/src/App.svelte +369 -0
  238. package/tests/fixtures/vanilla-project/index.html +76 -0
  239. package/tests/fixtures/vanilla-project/script.js +331 -0
  240. package/tests/fixtures/vanilla-project/styles.css +359 -0
  241. package/tests/fixtures/vue-project/package.json +12 -0
  242. package/tests/fixtures/vue-project/src/App.vue +216 -0
  243. package/tsconfig.json +36 -0
  244. package/vitest.config.ts +10 -0
@@ -0,0 +1,142 @@
1
+ import { UIComponents, Prompts } from '../ui/index.js';
2
+ import { ConfigurationManager, BROWSER_TARGET_PRESETS, type PresetName } from '../core/index.js';
3
+ import { ErrorHandler } from '../core/error-handler.js';
4
+
5
+ /**
6
+ * Initialize BaseGuard in a project
7
+ */
8
+ export async function init(options: {
9
+ preset?: string;
10
+ skipHooks?: boolean;
11
+ skipApiKeys?: boolean;
12
+ }): Promise<void> {
13
+ try {
14
+ UIComponents.showHeader();
15
+
16
+ // Check if already initialized
17
+ const configExists = await ConfigurationManager.exists();
18
+ if (configExists) {
19
+ const { default: inquirer } = await import('inquirer');
20
+ const { overwrite } = await inquirer.prompt([
21
+ {
22
+ type: 'confirm',
23
+ name: 'overwrite',
24
+ message: 'BaseGuard is already initialized. Overwrite existing configuration?',
25
+ default: false
26
+ }
27
+ ]);
28
+
29
+ if (!overwrite) {
30
+ UIComponents.showInfoBox('Initialization cancelled. Use "base config" to modify settings.');
31
+ return;
32
+ }
33
+ }
34
+
35
+ const spinner = UIComponents.createSpinner('Setting up BaseGuard configuration...');
36
+ spinner.start();
37
+
38
+ // Create configuration based on options or run setup wizard
39
+ let config;
40
+
41
+ if (options.preset && options.preset !== 'custom') {
42
+ // Use preset configuration
43
+ const presetName = options.preset as PresetName;
44
+ if (BROWSER_TARGET_PRESETS[presetName]) {
45
+ config = ConfigurationManager.createWithPreset(presetName);
46
+ spinner.text = `Using ${presetName} preset...`;
47
+ } else {
48
+ spinner.fail(`Invalid preset: ${options.preset}`);
49
+ UIComponents.showErrorBox(`Available presets: ${Object.keys(BROWSER_TARGET_PRESETS).join(', ')}`);
50
+ return;
51
+ }
52
+ } else {
53
+ // Run interactive setup wizard
54
+ spinner.stop();
55
+ const setupResult = await Prompts.setupWizard();
56
+ spinner.start();
57
+
58
+ config = ConfigurationManager.createWithCustomTargets(setupResult.targets);
59
+
60
+ // Set up automation if requested and not skipped
61
+ if (setupResult.installHooks && !options.skipHooks) {
62
+ config.automation = {
63
+ enabled: true,
64
+ trigger: setupResult.hookTrigger,
65
+ autoAnalyze: true,
66
+ autoFix: false,
67
+ blockCommit: true
68
+ };
69
+ }
70
+ }
71
+
72
+ // Set up API keys if not skipped
73
+ if (!options.skipApiKeys) {
74
+ spinner.stop();
75
+ const apiKeys = await Prompts.setupApiKeys();
76
+ spinner.start();
77
+
78
+ if (apiKeys.julesApiKey) {
79
+ config.apiKeys.jules = apiKeys.julesApiKey;
80
+ }
81
+ if (apiKeys.geminiApiKey) {
82
+ config.apiKeys.gemini = apiKeys.geminiApiKey;
83
+ }
84
+ }
85
+
86
+ await ConfigurationManager.save(config);
87
+ spinner.succeed('BaseGuard configuration created');
88
+
89
+ UIComponents.showSuccessBox('BaseGuard has been successfully initialized!');
90
+
91
+ // Show configuration summary
92
+ console.log('\n📋 Configuration Summary:');
93
+ UIComponents.showList([
94
+ `Browser targets: ${config.targets.map(t => `${t.browser} ${t.minVersion}`).join(', ')}`,
95
+ `API keys: ${config.apiKeys.jules ? '✅' : '❌'} Jules, ${config.apiKeys.gemini ? '✅' : '❌'} Gemini`,
96
+ `Automation: ${config.automation.enabled ? '✅ Enabled' : '❌ Disabled'}`
97
+ ]);
98
+
99
+ console.log('\n🚀 Next steps:');
100
+ const nextSteps = ['Run `base check` to scan for compatibility issues'];
101
+
102
+ if (config.apiKeys.jules && config.apiKeys.gemini) {
103
+ nextSteps.push('Run `base fix` to automatically fix issues with AI');
104
+ } else {
105
+ nextSteps.push('Run `base config set-keys` to configure AI services');
106
+ }
107
+
108
+ if (!config.automation.enabled) {
109
+ nextSteps.push('Run `base automation enable` to set up git hooks');
110
+ }
111
+
112
+ UIComponents.showList(nextSteps);
113
+
114
+ } catch (error) {
115
+ const apiError = ErrorHandler.handleAPIError(error);
116
+ ErrorHandler.displayError(apiError);
117
+
118
+ // Provide specific help for common init issues
119
+ console.log('\n💡 Common solutions:');
120
+ if (apiError.type === 'network') {
121
+ UIComponents.showList([
122
+ 'Check your internet connection',
123
+ 'Try running init again with --skip-api-keys to set up offline',
124
+ 'Configure API keys later with "base config set-keys"'
125
+ ]);
126
+ } else if (apiError.type === 'configuration') {
127
+ UIComponents.showList([
128
+ 'Delete .baseguardrc.json and try again',
129
+ 'Check file permissions in the current directory',
130
+ 'Run "base config validate" to check for issues'
131
+ ]);
132
+ } else {
133
+ UIComponents.showList([
134
+ 'Try running "base init --skip-api-keys" for offline setup',
135
+ 'Check the documentation at https://github.com/baseguard/baseguard#readme',
136
+ 'Report this issue if it persists'
137
+ ]);
138
+ }
139
+
140
+ process.exit(1);
141
+ }
142
+ }
File without changes
@@ -0,0 +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
+ }