@routstr/sdk 0.3.7 → 0.3.9

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/dist/client/index.d.mts +411 -0
  2. package/dist/client/index.d.ts +411 -0
  3. package/dist/client/index.js +4938 -0
  4. package/dist/client/index.js.map +1 -0
  5. package/dist/client/index.mjs +4932 -0
  6. package/dist/client/index.mjs.map +1 -0
  7. package/dist/discovery/index.d.mts +247 -0
  8. package/dist/discovery/index.d.ts +247 -0
  9. package/dist/discovery/index.js +894 -0
  10. package/dist/discovery/index.js.map +1 -0
  11. package/dist/discovery/index.mjs +891 -0
  12. package/dist/discovery/index.mjs.map +1 -0
  13. package/dist/index.d.mts +195 -0
  14. package/dist/index.d.ts +195 -0
  15. package/dist/index.js +6797 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/index.mjs +6746 -0
  18. package/dist/index.mjs.map +1 -0
  19. package/dist/interfaces-C-DYd9Jy.d.ts +176 -0
  20. package/dist/interfaces-Csn8Uq04.d.mts +176 -0
  21. package/dist/interfaces-Cv1k2EUK.d.mts +118 -0
  22. package/dist/interfaces-iL7CWeG5.d.ts +118 -0
  23. package/dist/storage/index.d.mts +113 -0
  24. package/dist/storage/index.d.ts +113 -0
  25. package/dist/storage/index.js +2019 -0
  26. package/dist/storage/index.js.map +1 -0
  27. package/dist/storage/index.mjs +1995 -0
  28. package/dist/storage/index.mjs.map +1 -0
  29. package/dist/store-58VcEUoA.d.ts +172 -0
  30. package/dist/store-C6dfj1cc.d.mts +172 -0
  31. package/dist/types-_21yYFZG.d.mts +234 -0
  32. package/dist/types-_21yYFZG.d.ts +234 -0
  33. package/dist/wallet/index.d.mts +249 -0
  34. package/dist/wallet/index.d.ts +249 -0
  35. package/dist/wallet/index.js +1383 -0
  36. package/dist/wallet/index.js.map +1 -0
  37. package/dist/wallet/index.mjs +1380 -0
  38. package/dist/wallet/index.mjs.map +1 -0
  39. package/package.json +3 -2
