-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
180 lines (153 loc) · 5.43 KB
/
Copy pathoptions.js
File metadata and controls
180 lines (153 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const providerInput = document.getElementById("provider");
const apiKeyInput = document.getElementById("apiKey");
const deepseekApiKeyInput = document.getElementById("deepseekApiKey");
const saveBtn = document.getElementById("saveBtn");
const testBtn = document.getElementById("testBtn");
const diagnosticModeInput = document.getElementById("diagnosticMode");
const statusEl = document.getElementById("status");
function normalizeProvider(provider) {
return String(provider || "").trim().toLowerCase() === "deepseek" ? "deepseek" : "moonshot";
}
function normalizeApiKey(rawKey) {
if (!rawKey) {
return "";
}
return String(rawKey)
.replace(/[\u200B-\u200D\uFEFF]/g, "")
.trim()
.replace(/^['"`]+|['"`]+$/g, "")
.replace(/^Bearer\s+/i, "")
.replace(/\s+/g, "");
}
function setStatus(text, isError = false) {
statusEl.style.color = isError ? "#b42318" : "#0a7f28";
statusEl.textContent = text;
}
function formatDiagnostics(diagnostics) {
if (!diagnostics || !Array.isArray(diagnostics.steps) || diagnostics.steps.length === 0) {
return "Diagnostic: no request details captured.";
}
const lines = [];
if (diagnostics.selectedProvider) {
lines.push(`Provider: ${diagnostics.selectedProvider}`);
}
if (diagnostics.selectedModel) {
lines.push(`Model: ${diagnostics.selectedModel}`);
}
diagnostics.steps.forEach((step, index) => {
lines.push(`${index + 1}. endpoint: ${step.endpoint || "N/A"}`);
lines.push(` status: ${typeof step.status === "number" && step.status >= 0 ? step.status : "N/A"}`);
if (step.error) {
lines.push(` error: ${step.error}`);
}
});
return lines.join("\n");
}
async function getStorageValue(area, keys) {
try {
return await chrome.storage[area].get(keys);
} catch {
return {};
}
}
async function setStorageValue(area, value) {
try {
await chrome.storage[area].set(value);
return true;
} catch {
return false;
}
}
async function load() {
const [syncData, localData] = await Promise.all([
getStorageValue("sync", ["aiProvider", "provider", "kimiApiKey", "apiKey", "moonshotApiKey", "deepseekApiKey", "kimiDiagnosticMode"]),
getStorageValue("local", ["aiProvider", "provider", "kimiApiKey", "apiKey", "moonshotApiKey", "deepseekApiKey", "kimiDiagnosticMode"])
]);
providerInput.value = normalizeProvider(syncData.aiProvider || localData.aiProvider || syncData.provider || localData.provider || "moonshot");
const moonshotApiKey =
syncData.kimiApiKey ||
localData.kimiApiKey ||
syncData.apiKey ||
localData.apiKey ||
syncData.moonshotApiKey ||
localData.moonshotApiKey ||
"";
apiKeyInput.value = moonshotApiKey;
deepseekApiKeyInput.value = syncData.deepseekApiKey || localData.deepseekApiKey || "";
diagnosticModeInput.checked = Boolean(syncData.kimiDiagnosticMode ?? localData.kimiDiagnosticMode ?? false);
}
async function save() {
const provider = normalizeProvider(providerInput.value);
const moonshotKey = normalizeApiKey(apiKeyInput.value);
const deepseekKey = normalizeApiKey(deepseekApiKeyInput.value);
const diagnosticMode = Boolean(diagnosticModeInput.checked);
const [syncOk, localOk] = await Promise.all([
setStorageValue("sync", {
aiProvider: provider,
provider,
kimiApiKey: moonshotKey,
deepseekApiKey: deepseekKey,
kimiDiagnosticMode: diagnosticMode
}),
setStorageValue("local", {
aiProvider: provider,
provider,
kimiApiKey: moonshotKey,
deepseekApiKey: deepseekKey,
kimiDiagnosticMode: diagnosticMode
})
]);
providerInput.value = provider;
apiKeyInput.value = moonshotKey;
deepseekApiKeyInput.value = deepseekKey;
if (!syncOk && !localOk) {
setStatus("Save failed: unable to write storage in this context.", true);
return false;
}
if (!syncOk && localOk) {
setStatus("Saved to local storage (sync unavailable).", false);
} else {
setStatus("Saved.", false);
}
setTimeout(() => {
setStatus("");
}, 1800);
return true;
}
async function testConnection() {
const provider = normalizeProvider(providerInput.value);
const moonshotKey = normalizeApiKey(apiKeyInput.value);
const deepseekKey = normalizeApiKey(deepseekApiKeyInput.value);
const key = provider === "deepseek" ? deepseekKey : moonshotKey;
const diagnose = Boolean(diagnosticModeInput.checked);
if (!key) {
setStatus(`Test failed: please paste ${provider === "deepseek" ? "DeepSeek" : "Moonshot/Kimi"} API key first.`, true);
return;
}
setStatus("Testing...", false);
try {
await save();
const response = await chrome.runtime.sendMessage({ type: "kimi-test-auth", apiKey: key, provider, diagnose });
if (!response?.ok) {
const error = new Error(response?.error || "Unknown error");
error.diagnostics = response?.diagnostics || null;
throw error;
}
if (diagnose) {
const details = formatDiagnostics(response?.diagnostics || null);
setStatus(`Connection OK. API key is valid.\n${details}`, false);
return;
}
setStatus("Connection OK. API key is valid.", false);
} catch (error) {
if (diagnose) {
const details = formatDiagnostics(error?.diagnostics || null);
setStatus(`Test failed: ${error.message || "Unknown error"}\n${details}`, true);
return;
}
setStatus(`Test failed: ${error.message || "Unknown error"}`, true);
}
}
saveBtn.addEventListener("click", save);
testBtn.addEventListener("click", testConnection);
load();