@shop-os/modelkit 0.1.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 +136 -0
- package/dist/index.cjs +5622 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +580 -0
- package/dist/index.d.ts +580 -0
- package/dist/index.js +5547 -0
- package/dist/index.js.map +1 -0
- package/package.json +73 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
import { ImageModel, experimental_generateVideo, generateText as generateText$1, LanguageModel, FlexibleSchema, InferSchema, ToolSet, Output, streamText as streamText$1, JSONValue, generateObject as generateObject$1, streamObject as streamObject$1, LanguageModelUsage } from 'ai';
|
|
2
|
+
export * from 'ai';
|
|
3
|
+
export { Warning as AiWarning } from 'ai';
|
|
4
|
+
import { LanguageModelV3, LanguageModelV2 } from '@ai-sdk/provider';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Canonical vocabulary shared by every adapter. Adapters translate these
|
|
9
|
+
* values to provider field names and casing; callers never do.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** The AI SDK video model spelling: a catalog id or a provider video model instance. */
|
|
13
|
+
type VideoModel = Parameters<typeof experimental_generateVideo>[0]["model"];
|
|
14
|
+
type AspectRatio = "1:1" | "16:9" | "9:16" | "4:3" | "3:4" | "3:2" | "2:3" | "4:5" | "5:4" | "21:9" | "9:21" | "97:30" | "adaptive";
|
|
15
|
+
type ImageSize = "1K" | "2K" | "4K";
|
|
16
|
+
type VideoResolution = "360p" | "480p" | "720p" | "768p" | "1080p" | "2k" | "4k";
|
|
17
|
+
type ImageQuality = "auto" | "low" | "medium" | "high";
|
|
18
|
+
type OutputFormat = "png" | "jpeg" | "webp";
|
|
19
|
+
type MediaRef = {
|
|
20
|
+
url: string;
|
|
21
|
+
} | {
|
|
22
|
+
bytes: Uint8Array;
|
|
23
|
+
mime: string;
|
|
24
|
+
};
|
|
25
|
+
type ModelType = "text" | "image" | "video" | "audio" | "upscale" | "bg-remove";
|
|
26
|
+
type Lifecycle = "active" | "hidden" | "deprecated" | "retired";
|
|
27
|
+
type AdapterId = "ai-sdk-text" | "google-image" | "openai-image" | "google-genai" | "ark" | "fal" | "replicate" | "prodia" | "higgsfield" | "xai-video";
|
|
28
|
+
interface ImageRequest {
|
|
29
|
+
model: string;
|
|
30
|
+
prompt: string;
|
|
31
|
+
references?: MediaRef[];
|
|
32
|
+
aspectRatio?: AspectRatio;
|
|
33
|
+
size?: ImageSize;
|
|
34
|
+
exactSize?: string;
|
|
35
|
+
quality?: ImageQuality;
|
|
36
|
+
seed?: number;
|
|
37
|
+
count?: number;
|
|
38
|
+
outputFormat?: OutputFormat;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* What a caller passes to generateImage. Same field names as the AI SDK's
|
|
42
|
+
* generateImage where the concept matches: `n`, `size` as `WxH`, `abortSignal`,
|
|
43
|
+
* `providerOptions`. The model is a catalog id or alias, or an AI SDK image
|
|
44
|
+
* model instance that runs through the SDK unchanged.
|
|
45
|
+
*/
|
|
46
|
+
interface ImageCallOptions extends Omit<ImageRequest, "model" | "size"> {
|
|
47
|
+
model: ImageModel;
|
|
48
|
+
/** Alias of `count`. */
|
|
49
|
+
n?: number;
|
|
50
|
+
/** Catalog tier or an exact `WxH` size (alias of `exactSize`). */
|
|
51
|
+
size?: ImageSize | `${number}x${number}`;
|
|
52
|
+
abortSignal?: AbortSignal;
|
|
53
|
+
/** Reaches the provider only for AI SDK model instances; catalog rows carry their own. */
|
|
54
|
+
providerOptions?: ProviderOptions;
|
|
55
|
+
}
|
|
56
|
+
interface VideoRequest {
|
|
57
|
+
model: string;
|
|
58
|
+
prompt: string;
|
|
59
|
+
firstFrame?: MediaRef;
|
|
60
|
+
lastFrame?: MediaRef;
|
|
61
|
+
sourceVideo?: MediaRef;
|
|
62
|
+
referenceImages?: MediaRef[];
|
|
63
|
+
referenceVideos?: MediaRef[];
|
|
64
|
+
referenceAudios?: MediaRef[];
|
|
65
|
+
aspectRatio?: AspectRatio;
|
|
66
|
+
resolution?: VideoResolution;
|
|
67
|
+
durationSeconds?: number;
|
|
68
|
+
generateAudio?: boolean;
|
|
69
|
+
seed?: number;
|
|
70
|
+
negativePrompt?: string;
|
|
71
|
+
cfgScale?: number;
|
|
72
|
+
promptExpansion?: "fast" | "balanced" | "quality";
|
|
73
|
+
mode?: "standard" | "pro";
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* What a caller passes to generateVideo. `duration` is the AI SDK spelling of
|
|
77
|
+
* `durationSeconds`. An AI SDK video model instance runs through
|
|
78
|
+
* `experimental_generateVideo` and is exposed as a settled job.
|
|
79
|
+
*/
|
|
80
|
+
interface VideoCallOptions extends Omit<VideoRequest, "model"> {
|
|
81
|
+
model: VideoModel;
|
|
82
|
+
/** Alias of `durationSeconds`. */
|
|
83
|
+
duration?: number;
|
|
84
|
+
abortSignal?: AbortSignal;
|
|
85
|
+
/** Reaches the provider only for AI SDK model instances; catalog rows carry their own. */
|
|
86
|
+
providerOptions?: ProviderOptions;
|
|
87
|
+
}
|
|
88
|
+
interface AudioRequest {
|
|
89
|
+
model: string;
|
|
90
|
+
prompt: string;
|
|
91
|
+
/** Lyria: optional reference image. MMAudio: the video to score. */
|
|
92
|
+
references?: MediaRef[];
|
|
93
|
+
sourceVideo?: MediaRef;
|
|
94
|
+
negativePrompt?: string;
|
|
95
|
+
durationSeconds?: number;
|
|
96
|
+
seed?: number;
|
|
97
|
+
/** Music models: no vocals. */
|
|
98
|
+
instrumental?: boolean;
|
|
99
|
+
}
|
|
100
|
+
interface AudioResult {
|
|
101
|
+
audio: {
|
|
102
|
+
bytes: Uint8Array;
|
|
103
|
+
mime: string;
|
|
104
|
+
} | {
|
|
105
|
+
url: string;
|
|
106
|
+
expiresAt?: Date;
|
|
107
|
+
};
|
|
108
|
+
/** Lyria returns lyrics as text parts next to the audio. */
|
|
109
|
+
text?: string;
|
|
110
|
+
usage: UsageEvent;
|
|
111
|
+
raw?: JsonValue;
|
|
112
|
+
}
|
|
113
|
+
/** An AI SDK file part ready for generateText/generateObject, e.g. a video for understanding. */
|
|
114
|
+
interface MediaPart {
|
|
115
|
+
type: "file";
|
|
116
|
+
data: string | Uint8Array;
|
|
117
|
+
mediaType: string;
|
|
118
|
+
/** Set when the bytes were uploaded to a provider file store (Gemini Files API). Call `cleanup` after the request. */
|
|
119
|
+
cleanup?: () => Promise<void>;
|
|
120
|
+
}
|
|
121
|
+
type RequiredInput = "firstFrame" | "sourceVideo" | "references";
|
|
122
|
+
type SupportedOption = "seed" | "quality" | "negativePrompt" | "cfgScale" | "generateAudio" | "promptExpansion" | "lastFrame" | "count" | "outputFormat" | "instrumental";
|
|
123
|
+
interface ReferenceLimits {
|
|
124
|
+
images?: number;
|
|
125
|
+
videos?: number;
|
|
126
|
+
audios?: number;
|
|
127
|
+
total?: number;
|
|
128
|
+
/** Provider rejects a request whose only references are audio. */
|
|
129
|
+
audioOnly?: boolean;
|
|
130
|
+
}
|
|
131
|
+
interface ModelConstraints {
|
|
132
|
+
aspectRatios?: AspectRatio[];
|
|
133
|
+
sizes?: ImageSize[];
|
|
134
|
+
resolutions?: VideoResolution[];
|
|
135
|
+
durations?: {
|
|
136
|
+
min: number;
|
|
137
|
+
max: number;
|
|
138
|
+
} | number[];
|
|
139
|
+
references?: ReferenceLimits;
|
|
140
|
+
requires?: RequiredInput[];
|
|
141
|
+
supports?: SupportedOption[];
|
|
142
|
+
/** Ratio is dropped, with a warning, when one of these inputs is present. */
|
|
143
|
+
ratioIgnoredWith?: ("firstFrame" | "sourceVideo")[];
|
|
144
|
+
}
|
|
145
|
+
/** JSON-compatible value. Provider options and raw payloads carry these, never `unknown`. */
|
|
146
|
+
type JsonPrimitive = string | number | boolean | null;
|
|
147
|
+
type JsonValue = JsonPrimitive | JsonValue[] | {
|
|
148
|
+
[key: string]: JsonValue;
|
|
149
|
+
};
|
|
150
|
+
type JsonObject = {
|
|
151
|
+
[key: string]: JsonValue;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Options handed to the provider untouched, keyed by provider name like the AI SDK's
|
|
155
|
+
* providerOptions: { anthropic: { thinking: ... } }. Values are JSON without null.
|
|
156
|
+
*/
|
|
157
|
+
type ProviderOptionValue = string | number | boolean | ProviderOptionValue[] | {
|
|
158
|
+
[key: string]: ProviderOptionValue;
|
|
159
|
+
};
|
|
160
|
+
type ProviderOptions = {
|
|
161
|
+
[provider: string]: {
|
|
162
|
+
[key: string]: ProviderOptionValue;
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
type UsageUnit = "tokens" | "images" | "seconds" | "reported_cost_usd" | "compute_seconds";
|
|
166
|
+
type UsageModality = "TEXT" | "IMAGE" | "VIDEO" | "AUDIO";
|
|
167
|
+
type PricingKind = "token" | "image_size" | "video_per_second" | "flat";
|
|
168
|
+
interface CatalogModel {
|
|
169
|
+
/** Canonical id. Equals the pricing key in pro-backend credit_pricing_* tables. */
|
|
170
|
+
id: string;
|
|
171
|
+
aliases: string[];
|
|
172
|
+
label: string;
|
|
173
|
+
type: ModelType;
|
|
174
|
+
adapter: AdapterId;
|
|
175
|
+
/** Id the provider expects, e.g. dola-seedream-5-0-pro-260628. */
|
|
176
|
+
providerModelId: string;
|
|
177
|
+
lifecycle: Lifecycle;
|
|
178
|
+
replacedBy?: string;
|
|
179
|
+
constraints: ModelConstraints;
|
|
180
|
+
defaults: Partial<Omit<ImageRequest, "model" | "prompt"> & Omit<VideoRequest, "model" | "prompt"> & Omit<AudioRequest, "model" | "prompt">>;
|
|
181
|
+
providerOptions?: ProviderOptions;
|
|
182
|
+
usage: {
|
|
183
|
+
unit: UsageUnit;
|
|
184
|
+
modality: UsageModality;
|
|
185
|
+
};
|
|
186
|
+
pricingKind: PricingKind;
|
|
187
|
+
}
|
|
188
|
+
interface Warning {
|
|
189
|
+
code: "ratio_coerced" | "ratio_dropped" | "size_coerced" | "resolution_coerced" | "duration_coerced" | "references_trimmed" | "option_ignored";
|
|
190
|
+
message: string;
|
|
191
|
+
field: string;
|
|
192
|
+
from?: unknown;
|
|
193
|
+
to?: unknown;
|
|
194
|
+
}
|
|
195
|
+
interface ImageBytes {
|
|
196
|
+
bytes: Uint8Array;
|
|
197
|
+
mime: string;
|
|
198
|
+
}
|
|
199
|
+
/** An image with the AI SDK `GeneratedFile` accessors next to the kit's `bytes`/`mime`. */
|
|
200
|
+
interface GeneratedImage extends ImageBytes {
|
|
201
|
+
readonly uint8Array: Uint8Array;
|
|
202
|
+
readonly base64: string;
|
|
203
|
+
readonly mediaType: string;
|
|
204
|
+
}
|
|
205
|
+
/** What an adapter returns. */
|
|
206
|
+
interface ImageResult {
|
|
207
|
+
images: ImageBytes[];
|
|
208
|
+
usage: UsageEvent;
|
|
209
|
+
raw?: unknown;
|
|
210
|
+
}
|
|
211
|
+
/** What generateImage returns: the adapter result plus `image`, SDK accessors and warnings. */
|
|
212
|
+
interface ImageCallResult extends ImageResult {
|
|
213
|
+
image: GeneratedImage;
|
|
214
|
+
images: GeneratedImage[];
|
|
215
|
+
warnings: Warning[];
|
|
216
|
+
}
|
|
217
|
+
interface VideoResult {
|
|
218
|
+
video: {
|
|
219
|
+
url: string;
|
|
220
|
+
expiresAt?: Date;
|
|
221
|
+
} | {
|
|
222
|
+
bytes: Uint8Array;
|
|
223
|
+
mime: string;
|
|
224
|
+
};
|
|
225
|
+
usage: UsageEvent;
|
|
226
|
+
raw?: unknown;
|
|
227
|
+
}
|
|
228
|
+
interface VideoCallJob extends VideoJob {
|
|
229
|
+
warnings: Warning[];
|
|
230
|
+
}
|
|
231
|
+
interface VideoJob {
|
|
232
|
+
jobId: string;
|
|
233
|
+
poll(): Promise<{
|
|
234
|
+
status: "pending" | "running" | "succeeded" | "failed";
|
|
235
|
+
raw?: unknown;
|
|
236
|
+
}>;
|
|
237
|
+
result(opts?: {
|
|
238
|
+
pollIntervalMs?: number;
|
|
239
|
+
timeoutMs?: number;
|
|
240
|
+
signal?: AbortSignal;
|
|
241
|
+
}): Promise<VideoResult>;
|
|
242
|
+
}
|
|
243
|
+
interface UsageEvent {
|
|
244
|
+
/** Canonical catalog id, never the provider id. */
|
|
245
|
+
model: string;
|
|
246
|
+
provider: string;
|
|
247
|
+
modality: UsageModality;
|
|
248
|
+
unit: UsageUnit;
|
|
249
|
+
quantity: number;
|
|
250
|
+
imageSize?: ImageSize;
|
|
251
|
+
imageQuality?: ImageQuality;
|
|
252
|
+
resolution?: VideoResolution;
|
|
253
|
+
durationSeconds?: number;
|
|
254
|
+
inputTokens?: number;
|
|
255
|
+
outputTokens?: number;
|
|
256
|
+
reportedCostUsd?: number;
|
|
257
|
+
raw?: unknown;
|
|
258
|
+
}
|
|
259
|
+
type ProviderKeyName = "openai" | "anthropic" | "google" | "xai" | "perplexity" | "deepseek" | "moonshot" | "minimax" | "ark" | "fal" | "replicate" | "prodia" | "higgsfield" | "openrouter" | "cloudflare";
|
|
260
|
+
type ProviderKeys = Partial<Record<ProviderKeyName, string>> & {
|
|
261
|
+
/** Higgsfield needs the pair; both switch together. */
|
|
262
|
+
higgsfieldSecret?: string;
|
|
263
|
+
/** Bedrock runtime credentials for Anthropic models when routing.anthropic = "bedrock". */
|
|
264
|
+
aws?: {
|
|
265
|
+
accessKeyId: string;
|
|
266
|
+
secretAccessKey: string;
|
|
267
|
+
region: string;
|
|
268
|
+
sessionToken?: string;
|
|
269
|
+
};
|
|
270
|
+
};
|
|
271
|
+
interface Routing {
|
|
272
|
+
/** Text models: direct provider, Cloudflare AI Gateway, or OpenRouter. */
|
|
273
|
+
text?: "direct" | "cloudflare" | "openrouter";
|
|
274
|
+
/** Anthropic chat via Bedrock (us. prefixed ids) or the Anthropic API. */
|
|
275
|
+
anthropic?: "direct" | "bedrock";
|
|
276
|
+
/** fal from geo-blocked regions goes through the Cloudflare passthrough. */
|
|
277
|
+
fal?: "direct" | "cloudflare-passthrough";
|
|
278
|
+
cloudflare?: {
|
|
279
|
+
accountId: string;
|
|
280
|
+
gatewayId: string;
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
interface ModelKitConfig {
|
|
284
|
+
keys: {
|
|
285
|
+
pro: ProviderKeys;
|
|
286
|
+
enterprise?: ProviderKeys;
|
|
287
|
+
};
|
|
288
|
+
routing?: Routing;
|
|
289
|
+
/** Called once per completed provider call. */
|
|
290
|
+
onUsage?: (event: UsageEvent) => void | Promise<void>;
|
|
291
|
+
/** Extra or replacement catalog rows, merged by id. */
|
|
292
|
+
catalogOverride?: CatalogModel[];
|
|
293
|
+
fetch?: typeof fetch;
|
|
294
|
+
logger?: Pick<Console, "debug" | "info" | "warn" | "error">;
|
|
295
|
+
}
|
|
296
|
+
interface CallContext {
|
|
297
|
+
/** "enterprise" picks keys.enterprise when every key for the provider is set, else falls back to pro. */
|
|
298
|
+
tier?: "pro" | "enterprise";
|
|
299
|
+
signal?: AbortSignal;
|
|
300
|
+
/** Seedance: input urls that are verified assets and may contain human faces. */
|
|
301
|
+
verifiedAssetUrls?: string[];
|
|
302
|
+
/** Space Builder: center-crop the image to the requested ratio after generation. */
|
|
303
|
+
postCropToAspect?: boolean;
|
|
304
|
+
onUnsupported?: "error" | "coerce";
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Provider body or the lifted detail (Gemini blockReason, fal body.detail). */
|
|
308
|
+
type ProviderErrorBody = string | JsonValue;
|
|
309
|
+
type ModelKitErrorCode = "unknown_model" | "unsupported_option" | "missing_input" | "content_blocked" | "provider_rejected" | "timeout" | "auth" | "network" | "rate_limited";
|
|
310
|
+
interface ModelKitErrorInit {
|
|
311
|
+
code: ModelKitErrorCode;
|
|
312
|
+
message: string;
|
|
313
|
+
provider?: string;
|
|
314
|
+
model?: string;
|
|
315
|
+
status?: number;
|
|
316
|
+
body?: ProviderErrorBody;
|
|
317
|
+
cause?: unknown;
|
|
318
|
+
retryable?: boolean;
|
|
319
|
+
}
|
|
320
|
+
declare class ModelKitError extends Error {
|
|
321
|
+
readonly code: ModelKitErrorCode;
|
|
322
|
+
readonly provider: string | undefined;
|
|
323
|
+
readonly model: string | undefined;
|
|
324
|
+
readonly status: number | undefined;
|
|
325
|
+
readonly body: ProviderErrorBody | undefined;
|
|
326
|
+
readonly retryable: boolean;
|
|
327
|
+
constructor(init: ModelKitErrorInit);
|
|
328
|
+
}
|
|
329
|
+
declare function isModelKitError(err: unknown): err is ModelKitError;
|
|
330
|
+
|
|
331
|
+
declare class Catalog {
|
|
332
|
+
private readonly byId;
|
|
333
|
+
private readonly byAlias;
|
|
334
|
+
constructor(models: CatalogModel[]);
|
|
335
|
+
add(model: CatalogModel): void;
|
|
336
|
+
/** Resolves a canonical id or any historical alias. */
|
|
337
|
+
resolveId(idOrAlias: string): string | undefined;
|
|
338
|
+
get(idOrAlias: string): CatalogModel | undefined;
|
|
339
|
+
require(idOrAlias: string): CatalogModel;
|
|
340
|
+
list(filter?: {
|
|
341
|
+
type?: ModelType;
|
|
342
|
+
lifecycle?: Lifecycle[];
|
|
343
|
+
}): CatalogModel[];
|
|
344
|
+
/** Merge rows by id. Used for catalogOverride. */
|
|
345
|
+
withOverrides(rows: CatalogModel[]): Catalog;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Seeded from Cowork, Space Builder and trigger-workflows. See catalog/*.ts. */
|
|
349
|
+
declare const MODELS: CatalogModel[];
|
|
350
|
+
|
|
351
|
+
type OnUnsupported = "error" | "coerce";
|
|
352
|
+
interface ResolveResult<T> {
|
|
353
|
+
request: T;
|
|
354
|
+
warnings: Warning[];
|
|
355
|
+
}
|
|
356
|
+
/** Nearest supported ratio in the same orientation bucket, else nearest overall, by log distance. */
|
|
357
|
+
declare function nearestAspectRatio(requested: AspectRatio, supported: AspectRatio[]): AspectRatio;
|
|
358
|
+
declare function resolveImageOptions(model: CatalogModel, request: ImageRequest, onUnsupported?: OnUnsupported): ResolveResult<ImageRequest>;
|
|
359
|
+
declare function resolveVideoOptions(model: CatalogModel, request: VideoRequest, onUnsupported?: OnUnsupported): ResolveResult<VideoRequest>;
|
|
360
|
+
|
|
361
|
+
/** Which key names an adapter needs. All of them must be set on a tier for that tier to win. */
|
|
362
|
+
declare const ADAPTER_KEYS: Record<AdapterId, ProviderKeyName[]>;
|
|
363
|
+
/**
|
|
364
|
+
* Enterprise wins only when every key the adapter needs is set on the
|
|
365
|
+
* enterprise tier; otherwise the pro keys are used. Mirrors trigger-workflows
|
|
366
|
+
* enterprise-keys.ts, extended to Ark and Prodia.
|
|
367
|
+
*/
|
|
368
|
+
declare function resolveKeys(config: ModelKitConfig, names: ProviderKeyName[], tier?: "pro" | "enterprise"): ProviderKeys;
|
|
369
|
+
/** Build a ProviderKeys map from env with the given prefix ("" for pro, "ENTERPRISE_" for enterprise). */
|
|
370
|
+
declare function keysFromEnv(env: NodeJS.ProcessEnv, prefix?: string): ProviderKeys;
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* One classifier for every adapter. Never retry safety blocks,
|
|
374
|
+
* content_policy_violation, or 4xx other than 429. Retry 429, 5xx, timeouts
|
|
375
|
+
* and network failures. Adapters must not stack their own retry loop on top.
|
|
376
|
+
*/
|
|
377
|
+
declare function classifyHttpError(status: number | undefined, body: ProviderErrorBody, provider: string, model?: string): ModelKitError;
|
|
378
|
+
declare function shouldRetry(err: Error): boolean;
|
|
379
|
+
interface RetryOptions {
|
|
380
|
+
attempts?: number;
|
|
381
|
+
baseDelayMs?: number;
|
|
382
|
+
maxDelayMs?: number;
|
|
383
|
+
factor?: number;
|
|
384
|
+
signal?: AbortSignal;
|
|
385
|
+
}
|
|
386
|
+
declare function withRetry<T>(fn: (attempt: number) => Promise<T>, opts?: RetryOptions): Promise<T>;
|
|
387
|
+
declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
|
|
388
|
+
/** Poll until `check` returns a value. Throws timeout after `timeoutMs`. */
|
|
389
|
+
declare function pollUntil<T>(check: () => Promise<T | undefined>, opts: {
|
|
390
|
+
intervalMs: number;
|
|
391
|
+
timeoutMs: number;
|
|
392
|
+
signal?: AbortSignal;
|
|
393
|
+
backoff?: number;
|
|
394
|
+
maxIntervalMs?: number;
|
|
395
|
+
}): Promise<T>;
|
|
396
|
+
|
|
397
|
+
/** Everything an adapter needs for one call. Resolved by the core before dispatch. */
|
|
398
|
+
interface AdapterContext {
|
|
399
|
+
model: CatalogModel;
|
|
400
|
+
keys: ProviderKeys;
|
|
401
|
+
routing: Routing;
|
|
402
|
+
fetch: typeof fetch;
|
|
403
|
+
logger: Pick<Console, "debug" | "info" | "warn" | "error">;
|
|
404
|
+
signal?: AbortSignal;
|
|
405
|
+
call: CallContext;
|
|
406
|
+
warnings: Warning[];
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* One adapter per provider API. The request the adapter receives has already
|
|
410
|
+
* passed resolveOptions: every option is inside the catalog constraints and the
|
|
411
|
+
* catalog defaults are filled in.
|
|
412
|
+
*/
|
|
413
|
+
interface Adapter {
|
|
414
|
+
id: string;
|
|
415
|
+
generateImage?(req: ImageRequest, ctx: AdapterContext): Promise<ImageResult>;
|
|
416
|
+
generateVideo?(req: VideoRequest, ctx: AdapterContext): Promise<VideoJob>;
|
|
417
|
+
generateAudio?(req: AudioRequest, ctx: AdapterContext): Promise<AudioResult>;
|
|
418
|
+
/**
|
|
419
|
+
* Turn a media reference into an AI SDK file part the provider's language
|
|
420
|
+
* model accepts. Google uploads large videos through the Files API.
|
|
421
|
+
*/
|
|
422
|
+
prepareMediaPart?(ref: MediaRef, mediaType: string, ctx: AdapterContext): Promise<MediaPart>;
|
|
423
|
+
/** Text: return an AI SDK LanguageModel for streamText / generateText. */
|
|
424
|
+
languageModel?(ctx: AdapterContext): LanguageModelV3;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* The AI SDK `LanguageModel` type. A string is a catalog id or alias; a provider
|
|
429
|
+
* instance (`openai("gpt-4o")`) runs through the SDK unchanged, with usage
|
|
430
|
+
* attributed to its own provider and model id.
|
|
431
|
+
*/
|
|
432
|
+
type CatalogTextModel = LanguageModel;
|
|
433
|
+
/** A provider instance the caller built; anything that is not a catalog id string. */
|
|
434
|
+
type TextModelInstance = LanguageModelV3 | LanguageModelV2;
|
|
435
|
+
type WithCatalogModel<T> = {
|
|
436
|
+
[K in keyof T]: K extends "model" ? CatalogTextModel : T[K];
|
|
437
|
+
};
|
|
438
|
+
type DistributiveOmit<T, K extends PropertyKey> = T extends T ? Omit<T, K> : never;
|
|
439
|
+
type ObjectMode<SCHEMA extends FlexibleSchema, OUTPUT extends ObjectOutput, RESULT> = (OUTPUT extends "enum" ? {
|
|
440
|
+
enum: Array<RESULT>;
|
|
441
|
+
output: "enum";
|
|
442
|
+
} : OUTPUT extends "no-schema" ? {} : {
|
|
443
|
+
schema: SCHEMA;
|
|
444
|
+
schemaName?: string;
|
|
445
|
+
schemaDescription?: string;
|
|
446
|
+
}) & {
|
|
447
|
+
output?: OUTPUT;
|
|
448
|
+
model: CatalogTextModel;
|
|
449
|
+
};
|
|
450
|
+
type ObjectOutput = "object" | "array" | "enum" | "no-schema";
|
|
451
|
+
type DefaultObjectOutput<S extends FlexibleSchema> = InferSchema<S> extends string ? "enum" : "object";
|
|
452
|
+
type ObjectResult<S extends FlexibleSchema, O extends ObjectOutput> = O extends "array" ? Array<InferSchema<S>> : InferSchema<S>;
|
|
453
|
+
type ModelkitGenerateTextOptions<TOOLS extends ToolSet, OUTPUT extends Output.Output = Output.Output<string, string>> = WithCatalogModel<Parameters<typeof generateText$1<TOOLS, OUTPUT>>[0]>;
|
|
454
|
+
type ModelkitStreamTextOptions<TOOLS extends ToolSet, OUTPUT extends Output.Output = Output.Output<string, string, never>> = WithCatalogModel<Parameters<typeof streamText$1<TOOLS, OUTPUT>>[0]>;
|
|
455
|
+
type ModelkitGenerateObjectOptions<SCHEMA extends FlexibleSchema = FlexibleSchema<JSONValue>, OUTPUT extends ObjectOutput = DefaultObjectOutput<SCHEMA>, RESULT = ObjectResult<SCHEMA, OUTPUT>> = DistributiveOmit<Parameters<typeof generateObject$1<SCHEMA, "no-schema", RESULT>>[0], "model" | "output"> & ObjectMode<SCHEMA, OUTPUT, RESULT>;
|
|
456
|
+
type ModelkitStreamObjectOptions<SCHEMA extends FlexibleSchema = FlexibleSchema<JSONValue>, OUTPUT extends ObjectOutput = DefaultObjectOutput<SCHEMA>, RESULT = ObjectResult<SCHEMA, OUTPUT>> = DistributiveOmit<Parameters<typeof streamObject$1<SCHEMA, "no-schema", RESULT>>[0], "model" | "output"> & ObjectMode<SCHEMA, OUTPUT, RESULT>;
|
|
457
|
+
interface TextMethods {
|
|
458
|
+
generateText<TOOLS extends ToolSet, OUTPUT extends Output.Output = Output.Output<string, string>>(options: ModelkitGenerateTextOptions<TOOLS, OUTPUT>, call?: CallContext): ReturnType<typeof generateText$1<TOOLS, OUTPUT>>;
|
|
459
|
+
streamText<TOOLS extends ToolSet, OUTPUT extends Output.Output = Output.Output<string, string, never>>(options: ModelkitStreamTextOptions<TOOLS, OUTPUT>, call?: CallContext): ReturnType<typeof streamText$1<TOOLS, OUTPUT>>;
|
|
460
|
+
generateObject<SCHEMA extends FlexibleSchema = FlexibleSchema<JSONValue>, OUTPUT extends ObjectOutput = DefaultObjectOutput<SCHEMA>, RESULT = ObjectResult<SCHEMA, OUTPUT>>(options: ModelkitGenerateObjectOptions<SCHEMA, OUTPUT, RESULT>, call?: CallContext): ReturnType<typeof generateObject$1<SCHEMA, OUTPUT, RESULT>>;
|
|
461
|
+
streamObject<SCHEMA extends FlexibleSchema = FlexibleSchema<JSONValue>, OUTPUT extends ObjectOutput = DefaultObjectOutput<SCHEMA>, RESULT = ObjectResult<SCHEMA, OUTPUT>>(options: ModelkitStreamObjectOptions<SCHEMA, OUTPUT, RESULT>, call?: CallContext): ReturnType<typeof streamObject$1<SCHEMA, OUTPUT, RESULT>>;
|
|
462
|
+
}
|
|
463
|
+
type AiTextProviderOptions = NonNullable<Parameters<typeof generateText$1>[0]["providerOptions"]>;
|
|
464
|
+
/**
|
|
465
|
+
* Merge catalog providerOptions under caller providerOptions. The catalog
|
|
466
|
+
* `modelkit` routing hint never reaches the provider. Per-provider objects
|
|
467
|
+
* merge shallowly so a caller can override one key without dropping the rest.
|
|
468
|
+
*/
|
|
469
|
+
declare function mergeTextProviderOptions(catalog: CatalogModel, caller: AiTextProviderOptions | undefined): AiTextProviderOptions;
|
|
470
|
+
/**
|
|
471
|
+
* A catalog row standing in for a provider instance the caller passed as `model`.
|
|
472
|
+
* It has no aliases, defaults or providerOptions; usage carries the SDK model id.
|
|
473
|
+
*/
|
|
474
|
+
declare function passthroughTextModel(model: TextModelInstance): CatalogModel;
|
|
475
|
+
/** Canonical token usage for a completed text call. Quantity is total tokens. */
|
|
476
|
+
declare function textUsageEvent(model: CatalogModel, provider: string, usage: LanguageModelUsage): UsageEvent;
|
|
477
|
+
|
|
478
|
+
interface ModelKit extends TextMethods {
|
|
479
|
+
catalog: Catalog;
|
|
480
|
+
/**
|
|
481
|
+
* Generate one or more images. Blocks until the provider returns bytes.
|
|
482
|
+
* `model` is a catalog id or an AI SDK image model instance.
|
|
483
|
+
*/
|
|
484
|
+
generateImage(req: ImageCallOptions, call?: CallContext): Promise<ImageCallResult>;
|
|
485
|
+
/**
|
|
486
|
+
* Submit a video job. Await `job.result()` for the final asset.
|
|
487
|
+
* `model` is a catalog id or an AI SDK video model instance.
|
|
488
|
+
*/
|
|
489
|
+
generateVideo(req: VideoCallOptions, call?: CallContext): Promise<VideoCallJob>;
|
|
490
|
+
/** AI SDK LanguageModel for chat models. Pair with the catalog's providerOptions. */
|
|
491
|
+
languageModel(modelId: string, call?: CallContext): LanguageModelHandle;
|
|
492
|
+
/** Generate music or a soundtrack. Blocks until the provider returns audio. */
|
|
493
|
+
generateAudio(req: AudioRequest, call?: CallContext): Promise<AudioResult>;
|
|
494
|
+
/**
|
|
495
|
+
* Prepare a video or document for a language model call (video understanding).
|
|
496
|
+
* Uses the provider file store when the payload is too large to inline.
|
|
497
|
+
*/
|
|
498
|
+
prepareMediaPart(modelId: string, ref: MediaRef, mediaType: string, call?: CallContext): Promise<MediaPart>;
|
|
499
|
+
/** Which options a caller may show for this model. */
|
|
500
|
+
describe(modelId: string): CatalogModel;
|
|
501
|
+
}
|
|
502
|
+
interface LanguageModelHandle {
|
|
503
|
+
model: LanguageModelV3;
|
|
504
|
+
providerOptions: ProviderOptions;
|
|
505
|
+
catalog: CatalogModel;
|
|
506
|
+
}
|
|
507
|
+
type AdapterRegistry = Partial<Record<AdapterId, Adapter>>;
|
|
508
|
+
declare function createModelKit(config: ModelKitConfig, adapters: AdapterRegistry): ModelKit;
|
|
509
|
+
|
|
510
|
+
declare const aiSdkTextAdapter: Adapter;
|
|
511
|
+
|
|
512
|
+
declare const arkAdapter: Adapter;
|
|
513
|
+
|
|
514
|
+
declare const falAdapter: Adapter;
|
|
515
|
+
|
|
516
|
+
declare const googleGenaiAdapter: Adapter;
|
|
517
|
+
|
|
518
|
+
declare const googleImageAdapter: Adapter;
|
|
519
|
+
|
|
520
|
+
declare const higgsfieldAdapter: Adapter;
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Vibe's aspect_ratio + image_size tier to a WIDTHxHEIGHT string for
|
|
524
|
+
* gpt-image-2. All tiers honor the true aspect ratio; the tier only sets the
|
|
525
|
+
* target long edge (1K->1024, 2K->2048, 4K->3840). gpt-image-2 hard constraints
|
|
526
|
+
* (see OpenAI docs - "Size and quality options"):
|
|
527
|
+
* - both edges divisible by 16
|
|
528
|
+
* - max edge <= 3840
|
|
529
|
+
* - long:short ratio <= 3:1 (all mapped ratios already satisfy this)
|
|
530
|
+
* - total pixels between 655,360 and 8,294,400
|
|
531
|
+
* We scale UP to clear the pixel floor (a literal 16:9 @ 1024 long edge is only
|
|
532
|
+
* 589,824 px, below the minimum) and DOWN to clear the ceiling.
|
|
533
|
+
*/
|
|
534
|
+
declare function aspectRatioToSize(ratio: string, tier: ImageSize): string;
|
|
535
|
+
declare const openaiImageAdapter: Adapter;
|
|
536
|
+
|
|
537
|
+
declare const prodiaAdapter: Adapter;
|
|
538
|
+
|
|
539
|
+
declare const replicateAdapter: Adapter;
|
|
540
|
+
|
|
541
|
+
declare const xaiVideoAdapter: Adapter;
|
|
542
|
+
|
|
543
|
+
declare const DEFAULT_ADAPTERS: AdapterRegistry;
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* All adapters registered. Keys default to process.env (pro = bare names,
|
|
547
|
+
* enterprise = ENTERPRISE_ prefix) when `keys` is omitted.
|
|
548
|
+
*/
|
|
549
|
+
declare function createDefaultModelKit(config?: Partial<ModelKitConfig>): ModelKit;
|
|
550
|
+
/**
|
|
551
|
+
* Environment-backed calls with the `ai` function names and shapes. Use a configured
|
|
552
|
+
* kit for routing, tier keys, or a usage sink.
|
|
553
|
+
*/
|
|
554
|
+
declare const generateText: ModelKit["generateText"];
|
|
555
|
+
declare const streamText: ModelKit["streamText"];
|
|
556
|
+
declare const generateObject: ModelKit["generateObject"];
|
|
557
|
+
declare const streamObject: ModelKit["streamObject"];
|
|
558
|
+
declare const generateImage: ModelKit["generateImage"];
|
|
559
|
+
declare const generateVideo: ModelKit["generateVideo"];
|
|
560
|
+
declare const generateAudio: ModelKit["generateAudio"];
|
|
561
|
+
declare const prepareMediaPart: ModelKit["prepareMediaPart"];
|
|
562
|
+
declare const languageModel: ModelKit["languageModel"];
|
|
563
|
+
declare const describe: ModelKit["describe"];
|
|
564
|
+
|
|
565
|
+
/** Decodes any provider payload into JsonValue at the I/O boundary. Non-JSON input becomes null. */
|
|
566
|
+
declare const JsonValueSchema: z.ZodType<JsonValue>;
|
|
567
|
+
declare const JsonObjectSchema: z.ZodType<JsonObject>;
|
|
568
|
+
/** Body text of a failed response, decoded to JSON when possible. */
|
|
569
|
+
declare function readErrorBody(res: Response): Promise<JsonValue>;
|
|
570
|
+
interface ThrownErrorInfo {
|
|
571
|
+
message: string;
|
|
572
|
+
status: number | undefined;
|
|
573
|
+
body: JsonValue;
|
|
574
|
+
}
|
|
575
|
+
declare function describeThrown(cause: unknown): ThrownErrorInfo;
|
|
576
|
+
|
|
577
|
+
/** Add the AI SDK `GeneratedFile` accessors to adapter image bytes. base64 is computed on first read. */
|
|
578
|
+
declare function toGeneratedImage(image: ImageBytes): GeneratedImage;
|
|
579
|
+
|
|
580
|
+
export { ADAPTER_KEYS, type Adapter, type AdapterContext, type AdapterId, type AdapterRegistry, type AiTextProviderOptions, type AspectRatio, type AudioRequest, type AudioResult, type CallContext, Catalog, type CatalogModel, type CatalogTextModel, DEFAULT_ADAPTERS, type DefaultObjectOutput, type GeneratedImage, type ImageBytes, type ImageCallOptions, type ImageCallResult, type ImageQuality, type ImageRequest, type ImageResult, type ImageSize, type JsonObject, JsonObjectSchema, type JsonPrimitive, type JsonValue, JsonValueSchema, type LanguageModelHandle, type Lifecycle, MODELS, type MediaPart, type MediaRef, type ModelConstraints, type ModelKit, type ModelKitConfig, ModelKitError, type ModelKitErrorCode, type ModelKitErrorInit, type ModelType, type ModelkitGenerateObjectOptions, type ModelkitGenerateTextOptions, type ModelkitStreamObjectOptions, type ModelkitStreamTextOptions, type ObjectOutput, type ObjectResult, type OnUnsupported, type OutputFormat, type PricingKind, type ProviderErrorBody, type ProviderKeyName, type ProviderKeys, type ProviderOptionValue, type ProviderOptions, type ReferenceLimits, type RequiredInput, type ResolveResult, type Routing, type SupportedOption, type TextMethods, type TextModelInstance, type UsageEvent, type UsageModality, type UsageUnit, type VideoCallJob, type VideoCallOptions, type VideoJob, type VideoModel, type VideoRequest, type VideoResolution, type VideoResult, type Warning, aiSdkTextAdapter, arkAdapter, aspectRatioToSize, classifyHttpError, createDefaultModelKit, createModelKit, describe, describeThrown, generateImage as experimental_generateImage, falAdapter, generateAudio, generateImage, generateObject, generateText, generateVideo, googleGenaiAdapter, googleImageAdapter, higgsfieldAdapter, isModelKitError, keysFromEnv, languageModel, mergeTextProviderOptions, nearestAspectRatio, openaiImageAdapter, passthroughTextModel, pollUntil, prepareMediaPart, prodiaAdapter, readErrorBody, replicateAdapter, resolveImageOptions, resolveKeys, resolveVideoOptions, shouldRetry, sleep, streamObject, streamText, textUsageEvent, toGeneratedImage, withRetry, xaiVideoAdapter };
|