pendra 0.17.2 → 0.18.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.mjs CHANGED
@@ -1037,17 +1037,27 @@ var MIME_EXT = {
1037
1037
  "audio/x-flac": "flac"
1038
1038
  };
1039
1039
  function extFromMime(type) {
1040
- if (!type) return "mp3";
1040
+ if (!type) return void 0;
1041
1041
  const base = type.split(";")[0].trim().toLowerCase();
1042
- return MIME_EXT[base] ?? "mp3";
1043
- }
1044
- function asBlob(file) {
1042
+ return MIME_EXT[base];
1043
+ }
1044
+ async function extFromHeader(blob) {
1045
+ const head = new Uint8Array(await blob.slice(0, 12).arrayBuffer());
1046
+ const ascii = (from, to) => String.fromCharCode(...head.subarray(from, to));
1047
+ if (head.length >= 12 && ascii(0, 4) === "RIFF" && ascii(8, 12) === "WAVE") return "wav";
1048
+ if (head.length >= 4 && ascii(0, 4) === "fLaC") return "flac";
1049
+ if (head.length >= 3 && ascii(0, 3) === "ID3") return "mp3";
1050
+ if (head.length >= 2 && head[0] === 255 && (head[1] & 224) === 224) return "mp3";
1051
+ return void 0;
1052
+ }
1053
+ async function asBlob(file) {
1045
1054
  if (file instanceof Blob) {
1046
1055
  const asFile = file;
1047
1056
  if (asFile.name) {
1048
1057
  return { blob: file, filename: asFile.name };
1049
1058
  }
1050
- return { blob: file, filename: `audio.${extFromMime(file.type)}` };
1059
+ const ext = extFromMime(file.type) ?? await extFromHeader(file);
1060
+ return { blob: file, filename: ext ? `audio.${ext}` : "audio" };
1051
1061
  }
1052
1062
  const { filename, content } = file;
1053
1063
  if (content instanceof Blob) {
@@ -1066,7 +1076,7 @@ var Transcriptions = class {
1066
1076
  if (params.beam_size != null && !(Number.isInteger(params.beam_size) && params.beam_size >= 1 && params.beam_size <= 8)) {
1067
1077
  throw new Error(`beam_size must be an integer between 1 and 8 (got ${params.beam_size})`);
1068
1078
  }
1069
- const { blob, filename } = asBlob(params.file);
1079
+ const { blob, filename } = await asBlob(params.file);
1070
1080
  if (blob.size === 0) {
1071
1081
  throw new Error("audio file is empty");
1072
1082
  }
@@ -1704,10 +1714,130 @@ var Models = class {
1704
1714
  }
1705
1715
  };
1706
1716
 
1707
- // src/resources/rerank.ts
1717
+ // src/resources/moderations.ts
1708
1718
  var ENCODER5 = new TextEncoder();
1709
1719
  var DECODER6 = new TextDecoder();
1720
+ var MAX_INPUTS = 32;
1721
+ function isPlainObject2(v) {
1722
+ return typeof v === "object" && v !== null && !Array.isArray(v);
1723
+ }
1724
+ function validatePart(part) {
1725
+ if (!isPlainObject2(part)) {
1726
+ throw new Error("input can't mix strings and parts");
1727
+ }
1728
+ if (part.type === "text") {
1729
+ if (typeof part.text !== "string" || part.text.length === 0) {
1730
+ throw new Error("a text part needs a non-empty text");
1731
+ }
1732
+ return;
1733
+ }
1734
+ if (part.type === "image_url") {
1735
+ const url = isPlainObject2(part.image_url) ? part.image_url.url : void 0;
1736
+ if (typeof url !== "string" || url.length === 0) {
1737
+ throw new Error("an image_url part needs image_url.url");
1738
+ }
1739
+ return;
1740
+ }
1741
+ throw new Error("parts must be of type text or image_url");
1742
+ }
1710
1743
  function validateParams3(params) {
1744
+ if (!params.model || typeof params.model !== "string") {
1745
+ throw new Error("model must be a non-empty string");
1746
+ }
1747
+ const input = params.input;
1748
+ if (typeof input === "string") {
1749
+ if (input.length === 0) throw new Error("input must be a non-empty string");
1750
+ return;
1751
+ }
1752
+ if (!Array.isArray(input)) {
1753
+ throw new Error("input must be a string, an array of strings, or an array of text and image_url parts");
1754
+ }
1755
+ if (input.length === 0 || input.length > MAX_INPUTS) {
1756
+ throw new Error(`input must hold 1 to ${MAX_INPUTS} items`);
1757
+ }
1758
+ if (input.every((i) => typeof i === "string")) {
1759
+ if (!input.every((s) => s.length > 0)) {
1760
+ throw new Error("input strings must be non-empty");
1761
+ }
1762
+ return;
1763
+ }
1764
+ input.forEach(validatePart);
1765
+ }
1766
+ function asModeration(parsed, status, label) {
1767
+ if (!isPlainObject2(parsed) || !Array.isArray(parsed.results)) {
1768
+ throw new APIResponseValidationError(`${label} was not a moderation response`, {
1769
+ status,
1770
+ body: JSON.stringify(parsed)
1771
+ });
1772
+ }
1773
+ return parsed;
1774
+ }
1775
+ var Moderations = class {
1776
+ constructor(client) {
1777
+ this.client = client;
1778
+ }
1779
+ /**
1780
+ * Check content with a moderation model installed on one of your workers
1781
+ * (OpenAI-compatible).
1782
+ *
1783
+ * `input` is a string, an array of up to 32 strings (one result each) or an
1784
+ * array of up to 32 `text` / `image_url` parts checked together (one
1785
+ * result). With private inference turned on, the check is end-to-end
1786
+ * encrypted: Pendra sees neither the content nor the verdict.
1787
+ *
1788
+ * @example
1789
+ * ```ts
1790
+ * const result = await client.moderations.create({
1791
+ * model: "llama-guard3:1b",
1792
+ * input: "Hello world",
1793
+ * });
1794
+ * console.log(result.results[0].flagged);
1795
+ * ```
1796
+ */
1797
+ async create(params) {
1798
+ validateParams3(params);
1799
+ const body = {
1800
+ model: params.model,
1801
+ input: params.input
1802
+ };
1803
+ const headers = params.worker_id ? { "X-Pendra-Worker-Id": params.worker_id } : void 0;
1804
+ if (this.client.privateInferenceEnabled) {
1805
+ return this.createEncrypted(params.model, body, headers);
1806
+ }
1807
+ const { response } = await this.client._fetch(
1808
+ "/api/v1/moderations",
1809
+ { method: "POST", body, headers }
1810
+ );
1811
+ const result = asModeration(await readJsonReply(response), response.status, "Response");
1812
+ return withPlacement(result, response.headers, result.pendra);
1813
+ }
1814
+ async createEncrypted(model, body, headers) {
1815
+ const engine = await this.client._privateEngine();
1816
+ const { topLevel, decryptor } = engine.seal({
1817
+ model,
1818
+ stream: false,
1819
+ taskType: "moderation",
1820
+ body: ENCODER5.encode(JSON.stringify({ model: body.model, stream: false, input: body.input }))
1821
+ });
1822
+ const { response } = await this.client._fetch(
1823
+ "/api/v1/moderations",
1824
+ { method: "POST", body: topLevel, headers }
1825
+ );
1826
+ const { inner, placement } = await readSealedReply(response);
1827
+ const plain = decryptor.openResult(inner);
1828
+ const result = asModeration(
1829
+ parseDecryptedJson(DECODER6.decode(plain), response.status),
1830
+ response.status,
1831
+ "Decrypted response"
1832
+ );
1833
+ return withPlacement(result, response.headers, result.pendra, placement);
1834
+ }
1835
+ };
1836
+
1837
+ // src/resources/rerank.ts
1838
+ var ENCODER6 = new TextEncoder();
1839
+ var DECODER7 = new TextDecoder();
1840
+ function validateParams4(params) {
1711
1841
  if (!params.model || typeof params.model !== "string") {
1712
1842
  throw new Error("model must be a non-empty string");
1713
1843
  }
@@ -1741,7 +1871,7 @@ var Rerank = class {
1741
1871
  * ```
1742
1872
  */
1743
1873
  async create(params) {
1744
- validateParams3(params);
1874
+ validateParams4(params);
1745
1875
  const body = {
1746
1876
  model: params.model,
1747
1877
  query: params.query,
@@ -1766,7 +1896,7 @@ var Rerank = class {
1766
1896
  model,
1767
1897
  stream: false,
1768
1898
  taskType: "rerank",
1769
- body: ENCODER5.encode(JSON.stringify(body))
1899
+ body: ENCODER6.encode(JSON.stringify(body))
1770
1900
  });
1771
1901
  const { response } = await this.client._fetch(
1772
1902
  "/api/v1/rerank",
@@ -1774,13 +1904,13 @@ var Rerank = class {
1774
1904
  );
1775
1905
  const { inner, placement } = await readSealedReply(response);
1776
1906
  const plain = decryptor.openResult(inner);
1777
- const result = JSON.parse(DECODER6.decode(plain));
1907
+ const result = JSON.parse(DECODER7.decode(plain));
1778
1908
  return withPlacement(result, response.headers, result.pendra, placement);
1779
1909
  }
1780
1910
  };
1781
1911
 
1782
1912
  // src/version.ts
1783
- var VERSION = "0.17.2";
1913
+ var VERSION = "0.18.0";
1784
1914
  var USER_AGENT = `pendra-node/${VERSION}`;
1785
1915
 
1786
1916
  // src/client.ts
@@ -1841,6 +1971,7 @@ var Pendra = class {
1841
1971
  this.models = new Models(this);
1842
1972
  this.audio = new Audio(this);
1843
1973
  this.rerank = new Rerank(this);
1974
+ this.moderations = new Moderations(this);
1844
1975
  }
1845
1976
  /**
1846
1977
  * @internal