@modelrelay/sdk 0.2.0
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/LICENSE +201 -0
- package/README.md +86 -0
- package/dist/index.cjs +960 -0
- package/dist/index.d.cts +449 -0
- package/dist/index.d.ts +449 -0
- package/dist/index.js +920 -0
- package/package.json +39 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
declare const SDK_VERSION: string;
|
|
2
|
+
declare const DEFAULT_BASE_URL = "https://api.modelrelay.ai/api/v1";
|
|
3
|
+
declare const STAGING_BASE_URL = "https://api-stg.modelrelay.ai/api/v1";
|
|
4
|
+
declare const SANDBOX_BASE_URL = "https://api.sandbox.modelrelay.ai/api/v1";
|
|
5
|
+
declare const DEFAULT_CLIENT_HEADER: string;
|
|
6
|
+
declare const DEFAULT_REQUEST_TIMEOUT_MS = 60000;
|
|
7
|
+
interface ModelRelayOptions {
|
|
8
|
+
/**
|
|
9
|
+
* API key (secret or publishable). Publishable keys are required for frontend token exchange.
|
|
10
|
+
*/
|
|
11
|
+
key?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Bearer token to call the API directly (server or frontend token).
|
|
14
|
+
*/
|
|
15
|
+
token?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Optional environment preset; overridden by `baseUrl` when provided.
|
|
18
|
+
*/
|
|
19
|
+
environment?: Environment;
|
|
20
|
+
baseUrl?: string;
|
|
21
|
+
fetch?: typeof fetch;
|
|
22
|
+
/**
|
|
23
|
+
* Default end-user metadata used when exchanging publishable keys for frontend tokens.
|
|
24
|
+
*/
|
|
25
|
+
endUser?: FrontendIdentity;
|
|
26
|
+
/**
|
|
27
|
+
* Optional client header override for telemetry.
|
|
28
|
+
*/
|
|
29
|
+
clientHeader?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Default request timeout in milliseconds (non-streaming). Set to 0 to disable.
|
|
32
|
+
*/
|
|
33
|
+
timeoutMs?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Retry configuration applied to all requests (can be overridden per call). Set to `false` to disable retries.
|
|
36
|
+
*/
|
|
37
|
+
retry?: RetryConfig | false;
|
|
38
|
+
/**
|
|
39
|
+
* Default HTTP headers applied to every request.
|
|
40
|
+
*/
|
|
41
|
+
defaultHeaders?: Record<string, string>;
|
|
42
|
+
/**
|
|
43
|
+
* Default metadata merged into every chat completion request.
|
|
44
|
+
*/
|
|
45
|
+
defaultMetadata?: Record<string, string>;
|
|
46
|
+
}
|
|
47
|
+
type Environment = "production" | "staging" | "sandbox";
|
|
48
|
+
interface FrontendIdentity {
|
|
49
|
+
id: string;
|
|
50
|
+
deviceId?: string;
|
|
51
|
+
ttlSeconds?: number;
|
|
52
|
+
}
|
|
53
|
+
interface FrontendTokenRequest {
|
|
54
|
+
publishableKey?: string;
|
|
55
|
+
userId: string;
|
|
56
|
+
deviceId?: string;
|
|
57
|
+
ttlSeconds?: number;
|
|
58
|
+
}
|
|
59
|
+
interface FrontendToken {
|
|
60
|
+
token: string;
|
|
61
|
+
expiresAt?: Date;
|
|
62
|
+
expiresIn?: number;
|
|
63
|
+
tokenType?: string;
|
|
64
|
+
keyId?: string;
|
|
65
|
+
sessionId?: string;
|
|
66
|
+
tokenScope?: string[];
|
|
67
|
+
tokenSource?: string;
|
|
68
|
+
/**
|
|
69
|
+
* The end-user identifier used when issuing the token. Added client-side for caching.
|
|
70
|
+
*/
|
|
71
|
+
endUserId?: string;
|
|
72
|
+
/**
|
|
73
|
+
* Publishable key used for issuance. Added client-side for caching.
|
|
74
|
+
*/
|
|
75
|
+
publishableKey?: string;
|
|
76
|
+
/**
|
|
77
|
+
* Device identifier used when issuing the token. Added client-side for caching.
|
|
78
|
+
*/
|
|
79
|
+
deviceId?: string;
|
|
80
|
+
}
|
|
81
|
+
interface CheckoutRequest {
|
|
82
|
+
endUserId: string;
|
|
83
|
+
deviceId?: string;
|
|
84
|
+
planId?: string;
|
|
85
|
+
plan?: string;
|
|
86
|
+
successUrl: string;
|
|
87
|
+
cancelUrl: string;
|
|
88
|
+
}
|
|
89
|
+
interface CheckoutSession {
|
|
90
|
+
id: string;
|
|
91
|
+
plan: string;
|
|
92
|
+
status: string;
|
|
93
|
+
url: string;
|
|
94
|
+
expiresAt?: Date;
|
|
95
|
+
completedAt?: Date;
|
|
96
|
+
}
|
|
97
|
+
interface EndUserRef {
|
|
98
|
+
id: string;
|
|
99
|
+
externalId: string;
|
|
100
|
+
ownerId: string;
|
|
101
|
+
}
|
|
102
|
+
interface CheckoutResponse {
|
|
103
|
+
endUser: EndUserRef;
|
|
104
|
+
session: CheckoutSession;
|
|
105
|
+
}
|
|
106
|
+
interface Usage {
|
|
107
|
+
inputTokens: number;
|
|
108
|
+
outputTokens: number;
|
|
109
|
+
totalTokens: number;
|
|
110
|
+
}
|
|
111
|
+
interface ChatMessage {
|
|
112
|
+
role: string;
|
|
113
|
+
content: string;
|
|
114
|
+
}
|
|
115
|
+
interface ChatCompletionCreateParams {
|
|
116
|
+
model: string;
|
|
117
|
+
messages: ChatMessage[];
|
|
118
|
+
provider?: string;
|
|
119
|
+
maxTokens?: number;
|
|
120
|
+
temperature?: number;
|
|
121
|
+
metadata?: Record<string, string>;
|
|
122
|
+
stop?: string[];
|
|
123
|
+
stopSequences?: string[];
|
|
124
|
+
/**
|
|
125
|
+
* When using publishable keys, an end-user id is required to mint a frontend token.
|
|
126
|
+
*/
|
|
127
|
+
endUserId?: string;
|
|
128
|
+
/**
|
|
129
|
+
* Opt out of SSE streaming and request a blocking JSON response.
|
|
130
|
+
*/
|
|
131
|
+
stream?: boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Optional request id to set on the call. If omitted, the server will generate one.
|
|
134
|
+
*/
|
|
135
|
+
requestId?: string;
|
|
136
|
+
}
|
|
137
|
+
interface ChatCompletionResponse {
|
|
138
|
+
id: string;
|
|
139
|
+
provider: string;
|
|
140
|
+
content: string[];
|
|
141
|
+
stopReason?: string;
|
|
142
|
+
model: string;
|
|
143
|
+
usage: Usage;
|
|
144
|
+
requestId?: string;
|
|
145
|
+
}
|
|
146
|
+
interface FieldError {
|
|
147
|
+
field?: string;
|
|
148
|
+
message: string;
|
|
149
|
+
}
|
|
150
|
+
interface RetryConfig {
|
|
151
|
+
maxAttempts?: number;
|
|
152
|
+
baseBackoffMs?: number;
|
|
153
|
+
maxBackoffMs?: number;
|
|
154
|
+
retryPost?: boolean;
|
|
155
|
+
}
|
|
156
|
+
type ChatEventType = "message_start" | "message_delta" | "message_stop" | "ping" | "custom";
|
|
157
|
+
interface MessageStartData {
|
|
158
|
+
responseId?: string;
|
|
159
|
+
model?: string;
|
|
160
|
+
message?: Record<string, unknown>;
|
|
161
|
+
[key: string]: unknown;
|
|
162
|
+
}
|
|
163
|
+
interface MessageDeltaData {
|
|
164
|
+
delta?: string | {
|
|
165
|
+
text?: string;
|
|
166
|
+
[key: string]: unknown;
|
|
167
|
+
};
|
|
168
|
+
responseId?: string;
|
|
169
|
+
model?: string;
|
|
170
|
+
[key: string]: unknown;
|
|
171
|
+
}
|
|
172
|
+
interface MessageStopData {
|
|
173
|
+
stopReason?: string;
|
|
174
|
+
usage?: Usage;
|
|
175
|
+
responseId?: string;
|
|
176
|
+
model?: string;
|
|
177
|
+
[key: string]: unknown;
|
|
178
|
+
}
|
|
179
|
+
interface ChatCompletionEvent<T = unknown> {
|
|
180
|
+
type: ChatEventType;
|
|
181
|
+
event: string;
|
|
182
|
+
data?: T;
|
|
183
|
+
textDelta?: string;
|
|
184
|
+
responseId?: string;
|
|
185
|
+
model?: string;
|
|
186
|
+
stopReason?: string;
|
|
187
|
+
usage?: Usage;
|
|
188
|
+
requestId?: string;
|
|
189
|
+
raw: string;
|
|
190
|
+
}
|
|
191
|
+
interface APIFrontendToken {
|
|
192
|
+
token: string;
|
|
193
|
+
expires_at?: string;
|
|
194
|
+
expiresAt?: string;
|
|
195
|
+
expires_in?: number;
|
|
196
|
+
expiresIn?: number;
|
|
197
|
+
token_type?: string;
|
|
198
|
+
tokenType?: string;
|
|
199
|
+
key_id?: string;
|
|
200
|
+
keyId?: string;
|
|
201
|
+
session_id?: string;
|
|
202
|
+
sessionId?: string;
|
|
203
|
+
token_scope?: string[];
|
|
204
|
+
tokenScope?: string[];
|
|
205
|
+
token_source?: string;
|
|
206
|
+
tokenSource?: string;
|
|
207
|
+
}
|
|
208
|
+
interface APIEndUserRef {
|
|
209
|
+
id: string;
|
|
210
|
+
external_id: string;
|
|
211
|
+
owner_id: string;
|
|
212
|
+
}
|
|
213
|
+
interface APICheckoutSession {
|
|
214
|
+
id: string;
|
|
215
|
+
plan: string;
|
|
216
|
+
status: string;
|
|
217
|
+
url: string;
|
|
218
|
+
expires_at?: string;
|
|
219
|
+
completed_at?: string;
|
|
220
|
+
}
|
|
221
|
+
interface APICheckoutResponse {
|
|
222
|
+
end_user?: APIEndUserRef;
|
|
223
|
+
session?: APICheckoutSession;
|
|
224
|
+
}
|
|
225
|
+
interface APIChatUsage {
|
|
226
|
+
input_tokens?: number;
|
|
227
|
+
output_tokens?: number;
|
|
228
|
+
total_tokens?: number;
|
|
229
|
+
inputTokens?: number;
|
|
230
|
+
outputTokens?: number;
|
|
231
|
+
totalTokens?: number;
|
|
232
|
+
}
|
|
233
|
+
interface APIChatResponse {
|
|
234
|
+
id?: string;
|
|
235
|
+
provider?: string;
|
|
236
|
+
content?: string | string[];
|
|
237
|
+
stop_reason?: string;
|
|
238
|
+
stopReason?: string;
|
|
239
|
+
model?: string;
|
|
240
|
+
usage?: APIChatUsage;
|
|
241
|
+
response_id?: string;
|
|
242
|
+
responseId?: string;
|
|
243
|
+
message?: {
|
|
244
|
+
id?: string;
|
|
245
|
+
model?: string;
|
|
246
|
+
};
|
|
247
|
+
delta?: string | {
|
|
248
|
+
text?: string;
|
|
249
|
+
content?: string;
|
|
250
|
+
};
|
|
251
|
+
type?: string;
|
|
252
|
+
event?: string;
|
|
253
|
+
}
|
|
254
|
+
interface APIKey {
|
|
255
|
+
id: string;
|
|
256
|
+
label: string;
|
|
257
|
+
kind: string;
|
|
258
|
+
createdAt: Date;
|
|
259
|
+
expiresAt?: Date;
|
|
260
|
+
lastUsedAt?: Date;
|
|
261
|
+
redactedKey: string;
|
|
262
|
+
secretKey?: string;
|
|
263
|
+
}
|
|
264
|
+
interface APIKeyCreateRequest {
|
|
265
|
+
label: string;
|
|
266
|
+
expiresAt?: Date;
|
|
267
|
+
kind?: string;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
interface RequestOptions {
|
|
271
|
+
method?: string;
|
|
272
|
+
headers?: HeadersInit;
|
|
273
|
+
body?: unknown;
|
|
274
|
+
signal?: AbortSignal;
|
|
275
|
+
apiKey?: string;
|
|
276
|
+
accessToken?: string;
|
|
277
|
+
accept?: string;
|
|
278
|
+
/**
|
|
279
|
+
* When true, the caller is responsible for handling non-2xx responses.
|
|
280
|
+
*/
|
|
281
|
+
raw?: boolean;
|
|
282
|
+
timeoutMs?: number;
|
|
283
|
+
/**
|
|
284
|
+
* Override retry behavior for this request. Set to `false` to disable retries.
|
|
285
|
+
*/
|
|
286
|
+
retry?: RetryConfig | false;
|
|
287
|
+
/**
|
|
288
|
+
* When false, skip the default client timeout (useful for streaming).
|
|
289
|
+
*/
|
|
290
|
+
useDefaultTimeout?: boolean;
|
|
291
|
+
}
|
|
292
|
+
declare class HTTPClient {
|
|
293
|
+
private readonly baseUrl;
|
|
294
|
+
private readonly apiKey?;
|
|
295
|
+
private readonly accessToken?;
|
|
296
|
+
private readonly fetchImpl?;
|
|
297
|
+
private readonly clientHeader?;
|
|
298
|
+
private readonly defaultTimeoutMs;
|
|
299
|
+
private readonly retry?;
|
|
300
|
+
private readonly defaultHeaders;
|
|
301
|
+
constructor(cfg: {
|
|
302
|
+
baseUrl?: string;
|
|
303
|
+
apiKey?: string;
|
|
304
|
+
accessToken?: string;
|
|
305
|
+
fetchImpl?: typeof fetch;
|
|
306
|
+
clientHeader?: string;
|
|
307
|
+
timeoutMs?: number;
|
|
308
|
+
retry?: RetryConfig | false;
|
|
309
|
+
defaultHeaders?: Record<string, string>;
|
|
310
|
+
environment?: Environment;
|
|
311
|
+
});
|
|
312
|
+
request(path: string, options?: RequestOptions): Promise<Response>;
|
|
313
|
+
json<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
interface AuthConfig {
|
|
317
|
+
apiKey?: string;
|
|
318
|
+
accessToken?: string;
|
|
319
|
+
endUser?: FrontendIdentity;
|
|
320
|
+
}
|
|
321
|
+
interface AuthHeaders {
|
|
322
|
+
apiKey?: string;
|
|
323
|
+
accessToken?: string;
|
|
324
|
+
}
|
|
325
|
+
declare class AuthClient {
|
|
326
|
+
private readonly http;
|
|
327
|
+
private readonly apiKey?;
|
|
328
|
+
private readonly accessToken?;
|
|
329
|
+
private readonly endUser?;
|
|
330
|
+
private cachedFrontend;
|
|
331
|
+
constructor(http: HTTPClient, cfg: AuthConfig);
|
|
332
|
+
/**
|
|
333
|
+
* Exchange a publishable key for a short-lived frontend token.
|
|
334
|
+
* Tokens are cached until they are close to expiry.
|
|
335
|
+
*/
|
|
336
|
+
frontendToken(request?: Partial<FrontendTokenRequest>): Promise<FrontendToken>;
|
|
337
|
+
/**
|
|
338
|
+
* Determine the correct auth headers for chat completions.
|
|
339
|
+
* Publishable keys are automatically exchanged for frontend tokens.
|
|
340
|
+
*/
|
|
341
|
+
authForChat(endUserId?: string, overrides?: Partial<FrontendIdentity>): Promise<AuthHeaders>;
|
|
342
|
+
/**
|
|
343
|
+
* Billing calls accept either bearer tokens or API keys (including publishable keys).
|
|
344
|
+
*/
|
|
345
|
+
authForBilling(): AuthHeaders;
|
|
346
|
+
}
|
|
347
|
+
declare function isPublishableKey(value?: string | null): boolean;
|
|
348
|
+
|
|
349
|
+
declare class ApiKeysClient {
|
|
350
|
+
private readonly http;
|
|
351
|
+
constructor(http: HTTPClient);
|
|
352
|
+
list(): Promise<APIKey[]>;
|
|
353
|
+
create(req: APIKeyCreateRequest): Promise<APIKey>;
|
|
354
|
+
delete(id: string): Promise<void>;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
declare class BillingClient {
|
|
358
|
+
private readonly http;
|
|
359
|
+
private readonly auth;
|
|
360
|
+
constructor(http: HTTPClient, auth: AuthClient);
|
|
361
|
+
/**
|
|
362
|
+
* Initiate a Stripe Checkout session for an end user.
|
|
363
|
+
*/
|
|
364
|
+
checkout(params: CheckoutRequest): Promise<CheckoutResponse>;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
interface ChatRequestOptions {
|
|
368
|
+
/**
|
|
369
|
+
* Abort the HTTP request and stream consumption.
|
|
370
|
+
*/
|
|
371
|
+
signal?: AbortSignal;
|
|
372
|
+
/**
|
|
373
|
+
* Override the Accept header to switch between streaming and blocking responses.
|
|
374
|
+
*/
|
|
375
|
+
stream?: boolean;
|
|
376
|
+
/**
|
|
377
|
+
* Optional request id header. `params.requestId` takes precedence if provided.
|
|
378
|
+
*/
|
|
379
|
+
requestId?: string;
|
|
380
|
+
/**
|
|
381
|
+
* Additional HTTP headers for this request.
|
|
382
|
+
*/
|
|
383
|
+
headers?: Record<string, string>;
|
|
384
|
+
/**
|
|
385
|
+
* Additional metadata merged into the request body.
|
|
386
|
+
*/
|
|
387
|
+
metadata?: Record<string, string>;
|
|
388
|
+
/**
|
|
389
|
+
* Override the per-request timeout in milliseconds (set to 0 to disable).
|
|
390
|
+
*/
|
|
391
|
+
timeoutMs?: number;
|
|
392
|
+
/**
|
|
393
|
+
* Override retry behavior for this call. Set to `false` to disable retries.
|
|
394
|
+
*/
|
|
395
|
+
retry?: RetryConfig | false;
|
|
396
|
+
}
|
|
397
|
+
declare class ChatClient {
|
|
398
|
+
readonly completions: ChatCompletionsClient;
|
|
399
|
+
constructor(http: HTTPClient, auth: AuthClient, cfg?: {
|
|
400
|
+
defaultMetadata?: Record<string, string>;
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
declare class ChatCompletionsClient {
|
|
404
|
+
private readonly http;
|
|
405
|
+
private readonly auth;
|
|
406
|
+
private readonly defaultMetadata?;
|
|
407
|
+
constructor(http: HTTPClient, auth: AuthClient, defaultMetadata?: Record<string, string>);
|
|
408
|
+
create(params: ChatCompletionCreateParams & {
|
|
409
|
+
stream: false;
|
|
410
|
+
}, options?: ChatRequestOptions): Promise<ChatCompletionResponse>;
|
|
411
|
+
create(params: ChatCompletionCreateParams, options: ChatRequestOptions & {
|
|
412
|
+
stream: false;
|
|
413
|
+
}): Promise<ChatCompletionResponse>;
|
|
414
|
+
create(params: ChatCompletionCreateParams, options?: ChatRequestOptions): Promise<ChatCompletionsStream>;
|
|
415
|
+
}
|
|
416
|
+
declare class ChatCompletionsStream implements AsyncIterable<ChatCompletionEvent> {
|
|
417
|
+
private readonly response;
|
|
418
|
+
private readonly requestId?;
|
|
419
|
+
private closed;
|
|
420
|
+
constructor(response: Response, requestId?: string);
|
|
421
|
+
cancel(reason?: unknown): Promise<void>;
|
|
422
|
+
[Symbol.asyncIterator](): AsyncIterator<ChatCompletionEvent>;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
declare class ModelRelayError extends Error {
|
|
426
|
+
status: number;
|
|
427
|
+
code?: string;
|
|
428
|
+
requestId?: string;
|
|
429
|
+
fields?: FieldError[];
|
|
430
|
+
data?: unknown;
|
|
431
|
+
constructor(message: string, opts: {
|
|
432
|
+
status: number;
|
|
433
|
+
code?: string;
|
|
434
|
+
requestId?: string;
|
|
435
|
+
fields?: FieldError[];
|
|
436
|
+
data?: unknown;
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
declare class ModelRelay {
|
|
441
|
+
readonly billing: BillingClient;
|
|
442
|
+
readonly chat: ChatClient;
|
|
443
|
+
readonly auth: AuthClient;
|
|
444
|
+
readonly apiKeys: ApiKeysClient;
|
|
445
|
+
readonly baseUrl: string;
|
|
446
|
+
constructor(options: ModelRelayOptions);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
export { type APIChatResponse, type APIChatUsage, type APICheckoutResponse, type APICheckoutSession, type APIEndUserRef, type APIFrontendToken, type APIKey, type APIKeyCreateRequest, ApiKeysClient, AuthClient, BillingClient, ChatClient, type ChatCompletionCreateParams, type ChatCompletionEvent, type ChatCompletionResponse, ChatCompletionsStream, type ChatEventType, type ChatMessage, type CheckoutRequest, type CheckoutResponse, type CheckoutSession, DEFAULT_BASE_URL, DEFAULT_CLIENT_HEADER, DEFAULT_REQUEST_TIMEOUT_MS, type EndUserRef, type Environment, type FieldError, type FrontendIdentity, type FrontendToken, type FrontendTokenRequest, type MessageDeltaData, type MessageStartData, type MessageStopData, ModelRelay, ModelRelayError, type ModelRelayOptions, type RetryConfig, SANDBOX_BASE_URL, SDK_VERSION, STAGING_BASE_URL, type Usage, isPublishableKey };
|