@routstr/sdk 0.3.6 → 0.3.7

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 (39) hide show
  1. package/package.json +2 -2
  2. package/dist/client/index.d.mts +0 -411
  3. package/dist/client/index.d.ts +0 -411
  4. package/dist/client/index.js +0 -4824
  5. package/dist/client/index.js.map +0 -1
  6. package/dist/client/index.mjs +0 -4818
  7. package/dist/client/index.mjs.map +0 -1
  8. package/dist/discovery/index.d.mts +0 -213
  9. package/dist/discovery/index.d.ts +0 -213
  10. package/dist/discovery/index.js +0 -735
  11. package/dist/discovery/index.js.map +0 -1
  12. package/dist/discovery/index.mjs +0 -732
  13. package/dist/discovery/index.mjs.map +0 -1
  14. package/dist/index.d.mts +0 -192
  15. package/dist/index.d.ts +0 -192
  16. package/dist/index.js +0 -6240
  17. package/dist/index.js.map +0 -1
  18. package/dist/index.mjs +0 -6191
  19. package/dist/index.mjs.map +0 -1
  20. package/dist/interfaces-Bp0Ngmqv.d.mts +0 -176
  21. package/dist/interfaces-Cqkt41QR.d.mts +0 -118
  22. package/dist/interfaces-D2FDCLyP.d.ts +0 -176
  23. package/dist/interfaces-D9qI1ym6.d.ts +0 -118
  24. package/dist/storage/index.d.mts +0 -87
  25. package/dist/storage/index.d.ts +0 -87
  26. package/dist/storage/index.js +0 -1740
  27. package/dist/storage/index.js.map +0 -1
  28. package/dist/storage/index.mjs +0 -1718
  29. package/dist/storage/index.mjs.map +0 -1
  30. package/dist/store-BFUGGr_v.d.ts +0 -172
  31. package/dist/store-C4FyyOnO.d.mts +0 -172
  32. package/dist/types-DPQM6tIG.d.mts +0 -234
  33. package/dist/types-DPQM6tIG.d.ts +0 -234
  34. package/dist/wallet/index.d.mts +0 -245
  35. package/dist/wallet/index.d.ts +0 -245
  36. package/dist/wallet/index.js +0 -1329
  37. package/dist/wallet/index.js.map +0 -1
  38. package/dist/wallet/index.mjs +0 -1326
  39. package/dist/wallet/index.mjs.map +0 -1
