@routstr/sdk 0.3.9 → 0.3.11
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 +6413 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.mjs +6361 -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 +6791 -0
- package/dist/bun.js.map +1 -0
- package/dist/bun.mjs +6733 -0
- package/dist/bun.mjs.map +1 -0
- package/dist/bunSqlite-BmXWNc25.d.ts +18 -0
- package/dist/bunSqlite-Bro9efsl.d.mts +18 -0
- package/dist/client/index.d.mts +85 -42
- package/dist/client/index.d.ts +85 -42
- package/dist/client/index.js +1243 -1584
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +1239 -1585
- 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 +30 -31
- package/dist/discovery/index.js.map +1 -1
- package/dist/discovery/index.mjs +30 -31
- package/dist/discovery/index.mjs.map +1 -1
- package/dist/index.d.mts +9 -7
- package/dist/index.d.ts +9 -7
- package/dist/index.js +1264 -1648
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1260 -1645
- 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 +6857 -0
- package/dist/node.js.map +1 -0
- package/dist/node.mjs +6801 -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 +1970 -0
- package/dist/storage/bun.js.map +1 -0
- package/dist/storage/bun.mjs +1946 -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 +238 -650
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/index.mjs +239 -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 +2034 -0
- package/dist/storage/node.js.map +1 -0
- package/dist/storage/node.mjs +2012 -0
- package/dist/storage/node.mjs.map +1 -0
- package/dist/{store-58VcEUoA.d.ts → store-CAQLSbEj.d.ts} +52 -1
- package/dist/{store-C6dfj1cc.d.mts → store-CuXwe5Rg.d.mts} +52 -1
- package/dist/wallet/index.js +38 -24
- package/dist/wallet/index.js.map +1 -1
- package/dist/wallet/index.mjs +38 -24
- package/dist/wallet/index.mjs.map +1 -1
- package/package.json +26 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { a as StorageDriver, c as UsageTrackingDriver } from './store-CAQLSbEj.js';
|
|
2
|
+
import { S as SdkLogger } from './types-_21yYFZG.js';
|
|
3
|
+
|
|
4
|
+
declare function createBunSqliteDriver(dbPath: string, options?: {
|
|
5
|
+
logger?: SdkLogger;
|
|
6
|
+
}): Promise<StorageDriver>;
|
|
7
|
+
|
|
8
|
+
interface BunSqliteUsageTrackingDriverOptions {
|
|
9
|
+
dbPath?: string;
|
|
10
|
+
tableName?: string;
|
|
11
|
+
legacyStorageDriver?: StorageDriver;
|
|
12
|
+
sqlite?: {
|
|
13
|
+
Database: any;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
declare const createBunSqliteUsageTrackingDriver: (options?: BunSqliteUsageTrackingDriverOptions) => UsageTrackingDriver;
|
|
17
|
+
|
|
18
|
+
export { type BunSqliteUsageTrackingDriverOptions as B, createBunSqliteUsageTrackingDriver as a, createBunSqliteDriver as c };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { a as StorageDriver, c as UsageTrackingDriver } from './store-CuXwe5Rg.mjs';
|
|
2
|
+
import { S as SdkLogger } from './types-_21yYFZG.mjs';
|
|
3
|
+
|
|
4
|
+
declare function createBunSqliteDriver(dbPath: string, options?: {
|
|
5
|
+
logger?: SdkLogger;
|
|
6
|
+
}): Promise<StorageDriver>;
|
|
7
|
+
|
|
8
|
+
interface BunSqliteUsageTrackingDriverOptions {
|
|
9
|
+
dbPath?: string;
|
|
10
|
+
tableName?: string;
|
|
11
|
+
legacyStorageDriver?: StorageDriver;
|
|
12
|
+
sqlite?: {
|
|
13
|
+
Database: any;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
declare const createBunSqliteUsageTrackingDriver: (options?: BunSqliteUsageTrackingDriverOptions) => UsageTrackingDriver;
|
|
17
|
+
|
|
18
|
+
export { type BunSqliteUsageTrackingDriverOptions as B, createBunSqliteUsageTrackingDriver as a, createBunSqliteDriver as c };
|
package/dist/client/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { e as Model, S as SdkLogger,
|
|
1
|
+
import { e as Model, S as SdkLogger, m as StreamingResult, U as UsageStats, M as Message, n as TransactionHistory } from '../types-_21yYFZG.mjs';
|
|
2
2
|
import { P as ProviderRegistry, W as WalletAdapter, S as StorageAdapter, a as StreamingCallbacks } from '../interfaces-Csn8Uq04.mjs';
|
|
3
|
-
import { S as SdkStore,
|
|
3
|
+
import { S as SdkStore, c as UsageTrackingDriver } from '../store-CuXwe5Rg.mjs';
|
|
4
4
|
import { CashuSpender, BalanceManager } from '../wallet/index.mjs';
|
|
5
5
|
import { Transform } from 'stream';
|
|
6
6
|
import 'zustand/vanilla';
|
|
@@ -25,6 +25,9 @@ interface ModelProviderPrice {
|
|
|
25
25
|
completionPerMillion: number;
|
|
26
26
|
totalPerMillion: number;
|
|
27
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* ProviderManager handles provider selection and failover
|
|
30
|
+
*/
|
|
28
31
|
declare class ProviderManager {
|
|
29
32
|
private providerRegistry;
|
|
30
33
|
private failedProviders;
|
|
@@ -162,19 +165,6 @@ declare class ProviderManager {
|
|
|
162
165
|
* Extracted from utils/apiUtils.ts
|
|
163
166
|
*/
|
|
164
167
|
|
|
165
|
-
/**
|
|
166
|
-
* Options for fetching AI response
|
|
167
|
-
*/
|
|
168
|
-
interface FetchOptions {
|
|
169
|
-
messageHistory: Message[];
|
|
170
|
-
selectedModel: Model;
|
|
171
|
-
baseUrl: string;
|
|
172
|
-
mintUrl: string;
|
|
173
|
-
balance: number;
|
|
174
|
-
transactionHistory: TransactionHistory[];
|
|
175
|
-
maxTokens?: number;
|
|
176
|
-
headers?: Record<string, string>;
|
|
177
|
-
}
|
|
178
168
|
/**
|
|
179
169
|
* RoutstrClient is the main SDK entry point
|
|
180
170
|
*/
|
|
@@ -191,6 +181,23 @@ interface RouteRequestParams {
|
|
|
191
181
|
modelId?: string;
|
|
192
182
|
clientApiKey?: string;
|
|
193
183
|
}
|
|
184
|
+
interface RequestResponseLogRequestInput {
|
|
185
|
+
method: string;
|
|
186
|
+
url: string;
|
|
187
|
+
path: string;
|
|
188
|
+
baseUrl: string;
|
|
189
|
+
headers: Record<string, string>;
|
|
190
|
+
body?: unknown;
|
|
191
|
+
rawBody?: string;
|
|
192
|
+
}
|
|
193
|
+
interface RequestResponseLogSink {
|
|
194
|
+
logRequest?(input: RequestResponseLogRequestInput): string | undefined | Promise<string | undefined>;
|
|
195
|
+
logResponseStart?(id: string | undefined, response: Response): void | Promise<void>;
|
|
196
|
+
logResponseChunk?(id: string | undefined, sequence: number, text: string): void | Promise<void>;
|
|
197
|
+
logResponseEnd?(id: string | undefined): void | Promise<void>;
|
|
198
|
+
logResponseError?(id: string | undefined, error: unknown): void | Promise<void>;
|
|
199
|
+
logResponseBody?(id: string | undefined, response: Response): void | Promise<void>;
|
|
200
|
+
}
|
|
194
201
|
interface RoutstrClientConfig {
|
|
195
202
|
usageTrackingDriver?: UsageTrackingDriver;
|
|
196
203
|
sdkStore?: SdkStore;
|
|
@@ -198,6 +205,8 @@ interface RoutstrClientConfig {
|
|
|
198
205
|
providerManager?: ProviderManager;
|
|
199
206
|
/** Optional: injectable logger (defaults to consoleLogger) */
|
|
200
207
|
logger?: SdkLogger;
|
|
208
|
+
/** Optional: raw request/response logging callbacks supplied by the runtime/app. */
|
|
209
|
+
requestResponseLogSink?: RequestResponseLogSink;
|
|
201
210
|
}
|
|
202
211
|
declare class RoutstrClient {
|
|
203
212
|
private walletAdapter;
|
|
@@ -205,7 +214,6 @@ declare class RoutstrClient {
|
|
|
205
214
|
private providerRegistry;
|
|
206
215
|
private cashuSpender;
|
|
207
216
|
private balanceManager;
|
|
208
|
-
private streamProcessor;
|
|
209
217
|
private providerManager;
|
|
210
218
|
private alertLevel;
|
|
211
219
|
private mode;
|
|
@@ -213,6 +221,7 @@ declare class RoutstrClient {
|
|
|
213
221
|
private usageTrackingDriver?;
|
|
214
222
|
private sdkStore?;
|
|
215
223
|
private logger;
|
|
224
|
+
private requestResponseLogSink?;
|
|
216
225
|
constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry: ProviderRegistry, alertLevel: AlertLevel, mode?: RoutstrClientMode, options?: RoutstrClientConfig);
|
|
217
226
|
/**
|
|
218
227
|
* Get the current client mode
|
|
@@ -251,10 +260,6 @@ declare class RoutstrClient {
|
|
|
251
260
|
* Extract clientApiKey from Authorization Bearer token if present
|
|
252
261
|
*/
|
|
253
262
|
private _extractClientApiKey;
|
|
254
|
-
/**
|
|
255
|
-
* Fetch AI response with streaming
|
|
256
|
-
*/
|
|
257
|
-
fetchAIResponse(options: FetchOptions, callbacks: StreamingCallbacks): Promise<void>;
|
|
258
263
|
/**
|
|
259
264
|
* Make the API request with failover support
|
|
260
265
|
*/
|
|
@@ -268,26 +273,6 @@ declare class RoutstrClient {
|
|
|
268
273
|
*/
|
|
269
274
|
private _handlePostResponseBalanceUpdate;
|
|
270
275
|
private _trackResponseUsage;
|
|
271
|
-
/**
|
|
272
|
-
* Convert messages for API format
|
|
273
|
-
*/
|
|
274
|
-
private _convertMessages;
|
|
275
|
-
/**
|
|
276
|
-
* Create assistant message from streaming result
|
|
277
|
-
*/
|
|
278
|
-
private _createAssistantMessage;
|
|
279
|
-
/**
|
|
280
|
-
* Calculate estimated costs from usage
|
|
281
|
-
*/
|
|
282
|
-
private _getEstimatedCosts;
|
|
283
|
-
/**
|
|
284
|
-
* Get pending API key amount
|
|
285
|
-
*/
|
|
286
|
-
private _getPendingCashuTokenAmount;
|
|
287
|
-
/**
|
|
288
|
-
* Handle errors and notify callbacks
|
|
289
|
-
*/
|
|
290
|
-
private _handleError;
|
|
291
276
|
/**
|
|
292
277
|
* Check wallet balance and throw if insufficient
|
|
293
278
|
*/
|
|
@@ -370,7 +355,24 @@ interface UsageTrackingData {
|
|
|
370
355
|
totalTokens: number;
|
|
371
356
|
cost: number;
|
|
372
357
|
satsCost: number;
|
|
358
|
+
/** Upstream provider/route that handled the request (e.g. "openrouter:openrouter:Anthropic"). */
|
|
359
|
+
provider?: string;
|
|
360
|
+
/** Full cost breakdown emitted by the upstream `cost` object. */
|
|
361
|
+
baseMsats?: number;
|
|
362
|
+
inputMsats?: number;
|
|
363
|
+
outputMsats?: number;
|
|
364
|
+
totalMsats?: number;
|
|
365
|
+
totalUsd?: number;
|
|
366
|
+
cacheReadInputTokens?: number;
|
|
367
|
+
cacheCreationInputTokens?: number;
|
|
368
|
+
cacheReadMsats?: number;
|
|
369
|
+
cacheCreationMsats?: number;
|
|
370
|
+
remainingBalanceMsats?: number;
|
|
373
371
|
}
|
|
372
|
+
declare function extractUsageFromResponseBody(body: unknown, fallbackSatsCost?: number): UsageTrackingData | null;
|
|
373
|
+
declare function extractResponseId(body: unknown): string | undefined;
|
|
374
|
+
declare function extractUsageFromSSEJson(parsed: any, fallbackSatsCost?: number): UsageTrackingData | null;
|
|
375
|
+
declare function toUsageStats(usage: UsageTrackingData | null | undefined): UsageStats | undefined;
|
|
374
376
|
|
|
375
377
|
/**
|
|
376
378
|
* Inspect a Web `ReadableStream<Uint8Array>` of SSE bytes for `usage` and
|
|
@@ -385,7 +387,10 @@ interface UsageTrackingData {
|
|
|
385
387
|
* The returned Promise resolves with the final captured values once the
|
|
386
388
|
* stream ends (or is cancelled / errors out).
|
|
387
389
|
*/
|
|
388
|
-
declare function inspectSSEWebStream(stream: ReadableStream<Uint8Array>, onUsage: (usage: UsageTrackingData) => void, onResponseId?: (responseId: string) => void
|
|
390
|
+
declare function inspectSSEWebStream(stream: ReadableStream<Uint8Array>, onUsage: (usage: UsageTrackingData) => void, onResponseId?: (responseId: string) => void, options?: {
|
|
391
|
+
/** Called with each raw chunk read from the tee'd inspection branch. */
|
|
392
|
+
onRawChunk?: (chunk: Uint8Array, sequence: number, text: string) => void | Promise<void>;
|
|
393
|
+
}): Promise<{
|
|
389
394
|
capturedUsage?: UsageTrackingData;
|
|
390
395
|
capturedResponseId?: string;
|
|
391
396
|
}>;
|
|
@@ -408,4 +413,42 @@ declare function inspectSSEWebStream(stream: ReadableStream<Uint8Array>, onUsage
|
|
|
408
413
|
*/
|
|
409
414
|
declare function createSSEParserTransform(onUsage: (usage: UsageTrackingData) => void, onResponseId?: (responseId: string) => void): Transform;
|
|
410
415
|
|
|
411
|
-
|
|
416
|
+
/**
|
|
417
|
+
* Options for fetching AI response
|
|
418
|
+
*/
|
|
419
|
+
interface FetchOptions {
|
|
420
|
+
messageHistory: Message[];
|
|
421
|
+
selectedModel: Model;
|
|
422
|
+
baseUrl: string;
|
|
423
|
+
mintUrl: string;
|
|
424
|
+
balance: number;
|
|
425
|
+
transactionHistory: TransactionHistory[];
|
|
426
|
+
maxTokens?: number;
|
|
427
|
+
headers?: Record<string, string>;
|
|
428
|
+
}
|
|
429
|
+
interface FetchAIResponseClient {
|
|
430
|
+
routeRequest(params: {
|
|
431
|
+
path: string;
|
|
432
|
+
method: string;
|
|
433
|
+
body?: unknown;
|
|
434
|
+
headers?: Record<string, string>;
|
|
435
|
+
baseUrl: string;
|
|
436
|
+
mintUrl: string;
|
|
437
|
+
modelId?: string;
|
|
438
|
+
}): Promise<Response>;
|
|
439
|
+
getMode(): RoutstrClientMode;
|
|
440
|
+
}
|
|
441
|
+
interface FetchAIResponseDeps {
|
|
442
|
+
client: FetchAIResponseClient;
|
|
443
|
+
alertLevel: AlertLevel;
|
|
444
|
+
logger: SdkLogger;
|
|
445
|
+
getPendingCashuTokenAmount?: () => number;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Fetch an AI chat/completions response using RoutstrClient.routeRequest for
|
|
449
|
+
* payment/auth/failover/accounting, then consume the returned SSE stream and
|
|
450
|
+
* drive the legacy streaming callbacks.
|
|
451
|
+
*/
|
|
452
|
+
declare function fetchAIResponse(options: FetchOptions, callbacks: StreamingCallbacks, deps: FetchAIResponseDeps): Promise<void>;
|
|
453
|
+
|
|
454
|
+
export { type AlertLevel, type DebugLevel, type FetchAIResponseDeps, type ModelProviderPrice, ProviderManager, type RequestResponseLogRequestInput, type RequestResponseLogSink, type RouteRequestParams, RoutstrClient, type RoutstrClientConfig, type RoutstrClientMode, type StreamCallbacks, StreamProcessor, type UsageTrackingData, createSSEParserTransform, extractResponseId, extractUsageFromResponseBody, extractUsageFromSSEJson, fetchAIResponse, inspectSSEWebStream, toUsageStats };
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { e as Model, S as SdkLogger,
|
|
1
|
+
import { e as Model, S as SdkLogger, m as StreamingResult, U as UsageStats, M as Message, n as TransactionHistory } from '../types-_21yYFZG.js';
|
|
2
2
|
import { P as ProviderRegistry, W as WalletAdapter, S as StorageAdapter, a as StreamingCallbacks } from '../interfaces-C-DYd9Jy.js';
|
|
3
|
-
import { S as SdkStore,
|
|
3
|
+
import { S as SdkStore, c as UsageTrackingDriver } from '../store-CAQLSbEj.js';
|
|
4
4
|
import { CashuSpender, BalanceManager } from '../wallet/index.js';
|
|
5
5
|
import { Transform } from 'stream';
|
|
6
6
|
import 'zustand/vanilla';
|
|
@@ -25,6 +25,9 @@ interface ModelProviderPrice {
|
|
|
25
25
|
completionPerMillion: number;
|
|
26
26
|
totalPerMillion: number;
|
|
27
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* ProviderManager handles provider selection and failover
|
|
30
|
+
*/
|
|
28
31
|
declare class ProviderManager {
|
|
29
32
|
private providerRegistry;
|
|
30
33
|
private failedProviders;
|
|
@@ -162,19 +165,6 @@ declare class ProviderManager {
|
|
|
162
165
|
* Extracted from utils/apiUtils.ts
|
|
163
166
|
*/
|
|
164
167
|
|
|
165
|
-
/**
|
|
166
|
-
* Options for fetching AI response
|
|
167
|
-
*/
|
|
168
|
-
interface FetchOptions {
|
|
169
|
-
messageHistory: Message[];
|
|
170
|
-
selectedModel: Model;
|
|
171
|
-
baseUrl: string;
|
|
172
|
-
mintUrl: string;
|
|
173
|
-
balance: number;
|
|
174
|
-
transactionHistory: TransactionHistory[];
|
|
175
|
-
maxTokens?: number;
|
|
176
|
-
headers?: Record<string, string>;
|
|
177
|
-
}
|
|
178
168
|
/**
|
|
179
169
|
* RoutstrClient is the main SDK entry point
|
|
180
170
|
*/
|
|
@@ -191,6 +181,23 @@ interface RouteRequestParams {
|
|
|
191
181
|
modelId?: string;
|
|
192
182
|
clientApiKey?: string;
|
|
193
183
|
}
|
|
184
|
+
interface RequestResponseLogRequestInput {
|
|
185
|
+
method: string;
|
|
186
|
+
url: string;
|
|
187
|
+
path: string;
|
|
188
|
+
baseUrl: string;
|
|
189
|
+
headers: Record<string, string>;
|
|
190
|
+
body?: unknown;
|
|
191
|
+
rawBody?: string;
|
|
192
|
+
}
|
|
193
|
+
interface RequestResponseLogSink {
|
|
194
|
+
logRequest?(input: RequestResponseLogRequestInput): string | undefined | Promise<string | undefined>;
|
|
195
|
+
logResponseStart?(id: string | undefined, response: Response): void | Promise<void>;
|
|
196
|
+
logResponseChunk?(id: string | undefined, sequence: number, text: string): void | Promise<void>;
|
|
197
|
+
logResponseEnd?(id: string | undefined): void | Promise<void>;
|
|
198
|
+
logResponseError?(id: string | undefined, error: unknown): void | Promise<void>;
|
|
199
|
+
logResponseBody?(id: string | undefined, response: Response): void | Promise<void>;
|
|
200
|
+
}
|
|
194
201
|
interface RoutstrClientConfig {
|
|
195
202
|
usageTrackingDriver?: UsageTrackingDriver;
|
|
196
203
|
sdkStore?: SdkStore;
|
|
@@ -198,6 +205,8 @@ interface RoutstrClientConfig {
|
|
|
198
205
|
providerManager?: ProviderManager;
|
|
199
206
|
/** Optional: injectable logger (defaults to consoleLogger) */
|
|
200
207
|
logger?: SdkLogger;
|
|
208
|
+
/** Optional: raw request/response logging callbacks supplied by the runtime/app. */
|
|
209
|
+
requestResponseLogSink?: RequestResponseLogSink;
|
|
201
210
|
}
|
|
202
211
|
declare class RoutstrClient {
|
|
203
212
|
private walletAdapter;
|
|
@@ -205,7 +214,6 @@ declare class RoutstrClient {
|
|
|
205
214
|
private providerRegistry;
|
|
206
215
|
private cashuSpender;
|
|
207
216
|
private balanceManager;
|
|
208
|
-
private streamProcessor;
|
|
209
217
|
private providerManager;
|
|
210
218
|
private alertLevel;
|
|
211
219
|
private mode;
|
|
@@ -213,6 +221,7 @@ declare class RoutstrClient {
|
|
|
213
221
|
private usageTrackingDriver?;
|
|
214
222
|
private sdkStore?;
|
|
215
223
|
private logger;
|
|
224
|
+
private requestResponseLogSink?;
|
|
216
225
|
constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry: ProviderRegistry, alertLevel: AlertLevel, mode?: RoutstrClientMode, options?: RoutstrClientConfig);
|
|
217
226
|
/**
|
|
218
227
|
* Get the current client mode
|
|
@@ -251,10 +260,6 @@ declare class RoutstrClient {
|
|
|
251
260
|
* Extract clientApiKey from Authorization Bearer token if present
|
|
252
261
|
*/
|
|
253
262
|
private _extractClientApiKey;
|
|
254
|
-
/**
|
|
255
|
-
* Fetch AI response with streaming
|
|
256
|
-
*/
|
|
257
|
-
fetchAIResponse(options: FetchOptions, callbacks: StreamingCallbacks): Promise<void>;
|
|
258
263
|
/**
|
|
259
264
|
* Make the API request with failover support
|
|
260
265
|
*/
|
|
@@ -268,26 +273,6 @@ declare class RoutstrClient {
|
|
|
268
273
|
*/
|
|
269
274
|
private _handlePostResponseBalanceUpdate;
|
|
270
275
|
private _trackResponseUsage;
|
|
271
|
-
/**
|
|
272
|
-
* Convert messages for API format
|
|
273
|
-
*/
|
|
274
|
-
private _convertMessages;
|
|
275
|
-
/**
|
|
276
|
-
* Create assistant message from streaming result
|
|
277
|
-
*/
|
|
278
|
-
private _createAssistantMessage;
|
|
279
|
-
/**
|
|
280
|
-
* Calculate estimated costs from usage
|
|
281
|
-
*/
|
|
282
|
-
private _getEstimatedCosts;
|
|
283
|
-
/**
|
|
284
|
-
* Get pending API key amount
|
|
285
|
-
*/
|
|
286
|
-
private _getPendingCashuTokenAmount;
|
|
287
|
-
/**
|
|
288
|
-
* Handle errors and notify callbacks
|
|
289
|
-
*/
|
|
290
|
-
private _handleError;
|
|
291
276
|
/**
|
|
292
277
|
* Check wallet balance and throw if insufficient
|
|
293
278
|
*/
|
|
@@ -370,7 +355,24 @@ interface UsageTrackingData {
|
|
|
370
355
|
totalTokens: number;
|
|
371
356
|
cost: number;
|
|
372
357
|
satsCost: number;
|
|
358
|
+
/** Upstream provider/route that handled the request (e.g. "openrouter:openrouter:Anthropic"). */
|
|
359
|
+
provider?: string;
|
|
360
|
+
/** Full cost breakdown emitted by the upstream `cost` object. */
|
|
361
|
+
baseMsats?: number;
|
|
362
|
+
inputMsats?: number;
|
|
363
|
+
outputMsats?: number;
|
|
364
|
+
totalMsats?: number;
|
|
365
|
+
totalUsd?: number;
|
|
366
|
+
cacheReadInputTokens?: number;
|
|
367
|
+
cacheCreationInputTokens?: number;
|
|
368
|
+
cacheReadMsats?: number;
|
|
369
|
+
cacheCreationMsats?: number;
|
|
370
|
+
remainingBalanceMsats?: number;
|
|
373
371
|
}
|
|
372
|
+
declare function extractUsageFromResponseBody(body: unknown, fallbackSatsCost?: number): UsageTrackingData | null;
|
|
373
|
+
declare function extractResponseId(body: unknown): string | undefined;
|
|
374
|
+
declare function extractUsageFromSSEJson(parsed: any, fallbackSatsCost?: number): UsageTrackingData | null;
|
|
375
|
+
declare function toUsageStats(usage: UsageTrackingData | null | undefined): UsageStats | undefined;
|
|
374
376
|
|
|
375
377
|
/**
|
|
376
378
|
* Inspect a Web `ReadableStream<Uint8Array>` of SSE bytes for `usage` and
|
|
@@ -385,7 +387,10 @@ interface UsageTrackingData {
|
|
|
385
387
|
* The returned Promise resolves with the final captured values once the
|
|
386
388
|
* stream ends (or is cancelled / errors out).
|
|
387
389
|
*/
|
|
388
|
-
declare function inspectSSEWebStream(stream: ReadableStream<Uint8Array>, onUsage: (usage: UsageTrackingData) => void, onResponseId?: (responseId: string) => void
|
|
390
|
+
declare function inspectSSEWebStream(stream: ReadableStream<Uint8Array>, onUsage: (usage: UsageTrackingData) => void, onResponseId?: (responseId: string) => void, options?: {
|
|
391
|
+
/** Called with each raw chunk read from the tee'd inspection branch. */
|
|
392
|
+
onRawChunk?: (chunk: Uint8Array, sequence: number, text: string) => void | Promise<void>;
|
|
393
|
+
}): Promise<{
|
|
389
394
|
capturedUsage?: UsageTrackingData;
|
|
390
395
|
capturedResponseId?: string;
|
|
391
396
|
}>;
|
|
@@ -408,4 +413,42 @@ declare function inspectSSEWebStream(stream: ReadableStream<Uint8Array>, onUsage
|
|
|
408
413
|
*/
|
|
409
414
|
declare function createSSEParserTransform(onUsage: (usage: UsageTrackingData) => void, onResponseId?: (responseId: string) => void): Transform;
|
|
410
415
|
|
|
411
|
-
|
|
416
|
+
/**
|
|
417
|
+
* Options for fetching AI response
|
|
418
|
+
*/
|
|
419
|
+
interface FetchOptions {
|
|
420
|
+
messageHistory: Message[];
|
|
421
|
+
selectedModel: Model;
|
|
422
|
+
baseUrl: string;
|
|
423
|
+
mintUrl: string;
|
|
424
|
+
balance: number;
|
|
425
|
+
transactionHistory: TransactionHistory[];
|
|
426
|
+
maxTokens?: number;
|
|
427
|
+
headers?: Record<string, string>;
|
|
428
|
+
}
|
|
429
|
+
interface FetchAIResponseClient {
|
|
430
|
+
routeRequest(params: {
|
|
431
|
+
path: string;
|
|
432
|
+
method: string;
|
|
433
|
+
body?: unknown;
|
|
434
|
+
headers?: Record<string, string>;
|
|
435
|
+
baseUrl: string;
|
|
436
|
+
mintUrl: string;
|
|
437
|
+
modelId?: string;
|
|
438
|
+
}): Promise<Response>;
|
|
439
|
+
getMode(): RoutstrClientMode;
|
|
440
|
+
}
|
|
441
|
+
interface FetchAIResponseDeps {
|
|
442
|
+
client: FetchAIResponseClient;
|
|
443
|
+
alertLevel: AlertLevel;
|
|
444
|
+
logger: SdkLogger;
|
|
445
|
+
getPendingCashuTokenAmount?: () => number;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Fetch an AI chat/completions response using RoutstrClient.routeRequest for
|
|
449
|
+
* payment/auth/failover/accounting, then consume the returned SSE stream and
|
|
450
|
+
* drive the legacy streaming callbacks.
|
|
451
|
+
*/
|
|
452
|
+
declare function fetchAIResponse(options: FetchOptions, callbacks: StreamingCallbacks, deps: FetchAIResponseDeps): Promise<void>;
|
|
453
|
+
|
|
454
|
+
export { type AlertLevel, type DebugLevel, type FetchAIResponseDeps, type ModelProviderPrice, ProviderManager, type RequestResponseLogRequestInput, type RequestResponseLogSink, type RouteRequestParams, RoutstrClient, type RoutstrClientConfig, type RoutstrClientMode, type StreamCallbacks, StreamProcessor, type UsageTrackingData, createSSEParserTransform, extractResponseId, extractUsageFromResponseBody, extractUsageFromSSEJson, fetchAIResponse, inspectSSEWebStream, toUsageStats };
|