@@ -0,0 +1,247 @@
1
+ import { D as DiscoveryAdapter } from '../interfaces-Cv1k2EUK.mjs';
2
+ import { S as SdkLogger, e as Model, k as ProviderInfo } from '../types-_21yYFZG.mjs';
3
+ import { EventStore } from 'applesauce-core';
4
+
5
+ /**
6
+ * ModelManager class for discovering, fetching, and managing models from providers
7
+ * Core responsibility: fetching models from providers, caching them, and selecting the best option
8
+ * (lowest cost) across multiple providers
9
+ */
10
+
11
+ /**
12
+ * Configuration for ModelManager
13
+ */
14
+ interface ModelManagerConfig {
15
+ /** URL to fetch provider directory from */
16
+ providerDirectoryUrl?: string;
17
+ /** Additional provider base URLs to include */
18
+ includeProviderUrls?: string[];
19
+ /** Provider base URLs to exclude */
20
+ excludeProviderUrls?: string[];
21
+ /** Cache TTL in milliseconds (default: 210 minutes) */
22
+ cacheTTL?: number;
23
+ /** Nostr pubkey for routstr review/model events (kind 38425/38423). Defaults to routstr's key. */
24
+ routstrPubkey?: string;
25
+ /** Optional injectable logger */
26
+ logger?: SdkLogger;
27
+ /** Path to SQLite database for persistent Nostr event storage.
28
+ * If provided, events fetched by ModelManager from relays (kinds 38421,
29
+ * 38423, 38425) are persisted and survive process restarts. The underlying
30
+ * EventStore can also be accessed for advanced/manual event management. */
31
+ eventStoreDbPath?: string;
32
+ }
33
+ /**
34
+ * ModelManager handles all model discovery and caching logic
35
+ * Abstracts away storage details via DiscoveryAdapter
36
+ */
37
+ declare class ModelManager {
38
+ private adapter;
39
+ private readonly cacheTTL;
40
+ private readonly providerDirectoryUrl;
41
+ private readonly includeProviderUrls;
42
+ private readonly excludeProviderUrls;
43
+ private readonly routstrPubkey;
44
+ private readonly logger;
45
+ private providerNodePubkeysByUrl;
46
+ /** Persistent event store for relay-fetched events (null if not configured/initialized) */
47
+ private eventStore;
48
+ private eventStoreDb;
49
+ private eventStoreInitPromise;
50
+ private readonly eventStoreDbPath?;
51
+ constructor(adapter: DiscoveryAdapter, config?: ModelManagerConfig);
52
+ /**
53
+ * Get the list of bootstrapped provider base URLs
54
+ * @returns Array of provider base URLs
55
+ */
56
+ getBaseUrls(): string[];
57
+ /**
58
+ * Lazily initialize the persistent event store.
59
+ * Returns null if no eventStoreDbPath was provided.
60
+ */
61
+ private ensureEventStore;
62
+ /**
63
+ * Get the persistent event store, initializing it if configured.
64
+ * Returns null if no eventStoreDbPath was provided.
65
+ */
66
+ getEventStore(): Promise<EventStore | null>;
67
+ private createPersistentEventDatabase;
68
+ /** Close the persistent event store database handle, if configured. */
69
+ closeEventStore(): void;
70
+ private initializeEventStoreMetadata;
71
+ private markEventFetched;
72
+ private getEventFetchedAt;
73
+ /**
74
+ * Check the persistent event store for fresh cached events.
75
+ * Returns events from SQLite if they were fetched within `maxAge`, otherwise
76
+ * returns empty array (caller should hit relays). Events without local fetch
77
+ * metadata fall back to Nostr created_at for backwards compatibility.
78
+ */
79
+ private getCachedNostrEvents;
80
+ static init(adapter: DiscoveryAdapter, config?: ModelManagerConfig, options?: {
81
+ torMode?: boolean;
82
+ forceRefresh?: boolean;
83
+ }): Promise<ModelManager>;
84
+ /**
85
+ * Bootstrap provider list from the provider directory
86
+ * First tries to fetch from Nostr (kind 30421), falls back to HTTP
87
+ * @param torMode Whether running in Tor context
88
+ * @param forceRefresh Ignore provider cache and refresh provider sources
89
+ * @returns Array of provider base URLs
90
+ * @throws ProviderBootstrapError if all providers fail to fetch
91
+ */
92
+ bootstrapProviders(torMode?: boolean, forceRefresh?: boolean): Promise<string[]>;
93
+ /**
94
+ * Bootstrap providers from Nostr network (kind 38421)
95
+ * @param kind The Nostr kind to fetch
96
+ * @param torMode Whether running in Tor context
97
+ * @returns Array of provider base URLs
98
+ */
99
+ private bootstrapFromNostr;
100
+ /**
101
+ * Bootstrap providers from HTTP endpoint
102
+ * @param torMode Whether running in Tor context
103
+ * @param forceRefresh Ignore routstr21 cache and fetch fresh data
104
+ * @returns Array of provider base URLs
105
+ */
106
+ private bootstrapFromHttp;
107
+ /**
108
+ * Fetch Routstr review events from Nostr (kind 38425) and disable providers
109
+ * whose 38421 node pubkey does not have at least one review tagged `t=lgtm`.
110
+ *
111
+ * Review events are expected to have:
112
+ * - `node`: the reviewed 38421 provider event pubkey
113
+ * - `t`: review label, where `lgtm` means the node looks good
114
+ *
115
+ * @param baseUrls Current provider base URLs to evaluate
116
+ * @returns Array of provider base URLs disabled by the review set
117
+ */
118
+ syncReviewedProvidersFromNostr(baseUrls?: string[], providerNodes?: Map<string, Set<string>>, forceRefresh?: boolean): Promise<string[]>;
119
+ private addProviderNode;
120
+ /**
121
+ * Fetch models from all providers and select best-priced options
122
+ * Uses cache if available and not expired
123
+ * @param baseUrls List of provider base URLs to fetch from
124
+ * @param forceRefresh Ignore cache and fetch fresh data
125
+ * @param onProgress Callback fired after each provider completes with current combined models
126
+ * @returns Array of unique models with best prices selected
127
+ */
128
+ fetchModels(baseUrls: string[], forceRefresh?: boolean, onProgress?: (models: Model[]) => void): Promise<Model[]>;
129
+ /**
130
+ * Fetch models from a single provider
131
+ * @param baseUrl Provider base URL
132
+ * @returns Array of models from provider
133
+ */
134
+ private fetchModelsFromProvider;
135
+ private isProviderDownError;
136
+ /**
137
+ * Get all cached models from all providers
138
+ * @returns Record mapping baseUrl -> models
139
+ */
140
+ getAllCachedModels(): Record<string, Model[]>;
141
+ /**
142
+ * Clear cache for a specific provider
143
+ * @param baseUrl Provider base URL
144
+ */
145
+ clearProviderCache(baseUrl: string): void;
146
+ /**
147
+ * Clear all model caches
148
+ */
149
+ clearAllCache(): void;
150
+ /**
151
+ * Filter base URLs based on Tor context
152
+ * @param baseUrls Provider URLs to filter
153
+ * @param torMode Whether in Tor context
154
+ * @returns Filtered URLs appropriate for Tor mode
155
+ */
156
+ filterBaseUrlsForTor(baseUrls: string[], torMode: boolean): string[];
157
+ /**
158
+ * Get provider endpoints from provider info
159
+ * @param provider Provider object from directory
160
+ * @param torMode Whether in Tor context
161
+ * @returns Array of endpoint URLs
162
+ */
163
+ private getProviderEndpoints;
164
+ /**
165
+ * Normalize provider URL with trailing slash
166
+ * @param url URL to normalize
167
+ * @returns Normalized URL
168
+ */
169
+ private normalizeUrl;
170
+ /**
171
+ * Fetch routstr21 models from Nostr network (kind 38423)
172
+ * Uses cache if available and not expired
173
+ * @returns Array of model IDs or empty array if not found
174
+ */
175
+ fetchRoutstr21Models(forceRefresh?: boolean): Promise<string[]>;
176
+ }
177
+
178
+ /**
179
+ * MintDiscovery class for discovering mints and provider info
180
+ * Core responsibility: fetching mint information from providers and caching it
181
+ */
182
+
183
+ /**
184
+ * Configuration for MintDiscovery
185
+ */
186
+ interface MintDiscoveryConfig {
187
+ /** Cache TTL in milliseconds (default: 21 minutes) */
188
+ cacheTTL?: number;
189
+ /** Optional injectable logger */
190
+ logger?: SdkLogger;
191
+ }
192
+ /**
193
+ * MintDiscovery handles mint and provider info discovery
194
+ * Abstracts away storage details via DiscoveryAdapter
195
+ */
196
+ declare class MintDiscovery {
197
+ private adapter;
198
+ private readonly cacheTTL;
199
+ private readonly logger;
200
+ constructor(adapter: DiscoveryAdapter, config?: MintDiscoveryConfig);
201
+ /**
202
+ * Fetch mints from all providers via their /v1/info endpoints
203
+ * Caches mints and full provider info for later access
204
+ * @param baseUrls List of provider base URLs to fetch from
205
+ * @returns Object with mints and provider info from all providers
206
+ */
207
+ discoverMints(baseUrls: string[], options?: {
208
+ forceRefresh?: boolean;
209
+ }): Promise<{
210
+ mintsFromProviders: Record<string, string[]>;
211
+ infoFromProviders: Record<string, ProviderInfo>;
212
+ }>;
213
+ /**
214
+ * Get cached mints from all providers
215
+ * @returns Record mapping baseUrl -> mint URLs
216
+ */
217
+ getCachedMints(): Record<string, string[]>;
218
+ /**
219
+ * Get cached provider info from all providers
220
+ * @returns Record mapping baseUrl -> provider info
221
+ */
222
+ getCachedProviderInfo(): Record<string, ProviderInfo>;
223
+ /**
224
+ * Get mints for a specific provider
225
+ * @param baseUrl Provider base URL
226
+ * @returns Array of mint URLs for the provider
227
+ */
228
+ getProviderMints(baseUrl: string): string[];
229
+ /**
230
+ * Get info for a specific provider
231
+ * @param baseUrl Provider base URL
232
+ * @returns Provider info object or null if not found
233
+ */
234
+ getProviderInfo(baseUrl: string): ProviderInfo | null;
235
+ /**
236
+ * Clear mint cache for a specific provider
237
+ * @param baseUrl Provider base URL
238
+ */
239
+ clearProviderMintCache(baseUrl: string): void;
240
+ /**
241
+ * Clear all mint caches
242
+ */
243
+ clearAllCache(): void;
244
+ private isProviderDownError;
245
+ }
246
+
247
+ export { DiscoveryAdapter, MintDiscovery, ModelManager, type ModelManagerConfig, ProviderInfo };
@@ -0,0 +1,247 @@
1
+ import { D as DiscoveryAdapter } from '../interfaces-iL7CWeG5.js';
2
+ import { S as SdkLogger, e as Model, k as ProviderInfo } from '../types-_21yYFZG.js';
3
+ import { EventStore } from 'applesauce-core';
4
+
5
+ /**
6
+ * ModelManager class for discovering, fetching, and managing models from providers
7
+ * Core responsibility: fetching models from providers, caching them, and selecting the best option
8
+ * (lowest cost) across multiple providers
9
+ */
10
+
11
+ /**
12
+ * Configuration for ModelManager
13
+ */
14
+ interface ModelManagerConfig {
15
+ /** URL to fetch provider directory from */
16
+ providerDirectoryUrl?: string;
17
+ /** Additional provider base URLs to include */
18
+ includeProviderUrls?: string[];
19
+ /** Provider base URLs to exclude */
20
+ excludeProviderUrls?: string[];
21
+ /** Cache TTL in milliseconds (default: 210 minutes) */
22
+ cacheTTL?: number;
23
+ /** Nostr pubkey for routstr review/model events (kind 38425/38423). Defaults to routstr's key. */
24
+ routstrPubkey?: string;
25
+ /** Optional injectable logger */
26
+ logger?: SdkLogger;
27
+ /** Path to SQLite database for persistent Nostr event storage.
28
+ * If provided, events fetched by ModelManager from relays (kinds 38421,
29
+ * 38423, 38425) are persisted and survive process restarts. The underlying
30
+ * EventStore can also be accessed for advanced/manual event management. */
31
+ eventStoreDbPath?: string;
32
+ }
33
+ /**
34
+ * ModelManager handles all model discovery and caching logic
35
+ * Abstracts away storage details via DiscoveryAdapter
36
+ */
37
+ declare class ModelManager {
38
+ private adapter;
39
+ private readonly cacheTTL;
40
+ private readonly providerDirectoryUrl;
41
+ private readonly includeProviderUrls;
42
+ private readonly excludeProviderUrls;
43
+ private readonly routstrPubkey;
44
+ private readonly logger;
45
+ private providerNodePubkeysByUrl;
46
+ /** Persistent event store for relay-fetched events (null if not configured/initialized) */
47
+ private eventStore;
48
+ private eventStoreDb;
49
+ private eventStoreInitPromise;
50
+ private readonly eventStoreDbPath?;
51
+ constructor(adapter: DiscoveryAdapter, config?: ModelManagerConfig);
52
+ /**
53
+ * Get the list of bootstrapped provider base URLs
54
+ * @returns Array of provider base URLs
55
+ */
56
+ getBaseUrls(): string[];
57
+ /**
58
+ * Lazily initialize the persistent event store.
59
+ * Returns null if no eventStoreDbPath was provided.
60
+ */
61
+ private ensureEventStore;
62
+ /**
63
+ * Get the persistent event store, initializing it if configured.
64
+ * Returns null if no eventStoreDbPath was provided.
65
+ */
66
+ getEventStore(): Promise<EventStore | null>;
67
+ private createPersistentEventDatabase;
68
+ /** Close the persistent event store database handle, if configured. */
69
+ closeEventStore(): void;
70
+ private initializeEventStoreMetadata;
71
+ private markEventFetched;
72
+ private getEventFetchedAt;
73
+ /**
74
+ * Check the persistent event store for fresh cached events.
75
+ * Returns events from SQLite if they were fetched within `maxAge`, otherwise
76
+ * returns empty array (caller should hit relays). Events without local fetch
77
+ * metadata fall back to Nostr created_at for backwards compatibility.
78
+ */
79
+ private getCachedNostrEvents;
80
+ static init(adapter: DiscoveryAdapter, config?: ModelManagerConfig, options?: {
81
+ torMode?: boolean;
82
+ forceRefresh?: boolean;
83
+ }): Promise<ModelManager>;
84
+ /**
85
+ * Bootstrap provider list from the provider directory
86
+ * First tries to fetch from Nostr (kind 30421), falls back to HTTP
87
+ * @param torMode Whether running in Tor context
88
+ * @param forceRefresh Ignore provider cache and refresh provider sources
89
+ * @returns Array of provider base URLs
90
+ * @throws ProviderBootstrapError if all providers fail to fetch
91
+ */
92
+ bootstrapProviders(torMode?: boolean, forceRefresh?: boolean): Promise<string[]>;
93
+ /**
94
+ * Bootstrap providers from Nostr network (kind 38421)
95
+ * @param kind The Nostr kind to fetch
96
+ * @param torMode Whether running in Tor context
97
+ * @returns Array of provider base URLs
98
+ */
99
+ private bootstrapFromNostr;
100
+ /**
101
+ * Bootstrap providers from HTTP endpoint
102
+ * @param torMode Whether running in Tor context
103
+ * @param forceRefresh Ignore routstr21 cache and fetch fresh data
104
+ * @returns Array of provider base URLs
105
+ */
106
+ private bootstrapFromHttp;
107
+ /**
108
+ * Fetch Routstr review events from Nostr (kind 38425) and disable providers
109
+ * whose 38421 node pubkey does not have at least one review tagged `t=lgtm`.
110
+ *
111
+ * Review events are expected to have:
112
+ * - `node`: the reviewed 38421 provider event pubkey
113
+ * - `t`: review label, where `lgtm` means the node looks good
114
+ *
115
+ * @param baseUrls Current provider base URLs to evaluate
116
+ * @returns Array of provider base URLs disabled by the review set
117
+ */
118
+ syncReviewedProvidersFromNostr(baseUrls?: string[], providerNodes?: Map<string, Set<string>>, forceRefresh?: boolean): Promise<string[]>;
119
+ private addProviderNode;
120
+ /**
121
+ * Fetch models from all providers and select best-priced options
122
+ * Uses cache if available and not expired
123
+ * @param baseUrls List of provider base URLs to fetch from
124
+ * @param forceRefresh Ignore cache and fetch fresh data
125
+ * @param onProgress Callback fired after each provider completes with current combined models
126
+ * @returns Array of unique models with best prices selected
127
+ */
128
+ fetchModels(baseUrls: string[], forceRefresh?: boolean, onProgress?: (models: Model[]) => void): Promise<Model[]>;
129
+ /**
130
+ * Fetch models from a single provider
131
+ * @param baseUrl Provider base URL
132
+ * @returns Array of models from provider
133
+ */
134
+ private fetchModelsFromProvider;
135
+ private isProviderDownError;
136
+ /**
137
+ * Get all cached models from all providers
138
+ * @returns Record mapping baseUrl -> models
139
+ */
140
+ getAllCachedModels(): Record<string, Model[]>;
141
+ /**
142
+ * Clear cache for a specific provider
143
+ * @param baseUrl Provider base URL
144
+ */
145
+ clearProviderCache(baseUrl: string): void;
146
+ /**
147
+ * Clear all model caches
148
+ */
149
+ clearAllCache(): void;
150
+ /**
151
+ * Filter base URLs based on Tor context
152
+ * @param baseUrls Provider URLs to filter
153
+ * @param torMode Whether in Tor context
154
+ * @returns Filtered URLs appropriate for Tor mode
155
+ */
156
+ filterBaseUrlsForTor(baseUrls: string[], torMode: boolean): string[];
157
+ /**
158
+ * Get provider endpoints from provider info
159
+ * @param provider Provider object from directory
160
+ * @param torMode Whether in Tor context
161
+ * @returns Array of endpoint URLs
162
+ */
163
+ private getProviderEndpoints;
164
+ /**
165
+ * Normalize provider URL with trailing slash
166
+ * @param url URL to normalize
167
+ * @returns Normalized URL
168
+ */
169
+ private normalizeUrl;
170
+ /**
171
+ * Fetch routstr21 models from Nostr network (kind 38423)
172
+ * Uses cache if available and not expired
173
+ * @returns Array of model IDs or empty array if not found
174
+ */
175
+ fetchRoutstr21Models(forceRefresh?: boolean): Promise<string[]>;
176
+ }
177
+
178
+ /**
179
+ * MintDiscovery class for discovering mints and provider info
180
+ * Core responsibility: fetching mint information from providers and caching it
181
+ */
182
+
183
+ /**
184
+ * Configuration for MintDiscovery
185
+ */
186
+ interface MintDiscoveryConfig {
187
+ /** Cache TTL in milliseconds (default: 21 minutes) */
188
+ cacheTTL?: number;
189
+ /** Optional injectable logger */
190
+ logger?: SdkLogger;
191
+ }
192
+ /**
193
+ * MintDiscovery handles mint and provider info discovery
194
+ * Abstracts away storage details via DiscoveryAdapter
195
+ */
196
+ declare class MintDiscovery {
197
+ private adapter;
198
+ private readonly cacheTTL;
199
+ private readonly logger;
200
+ constructor(adapter: DiscoveryAdapter, config?: MintDiscoveryConfig);
201
+ /**
202
+ * Fetch mints from all providers via their /v1/info endpoints
203
+ * Caches mints and full provider info for later access
204
+ * @param baseUrls List of provider base URLs to fetch from
205
+ * @returns Object with mints and provider info from all providers
206
+ */
207
+ discoverMints(baseUrls: string[], options?: {
208
+ forceRefresh?: boolean;
209
+ }): Promise<{
210
+ mintsFromProviders: Record<string, string[]>;
211
+ infoFromProviders: Record<string, ProviderInfo>;
212
+ }>;
213
+ /**
214
+ * Get cached mints from all providers
215
+ * @returns Record mapping baseUrl -> mint URLs
216
+ */
217
+ getCachedMints(): Record<string, string[]>;
218
+ /**
219
+ * Get cached provider info from all providers
220
+ * @returns Record mapping baseUrl -> provider info
221
+ */
222
+ getCachedProviderInfo(): Record<string, ProviderInfo>;
223
+ /**
224
+ * Get mints for a specific provider
225
+ * @param baseUrl Provider base URL
226
+ * @returns Array of mint URLs for the provider
227
+ */
228
+ getProviderMints(baseUrl: string): string[];
229
+ /**
230
+ * Get info for a specific provider
231
+ * @param baseUrl Provider base URL
232
+ * @returns Provider info object or null if not found
233
+ */
234
+ getProviderInfo(baseUrl: string): ProviderInfo | null;
235
+ /**
236
+ * Clear mint cache for a specific provider
237
+ * @param baseUrl Provider base URL
238
+ */
239
+ clearProviderMintCache(baseUrl: string): void;
240
+ /**
241
+ * Clear all mint caches
242
+ */
243
+ clearAllCache(): void;
244
+ private isProviderDownError;
245
+ }
246
+
247
+ export { DiscoveryAdapter, MintDiscovery, ModelManager, type ModelManagerConfig, ProviderInfo };