@routstr/sdk 0.3.2 → 0.3.4
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.d.mts +9 -5
- package/dist/client/index.d.ts +9 -5
- package/dist/client/index.js +70 -131
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +70 -131
- package/dist/client/index.mjs.map +1 -1
- package/dist/discovery/index.d.mts +8 -2
- package/dist/discovery/index.d.ts +8 -2
- package/dist/discovery/index.js +27 -16
- package/dist/discovery/index.js.map +1 -1
- package/dist/discovery/index.mjs +27 -16
- package/dist/discovery/index.mjs.map +1 -1
- package/dist/index.d.mts +10 -8
- package/dist/index.d.ts +10 -8
- package/dist/index.js +141 -185
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +140 -186
- package/dist/index.mjs.map +1 -1
- package/dist/{interfaces-C5fLD3jB.d.mts → interfaces-Bp0Ngmqv.d.mts} +1 -1
- package/dist/{interfaces-BxDEka72.d.ts → interfaces-CIfd_phZ.d.ts} +1 -1
- package/dist/{interfaces-BWJJTCXO.d.mts → interfaces-Cxi8R4TT.d.mts} +1 -1
- package/dist/{interfaces-B62Rw-dd.d.ts → interfaces-D2FDCLyP.d.ts} +1 -1
- package/dist/storage/index.d.mts +9 -7
- package/dist/storage/index.d.ts +9 -7
- package/dist/storage/index.js +51 -34
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/index.mjs +51 -34
- package/dist/storage/index.mjs.map +1 -1
- package/dist/{store-h7m23ffq.d.ts → store-BD5zF9Hp.d.ts} +4 -4
- package/dist/{store-DGeLPv9E.d.mts → store-CBSyK2qg.d.mts} +4 -4
- package/dist/{types-BYj_8c5c.d.mts → types-DPQM6tIG.d.mts} +10 -1
- package/dist/{types-BYj_8c5c.d.ts → types-DPQM6tIG.d.ts} +10 -1
- package/dist/wallet/index.d.mts +8 -6
- package/dist/wallet/index.d.ts +8 -6
- package/dist/wallet/index.js +43 -52
- package/dist/wallet/index.js.map +1 -1
- package/dist/wallet/index.mjs +43 -52
- package/dist/wallet/index.mjs.map +1 -1
- package/package.json +3 -2
package/dist/client/index.mjs
CHANGED
|
@@ -3,6 +3,19 @@ import { createStore } from 'zustand/vanilla';
|
|
|
3
3
|
import { Transform } from 'stream';
|
|
4
4
|
import { StringDecoder } from 'string_decoder';
|
|
5
5
|
|
|
6
|
+
// core/types.ts
|
|
7
|
+
function makeConsoleLogger(prefix) {
|
|
8
|
+
const fmt = (args) => prefix ? [prefix, ...args] : args;
|
|
9
|
+
return {
|
|
10
|
+
log: (...args) => console.log(...fmt(args)),
|
|
11
|
+
warn: (...args) => console.warn(...fmt(args)),
|
|
12
|
+
error: (...args) => console.error(...fmt(args)),
|
|
13
|
+
debug: (...args) => console.log(...fmt(args)),
|
|
14
|
+
child: (p) => makeConsoleLogger(prefix ? `${prefix}:${p}` : p)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
var consoleLogger = makeConsoleLogger();
|
|
18
|
+
|
|
6
19
|
// core/errors.ts
|
|
7
20
|
var InsufficientBalanceError = class extends Error {
|
|
8
21
|
constructor(required, available, maxMintBalance = 0, maxMintUrl = "", customMessage) {
|
|
@@ -102,14 +115,16 @@ function selectMintWithBalance(balances, units, amount, excludeMints = []) {
|
|
|
102
115
|
return { selectedMintUrl: null, selectedMintBalance: 0 };
|
|
103
116
|
}
|
|
104
117
|
var CashuSpender = class {
|
|
105
|
-
constructor(walletAdapter, storageAdapter, _providerRegistry, balanceManager) {
|
|
118
|
+
constructor(walletAdapter, storageAdapter, _providerRegistry, balanceManager, logger) {
|
|
106
119
|
this.walletAdapter = walletAdapter;
|
|
107
120
|
this.storageAdapter = storageAdapter;
|
|
108
121
|
this._providerRegistry = _providerRegistry;
|
|
109
122
|
this.balanceManager = balanceManager;
|
|
123
|
+
this.logger = (logger ?? consoleLogger).child("CashuSpender");
|
|
110
124
|
}
|
|
111
125
|
_isBusy = false;
|
|
112
126
|
debugLevel = "WARN";
|
|
127
|
+
logger;
|
|
113
128
|
async receiveToken(token) {
|
|
114
129
|
try {
|
|
115
130
|
const result = await this.walletAdapter.receiveToken(token);
|
|
@@ -205,13 +220,13 @@ var CashuSpender = class {
|
|
|
205
220
|
if (levelPriority[level] >= levelPriority[this.debugLevel]) {
|
|
206
221
|
switch (level) {
|
|
207
222
|
case "DEBUG":
|
|
208
|
-
|
|
223
|
+
this.logger.log(...args);
|
|
209
224
|
break;
|
|
210
225
|
case "WARN":
|
|
211
|
-
|
|
226
|
+
this.logger.warn(...args);
|
|
212
227
|
break;
|
|
213
228
|
case "ERROR":
|
|
214
|
-
|
|
229
|
+
this.logger.error(...args);
|
|
215
230
|
break;
|
|
216
231
|
}
|
|
217
232
|
}
|
|
@@ -638,10 +653,11 @@ var CashuSpender = class {
|
|
|
638
653
|
|
|
639
654
|
// wallet/BalanceManager.ts
|
|
640
655
|
var BalanceManager = class _BalanceManager {
|
|
641
|
-
constructor(walletAdapter, storageAdapter, providerRegistry, cashuSpender) {
|
|
656
|
+
constructor(walletAdapter, storageAdapter, providerRegistry, cashuSpender, logger) {
|
|
642
657
|
this.walletAdapter = walletAdapter;
|
|
643
658
|
this.storageAdapter = storageAdapter;
|
|
644
659
|
this.providerRegistry = providerRegistry;
|
|
660
|
+
this.logger = (logger ?? consoleLogger).child("BalanceManager");
|
|
645
661
|
if (cashuSpender) {
|
|
646
662
|
this.cashuSpender = cashuSpender;
|
|
647
663
|
} else {
|
|
@@ -658,6 +674,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
658
674
|
providerWalletOps = /* @__PURE__ */ new Map();
|
|
659
675
|
/** Cooldown (ms) between opposite operations on the same provider */
|
|
660
676
|
static PROVIDER_WALLET_COOLDOWN_MS = 1e4;
|
|
677
|
+
logger;
|
|
661
678
|
/**
|
|
662
679
|
* Check whether a wallet operation (topup/refund) may run for a provider.
|
|
663
680
|
* Returns the reason when blocked.
|
|
@@ -732,7 +749,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
732
749
|
const { mintUrl, baseUrl, apiKey, forceRefund } = options;
|
|
733
750
|
const guard = this._canRunProviderWalletOperation(baseUrl, "refund");
|
|
734
751
|
if (!guard.allowed) {
|
|
735
|
-
|
|
752
|
+
this.logger.log(`Skipping refund for ${baseUrl} - ${guard.reason}`);
|
|
736
753
|
return { success: false, message: guard.reason };
|
|
737
754
|
}
|
|
738
755
|
this._beginProviderWalletOperation(baseUrl, "refund");
|
|
@@ -752,9 +769,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
752
769
|
if (apiKeyEntry?.lastUsed) {
|
|
753
770
|
const fiveMinutesAgo = Date.now() - 5 * 60 * 1e3;
|
|
754
771
|
if (apiKeyEntry.lastUsed > fiveMinutesAgo) {
|
|
755
|
-
|
|
756
|
-
`[BalanceManager] Skipping refund for ${baseUrl} - used ${Math.round((Date.now() - apiKeyEntry.lastUsed) / 1e3)}s ago`
|
|
757
|
-
);
|
|
772
|
+
this.logger.log(`Skipping refund for ${baseUrl} - used ${Math.round((Date.now() - apiKeyEntry.lastUsed) / 1e3)}s ago`);
|
|
758
773
|
return {
|
|
759
774
|
success: false,
|
|
760
775
|
message: "API key was used recently, skipping refund"
|
|
@@ -796,7 +811,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
796
811
|
requestId: fetchResult.requestId
|
|
797
812
|
};
|
|
798
813
|
} catch (error) {
|
|
799
|
-
|
|
814
|
+
this.logger.error("API key refund error", error);
|
|
800
815
|
return this._handleRefundError(error, mintUrl, fetchResult?.requestId);
|
|
801
816
|
}
|
|
802
817
|
}
|
|
@@ -848,7 +863,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
848
863
|
};
|
|
849
864
|
} catch (error) {
|
|
850
865
|
clearTimeout(timeoutId);
|
|
851
|
-
|
|
866
|
+
this.logger.error("fetchRefundToken fetch error", error);
|
|
852
867
|
if (error instanceof Error) {
|
|
853
868
|
if (error.name === "AbortError") {
|
|
854
869
|
return {
|
|
@@ -874,7 +889,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
874
889
|
const { mintUrl, baseUrl, amount, token: providedToken } = options;
|
|
875
890
|
const guard = this._canRunProviderWalletOperation(baseUrl, "topup");
|
|
876
891
|
if (!guard.allowed) {
|
|
877
|
-
|
|
892
|
+
this.logger.log(`Skipping topup for ${baseUrl} - ${guard.reason}`);
|
|
878
893
|
return { success: false, message: guard.reason };
|
|
879
894
|
}
|
|
880
895
|
this._beginProviderWalletOperation(baseUrl, "topup");
|
|
@@ -911,7 +926,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
911
926
|
cashuToken = tokenResult.token;
|
|
912
927
|
const topUpResult = await this._postTopUp(baseUrl, apiKey, cashuToken);
|
|
913
928
|
requestId = topUpResult.requestId;
|
|
914
|
-
|
|
929
|
+
this.logger.log("topUpResult:", topUpResult);
|
|
915
930
|
if (!topUpResult.success) {
|
|
916
931
|
await this._recoverFailedTopUp(cashuToken);
|
|
917
932
|
return {
|
|
@@ -927,10 +942,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
927
942
|
requestId
|
|
928
943
|
};
|
|
929
944
|
} catch (error) {
|
|
930
|
-
|
|
931
|
-
"DEBUG",
|
|
932
|
-
`[TopuPU] topup: Topup result for ${baseUrl}: error=${error}`
|
|
933
|
-
);
|
|
945
|
+
this.logger.log(`topup error for ${baseUrl}: ${error}`);
|
|
934
946
|
if (cashuToken) {
|
|
935
947
|
await this._recoverFailedTopUp(cashuToken);
|
|
936
948
|
}
|
|
@@ -947,13 +959,9 @@ var BalanceManager = class _BalanceManager {
|
|
|
947
959
|
p2pkPubkey
|
|
948
960
|
} = options;
|
|
949
961
|
const adjustedAmount = Math.ceil(amount);
|
|
950
|
-
|
|
951
|
-
`[BalanceManager.createProviderToken] Starting: baseUrl=${baseUrl}, mintUrl=${mintUrl}, amount=${amount}, adjustedAmount=${adjustedAmount}, retryCount=${retryCount}`
|
|
952
|
-
);
|
|
962
|
+
this.logger.log(`createProviderToken: baseUrl=${baseUrl} mintUrl=${mintUrl} amount=${amount} adjustedAmount=${adjustedAmount} retryCount=${retryCount}`);
|
|
953
963
|
if (!adjustedAmount || isNaN(adjustedAmount)) {
|
|
954
|
-
|
|
955
|
-
`[BalanceManager.createProviderToken] FAILURE: Invalid amount - amount=${amount}, adjustedAmount=${adjustedAmount}`
|
|
956
|
-
);
|
|
964
|
+
this.logger.error(`createProviderToken: invalid amount=${amount}`);
|
|
957
965
|
return { success: false, error: "Invalid top up amount" };
|
|
958
966
|
}
|
|
959
967
|
const balanceState = await this.getBalanceState();
|
|
@@ -984,9 +992,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
984
992
|
{ url: "", balance: 0 }
|
|
985
993
|
).url
|
|
986
994
|
);
|
|
987
|
-
|
|
988
|
-
`[BalanceManager.createProviderToken] FAILURE: Insufficient balance - required=${adjustedAmount}, available=${totalMintBalance + targetProviderBalance}, totalMintBalance=${totalMintBalance}, targetProviderBalance=${targetProviderBalance}, refundableProviderBalance=${refundableProviderBalance}`
|
|
989
|
-
);
|
|
995
|
+
this.logger.error(`createProviderToken: insufficient balance required=${adjustedAmount} available=${totalMintBalance + targetProviderBalance} totalMint=${totalMintBalance} targetProvider=${targetProviderBalance}`);
|
|
990
996
|
return { success: false, error: error.message };
|
|
991
997
|
}
|
|
992
998
|
const providerMints = baseUrl && this.providerRegistry ? this.providerRegistry.getProviderMints(baseUrl) : [];
|
|
@@ -1022,9 +1028,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
1022
1028
|
maxMintUrl = mintUrl2;
|
|
1023
1029
|
}
|
|
1024
1030
|
}
|
|
1025
|
-
|
|
1026
|
-
`[BalanceManager.createProviderToken] FAILURE: No candidate mints found - requiredAmount=${requiredAmount}, totalMintBalance=${totalMintBalance}, maxBalance=${maxBalance}, maxMintUrl=${maxMintUrl}, providerMints=${JSON.stringify(providerMints)}`
|
|
1027
|
-
);
|
|
1031
|
+
this.logger.error(`createProviderToken: no candidate mints required=${requiredAmount} totalMint=${totalMintBalance} maxBalance=${maxBalance} maxMint=${maxMintUrl}`);
|
|
1028
1032
|
const error = new InsufficientBalanceError(
|
|
1029
1033
|
adjustedAmount,
|
|
1030
1034
|
totalMintBalance,
|
|
@@ -1036,17 +1040,13 @@ var BalanceManager = class _BalanceManager {
|
|
|
1036
1040
|
let lastError;
|
|
1037
1041
|
for (const candidateMint of candidates) {
|
|
1038
1042
|
try {
|
|
1039
|
-
|
|
1040
|
-
`[BalanceManager.createProviderToken] Attempting mint: ${candidateMint}, amount: ${requiredAmount}`
|
|
1041
|
-
);
|
|
1043
|
+
this.logger.log(`createProviderToken: attempting mint=${candidateMint} amount=${requiredAmount}`);
|
|
1042
1044
|
const token = await this.walletAdapter.sendToken(
|
|
1043
1045
|
candidateMint,
|
|
1044
1046
|
requiredAmount,
|
|
1045
1047
|
p2pkPubkey
|
|
1046
1048
|
);
|
|
1047
|
-
|
|
1048
|
-
`[BalanceManager.createProviderToken] SUCCESS: Token created from mint ${candidateMint}, all mint balances: ${JSON.stringify(Object.fromEntries(Object.entries(balances).map(([mint, balance]) => [mint, getBalanceInSats(balance, units[mint])])))}`
|
|
1049
|
-
);
|
|
1049
|
+
this.logger.log(`createProviderToken: success from mint=${candidateMint}`);
|
|
1050
1050
|
return {
|
|
1051
1051
|
success: true,
|
|
1052
1052
|
token,
|
|
@@ -1055,15 +1055,11 @@ var BalanceManager = class _BalanceManager {
|
|
|
1055
1055
|
};
|
|
1056
1056
|
} catch (error) {
|
|
1057
1057
|
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
1058
|
-
|
|
1059
|
-
`[BalanceManager.createProviderToken] FAILURE: Mint ${candidateMint} failed with error: ${errorMsg}`
|
|
1060
|
-
);
|
|
1058
|
+
this.logger.error(`createProviderToken: mint=${candidateMint} failed: ${errorMsg}`);
|
|
1061
1059
|
if (error instanceof Error) {
|
|
1062
1060
|
lastError = errorMsg;
|
|
1063
1061
|
if (isNetworkErrorMessage(error.message)) {
|
|
1064
|
-
|
|
1065
|
-
`[BalanceManager.createProviderToken] Network error from ${candidateMint}, trying next mint...`
|
|
1066
|
-
);
|
|
1062
|
+
this.logger.warn(`createProviderToken: network error from ${candidateMint}, trying next mint...`);
|
|
1067
1063
|
continue;
|
|
1068
1064
|
}
|
|
1069
1065
|
}
|
|
@@ -1073,9 +1069,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
1073
1069
|
};
|
|
1074
1070
|
}
|
|
1075
1071
|
}
|
|
1076
|
-
|
|
1077
|
-
`[BalanceManager.createProviderToken] FAILURE: All candidate mints exhausted - lastError=${lastError}, candidates=${JSON.stringify(candidates)}`
|
|
1078
|
-
);
|
|
1072
|
+
this.logger.error(`createProviderToken: all candidate mints exhausted lastError=${lastError}`);
|
|
1079
1073
|
return {
|
|
1080
1074
|
success: false,
|
|
1081
1075
|
error: lastError || "All candidate mints failed while creating top up token"
|
|
@@ -1190,7 +1184,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
1190
1184
|
return { success: true, requestId };
|
|
1191
1185
|
} catch (error) {
|
|
1192
1186
|
clearTimeout(timeoutId);
|
|
1193
|
-
|
|
1187
|
+
this.logger.error("_postTopUp fetch error", error);
|
|
1194
1188
|
if (error instanceof Error) {
|
|
1195
1189
|
if (error.name === "AbortError") {
|
|
1196
1190
|
return {
|
|
@@ -1216,10 +1210,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
1216
1210
|
try {
|
|
1217
1211
|
await this.cashuSpender.receiveToken(cashuToken);
|
|
1218
1212
|
} catch (error) {
|
|
1219
|
-
|
|
1220
|
-
"[BalanceManager._recoverFailedTopUp] Failed to recover token",
|
|
1221
|
-
error
|
|
1222
|
-
);
|
|
1213
|
+
this.logger.error("_recoverFailedTopUp: failed to recover token", error);
|
|
1223
1214
|
}
|
|
1224
1215
|
}
|
|
1225
1216
|
/**
|
|
@@ -1272,9 +1263,9 @@ var BalanceManager = class _BalanceManager {
|
|
|
1272
1263
|
apiKey: data.api_key
|
|
1273
1264
|
};
|
|
1274
1265
|
} else {
|
|
1275
|
-
|
|
1266
|
+
this.logger.warn(`getTokenBalance: status=${response.status}`);
|
|
1276
1267
|
const data = await response.json();
|
|
1277
|
-
|
|
1268
|
+
this.logger.warn("getTokenBalance: FAILED", data);
|
|
1278
1269
|
const isInvalidApiKey = response.status === 401 && data?.detail?.error?.code === "invalid_api_key" && data?.detail?.error?.message?.includes("proofs already spent");
|
|
1279
1270
|
return {
|
|
1280
1271
|
amount: -1,
|
|
@@ -1285,7 +1276,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
1285
1276
|
};
|
|
1286
1277
|
}
|
|
1287
1278
|
} catch (error) {
|
|
1288
|
-
|
|
1279
|
+
this.logger.error("getTokenBalance error", error);
|
|
1289
1280
|
}
|
|
1290
1281
|
return { amount: -1, reserved: 0, unit: "sat", apiKey: "" };
|
|
1291
1282
|
}
|
|
@@ -1751,9 +1742,10 @@ function isInsecureHttpUrl(url) {
|
|
|
1751
1742
|
return url.startsWith("http://");
|
|
1752
1743
|
}
|
|
1753
1744
|
var ProviderManager = class _ProviderManager {
|
|
1754
|
-
constructor(providerRegistry, store) {
|
|
1745
|
+
constructor(providerRegistry, store, logger) {
|
|
1755
1746
|
this.providerRegistry = providerRegistry;
|
|
1756
1747
|
this.instanceId = `pm_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
|
|
1748
|
+
this.logger = (logger ?? consoleLogger).child(`ProviderManager:${this.instanceId}`);
|
|
1757
1749
|
if (store) {
|
|
1758
1750
|
this.store = store;
|
|
1759
1751
|
this.hydrateFromStore();
|
|
@@ -1770,6 +1762,7 @@ var ProviderManager = class _ProviderManager {
|
|
|
1770
1762
|
store = null;
|
|
1771
1763
|
/** Instance ID for debugging */
|
|
1772
1764
|
instanceId;
|
|
1765
|
+
logger;
|
|
1773
1766
|
/**
|
|
1774
1767
|
* Hydrate in-memory state from persistent store
|
|
1775
1768
|
*/
|
|
@@ -1782,10 +1775,7 @@ var ProviderManager = class _ProviderManager {
|
|
|
1782
1775
|
this.providersOnCoolDown = state.providersOnCooldown.filter(
|
|
1783
1776
|
(entry) => now - entry.timestamp < _ProviderManager.COOLDOWN_DURATION_MS
|
|
1784
1777
|
).map((entry) => [entry.baseUrl, entry.timestamp]);
|
|
1785
|
-
|
|
1786
|
-
console.log(` failedProviders: ${this.failedProviders.size}`);
|
|
1787
|
-
console.log(` lastFailed: ${this.lastFailed.size}`);
|
|
1788
|
-
console.log(` providersOnCooldown: ${this.providersOnCoolDown.length}`);
|
|
1778
|
+
this.logger.log(`Hydrated from store: failedProviders=${this.failedProviders.size} lastFailed=${this.lastFailed.size} providersOnCooldown=${this.providersOnCoolDown.length}`);
|
|
1789
1779
|
}
|
|
1790
1780
|
/**
|
|
1791
1781
|
* Get instance ID for debugging
|
|
@@ -1805,9 +1795,7 @@ var ProviderManager = class _ProviderManager {
|
|
|
1805
1795
|
const age = now - timestamp;
|
|
1806
1796
|
const isExpired = age >= _ProviderManager.COOLDOWN_DURATION_MS;
|
|
1807
1797
|
if (isExpired) {
|
|
1808
|
-
|
|
1809
|
-
`[cleanupExpiredCooldowns:${this.instanceId}] Removing expired cooldown for ${url} (age: ${age}ms, cooldown: ${_ProviderManager.COOLDOWN_DURATION_MS}ms)`
|
|
1810
|
-
);
|
|
1798
|
+
this.logger.log(`Removing expired cooldown for ${url} (age: ${age}ms)`);
|
|
1811
1799
|
this.failedProviders.delete(url);
|
|
1812
1800
|
if (this.store) {
|
|
1813
1801
|
this.store.getState().removeFailedProvider(url);
|
|
@@ -1818,9 +1806,7 @@ var ProviderManager = class _ProviderManager {
|
|
|
1818
1806
|
);
|
|
1819
1807
|
const after = this.providersOnCoolDown.length;
|
|
1820
1808
|
if (before !== after) {
|
|
1821
|
-
|
|
1822
|
-
`[cleanupExpiredCooldowns:${this.instanceId}] Cleaned up ${before - after} expired cooldown(s), ${after} remaining`
|
|
1823
|
-
);
|
|
1809
|
+
this.logger.log(`Cleaned up ${before - after} expired cooldown(s), ${after} remaining`);
|
|
1824
1810
|
}
|
|
1825
1811
|
}
|
|
1826
1812
|
/**
|
|
@@ -1872,24 +1858,10 @@ var ProviderManager = class _ProviderManager {
|
|
|
1872
1858
|
markFailed(baseUrl) {
|
|
1873
1859
|
const now = Date.now();
|
|
1874
1860
|
const lastFailure = this.lastFailed.get(baseUrl);
|
|
1875
|
-
|
|
1876
|
-
console.log(
|
|
1877
|
-
`[markFailed:${this.instanceId}] lastFailure from map: ${lastFailure}`
|
|
1878
|
-
);
|
|
1879
|
-
console.log(
|
|
1880
|
-
`[markFailed:${this.instanceId}] current timestamp (now): ${now}`
|
|
1881
|
-
);
|
|
1882
|
-
console.log(
|
|
1883
|
-
`[markFailed:${this.instanceId}] COOLDOWN_DURATION_MS: ${_ProviderManager.COOLDOWN_DURATION_MS}`
|
|
1884
|
-
);
|
|
1861
|
+
this.logger.log(`markFailed: ${baseUrl} lastFailure=${lastFailure} now=${now}`);
|
|
1885
1862
|
if (lastFailure !== void 0) {
|
|
1886
1863
|
const timeSinceLastFailure = now - lastFailure;
|
|
1887
|
-
|
|
1888
|
-
`[markFailed:${this.instanceId}] timeSinceLastFailure: ${timeSinceLastFailure}ms`
|
|
1889
|
-
);
|
|
1890
|
-
console.log(
|
|
1891
|
-
`[markFailed:${this.instanceId}] isWithinCooldownWindow: ${timeSinceLastFailure < _ProviderManager.COOLDOWN_DURATION_MS}`
|
|
1892
|
-
);
|
|
1864
|
+
this.logger.log(`markFailed: timeSinceLastFailure=${timeSinceLastFailure}ms withinCooldown=${timeSinceLastFailure < _ProviderManager.COOLDOWN_DURATION_MS}`);
|
|
1893
1865
|
}
|
|
1894
1866
|
this.lastFailed.set(baseUrl, now);
|
|
1895
1867
|
this.failedProviders.add(baseUrl);
|
|
@@ -1897,38 +1869,23 @@ var ProviderManager = class _ProviderManager {
|
|
|
1897
1869
|
this.store.getState().setLastFailedTimestamp(baseUrl, now);
|
|
1898
1870
|
this.store.getState().addFailedProvider(baseUrl);
|
|
1899
1871
|
}
|
|
1900
|
-
|
|
1901
|
-
`[markFailed:${this.instanceId}] Updated lastFailed map for ${baseUrl} to ${now}`
|
|
1902
|
-
);
|
|
1903
|
-
console.log(
|
|
1904
|
-
`[markFailed:${this.instanceId}] failedProviders set size: ${this.failedProviders.size}`
|
|
1905
|
-
);
|
|
1872
|
+
this.logger.log(`markFailed: updated ${baseUrl} to ${now}, failedProviders=${this.failedProviders.size}`);
|
|
1906
1873
|
if (lastFailure !== void 0 && now - lastFailure < _ProviderManager.COOLDOWN_DURATION_MS) {
|
|
1907
|
-
|
|
1908
|
-
`[markFailed:${this.instanceId}] Second failure detected within cooldown window for ${baseUrl}`
|
|
1909
|
-
);
|
|
1874
|
+
this.logger.log(`markFailed: second failure within cooldown window for ${baseUrl}`);
|
|
1910
1875
|
if (!this.isOnCooldown(baseUrl)) {
|
|
1911
1876
|
this.providersOnCoolDown.push([baseUrl, now]);
|
|
1912
1877
|
if (this.store) {
|
|
1913
1878
|
this.store.getState().addProviderOnCooldown(baseUrl, now);
|
|
1914
1879
|
}
|
|
1915
|
-
|
|
1916
|
-
`[markFailed:${this.instanceId}] Provider ${baseUrl} added to cooldown after second failure within 5 minutes`
|
|
1917
|
-
);
|
|
1880
|
+
this.logger.log(`markFailed: ${baseUrl} added to cooldown`);
|
|
1918
1881
|
} else {
|
|
1919
|
-
|
|
1920
|
-
`[markFailed:${this.instanceId}] Provider ${baseUrl} is already on cooldown`
|
|
1921
|
-
);
|
|
1882
|
+
this.logger.log(`markFailed: ${baseUrl} already on cooldown`);
|
|
1922
1883
|
}
|
|
1923
1884
|
} else {
|
|
1924
1885
|
if (lastFailure === void 0) {
|
|
1925
|
-
|
|
1926
|
-
`[markFailed:${this.instanceId}] First failure for ${baseUrl} - not adding to cooldown yet`
|
|
1927
|
-
);
|
|
1886
|
+
this.logger.log(`markFailed: first failure for ${baseUrl}`);
|
|
1928
1887
|
} else {
|
|
1929
|
-
|
|
1930
|
-
`[markFailed:${this.instanceId}] Failure outside cooldown window for ${baseUrl} (timeSinceLastFailure: ${now - lastFailure}ms)`
|
|
1931
|
-
);
|
|
1888
|
+
this.logger.log(`markFailed: failure outside cooldown window for ${baseUrl} (${now - lastFailure}ms ago)`);
|
|
1932
1889
|
}
|
|
1933
1890
|
}
|
|
1934
1891
|
}
|
|
@@ -1985,25 +1942,12 @@ var ProviderManager = class _ProviderManager {
|
|
|
1985
1942
|
const disabledProviders = new Set(
|
|
1986
1943
|
this.providerRegistry.getDisabledProviders()
|
|
1987
1944
|
);
|
|
1988
|
-
|
|
1989
|
-
`[findNextBestProvider:${this.instanceId}] Starting search for model: ${modelId}`
|
|
1990
|
-
);
|
|
1991
|
-
console.log(
|
|
1992
|
-
`[findNextBestProvider:${this.instanceId}] disabledProviders: ${[...disabledProviders]}`
|
|
1993
|
-
);
|
|
1994
|
-
console.log(
|
|
1995
|
-
`[findNextBestProvider:${this.instanceId}] providersOnCooldown: ${this.providersOnCoolDown.map(([url]) => url)}`
|
|
1996
|
-
);
|
|
1945
|
+
this.logger.log(`findNextBestProvider: model=${modelId} disabled=${[...disabledProviders].length} onCooldown=${this.providersOnCoolDown.length}`);
|
|
1997
1946
|
const allProviders = this.providerRegistry.getAllProvidersModels();
|
|
1998
|
-
|
|
1999
|
-
`[findNextBestProvider:${this.instanceId}] Total providers in registry: ${Object.keys(allProviders).length}`
|
|
2000
|
-
);
|
|
1947
|
+
this.logger.log(`findNextBestProvider: total providers=${Object.keys(allProviders).length}`);
|
|
2001
1948
|
const candidates = [];
|
|
2002
1949
|
for (const [baseUrl, models] of Object.entries(allProviders)) {
|
|
2003
1950
|
if (baseUrl === currentBaseUrl) {
|
|
2004
|
-
console.log(
|
|
2005
|
-
`[findNextBestProvider:${this.instanceId}] SKIP (current): ${baseUrl}`
|
|
2006
|
-
);
|
|
2007
1951
|
continue;
|
|
2008
1952
|
}
|
|
2009
1953
|
if (disabledProviders.has(baseUrl)) {
|
|
@@ -2029,7 +1973,7 @@ var ProviderManager = class _ProviderManager {
|
|
|
2029
1973
|
return null;
|
|
2030
1974
|
}
|
|
2031
1975
|
} catch (error) {
|
|
2032
|
-
|
|
1976
|
+
this.logger.error("findNextBestProvider error:", error);
|
|
2033
1977
|
return null;
|
|
2034
1978
|
}
|
|
2035
1979
|
}
|
|
@@ -2156,16 +2100,9 @@ var ProviderManager = class _ProviderManager {
|
|
|
2156
2100
|
res.height
|
|
2157
2101
|
);
|
|
2158
2102
|
imageTokens += tokensFromImage;
|
|
2159
|
-
|
|
2160
|
-
width: res.width,
|
|
2161
|
-
height: res.height,
|
|
2162
|
-
tokensFromImage
|
|
2163
|
-
});
|
|
2103
|
+
this.logger.log(`IMAGE INPUT RESOLUTION width=${res.width} height=${res.height} tokens=${tokensFromImage}`);
|
|
2164
2104
|
} else {
|
|
2165
|
-
|
|
2166
|
-
"IMAGE INPUT RESOLUTION",
|
|
2167
|
-
"unknown (unsupported format or parse failure)"
|
|
2168
|
-
);
|
|
2105
|
+
this.logger.log("IMAGE INPUT RESOLUTION: unknown format");
|
|
2169
2106
|
}
|
|
2170
2107
|
}
|
|
2171
2108
|
}
|
|
@@ -2198,7 +2135,7 @@ var ProviderManager = class _ProviderManager {
|
|
|
2198
2135
|
const totalEstimatedCosts = (promptCosts + completionCost) * 1.05;
|
|
2199
2136
|
return totalEstimatedCosts;
|
|
2200
2137
|
} catch (e) {
|
|
2201
|
-
|
|
2138
|
+
this.logger.error("getRequiredSatsForModel error:", e);
|
|
2202
2139
|
return 0;
|
|
2203
2140
|
}
|
|
2204
2141
|
}
|
|
@@ -3695,6 +3632,7 @@ var RoutstrClient = class {
|
|
|
3695
3632
|
this.walletAdapter = walletAdapter;
|
|
3696
3633
|
this.storageAdapter = storageAdapter;
|
|
3697
3634
|
this.providerRegistry = providerRegistry;
|
|
3635
|
+
this.logger = (options.logger ?? consoleLogger).child("RoutstrClient");
|
|
3698
3636
|
this.balanceManager = new BalanceManager(
|
|
3699
3637
|
walletAdapter,
|
|
3700
3638
|
storageAdapter,
|
|
@@ -3711,7 +3649,7 @@ var RoutstrClient = class {
|
|
|
3711
3649
|
this.mode = mode;
|
|
3712
3650
|
this.usageTrackingDriver = options.usageTrackingDriver;
|
|
3713
3651
|
this.sdkStore = options.sdkStore;
|
|
3714
|
-
this.providerManager = options.providerManager ?? new ProviderManager(providerRegistry, this.sdkStore);
|
|
3652
|
+
this.providerManager = options.providerManager ?? new ProviderManager(providerRegistry, this.sdkStore, this.logger);
|
|
3715
3653
|
}
|
|
3716
3654
|
cashuSpender;
|
|
3717
3655
|
balanceManager;
|
|
@@ -3722,6 +3660,7 @@ var RoutstrClient = class {
|
|
|
3722
3660
|
debugLevel = "WARN";
|
|
3723
3661
|
usageTrackingDriver;
|
|
3724
3662
|
sdkStore;
|
|
3663
|
+
logger;
|
|
3725
3664
|
/**
|
|
3726
3665
|
* Get the current client mode
|
|
3727
3666
|
*/
|
|
@@ -3743,13 +3682,13 @@ var RoutstrClient = class {
|
|
|
3743
3682
|
if (levelPriority[level] >= levelPriority[this.debugLevel]) {
|
|
3744
3683
|
switch (level) {
|
|
3745
3684
|
case "DEBUG":
|
|
3746
|
-
|
|
3685
|
+
this.logger.log(...args);
|
|
3747
3686
|
break;
|
|
3748
3687
|
case "WARN":
|
|
3749
|
-
|
|
3688
|
+
this.logger.warn(...args);
|
|
3750
3689
|
break;
|
|
3751
3690
|
case "ERROR":
|
|
3752
|
-
|
|
3691
|
+
this.logger.error(...args);
|
|
3753
3692
|
break;
|
|
3754
3693
|
}
|
|
3755
3694
|
}
|
|
@@ -4353,7 +4292,7 @@ var RoutstrClient = class {
|
|
|
4353
4292
|
tryNextProvider = true;
|
|
4354
4293
|
}
|
|
4355
4294
|
}
|
|
4356
|
-
if ((status === 401 || status === 403 || status === 413 || status === 400 || status === 429 || status === 500 || status === 502 || status === 503 || status === 504 || status === 521) && !tryNextProvider) {
|
|
4295
|
+
if ((status === 401 || status === 403 || status === 404 || status === 413 || status === 400 || status === 429 || status === 500 || status === 502 || status === 503 || status === 504 || status === 521) && !tryNextProvider) {
|
|
4357
4296
|
this._log(
|
|
4358
4297
|
"DEBUG",
|
|
4359
4298
|
`[RoutstrClient] _handleErrorResponse: Status ${status} (${status === 429 ? "rate limited" : "auth/server error"}), attempting refund for ${baseUrl}, mode=${this.mode}`
|