@routstr/sdk 0.1.0 → 0.1.1

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,288 @@
1
+ import { M as Model, a as Message, T as TransactionHistory, l as StreamingResult } from '../types-BlHjmWRK.mjs';
2
+ import { P as ProviderRegistry, W as WalletAdapter, S as StorageAdapter, a as StreamingCallbacks } from '../interfaces-BOfiz3Tt.mjs';
3
+ import { CashuSpender, BalanceManager } from '../wallet/index.mjs';
4
+
5
+ /**
6
+ * ProviderManager - Handles provider selection and failover logic
7
+ *
8
+ * Handles:
9
+ * - Finding the best provider for a model based on price
10
+ * - Provider failover when errors occur
11
+ * - Tracking failed providers to avoid retry loops
12
+ * - Provider version compatibility
13
+ *
14
+ * Extracted from utils/apiUtils.ts findNextBestProvider and related logic
15
+ */
16
+
17
+ interface ModelProviderPrice {
18
+ baseUrl: string;
19
+ model: Model;
20
+ promptPerMillion: number;
21
+ completionPerMillion: number;
22
+ totalPerMillion: number;
23
+ }
24
+ declare class ProviderManager {
25
+ private providerRegistry;
26
+ private failedProviders;
27
+ constructor(providerRegistry: ProviderRegistry);
28
+ /**
29
+ * Reset the failed providers list
30
+ */
31
+ resetFailedProviders(): void;
32
+ /**
33
+ * Mark a provider as failed
34
+ */
35
+ markFailed(baseUrl: string): void;
36
+ /**
37
+ * Check if a provider has failed
38
+ */
39
+ hasFailed(baseUrl: string): boolean;
40
+ /**
41
+ * Find the next best provider for a model
42
+ * @param modelId The model ID to find a provider for
43
+ * @param currentBaseUrl The current provider to exclude
44
+ * @returns The best provider URL or null if none available
45
+ */
46
+ findNextBestProvider(modelId: string, currentBaseUrl: string): string | null;
47
+ /**
48
+ * Find the best model for a provider
49
+ * Useful when switching providers and need to find equivalent model
50
+ */
51
+ getModelForProvider(baseUrl: string, modelId: string): Promise<Model | null>;
52
+ /**
53
+ * Get all available providers for a model
54
+ * Returns sorted list by price
55
+ */
56
+ getAllProvidersForModel(modelId: string): Array<{
57
+ baseUrl: string;
58
+ model: Model;
59
+ cost: number;
60
+ }>;
61
+ /**
62
+ * Get providers for a model sorted by prompt+completion pricing
63
+ */
64
+ getProviderPriceRankingForModel(modelId: string, options?: {
65
+ torMode?: boolean;
66
+ includeDisabled?: boolean;
67
+ }): ModelProviderPrice[];
68
+ /**
69
+ * Get best-priced provider for a specific model
70
+ */
71
+ getBestProviderForModel(modelId: string, options?: {
72
+ torMode?: boolean;
73
+ includeDisabled?: boolean;
74
+ }): string | null;
75
+ private normalizeModelId;
76
+ /**
77
+ * Check if a provider accepts a specific mint
78
+ */
79
+ providerAcceptsMint(baseUrl: string, mintUrl: string): boolean;
80
+ /**
81
+ * Get required sats for a model based on message history
82
+ * Simple estimation based on typical usage
83
+ */
84
+ getRequiredSatsForModel(model: Model, apiMessages: any[], maxTokens?: number): number;
85
+ }
86
+
87
+ /**
88
+ * RoutstrClient - Main API client for Routstr
89
+ *
90
+ * Orchestrates:
91
+ * - Token spending via CashuSpender
92
+ * - API requests with authentication
93
+ * - Streaming response processing
94
+ * - Provider failover via ProviderManager
95
+ * - Error handling and refunds
96
+ *
97
+ * Extracted from utils/apiUtils.ts
98
+ */
99
+
100
+ /**
101
+ * Options for fetching AI response
102
+ */
103
+ interface FetchOptions {
104
+ messageHistory: Message[];
105
+ selectedModel: Model;
106
+ baseUrl: string;
107
+ mintUrl: string;
108
+ balance: number;
109
+ transactionHistory: TransactionHistory[];
110
+ maxTokens?: number;
111
+ headers?: Record<string, string>;
112
+ }
113
+ /**
114
+ * RoutstrClient is the main SDK entry point
115
+ */
116
+ type AlertLevel = "max" | "min";
117
+ type RoutstrClientMode = "xcashu" | "lazyrefund" | "apikeys";
118
+ interface RouteRequestParams {
119
+ path: string;
120
+ method: string;
121
+ body?: unknown;
122
+ headers?: Record<string, string>;
123
+ baseUrl: string;
124
+ mintUrl: string;
125
+ modelId?: string;
126
+ }
127
+ declare class RoutstrClient {
128
+ private walletAdapter;
129
+ private storageAdapter;
130
+ private providerRegistry;
131
+ private cashuSpender;
132
+ private balanceManager;
133
+ private streamProcessor;
134
+ private providerManager;
135
+ private alertLevel;
136
+ private mode;
137
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry: ProviderRegistry, alertLevel: AlertLevel, mode?: RoutstrClientMode);
138
+ /**
139
+ * Get the current client mode
140
+ */
141
+ getMode(): RoutstrClientMode;
142
+ /**
143
+ * Get the CashuSpender instance
144
+ */
145
+ getCashuSpender(): CashuSpender;
146
+ /**
147
+ * Get the BalanceManager instance
148
+ */
149
+ getBalanceManager(): BalanceManager;
150
+ /**
151
+ * Get the ProviderManager instance
152
+ */
153
+ getProviderManager(): ProviderManager;
154
+ /**
155
+ * Check if the client is currently busy (in critical section)
156
+ */
157
+ get isBusy(): boolean;
158
+ /**
159
+ * Route an API request to the upstream provider
160
+ *
161
+ * This is a simpler alternative to fetchAIResponse that just proxies
162
+ * the request upstream without the streaming callback machinery.
163
+ * Useful for daemon-style routing where you just need to forward
164
+ * requests and get responses back.
165
+ */
166
+ routeRequest(params: RouteRequestParams): Promise<Response>;
167
+ /**
168
+ * Fetch AI response with streaming
169
+ */
170
+ fetchAIResponse(options: FetchOptions, callbacks: StreamingCallbacks): Promise<void>;
171
+ /**
172
+ * Make the API request with failover support
173
+ */
174
+ private _makeRequest;
175
+ /**
176
+ * Handle error responses with failover
177
+ */
178
+ private _handleErrorResponse;
179
+ /**
180
+ * Handle post-response balance update for all modes
181
+ */
182
+ private _handlePostResponseBalanceUpdate;
183
+ /**
184
+ * Convert messages for API format
185
+ */
186
+ private _convertMessages;
187
+ /**
188
+ * Create assistant message from streaming result
189
+ */
190
+ private _createAssistantMessage;
191
+ /**
192
+ * Create a child key for a parent API key via the provider's API
193
+ * POST /v1/balance/child-key
194
+ */
195
+ private _createChildKey;
196
+ /**
197
+ * Calculate estimated costs from usage
198
+ */
199
+ private _getEstimatedCosts;
200
+ /**
201
+ * Get pending cashu token amount
202
+ */
203
+ private _getPendingCashuTokenAmount;
204
+ /**
205
+ * Check if error message indicates a network error
206
+ */
207
+ private _isNetworkError;
208
+ /**
209
+ * Handle errors and notify callbacks
210
+ */
211
+ private _handleError;
212
+ /**
213
+ * Check wallet balance and throw if insufficient
214
+ */
215
+ private _checkBalance;
216
+ /**
217
+ * Spend a token using CashuSpender with standardized error handling
218
+ */
219
+ private _spendToken;
220
+ /**
221
+ * Build request headers with common defaults and dev mock controls
222
+ */
223
+ private _buildBaseHeaders;
224
+ /**
225
+ * Attach auth headers using the active client mode
226
+ */
227
+ private _withAuthHeader;
228
+ }
229
+
230
+ /**
231
+ * StreamProcessor - Handles SSE streaming response parsing
232
+ *
233
+ * Handles:
234
+ * - Line buffering for large payloads
235
+ * - Content extraction from delta chunks
236
+ * - Thinking/reasoning block extraction
237
+ * - Image data merging and deduplication
238
+ * - Usage statistics extraction
239
+ * - Citations and annotations
240
+ *
241
+ * Extracted from utils/apiUtils.ts processStreamingResponse
242
+ */
243
+
244
+ /**
245
+ * Callbacks for streaming updates
246
+ */
247
+ interface StreamCallbacks {
248
+ /** Called when new content arrives */
249
+ onContent: (content: string) => void;
250
+ /** Called when thinking content arrives */
251
+ onThinking: (thinking: string) => void;
252
+ }
253
+ /**
254
+ * StreamProcessor parses SSE streaming responses
255
+ */
256
+ declare class StreamProcessor {
257
+ private accumulatedContent;
258
+ private accumulatedThinking;
259
+ private accumulatedImages;
260
+ private isInThinking;
261
+ private isInContent;
262
+ /**
263
+ * Process a streaming response
264
+ */
265
+ process(response: Response, callbacks: StreamCallbacks, modelId?: string): Promise<StreamingResult>;
266
+ /**
267
+ * Parse a single SSE line
268
+ */
269
+ private _parseLine;
270
+ /**
271
+ * Handle content delta with thinking support
272
+ */
273
+ private _handleContent;
274
+ /**
275
+ * Handle thinking/reasoning content
276
+ */
277
+ private _handleThinking;
278
+ /**
279
+ * Extract thinking blocks from content (for models with inline thinking)
280
+ */
281
+ private _extractThinkingFromContent;
282
+ /**
283
+ * Merge images into accumulated array, avoiding duplicates
284
+ */
285
+ private _mergeImages;
286
+ }
287
+
288
+ export { type AlertLevel, type FetchOptions, type ModelProviderPrice, ProviderManager, type RouteRequestParams, RoutstrClient, type RoutstrClientMode, type StreamCallbacks, StreamProcessor };
@@ -0,0 +1,288 @@
1
+ import { M as Model, a as Message, T as TransactionHistory, l as StreamingResult } from '../types-BlHjmWRK.js';
2
+ import { P as ProviderRegistry, W as WalletAdapter, S as StorageAdapter, a as StreamingCallbacks } from '../interfaces-BbAtkq7z.js';
3
+ import { CashuSpender, BalanceManager } from '../wallet/index.js';
4
+
5
+ /**
6
+ * ProviderManager - Handles provider selection and failover logic
7
+ *
8
+ * Handles:
9
+ * - Finding the best provider for a model based on price
10
+ * - Provider failover when errors occur
11
+ * - Tracking failed providers to avoid retry loops
12
+ * - Provider version compatibility
13
+ *
14
+ * Extracted from utils/apiUtils.ts findNextBestProvider and related logic
15
+ */
16
+
17
+ interface ModelProviderPrice {
18
+ baseUrl: string;
19
+ model: Model;
20
+ promptPerMillion: number;
21
+ completionPerMillion: number;
22
+ totalPerMillion: number;
23
+ }
24
+ declare class ProviderManager {
25
+ private providerRegistry;
26
+ private failedProviders;
27
+ constructor(providerRegistry: ProviderRegistry);
28
+ /**
29
+ * Reset the failed providers list
30
+ */
31
+ resetFailedProviders(): void;
32
+ /**
33
+ * Mark a provider as failed
34
+ */
35
+ markFailed(baseUrl: string): void;
36
+ /**
37
+ * Check if a provider has failed
38
+ */
39
+ hasFailed(baseUrl: string): boolean;
40
+ /**
41
+ * Find the next best provider for a model
42
+ * @param modelId The model ID to find a provider for
43
+ * @param currentBaseUrl The current provider to exclude
44
+ * @returns The best provider URL or null if none available
45
+ */
46
+ findNextBestProvider(modelId: string, currentBaseUrl: string): string | null;
47
+ /**
48
+ * Find the best model for a provider
49
+ * Useful when switching providers and need to find equivalent model
50
+ */
51
+ getModelForProvider(baseUrl: string, modelId: string): Promise<Model | null>;
52
+ /**
53
+ * Get all available providers for a model
54
+ * Returns sorted list by price
55
+ */
56
+ getAllProvidersForModel(modelId: string): Array<{
57
+ baseUrl: string;
58
+ model: Model;
59
+ cost: number;
60
+ }>;
61
+ /**
62
+ * Get providers for a model sorted by prompt+completion pricing
63
+ */
64
+ getProviderPriceRankingForModel(modelId: string, options?: {
65
+ torMode?: boolean;
66
+ includeDisabled?: boolean;
67
+ }): ModelProviderPrice[];
68
+ /**
69
+ * Get best-priced provider for a specific model
70
+ */
71
+ getBestProviderForModel(modelId: string, options?: {
72
+ torMode?: boolean;
73
+ includeDisabled?: boolean;
74
+ }): string | null;
75
+ private normalizeModelId;
76
+ /**
77
+ * Check if a provider accepts a specific mint
78
+ */
79
+ providerAcceptsMint(baseUrl: string, mintUrl: string): boolean;
80
+ /**
81
+ * Get required sats for a model based on message history
82
+ * Simple estimation based on typical usage
83
+ */
84
+ getRequiredSatsForModel(model: Model, apiMessages: any[], maxTokens?: number): number;
85
+ }
86
+
87
+ /**
88
+ * RoutstrClient - Main API client for Routstr
89
+ *
90
+ * Orchestrates:
91
+ * - Token spending via CashuSpender
92
+ * - API requests with authentication
93
+ * - Streaming response processing
94
+ * - Provider failover via ProviderManager
95
+ * - Error handling and refunds
96
+ *
97
+ * Extracted from utils/apiUtils.ts
98
+ */
99
+
100
+ /**
101
+ * Options for fetching AI response
102
+ */
103
+ interface FetchOptions {
104
+ messageHistory: Message[];
105
+ selectedModel: Model;
106
+ baseUrl: string;
107
+ mintUrl: string;
108
+ balance: number;
109
+ transactionHistory: TransactionHistory[];
110
+ maxTokens?: number;
111
+ headers?: Record<string, string>;
112
+ }
113
+ /**
114
+ * RoutstrClient is the main SDK entry point
115
+ */
116
+ type AlertLevel = "max" | "min";
117
+ type RoutstrClientMode = "xcashu" | "lazyrefund" | "apikeys";
118
+ interface RouteRequestParams {
119
+ path: string;
120
+ method: string;
121
+ body?: unknown;
122
+ headers?: Record<string, string>;
123
+ baseUrl: string;
124
+ mintUrl: string;
125
+ modelId?: string;
126
+ }
127
+ declare class RoutstrClient {
128
+ private walletAdapter;
129
+ private storageAdapter;
130
+ private providerRegistry;
131
+ private cashuSpender;
132
+ private balanceManager;
133
+ private streamProcessor;
134
+ private providerManager;
135
+ private alertLevel;
136
+ private mode;
137
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry: ProviderRegistry, alertLevel: AlertLevel, mode?: RoutstrClientMode);
138
+ /**
139
+ * Get the current client mode
140
+ */
141
+ getMode(): RoutstrClientMode;
142
+ /**
143
+ * Get the CashuSpender instance
144
+ */
145
+ getCashuSpender(): CashuSpender;
146
+ /**
147
+ * Get the BalanceManager instance
148
+ */
149
+ getBalanceManager(): BalanceManager;
150
+ /**
151
+ * Get the ProviderManager instance
152
+ */
153
+ getProviderManager(): ProviderManager;
154
+ /**
155
+ * Check if the client is currently busy (in critical section)
156
+ */
157
+ get isBusy(): boolean;
158
+ /**
159
+ * Route an API request to the upstream provider
160
+ *
161
+ * This is a simpler alternative to fetchAIResponse that just proxies
162
+ * the request upstream without the streaming callback machinery.
163
+ * Useful for daemon-style routing where you just need to forward
164
+ * requests and get responses back.
165
+ */
166
+ routeRequest(params: RouteRequestParams): Promise<Response>;
167
+ /**
168
+ * Fetch AI response with streaming
169
+ */
170
+ fetchAIResponse(options: FetchOptions, callbacks: StreamingCallbacks): Promise<void>;
171
+ /**
172
+ * Make the API request with failover support
173
+ */
174
+ private _makeRequest;
175
+ /**
176
+ * Handle error responses with failover
177
+ */
178
+ private _handleErrorResponse;
179
+ /**
180
+ * Handle post-response balance update for all modes
181
+ */
182
+ private _handlePostResponseBalanceUpdate;
183
+ /**
184
+ * Convert messages for API format
185
+ */
186
+ private _convertMessages;
187
+ /**
188
+ * Create assistant message from streaming result
189
+ */
190
+ private _createAssistantMessage;
191
+ /**
192
+ * Create a child key for a parent API key via the provider's API
193
+ * POST /v1/balance/child-key
194
+ */
195
+ private _createChildKey;
196
+ /**
197
+ * Calculate estimated costs from usage
198
+ */
199
+ private _getEstimatedCosts;
200
+ /**
201
+ * Get pending cashu token amount
202
+ */
203
+ private _getPendingCashuTokenAmount;
204
+ /**
205
+ * Check if error message indicates a network error
206
+ */
207
+ private _isNetworkError;
208
+ /**
209
+ * Handle errors and notify callbacks
210
+ */
211
+ private _handleError;
212
+ /**
213
+ * Check wallet balance and throw if insufficient
214
+ */
215
+ private _checkBalance;
216
+ /**
217
+ * Spend a token using CashuSpender with standardized error handling
218
+ */
219
+ private _spendToken;
220
+ /**
221
+ * Build request headers with common defaults and dev mock controls
222
+ */
223
+ private _buildBaseHeaders;
224
+ /**
225
+ * Attach auth headers using the active client mode
226
+ */
227
+ private _withAuthHeader;
228
+ }
229
+
230
+ /**
231
+ * StreamProcessor - Handles SSE streaming response parsing
232
+ *
233
+ * Handles:
234
+ * - Line buffering for large payloads
235
+ * - Content extraction from delta chunks
236
+ * - Thinking/reasoning block extraction
237
+ * - Image data merging and deduplication
238
+ * - Usage statistics extraction
239
+ * - Citations and annotations
240
+ *
241
+ * Extracted from utils/apiUtils.ts processStreamingResponse
242
+ */
243
+
244
+ /**
245
+ * Callbacks for streaming updates
246
+ */
247
+ interface StreamCallbacks {
248
+ /** Called when new content arrives */
249
+ onContent: (content: string) => void;
250
+ /** Called when thinking content arrives */
251
+ onThinking: (thinking: string) => void;
252
+ }
253
+ /**
254
+ * StreamProcessor parses SSE streaming responses
255
+ */
256
+ declare class StreamProcessor {
257
+ private accumulatedContent;
258
+ private accumulatedThinking;
259
+ private accumulatedImages;
260
+ private isInThinking;
261
+ private isInContent;
262
+ /**
263
+ * Process a streaming response
264
+ */
265
+ process(response: Response, callbacks: StreamCallbacks, modelId?: string): Promise<StreamingResult>;
266
+ /**
267
+ * Parse a single SSE line
268
+ */
269
+ private _parseLine;
270
+ /**
271
+ * Handle content delta with thinking support
272
+ */
273
+ private _handleContent;
274
+ /**
275
+ * Handle thinking/reasoning content
276
+ */
277
+ private _handleThinking;
278
+ /**
279
+ * Extract thinking blocks from content (for models with inline thinking)
280
+ */
281
+ private _extractThinkingFromContent;
282
+ /**
283
+ * Merge images into accumulated array, avoiding duplicates
284
+ */
285
+ private _mergeImages;
286
+ }
287
+
288
+ export { type AlertLevel, type FetchOptions, type ModelProviderPrice, ProviderManager, type RouteRequestParams, RoutstrClient, type RoutstrClientMode, type StreamCallbacks, StreamProcessor };