@silmaril-security/sdk 0.11.2 → 0.11.4

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.4
4
+
5
+ - Cancel the Node HTTP example on premature response closure.
6
+
7
+ ## 0.11.3
8
+
9
+ - Reject `protect()` with the caller's abort reason when the operation or output
10
+ extractor aborts the caller and then throws synchronously.
11
+ - Document that `flush()` waits for every check queued on the client,
12
+ foreground included, and not for direct `classify()` calls or the operation
13
+ that `protect()` runs.
14
+
3
15
  ## 0.11.2
4
16
 
5
17
  - Keep protect() responsive to caller cancellation through operation and output handling.
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.2 for reproducible installs:
38
+ Pin version 0.11.4 for reproducible installs:
39
39
 
40
40
  ```sh
41
- npm install @silmaril-security/sdk@0.11.2
41
+ npm install @silmaril-security/sdk@0.11.4
42
42
  ```
43
43
 
44
44
  Requires Node 20 or later.
@@ -203,11 +203,16 @@ Events that belong to one conversation are not independent. Send them with
203
203
  ordered individual `classify()` calls that share `metadata.conversationId`, and
204
204
  wait for each call before sending the next event for that conversation.
205
205
 
206
- Pass a `signal` to cancel one call without touching its siblings:
206
+ Pass a `signal` to cancel one call without touching its siblings. In Node's
207
+ HTTP server, listen for the outgoing response closing before it finishes:
207
208
 
208
209
  ```ts
209
210
  const controller = new AbortController();
210
- request.on("close", () => controller.abort());
211
+ const abortIfDisconnected = () => {
212
+ if (!response.writableFinished) controller.abort();
213
+ };
214
+ response.once("close", abortIfDisconnected);
215
+ if (response.destroyed) abortIfDisconnected();
211
216
 
212
217
  try {
213
218
  const result = await fw.classify(userInput, {
@@ -220,6 +225,8 @@ try {
220
225
  return; // caller went away
221
226
  }
222
227
  throw err;
228
+ } finally {
229
+ response.off("close", abortIfDisconnected);
223
230
  }
224
231
  ```
225
232
 
@@ -605,8 +612,10 @@ A background classification retries once, never rejects, logs its failure with
605
612
  exceed `maxQueued` unjoined checks for its conversation, or
606
613
  `maxConcurrent * maxQueued` background checks across all conversations
607
614
  (2,048 by default), is dropped with a warning. `await fw.flush()` waits for
608
- background classifications to finish, for example before a serverless handler
609
- returns.
615
+ every check queued on the client to finish, foreground or background, for
616
+ example before a serverless handler returns. `protect()` and the parallel or
617
+ background checks of framework adapters are queued. Direct `classify()` calls
618
+ are not, and `flush()` does not wait for the operation that `protect()` runs.
610
619
 
611
620
  ## Migration Notes
612
621
 
@@ -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-D8XAQ9gj.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-D8XAQ9gj.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-D8XAQ9gj.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-D8XAQ9gj.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-D8XAQ9gj.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-D8XAQ9gj.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-D8XAQ9gj.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-D8XAQ9gj.js';
1
+ export { g as FirewallMiddleware, S as createMiddleware } from '../firewall-BZ8kfq5c.js';
@@ -326,7 +326,12 @@ declare class Firewall {
326
326
  * when the caller's `signal` aborts at any point before `protect()` settles.
327
327
  */
328
328
  protect<TResult>(operation: (signal: AbortSignal) => PromiseLike<TResult> | TResult, options: ProtectOptions<TResult>): Promise<TResult>;
329
- /** 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
+ */
330
335
  flush(): Promise<void>;
331
336
  /**
332
337
  * @internal Classify in the background with one retry; resolves undefined on failure.
@@ -326,7 +326,12 @@ declare class Firewall {
326
326
  * when the caller's `signal` aborts at any point before `protect()` settles.
327
327
  */
328
328
  protect<TResult>(operation: (signal: AbortSignal) => PromiseLike<TResult> | TResult, options: ProtectOptions<TResult>): Promise<TResult>;
329
- /** 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
+ */
330
335
  flush(): Promise<void>;
331
336
  /**
332
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.2";
1583
+ var SDK_VERSION = "0.11.4";
1584
1584
  var DEFAULT_TIMEOUT_MS = 1e4;
1585
1585
  var DEFAULT_MAX_RETRIES = 5;
1586
1586
  var MAX_BACKOFF_SECONDS = 30;
@@ -1908,7 +1908,7 @@ function createAttemptSignal(timeoutMs, callerSignal) {
1908
1908
  }
1909
1909
  function linkCaller(callerSignal, controller) {
1910
1910
  if (callerSignal === void 0) {
1911
- return { until: (work) => Promise.resolve(work), dispose: () => void 0 };
1911
+ return { until: (work) => Promise.resolve(work), run: (callback) => callback(), dispose: () => void 0 };
1912
1912
  }
1913
1913
  let onAbort = () => void 0;
1914
1914
  const cancelled = new Promise((_resolve, reject) => {
@@ -1921,6 +1921,14 @@ function linkCaller(callerSignal, controller) {
1921
1921
  callerSignal.addEventListener("abort", onAbort, { once: true });
1922
1922
  return {
1923
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
+ },
1924
1932
  dispose: () => callerSignal.removeEventListener("abort", onAbort)
1925
1933
  };
1926
1934
  }
@@ -2003,10 +2011,10 @@ var Firewall = class {
2003
2011
  if (observed !== void 0) {
2004
2012
  void this.background(text, observed);
2005
2013
  }
2006
- const result2 = await caller.until(operation(controller.signal));
2014
+ const result2 = await caller.until(caller.run(() => operation(controller.signal)));
2007
2015
  callerSignal?.throwIfAborted();
2008
2016
  if (observed !== void 0 && output !== void 0) {
2009
- const generated = output(result2);
2017
+ const generated = caller.run(() => output(result2));
2010
2018
  callerSignal?.throwIfAborted();
2011
2019
  void this.background(generated, outputRequest(observed));
2012
2020
  }
@@ -2028,7 +2036,7 @@ var Firewall = class {
2028
2036
  ]));
2029
2037
  callerSignal?.throwIfAborted();
2030
2038
  if (output !== void 0) {
2031
- const generated = output(result);
2039
+ const generated = caller.run(() => output(result));
2032
2040
  callerSignal?.throwIfAborted();
2033
2041
  await caller.until(this.pending.submit(
2034
2042
  () => this.enforce(generated, outputOptions),
@@ -2041,7 +2049,12 @@ var Firewall = class {
2041
2049
  caller.dispose();
2042
2050
  }
2043
2051
  }
2044
- /** 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
+ */
2045
2058
  flush() {
2046
2059
  return this.pending.drain();
2047
2060
  }