@routstr/sdk 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,151 @@
1
+ import { StoreApi } from 'zustand/vanilla';
2
+ import { D as DiscoveryAdapter } from './interfaces-BxDEka72.js';
3
+ import { P as ProviderRegistry, S as StorageAdapter } from './interfaces-B62Rw-dd.js';
4
+ import { M as Model, P as ProviderInfo } from './types-BYj_8c5c.js';
5
+
6
+ interface UsageTrackingEntry {
7
+ id: string;
8
+ timestamp: number;
9
+ modelId: string;
10
+ baseUrl: string;
11
+ requestId: string;
12
+ cost: number;
13
+ satsCost: number;
14
+ promptTokens: number;
15
+ completionTokens: number;
16
+ totalTokens: number;
17
+ client?: string;
18
+ sessionId?: string;
19
+ tags?: string[];
20
+ }
21
+
22
+ interface ListUsageTrackingOptions {
23
+ limit?: number;
24
+ before?: number;
25
+ after?: number;
26
+ modelId?: string;
27
+ baseUrl?: string;
28
+ sessionId?: string;
29
+ client?: string;
30
+ }
31
+ interface UsageTrackingDriver {
32
+ migrate(): Promise<void>;
33
+ append(entry: UsageTrackingEntry): Promise<void>;
34
+ appendMany(entries: UsageTrackingEntry[]): Promise<void>;
35
+ list(options?: ListUsageTrackingOptions): Promise<UsageTrackingEntry[]>;
36
+ count(options?: Omit<ListUsageTrackingOptions, "limit">): Promise<number>;
37
+ deleteOlderThan(timestamp: number): Promise<number>;
38
+ clear(): Promise<void>;
39
+ }
40
+
41
+ interface StorageDriver {
42
+ getItem<T>(key: string, defaultValue: T): Promise<T>;
43
+ setItem<T>(key: string, value: T): Promise<void>;
44
+ removeItem(key: string): Promise<void>;
45
+ }
46
+ interface SdkStorageState {
47
+ modelsFromAllProviders: Record<string, Model[]>;
48
+ lastUsedModel: string | null;
49
+ baseUrlsList: string[];
50
+ disabledProviders: string[];
51
+ mintsFromAllProviders: Record<string, string[]>;
52
+ infoFromAllProviders: Record<string, ProviderInfo>;
53
+ lastModelsUpdate: Record<string, number>;
54
+ lastBaseUrlsUpdate: number | null;
55
+ apiKeys: Array<{
56
+ baseUrl: string;
57
+ key: string;
58
+ balance: number;
59
+ lastUsed: number | null;
60
+ }>;
61
+ childKeys: Array<{
62
+ parentBaseUrl: string;
63
+ childKey: string;
64
+ balance: number;
65
+ balanceLimit?: number;
66
+ validityDate?: number;
67
+ createdAt: number;
68
+ }>;
69
+ xcashuTokens: Record<string, Array<{
70
+ baseUrl: string;
71
+ token: string;
72
+ createdAt: number;
73
+ tryCount: number;
74
+ }>>;
75
+ routstr21Models: string[];
76
+ lastRoutstr21ModelsUpdate: number | null;
77
+ cachedReceiveTokens: Array<{
78
+ token: string;
79
+ amount: number;
80
+ unit: "sat" | "msat";
81
+ createdAt: number;
82
+ }>;
83
+ clientIds: Array<{
84
+ clientId: string;
85
+ name: string;
86
+ apiKey: string;
87
+ createdAt: number;
88
+ lastUsed?: number | null;
89
+ }>;
90
+ }
91
+
92
+ interface SdkStoreOptions {
93
+ driver: StorageDriver;
94
+ }
95
+ interface SdkStorageStore extends SdkStorageState {
96
+ setModelsFromAllProviders: (value: Record<string, Model[]>) => void;
97
+ setLastUsedModel: (value: string | null) => void;
98
+ setBaseUrlsList: (value: string[]) => void;
99
+ setBaseUrlsLastUpdate: (value: number | null) => void;
100
+ setDisabledProviders: (value: string[]) => void;
101
+ setMintsFromAllProviders: (value: Record<string, string[]>) => void;
102
+ setInfoFromAllProviders: (value: Record<string, ProviderInfo>) => void;
103
+ setLastModelsUpdate: (value: Record<string, number>) => void;
104
+ setApiKeys: (value: Array<{
105
+ baseUrl: string;
106
+ key: string;
107
+ balance?: number;
108
+ lastUsed?: number | null;
109
+ }> | ((current: SdkStorageStore["apiKeys"]) => SdkStorageStore["apiKeys"])) => void;
110
+ setChildKeys: (value: Array<{
111
+ parentBaseUrl: string;
112
+ childKey: string;
113
+ balance?: number;
114
+ balanceLimit?: number;
115
+ validityDate?: number;
116
+ createdAt?: number;
117
+ }>) => void;
118
+ setXcashuTokens: (value: Record<string, Array<{
119
+ baseUrl: string;
120
+ token: string;
121
+ createdAt?: number;
122
+ tryCount?: number;
123
+ }>>) => void;
124
+ updateXcashuTokenTryCount: (token: string, tryCount: number) => void;
125
+ setRoutstr21Models: (value: string[]) => void;
126
+ setRoutstr21ModelsLastUpdate: (value: number | null) => void;
127
+ setCachedReceiveTokens: (value: Array<{
128
+ token: string;
129
+ amount: number;
130
+ unit: "sat" | "msat";
131
+ createdAt?: number;
132
+ }>) => void;
133
+ setClientIds: (value: Array<{
134
+ clientId: string;
135
+ name: string;
136
+ apiKey: string;
137
+ createdAt?: number;
138
+ lastUsed?: number | null;
139
+ }> | ((current: SdkStorageStore["clientIds"]) => SdkStorageStore["clientIds"])) => void;
140
+ }
141
+ /** Store type returned after async initialization */
142
+ type SdkStore = StoreApi<SdkStorageStore>;
143
+ declare const createSdkStore: ({ driver, }: SdkStoreOptions) => {
144
+ store: SdkStore;
145
+ hydrate: Promise<void>;
146
+ };
147
+ declare const createDiscoveryAdapterFromStore: (store: SdkStore) => DiscoveryAdapter;
148
+ declare const createStorageAdapterFromStore: (store: SdkStore) => StorageAdapter;
149
+ declare const createProviderRegistryFromStore: (store: SdkStore) => ProviderRegistry;
150
+
151
+ export { type ListUsageTrackingOptions as L, type SdkStore as S, type UsageTrackingDriver as U, type StorageDriver as a, type UsageTrackingEntry as b, createDiscoveryAdapterFromStore as c, createProviderRegistryFromStore as d, createSdkStore as e, createStorageAdapterFromStore as f };
@@ -0,0 +1,151 @@
1
+ import { StoreApi } from 'zustand/vanilla';
2
+ import { D as DiscoveryAdapter } from './interfaces-BWJJTCXO.mjs';
3
+ import { P as ProviderRegistry, S as StorageAdapter } from './interfaces-C5fLD3jB.mjs';
4
+ import { M as Model, P as ProviderInfo } from './types-BYj_8c5c.mjs';
5
+
6
+ interface UsageTrackingEntry {
7
+ id: string;
8
+ timestamp: number;
9
+ modelId: string;
10
+ baseUrl: string;
11
+ requestId: string;
12
+ cost: number;
13
+ satsCost: number;
14
+ promptTokens: number;
15
+ completionTokens: number;
16
+ totalTokens: number;
17
+ client?: string;
18
+ sessionId?: string;
19
+ tags?: string[];
20
+ }
21
+
22
+ interface ListUsageTrackingOptions {
23
+ limit?: number;
24
+ before?: number;
25
+ after?: number;
26
+ modelId?: string;
27
+ baseUrl?: string;
28
+ sessionId?: string;
29
+ client?: string;
30
+ }
31
+ interface UsageTrackingDriver {
32
+ migrate(): Promise<void>;
33
+ append(entry: UsageTrackingEntry): Promise<void>;
34
+ appendMany(entries: UsageTrackingEntry[]): Promise<void>;
35
+ list(options?: ListUsageTrackingOptions): Promise<UsageTrackingEntry[]>;
36
+ count(options?: Omit<ListUsageTrackingOptions, "limit">): Promise<number>;
37
+ deleteOlderThan(timestamp: number): Promise<number>;
38
+ clear(): Promise<void>;
39
+ }
40
+
41
+ interface StorageDriver {
42
+ getItem<T>(key: string, defaultValue: T): Promise<T>;
43
+ setItem<T>(key: string, value: T): Promise<void>;
44
+ removeItem(key: string): Promise<void>;
45
+ }
46
+ interface SdkStorageState {
47
+ modelsFromAllProviders: Record<string, Model[]>;
48
+ lastUsedModel: string | null;
49
+ baseUrlsList: string[];
50
+ disabledProviders: string[];
51
+ mintsFromAllProviders: Record<string, string[]>;
52
+ infoFromAllProviders: Record<string, ProviderInfo>;
53
+ lastModelsUpdate: Record<string, number>;
54
+ lastBaseUrlsUpdate: number | null;
55
+ apiKeys: Array<{
56
+ baseUrl: string;
57
+ key: string;
58
+ balance: number;
59
+ lastUsed: number | null;
60
+ }>;
61
+ childKeys: Array<{
62
+ parentBaseUrl: string;
63
+ childKey: string;
64
+ balance: number;
65
+ balanceLimit?: number;
66
+ validityDate?: number;
67
+ createdAt: number;
68
+ }>;
69
+ xcashuTokens: Record<string, Array<{
70
+ baseUrl: string;
71
+ token: string;
72
+ createdAt: number;
73
+ tryCount: number;
74
+ }>>;
75
+ routstr21Models: string[];
76
+ lastRoutstr21ModelsUpdate: number | null;
77
+ cachedReceiveTokens: Array<{
78
+ token: string;
79
+ amount: number;
80
+ unit: "sat" | "msat";
81
+ createdAt: number;
82
+ }>;
83
+ clientIds: Array<{
84
+ clientId: string;
85
+ name: string;
86
+ apiKey: string;
87
+ createdAt: number;
88
+ lastUsed?: number | null;
89
+ }>;
90
+ }
91
+
92
+ interface SdkStoreOptions {
93
+ driver: StorageDriver;
94
+ }
95
+ interface SdkStorageStore extends SdkStorageState {
96
+ setModelsFromAllProviders: (value: Record<string, Model[]>) => void;
97
+ setLastUsedModel: (value: string | null) => void;
98
+ setBaseUrlsList: (value: string[]) => void;
99
+ setBaseUrlsLastUpdate: (value: number | null) => void;
100
+ setDisabledProviders: (value: string[]) => void;
101
+ setMintsFromAllProviders: (value: Record<string, string[]>) => void;
102
+ setInfoFromAllProviders: (value: Record<string, ProviderInfo>) => void;
103
+ setLastModelsUpdate: (value: Record<string, number>) => void;
104
+ setApiKeys: (value: Array<{
105
+ baseUrl: string;
106
+ key: string;
107
+ balance?: number;
108
+ lastUsed?: number | null;
109
+ }> | ((current: SdkStorageStore["apiKeys"]) => SdkStorageStore["apiKeys"])) => void;
110
+ setChildKeys: (value: Array<{
111
+ parentBaseUrl: string;
112
+ childKey: string;
113
+ balance?: number;
114
+ balanceLimit?: number;
115
+ validityDate?: number;
116
+ createdAt?: number;
117
+ }>) => void;
118
+ setXcashuTokens: (value: Record<string, Array<{
119
+ baseUrl: string;
120
+ token: string;
121
+ createdAt?: number;
122
+ tryCount?: number;
123
+ }>>) => void;
124
+ updateXcashuTokenTryCount: (token: string, tryCount: number) => void;
125
+ setRoutstr21Models: (value: string[]) => void;
126
+ setRoutstr21ModelsLastUpdate: (value: number | null) => void;
127
+ setCachedReceiveTokens: (value: Array<{
128
+ token: string;
129
+ amount: number;
130
+ unit: "sat" | "msat";
131
+ createdAt?: number;
132
+ }>) => void;
133
+ setClientIds: (value: Array<{
134
+ clientId: string;
135
+ name: string;
136
+ apiKey: string;
137
+ createdAt?: number;
138
+ lastUsed?: number | null;
139
+ }> | ((current: SdkStorageStore["clientIds"]) => SdkStorageStore["clientIds"])) => void;
140
+ }
141
+ /** Store type returned after async initialization */
142
+ type SdkStore = StoreApi<SdkStorageStore>;
143
+ declare const createSdkStore: ({ driver, }: SdkStoreOptions) => {
144
+ store: SdkStore;
145
+ hydrate: Promise<void>;
146
+ };
147
+ declare const createDiscoveryAdapterFromStore: (store: SdkStore) => DiscoveryAdapter;
148
+ declare const createStorageAdapterFromStore: (store: SdkStore) => StorageAdapter;
149
+ declare const createProviderRegistryFromStore: (store: SdkStore) => ProviderRegistry;
150
+
151
+ export { type ListUsageTrackingOptions as L, type SdkStore as S, type UsageTrackingDriver as U, type StorageDriver as a, type UsageTrackingEntry as b, createDiscoveryAdapterFromStore as c, createProviderRegistryFromStore as d, createSdkStore as e, createStorageAdapterFromStore as f };
@@ -1,5 +1,5 @@
1
- import { W as WalletAdapter, S as StorageAdapter, P as ProviderRegistry } from '../interfaces-C6Dr6hKy.mjs';
2
- export { A as ApiKeyEntry, C as ChildKeyEntry, R as RoutstrClientOptions, a as StreamingCallbacks } from '../interfaces-C6Dr6hKy.mjs';
1
+ import { W as WalletAdapter, S as StorageAdapter, P as ProviderRegistry } from '../interfaces-C5fLD3jB.mjs';
2
+ export { A as ApiKeyEntry, C as ChildKeyEntry, R as RoutstrClientOptions, a as StreamingCallbacks, X as XCashuTokenEntry } from '../interfaces-C5fLD3jB.mjs';
3
3
  import { R as RefundResult, m as TopUpResult, S as SpendResult } from '../types-BYj_8c5c.mjs';
