@silmaril-security/sdk 0.11.1 → 0.11.3

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.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { H as HookLabel, B as BlockResult, R as ReviewLevel } from './firewall-BD4YdAmB.cjs';
2
- export { A as ALL_HOOKS, C as ClassificationMetadata, a as ClassifyBatchOptions, b as ClassifyEvent, c as ClassifyOptions, D as DEFAULT_HOOKS, d as DEFAULT_TIMEOUT_MS, F as FIREWALL_HOOK_TO_LABEL, e as Firewall, f as FirewallHook, g as FirewallMiddleware, h as FirewallMode, i as FirewallOptions, G as GovernanceAction, j as GovernanceContext, k as GovernanceDecision, l as GovernanceResource, m as GovernanceResourceKind, n as HARMFUL_OUTCOMES, o as HarmfulOutcome, I as INPUT_HOOKS, K as KnownHarmfulOutcome, p as KnownPrimaryOutcome, L as LangChainAdapterOptions, q as LangChainFirewallHandler, M as MiddlewareOptions, O as OUTCOME_DESCRIPTIONS, r as OUTPUT_HOOKS, s as Outcome, P as PRIMARY_OUTCOMES, t as Prediction, u as PrimaryOutcome, v as ProtectOptions, U as UnknownOutcome, w as isHarmfulOutcome, x as isPrimaryOutcome, y as normalizeHarmfulOutcome, z as normalizeHarmfulOutcomeMap, E as normalizePrimaryOutcome, J as prependHook, N as prependToolName, Q as resolveHooks } from './firewall-BD4YdAmB.cjs';
1
+ import { H as HookLabel, B as BlockResult, R as ReviewLevel } from './firewall-BZ8kfq5c.cjs';
2
+ export { A as ALL_HOOKS, C as ClassificationMetadata, a as ClassifyBatchOptions, b as ClassifyEvent, c as ClassifyOptions, D as DEFAULT_HOOKS, d as DEFAULT_TIMEOUT_MS, F as FIREWALL_HOOK_TO_LABEL, e as Firewall, f as FirewallHook, g as FirewallMiddleware, h as FirewallMode, i as FirewallOptions, G as GovernanceAction, j as GovernanceContext, k as GovernanceDecision, l as GovernanceResource, m as GovernanceResourceKind, n as HARMFUL_OUTCOMES, o as HarmfulOutcome, I as INPUT_HOOKS, K as KnownHarmfulOutcome, p as KnownPrimaryOutcome, L as LangChainAdapterOptions, q as LangChainFirewallHandler, M as MiddlewareOptions, O as OUTCOME_DESCRIPTIONS, r as OUTPUT_HOOKS, s as Outcome, P as PRIMARY_OUTCOMES, t as Prediction, u as PrimaryOutcome, v as ProtectOptions, U as UnknownOutcome, w as isHarmfulOutcome, x as isPrimaryOutcome, y as normalizeHarmfulOutcome, z as normalizeHarmfulOutcomeMap, E as normalizePrimaryOutcome, J as prependHook, N as prependToolName, Q as resolveHooks } from './firewall-BZ8kfq5c.cjs';
3
3
 
