{"version":3,"file":"helpers.cjs","names":[],"sources":["../../../../src/batteries/generation/transformers_js/helpers.ts"],"sourcesContent":["/**\n * Env-branched PNG-encoding helper for transformers.js Generation's `RawImage` results.\n *\n * @module @nhtio/adk/batteries/generation/transformers_js/helpers\n *\n * @remarks\n * `generate_images()` resolves an array of `RawImage` instances (raw decoded pixel data, not encoded\n * bytes). Encoding those to PNG is environment-branched in the installed `@huggingface/transformers`\n * source (`src/utils/image.js`): browser/web-worker → `await img.toBlob('image/png')` (throws outside\n * a web env); Node/other → `img.toSharp().png().toBuffer()` (its `toSharp()` throws *inside* a web\n * env). Mirrors `RawImage.save()`'s own branch (`apis.IS_WEB_ENV` there) — but this module cannot\n * import `apis` itself: `@huggingface/transformers`'s package root only re-exports `env`/`LogLevel`\n * from `env.js` (verified against `src/transformers.js`'s export list), not `apis`. A local,\n * structurally-equivalent environment check (`typeof window !== 'undefined' && typeof\n * window.document !== 'undefined'`, matching `IS_BROWSER_ENV`'s own definition in `env.js`) stands in\n * for it, with a try/catch fallback to the other branch as a safety net for exotic runtimes (e.g. a\n * web worker, which has no `window` but is still a \"web env\" per `apis.IS_WEB_ENV`).\n */\n\nconst isBrowserLikeEnv = (): boolean =>\n  typeof window !== 'undefined' &&\n  typeof (window as { document?: unknown }).document !== 'undefined'\n\n/** The exact `toBlob`/`toSharp` surface this helper reads off a resolved `RawImage`-like value. */\nexport interface RawImageLike {\n  /** Browser-only: encodes the image to a `Blob` of the given MIME type. Throws outside a web env. */\n  toBlob?: (type?: string, quality?: number) => Promise<Blob>\n  /** Node-only: wraps the raw pixel data in a `sharp.Sharp` instance. Throws inside a web env. */\n  toSharp?: () => { png: () => { toBuffer: () => Promise<Buffer> } }\n}\n\nconst viaBlob = async (image: RawImageLike): Promise<Uint8Array> => {\n  const blob = await image.toBlob!('image/png')\n  const buf = await blob.arrayBuffer()\n  return new Uint8Array(buf)\n}\n\nconst viaSharp = async (image: RawImageLike): Promise<Uint8Array> => {\n  const buf = await image.toSharp!().png().toBuffer()\n  return new Uint8Array(buf)\n}\n\n/**\n * Encodes a resolved `RawImage`-like value to PNG bytes.\n *\n * @remarks\n * When `encodeImage` is supplied it takes over entirely — the primary hermetic-test seam, since a\n * fake `RawImage` double need not implement `toBlob`/`toSharp` at all. Otherwise this picks the\n * browser (`toBlob`) or Node (`toSharp`) path by a local environment probe, falling back to whichever\n * method is actually present on `image` if the probe disagrees with reality (e.g. a web worker with\n * neither `window` nor `document`, but a `toBlob`-only `RawImage`).\n *\n * @param image - The `RawImage`-like value to encode (one element of `generate_images()`'s result).\n * @param encodeImage - Optional override that fully replaces the env-branch.\n * @returns The encoded PNG bytes.\n */\nexport const rawImageToEncodedBytes = async (\n  image: RawImageLike,\n  encodeImage?: (image: RawImageLike) => Promise<Uint8Array>\n): Promise<Uint8Array> => {\n  if (encodeImage) return encodeImage(image)\n  if (isBrowserLikeEnv()) {\n    if (typeof image.toBlob === 'function') return viaBlob(image)\n    if (typeof image.toSharp === 'function') return viaSharp(image)\n  } else {\n    if (typeof image.toSharp === 'function') return viaSharp(image)\n    if (typeof image.toBlob === 'function') return viaBlob(image)\n  }\n  throw new Error(\n    'the resolved image exposes neither toBlob() nor toSharp() — cannot encode PNG bytes'\n  )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAmBA,IAAM,yBACJ,OAAO,WAAW,eAClB,OAAQ,OAAkC,aAAa;AAUzD,IAAM,UAAU,OAAO,UAA6C;CAElE,MAAM,MAAM,OAAM,MADC,MAAM,OAAQ,WAAW,GACrB,YAAY;CACnC,OAAO,IAAI,WAAW,GAAG;AAC3B;AAEA,IAAM,WAAW,OAAO,UAA6C;CACnE,MAAM,MAAM,MAAM,MAAM,QAAS,EAAE,IAAI,EAAE,SAAS;CAClD,OAAO,IAAI,WAAW,GAAG;AAC3B;;;;;;;;;;;;;;;AAgBA,IAAa,yBAAyB,OACpC,OACA,gBACwB;CACxB,IAAI,aAAa,OAAO,YAAY,KAAK;CACzC,IAAI,iBAAiB,GAAG;EACtB,IAAI,OAAO,MAAM,WAAW,YAAY,OAAO,QAAQ,KAAK;EAC5D,IAAI,OAAO,MAAM,YAAY,YAAY,OAAO,SAAS,KAAK;CAChE,OAAO;EACL,IAAI,OAAO,MAAM,YAAY,YAAY,OAAO,SAAS,KAAK;EAC9D,IAAI,OAAO,MAAM,WAAW,YAAY,OAAO,QAAQ,KAAK;CAC9D;CACA,MAAM,IAAI,MACR,qFACF;AACF"}