@signalvaultio/node 0.3.0 → 0.4.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 SignalVault
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -62,7 +62,7 @@ console.log(response.content[0].text);
62
62
 
63
63
  ## Streaming
64
64
 
65
- Streaming is fully supported for both providers. SignalVault logs the complete response once the stream finishes:
65
+ Streaming is fully supported for both providers. SignalVault logs the response when the stream ends — including a partial response if you stop iterating early or the stream fails. A stream you never iterate is not logged.
66
66
 
67
67
  ```typescript
68
68
  // OpenAI streaming
@@ -91,6 +91,8 @@ for await (const event of stream) {
91
91
  }
92
92
  ```
93
93
 
94
+ For token counts on OpenAI streams, pass `stream_options: { include_usage: true }`. The SDK does not add it for you, because OpenAI then sends a final chunk with an empty `choices` array.
95
+
94
96
  ## Agent Tool-Use Capture
95
97
 
96
98
  Log every tool invocation made by your agents — name, input, output, duration, and any error — as auditable events alongside your LLM calls.
@@ -130,14 +132,15 @@ When you wrap a tool or call `tools.record()`, SignalVault captures:
130
132
  - `error.message` if the tool throws (truncated to 1900 bytes)
131
133
  - `duration_ms`, `started_at`, and any `metadata` you attach
132
134
 
133
- These fields are stored encrypted at rest server-side, but they go on the wire
134
- to SignalVault's API. **If you pass user PII, secrets, or API keys as tool
135
- arguments, those values will leave your process and be stored in SignalVault.**
136
- Recommendations:
135
+ `tool_input` and `tool_output` are encrypted at rest server-side. `tool_name`,
136
+ `error`, `started_at` and `metadata` are stored as sent, **not encrypted**. All of
137
+ it goes on the wire to SignalVault's API: **if you pass user PII, secrets, or API
138
+ keys as tool arguments, those values will leave your process and be stored in
139
+ SignalVault.** Recommendations:
137
140
 
138
141
  - Sanitize sensitive arguments before invoking the wrapped tool, or use the
139
142
  manual `tools.record()` API and pass a redacted copy.
140
- - Don't put secrets in error messages — they end up in `error` verbatim.
143
+ - Don't put secrets in error messages — they end up in `error` verbatim and unencrypted.
141
144
  - Use `metadata` for non-sensitive identifiers (`user_id`, `feature`,
142
145
  `workspace_id`); avoid putting raw user content in metadata.
143
146
 
@@ -173,19 +176,37 @@ const response = await client.chat.completions.create(
173
176
  );
174
177
  ```
175
178
 
176
- ## Timeout Configuration
179
+ ## When SignalVault Is Unavailable
180
+
181
+ The pre-flight guardrail check is in your request's critical path. If it cannot return a decision — timeout, network error, invalid or revoked API key (401), inactive subscription (402), access denied (403), rate or trial limit (429), server error, or an invalid response — `failMode` decides what happens:
182
+
183
+ - `'open'` (default): the request goes to the provider **without** guardrails, and the SDK prints a warning (at most once a minute per cause, whether or not `debug` is on). If SignalVault was unreachable, timed out or errored, the request is still recorded in the background, marked `preflight_unavailable`, so it appears in your audit log.
184
+ - `'closed'`: the SDK throws `SignalVaultUnavailableError` and the provider is never called. Use this if every request must be checked.
177
185
 
178
- The pre-flight guardrail check is in your request's critical path. SignalVaultClient uses a short timeout and **fails open** — your request always goes through even if the SignalVault API is slow or unreachable:
186
+ `failMode` applies to the pre-flight check, so it has no effect in mirror mode.
187
+
188
+ **Rate limit.** The ingest API allows 120 events per minute per app, and each call sends two (request and response), plus one per tool call. Above roughly 60 LLM calls a minute, pre-flight checks are rate limited: with `failMode: 'open'` those requests go through unchecked. If you run at that volume and need enforcement, use `failMode: 'closed'`.
179
189
 
180
190
  ```typescript
181
191
  const client = new SignalVaultClient({
182
192
  apiKey: 'sk_live_...',
183
193
  openaiApiKey: process.env.OPENAI_API_KEY!,
184
- preflightTimeout: 2000, // ms — pre-flight check timeout (fails open). Default: 2000
194
+ failMode: 'closed', // 'open' | 'closed'. Default: 'open'
195
+ preflightTimeout: 2000, // ms — pre-flight check timeout. Default: 2000
185
196
  timeout: 10000, // ms — background/post-flight calls. Default: 10000
186
197
  });
