@routstr/sdk 0.3.9 → 0.3.10
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/browser.d.mts +12 -0
- package/dist/browser.d.ts +12 -0
- package/dist/browser.js +6278 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.mjs +6230 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/bun.d.mts +29 -0
- package/dist/bun.d.ts +29 -0
- package/dist/bun.js +6586 -0
- package/dist/bun.js.map +1 -0
- package/dist/bun.mjs +6532 -0
- package/dist/bun.mjs.map +1 -0
- package/dist/bunSqlite-BMTseLIz.d.ts +18 -0
- package/dist/bunSqlite-D6AreVE2.d.mts +18 -0
- package/dist/client/index.d.mts +63 -41
- package/dist/client/index.d.ts +63 -41
- package/dist/client/index.js +801 -1291
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +801 -1292
- package/dist/client/index.mjs.map +1 -1
- package/dist/discovery/index.d.mts +33 -3
- package/dist/discovery/index.d.ts +33 -3
- package/dist/discovery/index.js +28 -21
- package/dist/discovery/index.js.map +1 -1
- package/dist/discovery/index.mjs +28 -21
- package/dist/discovery/index.mjs.map +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1045 -1564
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1045 -1561
- package/dist/index.mjs.map +1 -1
- package/dist/node.d.mts +22 -0
- package/dist/node.d.ts +22 -0
- package/dist/node.js +6651 -0
- package/dist/node.js.map +1 -0
- package/dist/node.mjs +6599 -0
- package/dist/node.mjs.map +1 -0
- package/dist/storage/bun.d.mts +16 -0
- package/dist/storage/bun.d.ts +16 -0
- package/dist/storage/bun.js +1801 -0
- package/dist/storage/bun.js.map +1 -0
- package/dist/storage/bun.mjs +1777 -0
- package/dist/storage/bun.mjs.map +1 -0
- package/dist/storage/index.d.mts +4 -30
- package/dist/storage/index.d.ts +4 -30
- package/dist/storage/index.js +139 -650
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/index.mjs +140 -647
- package/dist/storage/index.mjs.map +1 -1
- package/dist/storage/node.d.mts +22 -0
- package/dist/storage/node.d.ts +22 -0
- package/dist/storage/node.js +1864 -0
- package/dist/storage/node.js.map +1 -0
- package/dist/storage/node.mjs +1842 -0
- package/dist/storage/node.mjs.map +1 -0
- package/dist/{store-C6dfj1cc.d.mts → store-BiuM2V9N.d.mts} +14 -0
- package/dist/{store-58VcEUoA.d.ts → store-C8MZlfuz.d.ts} +14 -0
- package/package.json +26 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { D as DiscoveryAdapter } from '../interfaces-Cv1k2EUK.mjs';
|
|
2
2
|
import { S as SdkLogger, e as Model, k as ProviderInfo } from '../types-_21yYFZG.mjs';
|
|
3
|
-
import { EventStore } from 'applesauce-core';
|
|
3
|
+
import { IEventDatabase, EventStore } from 'applesauce-core';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* ModelManager class for discovering, fetching, and managing models from providers
|
|
@@ -8,6 +8,18 @@ import { EventStore } from 'applesauce-core';
|
|
|
8
8
|
* (lowest cost) across multiple providers
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
type SqliteStatement = {
|
|
12
|
+
run?: (...params: any[]) => unknown;
|
|
13
|
+
get?: (...params: any[]) => any;
|
|
14
|
+
};
|
|
15
|
+
type PersistentEventDatabase = IEventDatabase & {
|
|
16
|
+
db?: {
|
|
17
|
+
exec: (sql: string) => void;
|
|
18
|
+
prepare: (sql: string) => SqliteStatement;
|
|
19
|
+
};
|
|
20
|
+
close?: () => void;
|
|
21
|
+
};
|
|
22
|
+
type PersistentEventDatabaseFactory = (dbPath: string) => Promise<PersistentEventDatabase> | PersistentEventDatabase;
|
|
11
23
|
/**
|
|
12
24
|
* Configuration for ModelManager
|
|
13
25
|
*/
|
|
@@ -22,13 +34,24 @@ interface ModelManagerConfig {
|
|
|
22
34
|
cacheTTL?: number;
|
|
23
35
|
/** Nostr pubkey for routstr review/model events (kind 38425/38423). Defaults to routstr's key. */
|
|
24
36
|
routstrPubkey?: string;
|
|
37
|
+
/** Nostr relay URLs for provider/model discovery.
|
|
38
|
+
* When set, these relays are used for all Nostr queries (kinds 38421, 38423, 38425).
|
|
39
|
+
* When unset, each method uses its own default relay set. */
|
|
40
|
+
nostrRelays?: string[];
|
|
25
41
|
/** Optional injectable logger */
|
|
26
42
|
logger?: SdkLogger;
|
|
27
|
-
/** Path to
|
|
43
|
+
/** Path to database for persistent Nostr event storage.
|
|
28
44
|
* If provided, events fetched by ModelManager from relays (kinds 38421,
|
|
29
45
|
* 38423, 38425) are persisted and survive process restarts. The underlying
|
|
30
|
-
* EventStore can also be accessed for advanced/manual event management.
|
|
46
|
+
* EventStore can also be accessed for advanced/manual event management.
|
|
47
|
+
*
|
|
48
|
+
* Runtime-specific SQLite implementations are intentionally not imported by
|
|
49
|
+
* the browser-safe default SDK entrypoint. Use @routstr/sdk/node or
|
|
50
|
+
* @routstr/sdk/bun to get a ModelManager preconfigured with a SQLite-backed
|
|
51
|
+
* persistentEventDatabaseFactory, or inject your own factory here. */
|
|
31
52
|
eventStoreDbPath?: string;
|
|
53
|
+
/** Factory used with eventStoreDbPath to create the persistent event DB. */
|
|
54
|
+
persistentEventDatabaseFactory?: PersistentEventDatabaseFactory;
|
|
32
55
|
}
|
|
33
56
|
/**
|
|
34
57
|
* ModelManager handles all model discovery and caching logic
|
|
@@ -41,6 +64,7 @@ declare class ModelManager {
|
|
|
41
64
|
private readonly includeProviderUrls;
|
|
42
65
|
private readonly excludeProviderUrls;
|
|
43
66
|
private readonly routstrPubkey;
|
|
67
|
+
private readonly nostrRelays;
|
|
44
68
|
private readonly logger;
|
|
45
69
|
private providerNodePubkeysByUrl;
|
|
46
70
|
/** Persistent event store for relay-fetched events (null if not configured/initialized) */
|
|
@@ -48,6 +72,7 @@ declare class ModelManager {
|
|
|
48
72
|
private eventStoreDb;
|
|
49
73
|
private eventStoreInitPromise;
|
|
50
74
|
private readonly eventStoreDbPath?;
|
|
75
|
+
private readonly persistentEventDatabaseFactory?;
|
|
51
76
|
constructor(adapter: DiscoveryAdapter, config?: ModelManagerConfig);
|
|
52
77
|
/**
|
|
53
78
|
* Get the list of bootstrapped provider base URLs
|
|
@@ -90,6 +115,11 @@ declare class ModelManager {
|
|
|
90
115
|
* @throws ProviderBootstrapError if all providers fail to fetch
|
|
91
116
|
*/
|
|
92
117
|
bootstrapProviders(torMode?: boolean, forceRefresh?: boolean): Promise<string[]>;
|
|
118
|
+
/**
|
|
119
|
+
* Resolve Nostr relay URLs for a given use case.
|
|
120
|
+
* Returns user-configured relays if set, otherwise the provided defaults.
|
|
121
|
+
*/
|
|
122
|
+
private getNostrRelays;
|
|
93
123
|
/**
|
|
94
124
|
* Bootstrap providers from Nostr network (kind 38421)
|
|
95
125
|
* @param kind The Nostr kind to fetch
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { D as DiscoveryAdapter } from '../interfaces-iL7CWeG5.js';
|
|
2
2
|
import { S as SdkLogger, e as Model, k as ProviderInfo } from '../types-_21yYFZG.js';
|
|
3
|
-
import { EventStore } from 'applesauce-core';
|
|
3
|
+
import { IEventDatabase, EventStore } from 'applesauce-core';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* ModelManager class for discovering, fetching, and managing models from providers
|
|
@@ -8,6 +8,18 @@ import { EventStore } from 'applesauce-core';
|
|
|
8
8
|
* (lowest cost) across multiple providers
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
type SqliteStatement = {
|
|
12
|
+
run?: (...params: any[]) => unknown;
|
|
13
|
+
get?: (...params: any[]) => any;
|
|
14
|
+
};
|
|
15
|
+
type PersistentEventDatabase = IEventDatabase & {
|
|
16
|
+
db?: {
|
|
17
|
+
exec: (sql: string) => void;
|
|
18
|
+
prepare: (sql: string) => SqliteStatement;
|
|
19
|
+
};
|
|
20
|
+
close?: () => void;
|
|
21
|
+
};
|
|
22
|
+
type PersistentEventDatabaseFactory = (dbPath: string) => Promise<PersistentEventDatabase> | PersistentEventDatabase;
|
|
11
23
|
/**
|
|
12
24
|
* Configuration for ModelManager
|
|
13
25
|
*/
|
|
@@ -22,13 +34,24 @@ interface ModelManagerConfig {
|
|
|
22
34
|
cacheTTL?: number;
|
|
23
35
|
/** Nostr pubkey for routstr review/model events (kind 38425/38423). Defaults to routstr's key. */
|
|
24
36
|
routstrPubkey?: string;
|
|
37
|
+
/** Nostr relay URLs for provider/model discovery.
|
|
38
|
+
* When set, these relays are used for all Nostr queries (kinds 38421, 38423, 38425).
|
|
39
|
+
* When unset, each method uses its own default relay set. */
|
|
40
|
+
nostrRelays?: string[];
|
|
25
41
|
/** Optional injectable logger */
|
|
26
42
|
logger?: SdkLogger;
|
|
27
|
-
/** Path to
|
|
43
|
+
/** Path to database for persistent Nostr event storage.
|
|
28
44
|
* If provided, events fetched by ModelManager from relays (kinds 38421,
|
|
29
45
|
* 38423, 38425) are persisted and survive process restarts. The underlying
|
|
30
|
-
* EventStore can also be accessed for advanced/manual event management.
|
|
46
|
+
* EventStore can also be accessed for advanced/manual event management.
|
|
47
|
+
*
|
|
48
|
+
* Runtime-specific SQLite implementations are intentionally not imported by
|
|
49
|
+
* the browser-safe default SDK entrypoint. Use @routstr/sdk/node or
|
|
50
|
+
* @routstr/sdk/bun to get a ModelManager preconfigured with a SQLite-backed
|
|
51
|
+
* persistentEventDatabaseFactory, or inject your own factory here. */
|
|
31
52
|
eventStoreDbPath?: string;
|
|
53
|
+
/** Factory used with eventStoreDbPath to create the persistent event DB. */
|
|
54
|
+
persistentEventDatabaseFactory?: PersistentEventDatabaseFactory;
|
|
32
55
|
}
|
|
33
56
|
/**
|
|
34
57
|
* ModelManager handles all model discovery and caching logic
|
|
@@ -41,6 +64,7 @@ declare class ModelManager {
|
|
|
41
64
|
private readonly includeProviderUrls;
|
|
42
65
|
private readonly excludeProviderUrls;
|
|
43
66
|
private readonly routstrPubkey;
|
|
67
|
+
private readonly nostrRelays;
|
|
44
68
|
private readonly logger;
|
|
45
69
|
private providerNodePubkeysByUrl;
|
|
46
70
|
/** Persistent event store for relay-fetched events (null if not configured/initialized) */
|
|
@@ -48,6 +72,7 @@ declare class ModelManager {
|
|
|
48
72
|
private eventStoreDb;
|
|
49
73
|
private eventStoreInitPromise;
|
|
50
74
|
private readonly eventStoreDbPath?;
|
|
75
|
+
private readonly persistentEventDatabaseFactory?;
|
|
51
76
|
constructor(adapter: DiscoveryAdapter, config?: ModelManagerConfig);
|
|
52
77
|
/**
|
|
53
78
|
* Get the list of bootstrapped provider base URLs
|
|
@@ -90,6 +115,11 @@ declare class ModelManager {
|
|
|
90
115
|
* @throws ProviderBootstrapError if all providers fail to fetch
|
|
91
116
|
*/
|
|
92
117
|
bootstrapProviders(torMode?: boolean, forceRefresh?: boolean): Promise<string[]>;
|
|
118
|
+
/**
|
|
119
|
+
* Resolve Nostr relay URLs for a given use case.
|
|
120
|
+
* Returns user-configured relays if set, otherwise the provided defaults.
|
|
121
|
+
*/
|
|
122
|
+
private getNostrRelays;
|
|
93
123
|
/**
|
|
94
124
|
* Bootstrap providers from Nostr network (kind 38421)
|
|
95
125
|
* @param kind The Nostr kind to fetch
|
package/dist/discovery/index.js
CHANGED
|
@@ -34,9 +34,6 @@ var NoProvidersAvailableError = class extends Error {
|
|
|
34
34
|
this.name = "NoProvidersAvailableError";
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
function isBunRuntime() {
|
|
38
|
-
return typeof Bun !== "undefined";
|
|
39
|
-
}
|
|
40
37
|
var ModelManager = class _ModelManager {
|
|
41
38
|
constructor(adapter, config = {}) {
|
|
42
39
|
this.adapter = adapter;
|
|
@@ -45,8 +42,10 @@ var ModelManager = class _ModelManager {
|
|
|
45
42
|
this.includeProviderUrls = config.includeProviderUrls || [];
|
|
46
43
|
this.excludeProviderUrls = config.excludeProviderUrls || [];
|
|
47
44
|
this.routstrPubkey = config.routstrPubkey || "4ad6fa2d16e2a9b576c863b4cf7404a70d4dc320c0c447d10ad6ff58993eacc8";
|
|
45
|
+
this.nostrRelays = config.nostrRelays;
|
|
48
46
|
this.logger = (config.logger ?? consoleLogger).child("ModelManager");
|
|
49
47
|
this.eventStoreDbPath = config.eventStoreDbPath;
|
|
48
|
+
this.persistentEventDatabaseFactory = config.persistentEventDatabaseFactory;
|
|
50
49
|
}
|
|
51
50
|
adapter;
|
|
52
51
|
cacheTTL;
|
|
@@ -54,6 +53,7 @@ var ModelManager = class _ModelManager {
|
|
|
54
53
|
includeProviderUrls;
|
|
55
54
|
excludeProviderUrls;
|
|
56
55
|
routstrPubkey;
|
|
56
|
+
nostrRelays;
|
|
57
57
|
logger;
|
|
58
58
|
providerNodePubkeysByUrl = /* @__PURE__ */ new Map();
|
|
59
59
|
/** Persistent event store for relay-fetched events (null if not configured/initialized) */
|
|
@@ -61,6 +61,7 @@ var ModelManager = class _ModelManager {
|
|
|
61
61
|
eventStoreDb = null;
|
|
62
62
|
eventStoreInitPromise = null;
|
|
63
63
|
eventStoreDbPath;
|
|
64
|
+
persistentEventDatabaseFactory;
|
|
64
65
|
/**
|
|
65
66
|
* Get the list of bootstrapped provider base URLs
|
|
66
67
|
* @returns Array of provider base URLs
|
|
@@ -89,7 +90,7 @@ var ModelManager = class _ModelManager {
|
|
|
89
90
|
} catch (error) {
|
|
90
91
|
this.eventStoreInitPromise = null;
|
|
91
92
|
throw new Error(
|
|
92
|
-
`
|
|
93
|
+
`Persistent Nostr event storage requires a runtime-specific database factory. Use @routstr/sdk/node, @routstr/sdk/bun, inject persistentEventDatabaseFactory, or omit eventStoreDbPath. (${error})`
|
|
93
94
|
);
|
|
94
95
|
}
|
|
95
96
|
})();
|
|
@@ -104,16 +105,15 @@ var ModelManager = class _ModelManager {
|
|
|
104
105
|
return this.ensureEventStore();
|
|
105
106
|
}
|
|
106
107
|
async createPersistentEventDatabase() {
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
if (!this.eventStoreDbPath) {
|
|
109
|
+
throw new Error("eventStoreDbPath is required");
|
|
110
|
+
}
|
|
111
|
+
if (!this.persistentEventDatabaseFactory) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
"persistentEventDatabaseFactory is required. Import ModelManager from @routstr/sdk/node or @routstr/sdk/bun for SQLite-backed persistent event storage."
|
|
111
114
|
);
|
|
112
115
|
}
|
|
113
|
-
|
|
114
|
-
return new BetterSqlite3EventDatabase(
|
|
115
|
-
this.eventStoreDbPath
|
|
116
|
-
);
|
|
116
|
+
return this.persistentEventDatabaseFactory(this.eventStoreDbPath);
|
|
117
117
|
}
|
|
118
118
|
/** Close the persistent event store database handle, if configured. */
|
|
119
119
|
closeEventStore() {
|
|
@@ -226,6 +226,13 @@ var ModelManager = class _ModelManager {
|
|
|
226
226
|
}
|
|
227
227
|
return this.bootstrapFromHttp(torMode, forceRefresh);
|
|
228
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Resolve Nostr relay URLs for a given use case.
|
|
231
|
+
* Returns user-configured relays if set, otherwise the provided defaults.
|
|
232
|
+
*/
|
|
233
|
+
getNostrRelays(defaults) {
|
|
234
|
+
return this.nostrRelays && this.nostrRelays.length > 0 ? this.nostrRelays : defaults;
|
|
235
|
+
}
|
|
229
236
|
/**
|
|
230
237
|
* Bootstrap providers from Nostr network (kind 38421)
|
|
231
238
|
* @param kind The Nostr kind to fetch
|
|
@@ -233,11 +240,11 @@ var ModelManager = class _ModelManager {
|
|
|
233
240
|
* @returns Array of provider base URLs
|
|
234
241
|
*/
|
|
235
242
|
async bootstrapFromNostr(kind, torMode, forceRefresh = false) {
|
|
236
|
-
const
|
|
243
|
+
const relays = this.getNostrRelays([
|
|
237
244
|
"wss://relay.primal.net",
|
|
238
245
|
"wss://nos.lol",
|
|
239
246
|
"wss://relay.damus.io"
|
|
240
|
-
];
|
|
247
|
+
]);
|
|
241
248
|
const cached = await this.getCachedNostrEvents(
|
|
242
249
|
{ kinds: [kind] },
|
|
243
250
|
this.cacheTTL,
|
|
@@ -248,7 +255,7 @@ var ModelManager = class _ModelManager {
|
|
|
248
255
|
const pool = new applesauceRelay.RelayPool();
|
|
249
256
|
const timeoutMs = 5e3;
|
|
250
257
|
await new Promise((resolve) => {
|
|
251
|
-
pool.req(
|
|
258
|
+
pool.req(relays, {
|
|
252
259
|
kinds: [kind],
|
|
253
260
|
limit: 100
|
|
254
261
|
}).pipe(
|
|
@@ -420,16 +427,16 @@ var ModelManager = class _ModelManager {
|
|
|
420
427
|
);
|
|
421
428
|
let sessionEvents = cached;
|
|
422
429
|
if (cached.length === 0) {
|
|
423
|
-
const
|
|
430
|
+
const lgtmRelays = this.getNostrRelays([
|
|
424
431
|
"wss://relay.primal.net",
|
|
425
432
|
"wss://nos.lol",
|
|
426
433
|
"wss://relay.damus.io",
|
|
427
434
|
"wss://relay.routstr.com"
|
|
428
|
-
];
|
|
435
|
+
]);
|
|
429
436
|
const pool = new applesauceRelay.RelayPool();
|
|
430
437
|
const timeoutMs = 5e3;
|
|
431
438
|
await new Promise((resolve) => {
|
|
432
|
-
pool.req(
|
|
439
|
+
pool.req(lgtmRelays, {
|
|
433
440
|
kinds: [38425],
|
|
434
441
|
"#t": ["lgtm"],
|
|
435
442
|
limit: 500,
|
|
@@ -671,11 +678,11 @@ var ModelManager = class _ModelManager {
|
|
|
671
678
|
return cachedModels;
|
|
672
679
|
}
|
|
673
680
|
}
|
|
674
|
-
const
|
|
681
|
+
const relays = this.getNostrRelays([
|
|
675
682
|
"wss://relay.damus.io",
|
|
676
683
|
"wss://nos.lol",
|
|
677
684
|
"wss://relay.routstr.com"
|
|
678
|
-
];
|
|
685
|
+
]);
|
|
679
686
|
const cached = await this.getCachedNostrEvents(
|
|
680
687
|
{ kinds: [38423], "#d": ["routstr-21-models"], authors: [this.routstrPubkey] },
|
|
681
688
|
this.cacheTTL,
|
|
@@ -686,7 +693,7 @@ var ModelManager = class _ModelManager {
|
|
|
686
693
|
const pool = new applesauceRelay.RelayPool();
|
|
687
694
|
const timeoutMs = 5e3;
|
|
688
695
|
await new Promise((resolve) => {
|
|
689
|
-
pool.req(
|
|
696
|
+
pool.req(relays, {
|
|
690
697
|
kinds: [38423],
|
|
691
698
|
"#d": ["routstr-21-models"],
|
|
692
699
|
limit: 1,
|