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.d.mts CHANGED
@@ -73,6 +73,15 @@ interface ChatCompletionMessageParam {
73
73
  tool_call_id?: string;
74
74
  /** On an assistant message you replay back, the calls it made. */
75
75
  tool_calls?: ChatCompletionMessageToolCall[];
76
+ /**
77
+ * On an assistant message you replay back, the reasoning it arrived with.
78
+ * Models that use earlier thinking keep track of their plan with it across
79
+ * tool calls; others ignore it. Accepts `null` so a returned
80
+ * {@link ChatCompletionMessage} can be appended to `messages` as it is.
81
+ */
82
+ reasoning_content?: string | null;
83
+ /** Alias of {@link ChatCompletionMessageParam.reasoning_content}; that one wins if both are set. */
84
+ reasoning?: string | null;
76
85
  }
77
86
  interface Choice {
78
87
  index: number;
@@ -626,6 +635,78 @@ declare class Models {
626
635
  list(): Promise<Model[]>;
627
636
  }
628
637
 
638
+ /** A text part of a multi-part moderation input. */
639
+ interface ModerationTextInput {
640
+ type: "text";
641
+ text: string;
642
+ }
643
+ /** An image part of a multi-part moderation input (a `data:` or `https://` URL). */
644
+ interface ModerationImageURLInput {
645
+ type: "image_url";
646
+ image_url: {
647
+ url: string;
648
+ };
649
+ }
650
+ type ModerationInputPart = ModerationTextInput | ModerationImageURLInput;
651
+ /**
652
+ * What to check: a string (one result), an array of 1 to 32 strings (one
653
+ * result each, in order) or an array of 1 to 32 parts (one result for the
654
+ * combined content). Strings and parts can't be mixed.
655
+ */
656
+ type ModerationInput = string | string[] | ModerationInputPart[];
657
+ interface ModerationCreateParams {
658
+ /** A moderation model id, e.g. `"llama-guard3:1b"`. */
659
+ model: string;
660
+ input: ModerationInput;
661
+ /** Pin to a specific worker (sent as the `X-Pendra-Worker-Id` header). */
662
+ worker_id?: string | null;
663
+ }
664
+ interface ModerationResult {
665
+ /** True when any category is set, including a borderline hit. */
666
+ flagged: boolean;
667
+ /** Only the categories the model checks are present: a missing key means "not checked". */
668
+ categories: Record<string, boolean>;
669
+ /** 1.0 flagged, 0.0 checked and clean, 0.5 a borderline hit. */
670
+ category_scores: Record<string, number>;
671
+ category_applied_input_types: Record<string, Array<"text" | "image">>;
672
+ }
673
+ /** OpenAI-compatible moderation response. */
674
+ interface ModerationResponse {
675
+ id: string;
676
+ model: string;
677
+ results: ModerationResult[];
678
+ /**
679
+ * Pendra's extension block — `pendra?.worker` names the worker that served
680
+ * the request. Absent when the API didn't report one.
681
+ */
682
+ pendra?: PendraExtension | null;
683
+ }
684
+
685
+ declare class Moderations {
686
+ private client;
687
+ constructor(client: Pendra);
688
+ /**
689
+ * Check content with a moderation model installed on one of your workers
690
+ * (OpenAI-compatible).
691
+ *
692
+ * `input` is a string, an array of up to 32 strings (one result each) or an
693
+ * array of up to 32 `text` / `image_url` parts checked together (one
694
+ * result). With private inference turned on, the check is end-to-end
695
+ * encrypted: Pendra sees neither the content nor the verdict.
696
+ *
697
+ * @example
698
+ * ```ts
699
+ * const result = await client.moderations.create({
700
+ * model: "llama-guard3:1b",
701
+ * input: "Hello world",
702
+ * });
703
+ * console.log(result.results[0].flagged);
704
+ * ```
705
+ */
706
+ create(params: ModerationCreateParams): Promise<ModerationResponse>;
707
+ private createEncrypted;
708
+ }
709
+
629
710
  interface RerankCreateParams {
630
711
  model: string;
631
712
  query: string;
@@ -799,9 +880,9 @@ interface PendraOptions {
799
880
  baseURL?: string;
800
881
  timeout?: number;
801
882
  /**
802
- * Enable end-to-end encrypted (private) inference. When `true`, chat and
803
- * embeddings requests are sealed to your org's workload key (TOFU-pinned on
804
- * first use) and the responses are decrypted client-side — Pendra relays only
883
+ * Enable end-to-end encrypted (private) inference. When `true`, inference
884
+ * requests (chat, embeddings, moderations and the rest) are sealed to your
885
+ * org's workload key (TOFU-pinned on first use) and the responses are decrypted client-side — Pendra relays only
805
886
  * ciphertext. Pass `{ pinnedFingerprint }` to hard-pin the org key fingerprint
806
887
  * (required in browser bundles, which cannot use the TOFU key store).
807
888
  *
@@ -822,6 +903,7 @@ declare class Pendra {
822
903
  models: Models;
823
904
  audio: Audio;
824
905
  rerank: Rerank;
906
+ moderations: Moderations;
825
907
  /** @internal Whether end-to-end encrypted inference is enabled for this client. */
826
908
  readonly privateInferenceEnabled: boolean;
827
909
  private _piEngine;
@@ -946,4 +1028,4 @@ declare class APIStreamError extends APIError {
946
1028
  });
947
1029
  }
948
1030
 
949
- export { APIConnectionError, APIError, APIResponseValidationError, APIStatusError, type APIStatusErrorOptions, APIStreamError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionContentPart, type ChatCompletionCreateParams, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionMessageToolCall, type Choice, type ChoiceDelta, type CompletionTokensDetails, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraExtension, type PendraNotice, type PendraOptions, type PendraTiming, type PendraWebToolStep, type PendraWorker, PrivateInferenceError, RateLimitError, type RerankCreateParams, type RerankResponse, type RerankResult, type RerankResultDocument, type SpeechCreateParams, type SpeechResponse, Stream, type StreamChoice, type TranscriptionCreateParams, type TranscriptionResponse, type TranscriptionSegment, type Usage, Pendra as default };
1031
+ export { APIConnectionError, APIError, APIResponseValidationError, APIStatusError, type APIStatusErrorOptions, APIStreamError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionContentPart, type ChatCompletionCreateParams, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionMessageToolCall, type Choice, type ChoiceDelta, type CompletionTokensDetails, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, type ModerationCreateParams, type ModerationImageURLInput, type ModerationInput, type ModerationInputPart, type ModerationResponse, type ModerationResult, type ModerationTextInput, Pendra, type PendraExtension, type PendraNotice, type PendraOptions, type PendraTiming, type PendraWebToolStep, type PendraWorker, PrivateInferenceError, RateLimitError, type RerankCreateParams, type RerankResponse, type RerankResult, type RerankResultDocument, type SpeechCreateParams, type SpeechResponse, Stream, type StreamChoice, type TranscriptionCreateParams, type TranscriptionResponse, type TranscriptionSegment, type Usage, Pendra as default };
package/dist/index.d.ts CHANGED
@@ -73,6 +73,15 @@ interface ChatCompletionMessageParam {
73
73
  tool_call_id?: string;
74
74
  /** On an assistant message you replay back, the calls it made. */
75
75
  tool_calls?: ChatCompletionMessageToolCall[];
76
+ /**
77
+ * On an assistant message you replay back, the reasoning it arrived with.
78
+ * Models that use earlier thinking keep track of their plan with it across
79
+ * tool calls; others ignore it. Accepts `null` so a returned
80
+ * {@link ChatCompletionMessage} can be appended to `messages` as it is.
81
+ */
82
+ reasoning_content?: string | null;
83
+ /** Alias of {@link ChatCompletionMessageParam.reasoning_content}; that one wins if both are set. */
84
+ reasoning?: string | null;
76
85
  }
77
86
  interface Choice {
78
87
  index: number;
@@ -626,6 +635,78 @@ declare class Models {
626
635
  list(): Promise<Model[]>;
627
636
  }
628
637
 
638
+ /** A text part of a multi-part moderation input. */
639
+ interface ModerationTextInput {
640
+ type: "text";
641
+ text: string;
642
+ }
643
+ /** An image part of a multi-part moderation input (a `data:` or `https://` URL). */
644
+ interface ModerationImageURLInput {
645
+ type: "image_url";
646
+ image_url: {
647
+ url: string;
648
+ };
649
+ }
650
+ type ModerationInputPart = ModerationTextInput | ModerationImageURLInput;
651
+ /**
652
+ * What to check: a string (one result), an array of 1 to 32 strings (one
653
+ * result each, in order) or an array of 1 to 32 parts (one result for the
654
+ * combined content). Strings and parts can't be mixed.
655
+ */
656
+ type ModerationInput = string | string[] | ModerationInputPart[];
657
+ interface ModerationCreateParams {
658
+ /** A moderation model id, e.g. `"llama-guard3:1b"`. */
659
+ model: string;
660
+ input: ModerationInput;
661
+ /** Pin to a specific worker (sent as the `X-Pendra-Worker-Id` header). */
662
+ worker_id?: string | null;
663
+ }
664
+ interface ModerationResult {
665
+ /** True when any category is set, including a borderline hit. */
666
+ flagged: boolean;
667
+ /** Only the categories the model checks are present: a missing key means "not checked". */
668
+ categories: Record<string, boolean>;
669
+ /** 1.0 flagged, 0.0 checked and clean, 0.5 a borderline hit. */
670
+ category_scores: Record<string, number>;
671
+ category_applied_input_types: Record<string, Array<"text" | "image">>;
672
+ }
673
+ /** OpenAI-compatible moderation response. */
674
+ interface ModerationResponse {
675
+ id: string;
676
+ model: string;
677
+ results: ModerationResult[];
678
+ /**
679
+ * Pendra's extension block — `pendra?.worker` names the worker that served
680
+ * the request. Absent when the API didn't report one.
681
+ */
682
+ pendra?: PendraExtension | null;
683
+ }
684
+
685
+ declare class Moderations {
686
+ private client;
687
+ constructor(client: Pendra);
688
+ /**
689
+ * Check content with a moderation model installed on one of your workers
690
+ * (OpenAI-compatible).
691
+ *
692
+ * `input` is a string, an array of up to 32 strings (one result each) or an
693
+ * array of up to 32 `text` / `image_url` parts checked together (one
694
+ * result). With private inference turned on, the check is end-to-end
695
+ * encrypted: Pendra sees neither the content nor the verdict.
696
+ *
697
+ * @example
698
+ * ```ts
699
+ * const result = await client.moderations.create({
700
+ * model: "llama-guard3:1b",
701
+ * input: "Hello world",
702
+ * });
703
+ * console.log(result.results[0].flagged);
704
+ * ```
705
+ */
706
+ create(params: ModerationCreateParams): Promise<ModerationResponse>;
707
+ private createEncrypted;
708
+ }
709
+
629
710
  interface RerankCreateParams {
630
711
  model: string;
631
712
  query: string;
@@ -799,9 +880,9 @@ interface PendraOptions {
799
880
  baseURL?: string;
800
881
  timeout?: number;
801
882
  /**
802
- * Enable end-to-end encrypted (private) inference. When `true`, chat and
803
- * embeddings requests are sealed to your org's workload key (TOFU-pinned on
804
- * first use) and the responses are decrypted client-side — Pendra relays only
883
+ * Enable end-to-end encrypted (private) inference. When `true`, inference
884
+ * requests (chat, embeddings, moderations and the rest) are sealed to your
885
+ * org's workload key (TOFU-pinned on first use) and the responses are decrypted client-side — Pendra relays only
805
886
  * ciphertext. Pass `{ pinnedFingerprint }` to hard-pin the org key fingerprint
806
887
  * (required in browser bundles, which cannot use the TOFU key store).
807
888
  *
@@ -822,6 +903,7 @@ declare class Pendra {
822
903
  models: Models;
823
904
  audio: Audio;
824
905
  rerank: Rerank;
906
+ moderations: Moderations;
825
907
  /** @internal Whether end-to-end encrypted inference is enabled for this client. */
826
908
  readonly privateInferenceEnabled: boolean;
827
909
  private _piEngine;
@@ -946,4 +1028,4 @@ declare class APIStreamError extends APIError {
946
1028
  });
947
1029
  }
948
1030
 
949
- export { APIConnectionError, APIError, APIResponseValidationError, APIStatusError, type APIStatusErrorOptions, APIStreamError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionContentPart, type ChatCompletionCreateParams, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionMessageToolCall, type Choice, type ChoiceDelta, type CompletionTokensDetails, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, Pendra, type PendraExtension, type PendraNotice, type PendraOptions, type PendraTiming, type PendraWebToolStep, type PendraWorker, PrivateInferenceError, RateLimitError, type RerankCreateParams, type RerankResponse, type RerankResult, type RerankResultDocument, type SpeechCreateParams, type SpeechResponse, Stream, type StreamChoice, type TranscriptionCreateParams, type TranscriptionResponse, type TranscriptionSegment, type Usage, Pendra as default };
1031
+ export { APIConnectionError, APIError, APIResponseValidationError, APIStatusError, type APIStatusErrorOptions, APIStreamError, type AudioFileInput, AuthenticationError, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionContentPart, type ChatCompletionCreateParams, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionMessageToolCall, type Choice, type ChoiceDelta, type CompletionTokensDetails, type CreateEmbeddingResponse, type Embedding, type EmbeddingCreateParams, type EmbeddingUsage, type ImageData, type ImageGenerationCreateParams, type ImageResponse, type Model, type ModerationCreateParams, type ModerationImageURLInput, type ModerationInput, type ModerationInputPart, type ModerationResponse, type ModerationResult, type ModerationTextInput, Pendra, type PendraExtension, type PendraNotice, type PendraOptions, type PendraTiming, type PendraWebToolStep, type PendraWorker, PrivateInferenceError, RateLimitError, type RerankCreateParams, type RerankResponse, type RerankResult, type RerankResultDocument, type SpeechCreateParams, type SpeechResponse, Stream, type StreamChoice, type TranscriptionCreateParams, type TranscriptionResponse, type TranscriptionSegment, type Usage, Pendra as default };
package/dist/index.js CHANGED
@@ -1760,10 +1760,130 @@ var Models = class {
1760
1760
  }
1761
1761
  };
1762
1762
 
1763
- // src/resources/rerank.ts
1763
+ // src/resources/moderations.ts
1764
1764
  var ENCODER5 = new TextEncoder();
1765
1765
  var DECODER6 = new TextDecoder();
1766
+ var MAX_INPUTS = 32;
1767
+ function isPlainObject2(v) {
1768
+ return typeof v === "object" && v !== null && !Array.isArray(v);
1769
+ }
1770
+ function validatePart(part) {
1771
+ if (!isPlainObject2(part)) {
1772
+ throw new Error("input can't mix strings and parts");
1773
+ }
1774
+ if (part.type === "text") {
1775
+ if (typeof part.text !== "string" || part.text.length === 0) {
1776
+ throw new Error("a text part needs a non-empty text");
1777
+ }
1778
+ return;
1779
+ }
1780
+ if (part.type === "image_url") {
1781
+ const url = isPlainObject2(part.image_url) ? part.image_url.url : void 0;
1782
+ if (typeof url !== "string" || url.length === 0) {
1783
+ throw new Error("an image_url part needs image_url.url");
1784
+ }
1785
+ return;
1786
+ }
1787
+ throw new Error("parts must be of type text or image_url");
1788
+ }
1766
1789
  function validateParams3(params) {
1790
+ if (!params.model || typeof params.model !== "string") {
1791
+ throw new Error("model must be a non-empty string");
1792
+ }
1793
+ const input = params.input;
1794
+ if (typeof input === "string") {
1795
+ if (input.length === 0) throw new Error("input must be a non-empty string");
1796
+ return;
1797
+ }
1798
+ if (!Array.isArray(input)) {
1799
+ throw new Error("input must be a string, an array of strings, or an array of text and image_url parts");
1800
+ }
1801
+ if (input.length === 0 || input.length > MAX_INPUTS) {
1802
+ throw new Error(`input must hold 1 to ${MAX_INPUTS} items`);
1803
+ }
1804
+ if (input.every((i) => typeof i === "string")) {
1805
+ if (!input.every((s) => s.length > 0)) {
1806
+ throw new Error("input strings must be non-empty");
1807
+ }
1808
+ return;
1809
+ }
1810
+ input.forEach(validatePart);
1811
+ }
1812
+ function asModeration(parsed, status, label) {
1813
+ if (!isPlainObject2(parsed) || !Array.isArray(parsed.results)) {
1814
+ throw new APIResponseValidationError(`${label} was not a moderation response`, {
1815
+ status,
1816
+ body: JSON.stringify(parsed)
1817
+ });
1818
+ }
1819
+ return parsed;
1820
+ }
1821
+ var Moderations = class {
1822
+ constructor(client) {
1823
+ this.client = client;
1824
+ }
1825
+ /**
1826
+ * Check content with a moderation model installed on one of your workers
1827
+ * (OpenAI-compatible).
1828
+ *
1829
+ * `input` is a string, an array of up to 32 strings (one result each) or an
1830
+ * array of up to 32 `text` / `image_url` parts checked together (one
1831
+ * result). With private inference turned on, the check is end-to-end
1832
+ * encrypted: Pendra sees neither the content nor the verdict.
1833
+ *
1834
+ * @example
1835
+ * ```ts
1836
+ * const result = await client.moderations.create({
1837
+ * model: "llama-guard3:1b",
1838
+ * input: "Hello world",
1839
+ * });
1840
+ * console.log(result.results[0].flagged);
1841
+ * ```
1842
+ */
1843
+ async create(params) {
1844
+ validateParams3(params);
1845
+ const body = {
1846
+ model: params.model,
1847
+ input: params.input
1848
+ };
1849
+ const headers = params.worker_id ? { "X-Pendra-Worker-Id": params.worker_id } : void 0;
1850
+ if (this.client.privateInferenceEnabled) {
1851
+ return this.createEncrypted(params.model, body, headers);
1852
+ }
1853
+ const { response } = await this.client._fetch(
1854
+ "/api/v1/moderations",
1855
+ { method: "POST", body, headers }
1856
+ );
1857
+ const result = asModeration(await readJsonReply(response), response.status, "Response");
1858
+ return withPlacement(result, response.headers, result.pendra);
1859
+ }
1860
+ async createEncrypted(model, body, headers) {
1861
+ const engine = await this.client._privateEngine();
1862
+ const { topLevel, decryptor } = engine.seal({
1863
+ model,
1864
+ stream: false,
1865
+ taskType: "moderation",
1866
+ body: ENCODER5.encode(JSON.stringify({ model: body.model, stream: false, input: body.input }))
1867
+ });
1868
+ const { response } = await this.client._fetch(
1869
+ "/api/v1/moderations",
1870
+ { method: "POST", body: topLevel, headers }
1871
+ );
1872
+ const { inner, placement } = await readSealedReply(response);
1873
+ const plain = decryptor.openResult(inner);
1874
+ const result = asModeration(
1875
+ parseDecryptedJson(DECODER6.decode(plain), response.status),
1876
+ response.status,
1877
+ "Decrypted response"
1878
+ );
1879
+ return withPlacement(result, response.headers, result.pendra, placement);
1880
+ }
1881
+ };
1882
+
1883
+ // src/resources/rerank.ts
1884
+ var ENCODER6 = new TextEncoder();
1885
+ var DECODER7 = new TextDecoder();
1886
+ function validateParams4(params) {
1767
1887
  if (!params.model || typeof params.model !== "string") {
1768
1888
  throw new Error("model must be a non-empty string");
1769
1889
  }
@@ -1797,7 +1917,7 @@ var Rerank = class {
1797
1917
  * ```
1798
1918
  */
1799
1919
  async create(params) {
1800
- validateParams3(params);
1920
+ validateParams4(params);
1801
1921
  const body = {
1802
1922
  model: params.model,
1803
1923
  query: params.query,
@@ -1822,7 +1942,7 @@ var Rerank = class {
1822
1942
  model,
1823
1943
  stream: false,
1824
1944
  taskType: "rerank",
1825
- body: ENCODER5.encode(JSON.stringify(body))
1945
+ body: ENCODER6.encode(JSON.stringify(body))
1826
1946
  });
1827
1947
  const { response } = await this.client._fetch(
1828
1948
  "/api/v1/rerank",
@@ -1830,13 +1950,13 @@ var Rerank = class {
1830
1950
  );
1831
1951
  const { inner, placement } = await readSealedReply(response);
1832
1952
  const plain = decryptor.openResult(inner);
1833
- const result = JSON.parse(DECODER6.decode(plain));
1953
+ const result = JSON.parse(DECODER7.decode(plain));
1834
1954
  return withPlacement(result, response.headers, result.pendra, placement);
1835
1955
  }
1836
1956
  };
1837
1957
 
1838
1958
  // src/version.ts
1839
- var VERSION = "0.17.3";
1959
+ var VERSION = "0.18.1";
1840
1960
  var USER_AGENT = `pendra-node/${VERSION}`;
1841
1961
 
1842
1962
  // src/client.ts
@@ -1897,6 +2017,7 @@ var Pendra = class {
1897
2017
  this.models = new Models(this);
1898
2018
  this.audio = new Audio(this);
1899
2019
  this.rerank = new Rerank(this);
2020
+ this.moderations = new Moderations(this);
1900
2021
  }
1901
2022
  /**
1902
2023
  * @internal