pi-keyrouter 0.2.2 → 0.2.3
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 +21 -15
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -49,16 +49,22 @@ export default function keyRouterExtension(pi: ExtensionAPI): void {
|
|
|
49
49
|
let notify: ((text: string, level: "info" | "warning" | "error") => void) | undefined;
|
|
50
50
|
let activationNotified = false;
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Get-or-create the runtime for a provider. Keyed by the RESOLVED
|
|
54
|
+
* name (the authStorage id, e.g. "zai"), but populated from the
|
|
55
|
+
* provider config passed in directly (avoids name-mismatch bugs).
|
|
56
|
+
*/
|
|
57
|
+
function ensureRuntime(
|
|
58
|
+
resolvedName: string,
|
|
59
|
+
providerCfg: { keys: ReadonlyArray<{ name: string; value: string }> },
|
|
60
|
+
): ProviderRuntime {
|
|
61
|
+
let rt = runtimes.get(resolvedName);
|
|
54
62
|
if (rt) return rt;
|
|
55
|
-
const providerCfg = cfg.providers.find((p) => p.name === providerName);
|
|
56
|
-
if (!providerCfg) return undefined;
|
|
57
63
|
rt = {
|
|
58
64
|
keys: initKeyStates(providerCfg.keys),
|
|
59
65
|
currentIndex: -1,
|
|
60
66
|
};
|
|
61
|
-
runtimes.set(
|
|
67
|
+
runtimes.set(resolvedName, rt);
|
|
62
68
|
return rt;
|
|
63
69
|
}
|
|
64
70
|
|
|
@@ -82,15 +88,15 @@ export default function keyRouterExtension(pi: ExtensionAPI): void {
|
|
|
82
88
|
const authStorage = ctx.modelRegistry.authStorage;
|
|
83
89
|
let newlyBootstrapped = 0;
|
|
84
90
|
for (const p of config.providers) {
|
|
85
|
-
const
|
|
91
|
+
const resolvedName = resolveProviderName(p.name);
|
|
86
92
|
// Skip providers we've already bootstrapped
|
|
87
|
-
if (runtimes.has(
|
|
88
|
-
if (bootstrap(
|
|
89
|
-
const rt = runtimes.get(
|
|
93
|
+
if (runtimes.has(resolvedName)) continue;
|
|
94
|
+
if (bootstrap(resolvedName, p)) {
|
|
95
|
+
const rt = runtimes.get(resolvedName);
|
|
90
96
|
if (rt && rt.currentIndex >= 0) {
|
|
91
97
|
const key = rt.keys[rt.currentIndex];
|
|
92
98
|
if (key) {
|
|
93
|
-
authStorage.setRuntimeApiKey(
|
|
99
|
+
authStorage.setRuntimeApiKey(resolvedName, key.value);
|
|
94
100
|
newlyBootstrapped++;
|
|
95
101
|
}
|
|
96
102
|
}
|
|
@@ -107,11 +113,11 @@ export default function keyRouterExtension(pi: ExtensionAPI): void {
|
|
|
107
113
|
}
|
|
108
114
|
|
|
109
115
|
/** Set the initial key for a provider on first use. */
|
|
110
|
-
function bootstrap(
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
116
|
+
function bootstrap(
|
|
117
|
+
resolvedName: string,
|
|
118
|
+
providerCfg: { keys: ReadonlyArray<{ name: string; value: string }> },
|
|
119
|
+
): boolean {
|
|
120
|
+
const rt = ensureRuntime(resolvedName, providerCfg);
|
|
115
121
|
if (rt.currentIndex >= 0) return true; // already bootstrapped
|
|
116
122
|
const idx = pickNextKey(rt.keys, 0, Date.now());
|
|
117
123
|
if (idx < 0) return false;
|
package/package.json
CHANGED