@@ -1,4824 +0,0 @@
1
- 'use strict';
2
-
3
- var cashuTs = require('@cashu/cashu-ts');
4
- var vanilla = require('zustand/vanilla');
5
- var stream = require('stream');
6
- var string_decoder = require('string_decoder');
7
-
8
- // core/types.ts
9
- function makeConsoleLogger(prefix) {
10
- const fmt = (args) => prefix ? [prefix, ...args] : args;
11
- return {
12
- log: (...args) => console.log(...fmt(args)),
13
- warn: (...args) => console.warn(...fmt(args)),
14
- error: (...args) => console.error(...fmt(args)),
15
- debug: (...args) => console.log(...fmt(args)),
16
- child: (p) => makeConsoleLogger(prefix ? `${prefix}:${p}` : p)
17
- };
18
- }
19
- var consoleLogger = makeConsoleLogger();
20
-
21
- // core/errors.ts
22
- var InsufficientBalanceError = class extends Error {
23
- constructor(required, available, maxMintBalance = 0, maxMintUrl = "", customMessage) {
24
- super(
25
- customMessage ?? `Insufficient balance: need ${required} sats, have ${available} sats available. ` + (maxMintBalance > 0 ? `Largest mint balance: ${maxMintBalance} sats from ${maxMintUrl}` : "")
26
- );
27
- this.required = required;
28
- this.available = available;
29
- this.maxMintBalance = maxMintBalance;
30
- this.maxMintUrl = maxMintUrl;
31
- this.name = "InsufficientBalanceError";
32
- }
33
- };
34
- var ProviderError = class extends Error {
35
- constructor(baseUrl, statusCode, message, requestId) {
36
- super(
37
- `Provider ${baseUrl} returned ${statusCode}: ${message}` + (requestId ? ` (Request ID: ${requestId})` : "")
38
- );
39
- this.baseUrl = baseUrl;
40
- this.statusCode = statusCode;
41
- this.requestId = requestId;
42
- this.name = "ProviderError";
43
- }
44
- };
45
- var FailoverError = class extends Error {
46
- constructor(originalProvider, failedProviders, message) {
47
- super(
48
- message || `All providers failed. Original: ${originalProvider}, Failed: ${failedProviders.join(", ")}`
49
- );
50
- this.originalProvider = originalProvider;
51
- this.failedProviders = failedProviders;
52
- this.name = "FailoverError";
53
- }
54
- };
55
-
56
- // wallet/AuditLogger.ts
57
- var AuditLogger = class _AuditLogger {
58
- static instance = null;
59
- static getInstance() {
60
- if (!_AuditLogger.instance) {
61
- _AuditLogger.instance = new _AuditLogger();
62
- }
63
- return _AuditLogger.instance;
64
- }
65
- async log(entry) {
66
- const fullEntry = {
67
- ...entry,
68
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
69
- };
70
- const logLine = JSON.stringify(fullEntry) + "\n";
71
- if (typeof window === "undefined") {
72
- try {
73
- const fs = await import('fs');
74
- const path = await import('path');
75
- const logPath = path.join(process.cwd(), "audit.log");
76
- fs.appendFileSync(logPath, logLine);
77
- } catch (error) {
78
- console.error("[AuditLogger] Failed to write to file:", error);
79
- }
80
- } else {
81
- console.log("[AUDIT]", logLine.trim());
82
- }
83
- }
84
- async logBalanceSnapshot(action, amounts, options) {
85
- await this.log({
86
- action,
87
- totalBalance: amounts.totalBalance,
88
- providerBalances: amounts.providerBalances,
89
- mintBalances: amounts.mintBalances,
90
- amount: options?.amount,
91
- mintUrl: options?.mintUrl,
92
- baseUrl: options?.baseUrl,
93
- status: options?.status ?? "success",
94
- details: options?.details
95
- });
96
- }
97
- };
98
- var auditLogger = AuditLogger.getInstance();
99
-
100
- // wallet/tokenUtils.ts
101
- function isNetworkErrorMessage(message) {
102
- return message.includes("NetworkError when attempting to fetch resource") || message.includes("Failed to fetch") || message.includes("Load failed") || message.includes("ERR_TLS_CERT_ALTNAME_INVALID") || message.includes("ERR_TLS_CERT_NOT_YET_VALID") || message.includes("ERR_TLS_CERT_EXPIRED") || message.includes("UNABLE_TO_VERIFY_LEAF_SIGNATURE") || message.includes("SELF_SIGNED_CERT_IN_CHAIN");
103
- }
104
- function getBalanceInSats(balance, unit) {
105
- return unit === "msat" ? balance / 1e3 : balance;
106
- }
107
- function selectMintWithBalance(balances, units, amount, excludeMints = []) {
108
- for (const mintUrl in balances) {
109
- if (excludeMints.includes(mintUrl)) {
110
- continue;
111
- }
112
- const balanceInSats = getBalanceInSats(balances[mintUrl], units[mintUrl]);
113
- if (balanceInSats >= amount) {
114
- return { selectedMintUrl: mintUrl, selectedMintBalance: balanceInSats };
115
- }
116
- }
117
- return { selectedMintUrl: null, selectedMintBalance: 0 };
118
- }
119
- var CashuSpender = class {
120
- constructor(walletAdapter, storageAdapter, _providerRegistry, balanceManager, logger) {
121
- this.walletAdapter = walletAdapter;
122
- this.storageAdapter = storageAdapter;
123
- this._providerRegistry = _providerRegistry;
124
- this.balanceManager = balanceManager;
125
- this.logger = (logger ?? consoleLogger).child("CashuSpender");
126
- }
127
- _isBusy = false;
128
- debugLevel = "WARN";
129
- logger;
130
- async receiveToken(token) {
131
- try {
132
- const result = await this.walletAdapter.receiveToken(token);
133
- return result;
134
- } catch (error) {
135
- const errorMessage = error instanceof Error ? error.message : String(error);
136
- if (errorMessage.includes("Failed to fetch mint")) {
137
- const cachedTokens = this.storageAdapter.getCachedReceiveTokens();
138
- const existingIndex = cachedTokens.findIndex((t) => t.token === token);
139
- if (existingIndex === -1) {
140
- const { amount: amount2, unit: unit2 } = this._decodeTokenAmount(token);
141
- this.storageAdapter.setCachedReceiveTokens([
142
- ...cachedTokens,
143
- {
144
- token,
145
- amount: amount2,
146
- unit: unit2,
147
- createdAt: Date.now()
148
- }
149
- ]);
150
- }
151
- }
152
- const { amount, unit } = this._decodeTokenAmount(token);
153
- return { success: false, amount, unit, message: errorMessage };
154
- }
155
- }
156
- _decodeTokenAmount(token) {
157
- try {
158
- const decoded = cashuTs.getDecodedToken(token);
159
- const amount = decoded.proofs.reduce(
160
- (acc, proof) => acc + proof.amount,
161
- 0
162
- );
163
- const unit = decoded.unit || "sat";
164
- return { amount, unit };
165
- } catch {
166
- return { amount: 0, unit: "sat" };
167
- }
168
- }
169
- async _getBalanceState() {
170
- if (this.balanceManager) {
171
- return this.balanceManager.getBalanceState();
172
- }
173
- const mintBalances = await this.walletAdapter.getBalances();
174
- const units = this.walletAdapter.getMintUnits();
175
- let totalMintBalance = 0;
176
- const normalizedMintBalances = {};
177
- for (const url in mintBalances) {
178
- const balance = mintBalances[url];
179
- const unit = units[url];
180
- const balanceInSats = getBalanceInSats(balance, unit);
181
- normalizedMintBalances[url] = balanceInSats;
182
- totalMintBalance += balanceInSats;
183
- }
184
- const providerBalances = {};
185
- let totalProviderBalance = 0;
186
- const apiKeys = this.storageAdapter.getAllApiKeys();
187
- for (const apiKey of apiKeys) {
188
- if (!providerBalances[apiKey.baseUrl]) {
189
- providerBalances[apiKey.baseUrl] = 0;
190
- }
191
- providerBalances[apiKey.baseUrl] += apiKey.balance;
192
- totalProviderBalance += apiKey.balance;
193
- }
194
- return {
195
- totalBalance: totalMintBalance + totalProviderBalance,
196
- providerBalances,
197
- mintBalances: normalizedMintBalances
198
- };
199
- }
200
- async _logTransaction(action, options) {
201
- const balanceState = await this._getBalanceState();
202
- await auditLogger.logBalanceSnapshot(action, balanceState, options);
203
- }
204
- /**
205
- * Check if the spender is currently in a critical operation
206
- */
207
- get isBusy() {
208
- return this._isBusy;
209
- }
210
- getDebugLevel() {
211
- return this.debugLevel;
212
- }
213
- setDebugLevel(level) {
214
- this.debugLevel = level;
215
- }
216
- _log(level, ...args) {
217
- const levelPriority = {
218
- DEBUG: 0,
219
- WARN: 1,
220
- ERROR: 2
221
- };
222
- if (levelPriority[level] >= levelPriority[this.debugLevel]) {
223
- switch (level) {
224
- case "DEBUG":
225
- this.logger.log(...args);
226
- break;
227
- case "WARN":
228
- this.logger.warn(...args);
229
- break;
230
- case "ERROR":
231
- this.logger.error(...args);
232
- break;
233
- }
234
- }
235
- }
236
- /**
237
- * Spend Cashu tokens with automatic mint selection and retry logic
238
- * Throws errors on failure instead of returning failed SpendResult
239
- */
240
- async spend(options) {
241
- const {
242
- mintUrl,
243
- amount,
244
- baseUrl,
245
- reuseToken = false,
246
- p2pkPubkey,
247
- excludeMints = [],
248
- retryCount = 0
249
- } = options;
250
- this._isBusy = true;
251
- try {
252
- const result = await this._spendInternal({
253
- mintUrl,
254
- amount,
255
- baseUrl,
256
- reuseToken,
257
- p2pkPubkey,
258
- excludeMints,
259
- retryCount
260
- });
261
- if (result.status === "failed" || !result.token) {
262
- const errorMsg = result.error || `Insufficient balance. Need ${amount} sats.`;
263
- if (this._isNetworkError(errorMsg)) {
264
- throw new Error(
265
- `Your mint ${mintUrl} is unreachable or is blocking your IP. Please try again later or switch mints.`
266
- );
267
- }
268
- if (result.errorDetails) {
269
- throw new InsufficientBalanceError(
270
- result.errorDetails.required,
271
- result.errorDetails.available,
272
- result.errorDetails.maxMintBalance,
273
- result.errorDetails.maxMintUrl
274
- );
275
- }
276
- throw new Error(errorMsg);
277
- }
278
- return result;
279
- } finally {
280
- this._isBusy = false;
281
- }
282
- }
283
- /**
284
- * Check if error message indicates a network error
285
- */
286
- _isNetworkError(message) {
287
- return isNetworkErrorMessage(message) || message.includes("Your mint") && message.includes("unreachable");
288
- }
289
- /**
290
- * Internal spending logic
291
- */
292
- async _spendInternal(options) {
293
- let {
294
- mintUrl,
295
- amount,
296
- baseUrl,
297
- reuseToken,
298
- p2pkPubkey,
299
- excludeMints,
300
- retryCount
301
- } = options;
302
- this._log(
303
- "DEBUG",
304
- `[CashuSpender] _spendInternal: amount=${amount}, mintUrl=${mintUrl}, baseUrl=${baseUrl}, reuseToken=${reuseToken}`
305
- );
306
- let adjustedAmount = Math.ceil(amount);
307
- if (!adjustedAmount || isNaN(adjustedAmount)) {
308
- this._log(
309
- "ERROR",
310
- `[CashuSpender] _spendInternal: Invalid amount: ${amount}`
311
- );
312
- return {
313
- token: null,
314
- status: "failed",
315
- balance: 0,
316
- error: "Please enter a valid amount"
317
- };
318
- }
319
- if (reuseToken && baseUrl) {
320
- this._log(
321
- "DEBUG",
322
- `[CashuSpender] _spendInternal: Attempting to reuse token for ${baseUrl}`
323
- );
324
- const existingResult = await this._tryReuseToken(
325
- baseUrl,
326
- adjustedAmount,
327
- mintUrl
328
- );
329
- if (existingResult) {
330
- this._log(
331
- "DEBUG",
332
- `[CashuSpender] _spendInternal: Successfully reused token, balance: ${existingResult.balance}`
333
- );
334
- return existingResult;
335
- }
336
- this._log(
337
- "DEBUG",
338
- `[CashuSpender] _spendInternal: Could not reuse token, will create new token`
339
- );
340
- }
341
- const balanceState = await this._getBalanceState();
342
- const totalAvailableBalance = balanceState.totalBalance;
343
- this._log(
344
- "DEBUG",
345
- `[CashuSpender] _spendInternal: totalAvailableBalance=${totalAvailableBalance}, adjustedAmount=${adjustedAmount}`
346
- );
347
- if (totalAvailableBalance < adjustedAmount) {
348
- this._log(
349
- "ERROR",
350
- `[CashuSpender] _spendInternal: Insufficient balance, have=${totalAvailableBalance}, need=${adjustedAmount}`
351
- );
352
- return this._createInsufficientBalanceError(
353
- adjustedAmount,
354
- balanceState.mintBalances,
355
- totalAvailableBalance
356
- );
357
- }
358
- let token = null;
359
- let selectedMintUrl;
360
- let spentAmount = adjustedAmount;
361
- if (this.balanceManager) {
362
- const tokenResult = await this.balanceManager.createProviderToken({
363
- mintUrl,
364
- baseUrl,
365
- amount: adjustedAmount,
366
- p2pkPubkey,
367
- excludeMints,
368
- retryCount
369
- });
370
- if (!tokenResult.success || !tokenResult.token) {
371
- if ((tokenResult.error || "").includes("Insufficient balance")) {
372
- return this._createInsufficientBalanceError(
373
- adjustedAmount,
374
- balanceState.mintBalances,
375
- totalAvailableBalance
376
- );
377
- }
378
- return {
379
- token: null,
380
- status: "failed",
381
- balance: 0,
382
- error: tokenResult.error || "Failed to create token"
383
- };
384
- }
385
- token = tokenResult.token;
386
- selectedMintUrl = tokenResult.selectedMintUrl;
387
- spentAmount = tokenResult.amountSpent || adjustedAmount;
388
- } else {
389
- try {
390
- token = await this.walletAdapter.sendToken(
391
- mintUrl,
392
- adjustedAmount,
393
- p2pkPubkey
394
- );
395
- selectedMintUrl = mintUrl;
396
- } catch (error) {
397
- const errorMsg = error instanceof Error ? error.message : String(error);
398
- return {
399
- token: null,
400
- status: "failed",
401
- balance: 0,
402
- error: `Error generating token: ${errorMsg}`
403
- };
404
- }
405
- }
406
- if (token) {
407
- this._log(
408
- "DEBUG",
409
- `[CashuSpender] _spendInternal: Successfully spent ${spentAmount}, returning token with balance=${spentAmount}`
410
- );
411
- }
412
- this._logTransaction("spend", {
413
- amount: spentAmount,
414
- mintUrl: selectedMintUrl || mintUrl,
415
- baseUrl,
416
- status: "success"
417
- });
418
- this._log(
419
- "DEBUG",
420
- `[CashuSpender] _spendInternal: Successfully spent ${spentAmount}, returning token with balance=${spentAmount}`
421
- );
422
- const units = this.walletAdapter.getMintUnits();
423
- return {
424
- token,
425
- status: "success",
426
- balance: spentAmount,
427
- unit: (selectedMintUrl ? units[selectedMintUrl] : units[mintUrl]) || "sat"
428
- };
429
- }
430
- /**
431
- * Try to reuse an existing API key
432
- */
433
- async _tryReuseToken(baseUrl, amount, mintUrl) {
434
- const apiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
435
- if (!apiKeyEntry) return null;
436
- const apiKeyDistribution = this.storageAdapter.getApiKeyDistribution();
437
- const balanceForBaseUrl = apiKeyDistribution.find((b) => b.baseUrl === baseUrl)?.amount || 0;
438
- this._log("DEBUG", "Reusing API key", balanceForBaseUrl, amount);
439
- if (balanceForBaseUrl > amount) {
440
- const units = this.walletAdapter.getMintUnits();
441
- const unit = units[mintUrl] || "sat";
442
- return {
443
- token: apiKeyEntry.key,
444
- status: "success",
445
- balance: balanceForBaseUrl,
446
- unit
447
- };
448
- }
449
- if (this.balanceManager) {
450
- const topUpAmount = Math.ceil(amount * 1.2 - balanceForBaseUrl);
451
- const topUpResult = await this.balanceManager.topUp({
452
- mintUrl,
453
- baseUrl,
454
- amount: topUpAmount,
455
- token: apiKeyEntry.key
456
- });
457
- this._log("DEBUG", "TOPUP ", topUpResult);
458
- if (topUpResult.success && topUpResult.toppedUpAmount) {
459
- const newBalance = balanceForBaseUrl + topUpResult.toppedUpAmount;
460
- const units = this.walletAdapter.getMintUnits();
461
- const unit = units[mintUrl] || "sat";
462
- this._logTransaction("topup", {
463
- amount: topUpResult.toppedUpAmount,
464
- mintUrl,
465
- baseUrl,
466
- status: "success"
467
- });
468
- return {
469
- token: apiKeyEntry.key,
470
- status: "success",
471
- balance: newBalance,
472
- unit
473
- };
474
- }
475
- const providerBalance = await this._getProviderTokenBalance(
476
- baseUrl,
477
- apiKeyEntry.key
478
- );
479
- this._log("DEBUG", providerBalance);
480
- if (providerBalance <= 0) {
481
- this.storageAdapter.removeApiKey(baseUrl);
482
- }
483
- }
484
- return null;
485
- }
486
- /**
487
- * Refund all xcashu tokens from storage by calling the provider's refund endpoint.
488
- * The xcashu token acts as an API key to claim the refund, and the response contains
489
- * the actual refunded Cashu token which is then received into the wallet.
490
- * @param mintUrl - The mint URL for receiving tokens
491
- * @param excludeBaseUrls - Base URLs to exclude from refund (optional)
492
- * @returns Results for each xcashu token refund attempt
493
- */
494
- async refundXcashuTokens(mintUrl, excludeBaseUrls) {
495
- const results = [];
496
- const xcashuTokens = this.storageAdapter.getXcashuTokens();
497
- const excludedUrls = new Set(excludeBaseUrls || []);
498
- for (const [baseUrl, tokens] of Object.entries(xcashuTokens)) {
499
- if (excludedUrls.has(baseUrl)) continue;
500
- for (const xcashuToken of tokens) {
501
- try {
502
- if (!this.balanceManager) {
503
- throw new Error("BalanceManager not available for xcashu refund");
504
- }
505
- const fetchResult = await this.balanceManager.fetchRefundToken(
506
- baseUrl,
507
- xcashuToken.token,
508
- true
509
- );
510
- if (!fetchResult.success || !fetchResult.token) {
511
- throw new Error(
512
- fetchResult.error || "Failed to fetch refund token from provider"
513
- );
514
- }
515
- const receiveResult = await this.receiveToken(fetchResult.token);
516
- if (receiveResult.success) {
517
- this.storageAdapter.removeXcashuToken(baseUrl, xcashuToken.token);
518
- results.push({
519
- baseUrl,
520
- token: xcashuToken.token,
521
- success: true
522
- });
523
- this._log(
524
- "DEBUG",
525
- `[CashuSpender] refundXcashuTokens: Successfully refunded xcashu token for ${baseUrl}, amount=${receiveResult.amount}`
526
- );
527
- } else {
528
- const currentTryCount = xcashuToken.tryCount ?? 0;
529
- const newTryCount = currentTryCount + 1;
530
- this.storageAdapter.updateXcashuTokenTryCount(
531
- xcashuToken.token,
532
- newTryCount
533
- );
534
- results.push({
535
- baseUrl,
536
- token: xcashuToken.token,
537
- success: false,
538
- error: receiveResult.message ?? "Refund failed"
539
- });
540
- this._log(
541
- "DEBUG",
542
- `[CashuSpender] refundXcashuTokens: Failed to receive refund token for ${baseUrl}, incremented tryCount to ${newTryCount}: ${receiveResult.message}`
543
- );
544
- }
545
- } catch (error) {
546
- const currentTryCount = xcashuToken.tryCount ?? 0;
547
- const newTryCount = currentTryCount + 1;
548
- this.storageAdapter.updateXcashuTokenTryCount(
549
- xcashuToken.token,
550
- newTryCount
551
- );
552
- const errorMessage = error instanceof Error ? error.message : String(error);
553
- results.push({
554
- baseUrl,
555
- token: xcashuToken.token,
556
- success: false,
557
- error: errorMessage
558
- });
559
- this._log(
560
- "ERROR",
561
- `[CashuSpender] refundXcashuTokens: Exception during refund for ${baseUrl}: ${errorMessage}, incremented tryCount to ${newTryCount}`
562
- );
563
- }
564
- }
565
- }
566
- return results;
567
- }
568
- /**
569
- * Refund specific providers without retrying spend
570
- */
571
- async refundProviders(mintUrl, forceRefund) {
572
- const results = [];
573
- const apiKeyDistribution = this.storageAdapter.getApiKeyDistribution();
574
- for (const apiKeyEntry of apiKeyDistribution) {
575
- const apiKeyEntryFull = this.storageAdapter.getApiKey(
576
- apiKeyEntry.baseUrl
577
- );
578
- if (apiKeyEntryFull && this.balanceManager) {
579
- try {
580
- const balanceResult = await this.balanceManager.getTokenBalance(
581
- apiKeyEntryFull.key,
582
- apiKeyEntry.baseUrl
583
- );
584
- if (balanceResult.isInvalidApiKey) {
585
- this.storageAdapter.removeApiKey(apiKeyEntry.baseUrl);
586
- results.push({
587
- baseUrl: apiKeyEntry.baseUrl,
588
- success: true
589
- });
590
- continue;
591
- }
592
- if (balanceResult.amount >= 0) {
593
- const balanceSat = balanceResult.unit === "msat" ? Math.floor(balanceResult.amount / 1e3) : balanceResult.amount;
594
- this.storageAdapter.updateApiKeyBalance(
595
- apiKeyEntry.baseUrl,
596
- balanceSat
597
- );
598
- }
599
- } catch {
600
- }
601
- const refreshedEntry = this.storageAdapter.getApiKey(
602
- apiKeyEntry.baseUrl
603
- );
604
- if (!refreshedEntry) continue;
605
- const refundResult = await this.balanceManager.refundApiKey({
606
- mintUrl,
607
- baseUrl: apiKeyEntry.baseUrl,
608
- apiKey: refreshedEntry.key,
609
- forceRefund
610
- });
611
- if (refundResult.success) {
612
- this.storageAdapter.removeApiKey(apiKeyEntry.baseUrl);
613
- } else {
614
- const currentEntry = this.storageAdapter.getApiKey(
615
- apiKeyEntry.baseUrl
616
- );
617
- if (currentEntry) {
618
- this.storageAdapter.updateApiKeyBalance(
619
- apiKeyEntry.baseUrl,
620
- currentEntry.balance
621
- );
622
- }
623
- }
624
- results.push({
625
- baseUrl: apiKeyEntry.baseUrl,
626
- success: refundResult.success
627
- });
628
- } else {
629
- results.push({
630
- baseUrl: apiKeyEntry.baseUrl,
631
- success: false
632
- });
633
- }
634
- }
635
- return results;
636
- }
637
- /**
638
- * Create an insufficient balance error result
639
- */
640
- _createInsufficientBalanceError(required, normalizedBalances, availableBalance) {
641
- let maxBalance = 0;
642
- let maxMintUrl = "";
643
- for (const mintUrl in normalizedBalances) {
644
- const balanceInSats = normalizedBalances[mintUrl];
645
- if (balanceInSats > maxBalance) {
646
- maxBalance = balanceInSats;
647
- maxMintUrl = mintUrl;
648
- }
649
- }
650
- const error = new InsufficientBalanceError(
651
- required,
652
- availableBalance ?? maxBalance,
653
- maxBalance,
654
- maxMintUrl
655
- );
656
- return {
657
- token: null,
658
- status: "failed",
659
- balance: 0,
660
- error: error.message,
661
- errorDetails: {
662
- required,
663
- available: availableBalance ?? maxBalance,
664
- maxMintBalance: maxBalance,
665
- maxMintUrl
666
- }
667
- };
668
- }
669
- async _getProviderTokenBalance(baseUrl, token) {
670
- try {
671
- const response = await fetch(`${baseUrl}v1/wallet/info`, {
672
- headers: {
673
- Authorization: `Bearer ${token}`
674
- }
675
- });
676
- if (response.ok) {
677
- const data = await response.json();
678
- return data.balance / 1e3;
679
- }
680
- } catch {
681
- return 0;
682
- }
683
- return 0;
684
- }
685
- };
686
-
687
- // wallet/BalanceManager.ts
688
- var BalanceManager = class _BalanceManager {
689
- constructor(walletAdapter, storageAdapter, providerRegistry, cashuSpender, logger) {
690
- this.walletAdapter = walletAdapter;
691
- this.storageAdapter = storageAdapter;
692
- this.providerRegistry = providerRegistry;
693
- this.logger = (logger ?? consoleLogger).child("BalanceManager");
694
- if (cashuSpender) {
695
- this.cashuSpender = cashuSpender;
696
- } else {
697
- this.cashuSpender = new CashuSpender(
698
- walletAdapter,
699
- storageAdapter,
700
- providerRegistry,
701
- this
702
- );
703
- }
704
- }
705
- cashuSpender;
706
- /** In-memory guard for per-provider wallet mutations (topup / refund) */
707
- providerWalletOps = /* @__PURE__ */ new Map();
708
- /** Cooldown (ms) between opposite operations on the same provider */
709
- static PROVIDER_WALLET_COOLDOWN_MS = 1e4;
710
- logger;
711
- /**
712
- * Check whether a wallet operation (topup/refund) may run for a provider.
713
- * Returns the reason when blocked.
714
- */
715
- _canRunProviderWalletOperation(baseUrl, type) {
716
- const existing = this.providerWalletOps.get(baseUrl);
717
- if (!existing) {
718
- return { allowed: true };
719
- }
720
- if (existing.type === type) {
721
- return { allowed: true };
722
- }
723
- if (!existing.endTime) {
724
- return {
725
- allowed: false,
726
- reason: `Provider wallet operation locked; ${existing.type} in progress`
727
- };
728
- }
729
- const elapsed = Date.now() - existing.endTime;
730
- if (elapsed < _BalanceManager.PROVIDER_WALLET_COOLDOWN_MS) {
731
- return {
732
- allowed: false,
733
- reason: `Provider wallet operation locked; recent ${existing.type} completed ${Math.round(elapsed / 1e3)}s ago`
734
- };
735
- }
736
- this.providerWalletOps.delete(baseUrl);
737
- return { allowed: true };
738
- }
739
- _beginProviderWalletOperation(baseUrl, type) {
740
- this.providerWalletOps.set(baseUrl, { type, startTime: Date.now() });
741
- }
742
- _endProviderWalletOperation(baseUrl, type) {
743
- const existing = this.providerWalletOps.get(baseUrl);
744
- if (existing && existing.type === type) {
745
- existing.endTime = Date.now();
746
- }
747
- }
748
- async getBalanceState() {
749
- const mintBalances = await this.walletAdapter.getBalances();
750
- const units = this.walletAdapter.getMintUnits();
751
- let totalMintBalance = 0;
752
- const normalizedMintBalances = {};
753
- for (const url in mintBalances) {
754
- const balance = mintBalances[url];
755
- const unit = units[url];
756
- const balanceInSats = getBalanceInSats(balance, unit);
757
- normalizedMintBalances[url] = balanceInSats;
758
- totalMintBalance += balanceInSats;
759
- }
760
- const providerBalances = {};
761
- let totalProviderBalance = 0;
762
- const apiKeys = this.storageAdapter.getAllApiKeys();
763
- for (const apiKey of apiKeys) {
764
- if (!providerBalances[apiKey.baseUrl]) {
765
- providerBalances[apiKey.baseUrl] = 0;
766
- }
767
- providerBalances[apiKey.baseUrl] += apiKey.balance;
768
- totalProviderBalance += apiKey.balance;
769
- }
770
- return {
771
- totalBalance: totalMintBalance + totalProviderBalance,
772
- providerBalances,
773
- mintBalances: normalizedMintBalances
774
- };
775
- }
776
- /**
777
- * Refund API key balance - convert remaining API key balance to cashu token
778
- * @param options - Refund options including forceRefund flag
779
- * @returns Refund result
780
- */
781
- async refundApiKey(options) {
782
- const { mintUrl, baseUrl, apiKey, forceRefund } = options;
783
- const guard = this._canRunProviderWalletOperation(baseUrl, "refund");
784
- if (!guard.allowed) {
785
- this.logger.log(`Skipping refund for ${baseUrl} - ${guard.reason}`);
786
- return { success: false, message: guard.reason };
787
- }
788
- this._beginProviderWalletOperation(baseUrl, "refund");
789
- try {
790
- return await this._refundApiKeyImpl({ mintUrl, baseUrl, apiKey, forceRefund });
791
- } finally {
792
- this._endProviderWalletOperation(baseUrl, "refund");
793
- }
794
- }
795
- async _refundApiKeyImpl(options) {
796
- const { mintUrl, baseUrl, apiKey, forceRefund } = options;
797
- if (!apiKey) {
798
- return { success: false, message: "No API key to refund" };
799
- }
800
- if (!forceRefund) {
801
- const apiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
802
- if (apiKeyEntry?.lastUsed) {
803
- const fiveMinutesAgo = Date.now() - 5 * 60 * 1e3;
804
- if (apiKeyEntry.lastUsed > fiveMinutesAgo) {
805
- this.logger.log(`Skipping refund for ${baseUrl} - used ${Math.round((Date.now() - apiKeyEntry.lastUsed) / 1e3)}s ago`);
806
- return {
807
- success: false,
808
- message: "API key was used recently, skipping refund"
809
- };
810
- }
811
- }
812
- }
813
- let fetchResult;
814
- try {
815
- fetchResult = await this.fetchRefundToken(baseUrl, apiKey);
816
- if (!fetchResult.success) {
817
- return {
818
- success: false,
819
- message: fetchResult.error || "API key refund failed",
820
- requestId: fetchResult.requestId
821
- };
822
- }
823
- if (!fetchResult.token) {
824
- return {
825
- success: false,
826
- message: "No token received from API key refund",
827
- requestId: fetchResult.requestId
828
- };
829
- }
830
- if (fetchResult.error === "No balance to refund") {
831
- this.storageAdapter.removeApiKey(baseUrl);
832
- return { success: true, message: "No balance to refund, key cleaned up" };
833
- }
834
- const receiveResult = await this.cashuSpender.receiveToken(
835
- fetchResult.token
836
- );
837
- const totalAmountMsat = receiveResult.unit === "msat" ? receiveResult.amount : receiveResult.amount * 1e3;
838
- if (receiveResult.success) {
839
- this.storageAdapter.removeApiKey(baseUrl);
840
- }
841
- return {
842
- success: receiveResult.success,
843
- refundedAmount: totalAmountMsat,
844
- message: receiveResult.message,
845
- requestId: fetchResult.requestId
846
- };
847
- } catch (error) {
848
- this.logger.error("API key refund error", error);
849
- return this._handleRefundError(error, mintUrl, fetchResult?.requestId);
850
- }
851
- }
852
- /**
853
- * Fetch refund token from provider API using API key (or xcashu token) authentication
854
- */
855
- async fetchRefundToken(baseUrl, apiKeyOrToken, xCashu = false) {
856
- if (!baseUrl) {
857
- return {
858
- success: false,
859
- error: "No base URL configured"
860
- };
861
- }
862
- const normalizedBaseUrl = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
863
- const url = `${normalizedBaseUrl}v1/wallet/refund`;
864
- const controller = new AbortController();
865
- const timeoutId = setTimeout(() => {
866
- controller.abort();
867
- }, 6e4);
868
- try {
869
- const headers = {
870
- "Content-Type": "application/json"
871
- };
872
- if (xCashu) {
873
- headers["X-Cashu"] = apiKeyOrToken;
874
- } else {
875
- headers["Authorization"] = `Bearer ${apiKeyOrToken}`;
876
- }
877
- const response = await fetch(url, {
878
- method: "POST",
879
- headers,
880
- signal: controller.signal
881
- });
882
- clearTimeout(timeoutId);
883
- const requestId = response.headers.get("x-routstr-request-id") || void 0;
884
- if (!response.ok) {
885
- const errorData = await response.json().catch(() => ({}));
886
- return {
887
- success: false,
888
- requestId,
889
- error: `API key refund failed: ${errorData?.detail || response.statusText}`
890
- };
891
- }
892
- const data = await response.json();
893
- return {
894
- success: true,
895
- token: data.token,
896
- requestId
897
- };
898
- } catch (error) {
899
- clearTimeout(timeoutId);
900
- this.logger.error("fetchRefundToken fetch error", error);
901
- if (error instanceof Error) {
902
- if (error.name === "AbortError") {
903
- return {
904
- success: false,
905
- error: "Request timed out after 1 minute"
906
- };
907
- }
908
- return {
909
- success: false,
910
- error: error.message
911
- };
912
- }
913
- return {
914
- success: false,
915
- error: "Unknown error occurred during API key refund request"
916
- };
917
- }
918
- }
919
- /**
920
- * Top up API key balance with a cashu token
921
- */
922
- async topUp(options) {
923
- const { mintUrl, baseUrl, amount, token: providedToken } = options;
924
- const guard = this._canRunProviderWalletOperation(baseUrl, "topup");
925
- if (!guard.allowed) {
926
- this.logger.log(`Skipping topup for ${baseUrl} - ${guard.reason}`);
927
- return { success: false, message: guard.reason };
928
- }
929
- this._beginProviderWalletOperation(baseUrl, "topup");
930
- try {
931
- return await this._topUpImpl({ mintUrl, baseUrl, amount, token: providedToken });
932
- } finally {
933
- this._endProviderWalletOperation(baseUrl, "topup");
934
- }
935
- }
936
- async _topUpImpl(options) {
937
- const { mintUrl, baseUrl, amount, token: providedToken } = options;
938
- if (!amount || amount <= 0) {
939
- return { success: false, message: "Invalid top up amount" };
940
- }
941
- const apiKeyEntry = providedToken ? null : this.storageAdapter.getApiKey(baseUrl);
942
- const apiKey = providedToken || apiKeyEntry?.key;
943
- if (!apiKey) {
944
- return { success: false, message: "No API key available for top up" };
945
- }
946
- let cashuToken = null;
947
- let requestId;
948
- try {
949
- const tokenResult = await this.createProviderToken({
950
- mintUrl,
951
- baseUrl,
952
- amount
953
- });
954
- if (!tokenResult.success || !tokenResult.token) {
955
- return {
956
- success: false,
957
- message: tokenResult.error || "Unable to create top up token"
958
- };
959
- }
960
- cashuToken = tokenResult.token;
961
- const topUpResult = await this._postTopUp(baseUrl, apiKey, cashuToken);
962
- requestId = topUpResult.requestId;
963
- this.logger.log("topUpResult:", topUpResult);
964
- if (!topUpResult.success) {
965
- await this._recoverFailedTopUp(cashuToken);
966
- return {
967
- success: false,
968
- message: topUpResult.error || "Top up failed",
969
- requestId,
970
- recoveredToken: true
971
- };
972
- }
973
- return {
974
- success: true,
975
- toppedUpAmount: amount,
976
- requestId
977
- };
978
- } catch (error) {
979
- this.logger.log(`topup error for ${baseUrl}: ${error}`);
980
- if (cashuToken) {
981
- await this._recoverFailedTopUp(cashuToken);
982
- }
983
- return this._handleTopUpError(error, mintUrl, requestId);
984
- }
985
- }
986
- async createProviderToken(options) {
987
- const {
988
- mintUrl,
989
- baseUrl,
990
- amount,
991
- retryCount = 0,
992
- excludeMints = [],
993
- p2pkPubkey
994
- } = options;
995
- const adjustedAmount = Math.ceil(amount);
996
- this.logger.log(`createProviderToken: baseUrl=${baseUrl} mintUrl=${mintUrl} amount=${amount} adjustedAmount=${adjustedAmount} retryCount=${retryCount}`);
997
- if (!adjustedAmount || isNaN(adjustedAmount)) {
998
- this.logger.error(`createProviderToken: invalid amount=${amount}`);
999
- return { success: false, error: "Invalid top up amount" };
1000
- }
1001
- const balanceState = await this.getBalanceState();
1002
- const balances = await this.walletAdapter.getBalances();
1003
- const units = this.walletAdapter.getMintUnits();
1004
- const totalMintBalance = Object.values(balanceState.mintBalances).reduce(
1005
- (sum, value) => sum + value,
1006
- 0
1007
- );
1008
- const targetProviderBalance = balanceState.providerBalances[baseUrl] || 0;
1009
- const refundableProviderBalance = Object.entries(
1010
- balanceState.providerBalances
1011
- ).filter(([providerBaseUrl]) => providerBaseUrl !== baseUrl).reduce((sum, [, value]) => sum + value, 0);
1012
- if (totalMintBalance + targetProviderBalance < adjustedAmount && totalMintBalance + targetProviderBalance + refundableProviderBalance >= adjustedAmount && retryCount < 2) {
1013
- await this._refundOtherProvidersForTopUp(baseUrl, mintUrl, retryCount);
1014
- return this.createProviderToken({
1015
- ...options,
1016
- retryCount: retryCount + 1
1017
- });
1018
- }
1019
- if (totalMintBalance + targetProviderBalance < adjustedAmount) {
1020
- const error = new InsufficientBalanceError(
1021
- adjustedAmount,
1022
- totalMintBalance + targetProviderBalance,
1023
- totalMintBalance,
1024
- Object.entries(balanceState.mintBalances).reduce(
1025
- (max, [url, balance]) => balance > max.balance ? { url, balance } : max,
1026
- { url: "", balance: 0 }
1027
- ).url
1028
- );
1029
- this.logger.error(`createProviderToken: insufficient balance required=${adjustedAmount} available=${totalMintBalance + targetProviderBalance} totalMint=${totalMintBalance} targetProvider=${targetProviderBalance}`);
1030
- return { success: false, error: error.message };
1031
- }
1032
- const providerMints = baseUrl && this.providerRegistry ? this.providerRegistry.getProviderMints(baseUrl) : [];
1033
- let requiredAmount = adjustedAmount;
1034
- const supportedMintsOnly = providerMints.length > 0;
1035
- let candidates = this._selectCandidateMints({
1036
- balances,
1037
- units,
1038
- amount: requiredAmount,
1039
- preferredMintUrl: mintUrl,
1040
- excludeMints,
1041
- allowedMints: supportedMintsOnly ? providerMints : void 0
1042
- });
1043
- if (candidates.length === 0 && supportedMintsOnly) {
1044
- requiredAmount += 2;
1045
- candidates = this._selectCandidateMints({
1046
- balances,
1047
- units,
1048
- amount: requiredAmount,
1049
- preferredMintUrl: mintUrl,
1050
- excludeMints
1051
- });
1052
- }
1053
- if (candidates.length === 0) {
1054
- let maxBalance = 0;
1055
- let maxMintUrl = "";
1056
- for (const mintUrl2 in balances) {
1057
- const balance = balances[mintUrl2];
1058
- const unit = units[mintUrl2];
1059
- const balanceInSats = getBalanceInSats(balance, unit);
1060
- if (balanceInSats > maxBalance) {
1061
- maxBalance = balanceInSats;
1062
- maxMintUrl = mintUrl2;
1063
- }
1064
- }
1065
- this.logger.error(`createProviderToken: no candidate mints required=${requiredAmount} totalMint=${totalMintBalance} maxBalance=${maxBalance} maxMint=${maxMintUrl}`);
1066
- const error = new InsufficientBalanceError(
1067
- adjustedAmount,
1068
- totalMintBalance,
1069
- maxBalance,
1070
- maxMintUrl
1071
- );
1072
- return { success: false, error: error.message };
1073
- }
1074
- let lastError;
1075
- for (const candidateMint of candidates) {
1076
- try {
1077
- this.logger.log(`createProviderToken: attempting mint=${candidateMint} amount=${requiredAmount}`);
1078
- const token = await this.walletAdapter.sendToken(
1079
- candidateMint,
1080
- requiredAmount,
1081
- p2pkPubkey
1082
- );
1083
- this.logger.log(`createProviderToken: success from mint=${candidateMint}`);
1084
- return {
1085
- success: true,
1086
- token,
1087
- selectedMintUrl: candidateMint,
1088
- amountSpent: requiredAmount
1089
- };
1090
- } catch (error) {
1091
- const errorMsg = error instanceof Error ? error.message : String(error);
1092
- this.logger.error(`createProviderToken: mint=${candidateMint} failed: ${errorMsg}`);
1093
- if (error instanceof Error) {
1094
- lastError = errorMsg;
1095
- if (isNetworkErrorMessage(error.message)) {
1096
- this.logger.warn(`createProviderToken: network error from ${candidateMint}, trying next mint...`);
1097
- continue;
1098
- }
1099
- }
1100
- return {
1101
- success: false,
1102
- error: lastError || "Failed to create top up token"
1103
- };
1104
- }
1105
- }
1106
- this.logger.error(`createProviderToken: all candidate mints exhausted lastError=${lastError}`);
1107
- return {
1108
- success: false,
1109
- error: lastError || "All candidate mints failed while creating top up token"
1110
- };
1111
- }
1112
- _selectCandidateMints(options) {
1113
- const {
1114
- balances,
1115
- units,
1116
- amount,
1117
- preferredMintUrl,
1118
- excludeMints,
1119
- allowedMints
1120
- } = options;
1121
- const candidates = [];
1122
- const { selectedMintUrl: firstMint } = selectMintWithBalance(
1123
- balances,
1124
- units,
1125
- amount,
1126
- excludeMints
1127
- );
1128
- if (firstMint && (!allowedMints || allowedMints.length === 0 || allowedMints.includes(firstMint))) {
1129
- candidates.push(firstMint);
1130
- }
1131
- const canUseMint = (mint) => {
1132
- if (excludeMints.includes(mint)) return false;
1133
- if (allowedMints && allowedMints.length > 0 && !allowedMints.includes(mint)) {
1134
- return false;
1135
- }
1136
- const rawBalance = balances[mint] || 0;
1137
- const unit = units[mint];
1138
- const balanceInSats = getBalanceInSats(rawBalance, unit);
1139
- return balanceInSats >= amount;
1140
- };
1141
- if (preferredMintUrl && canUseMint(preferredMintUrl) && !candidates.includes(preferredMintUrl)) {
1142
- candidates.push(preferredMintUrl);
1143
- }
1144
- for (const mint in balances) {
1145
- if (mint === preferredMintUrl || candidates.includes(mint)) continue;
1146
- if (canUseMint(mint)) {
1147
- candidates.push(mint);
1148
- }
1149
- }
1150
- return candidates;
1151
- }
1152
- async _refundOtherProvidersForTopUp(baseUrl, mintUrl, retryCount) {
1153
- const apiKeyDistribution = this.storageAdapter.getApiKeyDistribution();
1154
- const forceRefund = retryCount >= 2;
1155
- const apiKeysToRefund = apiKeyDistribution.filter(
1156
- (apiKey) => apiKey.baseUrl !== baseUrl && apiKey.amount > 0
1157
- );
1158
- const apiKeyRefundResults = await Promise.allSettled(
1159
- apiKeysToRefund.map(async (apiKeyEntry) => {
1160
- const fullApiKeyEntry = this.storageAdapter.getApiKey(
1161
- apiKeyEntry.baseUrl
1162
- );
1163
- if (!fullApiKeyEntry) {
1164
- return { baseUrl: apiKeyEntry.baseUrl, success: false };
1165
- }
1166
- const result = await this.refundApiKey({
1167
- mintUrl,
1168
- baseUrl: apiKeyEntry.baseUrl,
1169
- apiKey: fullApiKeyEntry.key,
1170
- forceRefund
1171
- });
1172
- return { baseUrl: apiKeyEntry.baseUrl, success: result.success };
1173
- })
1174
- );
1175
- for (const result of apiKeyRefundResults) {
1176
- if (result.status === "fulfilled" && result.value.success) {
1177
- this.storageAdapter.updateApiKeyBalance(result.value.baseUrl, 0);
1178
- }
1179
- }
1180
- }
1181
- /**
1182
- * Post topup request to provider API
1183
- */
1184
- async _postTopUp(baseUrl, storedToken, cashuToken) {
1185
- if (!baseUrl) {
1186
- return {
1187
- success: false,
1188
- error: "No base URL configured"
1189
- };
1190
- }
1191
- const normalizedBaseUrl = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
1192
- const url = `${normalizedBaseUrl}v1/wallet/topup?cashu_token=${encodeURIComponent(
1193
- cashuToken
1194
- )}`;
1195
- const controller = new AbortController();
1196
- const timeoutId = setTimeout(() => {
1197
- controller.abort();
1198
- }, 6e4);
1199
- try {
1200
- const response = await fetch(url, {
1201
- method: "POST",
1202
- headers: {
1203
- Authorization: `Bearer ${storedToken}`,
1204
- "Content-Type": "application/json"
1205
- },
1206
- signal: controller.signal
1207
- });
1208
- clearTimeout(timeoutId);
1209
- const requestId = response.headers.get("x-routstr-request-id") || void 0;
1210
- if (!response.ok) {
1211
- const errorData = await response.json().catch(() => ({}));
1212
- return {
1213
- success: false,
1214
- requestId,
1215
- error: errorData?.detail || `Top up failed with status ${response.status}`
1216
- };
1217
- }
1218
- return { success: true, requestId };
1219
- } catch (error) {
1220
- clearTimeout(timeoutId);
1221
- this.logger.error("_postTopUp fetch error", error);
1222
- if (error instanceof Error) {
1223
- if (error.name === "AbortError") {
1224
- return {
1225
- success: false,
1226
- error: "Request timed out after 1 minute"
1227
- };
1228
- }
1229
- return {
1230
- success: false,
1231
- error: error.message
1232
- };
1233
- }
1234
- return {
1235
- success: false,
1236
- error: "Unknown error occurred during top up request"
1237
- };
1238
- }
1239
- }
1240
- /**
1241
- * Attempt to receive token back after failed top up
1242
- */
1243
- async _recoverFailedTopUp(cashuToken) {
1244
- try {
1245
- await this.cashuSpender.receiveToken(cashuToken);
1246
- } catch (error) {
1247
- this.logger.error("_recoverFailedTopUp: failed to recover token", error);
1248
- }
1249
- }
1250
- /**
1251
- * Handle refund errors with specific error types
1252
- */
1253
- _handleRefundError(error, mintUrl, requestId) {
1254
- if (error instanceof Error) {
1255
- if (isNetworkErrorMessage(error.message)) {
1256
- return {
1257
- success: false,
1258
- message: `Failed to connect to the mint: ${mintUrl}`,
1259
- requestId
1260
- };
1261
- }
1262
- if (error.message.includes("Wallet not found")) {
1263
- return {
1264
- success: false,
1265
- message: `Wallet couldn't be loaded. Please save this refunded cashu token manually.`,
1266
- requestId
1267
- };
1268
- }
1269
- return {
1270
- success: false,
1271
- message: error.message,
1272
- requestId
1273
- };
1274
- }
1275
- return {
1276
- success: false,
1277
- message: "Refund failed",
1278
- requestId
1279
- };
1280
- }
1281
- /**
1282
- * Get token balance from provider
1283
- */
1284
- async getTokenBalance(token, baseUrl) {
1285
- try {
1286
- const response = await fetch(`${baseUrl}v1/wallet/info`, {
1287
- headers: {
1288
- Authorization: `Bearer ${token}`
1289
- }
1290
- });
1291
- if (response.ok) {
1292
- const data = await response.json();
1293
- return {
1294
- amount: data.balance,
1295
- reserved: data.reserved ?? 0,
1296
- unit: "msat",
1297
- apiKey: data.api_key
1298
- };
1299
- } else {
1300
- this.logger.warn(`getTokenBalance: status=${response.status}`);
1301
- const data = await response.json();
1302
- this.logger.warn("getTokenBalance: FAILED", data);
1303
- const isInvalidApiKey = response.status === 401 && data?.detail?.error?.code === "invalid_api_key" && data?.detail?.error?.message?.includes("proofs already spent");
1304
- return {
1305
- amount: -1,
1306
- reserved: data.reserved ?? 0,
1307
- unit: "msat",
1308
- apiKey: data.api_key,
1309
- isInvalidApiKey
1310
- };
1311
- }
1312
- } catch (error) {
1313
- this.logger.error("getTokenBalance error", error);
1314
- }
1315
- return { amount: -1, reserved: 0, unit: "sat", apiKey: "" };
1316
- }
1317
- /**
1318
- * Handle topup errors with specific error types
1319
- */
1320
- _handleTopUpError(error, mintUrl, requestId) {
1321
- if (error instanceof Error) {
1322
- if (isNetworkErrorMessage(error.message)) {
1323
- return {
1324
- success: false,
1325
- message: `Failed to connect to the mint: ${mintUrl}`,
1326
- requestId
1327
- };
1328
- }
1329
- if (error.message.includes("Wallet not found")) {
1330
- return {
1331
- success: false,
1332
- message: "Wallet couldn't be loaded. The cashu token was recovered locally.",
1333
- requestId
1334
- };
1335
- }
1336
- return {
1337
- success: false,
1338
- message: error.message,
1339
- requestId
1340
- };
1341
- }
1342
- return {
1343
- success: false,
1344
- message: "Top up failed",
1345
- requestId
1346
- };
1347
- }
1348
- };
1349
-
1350
- // client/usage.ts
1351
- function extractUsageFromResponseBody(body, fallbackSatsCost = 0) {
1352
- if (!body || typeof body !== "object") return null;
1353
- const usage = body.usage;
1354
- if (!usage || typeof usage !== "object") return null;
1355
- const promptTokens = Number(usage.prompt_tokens ?? 0);
1356
- const completionTokens = Number(usage.completion_tokens ?? 0);
1357
- const totalTokens = Number(usage.total_tokens ?? 0);
1358
- const costValue = usage.cost;
1359
- let cost = 0;
1360
- let satsCost = fallbackSatsCost;
1361
- if (typeof costValue === "number") {
1362
- cost = costValue;
1363
- } else if (costValue && typeof costValue === "object") {
1364
- const costObj = costValue;
1365
- const totalUsd = costObj.total_usd;
1366
- const totalMsats = costObj.total_msats;
1367
- cost = typeof totalUsd === "number" ? totalUsd : 0;
1368
- if (typeof totalMsats === "number") {
1369
- satsCost = totalMsats / 1e3;
1370
- }
1371
- }
1372
- if (promptTokens === 0 && completionTokens === 0 && totalTokens === 0 && cost === 0 && satsCost === 0) {
1373
- return null;
1374
- }
1375
- return {
1376
- promptTokens,
1377
- completionTokens,
1378
- totalTokens,
1379
- cost,
1380
- satsCost
1381
- };
1382
- }
1383
- function extractResponseId(body) {
1384
- if (!body || typeof body !== "object") return void 0;
1385
- const id = body.id;
1386
- if (typeof id !== "string") return void 0;
1387
- const trimmed = id.trim();
1388
- return trimmed.length > 0 ? trimmed : void 0;
1389
- }
1390
- function extractUsageFromSSEJson(parsed, fallbackSatsCost = 0) {
1391
- if (!parsed || typeof parsed !== "object") {
1392
- return null;
1393
- }
1394
- if (!parsed.usage && parsed.cost && typeof parsed.cost === "object") {
1395
- const costObj = parsed.cost;
1396
- const msats2 = costObj.total_msats ?? 0;
1397
- const cost2 = costObj.total_usd ?? 0;
1398
- if (msats2 === 0 && cost2 === 0) return null;
1399
- return {
1400
- promptTokens: Number(costObj.input_tokens ?? 0),
1401
- completionTokens: Number(costObj.output_tokens ?? 0),
1402
- totalTokens: Number((costObj.input_tokens ?? 0) + (costObj.output_tokens ?? 0)),
1403
- cost: Number(cost2),
1404
- satsCost: msats2 > 0 ? msats2 / 1e3 : fallbackSatsCost
1405
- };
1406
- }
1407
- if (!parsed.usage) {
1408
- return null;
1409
- }
1410
- const usage = parsed.usage;
1411
- const usageCost = usage.cost;
1412
- let cost = 0;
1413
- let msats = 0;
1414
- if (typeof usageCost === "number") {
1415
- cost = usageCost;
1416
- } else if (usageCost && typeof usageCost === "object") {
1417
- cost = usageCost.total_usd ?? 0;
1418
- msats = usageCost.total_msats ?? 0;
1419
- }
1420
- if (cost === 0) {
1421
- cost = parsed.metadata?.routstr?.cost?.total_usd ?? 0;
1422
- }
1423
- if (msats === 0) {
1424
- msats = parsed.metadata?.routstr?.cost?.total_msats ?? (typeof usage.cost_sats === "number" ? usage.cost_sats * 1e3 : 0);
1425
- }
1426
- const promptTokens = Number(usage.prompt_tokens ?? usage.input_tokens ?? 0);
1427
- const completionTokens = Number(usage.completion_tokens ?? usage.output_tokens ?? 0);
1428
- const totalTokens = Number(usage.total_tokens ?? promptTokens + completionTokens);
1429
- const result = {
1430
- promptTokens,
1431
- completionTokens,
1432
- totalTokens,
1433
- cost: Number(cost ?? 0),
1434
- satsCost: msats > 0 ? msats / 1e3 : fallbackSatsCost
1435
- };
1436
- if (result.promptTokens === 0 && result.completionTokens === 0 && result.totalTokens === 0 && result.cost === 0 && result.satsCost === 0) {
1437
- return null;
1438
- }
1439
- return result;
1440
- }
1441
- function toUsageStats(usage) {
1442
- if (!usage) return void 0;
1443
- return {
1444
- total_tokens: usage.totalTokens,
1445
- prompt_tokens: usage.promptTokens,
1446
- completion_tokens: usage.completionTokens,
1447
- cost: usage.cost,
1448
- sats_cost: usage.satsCost
1449
- };
1450
- }
1451
-
1452
- // client/StreamProcessor.ts
1453
- var StreamProcessor = class {
1454
- accumulatedContent = "";
1455
- accumulatedThinking = "";
1456
- accumulatedImages = [];
1457
- isInThinking = false;
1458
- isInContent = false;
1459
- /**
1460
- * Process a streaming response
1461
- */
1462
- async process(response, callbacks, modelId) {
1463
- if (!response.body) {
1464
- throw new Error("Response body is not available");
1465
- }
1466
- const reader = response.body.getReader();
1467
- const decoder = new TextDecoder("utf-8");
1468
- let buffer = "";
1469
- this.accumulatedContent = "";
1470
- this.accumulatedThinking = "";
1471
- this.accumulatedImages = [];
1472
- this.isInThinking = false;
1473
- this.isInContent = false;
1474
- let usage;
1475
- let model;
1476
- let finish_reason;
1477
- let citations;
1478
- let annotations;
1479
- let responseId;
1480
- try {
1481
- while (true) {
1482
- const { done, value } = await reader.read();
1483
- if (done) {
1484
- break;
1485
- }
1486
- const chunk = decoder.decode(value, { stream: true });
1487
- buffer += chunk;
1488
- const lines = buffer.split("\n");
1489
- buffer = lines.pop() || "";
1490
- for (const line of lines) {
1491
- const parsed = this._parseLine(line);
1492
- if (!parsed) continue;
1493
- if (parsed.content) {
1494
- this._handleContent(parsed.content, callbacks, modelId);
1495
- }
1496
- if (parsed.reasoning) {
1497
- this._handleThinking(parsed.reasoning, callbacks);
1498
- }
1499
- if (parsed.usage) {
1500
- usage = parsed.usage;
1501
- }
1502
- if (parsed.model) {
1503
- model = parsed.model;
1504
- }
1505
- if (parsed.finish_reason) {
1506
- finish_reason = parsed.finish_reason;
1507
- }
1508
- if (parsed.responseId) {
1509
- responseId = parsed.responseId;
1510
- }
1511
- if (parsed.citations) {
1512
- citations = parsed.citations;
1513
- }
1514
- if (parsed.annotations) {
1515
- annotations = parsed.annotations;
1516
- }
1517
- if (parsed.images) {
1518
- this._mergeImages(parsed.images);
1519
- }
1520
- }
1521
- }
1522
- } finally {
1523
- reader.releaseLock();
1524
- }
1525
- return {
1526
- content: this.accumulatedContent,
1527
- thinking: this.accumulatedThinking || void 0,
1528
- images: this.accumulatedImages.length > 0 ? this.accumulatedImages : void 0,
1529
- usage,
1530
- model,
1531
- responseId,
1532
- finish_reason,
1533
- citations,
1534
- annotations
1535
- };
1536
- }
1537
- /**
1538
- * Parse a single SSE line
1539
- */
1540
- _parseLine(line) {
1541
- if (!line.trim()) return null;
1542
- if (!line.startsWith("data: ")) {
1543
- return null;
1544
- }
1545
- const jsonData = line.slice(6);
1546
- if (jsonData === "[DONE]") {
1547
- return null;
1548
- }
1549
- try {
1550
- const parsed = JSON.parse(jsonData);
1551
- const result = {};
1552
- if (parsed.choices?.[0]?.delta?.content) {
1553
- result.content = parsed.choices[0].delta.content;
1554
- }
1555
- if (parsed.choices?.[0]?.delta?.reasoning) {
1556
- result.reasoning = parsed.choices[0].delta.reasoning;
1557
- }
1558
- const extractedUsage = extractUsageFromSSEJson(parsed);
1559
- if (extractedUsage) {
1560
- result.usage = toUsageStats(extractedUsage);
1561
- } else if (parsed.usage) {
1562
- result.usage = {
1563
- total_tokens: parsed.usage.total_tokens ?? parsed.usage.input_tokens + parsed.usage.output_tokens,
1564
- prompt_tokens: parsed.usage.prompt_tokens ?? parsed.usage.input_tokens,
1565
- completion_tokens: parsed.usage.completion_tokens ?? parsed.usage.output_tokens
1566
- };
1567
- }
1568
- if (parsed.id) {
1569
- result.responseId = parsed.id;
1570
- }
1571
- if (parsed.model) {
1572
- result.model = parsed.model;
1573
- }
1574
- if (parsed.citations) {
1575
- result.citations = parsed.citations;
1576
- }
1577
- if (parsed.annotations) {
1578
- result.annotations = parsed.annotations;
1579
- }
1580
- if (parsed.choices?.[0]?.finish_reason) {
1581
- result.finish_reason = parsed.choices[0].finish_reason;
1582
- }
1583
- const images = parsed.choices?.[0]?.message?.images || parsed.choices?.[0]?.delta?.images;
1584
- if (images && Array.isArray(images)) {
1585
- result.images = images;
1586
- }
1587
- return result;
1588
- } catch {
1589
- return null;
1590
- }
1591
- }
1592
- /**
1593
- * Handle content delta with thinking support
1594
- */
1595
- _handleContent(content, callbacks, modelId) {
1596
- if (this.isInThinking && !this.isInContent) {
1597
- this.accumulatedThinking += "</thinking>";
1598
- callbacks.onThinking(this.accumulatedThinking);
1599
- this.isInThinking = false;
1600
- this.isInContent = true;
1601
- }
1602
- if (modelId) {
1603
- this._extractThinkingFromContent(content, callbacks);
1604
- } else {
1605
- this.accumulatedContent += content;
1606
- }
1607
- callbacks.onContent(this.accumulatedContent);
1608
- }
1609
- /**
1610
- * Handle thinking/reasoning content
1611
- */
1612
- _handleThinking(reasoning, callbacks) {
1613
- if (!this.isInThinking) {
1614
- this.accumulatedThinking += "<thinking> ";
1615
- this.isInThinking = true;
1616
- }
1617
- this.accumulatedThinking += reasoning;
1618
- callbacks.onThinking(this.accumulatedThinking);
1619
- }
1620
- /**
1621
- * Extract thinking blocks from content (for models with inline thinking)
1622
- */
1623
- _extractThinkingFromContent(content, callbacks) {
1624
- const parts = content.split(/(<thinking>|<\/thinking>)/);
1625
- for (const part of parts) {
1626
- if (part === "<thinking>") {
1627
- this.isInThinking = true;
1628
- if (!this.accumulatedThinking.includes("<thinking>")) {
1629
- this.accumulatedThinking += "<thinking> ";
1630
- }
1631
- } else if (part === "</thinking>") {
1632
- this.isInThinking = false;
1633
- this.accumulatedThinking += "</thinking>";
1634
- } else if (this.isInThinking) {
1635
- this.accumulatedThinking += part;
1636
- } else {
1637
- this.accumulatedContent += part;
1638
- }
1639
- }
1640
- }
1641
- /**
1642
- * Merge images into accumulated array, avoiding duplicates
1643
- */
1644
- _mergeImages(newImages) {
1645
- for (const img of newImages) {
1646
- const newUrl = img.image_url?.url;
1647
- const existingIndex = this.accumulatedImages.findIndex((existing) => {
1648
- const existingUrl = existing.image_url?.url;
1649
- if (newUrl && existingUrl) {
1650
- return existingUrl === newUrl;
1651
- }
1652
- if (img.index !== void 0 && existing.index !== void 0) {
1653
- return existing.index === img.index;
1654
- }
1655
- return false;
1656
- });
1657
- if (existingIndex === -1) {
1658
- this.accumulatedImages.push(img);
1659
- } else {
1660
- this.accumulatedImages[existingIndex] = img;
1661
- }
1662
- }
1663
- }
1664
- };
1665
-
1666
- // utils/torUtils.ts
1667
- var TOR_ONION_SUFFIX = ".onion";
1668
- var isTorContext = () => {
1669
- if (typeof window === "undefined") return false;
1670
- const hostname = window.location.hostname.toLowerCase();
1671
- return hostname.endsWith(TOR_ONION_SUFFIX);
1672
- };
1673
- var isOnionUrl = (url) => {
1674
- if (!url) return false;
1675
- const trimmed = url.trim().toLowerCase();
1676
- if (!trimmed) return false;
1677
- try {
1678
- const candidate = trimmed.startsWith("http") ? trimmed : `http://${trimmed}`;
1679
- return new URL(candidate).hostname.endsWith(TOR_ONION_SUFFIX);
1680
- } catch {
1681
- return trimmed.includes(TOR_ONION_SUFFIX);
1682
- }
1683
- };
1684
-
1685
- // client/ProviderManager.ts
1686
- function getImageResolutionFromDataUrl(dataUrl) {
1687
- try {
1688
- if (typeof dataUrl !== "string" || !dataUrl.startsWith("data:"))
1689
- return null;
1690
- const commaIdx = dataUrl.indexOf(",");
1691
- if (commaIdx === -1) return null;
1692
- const meta = dataUrl.slice(5, commaIdx);
1693
- const base64 = dataUrl.slice(commaIdx + 1);
1694
- const binary = typeof atob === "function" ? atob(base64) : Buffer.from(base64, "base64").toString("binary");
1695
- const len = binary.length;
1696
- const bytes = new Uint8Array(len);
1697
- for (let i = 0; i < len; i++) bytes[i] = binary.charCodeAt(i);
1698
- const isPNG = meta.includes("image/png");
1699
- const isJPEG = meta.includes("image/jpeg") || meta.includes("image/jpg");
1700
- if (isPNG) {
1701
- const sig = [137, 80, 78, 71, 13, 10, 26, 10];
1702
- for (let i = 0; i < sig.length; i++) {
1703
- if (bytes[i] !== sig[i]) return null;
1704
- }
1705
- const view = new DataView(
1706
- bytes.buffer,
1707
- bytes.byteOffset,
1708
- bytes.byteLength
1709
- );
1710
- const width = view.getUint32(16, false);
1711
- const height = view.getUint32(20, false);
1712
- if (width > 0 && height > 0) return { width, height };
1713
- return null;
1714
- }
1715
- if (isJPEG) {
1716
- let offset = 0;
1717
- if (bytes[offset++] !== 255 || bytes[offset++] !== 216) return null;
1718
- while (offset < bytes.length) {
1719
- while (offset < bytes.length && bytes[offset] !== 255) offset++;
1720
- if (offset + 1 >= bytes.length) break;
1721
- while (bytes[offset] === 255) offset++;
1722
- const marker = bytes[offset++];
1723
- if (marker === 216 || marker === 217) continue;
1724
- if (offset + 1 >= bytes.length) break;
1725
- const length = bytes[offset] << 8 | bytes[offset + 1];
1726
- offset += 2;
1727
- if (marker === 192 || marker === 194) {
1728
- if (length < 7 || offset + length - 2 > bytes.length) return null;
1729
- const precision = bytes[offset];
1730
- const height = bytes[offset + 1] << 8 | bytes[offset + 2];
1731
- const width = bytes[offset + 3] << 8 | bytes[offset + 4];
1732
- if (precision > 0 && width > 0 && height > 0)
1733
- return { width, height };
1734
- return null;
1735
- } else {
1736
- offset += length - 2;
1737
- }
1738
- }
1739
- return null;
1740
- }
1741
- return null;
1742
- } catch {
1743
- return null;
1744
- }
1745
- }
1746
- function calculateImageTokens(width, height, detail = "auto") {
1747
- if (detail === "low") return 85;
1748
- let w = width;
1749
- let h = height;
1750
- if (w > 2048 || h > 2048) {
1751
- const aspectRatio = w / h;
1752
- if (w > h) {
1753
- w = 2048;
1754
- h = Math.floor(w / aspectRatio);
1755
- } else {
1756
- h = 2048;
1757
- w = Math.floor(h * aspectRatio);
1758
- }
1759
- }
1760
- if (w > 768 || h > 768) {
1761
- const aspectRatio = w / h;
1762
- if (w > h) {
1763
- w = 768;
1764
- h = Math.floor(w / aspectRatio);
1765
- } else {
1766
- h = 768;
1767
- w = Math.floor(h * aspectRatio);
1768
- }
1769
- }
1770
- const tilesWidth = Math.floor((w + 511) / 512);
1771
- const tilesHeight = Math.floor((h + 511) / 512);
1772
- const numTiles = tilesWidth * tilesHeight;
1773
- return 85 + 170 * numTiles;
1774
- }
1775
- function isInsecureHttpUrl(url) {
1776
- return url.startsWith("http://");
1777
- }
1778
- var ProviderManager = class _ProviderManager {
1779
- constructor(providerRegistry, store, logger) {
1780
- this.providerRegistry = providerRegistry;
1781
- this.instanceId = `pm_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
1782
- this.logger = (logger ?? consoleLogger).child(`ProviderManager:${this.instanceId}`);
1783
- if (store) {
1784
- this.store = store;
1785
- this.hydrateFromStore();
1786
- }
1787
- }
1788
- failedProviders = /* @__PURE__ */ new Set();
1789
- /** Track when each provider last failed (provider URL -> timestamp) */
1790
- lastFailed = /* @__PURE__ */ new Map();
1791
- /** Providers on cooldown: [provider_url, cooldown_started_timestamp][] */
1792
- providersOnCoolDown = [];
1793
- /** Cooldown duration in milliseconds (42 seconds) */
1794
- static COOLDOWN_DURATION_MS = 42 * 1e3;
1795
- /** Optional persistent store for failure tracking */
1796
- store = null;
1797
- /** Instance ID for debugging */
1798
- instanceId;
1799
- logger;
1800
- /**
1801
- * Hydrate in-memory state from persistent store
1802
- */
1803
- hydrateFromStore() {
1804
- if (!this.store) return;
1805
- const state = this.store.getState();
1806
- this.failedProviders = new Set(state.failedProviders);
1807
- this.lastFailed = new Map(Object.entries(state.lastFailed));
1808
- const now = Date.now();
1809
- this.providersOnCoolDown = state.providersOnCooldown.filter(
1810
- (entry) => now - entry.timestamp < _ProviderManager.COOLDOWN_DURATION_MS
1811
- ).map((entry) => [entry.baseUrl, entry.timestamp]);
1812
- this.logger.log(`Hydrated from store: failedProviders=${this.failedProviders.size} lastFailed=${this.lastFailed.size} providersOnCooldown=${this.providersOnCoolDown.length}`);
1813
- }
1814
- /**
1815
- * Get instance ID for debugging
1816
- */
1817
- getInstanceId() {
1818
- return this.instanceId;
1819
- }
1820
- /**
1821
- * Clean up expired cooldown entries
1822
- * Also removes the provider from failedProviders so it can be retried
1823
- */
1824
- cleanupExpiredCooldowns() {
1825
- const now = Date.now();
1826
- const before = this.providersOnCoolDown.length;
1827
- this.providersOnCoolDown = this.providersOnCoolDown.filter(
1828
- ([url, timestamp]) => {
1829
- const age = now - timestamp;
1830
- const isExpired = age >= _ProviderManager.COOLDOWN_DURATION_MS;
1831
- if (isExpired) {
1832
- this.logger.log(`Removing expired cooldown for ${url} (age: ${age}ms)`);
1833
- this.failedProviders.delete(url);
1834
- if (this.store) {
1835
- this.store.getState().removeFailedProvider(url);
1836
- }
1837
- }
1838
- return !isExpired;
1839
- }
1840
- );
1841
- const after = this.providersOnCoolDown.length;
1842
- if (before !== after) {
1843
- this.logger.log(`Cleaned up ${before - after} expired cooldown(s), ${after} remaining`);
1844
- }
1845
- }
1846
- /**
1847
- * Get the cooldown duration in milliseconds
1848
- */
1849
- getCooldownDurationMs() {
1850
- return _ProviderManager.COOLDOWN_DURATION_MS;
1851
- }
1852
- /**
1853
- * Check if a provider is currently on cooldown
1854
- */
1855
- isOnCooldown(baseUrl) {
1856
- this.cleanupExpiredCooldowns();
1857
- const result = this.providersOnCoolDown.some(([url]) => url === baseUrl);
1858
- return result;
1859
- }
1860
- /**
1861
- * Get all providers currently on cooldown
1862
- */
1863
- getProvidersOnCooldown() {
1864
- this.cleanupExpiredCooldowns();
1865
- return [...this.providersOnCoolDown];
1866
- }
1867
- /**
1868
- * Reset the failed providers list
1869
- */
1870
- resetFailedProviders() {
1871
- this.failedProviders.clear();
1872
- if (this.store) {
1873
- this.store.getState().setFailedProviders([]);
1874
- }
1875
- }
1876
- /**
1877
- * Get the last failed timestamp for a provider
1878
- */
1879
- getLastFailed(baseUrl) {
1880
- return this.lastFailed.get(baseUrl);
1881
- }
1882
- /**
1883
- * Get all providers with their last failed timestamps
1884
- */
1885
- getAllLastFailed() {
1886
- return new Map(this.lastFailed);
1887
- }
1888
- /**
1889
- * Mark a provider as failed
1890
- * If a provider fails twice within 5 minutes, it's added to cooldown
1891
- */
1892
- markFailed(baseUrl) {
1893
- const now = Date.now();
1894
- const lastFailure = this.lastFailed.get(baseUrl);
1895
- this.logger.log(`markFailed: ${baseUrl} lastFailure=${lastFailure} now=${now}`);
1896
- if (lastFailure !== void 0) {
1897
- const timeSinceLastFailure = now - lastFailure;
1898
- this.logger.log(`markFailed: timeSinceLastFailure=${timeSinceLastFailure}ms withinCooldown=${timeSinceLastFailure < _ProviderManager.COOLDOWN_DURATION_MS}`);
1899
- }
1900
- this.lastFailed.set(baseUrl, now);
1901
- this.failedProviders.add(baseUrl);
1902
- if (this.store) {
1903
- this.store.getState().setLastFailedTimestamp(baseUrl, now);
1904
- this.store.getState().addFailedProvider(baseUrl);
1905
- }
1906
- this.logger.log(`markFailed: updated ${baseUrl} to ${now}, failedProviders=${this.failedProviders.size}`);
1907
- if (lastFailure !== void 0 && now - lastFailure < _ProviderManager.COOLDOWN_DURATION_MS) {
1908
- this.logger.log(`markFailed: second failure within cooldown window for ${baseUrl}`);
1909
- if (!this.isOnCooldown(baseUrl)) {
1910
- this.providersOnCoolDown.push([baseUrl, now]);
1911
- if (this.store) {
1912
- this.store.getState().addProviderOnCooldown(baseUrl, now);
1913
- }
1914
- this.logger.log(`markFailed: ${baseUrl} added to cooldown`);
1915
- } else {
1916
- this.logger.log(`markFailed: ${baseUrl} already on cooldown`);
1917
- }
1918
- } else {
1919
- if (lastFailure === void 0) {
1920
- this.logger.log(`markFailed: first failure for ${baseUrl}`);
1921
- } else {
1922
- this.logger.log(`markFailed: failure outside cooldown window for ${baseUrl} (${now - lastFailure}ms ago)`);
1923
- }
1924
- }
1925
- }
1926
- /**
1927
- * Remove a provider from cooldown (e.g., after successful request)
1928
- */
1929
- removeFromCooldown(baseUrl) {
1930
- this.providersOnCoolDown = this.providersOnCoolDown.filter(
1931
- ([url]) => url !== baseUrl
1932
- );
1933
- if (this.store) {
1934
- this.store.getState().removeProviderFromCooldown(baseUrl);
1935
- }
1936
- }
1937
- /**
1938
- * Clear all cooldown tracking
1939
- */
1940
- clearCooldowns() {
1941
- this.providersOnCoolDown = [];
1942
- if (this.store) {
1943
- this.store.getState().clearProvidersOnCooldown();
1944
- }
1945
- }
1946
- /**
1947
- * Clear all failure tracking (lastFailed timestamps)
1948
- */
1949
- clearFailureHistory() {
1950
- this.lastFailed.clear();
1951
- if (this.store) {
1952
- this.store.getState().setLastFailed({});
1953
- }
1954
- }
1955
- /**
1956
- * Check if a provider has failed
1957
- */
1958
- hasFailed(baseUrl) {
1959
- return this.failedProviders.has(baseUrl);
1960
- }
1961
- /**
1962
- * Get a copy of the failed providers set
1963
- */
1964
- getFailedProviders() {
1965
- return new Set(this.failedProviders);
1966
- }
1967
- /**
1968
- * Find the next best provider for a model
1969
- * @param modelId The model ID to find a provider for
1970
- * @param currentBaseUrl The current provider to exclude
1971
- * @returns The best provider URL or null if none available
1972
- */
1973
- findNextBestProvider(modelId, currentBaseUrl) {
1974
- try {
1975
- const torMode = isTorContext();
1976
- const disabledProviders = new Set(
1977
- this.providerRegistry.getDisabledProviders()
1978
- );
1979
- this.logger.log(`findNextBestProvider: model=${modelId} disabled=${[...disabledProviders].length} onCooldown=${this.providersOnCoolDown.length}`);
1980
- const allProviders = this.providerRegistry.getAllProvidersModels();
1981
- this.logger.log(`findNextBestProvider: total providers=${Object.keys(allProviders).length}`);
1982
- const candidates = [];
1983
- for (const [baseUrl, models] of Object.entries(allProviders)) {
1984
- if (baseUrl === currentBaseUrl) {
1985
- continue;
1986
- }
1987
- if (disabledProviders.has(baseUrl)) {
1988
- continue;
1989
- }
1990
- if (this.isOnCooldown(baseUrl)) {
1991
- continue;
1992
- }
1993
- if (!torMode && (isOnionUrl(baseUrl) || isInsecureHttpUrl(baseUrl))) {
1994
- continue;
1995
- }
1996
- const model = models.find((m) => m.id === modelId);
1997
- if (!model) {
1998
- continue;
1999
- }
2000
- const cost = model.sats_pricing?.completion ?? 0;
2001
- candidates.push({ baseUrl, model, cost });
2002
- }
2003
- candidates.sort((a, b) => a.cost - b.cost);
2004
- if (candidates.length > 0) {
2005
- return candidates[0].baseUrl;
2006
- } else {
2007
- return null;
2008
- }
2009
- } catch (error) {
2010
- this.logger.error("findNextBestProvider error:", error);
2011
- return null;
2012
- }
2013
- }
2014
- /**
2015
- * Find the best model for a provider
2016
- * Useful when switching providers and need to find equivalent model
2017
- */
2018
- async getModelForProvider(baseUrl, modelId) {
2019
- const models = this.providerRegistry.getModelsForProvider(baseUrl);
2020
- const exactMatch = models.find((m) => m.id === modelId);
2021
- if (exactMatch) return exactMatch;
2022
- const providerInfo = await this.providerRegistry.getProviderInfo(baseUrl);
2023
- if (providerInfo?.version && /^0\.1\./.test(providerInfo.version)) {
2024
- const suffix = modelId.split("/").pop();
2025
- const suffixMatch = models.find((m) => m.id === suffix);
2026
- if (suffixMatch) return suffixMatch;
2027
- }
2028
- return null;
2029
- }
2030
- /**
2031
- * Get all available providers for a model
2032
- * Returns sorted list by price
2033
- */
2034
- getAllProvidersForModel(modelId) {
2035
- const candidates = [];
2036
- const allProviders = this.providerRegistry.getAllProvidersModels();
2037
- const disabledProviders = new Set(
2038
- this.providerRegistry.getDisabledProviders()
2039
- );
2040
- const torMode = isTorContext();
2041
- for (const [baseUrl, models] of Object.entries(allProviders)) {
2042
- if (disabledProviders.has(baseUrl)) continue;
2043
- if (this.isOnCooldown(baseUrl)) continue;
2044
- if (!torMode && (isOnionUrl(baseUrl) || isInsecureHttpUrl(baseUrl)))
2045
- continue;
2046
- const model = models.find((m) => m.id === modelId);
2047
- if (!model) continue;
2048
- const cost = model.sats_pricing?.completion ?? 0;
2049
- candidates.push({ baseUrl, model, cost });
2050
- }
2051
- return candidates.sort((a, b) => a.cost - b.cost);
2052
- }
2053
- /**
2054
- * Get providers for a model sorted by prompt+completion pricing
2055
- */
2056
- getProviderPriceRankingForModel(modelId, options = {}) {
2057
- const includeDisabled = options.includeDisabled ?? false;
2058
- const torMode = options.torMode ?? false;
2059
- const disabledProviders = new Set(
2060
- this.providerRegistry.getDisabledProviders()
2061
- );
2062
- const allModels = this.providerRegistry.getAllProvidersModels();
2063
- const results = [];
2064
- for (const [baseUrl, models] of Object.entries(allModels)) {
2065
- if (!includeDisabled && disabledProviders.has(baseUrl)) continue;
2066
- if (this.isOnCooldown(baseUrl)) continue;
2067
- if (torMode && !baseUrl.includes(".onion")) continue;
2068
- if (!torMode && (baseUrl.includes(".onion") || isInsecureHttpUrl(baseUrl)))
2069
- continue;
2070
- const match = models.find((model) => model.id === modelId);
2071
- if (!match?.sats_pricing) continue;
2072
- const prompt = match.sats_pricing.prompt;
2073
- const completion = match.sats_pricing.completion;
2074
- if (typeof prompt !== "number" || typeof completion !== "number") {
2075
- continue;
2076
- }
2077
- const promptPerMillion = prompt * 1e6;
2078
- const completionPerMillion = completion * 1e6;
2079
- const totalPerMillion = promptPerMillion + completionPerMillion;
2080
- results.push({
2081
- baseUrl,
2082
- model: match,
2083
- promptPerMillion,
2084
- completionPerMillion,
2085
- totalPerMillion
2086
- });
2087
- }
2088
- return results.sort((a, b) => {
2089
- if (a.totalPerMillion !== b.totalPerMillion) {
2090
- return a.totalPerMillion - b.totalPerMillion;
2091
- }
2092
- return a.baseUrl.localeCompare(b.baseUrl);
2093
- });
2094
- }
2095
- /**
2096
- * Get best-priced provider for a specific model
2097
- */
2098
- getBestProviderForModel(modelId, options = {}) {
2099
- const ranking = this.getProviderPriceRankingForModel(modelId, options);
2100
- return ranking[0]?.baseUrl ?? null;
2101
- }
2102
- normalizeModelId(modelId) {
2103
- return modelId.includes("/") ? modelId.split("/").pop() || modelId : modelId;
2104
- }
2105
- /**
2106
- * Check if a provider accepts a specific mint
2107
- */
2108
- providerAcceptsMint(baseUrl, mintUrl) {
2109
- const providerMints = this.providerRegistry.getProviderMints(baseUrl);
2110
- if (providerMints.length === 0) {
2111
- return true;
2112
- }
2113
- return providerMints.includes(mintUrl);
2114
- }
2115
- /**
2116
- * Get required sats for a model based on message history
2117
- * Simple estimation based on typical usage
2118
- */
2119
- getRequiredSatsForModel(model, apiMessages, maxTokens) {
2120
- try {
2121
- let imageTokens = 0;
2122
- if (apiMessages) {
2123
- for (const msg of apiMessages) {
2124
- const content = msg?.content;
2125
- if (Array.isArray(content)) {
2126
- for (const part of content) {
2127
- const isImage = part && typeof part === "object" && part.type === "image_url";
2128
- const url = isImage ? typeof part.image_url === "string" ? part.image_url : part.image_url?.url : void 0;
2129
- if (url && typeof url === "string" && url.startsWith("data:")) {
2130
- const res = getImageResolutionFromDataUrl(url);
2131
- if (res) {
2132
- const tokensFromImage = calculateImageTokens(
2133
- res.width,
2134
- res.height
2135
- );
2136
- imageTokens += tokensFromImage;
2137
- this.logger.log(`IMAGE INPUT RESOLUTION width=${res.width} height=${res.height} tokens=${tokensFromImage}`);
2138
- } else {
2139
- this.logger.log("IMAGE INPUT RESOLUTION: unknown format");
2140
- }
2141
- }
2142
- }
2143
- }
2144
- }
2145
- }
2146
- const apiMessagesNoImages = apiMessages ? apiMessages.map((m) => {
2147
- if (Array.isArray(m?.content)) {
2148
- const filtered = m.content.filter(
2149
- (p) => !(p && typeof p === "object" && p.type === "image_url")
2150
- );
2151
- return { ...m, content: filtered };
2152
- }
2153
- return m;
2154
- }) : void 0;
2155
- const approximateTokens = apiMessagesNoImages ? Math.ceil(JSON.stringify(apiMessagesNoImages, null, 2).length / 2.84) : 1e4;
2156
- const totalInputTokens = approximateTokens + imageTokens;
2157
- const sp = model?.sats_pricing;
2158
- if (!sp) {
2159
- return 0;
2160
- }
2161
- if (!sp.max_completion_cost) {
2162
- return sp.max_cost ?? 50;
2163
- }
2164
- const promptCosts = (sp.prompt || 0) * totalInputTokens;
2165
- let completionCost = sp.max_completion_cost;
2166
- if (maxTokens !== void 0 && sp.completion) {
2167
- completionCost = sp.completion * maxTokens;
2168
- }
2169
- const totalEstimatedCosts = (promptCosts + completionCost) * 1.05;
2170
- return totalEstimatedCosts;
2171
- } catch (e) {
2172
- this.logger.error("getRequiredSatsForModel error:", e);
2173
- return 0;
2174
- }
2175
- }
2176
- };
2177
-
2178
- // storage/drivers/localStorage.ts
2179
- var canUseLocalStorage = () => {
2180
- return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
2181
- };
2182
- var isQuotaExceeded = (error) => {
2183
- const e = error;
2184
- return !!e && (e?.name === "QuotaExceededError" || e?.code === 22 || e?.code === 1014);
2185
- };
2186
- var NON_CRITICAL_KEYS = /* @__PURE__ */ new Set(["modelsFromAllProviders"]);
2187
- var localStorageDriver = {
2188
- async getItem(key, defaultValue) {
2189
- if (!canUseLocalStorage()) return defaultValue;
2190
- try {
2191
- const item = window.localStorage.getItem(key);
2192
- if (item === null) return defaultValue;
2193
- try {
2194
- return JSON.parse(item);
2195
- } catch (parseError) {
2196
- if (typeof defaultValue === "string") {
2197
- return item;
2198
- }
2199
- throw parseError;
2200
- }
2201
- } catch (error) {
2202
- console.error(`Error retrieving item with key "${key}":`, error);
2203
- if (canUseLocalStorage()) {
2204
- try {
2205
- window.localStorage.removeItem(key);
2206
- } catch (removeError) {
2207
- console.error(
2208
- `Error removing corrupted item with key "${key}":`,
2209
- removeError
2210
- );
2211
- }
2212
- }
2213
- return defaultValue;
2214
- }
2215
- },
2216
- async setItem(key, value) {
2217
- if (!canUseLocalStorage()) return;
2218
- try {
2219
- window.localStorage.setItem(key, JSON.stringify(value));
2220
- } catch (error) {
2221
- if (isQuotaExceeded(error)) {
2222
- if (NON_CRITICAL_KEYS.has(key)) {
2223
- console.warn(
2224
- `Storage quota exceeded; skipping non-critical key "${key}".`
2225
- );
2226
- return;
2227
- }
2228
- try {
2229
- window.localStorage.removeItem("modelsFromAllProviders");
2230
- } catch {
2231
- }
2232
- try {
2233
- window.localStorage.setItem(key, JSON.stringify(value));
2234
- return;
2235
- } catch (retryError) {
2236
- console.warn(
2237
- `Storage quota exceeded; unable to persist key "${key}" after cleanup attempt.`,
2238
- retryError
2239
- );
2240
- return;
2241
- }
2242
- }
2243
- console.error(`Error storing item with key "${key}":`, error);
2244
- }
2245
- },
2246
- async removeItem(key) {
2247
- if (!canUseLocalStorage()) return;
2248
- try {
2249
- window.localStorage.removeItem(key);
2250
- } catch (error) {
2251
- console.error(`Error removing item with key "${key}":`, error);
2252
- }
2253
- }
2254
- };
2255
-
2256
- // storage/drivers/memory.ts
2257
- var createMemoryDriver = (seed) => {
2258
- const store = /* @__PURE__ */ new Map();
2259
- return {
2260
- async getItem(key, defaultValue) {
2261
- const item = store.get(key);
2262
- if (item === void 0) return defaultValue;
2263
- try {
2264
- return JSON.parse(item);
2265
- } catch (parseError) {
2266
- if (typeof defaultValue === "string") {
2267
- return item;
2268
- }
2269
- throw parseError;
2270
- }
2271
- },
2272
- async setItem(key, value) {
2273
- store.set(key, JSON.stringify(value));
2274
- },
2275
- async removeItem(key) {
2276
- store.delete(key);
2277
- }
2278
- };
2279
- };
2280
-
2281
- // storage/drivers/sqlite.ts
2282
- var isBun = () => {
2283
- return typeof process.versions.bun !== "undefined";
2284
- };
2285
- var cachedDbModule = null;
2286
- var loadDatabase = async (dbPath) => {
2287
- if (isBun()) {
2288
- throw new Error(
2289
- "SQLite driver not supported in Bun. Use createBunSqliteDriver() instead."
2290
- );
2291
- }
2292
- try {
2293
- if (!cachedDbModule) {
2294
- cachedDbModule = (await import('better-sqlite3')).default;
2295
- }
2296
- return new cachedDbModule(dbPath);
2297
- } catch (error) {
2298
- throw new Error(
2299
- `better-sqlite3 is required for sqlite storage. Install it to use sqlite storage. (${error})`
2300
- );
2301
- }
2302
- };
2303
- var createSqliteDriver = (options = {}) => {
2304
- const dbPath = options.dbPath || "routstr.sqlite";
2305
- const tableName = options.tableName || "sdk_storage";
2306
- let db;
2307
- let selectStmt;
2308
- let upsertStmt;
2309
- let deleteStmt;
2310
- const initDb = async () => {
2311
- if (!db) {
2312
- db = await loadDatabase(dbPath);
2313
- db.exec(
2314
- `CREATE TABLE IF NOT EXISTS ${tableName} (key TEXT PRIMARY KEY, value TEXT NOT NULL)`
2315
- );
2316
- selectStmt = db.prepare(`SELECT value FROM ${tableName} WHERE key = ?`);
2317
- upsertStmt = db.prepare(
2318
- `INSERT INTO ${tableName} (key, value) VALUES (?, ?)
2319
- ON CONFLICT(key) DO UPDATE SET value = excluded.value`
2320
- );
2321
- deleteStmt = db.prepare(`DELETE FROM ${tableName} WHERE key = ?`);
2322
- }
2323
- };
2324
- const ensureInit = async () => {
2325
- if (!db) {
2326
- await initDb();
2327
- }
2328
- };
2329
- return {
2330
- async getItem(key, defaultValue) {
2331
- try {
2332
- await ensureInit();
2333
- const row = selectStmt.get(key);
2334
- if (!row || typeof row.value !== "string") return defaultValue;
2335
- try {
2336
- return JSON.parse(row.value);
2337
- } catch (parseError) {
2338
- if (typeof defaultValue === "string") {
2339
- return row.value;
2340
- }
2341
- throw parseError;
2342
- }
2343
- } catch (error) {
2344
- console.error(`SQLite getItem failed for key "${key}":`, error);
2345
- return defaultValue;
2346
- }
2347
- },
2348
- async setItem(key, value) {
2349
- try {
2350
- await ensureInit();
2351
- upsertStmt.run(key, JSON.stringify(value));
2352
- } catch (error) {
2353
- console.error(`SQLite setItem failed for key "${key}":`, error);
2354
- }
2355
- },
2356
- async removeItem(key) {
2357
- try {
2358
- await ensureInit();
2359
- deleteStmt.run(key);
2360
- } catch (error) {
2361
- console.error(`SQLite removeItem failed for key "${key}":`, error);
2362
- }
2363
- }
2364
- };
2365
- };
2366
-
2367
- // storage/keys.ts
2368
- var SDK_STORAGE_KEYS = {
2369
- MODELS_FROM_ALL_PROVIDERS: "modelsFromAllProviders",
2370
- LAST_USED_MODEL: "lastUsedModel",
2371
- BASE_URLS_LIST: "base_urls_list",
2372
- DISABLED_PROVIDERS: "disabled_providers",
2373
- MINTS_FROM_ALL_PROVIDERS: "mints_from_all_providers",
2374
- INFO_FROM_ALL_PROVIDERS: "info_from_all_providers",
2375
- LAST_MODELS_UPDATE: "lastModelsUpdate",
2376
- LAST_BASE_URLS_UPDATE: "lastBaseUrlsUpdate",
2377
- API_KEYS: "api_keys",
2378
- CHILD_KEYS: "child_keys",
2379
- XCASHU_TOKENS: "xcashu_tokens",
2380
- ROUTSTR21_MODELS: "routstr21Models",
2381
- LAST_ROUTSTR21_MODELS_UPDATE: "lastRoutstr21ModelsUpdate",
2382
- CACHED_RECEIVE_TOKENS: "cached_receive_tokens",
2383
- USAGE_TRACKING: "usage_tracking",
2384
- CLIENT_IDS: "client_ids",
2385
- FAILED_PROVIDERS: "failed_providers",
2386
- LAST_FAILED: "last_failed",
2387
- PROVIDERS_ON_COOLDOWN: "providers_on_cooldown"
2388
- };
2389
-
2390
- // storage/usageTracking/indexedDB.ts
2391
- var DEFAULT_DB_NAME = "routstr-sdk";
2392
- var DEFAULT_STORE_NAME = "usage_tracking";
2393
- var MIGRATION_MARKER_KEY = "usage_tracking_migration_v1";
2394
- var isBrowser = typeof indexedDB !== "undefined";
2395
- var normalizeBaseUrl = (baseUrl) => baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
2396
- var openDatabase = (dbName, storeName) => {
2397
- if (!isBrowser) {
2398
- return Promise.reject(new Error("IndexedDB is not available"));
2399
- }
2400
- return new Promise((resolve, reject) => {
2401
- const request = indexedDB.open(dbName, 1);
2402
- request.onupgradeneeded = () => {
2403
- const db = request.result;
2404
- if (!db.objectStoreNames.contains(storeName)) {
2405
- const store = db.createObjectStore(storeName, { keyPath: "id" });
2406
- store.createIndex("timestamp", "timestamp", { unique: false });
2407
- store.createIndex("modelId", "modelId", { unique: false });
2408
- store.createIndex("baseUrl", "baseUrl", { unique: false });
2409
- store.createIndex("sessionId", "sessionId", { unique: false });
2410
- store.createIndex("client", "client", { unique: false });
2411
- }
2412
- };
2413
- request.onsuccess = () => resolve(request.result);
2414
- request.onerror = () => reject(request.error);
2415
- });
2416
- };
2417
- var matchesFilters = (entry, options = {}) => {
2418
- if (typeof options.before === "number" && entry.timestamp >= options.before) {
2419
- return false;
2420
- }
2421
- if (typeof options.after === "number" && entry.timestamp <= options.after) {
2422
- return false;
2423
- }
2424
- if (options.modelId && entry.modelId !== options.modelId) {
2425
- return false;
2426
- }
2427
- if (options.baseUrl && normalizeBaseUrl(entry.baseUrl) !== normalizeBaseUrl(options.baseUrl)) {
2428
- return false;
2429
- }
2430
- if (options.sessionId && entry.sessionId !== options.sessionId) {
2431
- return false;
2432
- }
2433
- if (options.client && entry.client !== options.client) {
2434
- return false;
2435
- }
2436
- return true;
2437
- };
2438
- var createIndexedDBUsageTrackingDriver = (options = {}) => {
2439
- const dbName = options.dbName || DEFAULT_DB_NAME;
2440
- const storeName = options.storeName || DEFAULT_STORE_NAME;
2441
- const legacyStorageDriver = options.legacyStorageDriver;
2442
- let dbPromise = null;
2443
- let migrationPromise = null;
2444
- const getDb = () => {
2445
- if (!dbPromise) {
2446
- dbPromise = openDatabase(dbName, storeName);
2447
- }
2448
- return dbPromise;
2449
- };
2450
- const putMany = async (entries) => {
2451
- if (entries.length === 0) return;
2452
- const db = await getDb();
2453
- await new Promise((resolve, reject) => {
2454
- const tx = db.transaction(storeName, "readwrite");
2455
- const store = tx.objectStore(storeName);
2456
- for (const entry of entries) {
2457
- store.put({ ...entry, baseUrl: normalizeBaseUrl(entry.baseUrl) });
2458
- }
2459
- tx.oncomplete = () => resolve();
2460
- tx.onerror = () => reject(tx.error);
2461
- });
2462
- };
2463
- const ensureMigrated = async () => {
2464
- if (!legacyStorageDriver) return;
2465
- if (!migrationPromise) {
2466
- migrationPromise = (async () => {
2467
- const migrated = await legacyStorageDriver.getItem(
2468
- MIGRATION_MARKER_KEY,
2469
- false
2470
- );
2471
- if (migrated) return;
2472
- const legacyEntries = await legacyStorageDriver.getItem(
2473
- SDK_STORAGE_KEYS.USAGE_TRACKING,
2474
- []
2475
- );
2476
- if (legacyEntries.length > 0) {
2477
- await putMany(legacyEntries);
2478
- await legacyStorageDriver.removeItem(SDK_STORAGE_KEYS.USAGE_TRACKING);
2479
- }
2480
- await legacyStorageDriver.setItem(MIGRATION_MARKER_KEY, true);
2481
- })();
2482
- }
2483
- await migrationPromise;
2484
- };
2485
- return {
2486
- async migrate() {
2487
- await ensureMigrated();
2488
- },
2489
- async append(entry) {
2490
- await ensureMigrated();
2491
- await putMany([entry]);
2492
- },
2493
- async appendMany(entries) {
2494
- await ensureMigrated();
2495
- await putMany(entries);
2496
- },
2497
- async list(options2 = {}) {
2498
- await ensureMigrated();
2499
- const db = await getDb();
2500
- return new Promise((resolve, reject) => {
2501
- const tx = db.transaction(storeName, "readonly");
2502
- const store = tx.objectStore(storeName);
2503
- const index = store.index("timestamp");
2504
- const direction = "prev";
2505
- const request = index.openCursor(null, direction);
2506
- const results = [];
2507
- const limit = options2.limit;
2508
- request.onsuccess = () => {
2509
- const cursor = request.result;
2510
- if (!cursor) {
2511
- resolve(results);
2512
- return;
2513
- }
2514
- const value = cursor.value;
2515
- if (matchesFilters(value, options2)) {
2516
- results.push(value);
2517
- if (typeof limit === "number" && results.length >= limit) {
2518
- resolve(results);
2519
- return;
2520
- }
2521
- }
2522
- cursor.continue();
2523
- };
2524
- request.onerror = () => reject(request.error);
2525
- });
2526
- },
2527
- async count(options2 = {}) {
2528
- const results = await this.list(options2);
2529
- return results.length;
2530
- },
2531
- async deleteOlderThan(timestamp) {
2532
- await ensureMigrated();
2533
- const db = await getDb();
2534
- return new Promise((resolve, reject) => {
2535
- const tx = db.transaction(storeName, "readwrite");
2536
- const store = tx.objectStore(storeName);
2537
- const index = store.index("timestamp");
2538
- const range = IDBKeyRange.upperBound(timestamp, true);
2539
- const request = index.openCursor(range);
2540
- let deleted = 0;
2541
- request.onsuccess = () => {
2542
- const cursor = request.result;
2543
- if (!cursor) {
2544
- resolve(deleted);
2545
- return;
2546
- }
2547
- deleted += 1;
2548
- cursor.delete();
2549
- cursor.continue();
2550
- };
2551
- request.onerror = () => reject(request.error);
2552
- });
2553
- },
2554
- async clear() {
2555
- await ensureMigrated();
2556
- const db = await getDb();
2557
- await new Promise((resolve, reject) => {
2558
- const tx = db.transaction(storeName, "readwrite");
2559
- tx.objectStore(storeName).clear();
2560
- tx.oncomplete = () => resolve();
2561
- tx.onerror = () => reject(tx.error);
2562
- });
2563
- }
2564
- };
2565
- };
2566
-
2567
- // storage/usageTracking/sqlite.ts
2568
- var MIGRATION_MARKER_KEY2 = "usage_tracking_migration_v1";
2569
- var normalizeBaseUrl2 = (baseUrl) => baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
2570
- var isBun2 = () => {
2571
- return typeof process.versions.bun !== "undefined";
2572
- };
2573
- var cachedDbModule2 = null;
2574
- var loadDatabase2 = async (dbPath) => {
2575
- if (isBun2()) {
2576
- throw new Error(
2577
- "SQLite driver not supported in Bun. Use createMemoryDriver() instead."
2578
- );
2579
- }
2580
- try {
2581
- if (!cachedDbModule2) {
2582
- cachedDbModule2 = (await import('better-sqlite3')).default;
2583
- }
2584
- return new cachedDbModule2(dbPath);
2585
- } catch (error) {
2586
- throw new Error(
2587
- `better-sqlite3 is required for sqlite usage tracking. Install it to use sqlite storage. (${error})`
2588
- );
2589
- }
2590
- };
2591
- var buildWhereClause = (options = {}) => {
2592
- const clauses = [];
2593
- const params = [];
2594
- if (typeof options.before === "number") {
2595
- clauses.push("timestamp < ?");
2596
- params.push(options.before);
2597
- }
2598
- if (typeof options.after === "number") {
2599
- clauses.push("timestamp > ?");
2600
- params.push(options.after);
2601
- }
2602
- if (options.modelId) {
2603
- clauses.push("model_id = ?");
2604
- params.push(options.modelId);
2605
- }
2606
- if (options.baseUrl) {
2607
- clauses.push("base_url = ?");
2608
- params.push(normalizeBaseUrl2(options.baseUrl));
2609
- }
2610
- if (options.sessionId) {
2611
- clauses.push("session_id = ?");
2612
- params.push(options.sessionId);
2613
- }
2614
- if (options.client) {
2615
- clauses.push("client = ?");
2616
- params.push(options.client);
2617
- }
2618
- return {
2619
- sql: clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : "",
2620
- params
2621
- };
2622
- };
2623
- var createSqliteUsageTrackingDriver = (options = {}) => {
2624
- const dbPath = options.dbPath || "routstr.sqlite";
2625
- const tableName = options.tableName || "usage_tracking";
2626
- const legacyStorageDriver = options.legacyStorageDriver;
2627
- let db;
2628
- let insertStmt;
2629
- let migrationComplete = false;
2630
- const initDb = async () => {
2631
- if (!db) {
2632
- db = await loadDatabase2(dbPath);
2633
- db.exec(`
2634
- CREATE TABLE IF NOT EXISTS ${tableName} (
2635
- id TEXT PRIMARY KEY,
2636
- timestamp INTEGER NOT NULL,
2637
- model_id TEXT NOT NULL,
2638
- base_url TEXT NOT NULL,
2639
- request_id TEXT NOT NULL,
2640
- cost REAL NOT NULL,
2641
- sats_cost REAL NOT NULL,
2642
- prompt_tokens INTEGER NOT NULL,
2643
- completion_tokens INTEGER NOT NULL,
2644
- total_tokens INTEGER NOT NULL,
2645
- client TEXT,
2646
- session_id TEXT,
2647
- tags TEXT
2648
- );
2649
- CREATE INDEX IF NOT EXISTS idx_${tableName}_timestamp ON ${tableName}(timestamp);
2650
- CREATE INDEX IF NOT EXISTS idx_${tableName}_model_id ON ${tableName}(model_id);
2651
- CREATE INDEX IF NOT EXISTS idx_${tableName}_base_url ON ${tableName}(base_url);
2652
- CREATE INDEX IF NOT EXISTS idx_${tableName}_session_id ON ${tableName}(session_id);
2653
- CREATE INDEX IF NOT EXISTS idx_${tableName}_client ON ${tableName}(client);
2654
- `);
2655
- insertStmt = db.prepare(`
2656
- INSERT OR REPLACE INTO ${tableName} (
2657
- id, timestamp, model_id, base_url, request_id,
2658
- cost, sats_cost, prompt_tokens, completion_tokens, total_tokens,
2659
- client, session_id, tags
2660
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2661
- `);
2662
- }
2663
- };
2664
- const ensureInit = async () => {
2665
- if (!db) {
2666
- await initDb();
2667
- }
2668
- };
2669
- const appendOne = (entry) => {
2670
- insertStmt.run(
2671
- entry.id,
2672
- entry.timestamp,
2673
- entry.modelId,
2674
- normalizeBaseUrl2(entry.baseUrl),
2675
- entry.requestId,
2676
- entry.cost,
2677
- entry.satsCost,
2678
- entry.promptTokens,
2679
- entry.completionTokens,
2680
- entry.totalTokens,
2681
- entry.client ?? null,
2682
- entry.sessionId ?? null,
2683
- JSON.stringify(entry.tags ?? [])
2684
- );
2685
- };
2686
- const ensureMigrated = async () => {
2687
- if (!legacyStorageDriver || migrationComplete) return;
2688
- const migrated = await legacyStorageDriver.getItem(
2689
- MIGRATION_MARKER_KEY2,
2690
- false
2691
- );
2692
- if (migrated) {
2693
- migrationComplete = true;
2694
- return;
2695
- }
2696
- const legacyEntries = await legacyStorageDriver.getItem(
2697
- SDK_STORAGE_KEYS.USAGE_TRACKING,
2698
- []
2699
- );
2700
- for (const entry of legacyEntries) {
2701
- appendOne(entry);
2702
- }
2703
- if (legacyEntries.length > 0) {
2704
- await legacyStorageDriver.removeItem(SDK_STORAGE_KEYS.USAGE_TRACKING);
2705
- }
2706
- await legacyStorageDriver.setItem(MIGRATION_MARKER_KEY2, true);
2707
- migrationComplete = true;
2708
- };
2709
- const mapRow = (row) => ({
2710
- id: row.id,
2711
- timestamp: row.timestamp,
2712
- modelId: row.model_id,
2713
- baseUrl: row.base_url,
2714
- requestId: row.request_id,
2715
- cost: row.cost,
2716
- satsCost: row.sats_cost,
2717
- promptTokens: row.prompt_tokens,
2718
- completionTokens: row.completion_tokens,
2719
- totalTokens: row.total_tokens,
2720
- client: row.client ?? void 0,
2721
- sessionId: row.session_id ?? void 0,
2722
- tags: typeof row.tags === "string" ? JSON.parse(row.tags) : void 0
2723
- });
2724
- return {
2725
- async migrate() {
2726
- await ensureInit();
2727
- await ensureMigrated();
2728
- },
2729
- async append(entry) {
2730
- await ensureInit();
2731
- await ensureMigrated();
2732
- appendOne(entry);
2733
- },
2734
- async appendMany(entries) {
2735
- await ensureInit();
2736
- await ensureMigrated();
2737
- for (const entry of entries) {
2738
- appendOne(entry);
2739
- }
2740
- },
2741
- async list(options2 = {}) {
2742
- await ensureInit();
2743
- await ensureMigrated();
2744
- const { sql, params } = buildWhereClause(options2);
2745
- const limitSql = typeof options2.limit === "number" ? " LIMIT ?" : "";
2746
- const stmt = db.prepare(
2747
- `SELECT * FROM ${tableName} ${sql} ORDER BY timestamp DESC${limitSql}`
2748
- );
2749
- const rows = stmt.all(
2750
- ...typeof options2.limit === "number" ? [...params, options2.limit] : params
2751
- );
2752
- return rows.map(mapRow);
2753
- },
2754
- async count(options2 = {}) {
2755
- await ensureInit();
2756
- await ensureMigrated();
2757
- const { sql, params } = buildWhereClause(options2);
2758
- const stmt = db.prepare(`SELECT COUNT(*) as count FROM ${tableName} ${sql}`);
2759
- const row = stmt.get(...params);
2760
- return Number(row?.count ?? 0);
2761
- },
2762
- async deleteOlderThan(timestamp) {
2763
- await ensureInit();
2764
- await ensureMigrated();
2765
- const stmt = db.prepare(`DELETE FROM ${tableName} WHERE timestamp < ?`);
2766
- const result = stmt.run(timestamp);
2767
- return result.changes;
2768
- },
2769
- async clear() {
2770
- await ensureInit();
2771
- await ensureMigrated();
2772
- db.prepare(`DELETE FROM ${tableName}`).run();
2773
- }
2774
- };
2775
- };
2776
-
2777
- // storage/usageTracking/bunSqlite.ts
2778
- var MIGRATION_MARKER_KEY3 = "usage_tracking_migration_v1";
2779
- var normalizeBaseUrl3 = (baseUrl) => baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
2780
- var buildWhereClause2 = (options = {}) => {
2781
- const clauses = [];
2782
- const params = [];
2783
- if (typeof options.before === "number") {
2784
- clauses.push("timestamp < ?");
2785
- params.push(options.before);
2786
- }
2787
- if (typeof options.after === "number") {
2788
- clauses.push("timestamp > ?");
2789
- params.push(options.after);
2790
- }
2791
- if (options.modelId) {
2792
- clauses.push("model_id = ?");
2793
- params.push(options.modelId);
2794
- }
2795
- if (options.baseUrl) {
2796
- clauses.push("base_url = ?");
2797
- params.push(normalizeBaseUrl3(options.baseUrl));
2798
- }
2799
- if (options.sessionId) {
2800
- clauses.push("session_id = ?");
2801
- params.push(options.sessionId);
2802
- }
2803
- if (options.client) {
2804
- clauses.push("client = ?");
2805
- params.push(options.client);
2806
- }
2807
- return {
2808
- sql: clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : "",
2809
- params
2810
- };
2811
- };
2812
- var createBunSqliteUsageTrackingDriver = (options = {}) => {
2813
- const dbPath = options.dbPath || "routstr.sqlite";
2814
- const tableName = options.tableName || "usage_tracking";
2815
- const legacyStorageDriver = options.legacyStorageDriver;
2816
- const SQLiteDatabase = options.sqlite?.Database;
2817
- let migrationPromise = null;
2818
- if (!SQLiteDatabase) {
2819
- throw new Error(
2820
- "Bun SQLite Database constructor is required. Pass { sqlite: { Database } } when creating the driver."
2821
- );
2822
- }
2823
- const db = new SQLiteDatabase(dbPath);
2824
- db.run(`
2825
- CREATE TABLE IF NOT EXISTS ${tableName} (
2826
- id TEXT PRIMARY KEY,
2827
- timestamp INTEGER NOT NULL,
2828
- model_id TEXT NOT NULL,
2829
- base_url TEXT NOT NULL,
2830
- request_id TEXT NOT NULL,
2831
- cost REAL NOT NULL,
2832
- sats_cost REAL NOT NULL,
2833
- prompt_tokens INTEGER NOT NULL,
2834
- completion_tokens INTEGER NOT NULL,
2835
- total_tokens INTEGER NOT NULL,
2836
- client TEXT,
2837
- session_id TEXT,
2838
- tags TEXT
2839
- )
2840
- `);
2841
- db.run(`CREATE INDEX IF NOT EXISTS idx_${tableName}_timestamp ON ${tableName}(timestamp)`);
2842
- db.run(`CREATE INDEX IF NOT EXISTS idx_${tableName}_model_id ON ${tableName}(model_id)`);
2843
- db.run(`CREATE INDEX IF NOT EXISTS idx_${tableName}_base_url ON ${tableName}(base_url)`);
2844
- const appendOne = (entry) => {
2845
- db.query(`
2846
- INSERT OR REPLACE INTO ${tableName} (
2847
- id, timestamp, model_id, base_url, request_id,
2848
- cost, sats_cost, prompt_tokens, completion_tokens, total_tokens,
2849
- client, session_id, tags
2850
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2851
- `).run(
2852
- entry.id,
2853
- entry.timestamp,
2854
- entry.modelId,
2855
- normalizeBaseUrl3(entry.baseUrl),
2856
- entry.requestId,
2857
- entry.cost,
2858
- entry.satsCost,
2859
- entry.promptTokens,
2860
- entry.completionTokens,
2861
- entry.totalTokens,
2862
- entry.client ?? null,
2863
- entry.sessionId ?? null,
2864
- JSON.stringify(entry.tags ?? [])
2865
- );
2866
- };
2867
- const mapRow = (row) => ({
2868
- id: row.id,
2869
- timestamp: row.timestamp,
2870
- modelId: row.model_id,
2871
- baseUrl: row.base_url,
2872
- requestId: row.request_id,
2873
- cost: row.cost,
2874
- satsCost: row.sats_cost,
2875
- promptTokens: row.prompt_tokens,
2876
- completionTokens: row.completion_tokens,
2877
- totalTokens: row.total_tokens,
2878
- client: row.client ?? void 0,
2879
- sessionId: row.session_id ?? void 0,
2880
- tags: typeof row.tags === "string" ? JSON.parse(row.tags) : void 0
2881
- });
2882
- const ensureMigrated = async () => {
2883
- if (!legacyStorageDriver) return;
2884
- if (!migrationPromise) {
2885
- migrationPromise = (async () => {
2886
- const migrated = await legacyStorageDriver.getItem(
2887
- MIGRATION_MARKER_KEY3,
2888
- false
2889
- );
2890
- if (migrated) return;
2891
- const legacyEntries = await legacyStorageDriver.getItem(
2892
- SDK_STORAGE_KEYS.USAGE_TRACKING,
2893
- []
2894
- );
2895
- if (legacyEntries.length > 0) {
2896
- for (const entry of legacyEntries) {
2897
- appendOne(entry);
2898
- }
2899
- await legacyStorageDriver.removeItem(SDK_STORAGE_KEYS.USAGE_TRACKING);
2900
- }
2901
- await legacyStorageDriver.setItem(MIGRATION_MARKER_KEY3, true);
2902
- })();
2903
- }
2904
- await migrationPromise;
2905
- };
2906
- return {
2907
- async migrate() {
2908
- await ensureMigrated();
2909
- },
2910
- async append(entry) {
2911
- await ensureMigrated();
2912
- appendOne(entry);
2913
- },
2914
- async appendMany(entries) {
2915
- await ensureMigrated();
2916
- for (const entry of entries) {
2917
- appendOne(entry);
2918
- }
2919
- },
2920
- async list(options2 = {}) {
2921
- await ensureMigrated();
2922
- const { sql, params } = buildWhereClause2(options2);
2923
- const limitSql = typeof options2.limit === "number" ? " LIMIT ?" : "";
2924
- const query = `SELECT * FROM ${tableName} ${sql} ORDER BY timestamp DESC${limitSql}`;
2925
- let rows;
2926
- if (typeof options2.limit === "number") {
2927
- rows = db.query(query).all(...params, options2.limit);
2928
- } else {
2929
- rows = db.query(query).all(...params);
2930
- }
2931
- return rows.map(mapRow);
2932
- },
2933
- async count(options2 = {}) {
2934
- const { sql, params } = buildWhereClause2(options2);
2935
- const query = `SELECT COUNT(*) as count FROM ${tableName} ${sql}`;
2936
- const row = db.query(query).get(...params);
2937
- return Number(row?.count ?? 0);
2938
- },
2939
- async deleteOlderThan(timestamp) {
2940
- await ensureMigrated();
2941
- const before = timestamp;
2942
- const result = db.query(`DELETE FROM ${tableName} WHERE timestamp < ?`).run(before);
2943
- return result.changes ?? 0;
2944
- },
2945
- async clear() {
2946
- await ensureMigrated();
2947
- db.query(`DELETE FROM ${tableName}`).run();
2948
- }
2949
- };
2950
- };
2951
-
2952
- // storage/usageTracking/memory.ts
2953
- var normalizeBaseUrl4 = (baseUrl) => baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
2954
- var matchesFilters2 = (entry, options = {}) => {
2955
- if (typeof options.before === "number" && entry.timestamp >= options.before) {
2956
- return false;
2957
- }
2958
- if (typeof options.after === "number" && entry.timestamp <= options.after) {
2959
- return false;
2960
- }
2961
- if (options.modelId && entry.modelId !== options.modelId) {
2962
- return false;
2963
- }
2964
- if (options.baseUrl && normalizeBaseUrl4(entry.baseUrl) !== normalizeBaseUrl4(options.baseUrl)) {
2965
- return false;
2966
- }
2967
- if (options.sessionId && entry.sessionId !== options.sessionId) {
2968
- return false;
2969
- }
2970
- if (options.client && entry.client !== options.client) {
2971
- return false;
2972
- }
2973
- return true;
2974
- };
2975
- var createMemoryUsageTrackingDriver = (seed = []) => {
2976
- const store = /* @__PURE__ */ new Map();
2977
- for (const entry of seed) {
2978
- store.set(entry.id, { ...entry, baseUrl: normalizeBaseUrl4(entry.baseUrl) });
2979
- }
2980
- return {
2981
- async migrate() {
2982
- return;
2983
- },
2984
- async append(entry) {
2985
- store.set(entry.id, { ...entry, baseUrl: normalizeBaseUrl4(entry.baseUrl) });
2986
- },
2987
- async appendMany(entries) {
2988
- for (const entry of entries) {
2989
- store.set(entry.id, { ...entry, baseUrl: normalizeBaseUrl4(entry.baseUrl) });
2990
- }
2991
- },
2992
- async list(options = {}) {
2993
- const entries = [...store.values()].filter((entry) => matchesFilters2(entry, options)).sort((a, b) => b.timestamp - a.timestamp);
2994
- if (typeof options.limit === "number") {
2995
- return entries.slice(0, options.limit);
2996
- }
2997
- return entries;
2998
- },
2999
- async count(options = {}) {
3000
- return (await this.list(options)).length;
3001
- },
3002
- async deleteOlderThan(timestamp) {
3003
- let deleted = 0;
3004
- for (const [id, entry] of store.entries()) {
3005
- if (entry.timestamp < timestamp) {
3006
- store.delete(id);
3007
- deleted += 1;
3008
- }
3009
- }
3010
- return deleted;
3011
- },
3012
- async clear() {
3013
- store.clear();
3014
- }
3015
- };
3016
- };
3017
- var normalizeBaseUrl5 = (baseUrl) => baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
3018
- var createEmptyStore = (driver) => vanilla.createStore((set, get) => ({
3019
- modelsFromAllProviders: {},
3020
- lastUsedModel: null,
3021
- baseUrlsList: [],
3022
- lastBaseUrlsUpdate: null,
3023
- disabledProviders: [],
3024
- mintsFromAllProviders: {},
3025
- infoFromAllProviders: {},
3026
- lastModelsUpdate: {},
3027
- apiKeys: [],
3028
- childKeys: [],
3029
- xcashuTokens: {},
3030
- routstr21Models: [],
3031
- lastRoutstr21ModelsUpdate: null,
3032
- cachedReceiveTokens: [],
3033
- clientIds: [],
3034
- failedProviders: [],
3035
- lastFailed: {},
3036
- providersOnCooldown: [],
3037
- setModelsFromAllProviders: (value) => {
3038
- const normalized = {};
3039
- for (const [baseUrl, models] of Object.entries(value)) {
3040
- normalized[normalizeBaseUrl5(baseUrl)] = models;
3041
- }
3042
- void driver.setItem(
3043
- SDK_STORAGE_KEYS.MODELS_FROM_ALL_PROVIDERS,
3044
- normalized
3045
- );
3046
- set({ modelsFromAllProviders: normalized });
3047
- },
3048
- setLastUsedModel: (value) => {
3049
- void driver.setItem(SDK_STORAGE_KEYS.LAST_USED_MODEL, value);
3050
- set({ lastUsedModel: value });
3051
- },
3052
- setBaseUrlsList: (value) => {
3053
- const normalized = value.map((url) => normalizeBaseUrl5(url));
3054
- void driver.setItem(SDK_STORAGE_KEYS.BASE_URLS_LIST, normalized);
3055
- set({ baseUrlsList: normalized });
3056
- },
3057
- setBaseUrlsLastUpdate: (value) => {
3058
- void driver.setItem(SDK_STORAGE_KEYS.LAST_BASE_URLS_UPDATE, value);
3059
- set({ lastBaseUrlsUpdate: value });
3060
- },
3061
- setDisabledProviders: (value) => {
3062
- const normalized = value.map((url) => normalizeBaseUrl5(url));
3063
- void driver.setItem(SDK_STORAGE_KEYS.DISABLED_PROVIDERS, normalized);
3064
- set({ disabledProviders: normalized });
3065
- },
3066
- setMintsFromAllProviders: (value) => {
3067
- const normalized = {};
3068
- for (const [baseUrl, mints] of Object.entries(value)) {
3069
- normalized[normalizeBaseUrl5(baseUrl)] = mints.map(
3070
- (mint) => mint.endsWith("/") ? mint.slice(0, -1) : mint
3071
- );
3072
- }
3073
- void driver.setItem(
3074
- SDK_STORAGE_KEYS.MINTS_FROM_ALL_PROVIDERS,
3075
- normalized
3076
- );
3077
- set({ mintsFromAllProviders: normalized });
3078
- },
3079
- setInfoFromAllProviders: (value) => {
3080
- const normalized = {};
3081
- for (const [baseUrl, info] of Object.entries(value)) {
3082
- normalized[normalizeBaseUrl5(baseUrl)] = info;
3083
- }
3084
- void driver.setItem(SDK_STORAGE_KEYS.INFO_FROM_ALL_PROVIDERS, normalized);
3085
- set({ infoFromAllProviders: normalized });
3086
- },
3087
- setLastModelsUpdate: (value) => {
3088
- const normalized = {};
3089
- for (const [baseUrl, timestamp] of Object.entries(value)) {
3090
- normalized[normalizeBaseUrl5(baseUrl)] = timestamp;
3091
- }
3092
- void driver.setItem(SDK_STORAGE_KEYS.LAST_MODELS_UPDATE, normalized);
3093
- set({ lastModelsUpdate: normalized });
3094
- },
3095
- setApiKeys: (value) => {
3096
- set((state) => {
3097
- const updates = typeof value === "function" ? value(state.apiKeys) : value;
3098
- const normalized = updates.map((entry) => ({
3099
- ...entry,
3100
- baseUrl: normalizeBaseUrl5(entry.baseUrl),
3101
- balance: entry.balance ?? 0,
3102
- lastUsed: entry.lastUsed ?? null
3103
- }));
3104
- void driver.setItem(SDK_STORAGE_KEYS.API_KEYS, normalized);
3105
- return { apiKeys: normalized };
3106
- });
3107
- },
3108
- setChildKeys: (value) => {
3109
- set((state) => {
3110
- const updates = typeof value === "function" ? value(state.childKeys) : value;
3111
- const normalized = updates.map((entry) => ({
3112
- parentBaseUrl: normalizeBaseUrl5(entry.parentBaseUrl),
3113
- childKey: entry.childKey,
3114
- balance: entry.balance ?? 0,
3115
- balanceLimit: entry.balanceLimit,
3116
- validityDate: entry.validityDate,
3117
- createdAt: entry.createdAt ?? Date.now()
3118
- }));
3119
- void driver.setItem(SDK_STORAGE_KEYS.CHILD_KEYS, normalized);
3120
- return { childKeys: normalized };
3121
- });
3122
- },
3123
- setXcashuTokens: (value) => {
3124
- const normalized = {};
3125
- for (const [baseUrl, tokens] of Object.entries(value)) {
3126
- normalized[normalizeBaseUrl5(baseUrl)] = tokens.map((entry) => ({
3127
- ...entry,
3128
- baseUrl: normalizeBaseUrl5(entry.baseUrl),
3129
- createdAt: entry.createdAt ?? Date.now(),
3130
- tryCount: entry.tryCount ?? 0
3131
- }));
3132
- }
3133
- void driver.setItem(SDK_STORAGE_KEYS.XCASHU_TOKENS, normalized);
3134
- set({ xcashuTokens: normalized });
3135
- },
3136
- updateXcashuTokenTryCount: (token, tryCount) => {
3137
- const currentTokens = get().xcashuTokens;
3138
- const updatedTokens = {};
3139
- for (const [baseUrl, tokens] of Object.entries(currentTokens)) {
3140
- updatedTokens[baseUrl] = tokens.map(
3141
- (entry) => entry.token === token ? { ...entry, tryCount } : entry
3142
- );
3143
- }
3144
- void driver.setItem(SDK_STORAGE_KEYS.XCASHU_TOKENS, updatedTokens);
3145
- set({ xcashuTokens: updatedTokens });
3146
- },
3147
- setRoutstr21Models: (value) => {
3148
- void driver.setItem(SDK_STORAGE_KEYS.ROUTSTR21_MODELS, value);
3149
- set({ routstr21Models: value });
3150
- },
3151
- setRoutstr21ModelsLastUpdate: (value) => {
3152
- void driver.setItem(SDK_STORAGE_KEYS.LAST_ROUTSTR21_MODELS_UPDATE, value);
3153
- set({ lastRoutstr21ModelsUpdate: value });
3154
- },
3155
- setCachedReceiveTokens: (value) => {
3156
- const normalized = value.map((entry) => ({
3157
- token: entry.token,
3158
- amount: entry.amount,
3159
- unit: entry.unit || "sat",
3160
- createdAt: entry.createdAt ?? Date.now()
3161
- }));
3162
- void driver.setItem(SDK_STORAGE_KEYS.CACHED_RECEIVE_TOKENS, normalized);
3163
- set({ cachedReceiveTokens: normalized });
3164
- },
3165
- setClientIds: (value) => {
3166
- set((state) => {
3167
- const updates = typeof value === "function" ? value(state.clientIds) : value;
3168
- const normalized = updates.map((entry) => ({
3169
- ...entry,
3170
- createdAt: entry.createdAt ?? Date.now(),
3171
- lastUsed: entry.lastUsed ?? null
3172
- }));
3173
- void driver.setItem(SDK_STORAGE_KEYS.CLIENT_IDS, normalized);
3174
- return { clientIds: normalized };
3175
- });
3176
- },
3177
- // ========== Failure Tracking ==========
3178
- setFailedProviders: (value) => {
3179
- const normalized = value.map((url) => normalizeBaseUrl5(url));
3180
- void driver.setItem(SDK_STORAGE_KEYS.FAILED_PROVIDERS, normalized);
3181
- set({ failedProviders: normalized });
3182
- },
3183
- addFailedProvider: (baseUrl) => {
3184
- const normalized = normalizeBaseUrl5(baseUrl);
3185
- const current = get().failedProviders;
3186
- if (!current.includes(normalized)) {
3187
- const updated = [...current, normalized];
3188
- void driver.setItem(SDK_STORAGE_KEYS.FAILED_PROVIDERS, updated);
3189
- set({ failedProviders: updated });
3190
- }
3191
- },
3192
- removeFailedProvider: (baseUrl) => {
3193
- const normalized = normalizeBaseUrl5(baseUrl);
3194
- const current = get().failedProviders;
3195
- const updated = current.filter((url) => url !== normalized);
3196
- void driver.setItem(SDK_STORAGE_KEYS.FAILED_PROVIDERS, updated);
3197
- set({ failedProviders: updated });
3198
- },
3199
- setLastFailed: (value) => {
3200
- const normalized = {};
3201
- for (const [baseUrl, timestamp] of Object.entries(value)) {
3202
- normalized[normalizeBaseUrl5(baseUrl)] = timestamp;
3203
- }
3204
- void driver.setItem(SDK_STORAGE_KEYS.LAST_FAILED, normalized);
3205
- set({ lastFailed: normalized });
3206
- },
3207
- setLastFailedTimestamp: (baseUrl, timestamp) => {
3208
- const normalized = normalizeBaseUrl5(baseUrl);
3209
- const current = get().lastFailed;
3210
- const updated = { ...current, [normalized]: timestamp };
3211
- void driver.setItem(SDK_STORAGE_KEYS.LAST_FAILED, updated);
3212
- set({ lastFailed: updated });
3213
- },
3214
- setProvidersOnCooldown: (value) => {
3215
- const normalized = value.map((entry) => ({
3216
- baseUrl: normalizeBaseUrl5(entry.baseUrl),
3217
- timestamp: entry.timestamp
3218
- }));
3219
- void driver.setItem(SDK_STORAGE_KEYS.PROVIDERS_ON_COOLDOWN, normalized);
3220
- set({ providersOnCooldown: normalized });
3221
- },
3222
- addProviderOnCooldown: (baseUrl, timestamp) => {
3223
- const normalized = normalizeBaseUrl5(baseUrl);
3224
- const current = get().providersOnCooldown;
3225
- if (!current.some((entry) => entry.baseUrl === normalized)) {
3226
- const updated = [...current, { baseUrl: normalized, timestamp }];
3227
- void driver.setItem(SDK_STORAGE_KEYS.PROVIDERS_ON_COOLDOWN, updated);
3228
- set({ providersOnCooldown: updated });
3229
- }
3230
- },
3231
- removeProviderFromCooldown: (baseUrl) => {
3232
- const normalized = normalizeBaseUrl5(baseUrl);
3233
- const current = get().providersOnCooldown;
3234
- const updated = current.filter((entry) => entry.baseUrl !== normalized);
3235
- void driver.setItem(SDK_STORAGE_KEYS.PROVIDERS_ON_COOLDOWN, updated);
3236
- set({ providersOnCooldown: updated });
3237
- },
3238
- clearProvidersOnCooldown: () => {
3239
- void driver.setItem(SDK_STORAGE_KEYS.PROVIDERS_ON_COOLDOWN, []);
3240
- set({ providersOnCooldown: [] });
3241
- }
3242
- }));
3243
- var hydrateStoreFromDriver = async (store, driver) => {
3244
- const [
3245
- rawModels,
3246
- lastUsedModel,
3247
- rawBaseUrls,
3248
- lastBaseUrlsUpdate,
3249
- rawDisabledProviders,
3250
- rawMints,
3251
- rawInfo,
3252
- rawLastModelsUpdate,
3253
- rawApiKeys,
3254
- rawChildKeys,
3255
- rawXcashuTokens,
3256
- rawRoutstr21Models,
3257
- rawLastRoutstr21ModelsUpdate,
3258
- rawCachedReceiveTokens,
3259
- rawClientIds,
3260
- rawFailedProviders,
3261
- rawLastFailed,
3262
- rawProvidersOnCooldown
3263
- ] = await Promise.all([
3264
- driver.getItem(
3265
- SDK_STORAGE_KEYS.MODELS_FROM_ALL_PROVIDERS,
3266
- {}
3267
- ),
3268
- driver.getItem(SDK_STORAGE_KEYS.LAST_USED_MODEL, null),
3269
- driver.getItem(SDK_STORAGE_KEYS.BASE_URLS_LIST, []),
3270
- driver.getItem(SDK_STORAGE_KEYS.LAST_BASE_URLS_UPDATE, null),
3271
- driver.getItem(SDK_STORAGE_KEYS.DISABLED_PROVIDERS, []),
3272
- driver.getItem(
3273
- SDK_STORAGE_KEYS.MINTS_FROM_ALL_PROVIDERS,
3274
- {}
3275
- ),
3276
- driver.getItem(
3277
- SDK_STORAGE_KEYS.INFO_FROM_ALL_PROVIDERS,
3278
- {}
3279
- ),
3280
- driver.getItem(
3281
- SDK_STORAGE_KEYS.LAST_MODELS_UPDATE,
3282
- {}
3283
- ),
3284
- driver.getItem(SDK_STORAGE_KEYS.API_KEYS, []),
3285
- driver.getItem(SDK_STORAGE_KEYS.CHILD_KEYS, []),
3286
- driver.getItem(SDK_STORAGE_KEYS.XCASHU_TOKENS, {}),
3287
- driver.getItem(SDK_STORAGE_KEYS.ROUTSTR21_MODELS, []),
3288
- driver.getItem(
3289
- SDK_STORAGE_KEYS.LAST_ROUTSTR21_MODELS_UPDATE,
3290
- null
3291
- ),
3292
- driver.getItem(SDK_STORAGE_KEYS.CACHED_RECEIVE_TOKENS, []),
3293
- driver.getItem(SDK_STORAGE_KEYS.CLIENT_IDS, []),
3294
- driver.getItem(SDK_STORAGE_KEYS.FAILED_PROVIDERS, []),
3295
- driver.getItem(SDK_STORAGE_KEYS.LAST_FAILED, {}),
3296
- driver.getItem(
3297
- SDK_STORAGE_KEYS.PROVIDERS_ON_COOLDOWN,
3298
- []
3299
- )
3300
- ]);
3301
- const modelsFromAllProviders = Object.fromEntries(
3302
- Object.entries(rawModels).map(([baseUrl, models]) => [
3303
- normalizeBaseUrl5(baseUrl),
3304
- models
3305
- ])
3306
- );
3307
- const baseUrlsList = rawBaseUrls.map((url) => normalizeBaseUrl5(url));
3308
- const disabledProviders = rawDisabledProviders.map(
3309
- (url) => normalizeBaseUrl5(url)
3310
- );
3311
- const mintsFromAllProviders = Object.fromEntries(
3312
- Object.entries(rawMints).map(([baseUrl, mints]) => [
3313
- normalizeBaseUrl5(baseUrl),
3314
- mints.map((mint) => mint.endsWith("/") ? mint.slice(0, -1) : mint)
3315
- ])
3316
- );
3317
- const infoFromAllProviders = Object.fromEntries(
3318
- Object.entries(rawInfo).map(([baseUrl, info]) => [
3319
- normalizeBaseUrl5(baseUrl),
3320
- info
3321
- ])
3322
- );
3323
- const lastModelsUpdate = Object.fromEntries(
3324
- Object.entries(rawLastModelsUpdate).map(([baseUrl, timestamp]) => [
3325
- normalizeBaseUrl5(baseUrl),
3326
- timestamp
3327
- ])
3328
- );
3329
- const apiKeys = rawApiKeys.map((entry) => ({
3330
- ...entry,
3331
- baseUrl: normalizeBaseUrl5(entry.baseUrl),
3332
- balance: entry.balance ?? 0,
3333
- lastUsed: entry.lastUsed ?? null
3334
- }));
3335
- const childKeys = rawChildKeys.map((entry) => ({
3336
- parentBaseUrl: normalizeBaseUrl5(entry.parentBaseUrl),
3337
- childKey: entry.childKey,
3338
- balance: entry.balance ?? 0,
3339
- balanceLimit: entry.balanceLimit,
3340
- validityDate: entry.validityDate,
3341
- createdAt: entry.createdAt ?? Date.now()
3342
- }));
3343
- const xcashuTokens = Object.fromEntries(
3344
- Object.entries(rawXcashuTokens).map(([baseUrl, tokens]) => [
3345
- normalizeBaseUrl5(baseUrl),
3346
- tokens.map((entry) => ({
3347
- baseUrl: normalizeBaseUrl5(entry.baseUrl),
3348
- token: entry.token,
3349
- createdAt: entry.createdAt ?? Date.now(),
3350
- tryCount: entry.tryCount ?? 0
3351
- }))
3352
- ])
3353
- );
3354
- const routstr21Models = rawRoutstr21Models;
3355
- const lastRoutstr21ModelsUpdate = rawLastRoutstr21ModelsUpdate;
3356
- const cachedReceiveTokens = rawCachedReceiveTokens?.map((entry) => ({
3357
- token: entry.token,
3358
- amount: entry.amount,
3359
- unit: entry.unit || "sat",
3360
- createdAt: entry.createdAt ?? Date.now()
3361
- }));
3362
- const clientIds = rawClientIds.map((entry) => ({
3363
- ...entry,
3364
- createdAt: entry.createdAt ?? Date.now(),
3365
- lastUsed: entry.lastUsed ?? null
3366
- }));
3367
- const failedProviders = rawFailedProviders.map(
3368
- (url) => normalizeBaseUrl5(url)
3369
- );
3370
- const lastFailed = Object.fromEntries(
3371
- Object.entries(rawLastFailed).map(([baseUrl, timestamp]) => [
3372
- normalizeBaseUrl5(baseUrl),
3373
- timestamp
3374
- ])
3375
- );
3376
- const providersOnCooldown = rawProvidersOnCooldown.map((entry) => ({
3377
- baseUrl: normalizeBaseUrl5(entry.baseUrl),
3378
- timestamp: entry.timestamp
3379
- }));
3380
- store.setState({
3381
- modelsFromAllProviders,
3382
- lastUsedModel,
3383
- baseUrlsList,
3384
- lastBaseUrlsUpdate,
3385
- disabledProviders,
3386
- mintsFromAllProviders,
3387
- infoFromAllProviders,
3388
- lastModelsUpdate,
3389
- apiKeys,
3390
- childKeys,
3391
- xcashuTokens,
3392
- routstr21Models,
3393
- lastRoutstr21ModelsUpdate,
3394
- cachedReceiveTokens,
3395
- clientIds,
3396
- failedProviders,
3397
- lastFailed,
3398
- providersOnCooldown
3399
- });
3400
- };
3401
- var createSdkStore = ({
3402
- driver
3403
- }) => {
3404
- const store = createEmptyStore(driver);
3405
- return {
3406
- store,
3407
- hydrate: hydrateStoreFromDriver(store, driver)
3408
- };
3409
- };
3410
-
3411
- // storage/index.ts
3412
- var isBrowser2 = () => {
3413
- try {
3414
- return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
3415
- } catch {
3416
- return false;
3417
- }
3418
- };
3419
- var isNode = () => {
3420
- try {
3421
- return typeof process !== "undefined" && process.versions != null && process.versions.node != null;
3422
- } catch {
3423
- return false;
3424
- }
3425
- };
3426
- var defaultDriver = null;
3427
- var isBun3 = () => {
3428
- return typeof process.versions.bun !== "undefined";
3429
- };
3430
- var getDefaultSdkDriver = () => {
3431
- if (defaultDriver) return defaultDriver;
3432
- if (isBrowser2()) {
3433
- defaultDriver = localStorageDriver;
3434
- return defaultDriver;
3435
- }
3436
- if (isBun3()) {
3437
- defaultDriver = createMemoryDriver();
3438
- return defaultDriver;
3439
- }
3440
- if (isNode()) {
3441
- defaultDriver = createSqliteDriver();
3442
- return defaultDriver;
3443
- }
3444
- defaultDriver = createMemoryDriver();
3445
- return defaultDriver;
3446
- };
3447
- var defaultStore = null;
3448
- var defaultUsageTrackingDriver = null;
3449
- var getDefaultSdkStore = () => {
3450
- if (!defaultStore) {
3451
- defaultStore = createSdkStore({ driver: getDefaultSdkDriver() });
3452
- }
3453
- return defaultStore.hydrate.then(() => defaultStore.store);
3454
- };
3455
- var getDefaultUsageTrackingDriver = () => {
3456
- if (defaultUsageTrackingDriver) return defaultUsageTrackingDriver;
3457
- const storageDriver = getDefaultSdkDriver();
3458
- if (isBrowser2()) {
3459
- defaultUsageTrackingDriver = createIndexedDBUsageTrackingDriver({
3460
- legacyStorageDriver: storageDriver
3461
- });
3462
- return defaultUsageTrackingDriver;
3463
- }
3464
- if (isBun3()) {
3465
- defaultUsageTrackingDriver = createBunSqliteUsageTrackingDriver();
3466
- return defaultUsageTrackingDriver;
3467
- }
3468
- if (isNode()) {
3469
- defaultUsageTrackingDriver = createSqliteUsageTrackingDriver({
3470
- legacyStorageDriver: storageDriver
3471
- });
3472
- return defaultUsageTrackingDriver;
3473
- }
3474
- defaultUsageTrackingDriver = createMemoryUsageTrackingDriver();
3475
- return defaultUsageTrackingDriver;
3476
- };
3477
- function mergeUsage(previous, next) {
3478
- if (!previous) return next;
3479
- return {
3480
- promptTokens: next.promptTokens > 0 ? next.promptTokens : previous.promptTokens,
3481
- completionTokens: next.completionTokens > 0 ? next.completionTokens : previous.completionTokens,
3482
- totalTokens: next.totalTokens > 0 ? next.totalTokens : previous.totalTokens,
3483
- cost: next.cost > 0 ? next.cost : previous.cost,
3484
- satsCost: next.satsCost > 0 ? next.satsCost : previous.satsCost
3485
- };
3486
- }
3487
- function hasUsageChanged(previous, next) {
3488
- if (!previous) return true;
3489
- return previous.promptTokens !== next.promptTokens || previous.completionTokens !== next.completionTokens || previous.totalTokens !== next.totalTokens || previous.cost !== next.cost || previous.satsCost !== next.satsCost;
3490
- }
3491
- async function inspectSSEWebStream(stream, onUsage, onResponseId) {
3492
- const reader = stream.getReader();
3493
- const decoder = new TextDecoder("utf-8");
3494
- let buffer = "";
3495
- let capturedUsage = null;
3496
- let capturedResponseId;
3497
- let responseIdCaptured = false;
3498
- const inspectDataPayload = (jsonText) => {
3499
- if (responseIdCaptured && capturedUsage && capturedUsage.totalTokens > 0) {
3500
- return;
3501
- }
3502
- const trimmed = jsonText.trim();
3503
- if (!trimmed || trimmed === "[DONE]") return;
3504
- if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return;
3505
- try {
3506
- const data = JSON.parse(trimmed);
3507
- if (!responseIdCaptured) {
3508
- const responseId = data?.id;
3509
- if (typeof responseId === "string" && responseId.trim().length > 0) {
3510
- capturedResponseId = responseId.trim();
3511
- onResponseId?.(capturedResponseId);
3512
- responseIdCaptured = true;
3513
- }
3514
- }
3515
- const usage = extractUsageFromSSEJson(data);
3516
- if (usage) {
3517
- const merged = mergeUsage(capturedUsage, usage);
3518
- if (hasUsageChanged(capturedUsage, merged)) {
3519
- capturedUsage = merged;
3520
- onUsage(merged);
3521
- }
3522
- }
3523
- } catch {
3524
- }
3525
- };
3526
- const inspectEventBlock = (eventBlock) => {
3527
- if (responseIdCaptured && capturedUsage && capturedUsage.totalTokens > 0) {
3528
- return;
3529
- }
3530
- const lines = eventBlock.split(/\r?\n/);
3531
- const dataParts = [];
3532
- for (const line of lines) {
3533
- if (!line || line.startsWith(":")) continue;
3534
- if (line.startsWith("data:")) {
3535
- const value = line.startsWith("data: ") ? line.slice(6) : line.slice(5);
3536
- dataParts.push(value);
3537
- }
3538
- }
3539
- if (dataParts.length === 0) return;
3540
- inspectDataPayload(dataParts.join("\n"));
3541
- };
3542
- const drainBufferedEvents = () => {
3543
- const terminator = /\r?\n\r?\n/g;
3544
- let lastIndex = 0;
3545
- let match;
3546
- while ((match = terminator.exec(buffer)) !== null) {
3547
- const block = buffer.slice(lastIndex, match.index);
3548
- lastIndex = match.index + match[0].length;
3549
- if (block.length > 0) inspectEventBlock(block);
3550
- }
3551
- if (lastIndex > 0) buffer = buffer.slice(lastIndex);
3552
- };
3553
- try {
3554
- while (true) {
3555
- const { value, done } = await reader.read();
3556
- if (done) break;
3557
- if (value && value.byteLength > 0) {
3558
- buffer += decoder.decode(value, { stream: true });
3559
- drainBufferedEvents();
3560
- }
3561
- }
3562
- buffer += decoder.decode();
3563
- drainBufferedEvents();
3564
- if (buffer.length > 0) {
3565
- const tail = buffer.replace(/\r?\n+$/, "");
3566
- if (tail.length > 0) inspectEventBlock(tail);
3567
- buffer = "";
3568
- }
3569
- } catch {
3570
- } finally {
3571
- try {
3572
- reader.releaseLock();
3573
- } catch {
3574
- }
3575
- }
3576
- return {
3577
- capturedUsage: capturedUsage ?? void 0,
3578
- capturedResponseId
3579
- };
3580
- }
3581
- function createSSEParserTransform(onUsage, onResponseId) {
3582
- let buffer = "";
3583
- const decoder = new string_decoder.StringDecoder("utf8");
3584
- let capturedUsage = null;
3585
- let responseIdCaptured = false;
3586
- const inspectDataPayload = (jsonText) => {
3587
- if (responseIdCaptured && capturedUsage && capturedUsage.totalTokens > 0) {
3588
- return;
3589
- }
3590
- const trimmed = jsonText.trim();
3591
- if (!trimmed || trimmed === "[DONE]") return;
3592
- if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return;
3593
- try {
3594
- const data = JSON.parse(trimmed);
3595
- if (!responseIdCaptured) {
3596
- const responseId = data?.id;
3597
- if (typeof responseId === "string" && responseId.trim().length > 0) {
3598
- onResponseId?.(responseId.trim());
3599
- responseIdCaptured = true;
3600
- }
3601
- }
3602
- const usage = extractUsageFromSSEJson(data);
3603
- if (usage) {
3604
- const mergedUsage = mergeUsage(capturedUsage, usage);
3605
- if (hasUsageChanged(capturedUsage, mergedUsage)) {
3606
- capturedUsage = mergedUsage;
3607
- onUsage(mergedUsage);
3608
- }
3609
- }
3610
- } catch {
3611
- }
3612
- };
3613
- const inspectEventBlock = (eventBlock) => {
3614
- if (responseIdCaptured && capturedUsage && capturedUsage.totalTokens > 0) {
3615
- return;
3616
- }
3617
- const lines = eventBlock.split(/\r?\n/);
3618
- const dataParts = [];
3619
- for (const line of lines) {
3620
- if (!line || line.startsWith(":")) continue;
3621
- if (line.startsWith("data:")) {
3622
- const value = line.startsWith("data: ") ? line.slice(6) : line.slice(5);
3623
- dataParts.push(value);
3624
- }
3625
- }
3626
- if (dataParts.length === 0) return;
3627
- const payload = dataParts.join("\n");
3628
- inspectDataPayload(payload);
3629
- };
3630
- const processBufferedEvents = () => {
3631
- const terminator = /\r?\n\r?\n/g;
3632
- let lastIndex = 0;
3633
- let match;
3634
- while ((match = terminator.exec(buffer)) !== null) {
3635
- const block = buffer.slice(lastIndex, match.index);
3636
- lastIndex = match.index + match[0].length;
3637
- if (block.length > 0) {
3638
- inspectEventBlock(block);
3639
- }
3640
- }
3641
- if (lastIndex > 0) {
3642
- buffer = buffer.slice(lastIndex);
3643
- }
3644
- };
3645
- return new stream.Transform({
3646
- transform(chunk, _encoding, callback) {
3647
- this.push(chunk);
3648
- buffer += decoder.write(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
3649
- processBufferedEvents();
3650
- callback();
3651
- },
3652
- flush(callback) {
3653
- buffer += decoder.end();
3654
- processBufferedEvents();
3655
- if (buffer.length > 0) {
3656
- const tail = buffer.replace(/\r?\n+$/, "");
3657
- if (tail.length > 0) {
3658
- inspectEventBlock(tail);
3659
- }
3660
- buffer = "";
3661
- }
3662
- callback();
3663
- }
3664
- });
3665
- }
3666
-
3667
- // client/RoutstrClient.ts
3668
- var TOPUP_MARGIN = 1.2;
3669
- var RoutstrClient = class {
3670
- constructor(walletAdapter, storageAdapter, providerRegistry, alertLevel, mode = "xcashu", options = {}) {
3671
- this.walletAdapter = walletAdapter;
3672
- this.storageAdapter = storageAdapter;
3673
- this.providerRegistry = providerRegistry;
3674
- this.logger = (options.logger ?? consoleLogger).child("RoutstrClient");
3675
- this.balanceManager = new BalanceManager(
3676
- walletAdapter,
3677
- storageAdapter,
3678
- providerRegistry
3679
- );
3680
- this.cashuSpender = new CashuSpender(
3681
- walletAdapter,
3682
- storageAdapter,
3683
- providerRegistry,
3684
- this.balanceManager
3685
- );
3686
- this.streamProcessor = new StreamProcessor();
3687
- this.alertLevel = alertLevel;
3688
- this.mode = mode;
3689
- this.usageTrackingDriver = options.usageTrackingDriver;
3690
- this.sdkStore = options.sdkStore;
3691
- this.providerManager = options.providerManager ?? new ProviderManager(providerRegistry, this.sdkStore, this.logger);
3692
- }
3693
- cashuSpender;
3694
- balanceManager;
3695
- streamProcessor;
3696
- providerManager;
3697
- alertLevel;
3698
- mode;
3699
- debugLevel = "WARN";
3700
- usageTrackingDriver;
3701
- sdkStore;
3702
- logger;
3703
- /**
3704
- * Get the current client mode
3705
- */
3706
- getMode() {
3707
- return this.mode;
3708
- }
3709
- getDebugLevel() {
3710
- return this.debugLevel;
3711
- }
3712
- setDebugLevel(level) {
3713
- this.debugLevel = level;
3714
- }
3715
- _log(level, ...args) {
3716
- const levelPriority = {
3717
- DEBUG: 0,
3718
- WARN: 1,
3719
- ERROR: 2
3720
- };
3721
- if (levelPriority[level] >= levelPriority[this.debugLevel]) {
3722
- switch (level) {
3723
- case "DEBUG":
3724
- this.logger.log(...args);
3725
- break;
3726
- case "WARN":
3727
- this.logger.warn(...args);
3728
- break;
3729
- case "ERROR":
3730
- this.logger.error(...args);
3731
- break;
3732
- }
3733
- }
3734
- }
3735
- /**
3736
- * Get the CashuSpender instance
3737
- */
3738
- getCashuSpender() {
3739
- return this.cashuSpender;
3740
- }
3741
- /**
3742
- * Get the BalanceManager instance
3743
- */
3744
- getBalanceManager() {
3745
- return this.balanceManager;
3746
- }
3747
- /**
3748
- * Get the ProviderManager instance
3749
- */
3750
- getProviderManager() {
3751
- return this.providerManager;
3752
- }
3753
- /**
3754
- * Check if the client is currently busy (in critical section)
3755
- */
3756
- get isBusy() {
3757
- return this.cashuSpender.isBusy;
3758
- }
3759
- /**
3760
- * Route an API request to the upstream provider
3761
- *
3762
- * This is a simpler alternative to fetchAIResponse that just proxies
3763
- * the request upstream without the streaming callback machinery.
3764
- * Useful for daemon-style routing where you just need to forward
3765
- * requests and get responses back.
3766
- */
3767
- async routeRequest(params) {
3768
- const prepared = await this._prepareRoutedRequest(params);
3769
- const contentType = prepared.response.headers.get("content-type") || "";
3770
- const isSSE = contentType.includes("text/event-stream");
3771
- const runFinalize = async () => {
3772
- const { capturedUsage, capturedResponseId } = await prepared.usagePromise;
3773
- const usage = capturedUsage ?? prepared.capturedUsage;
3774
- const requestId = capturedResponseId ?? prepared.capturedResponseId;
3775
- const satsSpent = await this._handlePostResponseBalanceUpdate({
3776
- token: prepared.tokenUsed,
3777
- baseUrl: prepared.baseUrlUsed,
3778
- mintUrl: params.mintUrl,
3779
- initialTokenBalance: prepared.tokenBalanceInSats,
3780
- response: prepared.response,
3781
- modelId: prepared.modelId,
3782
- usage,
3783
- requestId,
3784
- clientApiKey: prepared.clientApiKey
3785
- });
3786
- prepared.response.satsSpent = satsSpent;
3787
- prepared.response.usage = usage;
3788
- prepared.response.requestId = requestId;
3789
- return satsSpent;
3790
- };
3791
- if (isSSE) {
3792
- const finalizePromise = runFinalize().catch((error) => {
3793
- this._log("ERROR", "[RoutstrClient] SSE finalize failed:", error);
3794
- return 0;
3795
- });
3796
- prepared.response.finalize = () => finalizePromise;
3797
- return prepared.response;
3798
- }
3799
- await runFinalize();
3800
- return prepared.response;
3801
- }
3802
- async _prepareRoutedRequest(params) {
3803
- const {
3804
- path: requestPath,
3805
- method,
3806
- body,
3807
- headers = {},
3808
- baseUrl,
3809
- mintUrl,
3810
- modelId,
3811
- clientApiKey: providedClientApiKey
3812
- } = params;
3813
- const clientApiKey = providedClientApiKey ?? this._extractClientApiKey(headers);
3814
- await this._checkBalance();
3815
- let requiredSats = 1;
3816
- let selectedModel;
3817
- if (modelId) {
3818
- const providerModel = await this.providerManager.getModelForProvider(
3819
- baseUrl,
3820
- modelId
3821
- );
3822
- selectedModel = providerModel ?? void 0;
3823
- if (selectedModel) {
3824
- const requestMessages = Array.isArray(
3825
- body?.messages
3826
- ) ? body.messages : [];
3827
- const requestMaxTokens = typeof body?.max_tokens === "number" ? body.max_tokens : void 0;
3828
- this._log(
3829
- "DEBUG",
3830
- "[RoutstrClient] generic request pricing input",
3831
- {
3832
- modelId: selectedModel.id,
3833
- messageCount: requestMessages.length,
3834
- maxTokens: requestMaxTokens
3835
- }
3836
- );
3837
- requiredSats = this.providerManager.getRequiredSatsForModel(
3838
- selectedModel,
3839
- requestMessages,
3840
- requestMaxTokens
3841
- );
3842
- }
3843
- }
3844
- const { token, tokenBalance, tokenBalanceUnit } = await this._spendToken({
3845
- mintUrl,
3846
- amount: requiredSats,
3847
- baseUrl
3848
- });
3849
- let requestBody = body;
3850
- if (body && typeof body === "object") {
3851
- const bodyObj = body;
3852
- if (!bodyObj.stream) {
3853
- requestBody = { ...bodyObj, stream: false };
3854
- }
3855
- }
3856
- const baseHeaders = this._buildBaseHeaders();
3857
- const requestHeaders = this._withAuthHeader(baseHeaders, token);
3858
- const response = await this._makeRequest({
3859
- path: requestPath,
3860
- method,
3861
- body: method === "GET" ? void 0 : requestBody,
3862
- baseUrl,
3863
- mintUrl,
3864
- token,
3865
- requiredSats,
3866
- headers: requestHeaders,
3867
- baseHeaders,
3868
- selectedModel
3869
- });
3870
- const tokenBalanceInSats = tokenBalanceUnit === "msat" ? tokenBalance / 1e3 : tokenBalance;
3871
- const baseUrlUsed = response.baseUrl || baseUrl;
3872
- const tokenUsed = response.token || token;
3873
- const contentType = response.headers.get("content-type") || "";
3874
- let processedResponse = response;
3875
- let capturedUsage;
3876
- let capturedResponseId;
3877
- let usagePromise = Promise.resolve({});
3878
- if (contentType.includes("text/event-stream") && response.body) {
3879
- const [clientStream, inspectStream] = response.body.tee();
3880
- processedResponse = new Response(clientStream, {
3881
- status: response.status,
3882
- statusText: response.statusText,
3883
- headers: response.headers
3884
- });
3885
- processedResponse.baseUrl = response.baseUrl;
3886
- processedResponse.token = response.token;
3887
- usagePromise = inspectSSEWebStream(
3888
- inspectStream,
3889
- (usage) => {
3890
- capturedUsage = usage;
3891
- processedResponse.usage = usage;
3892
- },
3893
- (responseId) => {
3894
- capturedResponseId = responseId;
3895
- processedResponse.requestId = responseId;
3896
- }
3897
- );
3898
- processedResponse.usagePromise = usagePromise;
3899
- }
3900
- return {
3901
- response: processedResponse,
3902
- tokenUsed,
3903
- baseUrlUsed,
3904
- tokenBalanceInSats,
3905
- modelId,
3906
- capturedUsage,
3907
- capturedResponseId,
3908
- clientApiKey,
3909
- usagePromise
3910
- };
3911
- }
3912
- /**
3913
- * Extract clientApiKey from Authorization Bearer token if present
3914
- */
3915
- _extractClientApiKey(headers) {
3916
- const authHeader = headers["Authorization"] || headers["authorization"];
3917
- if (authHeader?.startsWith("Bearer ")) {
3918
- const extractedKey = authHeader.slice(7);
3919
- return extractedKey;
3920
- }
3921
- return void 0;
3922
- }
3923
- /**
3924
- * Fetch AI response with streaming
3925
- */
3926
- async fetchAIResponse(options, callbacks) {
3927
- const {
3928
- messageHistory,
3929
- selectedModel,
3930
- baseUrl,
3931
- mintUrl,
3932
- balance,
3933
- transactionHistory,
3934
- maxTokens,
3935
- headers
3936
- } = options;
3937
- const apiMessages = await this._convertMessages(messageHistory);
3938
- const requiredSats = this.providerManager.getRequiredSatsForModel(
3939
- selectedModel,
3940
- apiMessages,
3941
- maxTokens
3942
- );
3943
- try {
3944
- await this._checkBalance();
3945
- callbacks.onPaymentProcessing?.(true);
3946
- const spendResult = await this._spendToken({
3947
- mintUrl,
3948
- amount: requiredSats,
3949
- baseUrl
3950
- });
3951
- let token = spendResult.token;
3952
- let tokenBalance = spendResult.tokenBalance;
3953
- let tokenBalanceUnit = spendResult.tokenBalanceUnit;
3954
- const tokenBalanceInSats = tokenBalanceUnit === "msat" ? tokenBalance / 1e3 : tokenBalance;
3955
- callbacks.onTokenCreated?.(this._getPendingCashuTokenAmount());
3956
- const baseHeaders = this._buildBaseHeaders(headers);
3957
- const requestHeaders = this._withAuthHeader(baseHeaders, token);
3958
- const providerInfo = await this.providerRegistry.getProviderInfo(baseUrl);
3959
- const providerVersion = providerInfo?.version ?? "";
3960
- let modelIdForRequest = selectedModel.id;
3961
- if (/^0\.1\./.test(providerVersion)) {
3962
- const newModel = await this.providerManager.getModelForProvider(
3963
- baseUrl,
3964
- selectedModel.id
3965
- );
3966
- modelIdForRequest = newModel?.id ?? selectedModel.id;
3967
- }
3968
- const body = {
3969
- model: modelIdForRequest,
3970
- messages: apiMessages,
3971
- stream: true
3972
- };
3973
- if (maxTokens !== void 0) {
3974
- body.max_tokens = maxTokens;
3975
- }
3976
- if (selectedModel?.name?.startsWith("OpenAI:")) {
3977
- body.tools = [{ type: "web_search" }];
3978
- }
3979
- const response = await this._makeRequest({
3980
- path: "/v1/chat/completions",
3981
- method: "POST",
3982
- body,
3983
- selectedModel,
3984
- baseUrl,
3985
- mintUrl,
3986
- token,
3987
- requiredSats,
3988
- maxTokens,
3989
- headers: requestHeaders,
3990
- baseHeaders
3991
- });
3992
- if (!response.body) {
3993
- throw new Error("Response body is not available");
3994
- }
3995
- if (response.status === 200) {
3996
- const baseUrlUsed = response.baseUrl || baseUrl;
3997
- const streamingResult = await this.streamProcessor.process(
3998
- response,
3999
- {
4000
- onContent: callbacks.onStreamingUpdate,
4001
- onThinking: callbacks.onThinkingUpdate
4002
- },
4003
- selectedModel.id
4004
- );
4005
- if (streamingResult.finish_reason === "content_filter") {
4006
- callbacks.onMessageAppend({
4007
- role: "assistant",
4008
- content: "Your request was denied due to content filtering."
4009
- });
4010
- } else if (streamingResult.content || streamingResult.images && streamingResult.images.length > 0) {
4011
- const message = await this._createAssistantMessage(streamingResult);
4012
- callbacks.onMessageAppend(message);
4013
- } else {
4014
- callbacks.onMessageAppend({
4015
- role: "system",
4016
- content: "The provider did not respond to this request."
4017
- });
4018
- }
4019
- callbacks.onStreamingUpdate("");
4020
- callbacks.onThinkingUpdate("");
4021
- const isApikeysEstimate = this.mode === "apikeys";
4022
- let satsSpent = await this._handlePostResponseBalanceUpdate({
4023
- token,
4024
- baseUrl: baseUrlUsed,
4025
- mintUrl,
4026
- initialTokenBalance: tokenBalanceInSats,
4027
- fallbackSatsSpent: isApikeysEstimate ? this._getEstimatedCosts(selectedModel, streamingResult) : void 0,
4028
- response,
4029
- modelId: selectedModel.id,
4030
- usage: streamingResult.usage ? {
4031
- promptTokens: Number(streamingResult.usage.prompt_tokens ?? 0),
4032
- completionTokens: Number(
4033
- streamingResult.usage.completion_tokens ?? 0
4034
- ),
4035
- totalTokens: Number(streamingResult.usage.total_tokens ?? 0),
4036
- cost: Number(streamingResult.usage.cost ?? 0),
4037
- satsCost: Number(streamingResult.usage.sats_cost ?? 0)
4038
- } : void 0,
4039
- requestId: streamingResult.responseId
4040
- });
4041
- const estimatedCosts = this._getEstimatedCosts(
4042
- selectedModel,
4043
- streamingResult
4044
- );
4045
- const onLastMessageSatsUpdate = callbacks.onLastMessageSatsUpdate;
4046
- onLastMessageSatsUpdate?.(satsSpent, estimatedCosts);
4047
- } else {
4048
- throw new Error(`${response.status} ${response.statusText}`);
4049
- }
4050
- } catch (error) {
4051
- this._handleError(error, callbacks);
4052
- } finally {
4053
- callbacks.onPaymentProcessing?.(false);
4054
- }
4055
- }
4056
- /**
4057
- * Make the API request with failover support
4058
- */
4059
- async _makeRequest(params) {
4060
- const { path, method, body, baseUrl, token, headers } = params;
4061
- try {
4062
- const url = `${baseUrl.replace(/\/$/, "")}${path}`;
4063
- if (this.mode === "xcashu") this._log("DEBUG", "HEADERS,", headers);
4064
- const response = await fetch(url, {
4065
- method,
4066
- headers,
4067
- body: body === void 0 || method === "GET" ? void 0 : JSON.stringify(body)
4068
- });
4069
- if (this.mode === "xcashu") this._log("DEBUG", "response,", response);
4070
- response.baseUrl = baseUrl;
4071
- response.token = token;
4072
- if (!response.ok) {
4073
- const requestId = response.headers.get("x-routstr-request-id") || void 0;
4074
- let bodyText;
4075
- try {
4076
- bodyText = await response.text();
4077
- } catch (e) {
4078
- bodyText = void 0;
4079
- }
4080
- return await this._handleErrorResponse(
4081
- params,
4082
- token,
4083
- response.status,
4084
- requestId,
4085
- this.mode === "xcashu" ? response.headers.get("x-cashu") ?? void 0 : void 0,
4086
- bodyText,
4087
- params.retryCount ?? 0
4088
- );
4089
- }
4090
- return response;
4091
- } catch (error) {
4092
- if (isNetworkErrorMessage(error?.message || "")) {
4093
- return await this._handleErrorResponse(
4094
- params,
4095
- token,
4096
- -1,
4097
- // just for Network Error to skip all statuses
4098
- void 0,
4099
- void 0,
4100
- void 0,
4101
- params.retryCount ?? 0
4102
- );
4103
- }
4104
- throw error;
4105
- }
4106
- }
4107
- /**
4108
- * Handle error responses with failover
4109
- */
4110
- async _handleErrorResponse(params, token, status, requestId, xCashuRefundToken, responseBody, retryCount = 0) {
4111
- const MAX_RETRIES_PER_PROVIDER = 2;
4112
- const { path, method, body, selectedModel, baseUrl, mintUrl } = params;
4113
- let tryNextProvider = false;
4114
- const errorMessage = responseBody;
4115
- this._log(
4116
- "DEBUG",
4117
- `[RoutstrClient] _handleErrorResponse: status=${status}, baseUrl=${baseUrl}, mode=${this.mode}, token preview=${token}, requestId=${requestId}, errorMessage=${errorMessage}`
4118
- );
4119
- this._log(
4120
- "DEBUG",
4121
- `[RoutstrClient] _handleErrorResponse: Attempting to receive/restore token for ${baseUrl}`
4122
- );
4123
- if (params.token.startsWith("cashu")) {
4124
- const receiveResult = await this.cashuSpender.receiveToken(
4125
- params.token
4126
- );
4127
- if (receiveResult.success) {
4128
- this._log(
4129
- "DEBUG",
4130
- `[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${receiveResult.amount}`
4131
- );
4132
- tryNextProvider = true;
4133
- } else {
4134
- this._log(
4135
- "DEBUG",
4136
- `[RoutstrClient] _handleErrorResponse: Failed to receive token: ${receiveResult.message}`
4137
- );
4138
- }
4139
- }
4140
- if (this.mode === "xcashu") {
4141
- if (xCashuRefundToken) {
4142
- this._log(
4143
- "DEBUG",
4144
- `[RoutstrClient] _handleErrorResponse: Attempting to receive xcashu refund token, preview=${xCashuRefundToken.substring(0, 20)}...`
4145
- );
4146
- const receiveResult = await this.cashuSpender.receiveToken(xCashuRefundToken);
4147
- if (receiveResult.success) {
4148
- this._log(
4149
- "DEBUG",
4150
- `[RoutstrClient] _handleErrorResponse: xcashu refund received, amount=${receiveResult.amount}`
4151
- );
4152
- tryNextProvider = true;
4153
- } else {
4154
- this._log(
4155
- "ERROR",
4156
- `[xcashu] Failed to receive refund token: ${receiveResult.message}`
4157
- );
4158
- throw new ProviderError(
4159
- baseUrl,
4160
- status,
4161
- "[xcashu] Failed to receive refund token",
4162
- requestId
4163
- );
4164
- }
4165
- } else {
4166
- if (!tryNextProvider)
4167
- throw new ProviderError(
4168
- baseUrl,
4169
- status,
4170
- "[xcashu] Failed to receive refund token",
4171
- requestId
4172
- );
4173
- }
4174
- }
4175
- if (status === 402 && !tryNextProvider && this.mode === "apikeys") {
4176
- this.storageAdapter.getApiKey(baseUrl);
4177
- let topupAmount = params.requiredSats;
4178
- try {
4179
- const currentBalanceInfo = await this.balanceManager.getTokenBalance(
4180
- params.token,
4181
- baseUrl
4182
- );
4183
- const currentBalance = currentBalanceInfo.unit === "msat" ? currentBalanceInfo.amount / 1e3 : currentBalanceInfo.amount;
4184
- const reservedBalance = currentBalanceInfo.unit === "msat" ? (currentBalanceInfo.reserved ?? 0) / 1e3 : currentBalanceInfo.reserved ?? 0;
4185
- const shortfall = Math.max(0, params.requiredSats - currentBalance + reservedBalance);
4186
- topupAmount = shortfall > 0.21 * params.requiredSats ? shortfall : 0.21 * params.requiredSats;
4187
- this._log(
4188
- "DEBUG",
4189
- `The shortfall is: ${shortfall}. requiredSats: ${params.requiredSats}. Current Balance: ${currentBalance}. Reserved Balance: ${reservedBalance}. Available Balance: ${currentBalance - reservedBalance}`
4190
- );
4191
- } catch (e) {
4192
- this._log(
4193
- "WARN",
4194
- "Could not get current token balance for topup calculation:",
4195
- e
4196
- );
4197
- }
4198
- const topupResult = await this.balanceManager.topUp({
4199
- mintUrl,
4200
- baseUrl,
4201
- amount: topupAmount * TOPUP_MARGIN,
4202
- token: params.token
4203
- });
4204
- this._log(
4205
- "DEBUG",
4206
- `[RoutstrClient] _handleErrorResponse: Topup result for ${baseUrl}: success=${topupResult.success}, message=${topupResult.message}`
4207
- );
4208
- if (!topupResult.success) {
4209
- const message = topupResult.message || "";
4210
- if (message.includes("Insufficient balance")) {
4211
- const needMatch = message.match(/need (\d+)/);
4212
- const haveMatch = message.match(/have (\d+)/);
4213
- const required = needMatch ? parseInt(needMatch[1], 10) : params.requiredSats;
4214
- const available = haveMatch ? parseInt(haveMatch[1], 10) : 0;
4215
- this._log(
4216
- "DEBUG",
4217
- `[RoutstrClient] _handleErrorResponse: Insufficient balance, need=${required}, have=${available}`
4218
- );
4219
- throw new InsufficientBalanceError(
4220
- required,
4221
- available,
4222
- 0,
4223
- "",
4224
- message
4225
- );
4226
- } else {
4227
- this._log(
4228
- "DEBUG",
4229
- `[RoutstrClient] _handleErrorResponse: Topup failed with non-insufficient-balance error, will try next provider`
4230
- );
4231
- tryNextProvider = true;
4232
- }
4233
- } else {
4234
- this._log(
4235
- "DEBUG",
4236
- `[RoutstrClient] _handleErrorResponse: Topup successful, will retry with new token`
4237
- );
4238
- }
4239
- if (!tryNextProvider) {
4240
- if (retryCount < MAX_RETRIES_PER_PROVIDER) {
4241
- this._log(
4242
- "DEBUG",
4243
- `[RoutstrClient] _handleErrorResponse: Retrying 402 (attempt ${retryCount + 1}/${MAX_RETRIES_PER_PROVIDER})`
4244
- );
4245
- return this._makeRequest({
4246
- ...params,
4247
- token: params.token,
4248
- headers: this._withAuthHeader(params.baseHeaders, params.token),
4249
- retryCount: retryCount + 1
4250
- });
4251
- } else {
4252
- this._log(
4253
- "DEBUG",
4254
- `[RoutstrClient] _handleErrorResponse: 402 retry limit reached (${retryCount}/${MAX_RETRIES_PER_PROVIDER}), failing over to next provider`
4255
- );
4256
- tryNextProvider = true;
4257
- }
4258
- }
4259
- }
4260
- const isInsufficientBalance413 = status === 413 && responseBody?.includes("Insufficient balance");
4261
- if (isInsufficientBalance413 && !tryNextProvider && this.mode === "apikeys") {
4262
- let retryToken = params.token;
4263
- try {
4264
- const latestBalanceInfo = await this.balanceManager.getTokenBalance(
4265
- params.token,
4266
- baseUrl
4267
- );
4268
- if (latestBalanceInfo.isInvalidApiKey) {
4269
- this._log(
4270
- "DEBUG",
4271
- `[RoutstrClient] _handleErrorResponse: Invalid API key (proofs already spent), removing for ${baseUrl}`
4272
- );
4273
- this.storageAdapter.removeApiKey(baseUrl);
4274
- tryNextProvider = true;
4275
- } else {
4276
- const latestTokenBalance = latestBalanceInfo.unit === "msat" ? latestBalanceInfo.amount / 1e3 : latestBalanceInfo.amount;
4277
- if (latestBalanceInfo.apiKey) {
4278
- const storedApiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
4279
- if (storedApiKeyEntry?.key !== latestBalanceInfo.apiKey) {
4280
- if (storedApiKeyEntry) {
4281
- this.storageAdapter.removeApiKey(baseUrl);
4282
- }
4283
- this.storageAdapter.setApiKey(baseUrl, latestBalanceInfo.apiKey);
4284
- }
4285
- retryToken = latestBalanceInfo.apiKey;
4286
- }
4287
- if (latestTokenBalance >= 0) {
4288
- this.storageAdapter.updateApiKeyBalance(
4289
- baseUrl,
4290
- latestTokenBalance
4291
- );
4292
- }
4293
- }
4294
- } catch (error) {
4295
- this._log(
4296
- "WARN",
4297
- `[RoutstrClient] _handleErrorResponse: Failed to refresh API key after 413 insufficient balance for ${baseUrl}`,
4298
- error
4299
- );
4300
- }
4301
- if (retryCount < MAX_RETRIES_PER_PROVIDER) {
4302
- this._log(
4303
- "DEBUG",
4304
- `[RoutstrClient] _handleErrorResponse: Retrying 413 (attempt ${retryCount + 1}/${MAX_RETRIES_PER_PROVIDER})`
4305
- );
4306
- return this._makeRequest({
4307
- ...params,
4308
- token: retryToken,
4309
- headers: this._withAuthHeader(params.baseHeaders, retryToken),
4310
- retryCount: retryCount + 1
4311
- });
4312
- } else {
4313
- this._log(
4314
- "DEBUG",
4315
- `[RoutstrClient] _handleErrorResponse: 413 retry limit reached (${retryCount}/${MAX_RETRIES_PER_PROVIDER}), failing over to next provider`
4316
- );
4317
- tryNextProvider = true;
4318
- }
4319
- }
4320
- if (status === 401 && this.mode === "apikeys") {
4321
- this._log(
4322
- "DEBUG",
4323
- `[RoutstrClient] _handleErrorResponse: Checking balance for ${baseUrl}, key preview=${token}`
4324
- );
4325
- const latestBalanceInfo = await this.balanceManager.getTokenBalance(
4326
- token,
4327
- baseUrl
4328
- );
4329
- if (latestBalanceInfo.isInvalidApiKey) {
4330
- this.storageAdapter.removeApiKey(baseUrl);
4331
- tryNextProvider = true;
4332
- }
4333
- }
4334
- if ((status === 401 || status === 403 || status === 404 || status === 413 || status === 400 || status === 429 || status === 500 || status === 502 || status === 503 || status === 504 || status === 521) && !tryNextProvider) {
4335
- this._log(
4336
- "DEBUG",
4337
- `[RoutstrClient] _handleErrorResponse: Status ${status} (${status === 429 ? "rate limited" : "auth/server error"}), attempting refund for ${baseUrl}, mode=${this.mode}`
4338
- );
4339
- if (this.mode === "apikeys") {
4340
- this._log(
4341
- "DEBUG",
4342
- `[RoutstrClient] _handleErrorResponse: Attempting API key refund for ${baseUrl}, key preview=${token}`
4343
- );
4344
- const latestBalanceInfo = await this.balanceManager.getTokenBalance(
4345
- token,
4346
- baseUrl
4347
- );
4348
- this._log(
4349
- "DEBUG",
4350
- `[RoutstrClient] _handleErrorResponse: Initial API key balance: ${latestBalanceInfo.amount}`
4351
- );
4352
- const refundResult = await this.balanceManager.refundApiKey({
4353
- mintUrl,
4354
- baseUrl,
4355
- apiKey: token,
4356
- forceRefund: true
4357
- });
4358
- this._log(
4359
- "DEBUG",
4360
- `[RoutstrClient] _handleErrorResponse: API key refund result: success=${refundResult.success}, message=${refundResult.message}`
4361
- );
4362
- if (!refundResult.success && latestBalanceInfo.amount > 0) {
4363
- throw new ProviderError(
4364
- baseUrl,
4365
- status,
4366
- refundResult.message ?? "Unknown error"
4367
- );
4368
- }
4369
- }
4370
- }
4371
- this.providerManager.markFailed(baseUrl);
4372
- this._log(
4373
- "DEBUG",
4374
- `[RoutstrClient] _handleErrorResponse: Marked provider ${baseUrl} as failed`
4375
- );
4376
- if (!selectedModel) {
4377
- throw new ProviderError(
4378
- baseUrl,
4379
- status,
4380
- "Funny, no selected model. HMM. "
4381
- );
4382
- }
4383
- const nextProvider = this.providerManager.findNextBestProvider(
4384
- selectedModel.id,
4385
- baseUrl
4386
- );
4387
- if (nextProvider) {
4388
- this._log(
4389
- "DEBUG",
4390
- `[RoutstrClient] _handleErrorResponse: Failing over to next provider: ${nextProvider}, model: ${selectedModel.id}`
4391
- );
4392
- const newModel = await this.providerManager.getModelForProvider(
4393
- nextProvider,
4394
- selectedModel.id
4395
- ) ?? selectedModel;
4396
- const messagesForPricing = Array.isArray(
4397
- body?.messages
4398
- ) ? body.messages : [];
4399
- const newRequiredSats = this.providerManager.getRequiredSatsForModel(
4400
- newModel,
4401
- messagesForPricing,
4402
- params.maxTokens
4403
- );
4404
- this._log(
4405
- "DEBUG",
4406
- `[RoutstrClient] _handleErrorResponse: Creating new token for failover provider ${nextProvider}, required sats: ${newRequiredSats}`
4407
- );
4408
- const spendResult = await this._spendToken({
4409
- mintUrl,
4410
- amount: newRequiredSats,
4411
- baseUrl: nextProvider
4412
- });
4413
- return this._makeRequest({
4414
- ...params,
4415
- path,
4416
- method,
4417
- body,
4418
- baseUrl: nextProvider,
4419
- selectedModel: newModel,
4420
- token: spendResult.token,
4421
- requiredSats: newRequiredSats,
4422
- headers: this._withAuthHeader(params.baseHeaders, spendResult.token),
4423
- retryCount: 0
4424
- });
4425
- }
4426
- throw new FailoverError(
4427
- baseUrl,
4428
- Array.from(this.providerManager.getFailedProviders())
4429
- );
4430
- }
4431
- /**
4432
- * Handle post-response balance update for all modes
4433
- */
4434
- async _handlePostResponseBalanceUpdate(params) {
4435
- const {
4436
- token,
4437
- baseUrl,
4438
- mintUrl,
4439
- initialTokenBalance,
4440
- fallbackSatsSpent,
4441
- response,
4442
- modelId,
4443
- usage,
4444
- requestId,
4445
- clientApiKey
4446
- } = params;
4447
- let satsSpent = initialTokenBalance;
4448
- if (this.mode === "xcashu" && response) {
4449
- const refundToken = response.headers.get("x-cashu") ?? void 0;
4450
- if (refundToken) {
4451
- const receiveResult = await this.cashuSpender.receiveToken(refundToken);
4452
- if (receiveResult.success) {
4453
- this.storageAdapter.removeXcashuToken(baseUrl, token);
4454
- satsSpent = initialTokenBalance - receiveResult.amount * (receiveResult.unit == "sat" ? 1 : 1e3);
4455
- } else {
4456
- this._log(
4457
- "ERROR",
4458
- `[xcashu] Failed to receive refund token: ${receiveResult.message}`
4459
- );
4460
- }
4461
- }
4462
- } else if (this.mode === "apikeys") {
4463
- try {
4464
- const latestBalanceInfo = await this.balanceManager.getTokenBalance(
4465
- token,
4466
- baseUrl
4467
- );
4468
- this._log(
4469
- "DEBUG",
4470
- "LATEST Balance",
4471
- latestBalanceInfo.amount,
4472
- latestBalanceInfo.reserved,
4473
- latestBalanceInfo.apiKey,
4474
- baseUrl
4475
- );
4476
- const latestTokenBalance = latestBalanceInfo.unit === "msat" ? latestBalanceInfo.amount / 1e3 : latestBalanceInfo.amount;
4477
- const storedApiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
4478
- if (storedApiKeyEntry?.key.startsWith("cashu") && latestBalanceInfo.apiKey) {
4479
- this.storageAdapter.removeApiKey(baseUrl);
4480
- this.storageAdapter.setApiKey(baseUrl, latestBalanceInfo.apiKey);
4481
- }
4482
- this.storageAdapter.updateApiKeyBalance(baseUrl, latestTokenBalance);
4483
- satsSpent = initialTokenBalance - latestTokenBalance;
4484
- } catch (e) {
4485
- this._log("WARN", "Could not get updated API key balance:", e);
4486
- satsSpent = fallbackSatsSpent ?? initialTokenBalance;
4487
- }
4488
- }
4489
- await this._trackResponseUsage({
4490
- token,
4491
- baseUrl,
4492
- response,
4493
- modelId,
4494
- satsSpent,
4495
- usage,
4496
- requestId,
4497
- clientApiKey
4498
- });
4499
- (async () => {
4500
- })();
4501
- return satsSpent;
4502
- }
4503
- async _trackResponseUsage(params) {
4504
- const {
4505
- token,
4506
- baseUrl,
4507
- response,
4508
- modelId,
4509
- satsSpent,
4510
- usage: providedUsage,
4511
- requestId: providedRequestId,
4512
- clientApiKey
4513
- } = params;
4514
- if (!response || !modelId) {
4515
- return;
4516
- }
4517
- try {
4518
- let usage = providedUsage;
4519
- let requestId = providedRequestId;
4520
- if (!usage || !requestId) {
4521
- const contentType = response.headers.get("content-type") || "";
4522
- if (contentType.includes("text/event-stream")) {
4523
- usage = usage ?? response.usage;
4524
- requestId = requestId ?? response.requestId ?? response.headers.get("x-routstr-request-id") ?? void 0;
4525
- if (!usage) {
4526
- return;
4527
- }
4528
- } else {
4529
- const cloned = response.clone();
4530
- const responseBody = await cloned.json();
4531
- usage = usage ?? extractUsageFromResponseBody(responseBody, satsSpent) ?? void 0;
4532
- requestId = requestId ?? extractResponseId(responseBody) ?? response.headers.get("x-routstr-request-id") ?? void 0;
4533
- }
4534
- }
4535
- if (!usage) {
4536
- return;
4537
- }
4538
- const finalRequestId = requestId || "unknown";
4539
- const store = this.sdkStore ?? await getDefaultSdkStore();
4540
- const state = store.getState();
4541
- const matchKey = clientApiKey ?? token;
4542
- const matchingClient = state.clientIds.find(
4543
- (client) => client.apiKey === matchKey
4544
- );
4545
- const entryId = finalRequestId === "unknown" ? `req-${Date.now()}-${modelId}` : finalRequestId;
4546
- const usageTracking = this.usageTrackingDriver ?? getDefaultUsageTrackingDriver();
4547
- const entry = {
4548
- id: entryId,
4549
- timestamp: Date.now(),
4550
- modelId,
4551
- baseUrl,
4552
- requestId: finalRequestId,
4553
- client: matchingClient?.clientId,
4554
- ...usage
4555
- };
4556
- if (this.mode === "xcashu") {
4557
- entry.satsCost = satsSpent;
4558
- }
4559
- await usageTracking.append(entry);
4560
- } catch (error) {
4561
- }
4562
- }
4563
- /**
4564
- * Convert messages for API format
4565
- */
4566
- async _convertMessages(messages) {
4567
- return Promise.all(
4568
- messages.filter((m) => m.role !== "system").map(async (m) => ({
4569
- role: m.role,
4570
- content: typeof m.content === "string" ? m.content : m.content
4571
- }))
4572
- );
4573
- }
4574
- /**
4575
- * Create assistant message from streaming result
4576
- */
4577
- async _createAssistantMessage(result) {
4578
- if (result.images && result.images.length > 0) {
4579
- const content = [];
4580
- if (result.content) {
4581
- content.push({
4582
- type: "text",
4583
- text: result.content,
4584
- thinking: result.thinking,
4585
- citations: result.citations,
4586
- annotations: result.annotations
4587
- });
4588
- }
4589
- for (const img of result.images) {
4590
- content.push({
4591
- type: "image_url",
4592
- image_url: {
4593
- url: img.image_url.url
4594
- }
4595
- });
4596
- }
4597
- return {
4598
- role: "assistant",
4599
- content
4600
- };
4601
- }
4602
- return {
4603
- role: "assistant",
4604
- content: result.content || ""
4605
- };
4606
- }
4607
- /**
4608
- * Calculate estimated costs from usage
4609
- */
4610
- _getEstimatedCosts(selectedModel, streamingResult) {
4611
- let estimatedCosts = 0;
4612
- if (streamingResult.usage) {
4613
- const { completion_tokens, prompt_tokens } = streamingResult.usage;
4614
- if (completion_tokens !== void 0 && prompt_tokens !== void 0) {
4615
- estimatedCosts = (selectedModel.sats_pricing?.completion ?? 0) * completion_tokens + (selectedModel.sats_pricing?.prompt ?? 0) * prompt_tokens;
4616
- }
4617
- }
4618
- return estimatedCosts;
4619
- }
4620
- /**
4621
- * Get pending API key amount
4622
- */
4623
- _getPendingCashuTokenAmount() {
4624
- const apiKeyDistribution = this.storageAdapter.getApiKeyDistribution();
4625
- return apiKeyDistribution.reduce((total, item) => total + item.amount, 0);
4626
- }
4627
- /**
4628
- * Handle errors and notify callbacks
4629
- */
4630
- _handleError(error, callbacks) {
4631
- this._log("ERROR", "[RoutstrClient] _handleError: Error occurred", error);
4632
- if (error instanceof Error) {
4633
- const isStreamError = error.message.includes("Error in input stream") || error.message.includes("Load failed");
4634
- const modifiedErrorMsg = isStreamError ? "AI stream was cut off, turn on Keep Active or please try again" : error.message;
4635
- this._log(
4636
- "ERROR",
4637
- `[RoutstrClient] _handleError: Error type=${error.constructor.name}, message=${modifiedErrorMsg}, isStreamError=${isStreamError}`
4638
- );
4639
- callbacks.onMessageAppend({
4640
- role: "system",
4641
- content: "Uncaught Error: " + modifiedErrorMsg + (this.alertLevel === "max" ? " | " + error.stack : "")
4642
- });
4643
- } else {
4644
- callbacks.onMessageAppend({
4645
- role: "system",
4646
- content: "Unknown Error: Please tag Routstr on Nostr and/or retry."
4647
- });
4648
- }
4649
- }
4650
- /**
4651
- * Check wallet balance and throw if insufficient
4652
- */
4653
- async _checkBalance() {
4654
- const balances = await this.walletAdapter.getBalances();
4655
- const totalBalance = Object.values(balances).reduce((sum, v) => sum + v, 0);
4656
- if (totalBalance <= 0) {
4657
- throw new InsufficientBalanceError(1, 0);
4658
- }
4659
- }
4660
- /**
4661
- * Spend a token using CashuSpender with standardized error handling
4662
- */
4663
- async _spendToken(params) {
4664
- const { mintUrl, amount, baseUrl } = params;
4665
- this._log(
4666
- "DEBUG",
4667
- `[RoutstrClient] _spendToken: mode=${this.mode}, amount=${amount}, baseUrl=${baseUrl}, mintUrl=${mintUrl}`
4668
- );
4669
- if (this.mode === "apikeys") {
4670
- let parentApiKey = this.storageAdapter.getApiKey(baseUrl);
4671
- if (!parentApiKey) {
4672
- this._log(
4673
- "DEBUG",
4674
- `[RoutstrClient] _spendToken: No existing API key for ${baseUrl}, creating new one via Cashu`
4675
- );
4676
- const spendResult2 = await this.cashuSpender.spend({
4677
- mintUrl,
4678
- amount: amount * TOPUP_MARGIN,
4679
- baseUrl: "",
4680
- reuseToken: false
4681
- });
4682
- if (!spendResult2.token) {
4683
- this._log(
4684
- "ERROR",
4685
- `[RoutstrClient] _spendToken: Failed to create Cashu token for API key creation, error:`,
4686
- spendResult2.error
4687
- );
4688
- throw new Error(
4689
- `[RoutstrClient] _spendToken: Failed to create Cashu token for API key creation, error: ${spendResult2.error}`
4690
- );
4691
- } else {
4692
- this._log(
4693
- "DEBUG",
4694
- `[RoutstrClient] _spendToken: Cashu token created, token preview: ${spendResult2.token}`
4695
- );
4696
- }
4697
- this._log(
4698
- "DEBUG",
4699
- `[RoutstrClient] _spendToken: Created API key for ${baseUrl}, key preview: ${spendResult2.token}, balance: ${spendResult2.balance}`
4700
- );
4701
- try {
4702
- this.storageAdapter.setApiKey(baseUrl, spendResult2.token);
4703
- } catch (error) {
4704
- if (error instanceof Error && error.message.includes("ApiKey already exists")) {
4705
- const receiveResult = await this.cashuSpender.receiveToken(
4706
- spendResult2.token
4707
- );
4708
- if (receiveResult.success) {
4709
- this._log(
4710
- "DEBUG",
4711
- `[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${receiveResult.amount}`
4712
- );
4713
- } else {
4714
- this._log(
4715
- "DEBUG",
4716
- `[RoutstrClient] _handleErrorResponse: Token restore failed: ${receiveResult.message}`
4717
- );
4718
- }
4719
- this._log(
4720
- "DEBUG",
4721
- `[RoutstrClient] _spendToken: API key already exists for ${baseUrl}, using existing key`
4722
- );
4723
- } else {
4724
- throw error;
4725
- }
4726
- }
4727
- parentApiKey = this.storageAdapter.getApiKey(baseUrl);
4728
- } else {
4729
- this._log(
4730
- "DEBUG",
4731
- `[RoutstrClient] _spendToken: Using existing API key for ${baseUrl}, key preview: ${parentApiKey.key}`
4732
- );
4733
- }
4734
- let tokenBalance = 0;
4735
- let tokenBalanceUnit = "sat";
4736
- const apiKeyDistribution = this.storageAdapter.getApiKeyDistribution();
4737
- const distributionForBaseUrl = apiKeyDistribution.find(
4738
- (d) => d.baseUrl === baseUrl
4739
- );
4740
- if (distributionForBaseUrl) {
4741
- tokenBalance = distributionForBaseUrl.amount;
4742
- }
4743
- if (tokenBalance === 0 && parentApiKey) {
4744
- try {
4745
- const balanceInfo = await this.balanceManager.getTokenBalance(
4746
- parentApiKey.key,
4747
- baseUrl
4748
- );
4749
- tokenBalance = balanceInfo.amount;
4750
- tokenBalanceUnit = balanceInfo.unit;
4751
- } catch (e) {
4752
- this._log("WARN", "Could not get initial API key balance:", e);
4753
- }
4754
- }
4755
- this._log(
4756
- "DEBUG",
4757
- `[RoutstrClient] _spendToken: Returning token with balance=${tokenBalance} ${tokenBalanceUnit}`
4758
- );
4759
- return {
4760
- token: parentApiKey?.key ?? "",
4761
- tokenBalance,
4762
- tokenBalanceUnit
4763
- };
4764
- }
4765
- this._log(
4766
- "DEBUG",
4767
- `[RoutstrClient] _spendToken: Calling CashuSpender.spend for amount=${amount}, mintUrl=${mintUrl}, mode=${this.mode}`
4768
- );
4769
- const spendResult = await this.cashuSpender.spend({
4770
- mintUrl,
4771
- amount,
4772
- baseUrl: "",
4773
- reuseToken: false
4774
- });
4775
- if (!spendResult.token) {
4776
- this._log(
4777
- "ERROR",
4778
- `[RoutstrClient] _spendToken: CashuSpender.spend failed, error:`,
4779
- spendResult.error
4780
- );
4781
- } else {
4782
- this._log(
4783
- "DEBUG",
4784
- `[RoutstrClient] _spendToken: Cashu token created, token preview: ${spendResult.token}, balance: ${spendResult.balance} ${spendResult.unit ?? "sat"}`
4785
- );
4786
- this.storageAdapter.addXcashuToken(baseUrl, spendResult.token);
4787
- }
4788
- return {
4789
- token: spendResult.token,
4790
- tokenBalance: spendResult.balance,
4791
- tokenBalanceUnit: spendResult.unit ?? "sat"
4792
- };
4793
- }
4794
- /**
4795
- * Build request headers with common defaults and dev mock controls
4796
- */
4797
- _buildBaseHeaders(additionalHeaders = {}, token) {
4798
- const headers = {
4799
- ...additionalHeaders,
4800
- "Content-Type": "application/json"
4801
- };
4802
- return headers;
4803
- }
4804
- /**
4805
- * Attach auth headers using the active client mode
4806
- */
4807
- _withAuthHeader(headers, token) {
4808
- const nextHeaders = { ...headers };
4809
- if (this.mode === "xcashu") {
4810
- nextHeaders["X-Cashu"] = token;
4811
- } else {
4812
- nextHeaders["Authorization"] = `Bearer ${token}`;
4813
- }
4814
- return nextHeaders;
4815
- }
4816
- };
4817
-
4818
- exports.ProviderManager = ProviderManager;
4819
- exports.RoutstrClient = RoutstrClient;
4820
- exports.StreamProcessor = StreamProcessor;
4821
- exports.createSSEParserTransform = createSSEParserTransform;
4822
- exports.inspectSSEWebStream = inspectSSEWebStream;
4823
- //# sourceMappingURL=index.js.map
4824
- //# sourceMappingURL=index.js.map