4
4
  export { P as ProviderInfo } from '../types-BYj_8c5c.mjs';
5
5
 
@@ -15,17 +15,6 @@ export { P as ProviderInfo } from '../types-BYj_8c5c.mjs';
15
15
  * Extracted from utils/cashuUtils.ts
16
16
  */
17
17
 
18
- /**
19
- * Options for refunding tokens
20
- */
21
- interface RefundOptions {
22
- /** The mint URL (for NIP-60 wallet operations) */
23
- mintUrl: string;
24
- /** The provider base URL */
25
- baseUrl: string;
26
- /** Optional specific token to refund (if not provided, uses stored token) */
27
- token?: string;
28
- }
29
18
  /**
30
19
  * Options for refunding API key balance
31
20
  */
@@ -82,10 +71,6 @@ declare class BalanceManager {
82
71
  private cashuSpender;
83
72
  constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry?: ProviderRegistry | undefined, cashuSpender?: CashuSpender);
84
73
  getBalanceState(): Promise<BalanceState>;
85
- /**
86
- * Unified refund - handles both NIP-60 and legacy wallet refunds
87
- */
88
- refund(options: RefundOptions): Promise<RefundResult>;
89
74
  /**
90
75
  * Refund API key balance - convert remaining API key balance to cashu token
91
76
  * @param options - Refund options including forceRefund flag
@@ -103,10 +88,6 @@ declare class BalanceManager {
103
88
  createProviderToken(options: CreateProviderTokenOptions): Promise<ProviderTokenResult>;
104
89
  private _selectCandidateMints;
105
90
  private _refundOtherProvidersForTopUp;
106
- /**
107
- * Fetch refund token from provider API
108
- */
109
- private _fetchRefundToken;
110
91
  /**
111
92
  * Post topup request to provider API
112
93
  */
