@shop-os/modelkit 0.29.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1072,6 +1072,65 @@ var IMAGE_MODELS = [
1072
1072
  description: "Ideogram's precise edit model: repeated edits keep the untouched pixels and colours intact, at the source's native resolution. Also generates from text."
1073
1073
  }
1074
1074
  },
1075
+ {
1076
+ // Generation only: fal charges edits more, so editing is its own row
1077
+ // (ideogram-4.6-edit) with its own prices. fal bills any output above
1078
+ // 2048x2048 in area at the 4K price; very_low renders at most 2K, so it is
1079
+ // not exposed (no 4K price exists for it, and the catalog seed gate needs
1080
+ // every size x quality priced). https://fal.ai/models/ideogram/v4.6
1081
+ id: "ideogram-4.6",
1082
+ aliases: [],
1083
+ label: "Ideogram 4.6",
1084
+ type: "image",
1085
+ adapter: "fal",
1086
+ providerModelId: "ideogram/v4.6",
1087
+ lifecycle: "active",
1088
+ exposure: ["cowork", "space_builder"],
1089
+ constraints: {
1090
+ aspectRatios: ["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "5:4", "4:5"],
1091
+ sizes: ["2K", "4K"],
1092
+ qualityOptions: ["auto", "low", "medium", "high"],
1093
+ references: { images: 0 },
1094
+ supports: ["quality", "count", "seed"]
1095
+ },
1096
+ defaults: { size: "2K", aspectRatio: "1:1", quality: "medium", count: 1 },
1097
+ usage: { ...IMAGE_USAGE3, slug: "ideogram" },
1098
+ pricingKind: "image_size",
1099
+ display: {
1100
+ provider: "ideogram",
1101
+ description: "Ideogram 4.6 text-to-image from a prompt, at 2K or 4K. Edits go to Ideogram 4.6 Edit."
1102
+ }
1103
+ },
1104
+ {
1105
+ // Precise Edit only: references switch to the /edit endpoint, where the
1106
+ // first is the image to edit and the rest (up to 4) guide it, so 5 images
1107
+ // in all. The output keeps the source size, so `size` only picks the
1108
+ // billed tier. Same provider model as the generate row, whose adapter
1109
+ // routes on references. https://fal.ai/models/ideogram/v4.6/edit
1110
+ id: "ideogram-4.6-edit",
1111
+ aliases: [],
1112
+ label: "Ideogram 4.6 Edit",
1113
+ type: "image",
1114
+ adapter: "fal",
1115
+ providerModelId: "ideogram/v4.6",
1116
+ lifecycle: "active",
1117
+ exposure: ["cowork", "space_builder"],
1118
+ constraints: {
1119
+ aspectRatios: ["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "5:4", "4:5"],
1120
+ sizes: ["2K", "4K"],
1121
+ qualityOptions: ["auto", "low", "medium", "high"],
1122
+ references: { images: 5 },
1123
+ requires: ["references"],
1124
+ supports: ["quality", "count", "seed"]
1125
+ },
1126
+ defaults: { size: "2K", aspectRatio: "1:1", quality: "medium", count: 1 },
1127
+ usage: { ...IMAGE_USAGE3, slug: "ideogram" },
1128
+ pricingKind: "image_size",
1129
+ display: {
1130
+ provider: "ideogram",
1131
+ description: "Ideogram 4.6 Precise Edit: the first image is edited and up to 4 more guide it. Repeated edits keep the untouched pixels and colours intact, and the output keeps the source size."
1132
+ }
1133
+ },
1075
1134
  {
1076
1135
  id: "seedream-5-0-pro",
1077
1136
  aliases: ["seedream-4-5"],
@@ -9671,6 +9730,17 @@ function falImageSpec(providerModelId) {
9671
9730
  }
9672
9731
  var AREA_1K = 1024 * 1024;
9673
9732
  var AREA_2K = 2048 * 2048;
9733
+ var IDEOGRAM_46_EXACT = {
9734
+ minSide: 256,
9735
+ maxSide: 4096,
9736
+ minArea: 256 * 256,
9737
+ maxArea: 4096 * 4096,
9738
+ multipleOf: 32,
9739
+ maxAspect: 6,
9740
+ minTier: "2K",
9741
+ topTier: "4K"
9742
+ };
9743
+ var IDEOGRAM_46_IDS = /* @__PURE__ */ new Set(["ideogram/v4.6"]);
9674
9744
  function checkExactSize(exact, b) {
9675
9745
  const match = /^(\d+)x(\d+)$/.exec(exact);
9676
9746
  if (match === null) {
@@ -9694,7 +9764,7 @@ function checkExactSize(exact, b) {
9694
9764
  return { ok: true, size: { width, height, tier: exactTier(area, b) } };
9695
9765
  }
9696
9766
  function exactTier(area, b) {
9697
- if (area <= AREA_1K) return "1K";
9767
+ if (area <= AREA_1K) return b.minTier ?? "1K";
9698
9768
  if (b.topTier === void 0 || area <= AREA_2K) return "2K";
9699
9769
  return b.topTier;
9700
9770
  }
@@ -9756,23 +9826,43 @@ function falImageProblem(spec, req) {
9756
9826
  }
9757
9827
  function falExactSize(spec, req) {
9758
9828
  if (req.exactSize === void 0 || spec.exactSize === void 0) return void 0;
9759
- const check = checkExactSize(req.exactSize, spec.exactSize);
9829
+ return checkedExactSize(req.exactSize, spec.exactSize);
9830
+ }
9831
+ function checkedExactSize(exact, bounds) {
9832
+ const check = checkExactSize(exact, bounds);
9760
9833
  return check.ok ? check.size : void 0;
9761
9834
  }
9835
+ function ideogram46ExactSize(model, req) {
9836
+ if (req.exactSize === void 0) return void 0;
9837
+ const check = checkExactSize(req.exactSize, IDEOGRAM_46_EXACT);
9838
+ if (check.ok) return check.size;
9839
+ throw new ModelKitError({
9840
+ code: "unsupported_option",
9841
+ message: `${model.label}: ${check.message}`,
9842
+ model: model.id,
9843
+ retryable: false
9844
+ });
9845
+ }
9762
9846
  function applyFalImageRules(model, req, callerSize, warnings) {
9763
9847
  if (model.adapter !== "fal" && model.adapter !== "alibaba") return;
9764
9848
  const spec = falImageSpec(model.providerModelId);
9765
- if (spec === void 0) return;
9766
- const problem = falImageProblem(spec, withPromptForResolve(req));
9767
- if (problem !== void 0) {
9768
- throw new ModelKitError({
9769
- code: problem.code,
9770
- message: problem.message,
9771
- model: model.id,
9772
- retryable: false
9773
- });
9849
+ const ideogram46 = IDEOGRAM_46_IDS.has(model.providerModelId.toLowerCase());
9850
+ if (spec === void 0 && !ideogram46) return;
9851
+ let exact;
9852
+ if (spec === void 0) {
9853
+ exact = ideogram46ExactSize(model, req);
9854
+ } else {
9855
+ const problem = falImageProblem(spec, withPromptForResolve(req));
9856
+ if (problem !== void 0) {
9857
+ throw new ModelKitError({
9858
+ code: problem.code,
9859
+ message: problem.message,
9860
+ model: model.id,
9861
+ retryable: false
9862
+ });
9863
+ }
9864
+ exact = falExactSize(spec, req);
9774
9865
  }
9775
- const exact = falExactSize(spec, req);
9776
9866
  if (exact === void 0) return;
9777
9867
  if (callerSize !== void 0 && callerSize !== exact.tier) {
9778
9868
  warnings.push({
@@ -12739,6 +12829,17 @@ async function generateFalMask(req, ctx, run) {
12739
12829
  return result;
12740
12830
  }
12741
12831
 
12832
+ // src/adapters/png-size.ts
12833
+ var PNG_SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
12834
+ function pngDimensions(bytes) {
12835
+ if (bytes.length < 24) return void 0;
12836
+ if (PNG_SIGNATURE.some((byte, index) => bytes[index] !== byte)) return void 0;
12837
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
12838
+ const width = view.getUint32(16);
12839
+ const height = view.getUint32(20);
12840
+ return width > 0 && height > 0 ? { width, height } : void 0;
12841
+ }
12842
+
12742
12843
  // src/adapters/fal-audio-plan.ts
12743
12844
  var import_zod19 = require("zod");
12744
12845
  var ELEVENLABS_MUSIC = FAL_AUDIO.elevenLabsMusic;
@@ -14316,20 +14417,39 @@ var museImageResultSchema = import_zod23.z.object({
14316
14417
  images: import_zod23.z.array(
14317
14418
  import_zod23.z.object({
14318
14419
  url: import_zod23.z.string().optional(),
14319
- content_type: import_zod23.z.string().nullable().optional()
14420
+ content_type: import_zod23.z.string().nullable().optional(),
14421
+ // Some fal image endpoints report the delivered dimensions (Kling); Ideogram
14422
+ // 4.6's File schema reports none, so its files are measured instead.
14423
+ width: import_zod23.z.number().int().positive().optional(),
14424
+ height: import_zod23.z.number().int().positive().optional()
14320
14425
  })
14321
14426
  ).optional()
14322
14427
  });
14428
+ var IDEOGRAM_46_ID = "ideogram/v4.6";
14429
+ var IDEOGRAM_46_4K_AREA = 2048 * 2048;
14430
+ function isIdeogram46(providerModelId) {
14431
+ return providerModelId.toLowerCase() === IDEOGRAM_46_ID;
14432
+ }
14433
+ function ideogramBilledSize(delivered, fallback) {
14434
+ const areas = delivered.flatMap((image) => {
14435
+ const size = image.width !== void 0 && image.height !== void 0 ? { width: image.width, height: image.height } : pngDimensions(image.bytes);
14436
+ return size === void 0 ? [] : [size.width * size.height];
14437
+ });
14438
+ if (areas.length === 0) return fallback;
14439
+ return Math.max(...areas) > IDEOGRAM_46_4K_AREA ? "4K" : "2K";
14440
+ }
14323
14441
  async function collectFalImages(payload, ctx, req, label) {
14324
14442
  const parsed = museImageResultSchema.safeParse(payload);
14325
14443
  const images = parsed.success ? parsed.data.images ?? [] : [];
14326
- const files = await Promise.all(
14327
- images.flatMap((img) => {
14328
- if (img.url === void 0) return [];
14329
- return [decodeImage(ctx, img.url, img.content_type ?? void 0)];
14444
+ const downloaded = await Promise.all(
14445
+ images.map(async (image) => {
14446
+ if (image.url === void 0) return void 0;
14447
+ const file = await decodeImage(ctx, image.url, image.content_type ?? void 0);
14448
+ return image.width !== void 0 && image.height !== void 0 ? { ...file, width: image.width, height: image.height } : file;
14330
14449
  })
14331
14450
  );
14332
- if (files.length === 0) {
14451
+ const delivered = downloaded.filter((image) => image !== void 0);
14452
+ if (delivered.length === 0) {
14333
14453
  throw new ModelKitError({
14334
14454
  code: "provider_rejected",
14335
14455
  message: `${label} returned no image`,
@@ -14338,9 +14458,14 @@ async function collectFalImages(payload, ctx, req, label) {
14338
14458
  retryable: false
14339
14459
  });
14340
14460
  }
14461
+ const usage6 = imageUsage(ctx, req, delivered.length);
14462
+ if (isIdeogram46(ctx.model.providerModelId)) {
14463
+ const billed = ideogramBilledSize(delivered, req.size);
14464
+ if (billed !== void 0) usage6.imageSize = billed;
14465
+ }
14341
14466
  return {
14342
- images: files.map((f) => ({ bytes: f.bytes, mime: f.mime })),
14343
- usage: imageUsage(ctx, req, files.length),
14467
+ images: delivered.map(({ bytes, mime }) => ({ bytes, mime })),
14468
+ usage: usage6,
14344
14469
  raw: payload
14345
14470
  };
14346
14471
  }
@@ -14368,7 +14493,7 @@ async function generateMuseImage(req, ctx, deps) {
14368
14493
  const payload = await runJob(ctx, deps, endpoint, input);
14369
14494
  return collectFalImages(payload, ctx, req, "fal Meta Muse");
14370
14495
  }
14371
- var IDEOGRAM_MODEL_IDS = /* @__PURE__ */ new Set(["ideogram/v4.5"]);
14496
+ var IDEOGRAM_MODEL_IDS = /* @__PURE__ */ new Set(["ideogram/v4.5", "ideogram/v4.6"]);
14372
14497
  function isIdeogram(providerModelId) {
14373
14498
  return IDEOGRAM_MODEL_IDS.has(providerModelId.toLowerCase());
14374
14499
  }
@@ -14387,10 +14512,26 @@ function parseExactSize(value) {
14387
14512
  const match = /^(\d+)x(\d+)$/.exec(value ?? "");
14388
14513
  return match ? { width: Number(match[1]), height: Number(match[2]) } : void 0;
14389
14514
  }
14390
- function ideogramImageSize(req) {
14515
+ var IDEOGRAM_46_SIZES = {
14516
+ "1:1": { "2K": "2048x2048", "4K": "4096x4096" },
14517
+ "16:9": { "2K": "2560x1440", "4K": "4096x2304" },
14518
+ "9:16": { "2K": "1440x2560", "4K": "2304x4096" },
14519
+ "4:3": { "2K": "2304x1728", "4K": "4096x3072" },
14520
+ "3:4": { "2K": "1728x2304", "4K": "3072x4096" },
14521
+ "3:2": { "2K": "2496x1664", "4K": "4096x2720" },
14522
+ "2:3": { "2K": "1664x2496", "4K": "2720x4096" },
14523
+ "5:4": { "2K": "2240x1792", "4K": "4096x3264" },
14524
+ "4:5": { "2K": "1792x2240", "4K": "3264x4096" }
14525
+ };
14526
+ function ideogramImageSize(ctx, req) {
14391
14527
  const exact = parseExactSize(req.exactSize);
14392
14528
  if (exact) return exact;
14393
14529
  if (req.aspectRatio === void 0 || req.aspectRatio === "adaptive") return void 0;
14530
+ if (ctx.model.providerModelId.toLowerCase() === "ideogram/v4.6") {
14531
+ return parseExactSize(
14532
+ IDEOGRAM_46_SIZES[req.aspectRatio]?.[req.size === "4K" ? "4K" : "2K"]
14533
+ );
14534
+ }
14394
14535
  return parseExactSize(IDEOGRAM_SIZES[req.aspectRatio]?.[req.size === "1K" ? "1K" : "2K"]);
14395
14536
  }
14396
14537
  async function generateIdeogramImage(req, ctx, deps) {
@@ -14402,7 +14543,7 @@ async function generateIdeogramImage(req, ctx, deps) {
14402
14543
  let endpoint = id;
14403
14544
  if (source2 === void 0) {
14404
14545
  input.enable_prompt_expansion = false;
14405
- const size = ideogramImageSize(req);
14546
+ const size = ideogramImageSize(ctx, req);
14406
14547
  if (size) input.image_size = size;
14407
14548
  } else {
14408
14549
  endpoint = `${id}/edit`;