@modelrelay/sdk 4.0.0 → 5.3.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/README.md +19 -64
- package/dist/api-7TVb2cnl.d.cts +5319 -0
- package/dist/api-7TVb2cnl.d.ts +5319 -0
- package/dist/{api-CzFaWgs_.d.cts → api-B9x3HA3Z.d.cts} +10 -13
- package/dist/{api-CzFaWgs_.d.ts → api-B9x3HA3Z.d.ts} +10 -13
- package/dist/api-BRAJe_xm.d.cts +5315 -0
- package/dist/api-BRAJe_xm.d.ts +5315 -0
- package/dist/{api-jqOwvH6O.d.cts → api-HVh8Lusf.d.cts} +2650 -2111
- package/dist/{api-jqOwvH6O.d.ts → api-HVh8Lusf.d.ts} +2650 -2111
- package/dist/billing/index.d.cts +1 -1
- package/dist/billing/index.d.ts +1 -1
- package/dist/{chunk-EMQKOEF3.js → chunk-BL7GWXRZ.js} +1 -1
- package/dist/{chunk-ADQ74R7T.js → chunk-G5H7EY4F.js} +16 -14
- package/dist/{chunk-LW6SC7NQ.js → chunk-HLJAMT7F.js} +1 -1
- package/dist/{chunk-ZBQKY2W2.js → chunk-LZDGY24E.js} +2 -63
- package/dist/chunk-OJFVI3QJ.js +1133 -0
- package/dist/chunk-Z6R4G2TU.js +1133 -0
- package/dist/index.cjs +192 -504
- package/dist/index.d.cts +179 -623
- package/dist/index.d.ts +179 -623
- package/dist/index.js +185 -496
- package/dist/node.cjs +155 -2
- package/dist/node.d.cts +9 -0
- package/dist/node.d.ts +9 -0
- package/dist/node.js +155 -2
- package/dist/{tools-DAqAFZ8F.d.ts → tools-Db-F5rIL.d.cts} +6 -9
- package/dist/{tools-DAqAFZ8F.d.cts → tools-Db-F5rIL.d.ts} +6 -9
- package/package.json +1 -1
- package/dist/tools-Cnl2MgVa.d.cts +0 -1174
- package/dist/tools-Cnl2MgVa.d.ts +0 -1174
|
@@ -1,1174 +0,0 @@
|
|
|
1
|
-
declare const SDK_VERSION: string;
|
|
2
|
-
declare const DEFAULT_BASE_URL = "https://api.modelrelay.ai/api/v1";
|
|
3
|
-
declare const DEFAULT_CLIENT_HEADER: string;
|
|
4
|
-
declare const DEFAULT_CONNECT_TIMEOUT_MS = 5000;
|
|
5
|
-
declare const DEFAULT_REQUEST_TIMEOUT_MS = 60000;
|
|
6
|
-
type NonEmptyArray<T> = [T, ...T[]];
|
|
7
|
-
declare const StopReasons: {
|
|
8
|
-
readonly Completed: "completed";
|
|
9
|
-
readonly Stop: "stop";
|
|
10
|
-
readonly StopSequence: "stop_sequence";
|
|
11
|
-
readonly EndTurn: "end_turn";
|
|
12
|
-
readonly MaxTokens: "max_tokens";
|
|
13
|
-
readonly MaxLength: "max_len";
|
|
14
|
-
readonly MaxContext: "max_context";
|
|
15
|
-
readonly ToolCalls: "tool_calls";
|
|
16
|
-
readonly TimeLimit: "time_limit";
|
|
17
|
-
readonly ContentFilter: "content_filter";
|
|
18
|
-
readonly Incomplete: "incomplete";
|
|
19
|
-
readonly Unknown: "unknown";
|
|
20
|
-
};
|
|
21
|
-
type KnownStopReason = (typeof StopReasons)[keyof typeof StopReasons];
|
|
22
|
-
type StopReason = KnownStopReason | {
|
|
23
|
-
other: string;
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Branded type for provider identifiers (e.g., "anthropic", "openai").
|
|
27
|
-
* The brand prevents accidental use of arbitrary strings where a provider ID is expected.
|
|
28
|
-
*/
|
|
29
|
-
type ProviderId = string & {
|
|
30
|
-
readonly __brand: "ProviderId";
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Branded type for model identifiers (e.g., "claude-3-5-sonnet-20241022").
|
|
34
|
-
* The brand prevents accidental use of arbitrary strings where a model ID is expected.
|
|
35
|
-
*/
|
|
36
|
-
type ModelId = string & {
|
|
37
|
-
readonly __brand: "ModelId";
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* Branded type for tier codes (e.g., "free", "pro", "enterprise").
|
|
41
|
-
* The brand prevents accidental use of arbitrary strings where a tier code is expected.
|
|
42
|
-
*/
|
|
43
|
-
type TierCode = string & {
|
|
44
|
-
readonly __brand: "TierCode";
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Cast a string to a ProviderId. Use for known provider identifiers.
|
|
48
|
-
*/
|
|
49
|
-
declare function asProviderId(value: string): ProviderId;
|
|
50
|
-
/**
|
|
51
|
-
* Cast a string to a ModelId. Use for known model identifiers.
|
|
52
|
-
*/
|
|
53
|
-
declare function asModelId(value: string): ModelId;
|
|
54
|
-
/**
|
|
55
|
-
* Cast a string to a TierCode. Use for known tier codes.
|
|
56
|
-
*/
|
|
57
|
-
declare function asTierCode(value: string): TierCode;
|
|
58
|
-
declare const SubscriptionStatuses: {
|
|
59
|
-
readonly Active: "active";
|
|
60
|
-
readonly Trialing: "trialing";
|
|
61
|
-
readonly PastDue: "past_due";
|
|
62
|
-
readonly Canceled: "canceled";
|
|
63
|
-
readonly Unpaid: "unpaid";
|
|
64
|
-
readonly Incomplete: "incomplete";
|
|
65
|
-
readonly IncompleteExpired: "incomplete_expired";
|
|
66
|
-
readonly Paused: "paused";
|
|
67
|
-
};
|
|
68
|
-
type SubscriptionStatusKind = (typeof SubscriptionStatuses)[keyof typeof SubscriptionStatuses];
|
|
69
|
-
declare const BillingProviders: {
|
|
70
|
-
readonly Stripe: "stripe";
|
|
71
|
-
readonly Crypto: "crypto";
|
|
72
|
-
readonly AppStore: "app_store";
|
|
73
|
-
readonly External: "external";
|
|
74
|
-
};
|
|
75
|
-
type BillingProvider = (typeof BillingProviders)[keyof typeof BillingProviders];
|
|
76
|
-
/** Arbitrary customer metadata. Values can be any JSON type. */
|
|
77
|
-
type CustomerMetadata = Record<string, unknown>;
|
|
78
|
-
type PublishableKey = string & {
|
|
79
|
-
readonly __brand: "PublishableKey";
|
|
80
|
-
};
|
|
81
|
-
type SecretKey = string & {
|
|
82
|
-
readonly __brand: "SecretKey";
|
|
83
|
-
};
|
|
84
|
-
type ApiKey = PublishableKey | SecretKey;
|
|
85
|
-
/**
|
|
86
|
-
* TokenProvider supplies short-lived bearer tokens for ModelRelay data-plane calls.
|
|
87
|
-
*
|
|
88
|
-
* Providers are responsible for caching and refreshing tokens when needed.
|
|
89
|
-
*/
|
|
90
|
-
interface TokenProvider {
|
|
91
|
-
getToken(): Promise<string>;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Common configuration options for the ModelRelay client.
|
|
95
|
-
*/
|
|
96
|
-
interface ModelRelayBaseOptions {
|
|
97
|
-
/**
|
|
98
|
-
* Optional base URL override. Defaults to production API.
|
|
99
|
-
*/
|
|
100
|
-
baseUrl?: string;
|
|
101
|
-
fetch?: typeof fetch;
|
|
102
|
-
/**
|
|
103
|
-
* Default customer metadata used when exchanging publishable keys for frontend tokens.
|
|
104
|
-
*/
|
|
105
|
-
customer?: FrontendCustomer;
|
|
106
|
-
/**
|
|
107
|
-
* Optional client header override for telemetry.
|
|
108
|
-
*/
|
|
109
|
-
clientHeader?: string;
|
|
110
|
-
/**
|
|
111
|
-
* Default connect timeout in milliseconds (applies to each attempt).
|
|
112
|
-
*/
|
|
113
|
-
connectTimeoutMs?: number;
|
|
114
|
-
/**
|
|
115
|
-
* Default request timeout in milliseconds (non-streaming). Set to 0 to disable.
|
|
116
|
-
*/
|
|
117
|
-
timeoutMs?: number;
|
|
118
|
-
/**
|
|
119
|
-
* Retry configuration applied to all requests (can be overridden per call). Set to `false` to disable retries.
|
|
120
|
-
*/
|
|
121
|
-
retry?: RetryConfig | false;
|
|
122
|
-
/**
|
|
123
|
-
* Default HTTP headers applied to every request.
|
|
124
|
-
*/
|
|
125
|
-
defaultHeaders?: Record<string, string>;
|
|
126
|
-
/**
|
|
127
|
-
* Optional metrics callbacks for latency/usage.
|
|
128
|
-
*/
|
|
129
|
-
metrics?: MetricsCallbacks;
|
|
130
|
-
/**
|
|
131
|
-
* Optional trace/log hooks for request + stream lifecycle.
|
|
132
|
-
*/
|
|
133
|
-
trace?: TraceCallbacks;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Configuration options requiring an API key.
|
|
137
|
-
*/
|
|
138
|
-
interface ModelRelayKeyOptions extends ModelRelayBaseOptions {
|
|
139
|
-
/**
|
|
140
|
-
* API key (secret or publishable). Required.
|
|
141
|
-
* - Secret keys (`mr_sk_...`) are for server-side API calls.
|
|
142
|
-
* - Publishable keys (`mr_pk_...`) are for frontend token exchange.
|
|
143
|
-
*/
|
|
144
|
-
key: ApiKey;
|
|
145
|
-
/**
|
|
146
|
-
* Optional bearer token (takes precedence over key for requests when provided).
|
|
147
|
-
*/
|
|
148
|
-
token?: string;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Configuration options requiring an access token.
|
|
152
|
-
*/
|
|
153
|
-
interface ModelRelayTokenOptions extends ModelRelayBaseOptions {
|
|
154
|
-
/**
|
|
155
|
-
* Optional API key.
|
|
156
|
-
*/
|
|
157
|
-
key?: ApiKey;
|
|
158
|
-
/**
|
|
159
|
-
* Bearer token to call the API directly (server or frontend token). Required.
|
|
160
|
-
*/
|
|
161
|
-
token: string;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Configuration options requiring a TokenProvider.
|
|
165
|
-
*/
|
|
166
|
-
interface ModelRelayTokenProviderOptions extends ModelRelayBaseOptions {
|
|
167
|
-
/**
|
|
168
|
-
* Token provider used to fetch bearer tokens for `/responses`, `/runs`, and `/workflows/compile`.
|
|
169
|
-
*/
|
|
170
|
-
tokenProvider: TokenProvider;
|
|
171
|
-
/**
|
|
172
|
-
* Optional API key. Useful for non-data-plane endpoints (e.g., /customers, /tiers).
|
|
173
|
-
*/
|
|
174
|
-
key?: ApiKey;
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* ModelRelay client configuration.
|
|
178
|
-
*
|
|
179
|
-
* You must provide at least one of `key` or `token` for authentication.
|
|
180
|
-
* This is enforced at compile time through discriminated union types.
|
|
181
|
-
*
|
|
182
|
-
* @example With API key (server-side)
|
|
183
|
-
* ```typescript
|
|
184
|
-
* import { ModelRelay } from "@modelrelay/sdk";
|
|
185
|
-
* const client = ModelRelay.fromSecretKey("mr_sk_...");
|
|
186
|
-
* ```
|
|
187
|
-
*
|
|
188
|
-
* @example With access token (frontend or after token exchange)
|
|
189
|
-
* ```typescript
|
|
190
|
-
* const client = new ModelRelay({ token: frontendToken });
|
|
191
|
-
* ```
|
|
192
|
-
*
|
|
193
|
-
* @example With publishable key (frontend token exchange)
|
|
194
|
-
* ```typescript
|
|
195
|
-
* import { ModelRelay, parsePublishableKey } from "@modelrelay/sdk";
|
|
196
|
-
* const client = new ModelRelay({ key: parsePublishableKey("mr_pk_..."), customer: { provider: "oidc", subject: "user123" } });
|
|
197
|
-
* ```
|
|
198
|
-
*
|
|
199
|
-
* @example With token provider (OIDC exchange, backend-minted tokens, etc.)
|
|
200
|
-
* ```typescript
|
|
201
|
-
* import { ModelRelay } from "@modelrelay/sdk";
|
|
202
|
-
* const client = new ModelRelay({ tokenProvider });
|
|
203
|
-
* ```
|
|
204
|
-
*/
|
|
205
|
-
type ModelRelayOptions = ModelRelayKeyOptions | ModelRelayTokenOptions | ModelRelayTokenProviderOptions;
|
|
206
|
-
/**
|
|
207
|
-
* @deprecated Use ModelRelayOptions instead. This type allows empty configuration
|
|
208
|
-
* which will fail at runtime.
|
|
209
|
-
*/
|
|
210
|
-
interface FrontendCustomer {
|
|
211
|
-
provider: string;
|
|
212
|
-
subject: string;
|
|
213
|
-
/**
|
|
214
|
-
* Email address for auto-provisioning (if enabled for the project).
|
|
215
|
-
* If omitted and the identity isn't linked yet, the API may return EMAIL_REQUIRED.
|
|
216
|
-
*/
|
|
217
|
-
email?: string;
|
|
218
|
-
deviceId?: string;
|
|
219
|
-
ttlSeconds?: number;
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Request for fetching a frontend token for an existing identity.
|
|
223
|
-
*/
|
|
224
|
-
interface FrontendTokenRequest {
|
|
225
|
-
/** Publishable key (mr_pk_*) - required for authentication. */
|
|
226
|
-
publishableKey: PublishableKey;
|
|
227
|
-
/** Identity provider namespace (e.g. "oidc", "github", "oidc:https://issuer"). */
|
|
228
|
-
identityProvider: string;
|
|
229
|
-
/** Provider-scoped subject identifier (e.g. OIDC sub). */
|
|
230
|
-
identitySubject: string;
|
|
231
|
-
/** Optional device identifier for tracking/rate limiting. */
|
|
232
|
-
deviceId?: string;
|
|
233
|
-
/** Optional TTL in seconds for the issued token. */
|
|
234
|
-
ttlSeconds?: number;
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* Request for fetching a frontend token with auto-provisioning enabled.
|
|
238
|
-
* The project must explicitly enable auto-provisioning; email is required for customer creation.
|
|
239
|
-
*/
|
|
240
|
-
interface FrontendTokenAutoProvisionRequest {
|
|
241
|
-
/** Publishable key (mr_pk_*) - required for authentication. */
|
|
242
|
-
publishableKey: PublishableKey;
|
|
243
|
-
/** Identity provider namespace (e.g. "oidc", "github", "oidc:https://issuer"). */
|
|
244
|
-
identityProvider: string;
|
|
245
|
-
/** Provider-scoped subject identifier (e.g. OIDC sub). */
|
|
246
|
-
identitySubject: string;
|
|
247
|
-
/** Email address - required for auto-provisioning a new customer. */
|
|
248
|
-
email: string;
|
|
249
|
-
/** Optional device identifier for tracking/rate limiting. */
|
|
250
|
-
deviceId?: string;
|
|
251
|
-
/** Optional TTL in seconds for the issued token. */
|
|
252
|
-
ttlSeconds?: number;
|
|
253
|
-
}
|
|
254
|
-
/** Token type for OAuth2 bearer tokens. */
|
|
255
|
-
type TokenType = "Bearer";
|
|
256
|
-
interface FrontendToken {
|
|
257
|
-
/** The bearer token for authenticating LLM requests. */
|
|
258
|
-
token: string;
|
|
259
|
-
/** When the token expires. */
|
|
260
|
-
expiresAt: Date;
|
|
261
|
-
/** Seconds until the token expires. */
|
|
262
|
-
expiresIn: number;
|
|
263
|
-
/** Token type, always "Bearer". */
|
|
264
|
-
tokenType: TokenType;
|
|
265
|
-
/** The publishable key ID that issued this token. */
|
|
266
|
-
keyId: string;
|
|
267
|
-
/** Unique session identifier for this token. */
|
|
268
|
-
sessionId: string;
|
|
269
|
-
/** The project ID this token is scoped to. */
|
|
270
|
-
projectId: string;
|
|
271
|
-
/** The internal customer ID (UUID). Only present for managed billing projects; BYOB projects have end-users but not customers. */
|
|
272
|
-
customerId?: string;
|
|
273
|
-
/** The external customer ID provided by the application. */
|
|
274
|
-
customerExternalId: string;
|
|
275
|
-
/** The tier code for the customer (e.g., "free", "pro", "enterprise").
|
|
276
|
-
* Optional for BYOB (external billing) projects where customers may not have subscriptions.
|
|
277
|
-
*/
|
|
278
|
-
tierCode?: TierCode;
|
|
279
|
-
/**
|
|
280
|
-
* Publishable key used for issuance. Added client-side for caching.
|
|
281
|
-
*/
|
|
282
|
-
publishableKey?: PublishableKey;
|
|
283
|
-
/**
|
|
284
|
-
* Device identifier used when issuing the token. Added client-side for caching.
|
|
285
|
-
*/
|
|
286
|
-
deviceId?: string;
|
|
287
|
-
/**
|
|
288
|
-
* Identity provider used for issuance. Added client-side for caching.
|
|
289
|
-
*/
|
|
290
|
-
identityProvider?: string;
|
|
291
|
-
/**
|
|
292
|
-
* Identity subject used for issuance. Added client-side for caching.
|
|
293
|
-
*/
|
|
294
|
-
identitySubject?: string;
|
|
295
|
-
}
|
|
296
|
-
interface CustomerTokenRequest {
|
|
297
|
-
customerId?: string;
|
|
298
|
-
customerExternalId?: string;
|
|
299
|
-
ttlSeconds?: number;
|
|
300
|
-
}
|
|
301
|
-
interface CustomerToken {
|
|
302
|
-
token: string;
|
|
303
|
-
expiresAt: Date;
|
|
304
|
-
expiresIn: number;
|
|
305
|
-
tokenType: TokenType;
|
|
306
|
-
projectId: string;
|
|
307
|
-
/** Only present for managed billing projects; BYOB projects have end-users but not customers. */
|
|
308
|
-
customerId?: string;
|
|
309
|
-
customerExternalId: string;
|
|
310
|
-
/** Optional for BYOB (external billing) projects */
|
|
311
|
-
tierCode?: TierCode;
|
|
312
|
-
}
|
|
313
|
-
interface OIDCExchangeRequest {
|
|
314
|
-
idToken: string;
|
|
315
|
-
projectId?: string;
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Request options for starting a device authorization flow.
|
|
319
|
-
*/
|
|
320
|
-
interface DeviceStartRequest {
|
|
321
|
-
/**
|
|
322
|
-
* Optional provider for native device flow.
|
|
323
|
-
* - "github": Use GitHub's native device flow (user authenticates at github.com/login/device)
|
|
324
|
-
* - undefined: Use ModelRelay's wrapped OAuth flow (default)
|
|
325
|
-
*/
|
|
326
|
-
provider?: "github";
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Response from starting a device authorization flow.
|
|
330
|
-
*/
|
|
331
|
-
interface DeviceStartResponse {
|
|
332
|
-
/** Device code used for polling /auth/device/token */
|
|
333
|
-
deviceCode: string;
|
|
334
|
-
/** Human-enterable code shown to the user */
|
|
335
|
-
userCode: string;
|
|
336
|
-
/** URL where the user enters the code and completes authorization */
|
|
337
|
-
verificationUri: string;
|
|
338
|
-
/** Convenience URL that pre-fills the user_code (optional) */
|
|
339
|
-
verificationUriComplete?: string;
|
|
340
|
-
/** When the device code expires */
|
|
341
|
-
expiresAt: Date;
|
|
342
|
-
/** Minimum polling interval in seconds */
|
|
343
|
-
interval: number;
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* Successful response from polling device token endpoint.
|
|
347
|
-
*/
|
|
348
|
-
interface DeviceTokenResponse {
|
|
349
|
-
/** Bearer token for authenticating requests */
|
|
350
|
-
token: string;
|
|
351
|
-
/** When the token expires */
|
|
352
|
-
expiresAt: Date;
|
|
353
|
-
/** Seconds until the token expires */
|
|
354
|
-
expiresIn: number;
|
|
355
|
-
/** Token type, always "Bearer" */
|
|
356
|
-
tokenType: TokenType;
|
|
357
|
-
/** The project ID this token is scoped to */
|
|
358
|
-
projectId: string;
|
|
359
|
-
/** The internal customer ID (UUID). Only present for managed billing projects; BYOB projects have end-users but not customers. */
|
|
360
|
-
customerId?: string;
|
|
361
|
-
/** The external customer ID */
|
|
362
|
-
customerExternalId: string;
|
|
363
|
-
/** The tier code for the customer (optional for BYOB projects) */
|
|
364
|
-
tierCode?: TierCode;
|
|
365
|
-
}
|
|
366
|
-
/**
|
|
367
|
-
* Pending response from polling device token endpoint.
|
|
368
|
-
* Indicates the user hasn't completed authorization yet.
|
|
369
|
-
*/
|
|
370
|
-
interface DeviceTokenPending {
|
|
371
|
-
/** Error code from the device flow */
|
|
372
|
-
error: "authorization_pending" | "slow_down";
|
|
373
|
-
/** Human-readable error description */
|
|
374
|
-
errorDescription?: string;
|
|
375
|
-
/** Updated polling interval (when error is slow_down) */
|
|
376
|
-
interval?: number;
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Result from polling the device token endpoint.
|
|
380
|
-
* Either a successful token or a pending/error state.
|
|
381
|
-
*/
|
|
382
|
-
type DeviceTokenResult = {
|
|
383
|
-
status: "approved";
|
|
384
|
-
token: DeviceTokenResponse;
|
|
385
|
-
} | {
|
|
386
|
-
status: "pending";
|
|
387
|
-
pending: DeviceTokenPending;
|
|
388
|
-
} | {
|
|
389
|
-
status: "error";
|
|
390
|
-
error: string;
|
|
391
|
-
errorDescription?: string;
|
|
392
|
-
};
|
|
393
|
-
interface Usage {
|
|
394
|
-
inputTokens: number;
|
|
395
|
-
outputTokens: number;
|
|
396
|
-
totalTokens: number;
|
|
397
|
-
}
|
|
398
|
-
/**
|
|
399
|
-
* Creates a Usage object with automatic totalTokens calculation if not provided.
|
|
400
|
-
*/
|
|
401
|
-
declare function createUsage(inputTokens: number, outputTokens: number, totalTokens?: number): Usage;
|
|
402
|
-
interface UsageSummary {
|
|
403
|
-
plan: string;
|
|
404
|
-
planType?: string;
|
|
405
|
-
windowStart?: Date | string;
|
|
406
|
-
windowEnd?: Date | string;
|
|
407
|
-
limit?: number;
|
|
408
|
-
used?: number;
|
|
409
|
-
images?: number;
|
|
410
|
-
actionsLimit?: number;
|
|
411
|
-
actionsUsed?: number;
|
|
412
|
-
remaining?: number;
|
|
413
|
-
state?: string;
|
|
414
|
-
}
|
|
415
|
-
interface Project {
|
|
416
|
-
id: string;
|
|
417
|
-
name: string;
|
|
418
|
-
description?: string;
|
|
419
|
-
createdAt?: Date;
|
|
420
|
-
updatedAt?: Date;
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Valid roles for chat messages.
|
|
424
|
-
*/
|
|
425
|
-
declare const MessageRoles: {
|
|
426
|
-
readonly User: "user";
|
|
427
|
-
readonly Assistant: "assistant";
|
|
428
|
-
readonly System: "system";
|
|
429
|
-
readonly Tool: "tool";
|
|
430
|
-
};
|
|
431
|
-
type MessageRole = (typeof MessageRoles)[keyof typeof MessageRoles];
|
|
432
|
-
declare const ContentPartTypes: {
|
|
433
|
-
readonly Text: "text";
|
|
434
|
-
};
|
|
435
|
-
type ContentPartType = (typeof ContentPartTypes)[keyof typeof ContentPartTypes];
|
|
436
|
-
type ContentPart = {
|
|
437
|
-
type: "text";
|
|
438
|
-
text: string;
|
|
439
|
-
};
|
|
440
|
-
declare const InputItemTypes: {
|
|
441
|
-
readonly Message: "message";
|
|
442
|
-
};
|
|
443
|
-
type InputItemType = (typeof InputItemTypes)[keyof typeof InputItemTypes];
|
|
444
|
-
type InputItem = {
|
|
445
|
-
type: "message";
|
|
446
|
-
role: MessageRole;
|
|
447
|
-
content: ContentPart[];
|
|
448
|
-
toolCalls?: ToolCall[];
|
|
449
|
-
toolCallId?: string;
|
|
450
|
-
};
|
|
451
|
-
declare const OutputItemTypes: {
|
|
452
|
-
readonly Message: "message";
|
|
453
|
-
};
|
|
454
|
-
type OutputItemType = (typeof OutputItemTypes)[keyof typeof OutputItemTypes];
|
|
455
|
-
type OutputItem = {
|
|
456
|
-
type: "message";
|
|
457
|
-
role: MessageRole;
|
|
458
|
-
content: ContentPart[];
|
|
459
|
-
toolCalls?: ToolCall[];
|
|
460
|
-
};
|
|
461
|
-
declare const ToolTypes: {
|
|
462
|
-
readonly Function: "function";
|
|
463
|
-
readonly Web: "web";
|
|
464
|
-
readonly XSearch: "x_search";
|
|
465
|
-
readonly CodeExecution: "code_execution";
|
|
466
|
-
};
|
|
467
|
-
type ToolType = (typeof ToolTypes)[keyof typeof ToolTypes];
|
|
468
|
-
declare const WebToolIntents: {
|
|
469
|
-
readonly Auto: "auto";
|
|
470
|
-
readonly SearchWeb: "search_web";
|
|
471
|
-
readonly FetchURL: "fetch_url";
|
|
472
|
-
};
|
|
473
|
-
type WebToolIntent = (typeof WebToolIntents)[keyof typeof WebToolIntents];
|
|
474
|
-
interface FunctionTool {
|
|
475
|
-
name: string;
|
|
476
|
-
description?: string;
|
|
477
|
-
parameters?: Record<string, unknown>;
|
|
478
|
-
}
|
|
479
|
-
interface WebSearchConfig {
|
|
480
|
-
allowedDomains?: string[];
|
|
481
|
-
excludedDomains?: string[];
|
|
482
|
-
maxUses?: number;
|
|
483
|
-
intent?: WebToolIntent;
|
|
484
|
-
}
|
|
485
|
-
interface XSearchConfig {
|
|
486
|
-
allowedHandles?: string[];
|
|
487
|
-
excludedHandles?: string[];
|
|
488
|
-
fromDate?: string;
|
|
489
|
-
toDate?: string;
|
|
490
|
-
}
|
|
491
|
-
interface CodeExecConfig {
|
|
492
|
-
language?: string;
|
|
493
|
-
timeoutMs?: number;
|
|
494
|
-
}
|
|
495
|
-
interface Tool {
|
|
496
|
-
type: ToolType;
|
|
497
|
-
function?: FunctionTool;
|
|
498
|
-
web?: WebSearchConfig;
|
|
499
|
-
xSearch?: XSearchConfig;
|
|
500
|
-
codeExecution?: CodeExecConfig;
|
|
501
|
-
}
|
|
502
|
-
declare const ToolChoiceTypes: {
|
|
503
|
-
readonly Auto: "auto";
|
|
504
|
-
readonly Required: "required";
|
|
505
|
-
readonly None: "none";
|
|
506
|
-
};
|
|
507
|
-
type ToolChoiceType = (typeof ToolChoiceTypes)[keyof typeof ToolChoiceTypes];
|
|
508
|
-
interface ToolChoice {
|
|
509
|
-
type: ToolChoiceType;
|
|
510
|
-
/**
|
|
511
|
-
* Optional function tool name to force.
|
|
512
|
-
* Only valid when type is "required".
|
|
513
|
-
*/
|
|
514
|
-
function?: string;
|
|
515
|
-
}
|
|
516
|
-
interface FunctionCall {
|
|
517
|
-
name: string;
|
|
518
|
-
arguments: string;
|
|
519
|
-
}
|
|
520
|
-
interface ToolCall {
|
|
521
|
-
id: string;
|
|
522
|
-
type: ToolType;
|
|
523
|
-
function?: FunctionCall;
|
|
524
|
-
}
|
|
525
|
-
declare const OutputFormatTypes: {
|
|
526
|
-
readonly Text: "text";
|
|
527
|
-
readonly JsonSchema: "json_schema";
|
|
528
|
-
};
|
|
529
|
-
type OutputFormatType = (typeof OutputFormatTypes)[keyof typeof OutputFormatTypes];
|
|
530
|
-
interface JSONSchemaFormat {
|
|
531
|
-
name: string;
|
|
532
|
-
description?: string;
|
|
533
|
-
schema: Record<string, unknown>;
|
|
534
|
-
strict?: boolean;
|
|
535
|
-
}
|
|
536
|
-
interface OutputFormat {
|
|
537
|
-
type: OutputFormatType;
|
|
538
|
-
json_schema?: JSONSchemaFormat;
|
|
539
|
-
}
|
|
540
|
-
interface Citation {
|
|
541
|
-
url?: string;
|
|
542
|
-
title?: string;
|
|
543
|
-
}
|
|
544
|
-
interface Response {
|
|
545
|
-
id: string;
|
|
546
|
-
output: OutputItem[];
|
|
547
|
-
stopReason?: StopReason;
|
|
548
|
-
model: ModelId;
|
|
549
|
-
usage: Usage;
|
|
550
|
-
requestId?: string;
|
|
551
|
-
provider?: ProviderId;
|
|
552
|
-
citations?: Citation[];
|
|
553
|
-
}
|
|
554
|
-
interface FieldError {
|
|
555
|
-
field?: string;
|
|
556
|
-
message: string;
|
|
557
|
-
}
|
|
558
|
-
interface RetryConfig {
|
|
559
|
-
maxAttempts?: number;
|
|
560
|
-
baseBackoffMs?: number;
|
|
561
|
-
maxBackoffMs?: number;
|
|
562
|
-
retryPost?: boolean;
|
|
563
|
-
}
|
|
564
|
-
interface RetryMetadata {
|
|
565
|
-
attempts: number;
|
|
566
|
-
lastStatus?: number;
|
|
567
|
-
lastError?: string;
|
|
568
|
-
}
|
|
569
|
-
type TransportErrorKind = "timeout" | "connect" | "request" | "empty_response" | "other";
|
|
570
|
-
interface RequestContext {
|
|
571
|
-
method: string;
|
|
572
|
-
path: string;
|
|
573
|
-
model?: ModelId;
|
|
574
|
-
requestId?: string;
|
|
575
|
-
responseId?: string;
|
|
576
|
-
}
|
|
577
|
-
interface HttpRequestMetrics {
|
|
578
|
-
latencyMs: number;
|
|
579
|
-
status?: number;
|
|
580
|
-
error?: string;
|
|
581
|
-
retries?: RetryMetadata;
|
|
582
|
-
context: RequestContext;
|
|
583
|
-
}
|
|
584
|
-
interface StreamFirstTokenMetrics {
|
|
585
|
-
latencyMs: number;
|
|
586
|
-
error?: string;
|
|
587
|
-
context: RequestContext;
|
|
588
|
-
}
|
|
589
|
-
interface TokenUsageMetrics {
|
|
590
|
-
usage: Usage;
|
|
591
|
-
context: RequestContext;
|
|
592
|
-
}
|
|
593
|
-
interface MetricsCallbacks {
|
|
594
|
-
httpRequest?: (metrics: HttpRequestMetrics) => void;
|
|
595
|
-
streamFirstToken?: (metrics: StreamFirstTokenMetrics) => void;
|
|
596
|
-
usage?: (metrics: TokenUsageMetrics) => void;
|
|
597
|
-
}
|
|
598
|
-
interface TraceCallbacks {
|
|
599
|
-
requestStart?: (context: RequestContext) => void;
|
|
600
|
-
requestFinish?: (info: {
|
|
601
|
-
context: RequestContext;
|
|
602
|
-
status?: number;
|
|
603
|
-
error?: unknown;
|
|
604
|
-
retries?: RetryMetadata;
|
|
605
|
-
latencyMs: number;
|
|
606
|
-
}) => void;
|
|
607
|
-
streamEvent?: (info: {
|
|
608
|
-
context: RequestContext;
|
|
609
|
-
event: ResponseEvent;
|
|
610
|
-
}) => void;
|
|
611
|
-
streamError?: (info: {
|
|
612
|
-
context: RequestContext;
|
|
613
|
-
error: unknown;
|
|
614
|
-
}) => void;
|
|
615
|
-
}
|
|
616
|
-
declare function mergeMetrics(base?: MetricsCallbacks, override?: MetricsCallbacks): MetricsCallbacks | undefined;
|
|
617
|
-
declare function mergeTrace(base?: TraceCallbacks, override?: TraceCallbacks): TraceCallbacks | undefined;
|
|
618
|
-
declare function normalizeStopReason(value?: unknown): StopReason | undefined;
|
|
619
|
-
declare function stopReasonToString(value?: StopReason): string | undefined;
|
|
620
|
-
declare function normalizeModelId(value: unknown): ModelId | undefined;
|
|
621
|
-
declare function modelToString(value: ModelId): string;
|
|
622
|
-
type ResponseEventType = "message_start" | "message_delta" | "message_stop" | "tool_use_start" | "tool_use_delta" | "tool_use_stop" | "ping" | "custom";
|
|
623
|
-
interface MessageStartData {
|
|
624
|
-
responseId?: string;
|
|
625
|
-
model?: string;
|
|
626
|
-
message?: Record<string, unknown>;
|
|
627
|
-
[key: string]: unknown;
|
|
628
|
-
}
|
|
629
|
-
interface MessageDeltaData {
|
|
630
|
-
delta?: string | {
|
|
631
|
-
text?: string;
|
|
632
|
-
[key: string]: unknown;
|
|
633
|
-
};
|
|
634
|
-
responseId?: string;
|
|
635
|
-
model?: string;
|
|
636
|
-
[key: string]: unknown;
|
|
637
|
-
}
|
|
638
|
-
interface MessageStopData {
|
|
639
|
-
stopReason?: StopReason;
|
|
640
|
-
usage?: Usage;
|
|
641
|
-
responseId?: string;
|
|
642
|
-
model?: ModelId;
|
|
643
|
-
[key: string]: unknown;
|
|
644
|
-
}
|
|
645
|
-
/** Incremental update to a tool call during streaming. */
|
|
646
|
-
interface ToolCallDelta {
|
|
647
|
-
index: number;
|
|
648
|
-
id?: string;
|
|
649
|
-
type?: string;
|
|
650
|
-
function?: FunctionCallDelta;
|
|
651
|
-
}
|
|
652
|
-
/** Incremental function call data. */
|
|
653
|
-
interface FunctionCallDelta {
|
|
654
|
-
name?: string;
|
|
655
|
-
arguments?: string;
|
|
656
|
-
}
|
|
657
|
-
interface ResponseEvent<T = unknown> {
|
|
658
|
-
type: ResponseEventType;
|
|
659
|
-
event: string;
|
|
660
|
-
data?: T;
|
|
661
|
-
textDelta?: string;
|
|
662
|
-
/** Incremental tool call update during streaming. */
|
|
663
|
-
toolCallDelta?: ToolCallDelta;
|
|
664
|
-
/** Completed tool calls when type is tool_use_stop or message_stop. */
|
|
665
|
-
toolCalls?: ToolCall[];
|
|
666
|
-
/** Tool result payload when type is tool_use_stop. */
|
|
667
|
-
toolResult?: unknown;
|
|
668
|
-
responseId?: string;
|
|
669
|
-
model?: ModelId;
|
|
670
|
-
stopReason?: StopReason;
|
|
671
|
-
usage?: Usage;
|
|
672
|
-
requestId?: string;
|
|
673
|
-
raw: string;
|
|
674
|
-
}
|
|
675
|
-
type StructuredJSONRecordType = "start" | "update" | "completion" | "error";
|
|
676
|
-
/**
|
|
677
|
-
* Recursively makes all properties optional.
|
|
678
|
-
* Useful for typing partial payloads during progressive streaming before
|
|
679
|
-
* all fields are complete.
|
|
680
|
-
*
|
|
681
|
-
* @example
|
|
682
|
-
* interface Article { title: string; body: string; }
|
|
683
|
-
* type PartialArticle = DeepPartial<Article>;
|
|
684
|
-
* // { title?: string; body?: string; }
|
|
685
|
-
*/
|
|
686
|
-
type DeepPartial<T> = T extends object ? {
|
|
687
|
-
[P in keyof T]?: DeepPartial<T[P]>;
|
|
688
|
-
} : T;
|
|
689
|
-
interface StructuredJSONEvent<T> {
|
|
690
|
-
type: "update" | "completion";
|
|
691
|
-
payload: T;
|
|
692
|
-
requestId?: string;
|
|
693
|
-
/**
|
|
694
|
-
* Set of field paths that are complete (have their closing delimiter).
|
|
695
|
-
* Use dot notation for nested fields (e.g., "metadata.author").
|
|
696
|
-
* Check with completeFields.has("fieldName").
|
|
697
|
-
*/
|
|
698
|
-
completeFields: Set<string>;
|
|
699
|
-
}
|
|
700
|
-
interface APIFrontendToken {
|
|
701
|
-
token: string;
|
|
702
|
-
expires_at: string;
|
|
703
|
-
expires_in: number;
|
|
704
|
-
token_type: TokenType;
|
|
705
|
-
key_id: string;
|
|
706
|
-
session_id: string;
|
|
707
|
-
project_id: string;
|
|
708
|
-
/** Optional for BYOB projects where customers are not created in the billing sense. */
|
|
709
|
-
customer_id?: string;
|
|
710
|
-
customer_external_id: string;
|
|
711
|
-
tier_code?: TierCode;
|
|
712
|
-
}
|
|
713
|
-
interface APICustomerRef {
|
|
714
|
-
id: string;
|
|
715
|
-
external_id: string;
|
|
716
|
-
owner_id: string;
|
|
717
|
-
}
|
|
718
|
-
interface APICheckoutSession {
|
|
719
|
-
id: string;
|
|
720
|
-
plan: string;
|
|
721
|
-
status: string;
|
|
722
|
-
url: string;
|
|
723
|
-
expires_at?: string;
|
|
724
|
-
completed_at?: string;
|
|
725
|
-
}
|
|
726
|
-
interface APIUsage {
|
|
727
|
-
input_tokens?: number;
|
|
728
|
-
output_tokens?: number;
|
|
729
|
-
total_tokens?: number;
|
|
730
|
-
}
|
|
731
|
-
interface APIResponsesResponse {
|
|
732
|
-
id?: string;
|
|
733
|
-
stop_reason?: string;
|
|
734
|
-
model?: string;
|
|
735
|
-
usage?: APIUsage;
|
|
736
|
-
provider?: string;
|
|
737
|
-
output?: OutputItem[];
|
|
738
|
-
citations?: Citation[];
|
|
739
|
-
}
|
|
740
|
-
interface APIKey {
|
|
741
|
-
id: string;
|
|
742
|
-
label: string;
|
|
743
|
-
kind: string;
|
|
744
|
-
createdAt: Date;
|
|
745
|
-
expiresAt?: Date;
|
|
746
|
-
lastUsedAt?: Date;
|
|
747
|
-
redactedKey: string;
|
|
748
|
-
secretKey?: string;
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
/**
|
|
752
|
-
* Creates a user message.
|
|
753
|
-
*/
|
|
754
|
-
declare function createUserMessage(content: string): InputItem;
|
|
755
|
-
/**
|
|
756
|
-
* Creates an assistant message.
|
|
757
|
-
*/
|
|
758
|
-
declare function createAssistantMessage(content: string): InputItem;
|
|
759
|
-
/**
|
|
760
|
-
* Creates a system message.
|
|
761
|
-
*/
|
|
762
|
-
declare function createSystemMessage(content: string): InputItem;
|
|
763
|
-
/**
|
|
764
|
-
* Creates a tool call object.
|
|
765
|
-
*/
|
|
766
|
-
declare function createToolCall(id: string, name: string, args: string, type?: ToolType): ToolCall;
|
|
767
|
-
/**
|
|
768
|
-
* Creates a function call object.
|
|
769
|
-
*/
|
|
770
|
-
declare function createFunctionCall(name: string, args: string): FunctionCall;
|
|
771
|
-
/**
|
|
772
|
-
* Interface for Zod-like schema types.
|
|
773
|
-
* Compatible with Zod's ZodType and similar libraries.
|
|
774
|
-
*/
|
|
775
|
-
interface ZodLikeSchema {
|
|
776
|
-
_def: {
|
|
777
|
-
typeName: string;
|
|
778
|
-
[key: string]: unknown;
|
|
779
|
-
};
|
|
780
|
-
parse(data: unknown): unknown;
|
|
781
|
-
safeParse(data: unknown): {
|
|
782
|
-
success: boolean;
|
|
783
|
-
data?: unknown;
|
|
784
|
-
error?: unknown;
|
|
785
|
-
};
|
|
786
|
-
}
|
|
787
|
-
/**
|
|
788
|
-
* Options for JSON Schema generation.
|
|
789
|
-
*/
|
|
790
|
-
interface JsonSchemaOptions {
|
|
791
|
-
/** Whether to include $schema property. Defaults to false. */
|
|
792
|
-
includeSchema?: boolean;
|
|
793
|
-
/** Target JSON Schema version. Defaults to "draft-07". */
|
|
794
|
-
target?: "draft-04" | "draft-07" | "draft-2019-09" | "draft-2020-12";
|
|
795
|
-
}
|
|
796
|
-
/**
|
|
797
|
-
* Converts a Zod schema to JSON Schema.
|
|
798
|
-
* This is a simplified implementation that handles common Zod types.
|
|
799
|
-
* For full Zod support, consider using the 'zod-to-json-schema' package.
|
|
800
|
-
*
|
|
801
|
-
* @param schema - A Zod schema
|
|
802
|
-
* @param options - Optional JSON Schema generation options
|
|
803
|
-
* @returns A JSON Schema object
|
|
804
|
-
*/
|
|
805
|
-
declare function zodToJsonSchema(schema: ZodLikeSchema, options?: JsonSchemaOptions): Record<string, unknown>;
|
|
806
|
-
/**
|
|
807
|
-
* Creates a function tool from a Zod schema.
|
|
808
|
-
*
|
|
809
|
-
* This function automatically converts a Zod schema to a JSON Schema
|
|
810
|
-
* and creates a tool definition. It eliminates the need to manually
|
|
811
|
-
* write JSON schemas for tool parameters.
|
|
812
|
-
*
|
|
813
|
-
* @example
|
|
814
|
-
* ```typescript
|
|
815
|
-
* import { z } from "zod";
|
|
816
|
-
* import { createFunctionToolFromSchema } from "@modelrelay/sdk";
|
|
817
|
-
*
|
|
818
|
-
* const weatherParams = z.object({
|
|
819
|
-
* location: z.string().describe("City name"),
|
|
820
|
-
* unit: z.enum(["celsius", "fahrenheit"]).default("celsius"),
|
|
821
|
-
* });
|
|
822
|
-
*
|
|
823
|
-
* const weatherTool = createFunctionToolFromSchema(
|
|
824
|
-
* "get_weather",
|
|
825
|
-
* "Get weather for a location",
|
|
826
|
-
* weatherParams
|
|
827
|
-
* );
|
|
828
|
-
* ```
|
|
829
|
-
*
|
|
830
|
-
* @param name - The tool name
|
|
831
|
-
* @param description - A description of what the tool does
|
|
832
|
-
* @param schema - A Zod schema defining the tool's parameters
|
|
833
|
-
* @param options - Optional JSON Schema generation options
|
|
834
|
-
* @returns A Tool definition with the JSON schema derived from the Zod schema
|
|
835
|
-
*/
|
|
836
|
-
declare function createFunctionToolFromSchema(name: string, description: string, schema: ZodLikeSchema, options?: JsonSchemaOptions): Tool;
|
|
837
|
-
/**
|
|
838
|
-
* Creates a function tool with the given name, description, and JSON schema.
|
|
839
|
-
*/
|
|
840
|
-
declare function createFunctionTool(name: string, description: string, parameters?: Record<string, unknown>): Tool;
|
|
841
|
-
/**
|
|
842
|
-
* Creates a web tool with optional domain filters and intent.
|
|
843
|
-
*/
|
|
844
|
-
declare function createWebTool(options?: {
|
|
845
|
-
intent?: WebToolIntent;
|
|
846
|
-
allowedDomains?: string[];
|
|
847
|
-
excludedDomains?: string[];
|
|
848
|
-
maxUses?: number;
|
|
849
|
-
}): Tool;
|
|
850
|
-
/**
|
|
851
|
-
* Returns a ToolChoice that lets the model decide when to use tools.
|
|
852
|
-
*/
|
|
853
|
-
declare function toolChoiceAuto(): ToolChoice;
|
|
854
|
-
/**
|
|
855
|
-
* Returns a ToolChoice that forces the model to use a tool.
|
|
856
|
-
*/
|
|
857
|
-
declare function toolChoiceRequired(): ToolChoice;
|
|
858
|
-
/**
|
|
859
|
-
* Returns a ToolChoice that prevents the model from using tools.
|
|
860
|
-
*/
|
|
861
|
-
declare function toolChoiceNone(): ToolChoice;
|
|
862
|
-
/**
|
|
863
|
-
* Returns true if the response contains tool calls.
|
|
864
|
-
*/
|
|
865
|
-
declare function hasToolCalls(response: Response): boolean;
|
|
866
|
-
/**
|
|
867
|
-
* Returns the first tool call from a response, or undefined if none exist.
|
|
868
|
-
*/
|
|
869
|
-
declare function firstToolCall(response: Response): ToolCall | undefined;
|
|
870
|
-
/**
|
|
871
|
-
* Creates a message containing the result of a tool call.
|
|
872
|
-
*/
|
|
873
|
-
declare function toolResultMessage(toolCallId: string, result: unknown): InputItem;
|
|
874
|
-
/**
|
|
875
|
-
* Creates a tool result message from a ToolCall.
|
|
876
|
-
* Convenience wrapper around toolResultMessage using the call's ID.
|
|
877
|
-
*/
|
|
878
|
-
declare function respondToToolCall(call: ToolCall, result: unknown): InputItem;
|
|
879
|
-
/**
|
|
880
|
-
* Creates an assistant message that includes tool calls.
|
|
881
|
-
* Used to include the assistant's tool-calling turn in conversation history.
|
|
882
|
-
*/
|
|
883
|
-
declare function assistantMessageWithToolCalls(content: string, toolCalls: ToolCall[]): InputItem;
|
|
884
|
-
/**
|
|
885
|
-
* Accumulates streaming tool call deltas into complete tool calls.
|
|
886
|
-
*/
|
|
887
|
-
declare class ToolCallAccumulator {
|
|
888
|
-
private calls;
|
|
889
|
-
/**
|
|
890
|
-
* Processes a streaming tool call delta.
|
|
891
|
-
* Returns true if this started a new tool call.
|
|
892
|
-
*/
|
|
893
|
-
processDelta(delta: ToolCallDelta): boolean;
|
|
894
|
-
/**
|
|
895
|
-
* Returns all accumulated tool calls in index order.
|
|
896
|
-
*/
|
|
897
|
-
getToolCalls(): ToolCall[];
|
|
898
|
-
/**
|
|
899
|
-
* Returns a specific tool call by index, or undefined if not found.
|
|
900
|
-
*/
|
|
901
|
-
getToolCall(index: number): ToolCall | undefined;
|
|
902
|
-
/**
|
|
903
|
-
* Clears all accumulated tool calls.
|
|
904
|
-
*/
|
|
905
|
-
reset(): void;
|
|
906
|
-
}
|
|
907
|
-
/**
|
|
908
|
-
* Error thrown when tool argument parsing or validation fails.
|
|
909
|
-
* Contains a descriptive message suitable for sending back to the model.
|
|
910
|
-
*/
|
|
911
|
-
declare class ToolArgsError extends Error {
|
|
912
|
-
/** The tool call ID for correlation */
|
|
913
|
-
readonly toolCallId: string;
|
|
914
|
-
/** The tool name that was called */
|
|
915
|
-
readonly toolName: string;
|
|
916
|
-
/** The raw arguments string that failed to parse */
|
|
917
|
-
readonly rawArguments: string;
|
|
918
|
-
constructor(message: string, toolCallId: string, toolName: string, rawArguments: string);
|
|
919
|
-
}
|
|
920
|
-
/**
|
|
921
|
-
* Schema interface compatible with Zod, Yup, and similar validation libraries.
|
|
922
|
-
* Any object with a `parse` method that returns the validated type works.
|
|
923
|
-
*/
|
|
924
|
-
interface Schema<T> {
|
|
925
|
-
parse(data: unknown): T;
|
|
926
|
-
}
|
|
927
|
-
/**
|
|
928
|
-
* Parses and validates tool call arguments using a schema.
|
|
929
|
-
*
|
|
930
|
-
* Works with any schema library that has a `parse` method (Zod, Yup, etc.).
|
|
931
|
-
* Throws a descriptive ToolArgsError if parsing or validation fails.
|
|
932
|
-
*
|
|
933
|
-
* @example
|
|
934
|
-
* ```typescript
|
|
935
|
-
* import { z } from "zod";
|
|
936
|
-
*
|
|
937
|
-
* const WeatherArgs = z.object({
|
|
938
|
-
* location: z.string(),
|
|
939
|
-
* unit: z.enum(["celsius", "fahrenheit"]).default("celsius"),
|
|
940
|
-
* });
|
|
941
|
-
*
|
|
942
|
-
* // In your tool handler:
|
|
943
|
-
* const args = parseToolArgs(toolCall, WeatherArgs);
|
|
944
|
-
* // args is typed as { location: string; unit: "celsius" | "fahrenheit" }
|
|
945
|
-
* ```
|
|
946
|
-
*
|
|
947
|
-
* @param call - The tool call containing arguments to parse
|
|
948
|
-
* @param schema - A schema with a `parse` method (Zod, Yup, etc.)
|
|
949
|
-
* @returns The parsed and validated arguments with proper types
|
|
950
|
-
* @throws {ToolArgsError} If JSON parsing or schema validation fails
|
|
951
|
-
*/
|
|
952
|
-
declare function parseToolArgs<T>(call: ToolCall, schema: Schema<T>): T;
|
|
953
|
-
/**
|
|
954
|
-
* Attempts to parse tool arguments, returning a result object instead of throwing.
|
|
955
|
-
*
|
|
956
|
-
* @example
|
|
957
|
-
* ```typescript
|
|
958
|
-
* const result = tryParseToolArgs(toolCall, WeatherArgs);
|
|
959
|
-
* if (result.success) {
|
|
960
|
-
* console.log(result.data.location);
|
|
961
|
-
* } else {
|
|
962
|
-
* console.error(result.error.message);
|
|
963
|
-
* }
|
|
964
|
-
* ```
|
|
965
|
-
*
|
|
966
|
-
* @param call - The tool call containing arguments to parse
|
|
967
|
-
* @param schema - A schema with a `parse` method
|
|
968
|
-
* @returns An object with either { success: true, data: T } or { success: false, error: ToolArgsError }
|
|
969
|
-
*/
|
|
970
|
-
declare function tryParseToolArgs<T>(call: ToolCall, schema: Schema<T>): {
|
|
971
|
-
success: true;
|
|
972
|
-
data: T;
|
|
973
|
-
} | {
|
|
974
|
-
success: false;
|
|
975
|
-
error: ToolArgsError;
|
|
976
|
-
};
|
|
977
|
-
/**
|
|
978
|
-
* Parses raw JSON arguments without schema validation.
|
|
979
|
-
* Useful when you want JSON parsing error handling but not schema validation.
|
|
980
|
-
*
|
|
981
|
-
* @param call - The tool call containing arguments to parse
|
|
982
|
-
* @returns The parsed JSON as an unknown type
|
|
983
|
-
* @throws {ToolArgsError} If JSON parsing fails
|
|
984
|
-
*/
|
|
985
|
-
declare function parseToolArgsRaw(call: ToolCall): unknown;
|
|
986
|
-
/**
|
|
987
|
-
* Handler function type for tool execution.
|
|
988
|
-
* Can be sync or async, receives parsed arguments and returns a result.
|
|
989
|
-
*/
|
|
990
|
-
type ToolHandler<T = unknown, R = unknown> = (args: T, call: ToolCall) => R | Promise<R>;
|
|
991
|
-
/**
|
|
992
|
-
* Result of executing a tool call.
|
|
993
|
-
*/
|
|
994
|
-
interface ToolExecutionResult {
|
|
995
|
-
toolCallId: string;
|
|
996
|
-
toolName: string;
|
|
997
|
-
result: unknown;
|
|
998
|
-
error?: string;
|
|
999
|
-
/**
|
|
1000
|
-
* True if the error is due to malformed arguments (JSON parse or validation failure)
|
|
1001
|
-
* and the model should be given a chance to retry with corrected arguments.
|
|
1002
|
-
*/
|
|
1003
|
-
isRetryable?: boolean;
|
|
1004
|
-
}
|
|
1005
|
-
/**
|
|
1006
|
-
* Registry for mapping tool names to handler functions with automatic dispatch.
|
|
1007
|
-
*
|
|
1008
|
-
* @example
|
|
1009
|
-
* ```typescript
|
|
1010
|
-
* const registry = new ToolRegistry()
|
|
1011
|
-
* .register("get_weather", async (args) => {
|
|
1012
|
-
* return { temp: 72, unit: "fahrenheit" };
|
|
1013
|
-
* })
|
|
1014
|
-
* .register("search", async (args) => {
|
|
1015
|
-
* return { results: ["result1", "result2"] };
|
|
1016
|
-
* });
|
|
1017
|
-
*
|
|
1018
|
-
* // Execute all tool calls from a response
|
|
1019
|
-
* const results = await registry.executeAll(response.toolCalls);
|
|
1020
|
-
*
|
|
1021
|
-
* // Convert results to messages for the next request
|
|
1022
|
-
* const messages = registry.resultsToMessages(results);
|
|
1023
|
-
* ```
|
|
1024
|
-
*/
|
|
1025
|
-
declare class ToolRegistry {
|
|
1026
|
-
private handlers;
|
|
1027
|
-
/**
|
|
1028
|
-
* Registers a handler function for a tool name.
|
|
1029
|
-
* @param name - The tool name (must match the function name in the tool definition)
|
|
1030
|
-
* @param handler - Function to execute when this tool is called
|
|
1031
|
-
* @returns this for chaining
|
|
1032
|
-
*/
|
|
1033
|
-
register<T = unknown, R = unknown>(name: string, handler: ToolHandler<T, R>): this;
|
|
1034
|
-
/**
|
|
1035
|
-
* Unregisters a tool handler.
|
|
1036
|
-
* @param name - The tool name to unregister
|
|
1037
|
-
* @returns true if the handler was removed, false if it didn't exist
|
|
1038
|
-
*/
|
|
1039
|
-
unregister(name: string): boolean;
|
|
1040
|
-
/**
|
|
1041
|
-
* Checks if a handler is registered for the given tool name.
|
|
1042
|
-
*/
|
|
1043
|
-
has(name: string): boolean;
|
|
1044
|
-
/**
|
|
1045
|
-
* Returns the list of registered tool names.
|
|
1046
|
-
*/
|
|
1047
|
-
getRegisteredTools(): string[];
|
|
1048
|
-
/**
|
|
1049
|
-
* Executes a single tool call.
|
|
1050
|
-
* @param call - The tool call to execute
|
|
1051
|
-
* @returns The execution result
|
|
1052
|
-
*/
|
|
1053
|
-
execute(call: ToolCall): Promise<ToolExecutionResult>;
|
|
1054
|
-
/**
|
|
1055
|
-
* Executes multiple tool calls in parallel.
|
|
1056
|
-
* @param calls - Array of tool calls to execute
|
|
1057
|
-
* @returns Array of execution results in the same order as input
|
|
1058
|
-
*/
|
|
1059
|
-
executeAll(calls: ToolCall[]): Promise<ToolExecutionResult[]>;
|
|
1060
|
-
/**
|
|
1061
|
-
* Converts execution results to tool result messages.
|
|
1062
|
-
* Useful for appending to the conversation history.
|
|
1063
|
-
* @param results - Array of execution results
|
|
1064
|
-
* @returns Array of tool result input items (role "tool")
|
|
1065
|
-
*/
|
|
1066
|
-
resultsToMessages(results: ToolExecutionResult[]): InputItem[];
|
|
1067
|
-
}
|
|
1068
|
-
/**
|
|
1069
|
-
* Formats a tool execution error into a message suitable for sending back to the model.
|
|
1070
|
-
* The message is designed to help the model understand what went wrong and correct it.
|
|
1071
|
-
*
|
|
1072
|
-
* @example
|
|
1073
|
-
* ```typescript
|
|
1074
|
-
* const result = await registry.execute(toolCall);
|
|
1075
|
-
* if (result.error && result.isRetryable) {
|
|
1076
|
-
* const errorMessage = formatToolErrorForModel(result);
|
|
1077
|
-
* messages.push(toolResultMessage(result.toolCallId, errorMessage));
|
|
1078
|
-
* // Continue conversation to let model retry
|
|
1079
|
-
* }
|
|
1080
|
-
* ```
|
|
1081
|
-
*/
|
|
1082
|
-
declare function formatToolErrorForModel(result: ToolExecutionResult): string;
|
|
1083
|
-
/**
|
|
1084
|
-
* Checks if any results have retryable errors.
|
|
1085
|
-
*
|
|
1086
|
-
* @example
|
|
1087
|
-
* ```typescript
|
|
1088
|
-
* const results = await registry.executeAll(toolCalls);
|
|
1089
|
-
* if (hasRetryableErrors(results)) {
|
|
1090
|
-
* // Send error messages back to model and continue conversation
|
|
1091
|
-
* }
|
|
1092
|
-
* ```
|
|
1093
|
-
*/
|
|
1094
|
-
declare function hasRetryableErrors(results: ToolExecutionResult[]): boolean;
|
|
1095
|
-
/**
|
|
1096
|
-
* Filters results to only those with retryable errors.
|
|
1097
|
-
*/
|
|
1098
|
-
declare function getRetryableErrors(results: ToolExecutionResult[]): ToolExecutionResult[];
|
|
1099
|
-
/**
|
|
1100
|
-
* Creates tool result messages for retryable errors, formatted to help the model correct them.
|
|
1101
|
-
*
|
|
1102
|
-
* @example
|
|
1103
|
-
* ```typescript
|
|
1104
|
-
* const results = await registry.executeAll(toolCalls);
|
|
1105
|
-
* if (hasRetryableErrors(results)) {
|
|
1106
|
-
* const retryMessages = createRetryMessages(results);
|
|
1107
|
-
* messages.push(...retryMessages);
|
|
1108
|
-
* // Make another API call to let model retry
|
|
1109
|
-
* }
|
|
1110
|
-
* ```
|
|
1111
|
-
*/
|
|
1112
|
-
declare function createRetryMessages(results: ToolExecutionResult[]): InputItem[];
|
|
1113
|
-
/**
|
|
1114
|
-
* Options for executeWithRetry.
|
|
1115
|
-
*/
|
|
1116
|
-
interface RetryOptions {
|
|
1117
|
-
/**
|
|
1118
|
-
* Maximum number of retry attempts for parse/validation errors.
|
|
1119
|
-
* @default 2
|
|
1120
|
-
*/
|
|
1121
|
-
maxRetries?: number;
|
|
1122
|
-
/**
|
|
1123
|
-
* Callback invoked when a retryable error occurs.
|
|
1124
|
-
* Should return new tool calls from the model's response.
|
|
1125
|
-
* If not provided, executeWithRetry will not retry automatically.
|
|
1126
|
-
*
|
|
1127
|
-
* @param errorMessages - Messages to send back to the model
|
|
1128
|
-
* @param attempt - Current attempt number (1-based)
|
|
1129
|
-
* @returns New tool calls from the model, or empty array to stop retrying
|
|
1130
|
-
*/
|
|
1131
|
-
onRetry?: (errorMessages: InputItem[], attempt: number) => Promise<ToolCall[]>;
|
|
1132
|
-
}
|
|
1133
|
-
/**
|
|
1134
|
-
* Executes tool calls with automatic retry on parse/validation errors.
|
|
1135
|
-
*
|
|
1136
|
-
* This is a higher-level utility that wraps registry.executeAll with retry logic.
|
|
1137
|
-
* When a retryable error occurs, it calls the onRetry callback to get new tool calls
|
|
1138
|
-
* from the model and continues execution.
|
|
1139
|
-
*
|
|
1140
|
-
* **Result Preservation**: Successful results are preserved across retries. If you
|
|
1141
|
-
* execute multiple tool calls and only some fail, the successful results are kept
|
|
1142
|
-
* and merged with the results from retry attempts. Results are keyed by toolCallId,
|
|
1143
|
-
* so if a retry returns a call with the same ID as a previous result, the newer
|
|
1144
|
-
* result will replace it.
|
|
1145
|
-
*
|
|
1146
|
-
* @example
|
|
1147
|
-
* ```typescript
|
|
1148
|
-
* const results = await executeWithRetry(registry, toolCalls, {
|
|
1149
|
-
* maxRetries: 2,
|
|
1150
|
-
* onRetry: async (errorMessages, attempt) => {
|
|
1151
|
-
* console.log(`Retry attempt ${attempt}`);
|
|
1152
|
-
* // Add error messages to conversation and call the model again
|
|
1153
|
-
* messages.push(assistantMessageWithToolCalls("", toolCalls));
|
|
1154
|
-
* messages.push(...errorMessages);
|
|
1155
|
-
* const req = client.responses
|
|
1156
|
-
* .new()
|
|
1157
|
-
* .model("...")
|
|
1158
|
-
* .input(messages)
|
|
1159
|
-
* .tools(tools)
|
|
1160
|
-
* .build();
|
|
1161
|
-
* const response = await client.responses.create(req);
|
|
1162
|
-
* return firstToolCall(response) ? [firstToolCall(response)!] : [];
|
|
1163
|
-
* },
|
|
1164
|
-
* });
|
|
1165
|
-
* ```
|
|
1166
|
-
*
|
|
1167
|
-
* @param registry - The tool registry to use for execution
|
|
1168
|
-
* @param toolCalls - Initial tool calls to execute
|
|
1169
|
-
* @param options - Retry configuration
|
|
1170
|
-
* @returns Final execution results after all retries, including preserved successes
|
|
1171
|
-
*/
|
|
1172
|
-
declare function executeWithRetry(registry: ToolRegistry, toolCalls: ToolCall[], options?: RetryOptions): Promise<ToolExecutionResult[]>;
|
|
1173
|
-
|
|
1174
|
-
export { zodToJsonSchema as $, type ApiKey as A, createWebTool as B, type CustomerTokenRequest as C, type DeviceStartRequest as D, toolChoiceAuto as E, type FrontendCustomer as F, toolChoiceRequired as G, toolChoiceNone as H, type InputItem as I, hasToolCalls as J, firstToolCall as K, createUserMessage as L, type MetricsCallbacks as M, createAssistantMessage as N, type OIDCExchangeRequest as O, type ProviderId as P, createSystemMessage as Q, type RetryConfig as R, type StructuredJSONEvent as S, type TraceCallbacks as T, toolResultMessage as U, respondToToolCall as V, assistantMessageWithToolCalls as W, createToolCall as X, createFunctionCall as Y, type ZodLikeSchema as Z, ToolCallAccumulator as _, type RequestContext as a, type OutputFormatType as a$, parseToolArgs as a0, tryParseToolArgs as a1, parseToolArgsRaw as a2, ToolArgsError as a3, formatToolErrorForModel as a4, hasRetryableErrors as a5, getRetryableErrors as a6, createRetryMessages as a7, executeWithRetry as a8, type JsonSchemaOptions as a9, type Usage as aA, createUsage as aB, type UsageSummary as aC, type Project as aD, MessageRoles as aE, type MessageRole as aF, ContentPartTypes as aG, type ContentPartType as aH, type ContentPart as aI, InputItemTypes as aJ, type InputItemType as aK, OutputItemTypes as aL, type OutputItemType as aM, type OutputItem as aN, ToolTypes as aO, type ToolType as aP, WebToolIntents as aQ, type WebToolIntent as aR, type FunctionTool as aS, type WebSearchConfig as aT, type XSearchConfig as aU, type CodeExecConfig as aV, ToolChoiceTypes as aW, type ToolChoiceType as aX, type FunctionCall as aY, type ToolCall as aZ, OutputFormatTypes as a_, type Schema as aa, type ToolHandler as ab, type RetryOptions as ac, SDK_VERSION as ad, DEFAULT_CLIENT_HEADER as ae, DEFAULT_CONNECT_TIMEOUT_MS as af, DEFAULT_REQUEST_TIMEOUT_MS as ag, type NonEmptyArray as ah, StopReasons as ai, type KnownStopReason as aj, type StopReason as ak, type TierCode as al, asProviderId as am, asModelId as an, asTierCode as ao, SubscriptionStatuses as ap, type SubscriptionStatusKind as aq, BillingProviders as ar, type BillingProvider as as, type CustomerMetadata as at, type ModelRelayBaseOptions as au, type ModelRelayTokenOptions as av, type ModelRelayTokenProviderOptions as aw, type TokenType as ax, type DeviceTokenResponse as ay, type DeviceTokenPending as az, type TokenProvider as b, type JSONSchemaFormat as b0, type Citation as b1, type HttpRequestMetrics as b2, type StreamFirstTokenMetrics as b3, type TokenUsageMetrics as b4, mergeMetrics as b5, mergeTrace as b6, normalizeStopReason as b7, stopReasonToString as b8, normalizeModelId as b9, modelToString as ba, type ResponseEventType as bb, type MessageStartData as bc, type MessageDeltaData as bd, type MessageStopData as be, type ToolCallDelta as bf, type FunctionCallDelta as bg, type StructuredJSONRecordType as bh, type DeepPartial as bi, type APIFrontendToken as bj, type APICustomerRef as bk, type APICheckoutSession as bl, type APIUsage as bm, type APIResponsesResponse as bn, type APIKey as bo, type FrontendTokenRequest as c, type FrontendToken as d, type FrontendTokenAutoProvisionRequest as e, type CustomerToken as f, type DeviceStartResponse as g, type DeviceTokenResult as h, type ModelId as i, type OutputFormat as j, type Tool as k, type ToolChoice as l, type ResponseEvent as m, type Response as n, type FieldError as o, type RetryMetadata as p, type TransportErrorKind as q, ToolRegistry as r, type ToolExecutionResult as s, type PublishableKey as t, type SecretKey as u, type ModelRelayKeyOptions as v, type ModelRelayOptions as w, DEFAULT_BASE_URL as x, createFunctionTool as y, createFunctionToolFromSchema as z };
|