@broberg/ai-sdk 0.10.2 → 0.10.4

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.d.ts CHANGED
@@ -164,6 +164,9 @@ interface ImageRequest {
164
164
  height?: number;
165
165
  /** LoRAs to merge at inference (F021) — e.g. a trained brand/style LoRA. */
166
166
  loras?: LoraWeight[];
167
+ /** F021.4 — re-roll once with a fresh seed if fal's safety-checker false-positives
168
+ * and returns a black image (has_nsfw_concepts). fal only. */
169
+ retryOnBlack?: boolean;
167
170
  }
168
171
  interface ImageResult {
169
172
  url: string;
@@ -929,6 +932,8 @@ declare const imageInputSchema: z.ZodObject<{
929
932
  }>, "many">>;
930
933
  /** Shorthand for a single LoRA — normalized to loras:[{path, scale:1}]. */
931
934
  lora: z.ZodOptional<z.ZodString>;
935
+ /** F021.4 — re-roll once if fal returns a black image (NSFW false-positive). */
936
+ retryOnBlack: z.ZodOptional<z.ZodBoolean>;
932
937
  }, "strip", z.ZodTypeAny, {
933
938
  prompt: string;
934
939
  purpose?: string | undefined;
@@ -951,6 +956,7 @@ declare const imageInputSchema: z.ZodObject<{
951
956
  labels?: Record<string, string> | undefined;
952
957
  width?: number | undefined;
953
958
  height?: number | undefined;
959
+ retryOnBlack?: boolean | undefined;
954
960
  }, {
955
961
  prompt: string;
956
962
  purpose?: string | undefined;
@@ -973,6 +979,7 @@ declare const imageInputSchema: z.ZodObject<{
973
979
  labels?: Record<string, string> | undefined;
974
980
  width?: number | undefined;
975
981
  height?: number | undefined;
982
+ retryOnBlack?: boolean | undefined;
976
983
  }>;
977
984
  declare const trainStyleInputSchema: z.ZodObject<{
978
985
  tier: z.ZodOptional<z.ZodEnum<["fast", "smart", "powerful", "cheap", "vision", "video", "embedding"]>>;
@@ -1680,8 +1687,8 @@ declare const falStubAdapter: ProviderAdapter;
1680
1687
  * wires the live adapters. */
1681
1688
  declare const stubProviders: Record<string, ProviderAdapter>;
1682
1689
 
1683
- declare const VERSION: "0.10.2";
1684
- declare const SDK_TAG: "@broberg/ai-sdk@0.10.2";
1690
+ declare const VERSION: "0.10.4";
1691
+ declare const SDK_TAG: "@broberg/ai-sdk@0.10.4";
1685
1692
 
1686
1693
  /** Built-in defaults. Every entry is overridable via AiConfig.defaults or a
1687
1694
  * per-call override. Model IDs are current at scaffold time; callers pin their
package/dist/index.js CHANGED
@@ -1317,7 +1317,13 @@ function falAdapter(config = {}) {
1317
1317
  body.loras = req.loras.map((l) => ({ path: l.path, scale: l.scale ?? 1 }));
1318
1318
  }
1319
1319
  const mode = config.mode ?? "sync";
1320
- const url = await (mode === "sync" ? runSync(req.spec.model, headers, body) : runQueue(req.spec.model, headers, body));
1320
+ const run = (b) => mode === "sync" ? runSync(req.spec.model, headers, b) : runQueue(req.spec.model, headers, b);
1321
+ let calls = 1;
1322
+ let { url, flagged } = await run(body);
1323
+ if (flagged && req.retryOnBlack) {
1324
+ calls++;
1325
+ ({ url, flagged } = await run({ ...body, seed: Math.floor(Math.random() * 1e9) }));
1326
+ }
1321
1327
  const usage = freshUsage({
1322
1328
  provider: "fal",
1323
1329
  model: req.spec.model,
@@ -1326,7 +1332,7 @@ function falAdapter(config = {}) {
1326
1332
  inputTokens: 0,
1327
1333
  outputTokens: 0
1328
1334
  });
1329
- usage.costUsd = config.pricePerImage ?? FAL_IMAGE_PRICE_ESTIMATE[req.spec.model] ?? 0;
1335
+ usage.costUsd = (config.pricePerImage ?? FAL_IMAGE_PRICE_ESTIMATE[req.spec.model] ?? 0) * calls;
1330
1336
  return { url, usage };
1331
1337
  }
1332
1338
  async function trainStyle(req) {
@@ -1408,13 +1414,13 @@ function falAdapter(config = {}) {
1408
1414
  const data = await res.json();
1409
1415
  const out = data.images?.[0]?.url;
1410
1416
  if (!out) throw new Error(`fal: no image url in response`);
1411
- return out;
1417
+ return { url: out, flagged: data.has_nsfw_concepts?.[0] === true };
1412
1418
  }
1413
1419
  async function runQueue(model, headers, body) {
1414
1420
  const result = await queueResult(model, headers, body, timeoutMs);
1415
1421
  const out = result.images?.[0]?.url;
1416
1422
  if (!out) throw new Error("fal queue: no image url in result");
1417
- return out;
1423
+ return { url: out, flagged: result.has_nsfw_concepts?.[0] === true };
1418
1424
  }
1419
1425
  async function queueResult(model, headers, body, deadlineMs) {
1420
1426
  const submitRes = await doFetch(`${queueBase}/${model}`, {
@@ -1854,6 +1860,8 @@ var imageInputSchema = z.object({
1854
1860
  loras: z.array(loraWeightSchema).optional(),
1855
1861
  /** Shorthand for a single LoRA — normalized to loras:[{path, scale:1}]. */
1856
1862
  lora: z.string().optional(),
1863
+ /** F021.4 — re-roll once if fal returns a black image (NSFW false-positive). */
1864
+ retryOnBlack: z.boolean().optional(),
1857
1865
  ...callOptions
1858
1866
  });
1859
1867
  var trainStyleInputSchema = z.object({
@@ -1972,7 +1980,12 @@ function createAI(config = {}) {
1972
1980
  }
1973
1981
  }
1974
1982
  function toMessages(input) {
1975
- if (input.messages && input.messages.length > 0) return input.messages;
1983
+ if (input.messages && input.messages.length > 0) {
1984
+ if (input.system && input.messages[0]?.role !== "system") {
1985
+ return [{ role: "system", content: input.system }, ...input.messages];
1986
+ }
1987
+ return input.messages;
1988
+ }
1976
1989
  const msgs = [];
1977
1990
  if (input.system) msgs.push({ role: "system", content: input.system });
1978
1991
  msgs.push({ role: "user", content: input.prompt ?? "" });
@@ -2183,7 +2196,8 @@ function createAI(config = {}) {
2183
2196
  spec,
2184
2197
  width: input.width,
2185
2198
  height: input.height,
2186
- loras: loras.length ? loras : void 0
2199
+ loras: loras.length ? loras : void 0,
2200
+ retryOnBlack: input.retryOnBlack
2187
2201
  });
2188
2202
  }
2189
2203
  });
@@ -2438,8 +2452,8 @@ var stubProviders = {
2438
2452
  };
2439
2453
 
2440
2454
  // src/version.ts
2441
- var VERSION = "0.10.2";
2442
- var SDK_TAG = "@broberg/ai-sdk@0.10.2";
2455
+ var VERSION = "0.10.4";
2456
+ var SDK_TAG = "@broberg/ai-sdk@0.10.4";
2443
2457
 
2444
2458
  // src/cost/budget-store.ts
2445
2459
  function sqliteBudgetStore(config) {