187
198
  ```
188
199
 
200
+ Audit events (responses, mirror-mode events, tool calls) are sent in the background. Each carries an `event_id`, so a retry is never double-counted. The SDK retries once on connection errors and 5xx (not on timeouts). A rate-limited (429) event is retried only if `Retry-After` is 5 seconds or less; the SignalVault API currently asks for 60, so in practice those events are dropped, with a warning. `tools.record()` does not retry.
201
+
202
+ ## Shutting Down
203
+
204
+ Background events are queued in memory. Before a short-lived process exits (a script, a serverless handler), wait for them:
205
+
206
+ ```typescript
207
+ await client.flush(); // or client.close(); both wait up to 5s by default
208
+ ```
209
+
189
210
  ## Mirror Mode
190
211
 
191
212
  In mirror mode, requests go directly to the AI provider first and SignalVault audits them asynchronously — no latency added, never blocks:
@@ -201,7 +222,8 @@ const client = new SignalVaultClient({
201
222
  ## Features
202
223
 
203
224
  - **Automatic Logging** — Every request and response is recorded in your SignalVault dashboard
204
- - **Pre-flight Guardrails** — Block or redact requests before they reach the AI provider
225
+ - **Pre-flight Guardrails** — Block requests that break your rules before they reach the AI provider
226
+ - **Redaction at rest** — Rules with the `redact` action remove matches from what SignalVault stores. The request sent to the AI provider is **not** modified.
205
227
  - **PII Detection** — Automatically detect emails, phone numbers, SSNs in prompts
206
228
  - **Secret Detection** — Block API keys and tokens in prompts
207
229
  - **Token Limits** — Enforce cost controls per request
@@ -218,10 +240,11 @@ const client = new SignalVaultClient({
218
240
  apiKey: 'sk_live_...', // Your SignalVault API key (required)
219
241
  openaiApiKey: 'sk-...', // OpenAI API key (required for chat.completions)
220
242
  anthropicApiKey: 'sk-ant-...', // Anthropic API key (required for messages)
221
- baseUrl: 'https://api.signalvault.io',
222
- environment: 'production', // 'development' | 'staging' | 'production'
243
+ baseUrl: 'https://api.signalvault.io', // default; plain http:// only allowed for localhost
244
+ environment: 'production', // sent with events; the server uses the API key's environment
223
245
  debug: false,
224
246
  mirrorMode: false,
247
+ failMode: 'open', // 'open' | 'closed' — see "When SignalVault Is Unavailable"
225
248
  preflightTimeout: 2000, // ms
226
249
  timeout: 10000, // ms
227
250
  metadata: {}, // Default metadata for all events
@@ -231,14 +254,22 @@ const client = new SignalVaultClient({
231
254
  ## Error Handling
232
255
 
233
256
  ```typescript
