pendra 0.17.3 → 0.18.1

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
@@ -1714,10 +1714,130 @@ var Models = class {
1714
1714
  }
1715
1715
  };
1716
1716
 
1717
- // src/resources/rerank.ts
1717
+ // src/resources/moderations.ts
1718
1718
  var ENCODER5 = new TextEncoder();
1719
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
+ }
1720
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) {
1721
1841
  if (!params.model || typeof params.model !== "string") {
1722
1842
  throw new Error("model must be a non-empty string");
1723
1843
  }
@@ -1751,7 +1871,7 @@ var Rerank = class {
1751
1871
  * ```
1752
1872
  */
1753
1873
  async create(params) {
1754
- validateParams3(params);
1874
+ validateParams4(params);
1755
1875
  const body = {
1756
1876
  model: params.model,
1757
1877
  query: params.query,
@@ -1776,7 +1896,7 @@ var Rerank = class {
1776
1896
  model,
1777
1897
  stream: false,
1778
1898
  taskType: "rerank",
1779
- body: ENCODER5.encode(JSON.stringify(body))
1899
+ body: ENCODER6.encode(JSON.stringify(body))
1780
1900
  });
1781
1901
  const { response } = await this.client._fetch(
1782
1902
  "/api/v1/rerank",
@@ -1784,13 +1904,13 @@ var Rerank = class {
1784
1904
  );
1785
1905
  const { inner, placement } = await readSealedReply(response);
1786
1906
  const plain = decryptor.openResult(inner);
1787
- const result = JSON.parse(DECODER6.decode(plain));
1907
+ const result = JSON.parse(DECODER7.decode(plain));
1788
1908
  return withPlacement(result, response.headers, result.pendra, placement);
1789
1909
  }
1790
1910
  };
1791
1911
 
1792
1912
  // src/version.ts
1793
- var VERSION = "0.17.3";
1913
+ var VERSION = "0.18.1";
1794
1914
  var USER_AGENT = `pendra-node/${VERSION}`;
1795
1915
 
1796
1916
  // src/client.ts
@@ -1851,6 +1971,7 @@ var Pendra = class {
1851
1971
  this.models = new Models(this);
1852
1972
  this.audio = new Audio(this);
1853
1973
  this.rerank = new Rerank(this);
1974
+ this.moderations = new Moderations(this);
1854
1975
  }
1855
1976
  /**
1856
1977
  * @internal