@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.
Files changed (2) hide show
  1. package/index.ts +18 -7
  2. 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 newKey = parts[1]?.trim();
205
- if (!newKey || newKey.length < 5) {
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 = newKey;
210
- saveConfig({ apiKey: newKey, minIntervalMs });
211
- registerAgentRouterProviders(newKey);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@madgagarin/pi-agentrouter",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Pi coding agent extension for AgentRouter (GPT-5.6 Sol & Claude Opus 5) with rate-limit pacing and caching.",
5
5
  "publishConfig": {
6
6
  "access": "public"