@ai-sdk/prodia 1.0.5 → 1.0.7

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/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @ai-sdk/prodia
2
2
 
3
+ ## 1.0.7
4
+
5
+ ### Patch Changes
6
+
7
+ - 4de5a1d: chore: excluded tests from src folder in npm package
8
+ - Updated dependencies [4de5a1d]
9
+ - @ai-sdk/provider@3.0.5
10
+ - @ai-sdk/provider-utils@4.0.9
11
+
12
+ ## 1.0.6
13
+
14
+ ### Patch Changes
15
+
16
+ - 8dc54db: chore: add src folders to package bundle
17
+
3
18
  ## 1.0.5
4
19
 
5
20
  ### Patch Changes
package/dist/index.js CHANGED
@@ -376,7 +376,7 @@ var prodiaFailedResponseHandler = (0, import_provider_utils.createJsonErrorRespo
376
376
  });
377
377
 
378
378
  // src/version.ts
379
- var VERSION = true ? "1.0.5" : "0.0.0-test";
379
+ var VERSION = true ? "1.0.7" : "0.0.0-test";
380
380
 
381
381
  // src/prodia-provider.ts
382
382
  var defaultBaseURL = "https://inference.prodia.com/v2";
package/dist/index.mjs CHANGED
@@ -362,7 +362,7 @@ var prodiaFailedResponseHandler = createJsonErrorResponseHandler({
362
362
  });
363
363
 
364
364
  // src/version.ts
365
- var VERSION = true ? "1.0.5" : "0.0.0-test";
365
+ var VERSION = true ? "1.0.7" : "0.0.0-test";
366
366
 
367
367
  // src/prodia-provider.ts
368
368
  var defaultBaseURL = "https://inference.prodia.com/v2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/prodia",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -8,6 +8,11 @@
8
8
  "types": "./dist/index.d.ts",
9
9
  "files": [
10
10
  "dist/**/*",
11
+ "src",
12
+ "!src/**/*.test.ts",
13
+ "!src/**/*.test-d.ts",
14
+ "!src/**/__snapshots__",
15
+ "!src/**/__fixtures__",
11
16
  "CHANGELOG.md"
12
17
  ],
13
18
  "exports": {
@@ -19,15 +24,15 @@
19
24
  }
20
25
  },
21
26
  "dependencies": {
22
- "@ai-sdk/provider": "3.0.4",
23
- "@ai-sdk/provider-utils": "4.0.8"
27
+ "@ai-sdk/provider": "3.0.5",
28
+ "@ai-sdk/provider-utils": "4.0.9"
24
29
  },
25
30
  "devDependencies": {
26
31
  "@types/node": "20.17.24",
27
32
  "tsup": "^8",
28
33
  "typescript": "5.8.3",
29
34
  "zod": "3.25.76",
30
- "@ai-sdk/test-server": "1.0.1",
35
+ "@ai-sdk/test-server": "1.0.3",
31
36
  "@vercel/ai-tsconfig": "0.0.0"
32
37
  },