257
+ import { SignalVaultBlockedError, SignalVaultUnavailableError } from '@signalvaultio/node';
258
+
234
259
  try {
235
260
  const response = await client.chat.completions.create({
236
261
  model: 'gpt-4',
237
262
  messages: [{ role: 'user', content: 'my SSN is 123-45-6789' }],
238
263
  });
239
264
  } catch (error) {
240
- if (error.message.includes('[SignalVault]')) {
241
- console.log('Request blocked by guardrail');
265
+ if (error instanceof SignalVaultBlockedError) {
266
+ console.log('Blocked by guardrails:', error.violations.map((v) => v.type));
267
+ console.log('Details:', error.dashboardUrl);
268
+ } else if (error instanceof SignalVaultUnavailableError) {
269
+ // Only thrown with failMode: 'closed'
270
+ console.log('Guardrail check unavailable:', error.status);
271
+ } else {
272
+ throw error;
242
273
  }
243
274
  }
244
275
  ```
package/dist/index.d.ts CHANGED
@@ -1,20 +1,43 @@
1
1
  import OpenAI from 'openai';
2
2
  import { ToolsAPI } from './tools';
3
3
  export type { ToolRecordOptions, ToolsAPI } from './tools';
4
+ /** Kept in sync with package.json by a unit test. */
5
+ export declare const SDK_VERSION = "0.4.0";
6
+ /**
7
+ * Normalizes the base URL and refuses plaintext HTTP to anything but the local
8
+ * machine. The API key and full prompts travel in every request, so an
9
+ * `http://` URL would expose both before the server could redirect to HTTPS.
10
+ */
11
+ export declare function normalizeBaseUrl(raw: string): string;
12
+ /**
13
+ * What to do when SignalVault cannot return a guardrail decision (unreachable,
14
+ * timed out, rate limited, invalid key, inactive subscription, ...).
15
+ *
16
+ * - `'open'` (default): allow the request and print a warning.
17
+ * - `'closed'`: throw {@link SignalVaultUnavailableError} instead of calling
18
+ * the provider unchecked.
19
+ */
20
+ export type FailMode = 'open' | 'closed';
4
21
  export interface SignalVaultConfig {
5
22
  apiKey: string;
6
23
  /** Required for OpenAI (chat.completions). */
7
24
  openaiApiKey?: string;
8
25
  /** Required for Anthropic (messages). Install @anthropic-ai/sdk separately. */
9
26
  anthropicApiKey?: string;
27
+ /** Default: https://api.signalvault.io. Plain http:// is only accepted for localhost. */
10
28
  baseUrl?: string;
29
+ /**
30
+ * Sent with every event for backwards compatibility. The server takes the
31
+ * environment from the API key, so this value does not select rules.
32
+ */
11
33
  environment?: 'development' | 'staging' | 'production';
12
34
  debug?: boolean;
13
35
  mirrorMode?: boolean;
36
+ /** Behaviour when no guardrail decision can be obtained. Default: 'open'. */
37
+ failMode?: FailMode;
14
38
  /**
15
39
  * Timeout (ms) for the pre-flight /v1/events call in normal mode.
16
- * This is in the critical path — on timeout/error the SDK fails open (allows).
17
- * Default: 2000
40
+ * This is in the critical path. Default: 2000
18
41
  */
19
42
  preflightTimeout?: number;
20
43
  /**
@@ -28,25 +51,45 @@ export interface SignalVaultConfig {
28
51
  */
29
52
  metadata?: Record<string, unknown>;
30
53
  }
54
+ export interface SignalVaultViolation {
55
+ rule_id?: string;
56
+ type: string;
57
+ severity: number;
58
+ action: string;
59
+ details: Record<string, unknown>;
60
+ }
31
61
  export interface SignalVaultDecision {
32
62
  decision: 'allow' | 'warn' | 'block' | 'redact';
33
- violations: Array<{
34
- rule_id?: string;
35
- type: string;
36
- severity: number;
37
- action: string;
38
- details: Record<string, unknown>;
39
- }>;
63
+ violations: SignalVaultViolation[];
64
+ /**
65
+ * Rule types whose redact action matched, with match counts. Redaction
66
+ * applies to what SignalVault stores; the request sent to the provider is
67
+ * not modified. Check `.length`, never truthiness.
68
+ */
40
69
  redactions: Array<{
41
- start: number;
42
- end: number;
43
- replacement: string;
70
+ type: string;
71
+ count: number;
44
72
  }>;
73
+ /** Present on block decisions. */
74
+ dashboard_url?: string;
45
75
  }
46
76
  export interface CreateOptions {
47
77
  /** Per-call metadata, merged over config-level metadata. */
48
78
  metadata?: Record<string, unknown>;
49
79
  }
80
+ /** Thrown when a guardrail rule blocks the request. */
81
+ export declare class SignalVaultBlockedError extends Error {
82
+ readonly requestId: string;
83
+ readonly violations: SignalVaultViolation[];
84
+ readonly dashboardUrl?: string;
85
+ constructor(requestId: string, violations: SignalVaultViolation[], dashboardUrl?: string);
86
+ }
87
+ /** Thrown in `failMode: 'closed'` when no guardrail decision could be obtained. */
88
+ export declare class SignalVaultUnavailableError extends Error {
89
+ readonly requestId: string;
90
+ readonly status?: number;
91
+ constructor(requestId: string, reason: string, status?: number);
92
+ }
50
93
  /**
51
94
  * SignalVault Client — wraps OpenAI and/or Anthropic with guardrails and audit logging.
52
95
  *
@@ -55,10 +98,9 @@ export interface CreateOptions {
55
98
  * const client = new SignalVaultClient({
56
99
  * apiKey: 'sk_live_...',
57
100
  * openaiApiKey: process.env.OPENAI_API_KEY!,
58
- * baseUrl: 'https://api.signalvault.io',
59
101
  * metadata: { user_id: '123' },
60
102
  * });
61
- * const res = await client.chat.completions.create({ model: 'gpt-4', messages: [...] });
103
+ * const res = await client.chat.completions.create({ model: 'gpt-4o', messages: [...] });
62
104
  * ```
63
105
  *
64
106
  * @example Anthropic
@@ -66,9 +108,8 @@ export interface CreateOptions {
66
108
  * const client = new SignalVaultClient({
67
109
  * apiKey: 'sk_live_...',
68
110
  * anthropicApiKey: process.env.ANTHROPIC_API_KEY!,
69
- * baseUrl: 'https://api.signalvault.io',
70
111
  * });
71
- * const res = await client.messages.create({ model: 'claude-3-5-sonnet-20241022', messages: [...], max_tokens: 1024 });
112
+ * const res = await client.messages.create({ model: 'claude-sonnet-4-5', messages: [...], max_tokens: 1024 });
72
113
  * ```
73
114
  */
74
115
  export declare class SignalVaultClient {
@@ -77,12 +118,27 @@ export declare class SignalVaultClient {
77
118
  private readonly svEnvironment;
78
119
  private readonly debugMode;
79
120
  private readonly mirrorMode;
121
+ private readonly failMode;
80
122
  private readonly preflightTimeout;
81
123
  private readonly bgTimeout;
82
124
  private readonly defaultMetadata;
83
125
  private readonly openai;
84
126
  private readonly anthropic;
127
+ private readonly pending;
128
+ private readonly lastWarned;
85
129
  constructor(config: SignalVaultConfig);
130
+ /**
131
+ * Waits for in-flight audit events to finish sending, up to `timeoutMs`.
132
+ * Call before a short-lived process exits (scripts, serverless handlers),
133
+ * otherwise queued events are lost.
134
+ */
135
+ flush(timeoutMs?: number): Promise<void>;
136
+ /** Flushes pending events. The client can still be used afterwards. */
137
+ close(timeoutMs?: number): Promise<void>;
138
+ /** Tracks a background promise so flush() can wait for it. Never rejects. */
139
+ private track;
140
+ /** Prints a warning, at most once per minute per key. Not gated on debug. */
141
+ private warn;
86
142
  /**
87
143
  * Wraps a function so every call records an `agent.tool_call` event with
88
144
  * timing, input, output, and any error. The wrapper auto-times, runs the
@@ -150,11 +206,48 @@ export declare class SignalVaultClient {
150
206
  private handleOpenAINormal;
151
207
  private handleAnthropicMirror;
152
208
  private handleAnthropicNormal;
209
+ /**
210
+ * Yields chunks through unchanged and records the response when the stream
211
+ * ends — including when the consumer breaks out early or the stream throws,
212
+ * in which case the partial output is recorded.
213
+ */
153
214
  private wrapStream;
154
215
  private wrapAnthropicStream;
155
- private authHeaders;
216
+ /** Runs the pre-flight check and throws if the request must not proceed. */
217
+ private enforce;
218
+ /**
219
+ * Called when no decision could be obtained. Throws in failMode 'closed';
220
+ * otherwise warns (never silently) and allows.
221
+ */
222
+ private unavailable;
223
+ /**
224
+ * The ai.request body to record in the background when the pre-flight got
225
+ * no decision, so an unchecked call is still in the audit log. Undefined
226
+ * when the pre-flight was recorded normally; null when the server refused
227
+ * it and no audit event should be sent.
228
+ */
229
+ private fallbackRequest;
230
+ private requestBody;
231
+ private describeFailure;
232
+ private headers;
233
+ /** Single POST. Never follows redirects, so the API key is never re-sent elsewhere. */
234
+ private post;
235
+ /**
236
+ * POST for background events. Adds an `event_id` so a retry is never
237
+ * double-counted, and retries once on connection errors, 5xx, and 429 with a
238
+ * short Retry-After. Timeouts are not retried: the server may be slow rather
239
+ * than down, and a retry would double the time the event is held. Returns
240
+ * the last response, or null if nothing was received.
241
+ */
242
+ private postBackground;
243
+ private logUndelivered;
156
244
  private sendRequest;
157
245
  private sendResponseEvent;
246
+ /**
247
+ * Mirror mode: records the request, then the response. Sequential on
248
+ * purpose — the server rejects an ai.response whose ai.request it has not
249
+ * stored yet, so sending both at once can lose the response.
250
+ */
158
251
  private sendAuditEvents;
159
252
  }
160
253
  export default SignalVaultClient;