@routstr/sdk 0.3.7 → 0.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/index.d.mts +411 -0
- package/dist/client/index.d.ts +411 -0
- package/dist/client/index.js +4883 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/index.mjs +4877 -0
- package/dist/client/index.mjs.map +1 -0
- package/dist/discovery/index.d.mts +213 -0
- package/dist/discovery/index.d.ts +213 -0
- package/dist/discovery/index.js +738 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/index.mjs +735 -0
- package/dist/discovery/index.mjs.map +1 -0
- package/dist/index.d.mts +194 -0
- package/dist/index.d.ts +194 -0
- package/dist/index.js +6307 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +6258 -0
- package/dist/index.mjs.map +1 -0
- package/dist/interfaces-C-DYd9Jy.d.ts +176 -0
- package/dist/interfaces-Csn8Uq04.d.mts +176 -0
- package/dist/interfaces-Cv1k2EUK.d.mts +118 -0
- package/dist/interfaces-iL7CWeG5.d.ts +118 -0
- package/dist/storage/index.d.mts +87 -0
- package/dist/storage/index.d.ts +87 -0
- package/dist/storage/index.js +1740 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/index.mjs +1718 -0
- package/dist/storage/index.mjs.map +1 -0
- package/dist/store-58VcEUoA.d.ts +172 -0
- package/dist/store-C6dfj1cc.d.mts +172 -0
- package/dist/types-_21yYFZG.d.mts +234 -0
- package/dist/types-_21yYFZG.d.ts +234 -0
- package/dist/wallet/index.d.mts +245 -0
- package/dist/wallet/index.d.ts +245 -0
- package/dist/wallet/index.js +1376 -0
- package/dist/wallet/index.js.map +1 -0
- package/dist/wallet/index.mjs +1373 -0
- package/dist/wallet/index.mjs.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,738 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var applesauceRelay = require('applesauce-relay');
|
|
4
|
+
var applesauceCore = require('applesauce-core');
|
|
5
|
+
var rxjs = require('rxjs');
|
|
6
|
+
|
|
7
|
+
// core/types.ts
|
|
8
|
+
function makeConsoleLogger(prefix) {
|
|
9
|
+
const fmt = (args) => prefix ? [prefix, ...args] : args;
|
|
10
|
+
return {
|
|
11
|
+
log: (...args) => console.log(...fmt(args)),
|
|
12
|
+
warn: (...args) => console.warn(...fmt(args)),
|
|
13
|
+
error: (...args) => console.error(...fmt(args)),
|
|
14
|
+
debug: (...args) => console.log(...fmt(args)),
|
|
15
|
+
child: (p) => makeConsoleLogger(prefix ? `${prefix}:${p}` : p)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
var consoleLogger = makeConsoleLogger();
|
|
19
|
+
|
|
20
|
+
// core/errors.ts
|
|
21
|
+
var ProviderBootstrapError = class extends Error {
|
|
22
|
+
constructor(failedProviders, message) {
|
|
23
|
+
super(
|
|
24
|
+
message || `Failed to bootstrap providers. Tried: ${failedProviders.join(", ")}`
|
|
25
|
+
);
|
|
26
|
+
this.failedProviders = failedProviders;
|
|
27
|
+
this.name = "ProviderBootstrapError";
|
|
28
|
+
}
|
|
29
|
+
failedProviders;
|
|
30
|
+
};
|
|
31
|
+
var NoProvidersAvailableError = class extends Error {
|
|
32
|
+
constructor() {
|
|
33
|
+
super("No providers are available for model discovery");
|
|
34
|
+
this.name = "NoProvidersAvailableError";
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var ModelManager = class _ModelManager {
|
|
38
|
+
constructor(adapter, config = {}) {
|
|
39
|
+
this.adapter = adapter;
|
|
40
|
+
this.providerDirectoryUrl = config.providerDirectoryUrl || "https://api.routstr.com/v1/providers/";
|
|
41
|
+
this.cacheTTL = config.cacheTTL || 210 * 60 * 1e3;
|
|
42
|
+
this.includeProviderUrls = config.includeProviderUrls || [];
|
|
43
|
+
this.excludeProviderUrls = config.excludeProviderUrls || [];
|
|
44
|
+
this.routstrPubkey = config.routstrPubkey || "4ad6fa2d16e2a9b576c863b4cf7404a70d4dc320c0c447d10ad6ff58993eacc8";
|
|
45
|
+
this.logger = (config.logger ?? consoleLogger).child("ModelManager");
|
|
46
|
+
}
|
|
47
|
+
adapter;
|
|
48
|
+
cacheTTL;
|
|
49
|
+
providerDirectoryUrl;
|
|
50
|
+
includeProviderUrls;
|
|
51
|
+
excludeProviderUrls;
|
|
52
|
+
routstrPubkey;
|
|
53
|
+
logger;
|
|
54
|
+
providerNodePubkeysByUrl = /* @__PURE__ */ new Map();
|
|
55
|
+
/**
|
|
56
|
+
* Get the list of bootstrapped provider base URLs
|
|
57
|
+
* @returns Array of provider base URLs
|
|
58
|
+
*/
|
|
59
|
+
getBaseUrls() {
|
|
60
|
+
return this.adapter.getBaseUrlsList();
|
|
61
|
+
}
|
|
62
|
+
static async init(adapter, config = {}, options = {}) {
|
|
63
|
+
const manager = new _ModelManager(adapter, config);
|
|
64
|
+
const torMode = options.torMode ?? false;
|
|
65
|
+
const forceRefresh = options.forceRefresh ?? false;
|
|
66
|
+
const providers = await manager.bootstrapProviders(torMode, forceRefresh);
|
|
67
|
+
await manager.fetchModels(providers, forceRefresh);
|
|
68
|
+
return manager;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Bootstrap provider list from the provider directory
|
|
72
|
+
* First tries to fetch from Nostr (kind 30421), falls back to HTTP
|
|
73
|
+
* @param torMode Whether running in Tor context
|
|
74
|
+
* @param forceRefresh Ignore provider cache and refresh provider sources
|
|
75
|
+
* @returns Array of provider base URLs
|
|
76
|
+
* @throws ProviderBootstrapError if all providers fail to fetch
|
|
77
|
+
*/
|
|
78
|
+
async bootstrapProviders(torMode = false, forceRefresh = false) {
|
|
79
|
+
if (!forceRefresh) {
|
|
80
|
+
const cachedUrls = this.adapter.getBaseUrlsList();
|
|
81
|
+
if (cachedUrls.length > 0) {
|
|
82
|
+
const lastUpdate = this.adapter.getBaseUrlsLastUpdate();
|
|
83
|
+
const cacheValid = lastUpdate && Date.now() - lastUpdate <= this.cacheTTL;
|
|
84
|
+
if (cacheValid) {
|
|
85
|
+
const filteredCachedUrls = this.filterBaseUrlsForTor(
|
|
86
|
+
cachedUrls,
|
|
87
|
+
torMode
|
|
88
|
+
);
|
|
89
|
+
await this.fetchRoutstr21Models(forceRefresh);
|
|
90
|
+
await this.syncReviewedProvidersFromNostr(filteredCachedUrls);
|
|
91
|
+
return filteredCachedUrls;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const nostrProviders = await this.bootstrapFromNostr(38421, torMode);
|
|
97
|
+
if (nostrProviders.length > 0) {
|
|
98
|
+
const filtered = this.filterBaseUrlsForTor(nostrProviders, torMode);
|
|
99
|
+
this.adapter.setBaseUrlsList(filtered);
|
|
100
|
+
this.adapter.setBaseUrlsLastUpdate(Date.now());
|
|
101
|
+
await this.fetchRoutstr21Models(forceRefresh);
|
|
102
|
+
await this.syncReviewedProvidersFromNostr(filtered);
|
|
103
|
+
return filtered;
|
|
104
|
+
}
|
|
105
|
+
} catch (e) {
|
|
106
|
+
this.logger.warn("Nostr bootstrap failed, falling back to HTTP:", e);
|
|
107
|
+
}
|
|
108
|
+
return this.bootstrapFromHttp(torMode, forceRefresh);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Bootstrap providers from Nostr network (kind 30421)
|
|
112
|
+
* @param kind The Nostr kind to fetch
|
|
113
|
+
* @param torMode Whether running in Tor context
|
|
114
|
+
* @returns Array of provider base URLs
|
|
115
|
+
*/
|
|
116
|
+
async bootstrapFromNostr(kind, torMode) {
|
|
117
|
+
const DEFAULT_RELAYS = [
|
|
118
|
+
"wss://relay.primal.net",
|
|
119
|
+
"wss://nos.lol",
|
|
120
|
+
"wss://relay.damus.io"
|
|
121
|
+
];
|
|
122
|
+
const pool = new applesauceRelay.RelayPool();
|
|
123
|
+
const localEventStore = new applesauceCore.EventStore();
|
|
124
|
+
const timeoutMs = 5e3;
|
|
125
|
+
await new Promise((resolve) => {
|
|
126
|
+
pool.req(DEFAULT_RELAYS, {
|
|
127
|
+
kinds: [kind],
|
|
128
|
+
limit: 100
|
|
129
|
+
}).pipe(
|
|
130
|
+
applesauceRelay.onlyEvents(),
|
|
131
|
+
rxjs.tap((event) => {
|
|
132
|
+
localEventStore.add(event);
|
|
133
|
+
})
|
|
134
|
+
).subscribe({
|
|
135
|
+
complete: () => {
|
|
136
|
+
resolve();
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
setTimeout(() => {
|
|
140
|
+
resolve();
|
|
141
|
+
}, timeoutMs);
|
|
142
|
+
});
|
|
143
|
+
const timeline = localEventStore.getTimeline({ kinds: [kind] });
|
|
144
|
+
const bases = /* @__PURE__ */ new Set();
|
|
145
|
+
this.providerNodePubkeysByUrl = /* @__PURE__ */ new Map();
|
|
146
|
+
for (const event of timeline) {
|
|
147
|
+
const eventUrls = [];
|
|
148
|
+
for (const tag of event.tags) {
|
|
149
|
+
if (tag[0] === "u" && typeof tag[1] === "string") {
|
|
150
|
+
eventUrls.push(tag[1]);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (eventUrls.length > 0) {
|
|
154
|
+
for (const url of eventUrls) {
|
|
155
|
+
const normalized = this.normalizeUrl(url);
|
|
156
|
+
if (!torMode || normalized.includes(".onion")) {
|
|
157
|
+
bases.add(normalized);
|
|
158
|
+
this.addProviderNode(
|
|
159
|
+
this.providerNodePubkeysByUrl,
|
|
160
|
+
normalized,
|
|
161
|
+
event.pubkey
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
const content = JSON.parse(event.content);
|
|
169
|
+
const providers = Array.isArray(content) ? content : content.providers || [];
|
|
170
|
+
for (const p of providers) {
|
|
171
|
+
const endpoints = this.getProviderEndpoints(p, torMode);
|
|
172
|
+
for (const endpoint of endpoints) {
|
|
173
|
+
bases.add(endpoint);
|
|
174
|
+
this.addProviderNode(
|
|
175
|
+
this.providerNodePubkeysByUrl,
|
|
176
|
+
endpoint,
|
|
177
|
+
p?.pubkey || event.pubkey
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
} catch {
|
|
182
|
+
try {
|
|
183
|
+
const providers = JSON.parse(event.content);
|
|
184
|
+
if (Array.isArray(providers)) {
|
|
185
|
+
for (const p of providers) {
|
|
186
|
+
const endpoints = this.getProviderEndpoints(p, torMode);
|
|
187
|
+
for (const endpoint of endpoints) {
|
|
188
|
+
bases.add(endpoint);
|
|
189
|
+
this.addProviderNode(
|
|
190
|
+
this.providerNodePubkeysByUrl,
|
|
191
|
+
endpoint,
|
|
192
|
+
p?.pubkey || event.pubkey
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
} catch {
|
|
198
|
+
this.logger.warn(
|
|
199
|
+
"NostrBootstrap: failed to parse event content:",
|
|
200
|
+
event.id
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
for (const url of this.includeProviderUrls) {
|
|
206
|
+
const normalized = this.normalizeUrl(url);
|
|
207
|
+
if (!torMode || normalized.includes(".onion")) {
|
|
208
|
+
bases.add(normalized);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const excluded = new Set(
|
|
212
|
+
this.excludeProviderUrls.map((url) => this.normalizeUrl(url))
|
|
213
|
+
);
|
|
214
|
+
const result = Array.from(bases).filter((base) => !excluded.has(base));
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Bootstrap providers from HTTP endpoint
|
|
219
|
+
* @param torMode Whether running in Tor context
|
|
220
|
+
* @param forceRefresh Ignore routstr21 cache and fetch fresh data
|
|
221
|
+
* @returns Array of provider base URLs
|
|
222
|
+
*/
|
|
223
|
+
async bootstrapFromHttp(torMode, forceRefresh = false) {
|
|
224
|
+
try {
|
|
225
|
+
const res = await fetch(this.providerDirectoryUrl);
|
|
226
|
+
if (!res.ok) {
|
|
227
|
+
throw new Error(`Failed to fetch providers: ${res.status}`);
|
|
228
|
+
}
|
|
229
|
+
const data = await res.json();
|
|
230
|
+
const providers = Array.isArray(data?.providers) ? data.providers : [];
|
|
231
|
+
const bases = /* @__PURE__ */ new Set();
|
|
232
|
+
this.providerNodePubkeysByUrl = /* @__PURE__ */ new Map();
|
|
233
|
+
for (const p of providers) {
|
|
234
|
+
const endpoints = this.getProviderEndpoints(p, torMode);
|
|
235
|
+
for (const endpoint of endpoints) {
|
|
236
|
+
bases.add(endpoint);
|
|
237
|
+
this.addProviderNode(this.providerNodePubkeysByUrl, endpoint, p?.pubkey);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
for (const url of this.includeProviderUrls) {
|
|
241
|
+
const normalized = this.normalizeUrl(url);
|
|
242
|
+
if (!torMode || normalized.includes(".onion")) {
|
|
243
|
+
bases.add(normalized);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const excluded = new Set(
|
|
247
|
+
this.excludeProviderUrls.map((url) => this.normalizeUrl(url))
|
|
248
|
+
);
|
|
249
|
+
const list = Array.from(bases).filter((base) => !excluded.has(base));
|
|
250
|
+
if (list.length > 0) {
|
|
251
|
+
this.adapter.setBaseUrlsList(list);
|
|
252
|
+
this.adapter.setBaseUrlsLastUpdate(Date.now());
|
|
253
|
+
await this.fetchRoutstr21Models(forceRefresh);
|
|
254
|
+
await this.syncReviewedProvidersFromNostr(list);
|
|
255
|
+
}
|
|
256
|
+
return list;
|
|
257
|
+
} catch (e) {
|
|
258
|
+
this.logger.error("Failed to bootstrap providers", e);
|
|
259
|
+
throw new ProviderBootstrapError([], `Provider bootstrap failed: ${e}`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Fetch Routstr review events from Nostr (kind 38425) and disable providers
|
|
264
|
+
* whose 38421 node pubkey does not have at least one review tagged `t=lgtm`.
|
|
265
|
+
*
|
|
266
|
+
* Review events are expected to have:
|
|
267
|
+
* - `node`: the reviewed 38421 provider event pubkey
|
|
268
|
+
* - `t`: review label, where `lgtm` means the node looks good
|
|
269
|
+
*
|
|
270
|
+
* @param baseUrls Current provider base URLs to evaluate
|
|
271
|
+
* @returns Array of provider base URLs disabled by the review set
|
|
272
|
+
*/
|
|
273
|
+
async syncReviewedProvidersFromNostr(baseUrls = this.adapter.getBaseUrlsList(), providerNodes = this.providerNodePubkeysByUrl) {
|
|
274
|
+
if (baseUrls.length === 0) return [];
|
|
275
|
+
if (!this.adapter.setDisabledProviders) {
|
|
276
|
+
this.logger.warn(
|
|
277
|
+
"NostrReviews: adapter does not support setDisabledProviders; skipping provider disable sync"
|
|
278
|
+
);
|
|
279
|
+
return [];
|
|
280
|
+
}
|
|
281
|
+
const LGTM_RELAYS = [
|
|
282
|
+
"wss://relay.primal.net",
|
|
283
|
+
"wss://nos.lol",
|
|
284
|
+
"wss://relay.damus.io",
|
|
285
|
+
"wss://relay.routstr.com"
|
|
286
|
+
];
|
|
287
|
+
const reviewedNodePubkeys = /* @__PURE__ */ new Set();
|
|
288
|
+
{
|
|
289
|
+
const pool = new applesauceRelay.RelayPool();
|
|
290
|
+
const store = new applesauceCore.EventStore();
|
|
291
|
+
const timeoutMs = 5e3;
|
|
292
|
+
await new Promise((resolve) => {
|
|
293
|
+
pool.req(LGTM_RELAYS, {
|
|
294
|
+
kinds: [38425],
|
|
295
|
+
"#t": ["lgtm"],
|
|
296
|
+
limit: 500,
|
|
297
|
+
authors: [this.routstrPubkey]
|
|
298
|
+
}).pipe(
|
|
299
|
+
applesauceRelay.onlyEvents(),
|
|
300
|
+
rxjs.tap((event) => store.add(event))
|
|
301
|
+
).subscribe({ complete: () => resolve() });
|
|
302
|
+
setTimeout(() => resolve(), timeoutMs);
|
|
303
|
+
});
|
|
304
|
+
for (const event of store.getTimeline({ kinds: [38425] })) {
|
|
305
|
+
const hasLgtmTag = event.tags.some(
|
|
306
|
+
(tag) => tag[0] === "t" && tag[1]?.toLowerCase() === "lgtm"
|
|
307
|
+
);
|
|
308
|
+
if (!hasLgtmTag) continue;
|
|
309
|
+
for (const tag of event.tags) {
|
|
310
|
+
if (tag[0] === "node" && typeof tag[1] === "string" && tag[1]) {
|
|
311
|
+
reviewedNodePubkeys.add(tag[1]);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (reviewedNodePubkeys.size === 0) {
|
|
317
|
+
this.logger.warn(
|
|
318
|
+
"NostrReviews: no kind 38425 lgtm reviews found; keeping disabled providers unchanged"
|
|
319
|
+
);
|
|
320
|
+
return [];
|
|
321
|
+
}
|
|
322
|
+
if (providerNodes.size === 0) {
|
|
323
|
+
this.logger.warn(
|
|
324
|
+
"NostrReviews: no kind 38421 provider node metadata found; keeping disabled providers unchanged"
|
|
325
|
+
);
|
|
326
|
+
return [];
|
|
327
|
+
}
|
|
328
|
+
const disabledByReview = [];
|
|
329
|
+
for (const url of baseUrls) {
|
|
330
|
+
const normalized = this.normalizeUrl(url);
|
|
331
|
+
const nodePubkeys = providerNodes.get(normalized) || /* @__PURE__ */ new Set();
|
|
332
|
+
const hasLgtmReview = Array.from(nodePubkeys).some(
|
|
333
|
+
(pubkey) => reviewedNodePubkeys.has(pubkey)
|
|
334
|
+
);
|
|
335
|
+
if (!hasLgtmReview) {
|
|
336
|
+
disabledByReview.push(normalized);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
this.adapter.setDisabledProviders(Array.from(new Set(disabledByReview)));
|
|
340
|
+
return disabledByReview;
|
|
341
|
+
}
|
|
342
|
+
addProviderNode(map, url, pubkey) {
|
|
343
|
+
if (!pubkey) return;
|
|
344
|
+
const normalized = this.normalizeUrl(url);
|
|
345
|
+
const existing = map.get(normalized) || /* @__PURE__ */ new Set();
|
|
346
|
+
existing.add(pubkey);
|
|
347
|
+
map.set(normalized, existing);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Fetch models from all providers and select best-priced options
|
|
351
|
+
* Uses cache if available and not expired
|
|
352
|
+
* @param baseUrls List of provider base URLs to fetch from
|
|
353
|
+
* @param forceRefresh Ignore cache and fetch fresh data
|
|
354
|
+
* @param onProgress Callback fired after each provider completes with current combined models
|
|
355
|
+
* @returns Array of unique models with best prices selected
|
|
356
|
+
*/
|
|
357
|
+
async fetchModels(baseUrls, forceRefresh = false, onProgress) {
|
|
358
|
+
if (baseUrls.length === 0) {
|
|
359
|
+
throw new NoProvidersAvailableError();
|
|
360
|
+
}
|
|
361
|
+
const bestById = /* @__PURE__ */ new Map();
|
|
362
|
+
const modelsFromAllProviders = {};
|
|
363
|
+
const disabledProviders = this.adapter.getDisabledProviders();
|
|
364
|
+
const estimateMinCost = (m) => {
|
|
365
|
+
return m?.sats_pricing?.completion ?? 0;
|
|
366
|
+
};
|
|
367
|
+
const emitProgress = () => {
|
|
368
|
+
if (onProgress) {
|
|
369
|
+
const currentModels = Array.from(bestById.values()).map((v) => v.model);
|
|
370
|
+
onProgress(currentModels);
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
const fetchPromises = baseUrls.map(async (url) => {
|
|
374
|
+
const base = url.endsWith("/") ? url : `${url}/`;
|
|
375
|
+
try {
|
|
376
|
+
let list;
|
|
377
|
+
if (!forceRefresh) {
|
|
378
|
+
const lastUpdate = this.adapter.getProviderLastUpdate(base);
|
|
379
|
+
const cacheValid = lastUpdate && Date.now() - lastUpdate <= this.cacheTTL;
|
|
380
|
+
if (cacheValid) {
|
|
381
|
+
const cachedModels = this.adapter.getCachedModels();
|
|
382
|
+
const cachedList = cachedModels[base] || [];
|
|
383
|
+
list = cachedList;
|
|
384
|
+
} else {
|
|
385
|
+
list = await this.fetchModelsFromProvider(base);
|
|
386
|
+
}
|
|
387
|
+
} else {
|
|
388
|
+
list = await this.fetchModelsFromProvider(base);
|
|
389
|
+
}
|
|
390
|
+
modelsFromAllProviders[base] = list;
|
|
391
|
+
this.adapter.setProviderLastUpdate(base, Date.now());
|
|
392
|
+
if (!disabledProviders.includes(base)) {
|
|
393
|
+
for (const m of list) {
|
|
394
|
+
const existing = bestById.get(m.id);
|
|
395
|
+
if (!m.sats_pricing) continue;
|
|
396
|
+
if (!existing) {
|
|
397
|
+
bestById.set(m.id, { model: m, base });
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
const currentCost = estimateMinCost(m);
|
|
401
|
+
const existingCost = estimateMinCost(existing.model);
|
|
402
|
+
if (currentCost < existingCost && m.sats_pricing) {
|
|
403
|
+
bestById.set(m.id, { model: m, base });
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
emitProgress();
|
|
408
|
+
return { success: true, base, list };
|
|
409
|
+
} catch (error) {
|
|
410
|
+
if (this.isProviderDownError(error)) {
|
|
411
|
+
this.logger.warn(`Provider ${base} is down right now.`);
|
|
412
|
+
} else {
|
|
413
|
+
this.logger.warn(`Failed to fetch models from ${base}:`, error);
|
|
414
|
+
}
|
|
415
|
+
this.adapter.setProviderLastUpdate(base, Date.now());
|
|
416
|
+
return { success: false, base };
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
await Promise.allSettled(fetchPromises);
|
|
420
|
+
const existingCache = this.adapter.getCachedModels();
|
|
421
|
+
this.adapter.setCachedModels({
|
|
422
|
+
...existingCache,
|
|
423
|
+
...modelsFromAllProviders
|
|
424
|
+
});
|
|
425
|
+
return Array.from(bestById.values()).map((v) => v.model);
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Fetch models from a single provider
|
|
429
|
+
* @param baseUrl Provider base URL
|
|
430
|
+
* @returns Array of models from provider
|
|
431
|
+
*/
|
|
432
|
+
async fetchModelsFromProvider(baseUrl) {
|
|
433
|
+
const res = await fetch(`${baseUrl}v1/models`);
|
|
434
|
+
if (!res.ok) {
|
|
435
|
+
throw new Error(`Failed to fetch models: ${res.status}`);
|
|
436
|
+
}
|
|
437
|
+
const json = await res.json();
|
|
438
|
+
const list = Array.isArray(json?.data) ? json.data : [];
|
|
439
|
+
return list;
|
|
440
|
+
}
|
|
441
|
+
isProviderDownError(error) {
|
|
442
|
+
if (!(error instanceof Error)) return false;
|
|
443
|
+
const msg = error.message.toLowerCase();
|
|
444
|
+
if (msg.includes("fetch failed")) return true;
|
|
445
|
+
if (msg.includes("429")) return true;
|
|
446
|
+
if (msg.includes("502")) return true;
|
|
447
|
+
if (msg.includes("503")) return true;
|
|
448
|
+
if (msg.includes("504")) return true;
|
|
449
|
+
const cause = error.cause;
|
|
450
|
+
return cause?.code === "ENOTFOUND";
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Get all cached models from all providers
|
|
454
|
+
* @returns Record mapping baseUrl -> models
|
|
455
|
+
*/
|
|
456
|
+
getAllCachedModels() {
|
|
457
|
+
return this.adapter.getCachedModels();
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Clear cache for a specific provider
|
|
461
|
+
* @param baseUrl Provider base URL
|
|
462
|
+
*/
|
|
463
|
+
clearProviderCache(baseUrl) {
|
|
464
|
+
const base = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
|
|
465
|
+
const cached = this.adapter.getCachedModels();
|
|
466
|
+
delete cached[base];
|
|
467
|
+
this.adapter.setCachedModels(cached);
|
|
468
|
+
this.adapter.setProviderLastUpdate(base, 0);
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Clear all model caches
|
|
472
|
+
*/
|
|
473
|
+
clearAllCache() {
|
|
474
|
+
this.adapter.setCachedModels({});
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Filter base URLs based on Tor context
|
|
478
|
+
* @param baseUrls Provider URLs to filter
|
|
479
|
+
* @param torMode Whether in Tor context
|
|
480
|
+
* @returns Filtered URLs appropriate for Tor mode
|
|
481
|
+
*/
|
|
482
|
+
filterBaseUrlsForTor(baseUrls, torMode) {
|
|
483
|
+
if (!torMode) {
|
|
484
|
+
return baseUrls.filter((url) => !url.includes(".onion"));
|
|
485
|
+
}
|
|
486
|
+
return baseUrls.filter((url) => url.includes(".onion"));
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Get provider endpoints from provider info
|
|
490
|
+
* @param provider Provider object from directory
|
|
491
|
+
* @param torMode Whether in Tor context
|
|
492
|
+
* @returns Array of endpoint URLs
|
|
493
|
+
*/
|
|
494
|
+
getProviderEndpoints(provider, torMode) {
|
|
495
|
+
const endpoints = [];
|
|
496
|
+
if (torMode && provider.onion_url) {
|
|
497
|
+
endpoints.push(this.normalizeUrl(provider.onion_url));
|
|
498
|
+
} else if (provider.endpoint_url) {
|
|
499
|
+
endpoints.push(this.normalizeUrl(provider.endpoint_url));
|
|
500
|
+
}
|
|
501
|
+
return endpoints;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Normalize provider URL with trailing slash
|
|
505
|
+
* @param url URL to normalize
|
|
506
|
+
* @returns Normalized URL
|
|
507
|
+
*/
|
|
508
|
+
normalizeUrl(url) {
|
|
509
|
+
if (!url.startsWith("http")) {
|
|
510
|
+
url = `https://${url}`;
|
|
511
|
+
}
|
|
512
|
+
return url.endsWith("/") ? url : `${url}/`;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Fetch routstr21 models from Nostr network (kind 38423)
|
|
516
|
+
* Uses cache if available and not expired
|
|
517
|
+
* @returns Array of model IDs or empty array if not found
|
|
518
|
+
*/
|
|
519
|
+
async fetchRoutstr21Models(forceRefresh = false) {
|
|
520
|
+
const cachedModels = this.adapter.getRoutstr21Models();
|
|
521
|
+
if (!forceRefresh && cachedModels.length > 0) {
|
|
522
|
+
const lastUpdate = this.adapter.getRoutstr21ModelsLastUpdate();
|
|
523
|
+
const cacheValid = lastUpdate && Date.now() - lastUpdate <= this.cacheTTL;
|
|
524
|
+
if (cacheValid) {
|
|
525
|
+
return cachedModels;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
const DEFAULT_RELAYS = [
|
|
529
|
+
"wss://relay.damus.io",
|
|
530
|
+
"wss://nos.lol",
|
|
531
|
+
"wss://relay.routstr.com"
|
|
532
|
+
];
|
|
533
|
+
const pool = new applesauceRelay.RelayPool();
|
|
534
|
+
const localEventStore = new applesauceCore.EventStore();
|
|
535
|
+
const timeoutMs = 5e3;
|
|
536
|
+
await new Promise((resolve) => {
|
|
537
|
+
pool.req(DEFAULT_RELAYS, {
|
|
538
|
+
kinds: [38423],
|
|
539
|
+
"#d": ["routstr-21-models"],
|
|
540
|
+
limit: 1,
|
|
541
|
+
authors: [this.routstrPubkey]
|
|
542
|
+
}).pipe(
|
|
543
|
+
applesauceRelay.onlyEvents(),
|
|
544
|
+
rxjs.tap((event2) => {
|
|
545
|
+
localEventStore.add(event2);
|
|
546
|
+
})
|
|
547
|
+
).subscribe({
|
|
548
|
+
complete: () => {
|
|
549
|
+
resolve();
|
|
550
|
+
}
|
|
551
|
+
});
|
|
552
|
+
setTimeout(() => {
|
|
553
|
+
resolve();
|
|
554
|
+
}, timeoutMs);
|
|
555
|
+
});
|
|
556
|
+
const timeline = localEventStore.getTimeline({ kinds: [38423] });
|
|
557
|
+
if (timeline.length === 0) {
|
|
558
|
+
return cachedModels.length > 0 ? cachedModels : [];
|
|
559
|
+
}
|
|
560
|
+
const event = timeline[0];
|
|
561
|
+
try {
|
|
562
|
+
const content = JSON.parse(event.content);
|
|
563
|
+
const models = Array.isArray(content?.models) ? content.models : [];
|
|
564
|
+
this.adapter.setRoutstr21Models(models);
|
|
565
|
+
this.adapter.setRoutstr21ModelsLastUpdate(Date.now());
|
|
566
|
+
return models;
|
|
567
|
+
} catch {
|
|
568
|
+
this.logger.warn(
|
|
569
|
+
"Routstr21Models: failed to parse Nostr event content:",
|
|
570
|
+
event.id
|
|
571
|
+
);
|
|
572
|
+
return cachedModels.length > 0 ? cachedModels : [];
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
// discovery/MintDiscovery.ts
|
|
578
|
+
var MintDiscovery = class {
|
|
579
|
+
constructor(adapter, config = {}) {
|
|
580
|
+
this.adapter = adapter;
|
|
581
|
+
this.cacheTTL = config.cacheTTL || 21 * 60 * 1e3;
|
|
582
|
+
this.logger = (config.logger ?? consoleLogger).child("MintDiscovery");
|
|
583
|
+
}
|
|
584
|
+
adapter;
|
|
585
|
+
cacheTTL;
|
|
586
|
+
logger;
|
|
587
|
+
/**
|
|
588
|
+
* Fetch mints from all providers via their /v1/info endpoints
|
|
589
|
+
* Caches mints and full provider info for later access
|
|
590
|
+
* @param baseUrls List of provider base URLs to fetch from
|
|
591
|
+
* @returns Object with mints and provider info from all providers
|
|
592
|
+
*/
|
|
593
|
+
async discoverMints(baseUrls, options = {}) {
|
|
594
|
+
if (baseUrls.length === 0) {
|
|
595
|
+
return { mintsFromProviders: {}, infoFromProviders: {} };
|
|
596
|
+
}
|
|
597
|
+
const mintsFromAllProviders = {};
|
|
598
|
+
const infoFromAllProviders = {};
|
|
599
|
+
const forceRefresh = options.forceRefresh ?? false;
|
|
600
|
+
const fetchPromises = baseUrls.map(async (url) => {
|
|
601
|
+
const base = url.endsWith("/") ? url : `${url}/`;
|
|
602
|
+
try {
|
|
603
|
+
if (!forceRefresh) {
|
|
604
|
+
const lastUpdate = this.adapter.getProviderLastUpdate(base);
|
|
605
|
+
const cacheValid = lastUpdate && Date.now() - lastUpdate <= this.cacheTTL;
|
|
606
|
+
if (cacheValid) {
|
|
607
|
+
const cachedMints = this.adapter.getCachedMints()[base] || [];
|
|
608
|
+
const cachedInfo = this.adapter.getCachedProviderInfo()[base];
|
|
609
|
+
mintsFromAllProviders[base] = cachedMints;
|
|
610
|
+
if (cachedInfo) {
|
|
611
|
+
infoFromAllProviders[base] = cachedInfo;
|
|
612
|
+
}
|
|
613
|
+
return {
|
|
614
|
+
success: true,
|
|
615
|
+
base,
|
|
616
|
+
mints: cachedMints,
|
|
617
|
+
info: cachedInfo
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
const res = await fetch(`${base}v1/info`);
|
|
622
|
+
if (!res.ok) {
|
|
623
|
+
throw new Error(`Failed to fetch info: ${res.status}`);
|
|
624
|
+
}
|
|
625
|
+
const json = await res.json();
|
|
626
|
+
const mints = Array.isArray(json?.mints) ? json.mints : [];
|
|
627
|
+
const normalizedMints = mints.map(
|
|
628
|
+
(mint) => mint.endsWith("/") ? mint.slice(0, -1) : mint
|
|
629
|
+
);
|
|
630
|
+
mintsFromAllProviders[base] = normalizedMints;
|
|
631
|
+
infoFromAllProviders[base] = json;
|
|
632
|
+
this.adapter.setProviderLastUpdate(base, Date.now());
|
|
633
|
+
return { success: true, base, mints: normalizedMints, info: json };
|
|
634
|
+
} catch (error) {
|
|
635
|
+
this.adapter.setProviderLastUpdate(base, Date.now());
|
|
636
|
+
if (this.isProviderDownError(error)) {
|
|
637
|
+
this.logger.warn(`Provider ${base} is down right now.`);
|
|
638
|
+
} else {
|
|
639
|
+
this.logger.warn(`Failed to fetch mints from ${base}:`, error);
|
|
640
|
+
}
|
|
641
|
+
return { success: false, base, mints: [], info: null };
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
const results = await Promise.allSettled(fetchPromises);
|
|
645
|
+
for (const result of results) {
|
|
646
|
+
if (result.status === "fulfilled") {
|
|
647
|
+
const { base, mints, info } = result.value;
|
|
648
|
+
mintsFromAllProviders[base] = mints;
|
|
649
|
+
if (info) {
|
|
650
|
+
infoFromAllProviders[base] = info;
|
|
651
|
+
}
|
|
652
|
+
} else {
|
|
653
|
+
this.logger.error("Mint discovery error:", result.reason);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
try {
|
|
657
|
+
this.adapter.setCachedMints(mintsFromAllProviders);
|
|
658
|
+
this.adapter.setCachedProviderInfo(infoFromAllProviders);
|
|
659
|
+
} catch (error) {
|
|
660
|
+
this.logger.error("Error caching mint discovery results:", error);
|
|
661
|
+
}
|
|
662
|
+
return {
|
|
663
|
+
mintsFromProviders: mintsFromAllProviders,
|
|
664
|
+
infoFromProviders: infoFromAllProviders
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Get cached mints from all providers
|
|
669
|
+
* @returns Record mapping baseUrl -> mint URLs
|
|
670
|
+
*/
|
|
671
|
+
getCachedMints() {
|
|
672
|
+
return this.adapter.getCachedMints();
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Get cached provider info from all providers
|
|
676
|
+
* @returns Record mapping baseUrl -> provider info
|
|
677
|
+
*/
|
|
678
|
+
getCachedProviderInfo() {
|
|
679
|
+
return this.adapter.getCachedProviderInfo();
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Get mints for a specific provider
|
|
683
|
+
* @param baseUrl Provider base URL
|
|
684
|
+
* @returns Array of mint URLs for the provider
|
|
685
|
+
*/
|
|
686
|
+
getProviderMints(baseUrl) {
|
|
687
|
+
const normalized = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
|
|
688
|
+
const allMints = this.getCachedMints();
|
|
689
|
+
return allMints[normalized] || [];
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Get info for a specific provider
|
|
693
|
+
* @param baseUrl Provider base URL
|
|
694
|
+
* @returns Provider info object or null if not found
|
|
695
|
+
*/
|
|
696
|
+
getProviderInfo(baseUrl) {
|
|
697
|
+
const normalized = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
|
|
698
|
+
const allInfo = this.getCachedProviderInfo();
|
|
699
|
+
return allInfo[normalized] || null;
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Clear mint cache for a specific provider
|
|
703
|
+
* @param baseUrl Provider base URL
|
|
704
|
+
*/
|
|
705
|
+
clearProviderMintCache(baseUrl) {
|
|
706
|
+
const normalized = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
|
|
707
|
+
const mints = this.getCachedMints();
|
|
708
|
+
delete mints[normalized];
|
|
709
|
+
this.adapter.setCachedMints(mints);
|
|
710
|
+
const info = this.getCachedProviderInfo();
|
|
711
|
+
delete info[normalized];
|
|
712
|
+
this.adapter.setCachedProviderInfo(info);
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Clear all mint caches
|
|
716
|
+
*/
|
|
717
|
+
clearAllCache() {
|
|
718
|
+
this.adapter.setCachedMints({});
|
|
719
|
+
this.adapter.setCachedProviderInfo({});
|
|
720
|
+
}
|
|
721
|
+
isProviderDownError(error) {
|
|
722
|
+
if (!(error instanceof Error)) return false;
|
|
723
|
+
const msg = error.message.toLowerCase();
|
|
724
|
+
if (msg.includes("fetch failed")) return true;
|
|
725
|
+
if (msg.includes("429")) return true;
|
|
726
|
+
if (msg.includes("502")) return true;
|
|
727
|
+
if (msg.includes("503")) return true;
|
|
728
|
+
if (msg.includes("504")) return true;
|
|
729
|
+
const cause = error.cause;
|
|
730
|
+
if (cause?.code === "ENOTFOUND") return true;
|
|
731
|
+
return false;
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
exports.MintDiscovery = MintDiscovery;
|
|
736
|
+
exports.ModelManager = ModelManager;
|
|
737
|
+
//# sourceMappingURL=index.js.map
|
|
738
|
+
//# sourceMappingURL=index.js.map
|