@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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.11.3
4
+
5
+ - Reject `protect()` with the caller's abort reason when the operation or output
6
+ extractor aborts the caller and then throws synchronously.
7
+ - Document that `flush()` waits for every check queued on the client,
8
+ foreground included, and not for direct `classify()` calls or the operation
9
+ that `protect()` runs.
10
+
11
+ ## 0.11.2
12
+
13
+ - Keep protect() responsive to caller cancellation through operation and output handling.
14
+
3
15
  ## 0.11.1
4
16
 
5
17
  - Pin the Vercel AI SDK install recipes to compatible AI SDK and OpenAI
package/README.md CHANGED
@@ -35,10 +35,10 @@ This SDK is distributed as an npm package.
35
35
  npm install @silmaril-security/sdk
36
36
  ```
37
37
 
38
- Pin version 0.11.1 for reproducible installs:
38
+ Pin version 0.11.3 for reproducible installs:
39
39
 
40
40
  ```sh
41
- npm install @silmaril-security/sdk@0.11.1
41
+ npm install @silmaril-security/sdk@0.11.3
42
42
  ```
43
43
 
44
44
  Requires Node 20 or later.
@@ -591,15 +591,24 @@ classification that is still waiting.
591
591
 
592
592
  Aborting the `signal` you pass stops a classification that is still waiting
593
593
  for its turn, rejects `protect()` with the signal's reason, and aborts your
594
- operation's signal. Classifications queued after it keep their order.
594
+ operation's signal. This holds at any point before `protect()` settles: while
595
+ the operation is running after the input verdict, while the `output` check is
596
+ pending, and in Shadow and Warn. A signal that is already aborted rejects
597
+ `protect()` before your operation starts. A block that lands first still
598
+ rejects with `FirewallBlockedException`. Classifications queued after the
599
+ aborted one keep their order, and other `protect()` calls are unaffected. In
600
+ Shadow and Warn the input check already started keeps running in the
601
+ background after an abort, and no output check is started.
595
602
 
596
603
  A background classification retries once, never rejects, logs its failure with
597
604
  `console.warn`, and does not follow the caller's `signal`. One that would
598
605
  exceed `maxQueued` unjoined checks for its conversation, or
599
606
  `maxConcurrent * maxQueued` background checks across all conversations
600
607
  (2,048 by default), is dropped with a warning. `await fw.flush()` waits for
601
- background classifications to finish, for example before a serverless handler
602
- returns.
608
+ every check queued on the client to finish, foreground or background, for
609
+ example before a serverless handler returns. `protect()` and the parallel or
610
+ background checks of framework adapters are queued. Direct `classify()` calls
611
+ are not, and `flush()` does not wait for the operation that `protect()` runs.
603
612
 
604
613
  ## Migration Notes
605
614
 
@@ -13,7 +13,7 @@ import * as langchain from 'langchain';
13
13
  import { CreateAgentParams } from 'langchain';
14
14
  import * as _langchain_core_tools from '@langchain/core/tools';
15
15
  import { StructuredTool } from '@langchain/core/tools';
16
- import { h as FirewallMode, b as ClassifyEvent, e as Firewall } from '../firewall-BD4YdAmB.cjs';
16
+ import { h as FirewallMode, b as ClassifyEvent, e as Firewall } from '../firewall-BZ8kfq5c.cjs';
17
17
 
18
18
  declare const SAFE_TOOL_MESSAGE = "Silmaril Firewall blocked this tool interaction. Choose a different safe action.";
19
19
  declare const SAFE_FINAL_MESSAGE = "Silmaril Firewall stopped this request after repeated unsafe actions.";
@@ -13,7 +13,7 @@ import * as langchain from 'langchain';
13
13
  import { CreateAgentParams } from 'langchain';
14
14
  import * as _langchain_core_tools from '@langchain/core/tools';
15
15
  import { StructuredTool } from '@langchain/core/tools';
16
- import { h as FirewallMode, b as ClassifyEvent, e as Firewall } from '../firewall-BD4YdAmB.js';
16
+ import { h as FirewallMode, b as ClassifyEvent, e as Firewall } from '../firewall-BZ8kfq5c.js';
17
17
 
18
18
  declare const SAFE_TOOL_MESSAGE = "Silmaril Firewall blocked this tool interaction. Choose a different safe action.";
19
19
  declare const SAFE_FINAL_MESSAGE = "Silmaril Firewall stopped this request after repeated unsafe actions.";
@@ -1,6 +1,6 @@
1
1
  import { RunnableConfig, Runnable, RunnableBinding } from '@langchain/core/runnables';
2
2
  import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
3
- import { e as Firewall, L as LangChainAdapterOptions } from '../firewall-BD4YdAmB.cjs';
3
+ import { e as Firewall, L as LangChainAdapterOptions } from '../firewall-BZ8kfq5c.cjs';
4
4
 
5
5
  declare function createLangChainHandler(firewall: Firewall, options?: LangChainAdapterOptions): Promise<BaseCallbackHandler>;
6
6
  /** Protect a runnable using its actual invocation config, including direct model/tool calls. */
@@ -1,6 +1,6 @@
1
1
  import { RunnableConfig, Runnable, RunnableBinding } from '@langchain/core/runnables';
2
2
  import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
3
- import { e as Firewall, L as LangChainAdapterOptions } from '../firewall-BD4YdAmB.js';
3
+ import { e as Firewall, L as LangChainAdapterOptions } from '../firewall-BZ8kfq5c.js';
4
4
 
5
5
  declare function createLangChainHandler(firewall: Firewall, options?: LangChainAdapterOptions): Promise<BaseCallbackHandler>;
6
6
  /** Protect a runnable using its actual invocation config, including direct model/tool calls. */
@@ -1,4 +1,4 @@
1
- import { M as MiddlewareOptions, e as Firewall } from '../firewall-BD4YdAmB.cjs';
1
+ import { M as MiddlewareOptions, e as Firewall } from '../firewall-BZ8kfq5c.cjs';
2
2
 
3
3
  /**
4
4
  * Vercel AI SDK 7 middleware integration.
@@ -1,4 +1,4 @@
1
- import { M as MiddlewareOptions, e as Firewall } from '../firewall-BD4YdAmB.js';
1
+ import { M as MiddlewareOptions, e as Firewall } from '../firewall-BZ8kfq5c.js';
2
2
 
3
3
  /**
4
4
  * Vercel AI SDK 7 middleware integration.
@@ -1 +1 @@
1
- export { g as FirewallMiddleware, S as createMiddleware } from '../firewall-BD4YdAmB.cjs';
1
+ export { g as FirewallMiddleware, S as createMiddleware } from '../firewall-BZ8kfq5c.cjs';
@@ -1 +1 @@
1
- export { g as FirewallMiddleware, S as createMiddleware } from '../firewall-BD4YdAmB.js';
1
+ export { g as FirewallMiddleware, S as createMiddleware } from '../firewall-BZ8kfq5c.js';
@@ -143,6 +143,14 @@ interface ClassifyOptions {
143
143
  signal?: AbortSignal;
144
144
  }
145
145
  interface ProtectOptions<TResult> extends ClassifyOptions {
146
+ /**
147
+ * Cancels this `protect()` call only, at any point before it settles. Aborting
148
+ * aborts the operation's signal and rejects with the signal's reason, even
149
+ * after the input verdict has passed. A signal that is already aborted rejects
150
+ * before the operation starts. A background classification in Shadow or Warn is
151
+ * not cancelled.
152
+ */
153
+ signal?: AbortSignal;
146
154
  /** Input classified while the operation runs. */
147
155
  text: string;
148
156
  /** Extracts model output to classify as llm_output before the result is returned. */
@@ -314,10 +322,16 @@ declare class Firewall {
314
322
  classify(text: string, options?: ClassifyOptions): Promise<BlockResult>;
315
323
  /**
316
324
  * Run an operation while its input is classified and withhold a blocked result.
317
- * The operation's signal aborts when a block arrives before it finishes.
325
+ * The operation's signal aborts when a block arrives before it finishes, and
326
+ * when the caller's `signal` aborts at any point before `protect()` settles.
318
327
  */
319
328
  protect<TResult>(operation: (signal: AbortSignal) => PromiseLike<TResult> | TResult, options: ProtectOptions<TResult>): Promise<TResult>;
320
- /** Wait for classifications still running in the background. */
329
+ /**
330
+ * Wait for every check queued on this client, foreground or background.
331
+ * `protect()` and the parallel or background checks of framework adapters are
332
+ * queued. Direct `classify()` and `classifyBatch()` calls are not, and the
333
+ * operation that `protect()` runs is not awaited.
334
+ */
321
335
  flush(): Promise<void>;
322
336
  /**
323
337
  * @internal Classify in the background with one retry; resolves undefined on failure.
@@ -143,6 +143,14 @@ interface ClassifyOptions {
143
143
  signal?: AbortSignal;
144
144
  }
145
145
  interface ProtectOptions<TResult> extends ClassifyOptions {
146
+ /**
147
+ * Cancels this `protect()` call only, at any point before it settles. Aborting
148
+ * aborts the operation's signal and rejects with the signal's reason, even
149
+ * after the input verdict has passed. A signal that is already aborted rejects
150
+ * before the operation starts. A background classification in Shadow or Warn is
151
+ * not cancelled.
152
+ */
153
+ signal?: AbortSignal;
146
154
  /** Input classified while the operation runs. */
147
155
  text: string;
148
156
  /** Extracts model output to classify as llm_output before the result is returned. */
@@ -314,10 +322,16 @@ declare class Firewall {
314
322
  classify(text: string, options?: ClassifyOptions): Promise<BlockResult>;
315
323
  /**
316
324
  * Run an operation while its input is classified and withhold a blocked result.
317
- * The operation's signal aborts when a block arrives before it finishes.
325
+ * The operation's signal aborts when a block arrives before it finishes, and
326
+ * when the caller's `signal` aborts at any point before `protect()` settles.
318
327
  */
319
328
  protect<TResult>(operation: (signal: AbortSignal) => PromiseLike<TResult> | TResult, options: ProtectOptions<TResult>): Promise<TResult>;
320
- /** Wait for classifications still running in the background. */
329
+ /**
330
+ * Wait for every check queued on this client, foreground or background.
331
+ * `protect()` and the parallel or background checks of framework adapters are
332
+ * queued. Direct `classify()` and `classifyBatch()` calls are not, and the
333
+ * operation that `protect()` runs is not awaited.
334
+ */
321
335
  flush(): Promise<void>;
322
336
  /**
323
337
  * @internal Classify in the background with one retry; resolves undefined on failure.
package/dist/index.cjs CHANGED
@@ -1580,7 +1580,7 @@ function sanitizeText(text) {
1580
1580
  }
1581
1581
 
1582
1582
  // src/firewall.ts
1583
- var SDK_VERSION = "0.11.1";
1583
+ var SDK_VERSION = "0.11.3";
1584
1584
  var DEFAULT_TIMEOUT_MS = 1e4;
1585
1585
  var DEFAULT_MAX_RETRIES = 5;
1586
1586
  var MAX_BACKOFF_SECONDS = 30;
@@ -1906,6 +1906,32 @@ function createAttemptSignal(timeoutMs, callerSignal) {
1906
1906
  }
1907
1907
  };
1908
1908
  }
1909
+ function linkCaller(callerSignal, controller) {
1910
+ if (callerSignal === void 0) {
1911
+ return { until: (work) => Promise.resolve(work), run: (callback) => callback(), dispose: () => void 0 };
1912
+ }
1913
+ let onAbort = () => void 0;
1914
+ const cancelled = new Promise((_resolve, reject) => {
1915
+ onAbort = () => {
1916
+ reject(callerSignal.reason);
1917
+ controller.abort(callerSignal.reason);
1918
+ };
1919
+ });
1920
+ cancelled.catch(() => void 0);
1921
+ callerSignal.addEventListener("abort", onAbort, { once: true });
1922
+ return {
1923
+ until: (work) => Promise.race([work, cancelled]),
1924
+ run: (callback) => {
1925
+ try {
1926
+ return callback();
1927
+ } catch (error) {
1928
+ callerSignal.throwIfAborted();
1929
+ throw error;
1930
+ }
1931
+ },
1932
+ dispose: () => callerSignal.removeEventListener("abort", onAbort)
1933
+ };
1934
+ }
1909
1935
  function sleep(ms, signal) {
1910
1936
  return new Promise((resolve, reject) => {
1911
1937
  if (signal?.aborted) {
@@ -1969,45 +1995,66 @@ var Firewall = class {
1969
1995
  }
1970
1996
  /**
1971
1997
  * Run an operation while its input is classified and withhold a blocked result.
1972
- * The operation's signal aborts when a block arrives before it finishes.
1998
+ * The operation's signal aborts when a block arrives before it finishes, and
1999
+ * when the caller's `signal` aborts at any point before `protect()` settles.
1973
2000
  */
1974
2001
  async protect(operation, options) {
1975
2002
  const { text, output, ...request } = options;
1976
2003
  const requestedMode = validateMode(options.mode) ?? this.mode;
2004
+ const callerSignal = request.signal;
2005
+ callerSignal?.throwIfAborted();
1977
2006
  const controller = new AbortController();
1978
- if (requestedMode === "shadow" || requestedMode === "warn") {
1979
- const observed = this.observable(request);
1980
- if (observed !== void 0) {
1981
- void this.background(text, observed);
2007
+ const caller = linkCaller(callerSignal, controller);
2008
+ try {
2009
+ if (requestedMode === "shadow" || requestedMode === "warn") {
2010
+ const observed = this.observable(request);
2011
+ if (observed !== void 0) {
2012
+ void this.background(text, observed);
2013
+ }
2014
+ const result2 = await caller.until(caller.run(() => operation(controller.signal)));
2015
+ callerSignal?.throwIfAborted();
2016
+ if (observed !== void 0 && output !== void 0) {
2017
+ const generated = caller.run(() => output(result2));
2018
+ callerSignal?.throwIfAborted();
2019
+ void this.background(generated, outputRequest(observed));
2020
+ }
2021
+ return result2;
1982
2022
  }
1983
- const result2 = await operation(controller.signal);
1984
- if (observed !== void 0 && output !== void 0) {
1985
- void this.background(output(result2), outputRequest(observed));
2023
+ const input = snapshotRequest(request);
2024
+ const conversation = conversationId(input.metadata);
2025
+ const outputOptions = outputRequest(input);
2026
+ const verdict = this.pending.submit(() => this.enforce(text, input), conversation, input.signal);
2027
+ const [, result] = await caller.until(Promise.all([
2028
+ verdict.catch((error) => {
2029
+ controller.abort(error);
2030
+ throw error;
2031
+ }),
2032
+ Promise.resolve().then(() => {
2033
+ callerSignal?.throwIfAborted();
2034
+ return operation(controller.signal);
2035
+ })
2036
+ ]));
2037
+ callerSignal?.throwIfAborted();
2038
+ if (output !== void 0) {
2039
+ const generated = caller.run(() => output(result));
2040
+ callerSignal?.throwIfAborted();
2041
+ await caller.until(this.pending.submit(
2042
+ () => this.enforce(generated, outputOptions),
2043
+ conversation,
2044
+ input.signal
2045
+ ));
1986
2046
  }
1987
- return result2;
1988
- }
1989
- const input = snapshotRequest(request);
1990
- const conversation = conversationId(input.metadata);
1991
- const outputOptions = outputRequest(input);
1992
- const verdict = this.pending.submit(() => this.enforce(text, input), conversation, input.signal);
1993
- const [, result] = await Promise.all([
1994
- verdict.catch((error) => {
1995
- controller.abort(error);
1996
- throw error;
1997
- }),
1998
- Promise.resolve().then(() => operation(controller.signal))
1999
- ]);
2000
- if (output !== void 0) {
2001
- const generated = output(result);
2002
- await this.pending.submit(
2003
- () => this.enforce(generated, outputOptions),
2004
- conversation,
2005
- input.signal
2006
- );
2047
+ return result;
2048
+ } finally {
2049
+ caller.dispose();
2007
2050
  }
2008
- return result;
2009
2051
  }
2010
- /** Wait for classifications still running in the background. */
2052
+ /**
2053
+ * Wait for every check queued on this client, foreground or background.
2054
+ * `protect()` and the parallel or background checks of framework adapters are
2055
+ * queued. Direct `classify()` and `classifyBatch()` calls are not, and the
2056
+ * operation that `protect()` runs is not awaited.
2057
+ */
2011
2058
  flush() {
2012
2059
  return this.pending.drain();
2013
2060
  }