owenkaplinsky commited on
Commit
70594f9
·
verified ·
1 Parent(s): 0ec5503

Update project/src/index.js

Browse files
Files changed (1) hide show
  1. project/src/index.js +42 -40
project/src/index.js CHANGED
@@ -183,7 +183,7 @@ downloadCodeButton.addEventListener("click", () => {
183
  // Settings button and Keys Modal
184
  const settingsButton = document.querySelector('#settingsButton');
185
  const apiKeyModal = document.querySelector('#apiKeyModal');
186
- // const apiKeyInput = document.querySelector('#apiKeyInput'); // TEMP: No OpenAI key input field
187
  const hfKeyInput = document.querySelector('#hfKeyInput');
188
  const saveApiKeyButton = document.querySelector('#saveApiKey');
189
  const cancelApiKeyButton = document.querySelector('#cancelApiKey');
@@ -194,7 +194,9 @@ const HF_KEY_STORAGE = "mcp_blockly_hf_key";
194
  const loadStoredKeys = () => {
195
  const storedOpenAI = window.localStorage.getItem(OPENAI_KEY_STORAGE) || "";
196
  const storedHF = window.localStorage.getItem(HF_KEY_STORAGE) || "";
197
- // apiKeyInput.value = storedOpenAI; // TEMP: No OpenAI key input field
 
 
198
  if (hfKeyInput) {
199
  hfKeyInput.value = storedHF;
200
  }
@@ -210,13 +212,12 @@ settingsButton.addEventListener("click", () => {
210
  });
211
 
212
  saveApiKeyButton.addEventListener("click", () => {
213
- // const apiKey = apiKeyInput.value.trim(); // TEMP: No OpenAI key input field
214
- const apiKey = ""; // TEMP: Using free API key, OpenAI field disabled
215
  const hfKey = hfKeyInput?.value.trim() || "";
216
 
217
- // Validate OpenAI key format if provided
218
- if (apiKey && (!apiKey.startsWith("sk-") || apiKey.length < 40)) {
219
- alert("Invalid OpenAI API key format. Please enter a valid OpenAI API key (starts with 'sk-').");
220
  return;
221
  }
222
 
@@ -253,8 +254,7 @@ cancelApiKeyButton.addEventListener("click", () => {
253
 
254
  // Welcome Modal Setup
255
  const welcomeModal = document.querySelector('#welcomeModal');
256
- // TEMPORARY FREE API KEY - welcomeApiKeyInput removed from DOM
257
- // const welcomeApiKeyInput = document.querySelector('#welcomeApiKeyInput');
258
  const welcomeHfKeyInput = document.querySelector('#welcomeHfKeyInput');
259
  const saveWelcomeApiKeyButton = document.querySelector('#saveWelcomeApiKey');
260
  const skipTutorialButton = document.querySelector('#skipTutorialButton');
@@ -270,11 +270,9 @@ const getCookieValue = (name) => {
270
  };
271
 
272
  const loadWelcomeStoredKeys = () => {
273
- // TEMPORARY FREE API KEY
274
- // const storedOpenAI = window.localStorage.getItem(OPENAI_KEY_STORAGE) || "";
275
  const storedHF = window.localStorage.getItem(HF_KEY_STORAGE) || "";
276
- // TEMPORARY FREE API KEY - welcomeApiKeyInput removed from DOM
277
- // welcomeApiKeyInput.value = storedOpenAI;
278
  welcomeHfKeyInput.value = storedHF;
279
  };
280
 
@@ -294,42 +292,43 @@ const hideWelcomeModal = () => {
294
  }
295
  };
296
 
297
- saveWelcomeApiKeyButton.addEventListener("click", () => {
298
- // TEMPORARY FREE API KEY
299
- // const apiKey = welcomeApiKeyInput.value.trim();
300
- const apiKey = ""; // TEMPORARY FREE API KEY - using free API
301
  const hfKey = welcomeHfKeyInput.value.trim();
302
 
303
- // TEMPORARY FREE API KEY
304
- // // Validate OpenAI key format if provided
305
- // if (apiKey && (!apiKey.startsWith("sk-") || apiKey.length < 40)) {
306
- // alert("Invalid OpenAI API key format. Please enter a valid OpenAI API key (starts with 'sk-').");
307
- // return;
308
- // }
309
 
310
  // Validate Hugging Face key format if provided
311
  if (hfKey && (!hfKey.startsWith("hf_") || hfKey.length < 20)) {
312
  alert("Invalid Hugging Face API key format. Please enter a valid Hugging Face API key (starts with 'hf_').");
313
- return;
314
  }
315
 
316
- // Save API keys locally
317
- window.localStorage.setItem(OPENAI_KEY_STORAGE, apiKey);
318
- window.localStorage.setItem(HF_KEY_STORAGE, hfKey);
 
 
319
 
320
- // Share keys with backend via cookies (per-request, not stored server-side)
321
- const cookieOpts = "path=/; SameSite=None; Secure";
322
- // TEMPORARY FREE API KEY
323
- // if (apiKey) {
324
- // document.cookie = `mcp_openai_key=${encodeURIComponent(apiKey)}; ${cookieOpts}`;
325
- // } else {
326
- // document.cookie = `mcp_openai_key=; Max-Age=0; ${cookieOpts}`;
327
- // }
328
- document.cookie = `mcp_openai_key=; Max-Age=0; ${cookieOpts}`; // TEMPORARY FREE API KEY - clear free API cookie
329
- if (hfKey) {
330
- document.cookie = `mcp_hf_key=${encodeURIComponent(hfKey)}; ${cookieOpts}`;
331
- } else {
332
- document.cookie = `mcp_hf_key=; Max-Age=0; ${cookieOpts}`;
 
333
  }
334
 
335
  hideWelcomeModal();
@@ -342,6 +341,9 @@ saveWelcomeApiKeyButton.addEventListener("click", () => {
342
  });
343
 
344
  skipTutorialButton.addEventListener("click", () => {
 
 
 
345
  tutorialEnabled = false;
346
  hideWelcomeModal();
347
  });
 
183
  // Settings button and Keys Modal
184
  const settingsButton = document.querySelector('#settingsButton');
185
  const apiKeyModal = document.querySelector('#apiKeyModal');
186
+ const apiKeyInput = document.querySelector('#apiKeyInput');
187
  const hfKeyInput = document.querySelector('#hfKeyInput');
188
  const saveApiKeyButton = document.querySelector('#saveApiKey');
189
  const cancelApiKeyButton = document.querySelector('#cancelApiKey');
 
194
  const loadStoredKeys = () => {
195
  const storedOpenAI = window.localStorage.getItem(OPENAI_KEY_STORAGE) || "";
196
  const storedHF = window.localStorage.getItem(HF_KEY_STORAGE) || "";
197
+ if (apiKeyInput) {
198
+ apiKeyInput.value = storedOpenAI;
199
+ }
200
  if (hfKeyInput) {
201
  hfKeyInput.value = storedHF;
202
  }
 
212
  });
213
 
214
  saveApiKeyButton.addEventListener("click", () => {
215
+ const apiKey = apiKeyInput?.value.trim() || "";
 
216
  const hfKey = hfKeyInput?.value.trim() || "";
217
 
218
+ // Validate OpenAI key format (required)
219
+ if (!apiKey || !apiKey.startsWith("sk-") || apiKey.length < 40) {
220
+ alert("OpenAI API key is required. Please enter a valid OpenAI API key (starts with 'sk-').");
221
  return;
222
  }
223
 
 
254
 
255
  // Welcome Modal Setup
256
  const welcomeModal = document.querySelector('#welcomeModal');
257
+ const welcomeApiKeyInput = document.querySelector('#welcomeApiKeyInput');
 
258
  const welcomeHfKeyInput = document.querySelector('#welcomeHfKeyInput');
259
  const saveWelcomeApiKeyButton = document.querySelector('#saveWelcomeApiKey');
260
  const skipTutorialButton = document.querySelector('#skipTutorialButton');
 
270
  };
271
 
272
  const loadWelcomeStoredKeys = () => {
273
+ const storedOpenAI = window.localStorage.getItem(OPENAI_KEY_STORAGE) || "";
 
274
  const storedHF = window.localStorage.getItem(HF_KEY_STORAGE) || "";
275
+ welcomeApiKeyInput.value = storedOpenAI;
 
276
  welcomeHfKeyInput.value = storedHF;
277
  };
278
 
 
292
  }
293
  };
294
 
295
+ // Helper function to save welcome modal keys
296
+ const saveWelcomeKeys = (requireValidation = true) => {
297
+ const apiKey = welcomeApiKeyInput.value.trim();
 
298
  const hfKey = welcomeHfKeyInput.value.trim();
299
 
300
+ // Validate OpenAI key format (required if requireValidation is true)
301
+ if (requireValidation && (!apiKey || !apiKey.startsWith("sk-") || apiKey.length < 40)) {
302
+ alert("OpenAI API key is required. Please enter a valid OpenAI API key (starts with 'sk-').");
303
+ return false;
304
+ }
 
305
 
306
  // Validate Hugging Face key format if provided
307
  if (hfKey && (!hfKey.startsWith("hf_") || hfKey.length < 20)) {
308
  alert("Invalid Hugging Face API key format. Please enter a valid Hugging Face API key (starts with 'hf_').");
309
+ return false;
310
  }
311
 
312
+ // Save API keys locally if apiKey is provided
313
+ if (apiKey) {
314
+ console.log("[Welcome] Saving keys to localStorage");
315
+ window.localStorage.setItem(OPENAI_KEY_STORAGE, apiKey);
316
+ window.localStorage.setItem(HF_KEY_STORAGE, hfKey);
317
 
318
+ // Share keys with backend via cookies (per-request, not stored server-side)
319
+ const cookieOpts = "path=/; SameSite=None; Secure";
320
+ document.cookie = `mcp_openai_key=${encodeURIComponent(apiKey)}; ${cookieOpts}`;
321
+ if (hfKey) {
322
+ document.cookie = `mcp_hf_key=${encodeURIComponent(hfKey)}; ${cookieOpts}`;
323
+ }
324
+ }
325
+
326
+ return true;
327
+ };
328
+
329
+ saveWelcomeApiKeyButton.addEventListener("click", () => {
330
+ if (!saveWelcomeKeys(true)) {
331
+ return;
332
  }
333
 
334
  hideWelcomeModal();
 
341
  });
342
 
343
  skipTutorialButton.addEventListener("click", () => {
344
+ // Save keys even if skipping tutorial (no validation required)
345
+ saveWelcomeKeys(false);
346
+
347
  tutorialEnabled = false;
348
  hideWelcomeModal();
349
  });