@ai-sdk/prodia 2.0.0-beta.4 → 2.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -28,11 +28,159 @@ module.exports = __toCommonJS(index_exports);
28
28
 
29
29
  // src/prodia-provider.ts
30
30
  var import_provider = require("@ai-sdk/provider");
31
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
31
+ var import_provider_utils5 = require("@ai-sdk/provider-utils");
32
32
 
33
33
  // src/prodia-image-model.ts
34
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
35
+ var import_v42 = require("zod/v4");
36
+
37
+ // src/prodia-api.ts
34
38
  var import_provider_utils = require("@ai-sdk/provider-utils");
35
39
  var import_v4 = require("zod/v4");
40
+ var prodiaJobResultSchema = import_v4.z.object({
41
+ id: import_v4.z.string(),
42
+ created_at: import_v4.z.string().optional(),
43
+ updated_at: import_v4.z.string().optional(),
44
+ expires_at: import_v4.z.string().optional(),
45
+ state: import_v4.z.object({
46
+ current: import_v4.z.string()
47
+ }).optional(),
48
+ config: import_v4.z.object({
49
+ seed: import_v4.z.number().optional()
50
+ }).passthrough().optional(),
51
+ metrics: import_v4.z.object({
52
+ elapsed: import_v4.z.number().optional(),
53
+ ips: import_v4.z.number().optional()
54
+ }).optional(),
55
+ price: import_v4.z.object({
56
+ product: import_v4.z.string(),
57
+ dollars: import_v4.z.number()
58
+ }).nullish()
59
+ });
60
+ function buildProdiaProviderMetadata(jobResult) {
61
+ var _a, _b, _c, _d;
62
+ return {
63
+ jobId: jobResult.id,
64
+ ...((_a = jobResult.config) == null ? void 0 : _a.seed) != null && {
65
+ seed: jobResult.config.seed
66
+ },
67
+ ...((_b = jobResult.metrics) == null ? void 0 : _b.elapsed) != null && {
68
+ elapsed: jobResult.metrics.elapsed
69
+ },
70
+ ...((_c = jobResult.metrics) == null ? void 0 : _c.ips) != null && {
71
+ iterationsPerSecond: jobResult.metrics.ips
72
+ },
73
+ ...jobResult.created_at != null && {
74
+ createdAt: jobResult.created_at
75
+ },
76
+ ...jobResult.updated_at != null && {
77
+ updatedAt: jobResult.updated_at
78
+ },
79
+ ...((_d = jobResult.price) == null ? void 0 : _d.dollars) != null && {
80
+ dollars: jobResult.price.dollars
81
+ }
82
+ };
83
+ }
84
+ function parseMultipart(data, boundary) {
85
+ const parts = [];
86
+ const boundaryBytes = new TextEncoder().encode(`--${boundary}`);
87
+ const endBoundaryBytes = new TextEncoder().encode(`--${boundary}--`);
88
+ const positions = [];
89
+ for (let i = 0; i <= data.length - boundaryBytes.length; i++) {
90
+ let match = true;
91
+ for (let j = 0; j < boundaryBytes.length; j++) {
92
+ if (data[i + j] !== boundaryBytes[j]) {
93
+ match = false;
94
+ break;
95
+ }
96
+ }
97
+ if (match) {
98
+ positions.push(i);
99
+ }
100
+ }
101
+ for (let i = 0; i < positions.length - 1; i++) {
102
+ const start = positions[i] + boundaryBytes.length;
103
+ const end = positions[i + 1];
104
+ let isEndBoundary = true;
105
+ for (let j = 0; j < endBoundaryBytes.length && isEndBoundary; j++) {
106
+ if (data[positions[i] + j] !== endBoundaryBytes[j]) {
107
+ isEndBoundary = false;
108
+ }
109
+ }
110
+ if (isEndBoundary && positions[i] + endBoundaryBytes.length <= data.length) {
111
+ continue;
112
+ }
113
+ let partStart = start;
114
+ if (data[partStart] === 13 && data[partStart + 1] === 10) {
115
+ partStart += 2;
116
+ } else if (data[partStart] === 10) {
117
+ partStart += 1;
118
+ }
119
+ let partEnd = end;
120
+ if (data[partEnd - 2] === 13 && data[partEnd - 1] === 10) {
121
+ partEnd -= 2;
122
+ } else if (data[partEnd - 1] === 10) {
123
+ partEnd -= 1;
124
+ }
125
+ const partData = data.slice(partStart, partEnd);
126
+ let headerEnd = -1;
127
+ for (let j = 0; j < partData.length - 3; j++) {
128
+ if (partData[j] === 13 && partData[j + 1] === 10 && partData[j + 2] === 13 && partData[j + 3] === 10) {
129
+ headerEnd = j;
130
+ break;
131
+ }
132
+ if (partData[j] === 10 && partData[j + 1] === 10) {
133
+ headerEnd = j;
134
+ break;
135
+ }
136
+ }
137
+ if (headerEnd === -1) {
138
+ continue;
139
+ }
140
+ const headerBytes = partData.slice(0, headerEnd);
141
+ const headerStr = new TextDecoder().decode(headerBytes);
142
+ const headers = {};
143
+ for (const line of headerStr.split(/\r?\n/)) {
144
+ const colonIdx = line.indexOf(":");
145
+ if (colonIdx > 0) {
146
+ const key = line.slice(0, colonIdx).trim().toLowerCase();
147
+ const value = line.slice(colonIdx + 1).trim();
148
+ headers[key] = value;
149
+ }
150
+ }
151
+ let bodyStart = headerEnd + 2;
152
+ if (partData[headerEnd] === 13) {
153
+ bodyStart = headerEnd + 4;
154
+ }
155
+ const body = partData.slice(bodyStart);
156
+ parts.push({ headers, body });
157
+ }
158
+ return parts;
159
+ }
160
+ var prodiaErrorSchema = import_v4.z.object({
161
+ message: import_v4.z.string().optional(),
162
+ detail: import_v4.z.unknown().optional(),
163
+ error: import_v4.z.string().optional()
164
+ });
165
+ var prodiaFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
166
+ errorSchema: prodiaErrorSchema,
167
+ errorToMessage: (error) => {
168
+ var _a;
169
+ const parsed = prodiaErrorSchema.safeParse(error);
170
+ if (!parsed.success) return "Unknown Prodia error";
171
+ const { message, detail, error: errorField } = parsed.data;
172
+ if (typeof detail === "string") return detail;
173
+ if (detail != null) {
174
+ try {
175
+ return JSON.stringify(detail);
176
+ } catch (e) {
177
+ }
178
+ }
179
+ return (_a = errorField != null ? errorField : message) != null ? _a : "Unknown Prodia error";
180
+ }
181
+ });
182
+
183
+ // src/prodia-image-model.ts
36
184
  var ProdiaImageModel = class {
37
185
  constructor(modelId, config) {
38
186
  this.modelId = modelId;
@@ -50,7 +198,7 @@ var ProdiaImageModel = class {
50
198
  providerOptions
51
199
  }) {
52
200
  const warnings = [];
53
- const prodiaOptions = await (0, import_provider_utils.parseProviderOptions)({
201
+ const prodiaOptions = await (0, import_provider_utils2.parseProviderOptions)({
54
202
  provider: "prodia",
55
203
  providerOptions,
56
204
  schema: prodiaImageModelOptionsSchema
@@ -106,14 +254,14 @@ var ProdiaImageModel = class {
106
254
  return { body, warnings };
107
255
  }
108
256
  async doGenerate(options) {
109
- var _a, _b, _c, _d, _e, _f, _g;
257
+ var _a, _b, _c;
110
258
  const { body, warnings } = await this.getArgs(options);
111
259
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
112
- const combinedHeaders = (0, import_provider_utils.combineHeaders)(
113
- await (0, import_provider_utils.resolve)(this.config.headers),
260
+ const combinedHeaders = (0, import_provider_utils2.combineHeaders)(
261
+ await (0, import_provider_utils2.resolve)(this.config.headers),
114
262
  options.headers
115
263
  );
116
- const { value: multipartResult, responseHeaders } = await (0, import_provider_utils.postToApi)({
264
+ const { value: multipartResult, responseHeaders } = await (0, import_provider_utils2.postToApi)({
117
265
  url: `${this.config.baseURL}/job?price=true`,
118
266
  headers: {
119
267
  ...combinedHeaders,
@@ -135,29 +283,7 @@ var ProdiaImageModel = class {
135
283
  warnings,
136
284
  providerMetadata: {
137
285
  prodia: {
138
- images: [
139
- {
140
- jobId: jobResult.id,
141
- ...((_d = jobResult.config) == null ? void 0 : _d.seed) != null && {
142
- seed: jobResult.config.seed
143
- },
144
- ...((_e = jobResult.metrics) == null ? void 0 : _e.elapsed) != null && {
145
- elapsed: jobResult.metrics.elapsed
146
- },
147
- ...((_f = jobResult.metrics) == null ? void 0 : _f.ips) != null && {
148
- iterationsPerSecond: jobResult.metrics.ips
149
- },
150
- ...jobResult.created_at != null && {
151
- createdAt: jobResult.created_at
152
- },
153
- ...jobResult.updated_at != null && {
154
- updatedAt: jobResult.updated_at
155
- },
156
- ...((_g = jobResult.price) == null ? void 0 : _g.dollars) != null && {
157
- dollars: jobResult.price.dollars
158
- }
159
- }
160
- ]
286
+ images: [buildProdiaProviderMetadata(jobResult)]
161
287
  }
162
288
  },
163
289
  response: {
@@ -187,56 +313,36 @@ var stylePresets = [
187
313
  "texture",
188
314
  "craft-clay"
189
315
  ];
190
- var prodiaImageModelOptionsSchema = (0, import_provider_utils.lazySchema)(
191
- () => (0, import_provider_utils.zodSchema)(
192
- import_v4.z.object({
316
+ var prodiaImageModelOptionsSchema = (0, import_provider_utils2.lazySchema)(
317
+ () => (0, import_provider_utils2.zodSchema)(
318
+ import_v42.z.object({
193
319
  /**
194
320
  * Amount of computational iterations to run. More is typically higher quality.
195
321
  */
196
- steps: import_v4.z.number().int().min(1).max(4).optional(),
322
+ steps: import_v42.z.number().int().min(1).max(4).optional(),
197
323
  /**
198
324
  * Width of the output image in pixels.
199
325
  */
200
- width: import_v4.z.number().int().min(256).max(1920).optional(),
326
+ width: import_v42.z.number().int().min(256).max(1920).optional(),
201
327
  /**
202
328
  * Height of the output image in pixels.
203
329
  */
204
- height: import_v4.z.number().int().min(256).max(1920).optional(),
330
+ height: import_v42.z.number().int().min(256).max(1920).optional(),
205
331
  /**
206
332
  * Apply a visual theme to your output image.
207
333
  */
208
- stylePreset: import_v4.z.enum(stylePresets).optional(),
334
+ stylePreset: import_v42.z.enum(stylePresets).optional(),
209
335
  /**
210
336
  * Augment the output with a LoRa model.
211
337
  */
212
- loras: import_v4.z.array(import_v4.z.string()).max(3).optional(),
338
+ loras: import_v42.z.array(import_v42.z.string()).max(3).optional(),
213
339
  /**
214
340
  * When using JPEG output, return a progressive JPEG.
215
341
  */
216
- progressive: import_v4.z.boolean().optional()
342
+ progressive: import_v42.z.boolean().optional()
217
343
  })
218
344
  )
219
345
  );
220
- var prodiaJobResultSchema = import_v4.z.object({
221
- id: import_v4.z.string(),
222
- created_at: import_v4.z.string().optional(),
223
- updated_at: import_v4.z.string().optional(),
224
- expires_at: import_v4.z.string().optional(),
225
- state: import_v4.z.object({
226
- current: import_v4.z.string()
227
- }).optional(),
228
- config: import_v4.z.object({
229
- seed: import_v4.z.number().optional()
230
- }).passthrough().optional(),
231
- metrics: import_v4.z.object({
232
- elapsed: import_v4.z.number().optional(),
233
- ips: import_v4.z.number().optional()
234
- }).optional(),
235
- price: import_v4.z.object({
236
- product: import_v4.z.string(),
237
- dollars: import_v4.z.number()
238
- }).nullish()
239
- });
240
346
  function createMultipartResponseHandler() {
241
347
  return async ({
242
348
  response
@@ -264,7 +370,10 @@ function createMultipartResponseHandler() {
264
370
  const partContentType = (_c = part.headers["content-type"]) != null ? _c : "";
265
371
  if (contentDisposition.includes('name="job"')) {
266
372
  const jsonStr = new TextDecoder().decode(part.body);
267
- jobResult = prodiaJobResultSchema.parse(JSON.parse(jsonStr));
373
+ jobResult = await (0, import_provider_utils2.parseJSON)({
374
+ text: jsonStr,
375
+ schema: (0, import_provider_utils2.zodSchema)(prodiaJobResultSchema)
376
+ });
268
377
  } else if (contentDisposition.includes('name="output"')) {
269
378
  imageBytes = part.body;
270
379
  } else if (partContentType.startsWith("image/")) {
@@ -283,116 +392,524 @@ function createMultipartResponseHandler() {
283
392
  };
284
393
  };
285
394
  }
286
- function parseMultipart(data, boundary) {
287
- const parts = [];
288
- const boundaryBytes = new TextEncoder().encode(`--${boundary}`);
289
- const endBoundaryBytes = new TextEncoder().encode(`--${boundary}--`);
290
- const positions = [];
291
- for (let i = 0; i <= data.length - boundaryBytes.length; i++) {
292
- let match = true;
293
- for (let j = 0; j < boundaryBytes.length; j++) {
294
- if (data[i + j] !== boundaryBytes[j]) {
295
- match = false;
296
- break;
297
- }
395
+
396
+ // src/prodia-language-model.ts
397
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
398
+ var import_v43 = require("zod/v4");
399
+ var ProdiaLanguageModel = class {
400
+ constructor(modelId, config) {
401
+ this.modelId = modelId;
402
+ this.config = config;
403
+ this.specificationVersion = "v4";
404
+ this.supportedUrls = {};
405
+ }
406
+ get provider() {
407
+ return this.config.provider;
408
+ }
409
+ async doGenerate(options) {
410
+ var _a, _b, _c, _d;
411
+ const warnings = [];
412
+ if (options.temperature !== void 0) {
413
+ warnings.push({ type: "unsupported", feature: "temperature" });
298
414
  }
299
- if (match) {
300
- positions.push(i);
415
+ if (options.topP !== void 0) {
416
+ warnings.push({ type: "unsupported", feature: "topP" });
301
417
  }
302
- }
303
- for (let i = 0; i < positions.length - 1; i++) {
304
- const start = positions[i] + boundaryBytes.length;
305
- const end = positions[i + 1];
306
- let isEndBoundary = true;
307
- for (let j = 0; j < endBoundaryBytes.length && isEndBoundary; j++) {
308
- if (data[positions[i] + j] !== endBoundaryBytes[j]) {
309
- isEndBoundary = false;
310
- }
418
+ if (options.topK !== void 0) {
419
+ warnings.push({ type: "unsupported", feature: "topK" });
311
420
  }
312
- if (isEndBoundary && positions[i] + endBoundaryBytes.length <= data.length) {
313
- continue;
421
+ if (options.maxOutputTokens !== void 0) {
422
+ warnings.push({ type: "unsupported", feature: "maxOutputTokens" });
314
423
  }
315
- let partStart = start;
316
- if (data[partStart] === 13 && data[partStart + 1] === 10) {
317
- partStart += 2;
318
- } else if (data[partStart] === 10) {
319
- partStart += 1;
424
+ if (options.stopSequences !== void 0) {
425
+ warnings.push({ type: "unsupported", feature: "stopSequences" });
320
426
  }
321
- let partEnd = end;
322
- if (data[partEnd - 2] === 13 && data[partEnd - 1] === 10) {
323
- partEnd -= 2;
324
- } else if (data[partEnd - 1] === 10) {
325
- partEnd -= 1;
427
+ if (options.presencePenalty !== void 0) {
428
+ warnings.push({ type: "unsupported", feature: "presencePenalty" });
326
429
  }
327
- const partData = data.slice(partStart, partEnd);
328
- let headerEnd = -1;
329
- for (let j = 0; j < partData.length - 3; j++) {
330
- if (partData[j] === 13 && partData[j + 1] === 10 && partData[j + 2] === 13 && partData[j + 3] === 10) {
331
- headerEnd = j;
430
+ if (options.frequencyPenalty !== void 0) {
431
+ warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
432
+ }
433
+ if (options.tools !== void 0 && options.tools.length > 0) {
434
+ warnings.push({ type: "unsupported", feature: "tools" });
435
+ }
436
+ if (options.toolChoice !== void 0) {
437
+ warnings.push({ type: "unsupported", feature: "toolChoice" });
438
+ }
439
+ if (options.responseFormat !== void 0 && options.responseFormat.type !== "text") {
440
+ warnings.push({ type: "unsupported", feature: "responseFormat" });
441
+ }
442
+ const prodiaOptions = await (0, import_provider_utils3.parseProviderOptions)({
443
+ provider: "prodia",
444
+ providerOptions: options.providerOptions,
445
+ schema: prodiaLanguageModelOptionsSchema
446
+ });
447
+ let prompt = "";
448
+ let systemMessage = "";
449
+ for (const message of options.prompt) {
450
+ if (message.role === "system") {
451
+ systemMessage = message.content;
452
+ }
453
+ }
454
+ for (let i = options.prompt.length - 1; i >= 0; i--) {
455
+ const message = options.prompt[i];
456
+ if (message.role === "user") {
457
+ for (const part of message.content) {
458
+ if (part.type === "text") {
459
+ prompt += (prompt ? "\n" : "") + part.text;
460
+ }
461
+ }
332
462
  break;
333
463
  }
334
- if (partData[j] === 10 && partData[j + 1] === 10) {
335
- headerEnd = j;
464
+ }
465
+ if (systemMessage) {
466
+ prompt = systemMessage + "\n" + prompt;
467
+ }
468
+ let imageBytes;
469
+ let imageMediaType = "image/png";
470
+ for (let i = options.prompt.length - 1; i >= 0; i--) {
471
+ const message = options.prompt[i];
472
+ if (message.role === "user") {
473
+ for (const part of message.content) {
474
+ if (part.type === "file" && part.mediaType.startsWith("image/")) {
475
+ if (part.data instanceof Uint8Array) {
476
+ imageBytes = part.data;
477
+ } else if (typeof part.data === "string") {
478
+ imageBytes = (0, import_provider_utils3.convertBase64ToUint8Array)(part.data);
479
+ } else if (part.data instanceof URL) {
480
+ const fetchFn = (_a = this.config.fetch) != null ? _a : globalThis.fetch;
481
+ const response = await fetchFn(part.data.toString());
482
+ const arrayBuffer = await response.arrayBuffer();
483
+ imageBytes = new Uint8Array(arrayBuffer);
484
+ }
485
+ imageMediaType = part.mediaType;
486
+ break;
487
+ }
488
+ }
336
489
  break;
337
490
  }
338
491
  }
339
- if (headerEnd === -1) {
340
- continue;
492
+ const jobConfig = {
493
+ prompt,
494
+ include_messages: true
495
+ };
496
+ if ((prodiaOptions == null ? void 0 : prodiaOptions.aspectRatio) !== void 0) {
497
+ jobConfig.aspect_ratio = prodiaOptions.aspectRatio;
341
498
  }
342
- const headerBytes = partData.slice(0, headerEnd);
343
- const headerStr = new TextDecoder().decode(headerBytes);
344
- const headers = {};
345
- for (const line of headerStr.split(/\r?\n/)) {
346
- const colonIdx = line.indexOf(":");
347
- if (colonIdx > 0) {
348
- const key = line.slice(0, colonIdx).trim().toLowerCase();
349
- const value = line.slice(colonIdx + 1).trim();
350
- headers[key] = value;
499
+ const body = {
500
+ type: this.modelId,
501
+ config: jobConfig
502
+ };
503
+ const currentDate = (_d = (_c = (_b = this.config._internal) == null ? void 0 : _b.currentDate) == null ? void 0 : _c.call(_b)) != null ? _d : /* @__PURE__ */ new Date();
504
+ const combinedHeaders = (0, import_provider_utils3.combineHeaders)(
505
+ await (0, import_provider_utils3.resolve)(this.config.headers),
506
+ options.headers
507
+ );
508
+ const formData = new FormData();
509
+ formData.append(
510
+ "job",
511
+ new Blob([JSON.stringify(body)], { type: "application/json" }),
512
+ "job.json"
513
+ );
514
+ if (imageBytes) {
515
+ const ext = imageMediaType === "image/png" ? ".png" : imageMediaType === "image/jpeg" ? ".jpg" : imageMediaType === "image/webp" ? ".webp" : "";
516
+ formData.append(
517
+ "input",
518
+ new Blob([imageBytes], { type: imageMediaType }),
519
+ "input" + ext
520
+ );
521
+ }
522
+ const { value: multipartResult, responseHeaders } = await (0, import_provider_utils3.postFormDataToApi)(
523
+ {
524
+ url: `${this.config.baseURL}/job?price=true`,
525
+ headers: {
526
+ ...combinedHeaders,
527
+ Accept: "multipart/form-data"
528
+ },
529
+ formData,
530
+ failedResponseHandler: prodiaFailedResponseHandler,
531
+ successfulResponseHandler: createLanguageMultipartResponseHandler(),
532
+ abortSignal: options.abortSignal,
533
+ fetch: this.config.fetch
351
534
  }
535
+ );
536
+ const { jobResult, textContent, fileContent } = multipartResult;
537
+ const content = [];
538
+ if (textContent !== void 0) {
539
+ content.push({ type: "text", text: textContent });
352
540
  }
353
- let bodyStart = headerEnd + 2;
354
- if (partData[headerEnd] === 13) {
355
- bodyStart = headerEnd + 4;
541
+ for (const file of fileContent) {
542
+ content.push({
543
+ type: "file",
544
+ mediaType: file.mediaType,
545
+ data: file.data
546
+ });
356
547
  }
357
- const body = partData.slice(bodyStart);
358
- parts.push({ headers, body });
548
+ return {
549
+ content,
550
+ finishReason: { unified: "stop", raw: void 0 },
551
+ usage: {
552
+ inputTokens: {
553
+ total: void 0,
554
+ noCache: void 0,
555
+ cacheRead: void 0,
556
+ cacheWrite: void 0
557
+ },
558
+ outputTokens: {
559
+ total: void 0,
560
+ text: void 0,
561
+ reasoning: void 0
562
+ }
563
+ },
564
+ warnings,
565
+ providerMetadata: {
566
+ prodia: buildProdiaProviderMetadata(jobResult)
567
+ },
568
+ response: {
569
+ modelId: this.modelId,
570
+ timestamp: currentDate,
571
+ headers: responseHeaders
572
+ }
573
+ };
359
574
  }
360
- return parts;
361
- }
362
- var prodiaErrorSchema = import_v4.z.object({
363
- message: import_v4.z.string().optional(),
364
- detail: import_v4.z.unknown().optional(),
365
- error: import_v4.z.string().optional()
366
- });
367
- var prodiaFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
368
- errorSchema: prodiaErrorSchema,
369
- errorToMessage: (error) => {
575
+ async doStream(options) {
370
576
  var _a;
371
- const parsed = prodiaErrorSchema.safeParse(error);
372
- if (!parsed.success) return "Unknown Prodia error";
373
- const { message, detail, error: errorField } = parsed.data;
374
- if (typeof detail === "string") return detail;
375
- if (detail != null) {
376
- try {
377
- return JSON.stringify(detail);
378
- } catch (e) {
577
+ const result = await this.doGenerate(options);
578
+ const stream = new ReadableStream({
579
+ start(controller) {
580
+ var _a2, _b;
581
+ controller.enqueue({
582
+ type: "stream-start",
583
+ warnings: result.warnings
584
+ });
585
+ controller.enqueue({
586
+ type: "response-metadata",
587
+ modelId: (_a2 = result.response) == null ? void 0 : _a2.modelId,
588
+ timestamp: (_b = result.response) == null ? void 0 : _b.timestamp
589
+ });
590
+ for (const part of result.content) {
591
+ if (part.type === "text") {
592
+ const id = (0, import_provider_utils3.generateId)();
593
+ controller.enqueue({ type: "text-start", id });
594
+ controller.enqueue({
595
+ type: "text-delta",
596
+ id,
597
+ delta: part.text
598
+ });
599
+ controller.enqueue({ type: "text-end", id });
600
+ } else if (part.type === "file") {
601
+ controller.enqueue({
602
+ type: "file",
603
+ mediaType: part.mediaType,
604
+ data: part.data
605
+ });
606
+ }
607
+ }
608
+ controller.enqueue({
609
+ type: "finish",
610
+ usage: result.usage,
611
+ finishReason: result.finishReason,
612
+ providerMetadata: result.providerMetadata
613
+ });
614
+ controller.close();
379
615
  }
616
+ });
617
+ return {
618
+ stream,
619
+ response: {
620
+ headers: (_a = result.response) == null ? void 0 : _a.headers
621
+ }
622
+ };
623
+ }
624
+ };
625
+ var prodiaLanguageModelOptionsSchema = (0, import_provider_utils3.lazySchema)(
626
+ () => (0, import_provider_utils3.zodSchema)(
627
+ import_v43.z.object({
628
+ /**
629
+ * Aspect ratio for the output image.
630
+ */
631
+ aspectRatio: import_v43.z.enum([
632
+ "1:1",
633
+ "2:3",
634
+ "3:2",
635
+ "4:5",
636
+ "5:4",
637
+ "4:7",
638
+ "7:4",
639
+ "9:16",
640
+ "16:9",
641
+ "9:21",
642
+ "21:9"
643
+ ]).optional()
644
+ })
645
+ )
646
+ );
647
+ function createLanguageMultipartResponseHandler() {
648
+ return async ({
649
+ response
650
+ }) => {
651
+ var _a, _b, _c;
652
+ const contentType = (_a = response.headers.get("content-type")) != null ? _a : "";
653
+ const responseHeaders = {};
654
+ response.headers.forEach((value, key) => {
655
+ responseHeaders[key] = value;
656
+ });
657
+ const boundaryMatch = contentType.match(/boundary=([^\s;]+)/);
658
+ if (!boundaryMatch) {
659
+ throw new Error(
660
+ `Prodia response missing multipart boundary in content-type: ${contentType}`
661
+ );
380
662
  }
381
- return (_a = errorField != null ? errorField : message) != null ? _a : "Unknown Prodia error";
663
+ const boundary = boundaryMatch[1];
664
+ const arrayBuffer = await response.arrayBuffer();
665
+ const bytes = new Uint8Array(arrayBuffer);
666
+ const parts = parseMultipart(bytes, boundary);
667
+ let jobResult;
668
+ let textContent;
669
+ const fileContent = [];
670
+ for (const part of parts) {
671
+ const contentDisposition = (_b = part.headers["content-disposition"]) != null ? _b : "";
672
+ const partContentType = (_c = part.headers["content-type"]) != null ? _c : "";
673
+ if (contentDisposition.includes('name="job"')) {
674
+ const jsonStr = new TextDecoder().decode(part.body);
675
+ jobResult = await (0, import_provider_utils3.parseJSON)({
676
+ text: jsonStr,
677
+ schema: (0, import_provider_utils3.zodSchema)(prodiaJobResultSchema)
678
+ });
679
+ } else if (contentDisposition.includes('name="output"')) {
680
+ if (partContentType.startsWith("text/") || contentDisposition.includes(".txt")) {
681
+ textContent = new TextDecoder().decode(part.body);
682
+ } else if (partContentType.startsWith("image/")) {
683
+ fileContent.push({
684
+ mediaType: partContentType,
685
+ data: part.body
686
+ });
687
+ }
688
+ }
689
+ }
690
+ if (!jobResult) {
691
+ throw new Error("Prodia multipart response missing job part");
692
+ }
693
+ return {
694
+ value: { jobResult, textContent, fileContent },
695
+ responseHeaders
696
+ };
697
+ };
698
+ }
699
+
700
+ // src/prodia-video-model.ts
701
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
702
+ var import_v44 = require("zod/v4");
703
+ var ProdiaVideoModel = class {
704
+ constructor(modelId, config) {
705
+ this.modelId = modelId;
706
+ this.config = config;
707
+ this.specificationVersion = "v4";
708
+ this.maxVideosPerCall = 1;
382
709
  }
383
- });
710
+ get provider() {
711
+ return this.config.provider;
712
+ }
713
+ async doGenerate(options) {
714
+ var _a, _b, _c;
715
+ const warnings = [];
716
+ const prodiaOptions = await (0, import_provider_utils4.parseProviderOptions)({
717
+ provider: "prodia",
718
+ providerOptions: options.providerOptions,
719
+ schema: prodiaVideoModelOptionsSchema
720
+ });
721
+ const jobConfig = {};
722
+ if (options.prompt !== void 0) {
723
+ jobConfig.prompt = options.prompt;
724
+ }
725
+ if (options.seed !== void 0) {
726
+ jobConfig.seed = options.seed;
727
+ }
728
+ if ((prodiaOptions == null ? void 0 : prodiaOptions.resolution) !== void 0) {
729
+ jobConfig.resolution = prodiaOptions.resolution;
730
+ }
731
+ const body = {
732
+ type: this.modelId,
733
+ config: jobConfig
734
+ };
735
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
736
+ const combinedHeaders = (0, import_provider_utils4.combineHeaders)(
737
+ await (0, import_provider_utils4.resolve)(this.config.headers),
738
+ options.headers
739
+ );
740
+ let multipartResult;
741
+ let responseHeaders;
742
+ if (options.image) {
743
+ const imageData = await resolveVideoFileData(
744
+ options.image,
745
+ this.config.fetch
746
+ );
747
+ const formData = new FormData();
748
+ formData.append(
749
+ "job",
750
+ new Blob([JSON.stringify(body)], { type: "application/json" }),
751
+ "job.json"
752
+ );
753
+ formData.append(
754
+ "input",
755
+ new Blob([imageData.bytes], { type: imageData.mediaType }),
756
+ "input" + getExtension(imageData.mediaType)
757
+ );
758
+ const result = await (0, import_provider_utils4.postFormDataToApi)({
759
+ url: `${this.config.baseURL}/job?price=true`,
760
+ headers: {
761
+ ...combinedHeaders,
762
+ Accept: "multipart/form-data; video/mp4"
763
+ },
764
+ formData,
765
+ failedResponseHandler: prodiaFailedResponseHandler,
766
+ successfulResponseHandler: createVideoMultipartResponseHandler(),
767
+ abortSignal: options.abortSignal,
768
+ fetch: this.config.fetch
769
+ });
770
+ multipartResult = result.value;
771
+ responseHeaders = result.responseHeaders;
772
+ } else {
773
+ const result = await (0, import_provider_utils4.postToApi)({
774
+ url: `${this.config.baseURL}/job?price=true`,
775
+ headers: {
776
+ ...combinedHeaders,
777
+ Accept: "multipart/form-data; video/mp4",
778
+ "Content-Type": "application/json"
779
+ },
780
+ body: {
781
+ content: JSON.stringify(body),
782
+ values: body
783
+ },
784
+ failedResponseHandler: prodiaFailedResponseHandler,
785
+ successfulResponseHandler: createVideoMultipartResponseHandler(),
786
+ abortSignal: options.abortSignal,
787
+ fetch: this.config.fetch
788
+ });
789
+ multipartResult = result.value;
790
+ responseHeaders = result.responseHeaders;
791
+ }
792
+ const { jobResult, videoBytes, videoMediaType } = multipartResult;
793
+ return {
794
+ videos: [
795
+ {
796
+ type: "binary",
797
+ data: videoBytes,
798
+ mediaType: videoMediaType
799
+ }
800
+ ],
801
+ warnings,
802
+ providerMetadata: {
803
+ prodia: {
804
+ videos: [buildProdiaProviderMetadata(jobResult)]
805
+ }
806
+ },
807
+ response: {
808
+ modelId: this.modelId,
809
+ timestamp: currentDate,
810
+ headers: responseHeaders
811
+ }
812
+ };
813
+ }
814
+ };
815
+ var prodiaVideoModelOptionsSchema = (0, import_provider_utils4.lazySchema)(
816
+ () => (0, import_provider_utils4.zodSchema)(
817
+ import_v44.z.object({
818
+ /**
819
+ * Video resolution (e.g. "480p", "720p").
820
+ */
821
+ resolution: import_v44.z.string().optional()
822
+ })
823
+ )
824
+ );
825
+ function createVideoMultipartResponseHandler() {
826
+ return async ({
827
+ response
828
+ }) => {
829
+ var _a, _b, _c;
830
+ const contentType = (_a = response.headers.get("content-type")) != null ? _a : "";
831
+ const responseHeaders = {};
832
+ response.headers.forEach((value, key) => {
833
+ responseHeaders[key] = value;
834
+ });
835
+ const boundaryMatch = contentType.match(/boundary=([^\s;]+)/);
836
+ if (!boundaryMatch) {
837
+ throw new Error(
838
+ `Prodia response missing multipart boundary in content-type: ${contentType}`
839
+ );
840
+ }
841
+ const boundary = boundaryMatch[1];
842
+ const arrayBuffer = await response.arrayBuffer();
843
+ const bytes = new Uint8Array(arrayBuffer);
844
+ const parts = parseMultipart(bytes, boundary);
845
+ let jobResult;
846
+ let videoBytes;
847
+ let videoMediaType = "video/mp4";
848
+ for (const part of parts) {
849
+ const contentDisposition = (_b = part.headers["content-disposition"]) != null ? _b : "";
850
+ const partContentType = (_c = part.headers["content-type"]) != null ? _c : "";
851
+ if (contentDisposition.includes('name="job"')) {
852
+ const jsonStr = new TextDecoder().decode(part.body);
853
+ jobResult = await (0, import_provider_utils4.parseJSON)({
854
+ text: jsonStr,
855
+ schema: (0, import_provider_utils4.zodSchema)(prodiaJobResultSchema)
856
+ });
857
+ } else if (contentDisposition.includes('name="output"')) {
858
+ videoBytes = part.body;
859
+ if (partContentType.startsWith("video/")) {
860
+ videoMediaType = partContentType;
861
+ }
862
+ } else if (partContentType.startsWith("video/")) {
863
+ videoBytes = part.body;
864
+ videoMediaType = partContentType;
865
+ }
866
+ }
867
+ if (!jobResult) {
868
+ throw new Error("Prodia multipart response missing job part");
869
+ }
870
+ if (!videoBytes) {
871
+ throw new Error("Prodia multipart response missing output video");
872
+ }
873
+ return {
874
+ value: { jobResult, videoBytes, videoMediaType },
875
+ responseHeaders
876
+ };
877
+ };
878
+ }
879
+ async function resolveVideoFileData(file, fetchFunction) {
880
+ var _a;
881
+ if (file.type === "file") {
882
+ const data = typeof file.data === "string" ? (0, import_provider_utils4.convertBase64ToUint8Array)(file.data) : file.data;
883
+ return { bytes: data, mediaType: file.mediaType };
884
+ }
885
+ const response = await (fetchFunction != null ? fetchFunction : globalThis.fetch)(file.url);
886
+ const arrayBuffer = await response.arrayBuffer();
887
+ const mediaType = (_a = response.headers.get("content-type")) != null ? _a : "application/octet-stream";
888
+ return { bytes: new Uint8Array(arrayBuffer), mediaType };
889
+ }
890
+ function getExtension(mediaType) {
891
+ var _a;
892
+ const map = {
893
+ "image/png": ".png",
894
+ "image/jpeg": ".jpg",
895
+ "image/webp": ".webp",
896
+ "video/mp4": ".mp4",
897
+ "video/webm": ".webm"
898
+ };
899
+ return (_a = map[mediaType]) != null ? _a : "";
900
+ }
384
901
 
385
902
  // src/version.ts
386
- var VERSION = true ? "2.0.0-beta.4" : "0.0.0-test";
903
+ var VERSION = true ? "2.0.0-beta.5" : "0.0.0-test";
387
904
 
388
905
  // src/prodia-provider.ts
389
906
  var defaultBaseURL = "https://inference.prodia.com/v2";
390
907
  function createProdia(options = {}) {
391
908
  var _a;
392
- const baseURL = (0, import_provider_utils2.withoutTrailingSlash)((_a = options.baseURL) != null ? _a : defaultBaseURL);
393
- const getHeaders = () => (0, import_provider_utils2.withUserAgentSuffix)(
909
+ const baseURL = (0, import_provider_utils5.withoutTrailingSlash)((_a = options.baseURL) != null ? _a : defaultBaseURL);
910
+ const getHeaders = () => (0, import_provider_utils5.withUserAgentSuffix)(
394
911
  {
395
- Authorization: `Bearer ${(0, import_provider_utils2.loadApiKey)({
912
+ Authorization: `Bearer ${(0, import_provider_utils5.loadApiKey)({
396
913
  apiKey: options.apiKey,
397
914
  environmentVariableName: "PRODIA_TOKEN",
398
915
  description: "Prodia"
@@ -407,23 +924,31 @@ function createProdia(options = {}) {
407
924
  headers: getHeaders,
408
925
  fetch: options.fetch
409
926
  });
927
+ const createLanguageModel = (modelId) => new ProdiaLanguageModel(modelId, {
928
+ provider: "prodia.language",
929
+ baseURL: baseURL != null ? baseURL : defaultBaseURL,
930
+ headers: getHeaders,
931
+ fetch: options.fetch
932
+ });
933
+ const createVideoModel = (modelId) => new ProdiaVideoModel(modelId, {
934
+ provider: "prodia.video",
935
+ baseURL: baseURL != null ? baseURL : defaultBaseURL,
936
+ headers: getHeaders,
937
+ fetch: options.fetch
938
+ });
410
939
  const embeddingModel = (modelId) => {
411
940
  throw new import_provider.NoSuchModelError({
412
941
  modelId,
413
942
  modelType: "embeddingModel"
414
943
  });
415
944
  };
416
- const languageModel = (modelId) => {
417
- throw new import_provider.NoSuchModelError({
418
- modelId,
419
- modelType: "languageModel"
420
- });
421
- };
422
945
  return {
423
946
  specificationVersion: "v4",
947
+ languageModel: createLanguageModel,
424
948
  imageModel: createImageModel,
425
949
  image: createImageModel,
426
- languageModel,
950
+ videoModel: createVideoModel,
951
+ video: createVideoModel,
427
952
  embeddingModel,
428
953
  textEmbeddingModel: embeddingModel
429
954
  };