@routstr/sdk 0.1.5 → 0.1.6
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/dist/client/index.js +36 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +36 -2
- package/dist/client/index.mjs.map +1 -1
- package/dist/discovery/index.d.mts +3 -1
- package/dist/discovery/index.d.ts +3 -1
- package/dist/discovery/index.js +17 -13
- package/dist/discovery/index.js.map +1 -1
- package/dist/discovery/index.mjs +17 -13
- package/dist/discovery/index.mjs.map +1 -1
- package/dist/index.js +53 -15
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -15
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -123,7 +123,7 @@ var ModelManager = class _ModelManager {
|
|
|
123
123
|
const manager = new _ModelManager(adapter, config);
|
|
124
124
|
const torMode = options.torMode ?? false;
|
|
125
125
|
const forceRefresh = options.forceRefresh ?? false;
|
|
126
|
-
const providers = await manager.bootstrapProviders(torMode);
|
|
126
|
+
const providers = await manager.bootstrapProviders(torMode, forceRefresh);
|
|
127
127
|
await manager.fetchModels(providers, forceRefresh);
|
|
128
128
|
return manager;
|
|
129
129
|
}
|
|
@@ -131,17 +131,20 @@ var ModelManager = class _ModelManager {
|
|
|
131
131
|
* Bootstrap provider list from the provider directory
|
|
132
132
|
* First tries to fetch from Nostr (kind 30421), falls back to HTTP
|
|
133
133
|
* @param torMode Whether running in Tor context
|
|
134
|
+
* @param forceRefresh Ignore provider cache and refresh provider sources
|
|
134
135
|
* @returns Array of provider base URLs
|
|
135
136
|
* @throws ProviderBootstrapError if all providers fail to fetch
|
|
136
137
|
*/
|
|
137
|
-
async bootstrapProviders(torMode = false) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
138
|
+
async bootstrapProviders(torMode = false, forceRefresh = false) {
|
|
139
|
+
if (!forceRefresh) {
|
|
140
|
+
const cachedUrls = this.adapter.getBaseUrlsList();
|
|
141
|
+
if (cachedUrls.length > 0) {
|
|
142
|
+
const lastUpdate = this.adapter.getBaseUrlsLastUpdate();
|
|
143
|
+
const cacheValid = lastUpdate && Date.now() - lastUpdate <= this.cacheTTL;
|
|
144
|
+
if (cacheValid) {
|
|
145
|
+
await this.fetchRoutstr21Models(forceRefresh);
|
|
146
|
+
return this.filterBaseUrlsForTor(cachedUrls, torMode);
|
|
147
|
+
}
|
|
145
148
|
}
|
|
146
149
|
}
|
|
147
150
|
try {
|
|
@@ -150,13 +153,13 @@ var ModelManager = class _ModelManager {
|
|
|
150
153
|
const filtered = this.filterBaseUrlsForTor(nostrProviders, torMode);
|
|
151
154
|
this.adapter.setBaseUrlsList(filtered);
|
|
152
155
|
this.adapter.setBaseUrlsLastUpdate(Date.now());
|
|
153
|
-
await this.fetchRoutstr21Models();
|
|
156
|
+
await this.fetchRoutstr21Models(forceRefresh);
|
|
154
157
|
return filtered;
|
|
155
158
|
}
|
|
156
159
|
} catch (e) {
|
|
157
160
|
console.warn("Nostr bootstrap failed, falling back to HTTP:", e);
|
|
158
161
|
}
|
|
159
|
-
return this.bootstrapFromHttp(torMode);
|
|
162
|
+
return this.bootstrapFromHttp(torMode, forceRefresh);
|
|
160
163
|
}
|
|
161
164
|
/**
|
|
162
165
|
* Bootstrap providers from Nostr network (kind 30421)
|
|
@@ -252,9 +255,10 @@ var ModelManager = class _ModelManager {
|
|
|
252
255
|
/**
|
|
253
256
|
* Bootstrap providers from HTTP endpoint
|
|
254
257
|
* @param torMode Whether running in Tor context
|
|
258
|
+
* @param forceRefresh Ignore routstr21 cache and fetch fresh data
|
|
255
259
|
* @returns Array of provider base URLs
|
|
256
260
|
*/
|
|
257
|
-
async bootstrapFromHttp(torMode) {
|
|
261
|
+
async bootstrapFromHttp(torMode, forceRefresh = false) {
|
|
258
262
|
try {
|
|
259
263
|
const res = await fetch(this.providerDirectoryUrl);
|
|
260
264
|
if (!res.ok) {
|
|
@@ -282,7 +286,7 @@ var ModelManager = class _ModelManager {
|
|
|
282
286
|
if (list.length > 0) {
|
|
283
287
|
this.adapter.setBaseUrlsList(list);
|
|
284
288
|
this.adapter.setBaseUrlsLastUpdate(Date.now());
|
|
285
|
-
await this.fetchRoutstr21Models();
|
|
289
|
+
await this.fetchRoutstr21Models(forceRefresh);
|
|
286
290
|
}
|
|
287
291
|
return list;
|
|
288
292
|
} catch (e) {
|
|
@@ -2853,8 +2857,7 @@ var RoutstrClient = class {
|
|
|
2853
2857
|
);
|
|
2854
2858
|
}
|
|
2855
2859
|
}
|
|
2856
|
-
if (
|
|
2857
|
-
console.log("RESPONSFE ", responseBody);
|
|
2860
|
+
if (status === 402 && !tryNextProvider && (this.mode === "apikeys" || this.mode === "lazyrefund")) {
|
|
2858
2861
|
const topupResult = await this.balanceManager.topUp({
|
|
2859
2862
|
mintUrl,
|
|
2860
2863
|
baseUrl,
|
|
@@ -2897,6 +2900,41 @@ var RoutstrClient = class {
|
|
|
2897
2900
|
headers: this._withAuthHeader(params.baseHeaders, params.token)
|
|
2898
2901
|
});
|
|
2899
2902
|
}
|
|
2903
|
+
const isInsufficientBalance413 = status === 413 && responseBody?.includes("Insufficient balance");
|
|
2904
|
+
if (isInsufficientBalance413 && !tryNextProvider && this.mode === "apikeys") {
|
|
2905
|
+
let retryToken = params.token;
|
|
2906
|
+
try {
|
|
2907
|
+
const latestBalanceInfo = await this.balanceManager.getTokenBalance(
|
|
2908
|
+
params.token,
|
|
2909
|
+
baseUrl
|
|
2910
|
+
);
|
|
2911
|
+
const latestTokenBalance = latestBalanceInfo.unit === "msat" ? latestBalanceInfo.amount / 1e3 : latestBalanceInfo.amount;
|
|
2912
|
+
if (latestBalanceInfo.apiKey) {
|
|
2913
|
+
const storedApiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
|
|
2914
|
+
if (storedApiKeyEntry?.key !== latestBalanceInfo.apiKey) {
|
|
2915
|
+
if (storedApiKeyEntry) {
|
|
2916
|
+
this.storageAdapter.removeApiKey(baseUrl);
|
|
2917
|
+
}
|
|
2918
|
+
this.storageAdapter.setApiKey(baseUrl, latestBalanceInfo.apiKey);
|
|
2919
|
+
}
|
|
2920
|
+
retryToken = latestBalanceInfo.apiKey;
|
|
2921
|
+
}
|
|
2922
|
+
if (latestTokenBalance >= 0) {
|
|
2923
|
+
this.storageAdapter.updateApiKeyBalance(baseUrl, latestTokenBalance);
|
|
2924
|
+
}
|
|
2925
|
+
} catch (error) {
|
|
2926
|
+
this._log(
|
|
2927
|
+
"WARN",
|
|
2928
|
+
`[RoutstrClient] _handleErrorResponse: Failed to refresh API key after 413 insufficient balance for ${baseUrl}`,
|
|
2929
|
+
error
|
|
2930
|
+
);
|
|
2931
|
+
}
|
|
2932
|
+
return this._makeRequest({
|
|
2933
|
+
...params,
|
|
2934
|
+
token: retryToken,
|
|
2935
|
+
headers: this._withAuthHeader(params.baseHeaders, retryToken)
|
|
2936
|
+
});
|
|
2937
|
+
}
|
|
2900
2938
|
if ((status === 401 || status === 403 || status === 413 || status === 400 || status === 500 || status === 502 || status === 503 || status === 504 || status === 521) && !tryNextProvider) {
|
|
2901
2939
|
this._log(
|
|
2902
2940
|
"DEBUG",
|