Spaces:
Running
Running
Upload 2 files
Browse files- kimi-js/kimi-module.js +54 -9
- kimi-js/kimi-script.js +32 -23
kimi-js/kimi-module.js
CHANGED
|
@@ -571,9 +571,24 @@ async function analyzeAndReact(text, useAdvancedLLM = true) {
|
|
| 571 |
if (useAdvancedLLM && isSystemReady && kimiLLM) {
|
| 572 |
try {
|
| 573 |
const providerPref = kimiDB ? await kimiDB.getPreference("llmProvider", "openrouter") : "openrouter";
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
if (apiKey && apiKey.trim() !== "") {
|
| 579 |
try {
|
|
@@ -640,9 +655,24 @@ async function analyzeAndReact(text, useAdvancedLLM = true) {
|
|
| 640 |
} catch (e) {}
|
| 641 |
// Still show API key message if no key is configured
|
| 642 |
const providerPref2 = kimiDB ? await kimiDB.getPreference("llmProvider", "openrouter") : "openrouter";
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 646 |
if (!apiKey || apiKey.trim() === "") {
|
| 647 |
response = window.KimiFallbackManager
|
| 648 |
? window.KimiFallbackManager.getFallbackMessage("api_missing")
|
|
@@ -666,9 +696,24 @@ async function analyzeAndReact(text, useAdvancedLLM = true) {
|
|
| 666 |
} else {
|
| 667 |
// System not ready - check if it's because of missing API key
|
| 668 |
const providerPref3 = kimiDB ? await kimiDB.getPreference("llmProvider", "openrouter") : "openrouter";
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
if (!apiKey || apiKey.trim() === "") {
|
| 673 |
response = window.KimiFallbackManager
|
| 674 |
? window.KimiFallbackManager.getFallbackMessage("api_missing")
|
|
|
|
| 571 |
if (useAdvancedLLM && isSystemReady && kimiLLM) {
|
| 572 |
try {
|
| 573 |
const providerPref = kimiDB ? await kimiDB.getPreference("llmProvider", "openrouter") : "openrouter";
|
| 574 |
+
let apiKey = null;
|
| 575 |
+
if (kimiDB) {
|
| 576 |
+
if (providerPref === "ollama") {
|
| 577 |
+
apiKey = "__local__"; // no key required
|
| 578 |
+
} else if (providerPref === "openrouter") {
|
| 579 |
+
apiKey = await kimiDB.getPreference("openrouterApiKey");
|
| 580 |
+
} else {
|
| 581 |
+
const keyPrefMap = {
|
| 582 |
+
openai: "apiKey_openai",
|
| 583 |
+
groq: "apiKey_groq",
|
| 584 |
+
together: "apiKey_together",
|
| 585 |
+
deepseek: "apiKey_deepseek",
|
| 586 |
+
"openai-compatible": "apiKey_custom"
|
| 587 |
+
};
|
| 588 |
+
const keyPref = keyPrefMap[providerPref] || "llmApiKey";
|
| 589 |
+
apiKey = await kimiDB.getPreference(keyPref);
|
| 590 |
+
}
|
| 591 |
+
}
|
| 592 |
|
| 593 |
if (apiKey && apiKey.trim() !== "") {
|
| 594 |
try {
|
|
|
|
| 655 |
} catch (e) {}
|
| 656 |
// Still show API key message if no key is configured
|
| 657 |
const providerPref2 = kimiDB ? await kimiDB.getPreference("llmProvider", "openrouter") : "openrouter";
|
| 658 |
+
let apiKey = null;
|
| 659 |
+
if (kimiDB) {
|
| 660 |
+
if (providerPref2 === "ollama") {
|
| 661 |
+
apiKey = "__local__";
|
| 662 |
+
} else if (providerPref2 === "openrouter") {
|
| 663 |
+
apiKey = await kimiDB.getPreference("openrouterApiKey");
|
| 664 |
+
} else {
|
| 665 |
+
const keyPrefMap = {
|
| 666 |
+
openai: "apiKey_openai",
|
| 667 |
+
groq: "apiKey_groq",
|
| 668 |
+
together: "apiKey_together",
|
| 669 |
+
deepseek: "apiKey_deepseek",
|
| 670 |
+
"openai-compatible": "apiKey_custom"
|
| 671 |
+
};
|
| 672 |
+
const keyPref = keyPrefMap[providerPref2] || "llmApiKey";
|
| 673 |
+
apiKey = await kimiDB.getPreference(keyPref);
|
| 674 |
+
}
|
| 675 |
+
}
|
| 676 |
if (!apiKey || apiKey.trim() === "") {
|
| 677 |
response = window.KimiFallbackManager
|
| 678 |
? window.KimiFallbackManager.getFallbackMessage("api_missing")
|
|
|
|
| 696 |
} else {
|
| 697 |
// System not ready - check if it's because of missing API key
|
| 698 |
const providerPref3 = kimiDB ? await kimiDB.getPreference("llmProvider", "openrouter") : "openrouter";
|
| 699 |
+
let apiKey = null;
|
| 700 |
+
if (kimiDB) {
|
| 701 |
+
if (providerPref3 === "ollama") {
|
| 702 |
+
apiKey = "__local__";
|
| 703 |
+
} else if (providerPref3 === "openrouter") {
|
| 704 |
+
apiKey = await kimiDB.getPreference("openrouterApiKey");
|
| 705 |
+
} else {
|
| 706 |
+
const keyPrefMap = {
|
| 707 |
+
openai: "apiKey_openai",
|
| 708 |
+
groq: "apiKey_groq",
|
| 709 |
+
together: "apiKey_together",
|
| 710 |
+
deepseek: "apiKey_deepseek",
|
| 711 |
+
"openai-compatible": "apiKey_custom"
|
| 712 |
+
};
|
| 713 |
+
const keyPref = keyPrefMap[providerPref3] || "llmApiKey";
|
| 714 |
+
apiKey = await kimiDB.getPreference(keyPref);
|
| 715 |
+
}
|
| 716 |
+
}
|
| 717 |
if (!apiKey || apiKey.trim() === "") {
|
| 718 |
response = window.KimiFallbackManager
|
| 719 |
? window.KimiFallbackManager.getFallbackMessage("api_missing")
|
kimi-js/kimi-script.js
CHANGED
|
@@ -157,9 +157,9 @@ document.addEventListener("DOMContentLoaded", async function () {
|
|
| 157 |
const savedBadge = ApiUi.savedBadge();
|
| 158 |
if (savedBadge) savedBadge.style.display = storedKey ? "inline" : "none";
|
| 159 |
ApiUi.clearStatus();
|
| 160 |
-
// Enable/disable Test button according to validation
|
| 161 |
const valid = !!(window.KIMI_VALIDATORS && window.KIMI_VALIDATORS.validateApiKey(storedKey || ""));
|
| 162 |
-
ApiUi.setTestEnabled(valid);
|
| 163 |
// Update dynamic label and placeholders using change handler logic
|
| 164 |
if (providerSelect && typeof providerSelect.dispatchEvent === "function") {
|
| 165 |
const ev = new Event("change");
|
|
@@ -244,11 +244,15 @@ document.addEventListener("DOMContentLoaded", async function () {
|
|
| 244 |
const keyPref = keyPrefMap[provider] || "llmApiKey";
|
| 245 |
const storedKey = await window.kimiDB.getPreference(keyPref, "");
|
| 246 |
if (apiKeyInput) apiKeyInput.value = storedKey || "";
|
| 247 |
-
const color = storedKey && storedKey.length > 0 ? "#4caf50" : "#9e9e9e";
|
| 248 |
ApiUi.setPresence(color);
|
| 249 |
// Changing provider invalidates previous test state
|
| 250 |
ApiUi.setTestPresence("#9e9e9e");
|
| 251 |
-
ApiUi.setTestEnabled(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
// Dynamic label per provider
|
| 254 |
if (apiKeyLabel) {
|
|
@@ -658,33 +662,38 @@ document.addEventListener("DOMContentLoaded", async function () {
|
|
| 658 |
|
| 659 |
if (!statusSpan) return;
|
| 660 |
|
| 661 |
-
if (!apiKey) {
|
| 662 |
statusSpan.textContent = window.kimiI18nManager?.t("api_key_missing") || "API key missing";
|
| 663 |
statusSpan.style.color = "#ff6b6b";
|
| 664 |
return;
|
| 665 |
}
|
| 666 |
|
| 667 |
// Validate API key format before saving/testing
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
|
|
|
|
|
|
|
|
|
| 674 |
}
|
| 675 |
|
| 676 |
if (window.kimiDB) {
|
| 677 |
-
// Save API key under provider-specific preference key
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
|
|
|
|
|
|
| 688 |
await window.kimiDB.setPreference("llmProvider", provider);
|
| 689 |
if (baseUrl) await window.kimiDB.setPreference("llmBaseUrl", baseUrl);
|
| 690 |
if (modelId) await window.kimiDB.setPreference("llmModelId", modelId);
|
|
@@ -770,7 +779,7 @@ document.addEventListener("DOMContentLoaded", async function () {
|
|
| 770 |
const value = input.value.trim();
|
| 771 |
// Update Test button state immediately
|
| 772 |
const validNow = !!(window.KIMI_VALIDATORS && window.KIMI_VALIDATORS.validateApiKey(value));
|
| 773 |
-
ApiUi.setTestEnabled(validNow);
|
| 774 |
if (window.kimiDB) {
|
| 775 |
try {
|
| 776 |
await window.kimiDB.setPreference(keyPref, value);
|
|
|
|
| 157 |
const savedBadge = ApiUi.savedBadge();
|
| 158 |
if (savedBadge) savedBadge.style.display = storedKey ? "inline" : "none";
|
| 159 |
ApiUi.clearStatus();
|
| 160 |
+
// Enable/disable Test button according to validation (Ollama does not require API key)
|
| 161 |
const valid = !!(window.KIMI_VALIDATORS && window.KIMI_VALIDATORS.validateApiKey(storedKey || ""));
|
| 162 |
+
ApiUi.setTestEnabled(provider === "ollama" ? true : valid);
|
| 163 |
// Update dynamic label and placeholders using change handler logic
|
| 164 |
if (providerSelect && typeof providerSelect.dispatchEvent === "function") {
|
| 165 |
const ev = new Event("change");
|
|
|
|
| 244 |
const keyPref = keyPrefMap[provider] || "llmApiKey";
|
| 245 |
const storedKey = await window.kimiDB.getPreference(keyPref, "");
|
| 246 |
if (apiKeyInput) apiKeyInput.value = storedKey || "";
|
| 247 |
+
const color = provider === "ollama" ? "#9e9e9e" : storedKey && storedKey.length > 0 ? "#4caf50" : "#9e9e9e";
|
| 248 |
ApiUi.setPresence(color);
|
| 249 |
// Changing provider invalidates previous test state
|
| 250 |
ApiUi.setTestPresence("#9e9e9e");
|
| 251 |
+
ApiUi.setTestEnabled(
|
| 252 |
+
provider === "ollama"
|
| 253 |
+
? true
|
| 254 |
+
: !!(window.KIMI_VALIDATORS && window.KIMI_VALIDATORS.validateApiKey(storedKey || ""))
|
| 255 |
+
);
|
| 256 |
|
| 257 |
// Dynamic label per provider
|
| 258 |
if (apiKeyLabel) {
|
|
|
|
| 662 |
|
| 663 |
if (!statusSpan) return;
|
| 664 |
|
| 665 |
+
if (provider !== "ollama" && !apiKey) {
|
| 666 |
statusSpan.textContent = window.kimiI18nManager?.t("api_key_missing") || "API key missing";
|
| 667 |
statusSpan.style.color = "#ff6b6b";
|
| 668 |
return;
|
| 669 |
}
|
| 670 |
|
| 671 |
// Validate API key format before saving/testing
|
| 672 |
+
if (provider !== "ollama") {
|
| 673 |
+
const isValid = (window.KIMI_VALIDATORS && window.KIMI_VALIDATORS.validateApiKey(apiKey)) || false;
|
| 674 |
+
if (!isValid) {
|
| 675 |
+
statusSpan.textContent =
|
| 676 |
+
window.kimiI18nManager?.t("api_key_invalid_format") ||
|
| 677 |
+
"Invalid API key format (must start with sk-or-v1-)";
|
| 678 |
+
statusSpan.style.color = "#ff6b6b";
|
| 679 |
+
return;
|
| 680 |
+
}
|
| 681 |
}
|
| 682 |
|
| 683 |
if (window.kimiDB) {
|
| 684 |
+
// Save API key under provider-specific preference key (skip for Ollama)
|
| 685 |
+
if (provider !== "ollama") {
|
| 686 |
+
const keyPrefMap = {
|
| 687 |
+
openrouter: "openrouterApiKey",
|
| 688 |
+
openai: "apiKey_openai",
|
| 689 |
+
groq: "apiKey_groq",
|
| 690 |
+
together: "apiKey_together",
|
| 691 |
+
deepseek: "apiKey_deepseek",
|
| 692 |
+
"openai-compatible": "apiKey_custom"
|
| 693 |
+
};
|
| 694 |
+
const keyPref = keyPrefMap[provider] || "llmApiKey";
|
| 695 |
+
await window.kimiDB.setPreference(keyPref, apiKey);
|
| 696 |
+
}
|
| 697 |
await window.kimiDB.setPreference("llmProvider", provider);
|
| 698 |
if (baseUrl) await window.kimiDB.setPreference("llmBaseUrl", baseUrl);
|
| 699 |
if (modelId) await window.kimiDB.setPreference("llmModelId", modelId);
|
|
|
|
| 779 |
const value = input.value.trim();
|
| 780 |
// Update Test button state immediately
|
| 781 |
const validNow = !!(window.KIMI_VALIDATORS && window.KIMI_VALIDATORS.validateApiKey(value));
|
| 782 |
+
ApiUi.setTestEnabled(provider === "ollama" ? true : validNow);
|
| 783 |
if (window.kimiDB) {
|
| 784 |
try {
|
| 785 |
await window.kimiDB.setPreference(keyPref, value);
|