@ai-sdk/prodia 2.0.0-beta.2 → 2.0.0-beta.21

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
@@ -27,12 +27,160 @@ __export(index_exports, {
27
27
  module.exports = __toCommonJS(index_exports);
28
28
 
29
29
  // src/prodia-provider.ts
30
- var import_provider = require("@ai-sdk/provider");
31
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
30
+ var import_provider2 = require("@ai-sdk/provider");
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,537 @@ 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 = require("@ai-sdk/provider");
398
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
399
+ var import_v43 = require("zod/v4");
400
+ var ProdiaLanguageModel = class {
401
+ constructor(modelId, config) {
402
+ this.modelId = modelId;
403
+ this.config = config;
404
+ this.specificationVersion = "v4";
405
+ this.supportedUrls = {};
406
+ }
407
+ get provider() {
408
+ return this.config.provider;
409
+ }
410
+ async doGenerate(options) {
411
+ var _a, _b, _c, _d;
412
+ const warnings = [];
413
+ if (options.temperature !== void 0) {
414
+ warnings.push({ type: "unsupported", feature: "temperature" });
298
415
  }
299
- if (match) {
300
- positions.push(i);
416
+ if (options.topP !== void 0) {
417
+ warnings.push({ type: "unsupported", feature: "topP" });
301
418
  }
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
- }
419
+ if (options.topK !== void 0) {
420
+ warnings.push({ type: "unsupported", feature: "topK" });
311
421
  }
312
- if (isEndBoundary && positions[i] + endBoundaryBytes.length <= data.length) {
313
- continue;
422
+ if (options.maxOutputTokens !== void 0) {
423
+ warnings.push({ type: "unsupported", feature: "maxOutputTokens" });
314
424
  }
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;
425
+ if (options.stopSequences !== void 0) {
426
+ warnings.push({ type: "unsupported", feature: "stopSequences" });
320
427
  }
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;
428
+ if (options.presencePenalty !== void 0) {
429
+ warnings.push({ type: "unsupported", feature: "presencePenalty" });
326
430
  }
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;
431
+ if (options.frequencyPenalty !== void 0) {
432
+ warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
433
+ }
434
+ if (options.tools !== void 0 && options.tools.length > 0) {
435
+ warnings.push({ type: "unsupported", feature: "tools" });
436
+ }
437
+ if (options.toolChoice !== void 0) {
438
+ warnings.push({ type: "unsupported", feature: "toolChoice" });
439
+ }
440
+ if (options.responseFormat !== void 0 && options.responseFormat.type !== "text") {
441
+ warnings.push({ type: "unsupported", feature: "responseFormat" });
442
+ }
443
+ if ((0, import_provider_utils3.isCustomReasoning)(options.reasoning)) {
444
+ warnings.push({
445
+ type: "unsupported",
446
+ feature: "reasoning",
447
+ details: "This provider does not support reasoning configuration."
448
+ });
449
+ }
450
+ const prodiaOptions = await (0, import_provider_utils3.parseProviderOptions)({
451
+ provider: "prodia",
452
+ providerOptions: options.providerOptions,
453
+ schema: prodiaLanguageModelOptionsSchema
454
+ });
455
+ let prompt = "";
456
+ let systemMessage = "";
457
+ for (const message of options.prompt) {
458
+ if (message.role === "system") {
459
+ systemMessage = message.content;
460
+ }
461
+ }
462
+ for (let i = options.prompt.length - 1; i >= 0; i--) {
463
+ const message = options.prompt[i];
464
+ if (message.role === "user") {
465
+ for (const part of message.content) {
466
+ if (part.type === "text") {
467
+ prompt += (prompt ? "\n" : "") + part.text;
468
+ }
469
+ }
332
470
  break;
333
471
  }
334
- if (partData[j] === 10 && partData[j + 1] === 10) {
335
- headerEnd = j;
472
+ }
473
+ if (systemMessage) {
474
+ prompt = systemMessage + "\n" + prompt;
475
+ }
476
+ let imageBytes;
477
+ let imageMediaType = "image/png";
478
+ for (let i = options.prompt.length - 1; i >= 0; i--) {
479
+ const message = options.prompt[i];
480
+ if (message.role === "user") {
481
+ for (const part of message.content) {
482
+ if (part.type === "file" && part.mediaType.startsWith("image/")) {
483
+ if ((0, import_provider_utils3.isProviderReference)(part.data)) {
484
+ throw new import_provider.UnsupportedFunctionalityError({
485
+ functionality: "file parts with provider references"
486
+ });
487
+ }
488
+ if (part.data instanceof Uint8Array) {
489
+ imageBytes = part.data;
490
+ } else if (typeof part.data === "string") {
491
+ imageBytes = (0, import_provider_utils3.convertBase64ToUint8Array)(part.data);
492
+ } else if (part.data instanceof URL) {
493
+ const fetchFn = (_a = this.config.fetch) != null ? _a : globalThis.fetch;
494
+ const response = await fetchFn(part.data.toString());
495
+ const arrayBuffer = await response.arrayBuffer();
496
+ imageBytes = new Uint8Array(arrayBuffer);
497
+ }
498
+ imageMediaType = part.mediaType;
499
+ break;
500
+ }
501
+ }
336
502
  break;
337
503
  }
338
504
  }
339
- if (headerEnd === -1) {
340
- continue;
505
+ const jobConfig = {
506
+ prompt,
507
+ include_messages: true
508
+ };
509
+ if ((prodiaOptions == null ? void 0 : prodiaOptions.aspectRatio) !== void 0) {
510
+ jobConfig.aspect_ratio = prodiaOptions.aspectRatio;
341
511
  }
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;
512
+ const body = {
513
+ type: this.modelId,
514
+ config: jobConfig
515
+ };
516
+ const currentDate = (_d = (_c = (_b = this.config._internal) == null ? void 0 : _b.currentDate) == null ? void 0 : _c.call(_b)) != null ? _d : /* @__PURE__ */ new Date();
517
+ const combinedHeaders = (0, import_provider_utils3.combineHeaders)(
518
+ await (0, import_provider_utils3.resolve)(this.config.headers),
519
+ options.headers
520
+ );
521
+ const formData = new FormData();
522
+ formData.append(
523
+ "job",
524
+ new Blob([JSON.stringify(body)], { type: "application/json" }),
525
+ "job.json"
526
+ );
527
+ if (imageBytes) {
528
+ const ext = imageMediaType === "image/png" ? ".png" : imageMediaType === "image/jpeg" ? ".jpg" : imageMediaType === "image/webp" ? ".webp" : "";
529
+ formData.append(
530
+ "input",
531
+ new Blob([imageBytes], { type: imageMediaType }),
532
+ "input" + ext
533
+ );
534
+ }
535
+ const { value: multipartResult, responseHeaders } = await (0, import_provider_utils3.postFormDataToApi)(
536
+ {
537
+ url: `${this.config.baseURL}/job?price=true`,
538
+ headers: {
539
+ ...combinedHeaders,
540
+ Accept: "multipart/form-data"
541
+ },
542
+ formData,
543
+ failedResponseHandler: prodiaFailedResponseHandler,
544
+ successfulResponseHandler: createLanguageMultipartResponseHandler(),
545
+ abortSignal: options.abortSignal,
546
+ fetch: this.config.fetch
351
547
  }
548
+ );
549
+ const { jobResult, textContent, fileContent } = multipartResult;
550
+ const content = [];
551
+ if (textContent !== void 0) {
552
+ content.push({ type: "text", text: textContent });
352
553
  }
353
- let bodyStart = headerEnd + 2;
354
- if (partData[headerEnd] === 13) {
355
- bodyStart = headerEnd + 4;
554
+ for (const file of fileContent) {
555
+ content.push({
556
+ type: "file",
557
+ mediaType: file.mediaType,
558
+ data: file.data
559
+ });
356
560
  }
357
- const body = partData.slice(bodyStart);
358
- parts.push({ headers, body });
561
+ return {
562
+ content,
563
+ finishReason: { unified: "stop", raw: void 0 },
564
+ usage: {
565
+ inputTokens: {
566
+ total: void 0,
567
+ noCache: void 0,
568
+ cacheRead: void 0,
569
+ cacheWrite: void 0
570
+ },
571
+ outputTokens: {
572
+ total: void 0,
573
+ text: void 0,
574
+ reasoning: void 0
575
+ }
576
+ },
577
+ warnings,
578
+ providerMetadata: {
579
+ prodia: buildProdiaProviderMetadata(jobResult)
580
+ },
581
+ response: {
582
+ modelId: this.modelId,
583
+ timestamp: currentDate,
584
+ headers: responseHeaders
585
+ }
586
+ };
359
587
  }
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) => {
588
+ async doStream(options) {
370
589
  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) {
590
+ const result = await this.doGenerate(options);
591
+ const stream = new ReadableStream({
592
+ start(controller) {
593
+ var _a2, _b;
594
+ controller.enqueue({
595
+ type: "stream-start",
596
+ warnings: result.warnings
597
+ });
598
+ controller.enqueue({
599
+ type: "response-metadata",
600
+ modelId: (_a2 = result.response) == null ? void 0 : _a2.modelId,
601
+ timestamp: (_b = result.response) == null ? void 0 : _b.timestamp
602
+ });
603
+ for (const part of result.content) {
604
+ if (part.type === "text") {
605
+ const id = (0, import_provider_utils3.generateId)();
606
+ controller.enqueue({ type: "text-start", id });
607
+ controller.enqueue({
608
+ type: "text-delta",
609
+ id,
610
+ delta: part.text
611
+ });
612
+ controller.enqueue({ type: "text-end", id });
613
+ } else if (part.type === "file") {
614
+ controller.enqueue({
615
+ type: "file",
616
+ mediaType: part.mediaType,
617
+ data: part.data
618
+ });
619
+ }
620
+ }
621
+ controller.enqueue({
622
+ type: "finish",
623
+ usage: result.usage,
624
+ finishReason: result.finishReason,
625
+ providerMetadata: result.providerMetadata
626
+ });
627
+ controller.close();
628
+ }
629
+ });
630
+ return {
631
+ stream,
632
+ response: {
633
+ headers: (_a = result.response) == null ? void 0 : _a.headers
379
634
  }
635
+ };
636
+ }
637
+ };
638
+ var prodiaLanguageModelOptionsSchema = (0, import_provider_utils3.lazySchema)(
639
+ () => (0, import_provider_utils3.zodSchema)(
640
+ import_v43.z.object({
641
+ /**
642
+ * Aspect ratio for the output image.
643
+ */
644
+ aspectRatio: import_v43.z.enum([
645
+ "1:1",
646
+ "2:3",
647
+ "3:2",
648
+ "4:5",
649
+ "5:4",
650
+ "4:7",
651
+ "7:4",
652
+ "9:16",
653
+ "16:9",
654
+ "9:21",
655
+ "21:9"
656
+ ]).optional()
657
+ })
658
+ )
659
+ );
660
+ function createLanguageMultipartResponseHandler() {
661
+ return async ({
662
+ response
663
+ }) => {
664
+ var _a, _b, _c;
665
+ const contentType = (_a = response.headers.get("content-type")) != null ? _a : "";
666
+ const responseHeaders = {};
667
+ response.headers.forEach((value, key) => {
668
+ responseHeaders[key] = value;
669
+ });
670
+ const boundaryMatch = contentType.match(/boundary=([^\s;]+)/);
671
+ if (!boundaryMatch) {
672
+ throw new Error(
673
+ `Prodia response missing multipart boundary in content-type: ${contentType}`
674
+ );
380
675
  }
381
- return (_a = errorField != null ? errorField : message) != null ? _a : "Unknown Prodia error";
676
+ const boundary = boundaryMatch[1];
677
+ const arrayBuffer = await response.arrayBuffer();
678
+ const bytes = new Uint8Array(arrayBuffer);
679
+ const parts = parseMultipart(bytes, boundary);
680
+ let jobResult;
681
+ let textContent;
682
+ const fileContent = [];
683
+ for (const part of parts) {
684
+ const contentDisposition = (_b = part.headers["content-disposition"]) != null ? _b : "";
685
+ const partContentType = (_c = part.headers["content-type"]) != null ? _c : "";
686
+ if (contentDisposition.includes('name="job"')) {
687
+ const jsonStr = new TextDecoder().decode(part.body);
688
+ jobResult = await (0, import_provider_utils3.parseJSON)({
689
+ text: jsonStr,
690
+ schema: (0, import_provider_utils3.zodSchema)(prodiaJobResultSchema)
691
+ });
692
+ } else if (contentDisposition.includes('name="output"')) {
693
+ if (partContentType.startsWith("text/") || contentDisposition.includes(".txt")) {
694
+ textContent = new TextDecoder().decode(part.body);
695
+ } else if (partContentType.startsWith("image/")) {
696
+ fileContent.push({
697
+ mediaType: partContentType,
698
+ data: part.body
699
+ });
700
+ }
701
+ }
702
+ }
703
+ if (!jobResult) {
704
+ throw new Error("Prodia multipart response missing job part");
705
+ }
706
+ return {
707
+ value: { jobResult, textContent, fileContent },
708
+ responseHeaders
709
+ };
710
+ };
711
+ }
712
+
713
+ // src/prodia-video-model.ts
714
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
715
+ var import_v44 = require("zod/v4");
716
+ var ProdiaVideoModel = class {
717
+ constructor(modelId, config) {
718
+ this.modelId = modelId;
719
+ this.config = config;
720
+ this.specificationVersion = "v4";
721
+ this.maxVideosPerCall = 1;
382
722
  }
383
- });
723
+ get provider() {
724
+ return this.config.provider;
725
+ }
726
+ async doGenerate(options) {
727
+ var _a, _b, _c;
728
+ const warnings = [];
729
+ const prodiaOptions = await (0, import_provider_utils4.parseProviderOptions)({
730
+ provider: "prodia",
731
+ providerOptions: options.providerOptions,
732
+ schema: prodiaVideoModelOptionsSchema
733
+ });
734
+ const jobConfig = {};
735
+ if (options.prompt !== void 0) {
736
+ jobConfig.prompt = options.prompt;
737
+ }
738
+ if (options.seed !== void 0) {
739
+ jobConfig.seed = options.seed;
740
+ }
741
+ if ((prodiaOptions == null ? void 0 : prodiaOptions.resolution) !== void 0) {
742
+ jobConfig.resolution = prodiaOptions.resolution;
743
+ }
744
+ const body = {
745
+ type: this.modelId,
746
+ config: jobConfig
747
+ };
748
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
749
+ const combinedHeaders = (0, import_provider_utils4.combineHeaders)(
750
+ await (0, import_provider_utils4.resolve)(this.config.headers),
751
+ options.headers
752
+ );
753
+ let multipartResult;
754
+ let responseHeaders;
755
+ if (options.image) {
756
+ const imageData = await resolveVideoFileData(
757
+ options.image,
758
+ this.config.fetch
759
+ );
760
+ const formData = new FormData();
761
+ formData.append(
762
+ "job",
763
+ new Blob([JSON.stringify(body)], { type: "application/json" }),
764
+ "job.json"
765
+ );
766
+ formData.append(
767
+ "input",
768
+ new Blob([imageData.bytes], { type: imageData.mediaType }),
769
+ "input" + getExtension(imageData.mediaType)
770
+ );
771
+ const result = await (0, import_provider_utils4.postFormDataToApi)({
772
+ url: `${this.config.baseURL}/job?price=true`,
773
+ headers: {
774
+ ...combinedHeaders,
775
+ Accept: "multipart/form-data; video/mp4"
776
+ },
777
+ formData,
778
+ failedResponseHandler: prodiaFailedResponseHandler,
779
+ successfulResponseHandler: createVideoMultipartResponseHandler(),
780
+ abortSignal: options.abortSignal,
781
+ fetch: this.config.fetch
782
+ });
783
+ multipartResult = result.value;
784
+ responseHeaders = result.responseHeaders;
785
+ } else {
786
+ const result = await (0, import_provider_utils4.postToApi)({
787
+ url: `${this.config.baseURL}/job?price=true`,
788
+ headers: {
789
+ ...combinedHeaders,
790
+ Accept: "multipart/form-data; video/mp4",
791
+ "Content-Type": "application/json"
792
+ },
793
+ body: {
794
+ content: JSON.stringify(body),
795
+ values: body
796
+ },
797
+ failedResponseHandler: prodiaFailedResponseHandler,
798
+ successfulResponseHandler: createVideoMultipartResponseHandler(),
799
+ abortSignal: options.abortSignal,
800
+ fetch: this.config.fetch
801
+ });
802
+ multipartResult = result.value;
803
+ responseHeaders = result.responseHeaders;
804
+ }
805
+ const { jobResult, videoBytes, videoMediaType } = multipartResult;
806
+ return {
807
+ videos: [
808
+ {
809
+ type: "binary",
810
+ data: videoBytes,
811
+ mediaType: videoMediaType
812
+ }
813
+ ],
814
+ warnings,
815
+ providerMetadata: {
816
+ prodia: {
817
+ videos: [buildProdiaProviderMetadata(jobResult)]
818
+ }
819
+ },
820
+ response: {
821
+ modelId: this.modelId,
822
+ timestamp: currentDate,
823
+ headers: responseHeaders
824
+ }
825
+ };
826
+ }
827
+ };
828
+ var prodiaVideoModelOptionsSchema = (0, import_provider_utils4.lazySchema)(
829
+ () => (0, import_provider_utils4.zodSchema)(
830
+ import_v44.z.object({
831
+ /**
832
+ * Video resolution (e.g. "480p", "720p").
833
+ */
834
+ resolution: import_v44.z.string().optional()
835
+ })
836
+ )
837
+ );
838
+ function createVideoMultipartResponseHandler() {
839
+ return async ({
840
+ response
841
+ }) => {
842
+ var _a, _b, _c;
843
+ const contentType = (_a = response.headers.get("content-type")) != null ? _a : "";
844
+ const responseHeaders = {};
845
+ response.headers.forEach((value, key) => {
846
+ responseHeaders[key] = value;
847
+ });
848
+ const boundaryMatch = contentType.match(/boundary=([^\s;]+)/);
849
+ if (!boundaryMatch) {
850
+ throw new Error(
851
+ `Prodia response missing multipart boundary in content-type: ${contentType}`
852
+ );
853
+ }
854
+ const boundary = boundaryMatch[1];
855
+ const arrayBuffer = await response.arrayBuffer();
856
+ const bytes = new Uint8Array(arrayBuffer);
857
+ const parts = parseMultipart(bytes, boundary);
858
+ let jobResult;
859
+ let videoBytes;
860
+ let videoMediaType = "video/mp4";
861
+ for (const part of parts) {
862
+ const contentDisposition = (_b = part.headers["content-disposition"]) != null ? _b : "";
863
+ const partContentType = (_c = part.headers["content-type"]) != null ? _c : "";
864
+ if (contentDisposition.includes('name="job"')) {
865
+ const jsonStr = new TextDecoder().decode(part.body);
866
+ jobResult = await (0, import_provider_utils4.parseJSON)({
867
+ text: jsonStr,
868
+ schema: (0, import_provider_utils4.zodSchema)(prodiaJobResultSchema)
869
+ });
870
+ } else if (contentDisposition.includes('name="output"')) {
871
+ videoBytes = part.body;
872
+ if (partContentType.startsWith("video/")) {
873
+ videoMediaType = partContentType;
874
+ }
875
+ } else if (partContentType.startsWith("video/")) {
876
+ videoBytes = part.body;
877
+ videoMediaType = partContentType;
878
+ }
879
+ }
880
+ if (!jobResult) {
881
+ throw new Error("Prodia multipart response missing job part");
882
+ }
883
+ if (!videoBytes) {
884
+ throw new Error("Prodia multipart response missing output video");
885
+ }
886
+ return {
887
+ value: { jobResult, videoBytes, videoMediaType },
888
+ responseHeaders
889
+ };
890
+ };
891
+ }
892
+ async function resolveVideoFileData(file, fetchFunction) {
893
+ var _a;
894
+ if (file.type === "file") {
895
+ const data = typeof file.data === "string" ? (0, import_provider_utils4.convertBase64ToUint8Array)(file.data) : file.data;
896
+ return { bytes: data, mediaType: file.mediaType };
897
+ }
898
+ const response = await (fetchFunction != null ? fetchFunction : globalThis.fetch)(file.url);
899
+ const arrayBuffer = await response.arrayBuffer();
900
+ const mediaType = (_a = response.headers.get("content-type")) != null ? _a : "application/octet-stream";
901
+ return { bytes: new Uint8Array(arrayBuffer), mediaType };
902
+ }
903
+ function getExtension(mediaType) {
904
+ var _a;
905
+ const map = {
906
+ "image/png": ".png",
907
+ "image/jpeg": ".jpg",
908
+ "image/webp": ".webp",
909
+ "video/mp4": ".mp4",
910
+ "video/webm": ".webm"
911
+ };
912
+ return (_a = map[mediaType]) != null ? _a : "";
913
+ }
384
914
 
385
915
  // src/version.ts
386
- var VERSION = true ? "2.0.0-beta.2" : "0.0.0-test";
916
+ var VERSION = true ? "2.0.0-beta.21" : "0.0.0-test";
387
917
 
388
918
  // src/prodia-provider.ts
389
919
  var defaultBaseURL = "https://inference.prodia.com/v2";
390
920
  function createProdia(options = {}) {
391
921
  var _a;
392
- const baseURL = (0, import_provider_utils2.withoutTrailingSlash)((_a = options.baseURL) != null ? _a : defaultBaseURL);
393
- const getHeaders = () => (0, import_provider_utils2.withUserAgentSuffix)(
922
+ const baseURL = (0, import_provider_utils5.withoutTrailingSlash)((_a = options.baseURL) != null ? _a : defaultBaseURL);
923
+ const getHeaders = () => (0, import_provider_utils5.withUserAgentSuffix)(
394
924
  {
395
- Authorization: `Bearer ${(0, import_provider_utils2.loadApiKey)({
925
+ Authorization: `Bearer ${(0, import_provider_utils5.loadApiKey)({
396
926
  apiKey: options.apiKey,
397
927
  environmentVariableName: "PRODIA_TOKEN",
398
928
  description: "Prodia"
@@ -407,23 +937,31 @@ function createProdia(options = {}) {
407
937
  headers: getHeaders,
408
938
  fetch: options.fetch
409
939
  });
940
+ const createLanguageModel = (modelId) => new ProdiaLanguageModel(modelId, {
941
+ provider: "prodia.language",
942
+ baseURL: baseURL != null ? baseURL : defaultBaseURL,
943
+ headers: getHeaders,
944
+ fetch: options.fetch
945
+ });
946
+ const createVideoModel = (modelId) => new ProdiaVideoModel(modelId, {
947
+ provider: "prodia.video",
948
+ baseURL: baseURL != null ? baseURL : defaultBaseURL,
949
+ headers: getHeaders,
950
+ fetch: options.fetch
951
+ });
410
952
  const embeddingModel = (modelId) => {
411
- throw new import_provider.NoSuchModelError({
953
+ throw new import_provider2.NoSuchModelError({
412
954
  modelId,
413
955
  modelType: "embeddingModel"
414
956
  });
415
957
  };
416
- const languageModel = (modelId) => {
417
- throw new import_provider.NoSuchModelError({
418
- modelId,
419
- modelType: "languageModel"
420
- });
421
- };
422
958
  return {
423
959
  specificationVersion: "v4",
960
+ languageModel: createLanguageModel,
424
961
  imageModel: createImageModel,
425
962
  image: createImageModel,
426
- languageModel,
963
+ videoModel: createVideoModel,
964
+ video: createVideoModel,
427
965
  embeddingModel,
428
966
  textEmbeddingModel: embeddingModel
429
967
  };