4
4
  interface MalformedInputDetails {
5
5
  field?: string;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { H as HookLabel, B as BlockResult, R as ReviewLevel } from './firewall-BD4YdAmB.js';
2
- export { A as ALL_HOOKS, C as ClassificationMetadata, a as ClassifyBatchOptions, b as ClassifyEvent, c as ClassifyOptions, D as DEFAULT_HOOKS, d as DEFAULT_TIMEOUT_MS, F as FIREWALL_HOOK_TO_LABEL, e as Firewall, f as FirewallHook, g as FirewallMiddleware, h as FirewallMode, i as FirewallOptions, G as GovernanceAction, j as GovernanceContext, k as GovernanceDecision, l as GovernanceResource, m as GovernanceResourceKind, n as HARMFUL_OUTCOMES, o as HarmfulOutcome, I as INPUT_HOOKS, K as KnownHarmfulOutcome, p as KnownPrimaryOutcome, L as LangChainAdapterOptions, q as LangChainFirewallHandler, M as MiddlewareOptions, O as OUTCOME_DESCRIPTIONS, r as OUTPUT_HOOKS, s as Outcome, P as PRIMARY_OUTCOMES, t as Prediction, u as PrimaryOutcome, v as ProtectOptions, U as UnknownOutcome, w as isHarmfulOutcome, x as isPrimaryOutcome, y as normalizeHarmfulOutcome, z as normalizeHarmfulOutcomeMap, E as normalizePrimaryOutcome, J as prependHook, N as prependToolName, Q as resolveHooks } from './firewall-BD4YdAmB.js';
1
+ import { H as HookLabel, B as BlockResult, R as ReviewLevel } from './firewall-BZ8kfq5c.js';
2
+ export { A as ALL_HOOKS, C as ClassificationMetadata, a as ClassifyBatchOptions, b as ClassifyEvent, c as ClassifyOptions, D as DEFAULT_HOOKS, d as DEFAULT_TIMEOUT_MS, F as FIREWALL_HOOK_TO_LABEL, e as Firewall, f as FirewallHook, g as FirewallMiddleware, h as FirewallMode, i as FirewallOptions, G as GovernanceAction, j as GovernanceContext, k as GovernanceDecision, l as GovernanceResource, m as GovernanceResourceKind, n as HARMFUL_OUTCOMES, o as HarmfulOutcome, I as INPUT_HOOKS, K as KnownHarmfulOutcome, p as KnownPrimaryOutcome, L as LangChainAdapterOptions, q as LangChainFirewallHandler, M as MiddlewareOptions, O as OUTCOME_DESCRIPTIONS, r as OUTPUT_HOOKS, s as Outcome, P as PRIMARY_OUTCOMES, t as Prediction, u as PrimaryOutcome, v as ProtectOptions, U as UnknownOutcome, w as isHarmfulOutcome, x as isPrimaryOutcome, y as normalizeHarmfulOutcome, z as normalizeHarmfulOutcomeMap, E as normalizePrimaryOutcome, J as prependHook, N as prependToolName, Q as resolveHooks } from './firewall-BZ8kfq5c.js';
3
3
 
4
4
  interface MalformedInputDetails {
5
5
  field?: string;
package/dist/index.js CHANGED
@@ -1527,7 +1527,7 @@ function sanitizeText(text) {
1527
1527
  }
1528
1528
 
1529
1529
  // src/firewall.ts
1530
- var SDK_VERSION = "0.11.1";
1530
+ var SDK_VERSION = "0.11.3";
1531
1531
  var DEFAULT_TIMEOUT_MS = 1e4;
1532
1532
  var DEFAULT_MAX_RETRIES = 5;
1533
1533
  var MAX_BACKOFF_SECONDS = 30;
@@ -1853,6 +1853,32 @@ function createAttemptSignal(timeoutMs, callerSignal) {
1853
1853
  }
1854
1854
  };
1855
1855
  }