33
38
  "peerDependencies": {
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export type { ProdiaImageProviderOptions } from './prodia-image-model';
2
+ export type { ProdiaImageModelId } from './prodia-image-settings';
3
+ export type { ProdiaProvider, ProdiaProviderSettings } from './prodia-provider';
4
+ export { createProdia, prodia } from './prodia-provider';
5
+ export { VERSION } from './version';
@@ -0,0 +1,450 @@
1
+ import type { ImageModelV3, SharedV3Warning } from '@ai-sdk/provider';
2
+ import type { InferSchema, Resolvable } from '@ai-sdk/provider-utils';
3
+ import {
4
+ combineHeaders,
5
+ createJsonErrorResponseHandler,
6
+ type FetchFunction,
7
+ lazySchema,
8
+ parseProviderOptions,
9
+ postToApi,
10
+ resolve,
11
+ zodSchema,
12
+ } from '@ai-sdk/provider-utils';
13
+ import { z } from 'zod/v4';
14
+ import type { ProdiaImageModelId } from './prodia-image-settings';
15
+
16
+ export class ProdiaImageModel implements ImageModelV3 {
17
+ readonly specificationVersion = 'v3';
18
+ readonly maxImagesPerCall = 1;
19
+
20
+ get provider(): string {
21
+ return this.config.provider;
22
+ }
23
+
24
+ constructor(
25
+ readonly modelId: ProdiaImageModelId,
26
+ private readonly config: ProdiaImageModelConfig,
27
+ ) {}
28
+
29
+ private async getArgs({
30
+ prompt,
31
+ size,
32
+ seed,
33
+ providerOptions,
34
+ }: Parameters<ImageModelV3['doGenerate']>[0]) {
35
+ const warnings: Array<SharedV3Warning> = [];
36
+
37
+ const prodiaOptions = await parseProviderOptions({
38
+ provider: 'prodia',
39
+ providerOptions,
40
+ schema: prodiaImageProviderOptionsSchema,
41
+ });
42
+
43
+ let width: number | undefined;
44
+ let height: number | undefined;
45
+ if (size) {
46
+ const [widthStr, heightStr] = size.split('x');
47
+ width = Number(widthStr);
48
+ height = Number(heightStr);
49
+ if (
50
+ !widthStr ||
51
+ !heightStr ||
52
+ !Number.isFinite(width) ||
53
+ !Number.isFinite(height)
54
+ ) {
55
+ warnings.push({
56
+ type: 'unsupported',
57
+ feature: 'size',
58
+ details: `Invalid size format: ${size}. Expected format: WIDTHxHEIGHT (e.g., 1024x1024)`,
59
+ });
60
+ width = undefined;
61
+ height = undefined;
62
+ }
63
+ }
64
+
65
+ const jobConfig: Record<string, unknown> = {
66
+ prompt,
67
+ };
68
+
69
+ if (prodiaOptions?.width !== undefined) {
70
+ jobConfig.width = prodiaOptions.width;
71
+ } else if (width !== undefined) {
72
+ jobConfig.width = width;
73
+ }
74
+
75
+ if (prodiaOptions?.height !== undefined) {
76
+ jobConfig.height = prodiaOptions.height;
77
+ } else if (height !== undefined) {
78
+ jobConfig.height = height;
79
+ }
80
+
81
+ if (seed !== undefined) {
82
+ jobConfig.seed = seed;
83
+ }
84
+ if (prodiaOptions?.steps !== undefined) {
85
+ jobConfig.steps = prodiaOptions.steps;
86
+ }
87
+ if (prodiaOptions?.stylePreset !== undefined) {
88
+ jobConfig.style_preset = prodiaOptions.stylePreset;
89
+ }
90
+ if (prodiaOptions?.loras !== undefined && prodiaOptions.loras.length > 0) {
91
+ jobConfig.loras = prodiaOptions.loras;
92
+ }
93
+ if (prodiaOptions?.progressive !== undefined) {
94
+ jobConfig.progressive = prodiaOptions.progressive;
95
+ }
96
+
97
+ const body = {
98
+ type: this.modelId,
99
+ config: jobConfig,
100
+ };
101
+
102
+ return { body, warnings };
103
+ }
104
+
105
+ async doGenerate(
106
+ options: Parameters<ImageModelV3['doGenerate']>[0],
107
+ ): Promise<Awaited<ReturnType<ImageModelV3['doGenerate']>>> {
108
+ const { body, warnings } = await this.getArgs(options);
109
+
110
+ const currentDate = this.config._internal?.currentDate?.() ?? new Date();
111
+ const combinedHeaders = combineHeaders(
112
+ await resolve(this.config.headers),
113
+ options.headers,
114
+ );
115
+
116
+ const { value: multipartResult, responseHeaders } = await postToApi({
117
+ url: `${this.config.baseURL}/job`,
118
+ headers: {
119
+ ...combinedHeaders,
120
+ Accept: 'multipart/form-data; image/png',
121
+ 'Content-Type': 'application/json',
122
+ },
123
+ body: {
124
+ content: JSON.stringify(body),
125
+ values: body,
126
+ },
127
+ failedResponseHandler: prodiaFailedResponseHandler,
128
+ successfulResponseHandler: createMultipartResponseHandler(),
129
+ abortSignal: options.abortSignal,
130
+ fetch: this.config.fetch,
131
+ });
132
+
133
+ const { jobResult, imageBytes } = multipartResult;
134
+
135
+ return {
136
+ images: [imageBytes],
137
+ warnings,
138
+ providerMetadata: {
139
+ prodia: {
140
+ images: [
141
+ {
142
+ jobId: jobResult.id,
143
+ ...(jobResult.config?.seed != null && {
144
+ seed: jobResult.config.seed,
145
+ }),
146
+ ...(jobResult.metrics?.elapsed != null && {
147
+ elapsed: jobResult.metrics.elapsed,
148
+ }),
149
+ ...(jobResult.metrics?.ips != null && {
150
+ iterationsPerSecond: jobResult.metrics.ips,
151
+ }),
152
+ ...(jobResult.created_at != null && {
153
+ createdAt: jobResult.created_at,
154
+ }),
155
+ ...(jobResult.updated_at != null && {
156
+ updatedAt: jobResult.updated_at,
157
+ }),
158
+ },
159
+ ],
160
+ },
161
+ },
162
+ response: {
163
+ modelId: this.modelId,
164
+ timestamp: currentDate,
165
+ headers: responseHeaders,
166
+ },
167
+ };
168
+ }
169
+ }
170
+
171
+ const stylePresets = [
172
+ '3d-model',
173
+ 'analog-film',
174
+ 'anime',
175
+ 'cinematic',
176
+ 'comic-book',
177
+ 'digital-art',
178
+ 'enhance',
179
+ 'fantasy-art',
180
+ 'isometric',
181
+ 'line-art',
182
+ 'low-poly',
183
+ 'neon-punk',
184
+ 'origami',
185
+ 'photographic',
186
+ 'pixel-art',
187
+ 'texture',
188
+ 'craft-clay',
189
+ ] as const;
190
+
191
+ export const prodiaImageProviderOptionsSchema = lazySchema(() =>
192
+ zodSchema(
193
+ z.object({
194
+ /**
195
+ * Amount of computational iterations to run. More is typically higher quality.
196
+ */
197
+ steps: z.number().int().min(1).max(4).optional(),
198
+ /**
199
+ * Width of the output image in pixels.
200
+ */
201
+ width: z.number().int().min(256).max(1920).optional(),
202
+ /**
203
+ * Height of the output image in pixels.
204
+ */
205
+ height: z.number().int().min(256).max(1920).optional(),
206
+ /**
207
+ * Apply a visual theme to your output image.
208
+ */
209
+ stylePreset: z.enum(stylePresets).optional(),
210
+ /**
211
+ * Augment the output with a LoRa model.
212
+ */
213
+ loras: z.array(z.string()).max(3).optional(),
214
+ /**
215
+ * When using JPEG output, return a progressive JPEG.
216
+ */
217
+ progressive: z.boolean().optional(),
218
+ }),
219
+ ),
220
+ );
221
+
222
+ export type ProdiaImageProviderOptions = InferSchema<
223
+ typeof prodiaImageProviderOptionsSchema
224
+ >;
225
+
226
+ interface ProdiaImageModelConfig {
227
+ provider: string;
228
+ baseURL: string;
229
+ headers?: Resolvable<Record<string, string | undefined>>;
230
+ fetch?: FetchFunction;
231
+ _internal?: {
232
+ currentDate?: () => Date;
233
+ };
234
+ }
235
+
236
+ const prodiaJobResultSchema = z.object({
237
+ id: z.string(),
238
+ created_at: z.string().optional(),
239
+ updated_at: z.string().optional(),
240
+ expires_at: z.string().optional(),
241
+ state: z
242
+ .object({
243
+ current: z.string(),
244
+ })
245
+ .optional(),
246
+ config: z
247
+ .object({
248
+ seed: z.number().optional(),
249
+ })
250
+ .passthrough()
251
+ .optional(),
252
+ metrics: z
253
+ .object({
254
+ elapsed: z.number().optional(),
255
+ ips: z.number().optional(),
256
+ })
257
+ .optional(),
258
+ });
259
+
260
+ type ProdiaJobResult = z.infer<typeof prodiaJobResultSchema>;
261
+
262
+ interface MultipartResult {
263
+ jobResult: ProdiaJobResult;
264
+ imageBytes: Uint8Array;
265
+ }
266
+
267
+ function createMultipartResponseHandler() {
268
+ return async ({
269
+ response,
270
+ }: {
271
+ response: Response;
272
+ }): Promise<{
273
+ value: MultipartResult;
274
+ responseHeaders: Record<string, string>;
275
+ }> => {
276
+ const contentType = response.headers.get('content-type') ?? '';
277
+ const responseHeaders: Record<string, string> = {};
278
+ response.headers.forEach((value, key) => {
279
+ responseHeaders[key] = value;
280
+ });
281
+
282
+ const boundaryMatch = contentType.match(/boundary=([^\s;]+)/);
283
+ if (!boundaryMatch) {
284
+ throw new Error(
285
+ `Prodia response missing multipart boundary in content-type: ${contentType}`,
286
+ );
287
+ }
288
+ const boundary = boundaryMatch[1];
289
+
290
+ const arrayBuffer = await response.arrayBuffer();
291
+ const bytes = new Uint8Array(arrayBuffer);
292
+
293
+ const parts = parseMultipart(bytes, boundary);
294
+
295
+ let jobResult: ProdiaJobResult | undefined;
296
+ let imageBytes: Uint8Array | undefined;
297
+
298
+ for (const part of parts) {
299
+ const contentDisposition = part.headers['content-disposition'] ?? '';
300
+ const partContentType = part.headers['content-type'] ?? '';
301
+
302
+ if (contentDisposition.includes('name="job"')) {
303
+ const jsonStr = new TextDecoder().decode(part.body);
304
+ jobResult = prodiaJobResultSchema.parse(JSON.parse(jsonStr));
305
+ } else if (contentDisposition.includes('name="output"')) {
306
+ imageBytes = part.body;
307
+ } else if (partContentType.startsWith('image/')) {
308
+ imageBytes = part.body;
309
+ }
310
+ }
311
+
312
+ if (!jobResult) {
313
+ throw new Error('Prodia multipart response missing job part');
314
+ }
315
+ if (!imageBytes) {
316
+ throw new Error('Prodia multipart response missing output image');
317
+ }
318
+
319
+ return {
320
+ value: { jobResult, imageBytes },
321
+ responseHeaders,
322
+ };
323
+ };
324
+ }
325
+
326
+ interface MultipartPart {
327
+ headers: Record<string, string>;
328
+ body: Uint8Array;
329
+ }
330
+
331
+ function parseMultipart(data: Uint8Array, boundary: string): MultipartPart[] {
332
+ const parts: MultipartPart[] = [];
333
+ const boundaryBytes = new TextEncoder().encode(`--${boundary}`);
334
+ const endBoundaryBytes = new TextEncoder().encode(`--${boundary}--`);
335
+
336
+ const positions: number[] = [];
337
+ for (let i = 0; i <= data.length - boundaryBytes.length; i++) {
338
+ let match = true;
339
+ for (let j = 0; j < boundaryBytes.length; j++) {
340
+ if (data[i + j] !== boundaryBytes[j]) {
341
+ match = false;
342
+ break;
343
+ }
344
+ }
345
+ if (match) {
346
+ positions.push(i);
347
+ }
348
+ }
349
+
350
+ for (let i = 0; i < positions.length - 1; i++) {
351
+ const start = positions[i] + boundaryBytes.length;
352
+ const end = positions[i + 1];
353
+
354
+ let isEndBoundary = true;
355
+ for (let j = 0; j < endBoundaryBytes.length && isEndBoundary; j++) {
356
+ if (data[positions[i] + j] !== endBoundaryBytes[j]) {
357
+ isEndBoundary = false;
358
+ }
359
+ }
360
+ if (
361
+ isEndBoundary &&
362
+ positions[i] + endBoundaryBytes.length <= data.length
363
+ ) {
364
+ continue;
365
+ }
366
+
367
+ let partStart = start;
368
+ if (data[partStart] === 0x0d && data[partStart + 1] === 0x0a) {
369
+ partStart += 2;
370
+ } else if (data[partStart] === 0x0a) {
371
+ partStart += 1;
372
+ }
373
+
374
+ let partEnd = end;
375
+ if (data[partEnd - 2] === 0x0d && data[partEnd - 1] === 0x0a) {
376
+ partEnd -= 2;
377
+ } else if (data[partEnd - 1] === 0x0a) {
378
+ partEnd -= 1;
379
+ }
380
+
381
+ const partData = data.slice(partStart, partEnd);
382
+
383
+ let headerEnd = -1;
384
+ for (let j = 0; j < partData.length - 3; j++) {
385
+ if (
386
+ partData[j] === 0x0d &&
387
+ partData[j + 1] === 0x0a &&
388
+ partData[j + 2] === 0x0d &&
389
+ partData[j + 3] === 0x0a
390
+ ) {
391
+ headerEnd = j;
392
+ break;
393
+ }
394
+ if (partData[j] === 0x0a && partData[j + 1] === 0x0a) {
395
+ headerEnd = j;
396
+ break;
397
+ }
398
+ }
399
+
400
+ if (headerEnd === -1) {
401
+ continue;
402
+ }
403
+
404
+ const headerBytes = partData.slice(0, headerEnd);
405
+ const headerStr = new TextDecoder().decode(headerBytes);
406
+ const headers: Record<string, string> = {};
407
+ for (const line of headerStr.split(/\r?\n/)) {
408
+ const colonIdx = line.indexOf(':');
409
+ if (colonIdx > 0) {
410
+ const key = line.slice(0, colonIdx).trim().toLowerCase();
411
+ const value = line.slice(colonIdx + 1).trim();
412
+ headers[key] = value;
413
+ }
414
+ }
415
+
416
+ let bodyStart = headerEnd + 2;
417
+ if (partData[headerEnd] === 0x0d) {
418
+ bodyStart = headerEnd + 4;
419
+ }
420
+ const body = partData.slice(bodyStart);
421
+
422
+ parts.push({ headers, body });
423
+ }
424
+
425
+ return parts;
426
+ }
427
+
428
+ const prodiaErrorSchema = z.object({
429
+ message: z.string().optional(),
430
+ detail: z.unknown().optional(),
431
+ error: z.string().optional(),
432
+ });
433
+
434
+ const prodiaFailedResponseHandler = createJsonErrorResponseHandler({
435
+ errorSchema: prodiaErrorSchema,
436
+ errorToMessage: error => {
437
+ const parsed = prodiaErrorSchema.safeParse(error);
438
+ if (!parsed.success) return 'Unknown Prodia error';
439
+ const { message, detail, error: errorField } = parsed.data;
440
+ if (typeof detail === 'string') return detail;
441
+ if (detail != null) {
442
+ try {
443
+ return JSON.stringify(detail);
444
+ } catch {
445
+ // ignore
446
+ }
447
+ }
448
+ return errorField ?? message ?? 'Unknown Prodia error';
449
+ },
450
+ });
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Prodia job types for image generation.
3
+ */
4
+ export type ProdiaImageModelId =
5
+ | 'inference.flux-fast.schnell.txt2img.v2'
6
+ | 'inference.flux.schnell.txt2img.v2'
7
+ | (string & {});
@@ -0,0 +1,107 @@
1
+ import {
2
+ type ImageModelV3,
3
+ NoSuchModelError,
4
+ type ProviderV3,
5
+ } from '@ai-sdk/provider';
6
+ import type { FetchFunction } from '@ai-sdk/provider-utils';
7
+ import {
8
+ loadApiKey,
9
+ withoutTrailingSlash,
10
+ withUserAgentSuffix,
11
+ } from '@ai-sdk/provider-utils';
12
+ import { ProdiaImageModel } from './prodia-image-model';
13
+ import type { ProdiaImageModelId } from './prodia-image-settings';
14
+ import { VERSION } from './version';
15
+
16
+ export interface ProdiaProviderSettings {
17
+ /**
18
+ * Prodia API key. Default value is taken from the `PRODIA_TOKEN` environment variable.
19
+ */
20
+ apiKey?: string;
21
+
22
+ /**
23
+ * Base URL for the API calls. Defaults to `https://inference.prodia.com/v2`.
24
+ */
25
+ baseURL?: string;
26
+
27
+ /**
28
+ * Custom headers to include in the requests.
29
+ */
30
+ headers?: Record<string, string>;
31
+
32
+ /**
33
+ * Custom fetch implementation. You can use it as a middleware to intercept
34
+ * requests, or to provide a custom fetch implementation for e.g. testing.
35
+ */
36
+ fetch?: FetchFunction;
37
+ }
38
+
39
+ export interface ProdiaProvider extends ProviderV3 {
40
+ /**
41
+ * Creates a model for image generation.
42
+ */
43
+ image(modelId: ProdiaImageModelId): ImageModelV3;
44
+
45
+ /**
46
+ * Creates a model for image generation.
47
+ */
48
+ imageModel(modelId: ProdiaImageModelId): ImageModelV3;
49
+
50
+ /**
51
+ * @deprecated Use `embeddingModel` instead.
52
+ */
53
+ textEmbeddingModel(modelId: string): never;
54
+ }
55
+
56
+ const defaultBaseURL = 'https://inference.prodia.com/v2';
57
+
58
+ export function createProdia(
59
+ options: ProdiaProviderSettings = {},
60
+ ): ProdiaProvider {
61
+ const baseURL = withoutTrailingSlash(options.baseURL ?? defaultBaseURL);
62
+ const getHeaders = () =>
63
+ withUserAgentSuffix(
64
+ {
65
+ Authorization: `Bearer ${loadApiKey({
66
+ apiKey: options.apiKey,
67
+ environmentVariableName: 'PRODIA_TOKEN',
68
+ description: 'Prodia',
69
+ })}`,
70
+ ...options.headers,
71
+ },
72
+ `ai-sdk/prodia/${VERSION}`,
73
+ );
74
+
75
+ const createImageModel = (modelId: ProdiaImageModelId) =>
76
+ new ProdiaImageModel(modelId, {
77
+ provider: 'prodia.image',
78
+ baseURL: baseURL ?? defaultBaseURL,
79
+ headers: getHeaders,
80
+ fetch: options.fetch,
81
+ });
82
+
83
+ const embeddingModel = (modelId: string) => {
84
+ throw new NoSuchModelError({
85
+ modelId,
86
+ modelType: 'embeddingModel',
87
+ });
88
+ };
89
+
90
+ const languageModel = (modelId: string) => {
91
+ throw new NoSuchModelError({
92
+ modelId,
93
+ modelType: 'languageModel',
94
+ });
95
+ };
96
+
97
+ return {
98
+ specificationVersion: 'v3',
99
+ imageModel: createImageModel,
100
+ image: createImageModel,
101
+ languageModel,
102
+ embeddingModel,
103
+ textEmbeddingModel: embeddingModel,
104
+ };
105
+ }
106
+
107
+ export const prodia = createProdia();
package/src/version.ts ADDED
@@ -0,0 +1,6 @@
1
+ // Version string of this package injected at build time.
2
+ declare const __PACKAGE_VERSION__: string | undefined;
3
+ export const VERSION: string =
4
+ typeof __PACKAGE_VERSION__ !== 'undefined'
5
+ ? __PACKAGE_VERSION__
6
+ : '0.0.0-test';