@madgagarin/pi-agentrouter 1.0.1 → 1.0.2
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.
- package/index.ts +18 -7
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -27,10 +27,20 @@ export function saveConfig(cfg: AgentRouterConfig): void {
|
|
|
27
27
|
}
|
|
28
28
|
fs.writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2), "utf-8");
|
|
29
29
|
} catch {}
|
|
30
|
+
export function normalizeApiKey(key?: string): string {
|
|
31
|
+
if (!key) return "";
|
|
32
|
+
let clean = key.trim().replace(/^["']|["']$/g, "").trim();
|
|
33
|
+
if (clean.toLowerCase().startsWith("bearer ")) {
|
|
34
|
+
clean = clean.slice(7).trim();
|
|
35
|
+
}
|
|
36
|
+
if (clean.startsWith("sk-")) {
|
|
37
|
+
clean = clean.slice(3).trim();
|
|
38
|
+
}
|
|
39
|
+
return clean;
|
|
30
40
|
}
|
|
31
41
|
|
|
32
42
|
const initialConfig = loadConfig();
|
|
33
|
-
let currentApiKey = process.env.AGENTROUTER_API_KEY || initialConfig.apiKey || "";
|
|
43
|
+
let currentApiKey = normalizeApiKey(process.env.AGENTROUTER_API_KEY || initialConfig.apiKey || "");
|
|
34
44
|
let minIntervalMs = initialConfig.minIntervalMs ?? 2500;
|
|
35
45
|
let lastRequestTime = 0;
|
|
36
46
|
|
|
@@ -201,15 +211,16 @@ export default function (pi: ExtensionAPI) {
|
|
|
201
211
|
const action = parts[0]?.toLowerCase();
|
|
202
212
|
|
|
203
213
|
if (action === "key" || action === "set-key") {
|
|
204
|
-
const
|
|
205
|
-
|
|
214
|
+
const rawKey = parts[1]?.trim();
|
|
215
|
+
const cleanKey = normalizeApiKey(rawKey);
|
|
216
|
+
if (!cleanKey || cleanKey.length < 5) {
|
|
206
217
|
ctx.ui.notify("Please provide a valid API key: /agentrouter key <your-key>", "error");
|
|
207
218
|
return;
|
|
208
219
|
}
|
|
209
|
-
currentApiKey =
|
|
210
|
-
saveConfig({ apiKey:
|
|
211
|
-
registerAgentRouterProviders(
|
|
212
|
-
ctx.ui.notify("AgentRouter API key updated successfully for all models.", "info");
|
|
220
|
+
currentApiKey = cleanKey;
|
|
221
|
+
saveConfig({ apiKey: cleanKey, minIntervalMs });
|
|
222
|
+
registerAgentRouterProviders(cleanKey);
|
|
223
|
+
ctx.ui.notify("AgentRouter API key updated and normalized successfully for all models.", "info");
|
|
213
224
|
return;
|
|
214
225
|
}
|
|
215
226
|
|
package/package.json
CHANGED