@ai-sdk/prodia 0.0.4 → 0.0.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
@@ -18,21 +18,169 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
23
  VERSION: () => VERSION,
24
24
  createProdia: () => createProdia,
25
25
  prodia: () => prodia
26
26
  });
27
- module.exports = __toCommonJS(src_exports);
27
+ 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_utils4 = 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: prodiaImageProviderOptionsSchema
@@ -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 prodiaImageProviderOptionsSchema = (0, import_provider_utils.lazySchema)(
191
- () => (0, import_provider_utils.zodSchema)(
192
- import_v4.z.object({
316
+ var prodiaImageProviderOptionsSchema = (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,320 @@ 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 = "v2";
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-setting", setting: "temperature" });
298
414
  }
299
- if (match) {
300
- positions.push(i);
415
+ if (options.topP !== void 0) {
416
+ warnings.push({ type: "unsupported-setting", setting: "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-setting", setting: "topK" });
311
420
  }
312
- if (isEndBoundary && positions[i] + endBoundaryBytes.length <= data.length) {
313
- continue;
421
+ if (options.maxOutputTokens !== void 0) {
422
+ warnings.push({
423
+ type: "unsupported-setting",
424
+ setting: "maxOutputTokens"
425
+ });
314
426
  }
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;
427
+ if (options.stopSequences !== void 0) {
428
+ warnings.push({ type: "unsupported-setting", setting: "stopSequences" });
320
429
  }
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;
430
+ if (options.presencePenalty !== void 0) {
431
+ warnings.push({
432
+ type: "unsupported-setting",
433
+ setting: "presencePenalty"
434
+ });
326
435
  }
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;
436
+ if (options.frequencyPenalty !== void 0) {
437
+ warnings.push({
438
+ type: "unsupported-setting",
439
+ setting: "frequencyPenalty"
440
+ });
441
+ }
442
+ if (options.tools !== void 0 && options.tools.length > 0) {
443
+ warnings.push({ type: "unsupported-setting", setting: "tools" });
444
+ }
445
+ if (options.toolChoice !== void 0) {
446
+ warnings.push({ type: "unsupported-setting", setting: "toolChoice" });
447
+ }
448
+ if (options.responseFormat !== void 0 && options.responseFormat.type !== "text") {
449
+ warnings.push({ type: "unsupported-setting", setting: "responseFormat" });
450
+ }
451
+ const prodiaOptions = await (0, import_provider_utils3.parseProviderOptions)({
452
+ provider: "prodia",
453
+ providerOptions: options.providerOptions,
454
+ schema: prodiaLanguageModelOptionsSchema
455
+ });
456
+ let prompt = "";
457
+ let systemMessage = "";
458
+ for (const message of options.prompt) {
459
+ if (message.role === "system") {
460
+ systemMessage = message.content;
461
+ }
462
+ }
463
+ for (let i = options.prompt.length - 1; i >= 0; i--) {
464
+ const message = options.prompt[i];
465
+ if (message.role === "user") {
466
+ for (const part of message.content) {
467
+ if (part.type === "text") {
468
+ prompt += (prompt ? "\n" : "") + part.text;
469
+ }
470
+ }
332
471
  break;
333
472
  }
334
- if (partData[j] === 10 && partData[j + 1] === 10) {
335
- headerEnd = j;
473
+ }
474
+ if (systemMessage) {
475
+ prompt = systemMessage + "\n" + prompt;
476
+ }
477
+ let imageBytes;
478
+ let imageMediaType = "image/png";
479
+ for (let i = options.prompt.length - 1; i >= 0; i--) {
480
+ const message = options.prompt[i];
481
+ if (message.role === "user") {
482
+ for (const part of message.content) {
483
+ if (part.type === "file" && part.mediaType.startsWith("image/")) {
484
+ if (part.data instanceof Uint8Array) {
485
+ imageBytes = part.data;
486
+ } else if (typeof part.data === "string") {
487
+ imageBytes = (0, import_provider_utils3.convertBase64ToUint8Array)(part.data);
488
+ } else if (part.data instanceof URL) {
489
+ const fetchFn = (_a = this.config.fetch) != null ? _a : globalThis.fetch;
490
+ const response = await fetchFn(part.data.toString());
491
+ const arrayBuffer = await response.arrayBuffer();
492
+ imageBytes = new Uint8Array(arrayBuffer);
493
+ }
494
+ imageMediaType = part.mediaType;
495
+ break;
496
+ }
497
+ }
336
498
  break;
337
499
  }
338
500
  }
339
- if (headerEnd === -1) {
340
- continue;
501
+ const jobConfig = {
502
+ prompt,
503
+ include_messages: true
504
+ };
505
+ if ((prodiaOptions == null ? void 0 : prodiaOptions.aspectRatio) !== void 0) {
506
+ jobConfig.aspect_ratio = prodiaOptions.aspectRatio;
341
507
  }
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;
508
+ const body = {
509
+ type: this.modelId,
510
+ config: jobConfig
511
+ };
512
+ const currentDate = (_d = (_c = (_b = this.config._internal) == null ? void 0 : _b.currentDate) == null ? void 0 : _c.call(_b)) != null ? _d : /* @__PURE__ */ new Date();
513
+ const combinedHeaders = (0, import_provider_utils3.combineHeaders)(
514
+ await (0, import_provider_utils3.resolve)(this.config.headers),
515
+ options.headers
516
+ );
517
+ const formData = new FormData();
518
+ formData.append(
519
+ "job",
520
+ new Blob([JSON.stringify(body)], { type: "application/json" }),
521
+ "job.json"
522
+ );
523
+ if (imageBytes) {
524
+ const ext = imageMediaType === "image/png" ? ".png" : imageMediaType === "image/jpeg" ? ".jpg" : imageMediaType === "image/webp" ? ".webp" : "";
525
+ formData.append(
526
+ "input",
527
+ new Blob([imageBytes], { type: imageMediaType }),
528
+ "input" + ext
529
+ );
530
+ }
531
+ const { value: multipartResult, responseHeaders } = await (0, import_provider_utils3.postFormDataToApi)(
532
+ {
533
+ url: `${this.config.baseURL}/job?price=true`,
534
+ headers: {
535
+ ...combinedHeaders,
536
+ Accept: "multipart/form-data"
537
+ },
538
+ formData,
539
+ failedResponseHandler: prodiaFailedResponseHandler,
540
+ successfulResponseHandler: createLanguageMultipartResponseHandler(),
541
+ abortSignal: options.abortSignal,
542
+ fetch: this.config.fetch
351
543
  }
544
+ );
545
+ const { jobResult, textContent, fileContent } = multipartResult;
546
+ const content = [];
547
+ if (textContent !== void 0) {
548
+ content.push({ type: "text", text: textContent });
352
549
  }
353
- let bodyStart = headerEnd + 2;
354
- if (partData[headerEnd] === 13) {
355
- bodyStart = headerEnd + 4;
550
+ for (const file of fileContent) {
551
+ content.push({
552
+ type: "file",
553
+ mediaType: file.mediaType,
554
+ data: file.data
555
+ });
356
556
  }
357
- const body = partData.slice(bodyStart);
358
- parts.push({ headers, body });
557
+ return {
558
+ content,
559
+ finishReason: "stop",
560
+ usage: {
561
+ inputTokens: void 0,
562
+ outputTokens: void 0,
563
+ totalTokens: void 0
564
+ },
565
+ warnings,
566
+ providerMetadata: {
567
+ prodia: buildProdiaProviderMetadata(jobResult)
568
+ },
569
+ response: {
570
+ modelId: this.modelId,
571
+ timestamp: currentDate,
572
+ headers: responseHeaders
573
+ }
574
+ };
359
575
  }
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) => {
576
+ async doStream(options) {
370
577
  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) {
578
+ const result = await this.doGenerate(options);
579
+ const stream = new ReadableStream({
580
+ start(controller) {
581
+ var _a2, _b;
582
+ controller.enqueue({
583
+ type: "stream-start",
584
+ warnings: result.warnings
585
+ });
586
+ controller.enqueue({
587
+ type: "response-metadata",
588
+ modelId: (_a2 = result.response) == null ? void 0 : _a2.modelId,
589
+ timestamp: (_b = result.response) == null ? void 0 : _b.timestamp
590
+ });
591
+ for (const part of result.content) {
592
+ if (part.type === "text") {
593
+ const id = (0, import_provider_utils3.generateId)();
594
+ controller.enqueue({ type: "text-start", id });
595
+ controller.enqueue({
596
+ type: "text-delta",
597
+ id,
598
+ delta: part.text
599
+ });
600
+ controller.enqueue({ type: "text-end", id });
601
+ } else if (part.type === "file") {
602
+ controller.enqueue({
603
+ type: "file",
604
+ mediaType: part.mediaType,
605
+ data: part.data
606
+ });
607
+ }
608
+ }
609
+ controller.enqueue({
610
+ type: "finish",
611
+ usage: result.usage,
612
+ finishReason: result.finishReason,
613
+ providerMetadata: result.providerMetadata
614
+ });
615
+ controller.close();
379
616
  }
380
- }
381
- return (_a = errorField != null ? errorField : message) != null ? _a : "Unknown Prodia error";
617
+ });
618
+ return {
619
+ stream,
620
+ response: {
621
+ headers: (_a = result.response) == null ? void 0 : _a.headers
622
+ }
623
+ };
382
624
  }
383
- });
625
+ };
626
+ var prodiaLanguageModelOptionsSchema = (0, import_provider_utils3.lazySchema)(
627
+ () => (0, import_provider_utils3.zodSchema)(
628
+ import_v43.z.object({
629
+ aspectRatio: import_v43.z.enum([
630
+ "1:1",
631
+ "2:3",
632
+ "3:2",
633
+ "4:5",
634
+ "5:4",
635
+ "4:7",
636
+ "7:4",
637
+ "9:16",
638
+ "16:9",
639
+ "9:21",
640
+ "21:9"
641
+ ]).optional()
642
+ })
643
+ )
644
+ );
645
+ function createLanguageMultipartResponseHandler() {
646
+ return async ({
647
+ response
648
+ }) => {
649
+ var _a, _b, _c;
650
+ const contentType = (_a = response.headers.get("content-type")) != null ? _a : "";
651
+ const responseHeaders = {};
652
+ response.headers.forEach((value, key) => {
653
+ responseHeaders[key] = value;
654
+ });
655
+ const boundaryMatch = contentType.match(/boundary=([^\s;]+)/);
656
+ if (!boundaryMatch) {
657
+ throw new Error(
658
+ `Prodia response missing multipart boundary in content-type: ${contentType}`
659
+ );
660
+ }
661
+ const boundary = boundaryMatch[1];
662
+ const arrayBuffer = await response.arrayBuffer();
663
+ const bytes = new Uint8Array(arrayBuffer);
664
+ const parts = parseMultipart(bytes, boundary);
665
+ let jobResult;
666
+ let textContent;
667
+ const fileContent = [];
668
+ for (const part of parts) {
669
+ const contentDisposition = (_b = part.headers["content-disposition"]) != null ? _b : "";
670
+ const partContentType = (_c = part.headers["content-type"]) != null ? _c : "";
671
+ if (contentDisposition.includes('name="job"')) {
672
+ const jsonStr = new TextDecoder().decode(part.body);
673
+ jobResult = await (0, import_provider_utils3.parseJSON)({
674
+ text: jsonStr,
675
+ schema: (0, import_provider_utils3.zodSchema)(prodiaJobResultSchema)
676
+ });
677
+ } else if (contentDisposition.includes('name="output"')) {
678
+ if (partContentType.startsWith("text/") || contentDisposition.includes(".txt")) {
679
+ textContent = new TextDecoder().decode(part.body);
680
+ } else if (partContentType.startsWith("image/")) {
681
+ fileContent.push({
682
+ mediaType: partContentType,
683
+ data: part.body
684
+ });
685
+ }
686
+ }
687
+ }
688
+ if (!jobResult) {
689
+ throw new Error("Prodia multipart response missing job part");
690
+ }
691
+ return {
692
+ value: { jobResult, textContent, fileContent },
693
+ responseHeaders
694
+ };
695
+ };
696
+ }
384
697
 
385
698
  // src/version.ts
386
- var VERSION = true ? "0.0.4" : "0.0.0-test";
699
+ var VERSION = true ? "0.0.5" : "0.0.0-test";
387
700
 
388
701
  // src/prodia-provider.ts
389
702
  var defaultBaseURL = "https://inference.prodia.com/v2";
390
703
  function createProdia(options = {}) {
391
704
  var _a;
392
- const baseURL = (0, import_provider_utils2.withoutTrailingSlash)((_a = options.baseURL) != null ? _a : defaultBaseURL);
393
- const getHeaders = () => (0, import_provider_utils2.withUserAgentSuffix)(
705
+ const baseURL = (0, import_provider_utils4.withoutTrailingSlash)((_a = options.baseURL) != null ? _a : defaultBaseURL);
706
+ const getHeaders = () => (0, import_provider_utils4.withUserAgentSuffix)(
394
707
  {
395
- Authorization: `Bearer ${(0, import_provider_utils2.loadApiKey)({
708
+ Authorization: `Bearer ${(0, import_provider_utils4.loadApiKey)({
396
709
  apiKey: options.apiKey,
397
710
  environmentVariableName: "PRODIA_TOKEN",
398
711
  description: "Prodia"
@@ -407,22 +720,22 @@ function createProdia(options = {}) {
407
720
  headers: getHeaders,
408
721
  fetch: options.fetch
409
722
  });
723
+ const createLanguageModel = (modelId) => new ProdiaLanguageModel(modelId, {
724
+ provider: "prodia.language",
725
+ baseURL: baseURL != null ? baseURL : defaultBaseURL,
726
+ headers: getHeaders,
727
+ fetch: options.fetch
728
+ });
410
729
  const textEmbeddingModel = (modelId) => {
411
730
  throw new import_provider.NoSuchModelError({
412
731
  modelId,
413
732
  modelType: "textEmbeddingModel"
414
733
  });
415
734
  };
416
- const languageModel = (modelId) => {
417
- throw new import_provider.NoSuchModelError({
418
- modelId,
419
- modelType: "languageModel"
420
- });
421
- };
422
735
  return {
736
+ languageModel: createLanguageModel,
423
737
  imageModel: createImageModel,
424
738
  image: createImageModel,
425
- languageModel,
426
739
  textEmbeddingModel
427
740
  };
428
741
  }