1856
+ function linkCaller(callerSignal, controller) {
1857
+ if (callerSignal === void 0) {
1858
+ return { until: (work) => Promise.resolve(work), run: (callback) => callback(), dispose: () => void 0 };
1859
+ }
1860
+ let onAbort = () => void 0;
1861
+ const cancelled = new Promise((_resolve, reject) => {
1862
+ onAbort = () => {
1863
+ reject(callerSignal.reason);
1864
+ controller.abort(callerSignal.reason);
1865
+ };
1866
+ });
1867
+ cancelled.catch(() => void 0);
1868
+ callerSignal.addEventListener("abort", onAbort, { once: true });
1869
+ return {
1870
+ until: (work) => Promise.race([work, cancelled]),
1871
+ run: (callback) => {
1872
+ try {
1873
+ return callback();
1874
+ } catch (error) {
1875
+ callerSignal.throwIfAborted();
1876
+ throw error;
1877
+ }
1878
+ },
1879
+ dispose: () => callerSignal.removeEventListener("abort", onAbort)
1880
+ };
1881
+ }
1856
1882
  function sleep(ms, signal) {
1857
1883
  return new Promise((resolve, reject) => {
1858
1884
  if (signal?.aborted) {
@@ -1916,45 +1942,66 @@ var Firewall = class {
1916
1942
  }
1917
1943
  /**
1918
1944
  * Run an operation while its input is classified and withhold a blocked result.
1919
- * The operation's signal aborts when a block arrives before it finishes.
1945
+ * The operation's signal aborts when a block arrives before it finishes, and
1946
+ * when the caller's `signal` aborts at any point before `protect()` settles.
1920
1947
  */
1921
1948
  async protect(operation, options) {
1922
1949
  const { text, output, ...request } = options;
1923
1950
  const requestedMode = validateMode(options.mode) ?? this.mode;
1951
+ const callerSignal = request.signal;
1952
+ callerSignal?.throwIfAborted();
1924
1953
  const controller = new AbortController();
1925
- if (requestedMode === "shadow" || requestedMode === "warn") {
1926
- const observed = this.observable(request);
1927
- if (observed !== void 0) {
1928
- void this.background(text, observed);
1954
+ const caller = linkCaller(callerSignal, controller);
1955
+ try {
1956
+ if (requestedMode === "shadow" || requestedMode === "warn") {
1957
+ const observed = this.observable(request);
1958
+ if (observed !== void 0) {
1959
+ void this.background(text, observed);
1960
+ }
1961
+ const result2 = await caller.until(caller.run(() => operation(controller.signal)));
1962
+ callerSignal?.throwIfAborted();
1963
+ if (observed !== void 0 && output !== void 0) {
1964
+ const generated = caller.run(() => output(result2));
1965
+ callerSignal?.throwIfAborted();
1966
+ void this.background(generated, outputRequest(observed));
1967
+ }
1968
+ return result2;
1929
1969
  }
1930
- const result2 = await operation(controller.signal);
1931
- if (observed !== void 0 && output !== void 0) {
1932
- void this.background(output(result2), outputRequest(observed));
1970
+ const input = snapshotRequest(request);
1971
+ const conversation = conversationId(input.metadata);
1972
+ const outputOptions = outputRequest(input);
1973
+ const verdict = this.pending.submit(() => this.enforce(text, input), conversation, input.signal);
1974
+ const [, result] = await caller.until(Promise.all([
1975
+ verdict.catch((error) => {
1976
+ controller.abort(error);
1977
+ throw error;
1978
+ }),
1979
+ Promise.resolve().then(() => {
1980
+ callerSignal?.throwIfAborted();
1981
+ return operation(controller.signal);
1982
+ })
1983
+ ]));
1984
+ callerSignal?.throwIfAborted();
1985
+ if (output !== void 0) {
1986
+ const generated = caller.run(() => output(result));
1987
+ callerSignal?.throwIfAborted();
1988
+ await caller.until(this.pending.submit(
1989
+ () => this.enforce(generated, outputOptions),
1990
+ conversation,
1991
+ input.signal
1992
+ ));
1933
1993
  }
1934
- return result2;
1935
- }
1936
- const input = snapshotRequest(request);
1937
- const conversation = conversationId(input.metadata);
1938
- const outputOptions = outputRequest(input);
1939
- const verdict = this.pending.submit(() => this.enforce(text, input), conversation, input.signal);
1940
- const [, result] = await Promise.all([
1941
- verdict.catch((error) => {
1942
- controller.abort(error);
1943
- throw error;
1944
- }),
1945
- Promise.resolve().then(() => operation(controller.signal))
1946
- ]);
1947
- if (output !== void 0) {
1948
- const generated = output(result);
1949
- await this.pending.submit(
1950
- () => this.enforce(generated, outputOptions),
1951
- conversation,
1952
- input.signal
1953
- );
1994
+ return result;
1995
+ } finally {
1996
+ caller.dispose();
1954
1997
  }
1955
- return result;
1956
1998
  }
1957
- /** Wait for classifications still running in the background. */
1999
+ /**
2000
+ * Wait for every check queued on this client, foreground or background.
2001
+ * `protect()` and the parallel or background checks of framework adapters are
2002
+ * queued. Direct `classify()` and `classifyBatch()` calls are not, and the
2003
+ * operation that `protect()` runs is not awaited.
2004
+ */
1958
2005
  flush() {
1959
2006
  return this.pending.drain();
1960
2007
  }