@@ -209,13 +190,26 @@ declare class CashuSpender {
209
190
  */
210
191
  private _spendInternal;
211
192
  /**
212
- * Try to reuse an existing token
193
+ * Try to reuse an existing API key
213
194
  */
214
195
  private _tryReuseToken;
196
+ /**
197
+ * Refund all xcashu tokens from storage and increment tryCounts on failure.
198
+ * Reuses receiveToken from BalanceManager/CashuSpender for receiving refunds.
199
+ * @param mintUrl - The mint URL for receiving tokens
200
+ * @param excludeBaseUrls - Base URLs to exclude from refund (optional)
201
+ * @returns Results for each xcashu token refund attempt
202
+ */
203
+ refundXcashuTokens(mintUrl: string, excludeBaseUrls?: string[]): Promise<{
204
+ baseUrl: string;
205
+ token: string;
206
+ success: boolean;
207
+ error?: string;
208
+ }[]>;
215
209
  /**
216
210
  * Refund specific providers without retrying spend
217
211
  */
218
- refundProviders(baseUrls: string[], mintUrl: string, refundApiKeys?: boolean, forceRefund?: boolean): Promise<{
212
+ refundProviders(mintUrl: string, forceRefund?: boolean): Promise<{
219
213
  baseUrl: string;
220
214
  success: boolean;
221
215
  }[]>;
@@ -226,4 +220,4 @@ declare class CashuSpender {
226
220
  private _getProviderTokenBalance;
227
221
  }
228
222
 
229
- export { BalanceManager, type BalanceState, CashuSpender, type CreateProviderTokenOptions, ProviderRegistry, type ProviderTokenResult, type RefundApiKeyOptions, type RefundOptions, type SpendOptions, StorageAdapter, type TopUpOptions, WalletAdapter };
223
+ export { BalanceManager, type BalanceState, CashuSpender, type CreateProviderTokenOptions, ProviderRegistry, type ProviderTokenResult, type RefundApiKeyOptions, type SpendOptions, StorageAdapter, type TopUpOptions, WalletAdapter };
@@ -1,5 +1,5 @@
1
- import { W as WalletAdapter, S as StorageAdapter, P as ProviderRegistry } from '../interfaces-CluftN4z.js';
2
- export { A as ApiKeyEntry, C as ChildKeyEntry, R as RoutstrClientOptions, a as StreamingCallbacks } from '../interfaces-CluftN4z.js';
1
+ import { W as WalletAdapter, S as StorageAdapter, P as ProviderRegistry } from '../interfaces-B62Rw-dd.js';
2
+ export { A as ApiKeyEntry, C as ChildKeyEntry, R as RoutstrClientOptions, a as StreamingCallbacks, X as XCashuTokenEntry } from '../interfaces-B62Rw-dd.js';
3
3
  import { R as RefundResult, m as TopUpResult, S as SpendResult } from '../types-BYj_8c5c.js';
4
4
  export { P as ProviderInfo } from '../types-BYj_8c5c.js';
5
5
 
@@ -15,17 +15,6 @@ export { P as ProviderInfo } from '../types-BYj_8c5c.js';
15
15
  * Extracted from utils/cashuUtils.ts
16
16
  */
17
17
 
18
- /**
19
- * Options for refunding tokens
20
- */
21
- interface RefundOptions {
22
- /** The mint URL (for NIP-60 wallet operations) */
23
- mintUrl: string;
24
- /** The provider base URL */
25
- baseUrl: string;
26
- /** Optional specific token to refund (if not provided, uses stored token) */
27
- token?: string;
28
- }
29
18
  /**
30
19
  * Options for refunding API key balance
31
20
  */
@@ -82,10 +71,6 @@ declare class BalanceManager {
82
71
  private cashuSpender;
83
72
  constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry?: ProviderRegistry | undefined, cashuSpender?: CashuSpender);
84
73
  getBalanceState(): Promise<BalanceState>;
85
- /**
86
- * Unified refund - handles both NIP-60 and legacy wallet refunds
87
- */
88
- refund(options: RefundOptions): Promise<RefundResult>;
89
74
  /**
90
75
  * Refund API key balance - convert remaining API key balance to cashu token
91
76
  * @param options - Refund options including forceRefund flag
@@ -103,10 +88,6 @@ declare class BalanceManager {
103
88
  createProviderToken(options: CreateProviderTokenOptions): Promise<ProviderTokenResult>;
104
89
  private _selectCandidateMints;
105
90
  private _refundOtherProvidersForTopUp;
106
- /**
107
- * Fetch refund token from provider API
108
- */
109
- private _fetchRefundToken;
110
91
  /**
111
92
  * Post topup request to provider API
112
93
  */
@@ -209,13 +190,26 @@ declare class CashuSpender {
209
190
  */
210
191
  private _spendInternal;
211
192
  /**
212
- * Try to reuse an existing token
193
+ * Try to reuse an existing API key
213
194
  */
214
195
  private _tryReuseToken;
196
+ /**
197
+ * Refund all xcashu tokens from storage and increment tryCounts on failure.
198
+ * Reuses receiveToken from BalanceManager/CashuSpender for receiving refunds.
199
+ * @param mintUrl - The mint URL for receiving tokens
200
+ * @param excludeBaseUrls - Base URLs to exclude from refund (optional)
201
+ * @returns Results for each xcashu token refund attempt
202
+ */
203
+ refundXcashuTokens(mintUrl: string, excludeBaseUrls?: string[]): Promise<{
204
+ baseUrl: string;
205
+ token: string;
206
+ success: boolean;
207
+ error?: string;
208
+ }[]>;
215
209
  /**
216
210
  * Refund specific providers without retrying spend
217
211
  */
218
- refundProviders(baseUrls: string[], mintUrl: string, refundApiKeys?: boolean, forceRefund?: boolean): Promise<{
212
+ refundProviders(mintUrl: string, forceRefund?: boolean): Promise<{
219
213
  baseUrl: string;
220
214
  success: boolean;
221
215
  }[]>;
@@ -226,4 +220,4 @@ declare class CashuSpender {
226
220
  private _getProviderTokenBalance;
227
221
  }
228
222
 
229
- export { BalanceManager, type BalanceState, CashuSpender, type CreateProviderTokenOptions, ProviderRegistry, type ProviderTokenResult, type RefundApiKeyOptions, type RefundOptions, type SpendOptions, StorageAdapter, type TopUpOptions, WalletAdapter };
223
+ export { BalanceManager, type BalanceState, CashuSpender, type CreateProviderTokenOptions, ProviderRegistry, type ProviderTokenResult, type RefundApiKeyOptions, type SpendOptions, StorageAdapter, type TopUpOptions, WalletAdapter };