provider-kit 1.1.0 → 1.1.1
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "provider-kit",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "42 LLM provider unified API — one interface for OpenAI, Anthropic, Ollama, OpenRouter, and 38 more. Built-in retry, timeout, and error handling.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -17,8 +17,10 @@ import { persistentConfig } from '../core/persistent-config.js';
|
|
|
17
17
|
|
|
18
18
|
class ProviderRegistry {
|
|
19
19
|
constructor() {
|
|
20
|
-
this.providers = new Map();
|
|
21
|
-
this.models = new Map();
|
|
20
|
+
this.providers = new Map();
|
|
21
|
+
this.models = new Map();
|
|
22
|
+
this._modelTimestamps = new Map(); // providerId -> last fetch timestamp
|
|
23
|
+
this._modelCacheTtl = 60000; // 60s
|
|
22
24
|
this.presets = PRESET_PROVIDERS;
|
|
23
25
|
}
|
|
24
26
|
|
|
@@ -125,25 +127,30 @@ class ProviderRegistry {
|
|
|
125
127
|
try {
|
|
126
128
|
const models = await provider.fetchModels();
|
|
127
129
|
this.models.set(providerId, models);
|
|
130
|
+
this._modelTimestamps.set(providerId, Date.now());
|
|
128
131
|
return models;
|
|
129
132
|
} catch (e) {
|
|
130
|
-
// 如果失败,返回本地模型列表
|
|
131
133
|
return provider.getModels();
|
|
132
134
|
}
|
|
133
135
|
}
|
|
134
136
|
|
|
135
137
|
/**
|
|
136
|
-
*
|
|
138
|
+
* 获取模型列表(缓存 60 秒,过期自动刷新)
|
|
137
139
|
*/
|
|
138
|
-
getModels(providerId) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
140
|
+
async getModels(providerId) {
|
|
141
|
+
const cached = this.models.get(providerId);
|
|
142
|
+
const lastFetch = this._modelTimestamps.get(providerId) || 0;
|
|
143
|
+
const stale = Date.now() - lastFetch > this._modelCacheTtl;
|
|
144
|
+
|
|
145
|
+
if (cached && !stale) return cached;
|
|
146
|
+
|
|
147
|
+
// 尝试刷新,失败则返回缓存
|
|
148
|
+
if (this.getProvider(providerId)) {
|
|
149
|
+
const fresh = await this.refreshModels(providerId).catch(() => cached || []);
|
|
150
|
+
return fresh;
|
|
142
151
|
}
|
|
143
152
|
|
|
144
|
-
|
|
145
|
-
const provider = this.getProvider(providerId);
|
|
146
|
-
return provider ? provider.getModels() : [];
|
|
153
|
+
return cached || [];
|
|
147
154
|
}
|
|
148
155
|
|
|
149
156
|
/**
|