@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.js CHANGED
@@ -915,6 +915,65 @@ var IMAGE_MODELS = [
915
915
  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."
916
916
  }
917
917
  },
918
+ {
919
+ // Generation only: fal charges edits more, so editing is its own row
920
+ // (ideogram-4.6-edit) with its own prices. fal bills any output above
921
+ // 2048x2048 in area at the 4K price; very_low renders at most 2K, so it is
922
+ // not exposed (no 4K price exists for it, and the catalog seed gate needs
923
+ // every size x quality priced). https://fal.ai/models/ideogram/v4.6
924
+ id: "ideogram-4.6",
925
+ aliases: [],
926
+ label: "Ideogram 4.6",
927
+ type: "image",
928
+ adapter: "fal",
929
+ providerModelId: "ideogram/v4.6",
930
+ lifecycle: "active",
931
+ exposure: ["cowork", "space_builder"],
932
+ constraints: {
933
+ aspectRatios: ["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "5:4", "4:5"],
934
+ sizes: ["2K", "4K"],
935
+ qualityOptions: ["auto", "low", "medium", "high"],
936
+ references: { images: 0 },
937
+ supports: ["quality", "count", "seed"]
938
+ },
939
+ defaults: { size: "2K", aspectRatio: "1:1", quality: "medium", count: 1 },
940
+ usage: { ...IMAGE_USAGE3, slug: "ideogram" },
941
+ pricingKind: "image_size",
942
+ display: {
943
+ provider: "ideogram",
944
+ description: "Ideogram 4.6 text-to-image from a prompt, at 2K or 4K. Edits go to Ideogram 4.6 Edit."
945
+ }
946
+ },
947
+ {
948
+ // Precise Edit only: references switch to the /edit endpoint, where the
949
+ // first is the image to edit and the rest (up to 4) guide it, so 5 images
950
+ // in all. The output keeps the source size, so `size` only picks the
951
+ // billed tier. Same provider model as the generate row, whose adapter
952
+ // routes on references. https://fal.ai/models/ideogram/v4.6/edit
953
+ id: "ideogram-4.6-edit",
954
+ aliases: [],
955
+ label: "Ideogram 4.6 Edit",
956
+ type: "image",
957
+ adapter: "fal",
958
+ providerModelId: "ideogram/v4.6",
959
+ lifecycle: "active",
960
+ exposure: ["cowork", "space_builder"],
961
+ constraints: {
962
+ aspectRatios: ["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "5:4", "4:5"],
963
+ sizes: ["2K", "4K"],
964
+ qualityOptions: ["auto", "low", "medium", "high"],
965
+ references: { images: 5 },
966
+ requires: ["references"],
967
+ supports: ["quality", "count", "seed"]
968
+ },
969
+ defaults: { size: "2K", aspectRatio: "1:1", quality: "medium", count: 1 },
970
+ usage: { ...IMAGE_USAGE3, slug: "ideogram" },
971
+ pricingKind: "image_size",
972
+ display: {
973
+ provider: "ideogram",
974
+ 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."
975
+ }
976
+ },
918
977
  {
919
978
  id: "seedream-5-0-pro",
920
979
  aliases: ["seedream-4-5"],
@@ -9514,6 +9573,17 @@ function falImageSpec(providerModelId) {
9514
9573
  }
9515
9574
  var AREA_1K = 1024 * 1024;
9516
9575
  var AREA_2K = 2048 * 2048;
9576
+ var IDEOGRAM_46_EXACT = {
9577
+ minSide: 256,
9578
+ maxSide: 4096,
9579
+ minArea: 256 * 256,
9580
+ maxArea: 4096 * 4096,
9581
+ multipleOf: 32,
9582
+ maxAspect: 6,
9583
+ minTier: "2K",
9584
+ topTier: "4K"
9585
+ };
9586
+ var IDEOGRAM_46_IDS = /* @__PURE__ */ new Set(["ideogram/v4.6"]);
9517
9587
  function checkExactSize(exact, b) {
9518
9588
  const match = /^(\d+)x(\d+)$/.exec(exact);
9519
9589
  if (match === null) {
@@ -9537,7 +9607,7 @@ function checkExactSize(exact, b) {
9537
9607
  return { ok: true, size: { width, height, tier: exactTier(area, b) } };
9538
9608
  }
9539
9609
  function exactTier(area, b) {
9540
- if (area <= AREA_1K) return "1K";
9610
+ if (area <= AREA_1K) return b.minTier ?? "1K";
9541
9611
  if (b.topTier === void 0 || area <= AREA_2K) return "2K";
9542
9612
  return b.topTier;
9543
9613
  }
@@ -9599,23 +9669,43 @@ function falImageProblem(spec, req) {
9599
9669
  }
9600
9670
  function falExactSize(spec, req) {
9601
9671
  if (req.exactSize === void 0 || spec.exactSize === void 0) return void 0;
9602
- const check = checkExactSize(req.exactSize, spec.exactSize);
9672
+ return checkedExactSize(req.exactSize, spec.exactSize);
9673
+ }
9674
+ function checkedExactSize(exact, bounds) {
9675
+ const check = checkExactSize(exact, bounds);
9603
9676
  return check.ok ? check.size : void 0;
9604
9677
  }
9678
+ function ideogram46ExactSize(model, req) {
9679
+ if (req.exactSize === void 0) return void 0;
9680
+ const check = checkExactSize(req.exactSize, IDEOGRAM_46_EXACT);
9681
+ if (check.ok) return check.size;
9682
+ throw new ModelKitError({
9683
+ code: "unsupported_option",
9684
+ message: `${model.label}: ${check.message}`,
9685
+ model: model.id,
9686
+ retryable: false
9687
+ });
9688
+ }
9605
9689
  function applyFalImageRules(model, req, callerSize, warnings) {
9606
9690
  if (model.adapter !== "fal" && model.adapter !== "alibaba") return;
9607
9691
  const spec = falImageSpec(model.providerModelId);
9608
- if (spec === void 0) return;
9609
- const problem = falImageProblem(spec, withPromptForResolve(req));
9610
- if (problem !== void 0) {
9611
- throw new ModelKitError({
9612
- code: problem.code,
9613
- message: problem.message,
9614
- model: model.id,
9615
- retryable: false
9616
- });
9692
+ const ideogram46 = IDEOGRAM_46_IDS.has(model.providerModelId.toLowerCase());
9693
+ if (spec === void 0 && !ideogram46) return;
9694
+ let exact;
9695
+ if (spec === void 0) {
9696
+ exact = ideogram46ExactSize(model, req);
9697
+ } else {
9698
+ const problem = falImageProblem(spec, withPromptForResolve(req));
9699
+ if (problem !== void 0) {
9700
+ throw new ModelKitError({
9701
+ code: problem.code,
9702
+ message: problem.message,
9703
+ model: model.id,
9704
+ retryable: false
9705
+ });
9706
+ }
9707
+ exact = falExactSize(spec, req);
9617
9708
  }
9618
- const exact = falExactSize(spec, req);
9619
9709
  if (exact === void 0) return;
9620
9710
  if (callerSize !== void 0 && callerSize !== exact.tier) {
9621
9711
  warnings.push({
@@ -12582,6 +12672,17 @@ async function generateFalMask(req, ctx, run) {
12582
12672
  return result;
12583
12673
  }
12584
12674
 
12675
+ // src/adapters/png-size.ts
12676
+ var PNG_SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
12677
+ function pngDimensions(bytes) {
12678
+ if (bytes.length < 24) return void 0;
12679
+ if (PNG_SIGNATURE.some((byte, index) => bytes[index] !== byte)) return void 0;
12680
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
12681
+ const width = view.getUint32(16);
12682
+ const height = view.getUint32(20);
12683
+ return width > 0 && height > 0 ? { width, height } : void 0;
12684
+ }
12685
+
12585
12686
  // src/adapters/fal-audio-plan.ts
12586
12687
  import { z as z19 } from "zod";
12587
12688
  var ELEVENLABS_MUSIC = FAL_AUDIO.elevenLabsMusic;
@@ -14159,20 +14260,39 @@ var museImageResultSchema = z23.object({
14159
14260
  images: z23.array(
14160
14261
  z23.object({
14161
14262
  url: z23.string().optional(),
14162
- content_type: z23.string().nullable().optional()
14263
+ content_type: z23.string().nullable().optional(),
14264
+ // Some fal image endpoints report the delivered dimensions (Kling); Ideogram
14265
+ // 4.6's File schema reports none, so its files are measured instead.
14266
+ width: z23.number().int().positive().optional(),
14267
+ height: z23.number().int().positive().optional()
14163
14268
  })
14164
14269
  ).optional()
14165
14270
  });
14271
+ var IDEOGRAM_46_ID = "ideogram/v4.6";
14272
+ var IDEOGRAM_46_4K_AREA = 2048 * 2048;
14273
+ function isIdeogram46(providerModelId) {
14274
+ return providerModelId.toLowerCase() === IDEOGRAM_46_ID;
14275
+ }
14276
+ function ideogramBilledSize(delivered, fallback) {
14277
+ const areas = delivered.flatMap((image) => {
14278
+ const size = image.width !== void 0 && image.height !== void 0 ? { width: image.width, height: image.height } : pngDimensions(image.bytes);
14279
+ return size === void 0 ? [] : [size.width * size.height];
14280
+ });
14281
+ if (areas.length === 0) return fallback;
14282
+ return Math.max(...areas) > IDEOGRAM_46_4K_AREA ? "4K" : "2K";
14283
+ }
14166
14284
  async function collectFalImages(payload, ctx, req, label) {
14167
14285
  const parsed = museImageResultSchema.safeParse(payload);
14168
14286
  const images = parsed.success ? parsed.data.images ?? [] : [];
14169
- const files = await Promise.all(
14170
- images.flatMap((img) => {
14171
- if (img.url === void 0) return [];
14172
- return [decodeImage(ctx, img.url, img.content_type ?? void 0)];
14287
+ const downloaded = await Promise.all(
14288
+ images.map(async (image) => {
14289
+ if (image.url === void 0) return void 0;
14290
+ const file = await decodeImage(ctx, image.url, image.content_type ?? void 0);
14291
+ return image.width !== void 0 && image.height !== void 0 ? { ...file, width: image.width, height: image.height } : file;
14173
14292
  })
14174
14293
  );
14175
- if (files.length === 0) {
14294
+ const delivered = downloaded.filter((image) => image !== void 0);
14295
+ if (delivered.length === 0) {
14176
14296
  throw new ModelKitError({
14177
14297
  code: "provider_rejected",
14178
14298
  message: `${label} returned no image`,
@@ -14181,9 +14301,14 @@ async function collectFalImages(payload, ctx, req, label) {
14181
14301
  retryable: false
14182
14302
  });
14183
14303
  }
14304
+ const usage6 = imageUsage(ctx, req, delivered.length);
14305
+ if (isIdeogram46(ctx.model.providerModelId)) {
14306
+ const billed = ideogramBilledSize(delivered, req.size);
14307
+ if (billed !== void 0) usage6.imageSize = billed;
14308
+ }
14184
14309
  return {
14185
- images: files.map((f) => ({ bytes: f.bytes, mime: f.mime })),
14186
- usage: imageUsage(ctx, req, files.length),
14310
+ images: delivered.map(({ bytes, mime }) => ({ bytes, mime })),
14311
+ usage: usage6,
14187
14312
  raw: payload
14188
14313
  };
14189
14314
  }
@@ -14211,7 +14336,7 @@ async function generateMuseImage(req, ctx, deps) {
14211
14336
  const payload = await runJob(ctx, deps, endpoint, input);
14212
14337
  return collectFalImages(payload, ctx, req, "fal Meta Muse");
14213
14338
  }
14214
- var IDEOGRAM_MODEL_IDS = /* @__PURE__ */ new Set(["ideogram/v4.5"]);
14339
+ var IDEOGRAM_MODEL_IDS = /* @__PURE__ */ new Set(["ideogram/v4.5", "ideogram/v4.6"]);
14215
14340
  function isIdeogram(providerModelId) {
14216
14341
  return IDEOGRAM_MODEL_IDS.has(providerModelId.toLowerCase());
14217
14342
  }
@@ -14230,10 +14355,26 @@ function parseExactSize(value) {
14230
14355
  const match = /^(\d+)x(\d+)$/.exec(value ?? "");
14231
14356
  return match ? { width: Number(match[1]), height: Number(match[2]) } : void 0;
14232
14357
  }
14233
- function ideogramImageSize(req) {
14358
+ var IDEOGRAM_46_SIZES = {
14359
+ "1:1": { "2K": "2048x2048", "4K": "4096x4096" },
14360
+ "16:9": { "2K": "2560x1440", "4K": "4096x2304" },
14361
+ "9:16": { "2K": "1440x2560", "4K": "2304x4096" },
14362
+ "4:3": { "2K": "2304x1728", "4K": "4096x3072" },
14363
+ "3:4": { "2K": "1728x2304", "4K": "3072x4096" },
14364
+ "3:2": { "2K": "2496x1664", "4K": "4096x2720" },
14365
+ "2:3": { "2K": "1664x2496", "4K": "2720x4096" },
14366
+ "5:4": { "2K": "2240x1792", "4K": "4096x3264" },
14367
+ "4:5": { "2K": "1792x2240", "4K": "3264x4096" }
14368
+ };
14369
+ function ideogramImageSize(ctx, req) {
14234
14370
  const exact = parseExactSize(req.exactSize);
14235
14371
  if (exact) return exact;
14236
14372
  if (req.aspectRatio === void 0 || req.aspectRatio === "adaptive") return void 0;
14373
+ if (ctx.model.providerModelId.toLowerCase() === "ideogram/v4.6") {
14374
+ return parseExactSize(
14375
+ IDEOGRAM_46_SIZES[req.aspectRatio]?.[req.size === "4K" ? "4K" : "2K"]
14376
+ );
14377
+ }
14237
14378
  return parseExactSize(IDEOGRAM_SIZES[req.aspectRatio]?.[req.size === "1K" ? "1K" : "2K"]);
14238
14379
  }
14239
14380
  async function generateIdeogramImage(req, ctx, deps) {
@@ -14245,7 +14386,7 @@ async function generateIdeogramImage(req, ctx, deps) {
14245
14386
  let endpoint = id;
14246
14387
  if (source2 === void 0) {
14247
14388
  input.enable_prompt_expansion = false;
14248
- const size = ideogramImageSize(req);
14389
+ const size = ideogramImageSize(ctx, req);
14249
14390
  if (size) input.image_size = size;
14250
14391
  } else {
14251
14392
  endpoint = `${id}/edit`;