@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/dist/index.js CHANGED
@@ -3,10 +3,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.SignalVaultClient = void 0;
6
+ exports.SignalVaultClient = exports.SignalVaultUnavailableError = exports.SignalVaultBlockedError = exports.SDK_VERSION = void 0;
7
+ exports.normalizeBaseUrl = normalizeBaseUrl;
8
+ const crypto_1 = require("crypto");
7
9
  const openai_1 = __importDefault(require("openai"));
8
- const uuid_1 = require("uuid");
9
10
  const tools_1 = require("./tools");
11
+ /** Kept in sync with package.json by a unit test. */
12
+ exports.SDK_VERSION = '0.4.0';
13
+ const DEFAULT_BASE_URL = 'https://api.signalvault.io';
14
+ const LOCAL_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '[::1]']);
15
+ /** Longest Retry-After the SDK will wait out before retrying a background event once. */
16
+ const MAX_RETRY_AFTER_MS = 5000;
17
+ /** A repeated warning is printed at most once per this window. */
18
+ const WARN_INTERVAL_MS = 60000;
10
19
  // ---------------------------------------------------------------------------
11
20
  // Environment guard
12
21
  // ---------------------------------------------------------------------------
@@ -27,6 +36,104 @@ function assertNodeEnvironment() {
27
36
  'it will leak your API keys to end users. Call SignalVault from your backend instead.');
28
37
  }
29
38
  }
39
+ /**
40
+ * Normalizes the base URL and refuses plaintext HTTP to anything but the local
41
+ * machine. The API key and full prompts travel in every request, so an
42
+ * `http://` URL would expose both before the server could redirect to HTTPS.
43
+ */
44
+ function normalizeBaseUrl(raw) {
45
+ let url;
46
+ try {
47
+ url = new URL(raw);
48
+ }
49
+ catch {
50
+ throw new Error(`[SignalVault] baseUrl is not a valid URL: ${raw}`);
51
+ }
52
+ if (url.protocol === 'http:' && !LOCAL_HOSTS.has(url.hostname)) {
53
+ throw new Error('[SignalVault] baseUrl must use https:// (plain http:// is only allowed for localhost). ' +
54
+ 'Your API key and prompts would otherwise be sent unencrypted.');
55
+ }
56
+ if (url.search || url.hash) {
57
+ throw new Error(`[SignalVault] baseUrl must not contain a query or fragment: ${raw}`);
58
+ }
59
+ if (url.protocol !== 'https:' && url.protocol !== 'http:') {
60
+ throw new Error(`[SignalVault] baseUrl must be an https:// URL, got ${url.protocol}`);
61
+ }
62
+ return raw.replace(/\/+$/, '');
63
+ }
64
+ /** Thrown when a guardrail rule blocks the request. */
65
+ class SignalVaultBlockedError extends Error {
66
+ constructor(requestId, violations, dashboardUrl) {
67
+ const types = [...new Set(violations.map((v) => String(v?.type ?? '')).filter(Boolean))].join(', ') || 'policy';
68
+ super(`[SignalVault] Request blocked by guardrails (${types}).`);
69
+ this.name = 'SignalVaultBlockedError';
70
+ this.requestId = requestId;
71
+ this.violations = violations;
72
+ this.dashboardUrl = dashboardUrl;
73
+ }
74
+ }
75
+ exports.SignalVaultBlockedError = SignalVaultBlockedError;
76
+ /** Thrown in `failMode: 'closed'` when no guardrail decision could be obtained. */
77
+ class SignalVaultUnavailableError extends Error {
78
+ constructor(requestId, reason, status) {
79
+ super(`[SignalVault] Guardrail check unavailable: ${reason}. Request not sent (failMode: 'closed').`);
80
+ this.name = 'SignalVaultUnavailableError';
81
+ this.requestId = requestId;
82
+ this.status = status;
83
+ }
84
+ }
85
+ exports.SignalVaultUnavailableError = SignalVaultUnavailableError;
86
+ const ALLOW = { decision: 'allow', violations: [], redactions: [] };
87
+ /**
88
+ * Set on the decision returned when no pre-flight decision was obtained.
89
+ * 'record': the server may not have stored the request (network, timeout,
90
+ * 5xx, invalid response), so it is recorded in the background and the
91
+ * unchecked call still appears in the audit log. 'skip': the server refused
92
+ * it (401/402/403/429/3xx), so a response event would be refused too.
93
+ */
94
+ const PREFLIGHT_FAILED = Symbol('preflightFailed');
95
+ /** Causes where re-sending the ai.request later can succeed. */
96
+ const RECORDABLE_CAUSES = new Set(['timeout', 'network', '5xx', 'invalid-response']);
97
+ const DECISIONS = new Set(['allow', 'warn', 'block', 'redact']);
98
+ /**
99
+ * Checks the name rather than `instanceof Error`: fetch rejects with a
100
+ * DOMException, which fails `instanceof` across realms (e.g. under Jest).
101
+ */
102
+ function isTimeout(error) {
103
+ const name = error?.name;
104
+ return name === 'TimeoutError' || name === 'AbortError';
105
+ }
106
+ // ---------------------------------------------------------------------------
107
+ // Payload helpers
108
+ // ---------------------------------------------------------------------------
109
+ /**
110
+ * Anthropic takes the system prompt as a top-level `system` param. The
111
+ * server scans `payload.messages`, so it is included there as a system
112
+ * message — otherwise secrets or PII in the system prompt go unchecked.
113
+ */
114
+ function anthropicMessagesForAudit(params) {
115
+ const messages = Array.isArray(params?.messages) ? params.messages : [];
116
+ if (params?.system === undefined || params?.system === null || params?.system === '') {
117
+ return messages;
118
+ }
119
+ return [{ role: 'system', content: params.system }, ...messages];
120
+ }
121
+ /** Concatenates every text block; `content[0]` alone misses text after a tool_use block. */
122
+ function anthropicOutputText(response) {
123
+ const blocks = Array.isArray(response?.content) ? response.content : [];
124
+ return blocks
125
+ .filter((b) => b?.type === 'text' && typeof b.text === 'string')
126
+ .map((b) => b.text)
127
+ .join('');
128
+ }
129
+ function parseRetryAfterMs(response) {
130
+ const raw = response?.headers?.get?.('retry-after');
131
+ if (!raw)
132
+ return null;
133
+ const seconds = Number(raw);
134
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds * 1000 : null;
135
+ }
136
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
30
137
  // ---------------------------------------------------------------------------
31
138
  // SignalVaultClient
32
139
  // ---------------------------------------------------------------------------
@@ -38,10 +145,9 @@ function assertNodeEnvironment() {
38
145
  * const client = new SignalVaultClient({
39
146
  * apiKey: 'sk_live_...',
40
147
  * openaiApiKey: process.env.OPENAI_API_KEY!,
41
- * baseUrl: 'https://api.signalvault.io',
42
148
  * metadata: { user_id: '123' },
43
149
  * });
44
- * const res = await client.chat.completions.create({ model: 'gpt-4', messages: [...] });
150
+ * const res = await client.chat.completions.create({ model: 'gpt-4o', messages: [...] });
45
151
  * ```
46
152
  *
47
153
  * @example Anthropic
@@ -49,22 +155,27 @@ function assertNodeEnvironment() {
49
155
  * const client = new SignalVaultClient({
50
156
  * apiKey: 'sk_live_...',
51
157
  * anthropicApiKey: process.env.ANTHROPIC_API_KEY!,
52
- * baseUrl: 'https://api.signalvault.io',
53
158
  * });
54
- * const res = await client.messages.create({ model: 'claude-3-5-sonnet-20241022', messages: [...], max_tokens: 1024 });
159
+ * const res = await client.messages.create({ model: 'claude-sonnet-4-5', messages: [...], max_tokens: 1024 });
55
160
  * ```
56
161
  */
57
162
  class SignalVaultClient {
58
163
  constructor(config) {
164
+ this.pending = new Set();
165
+ this.lastWarned = new Map();
59
166
  assertNodeEnvironment();
60
167
  if (!config.openaiApiKey && !config.anthropicApiKey) {
61
168
  throw new Error('[SignalVault] At least one of openaiApiKey or anthropicApiKey is required.');
62
169
  }
170
+ if (config.failMode !== undefined && config.failMode !== 'open' && config.failMode !== 'closed') {
171
+ throw new Error("[SignalVault] failMode must be 'open' or 'closed'.");
172
+ }
63
173
  this.svApiKey = config.apiKey;
64
- this.svBaseUrl = (config.baseUrl || 'http://localhost:4000').replace(/\/+$/, '');
174
+ this.svBaseUrl = normalizeBaseUrl(config.baseUrl || DEFAULT_BASE_URL);
65
175
  this.svEnvironment = config.environment || 'production';
66
176
  this.debugMode = config.debug || false;
67
177
  this.mirrorMode = config.mirrorMode || false;
178
+ this.failMode = config.failMode ?? 'open';
68
179
  this.preflightTimeout = config.preflightTimeout ?? 2000;
69
180
  this.bgTimeout = config.timeout ?? 10000;
70
181
  this.defaultMetadata = config.metadata ?? {};
@@ -87,6 +198,55 @@ class SignalVaultClient {
87
198
  }
88
199
  }
89
200
  // ---------------------------------------------------------------------------
201
+ // Lifecycle
202
+ // ---------------------------------------------------------------------------
203
+ /**
204
+ * Waits for in-flight audit events to finish sending, up to `timeoutMs`.
205
+ * Call before a short-lived process exits (scripts, serverless handlers),
206
+ * otherwise queued events are lost.
207
+ */
208
+ async flush(timeoutMs = 5000) {
209
+ const deadline = Date.now() + timeoutMs;
210
+ while (this.pending.size > 0) {
211
+ const remaining = deadline - Date.now();
212
+ if (remaining <= 0) {
213
+ this.warn('flush-timeout', `[SignalVault] flush() timed out with ${this.pending.size} event(s) still sending.`);
214
+ return;
215
+ }
216
+ let timer;
217
+ await Promise.race([
218
+ Promise.allSettled([...this.pending]),
219
+ new Promise((resolve) => {
220
+ timer = setTimeout(resolve, remaining);
221
+ }),
222
+ ]);
223
+ clearTimeout(timer);
224
+ }
225
+ }
226
+ /** Flushes pending events. The client can still be used afterwards. */
227
+ async close(timeoutMs = 5000) {
228
+ await this.flush(timeoutMs);
229
+ }
230
+ /** Tracks a background promise so flush() can wait for it. Never rejects. */
231
+ track(promise) {
232
+ const tracked = promise
233
+ .catch((err) => {
234
+ if (this.debugMode)
235
+ console.error('[SignalVault] Background event failed:', err);
236
+ })
237
+ .finally(() => this.pending.delete(tracked));
238
+ this.pending.add(tracked);
239
+ }
240
+ /** Prints a warning, at most once per minute per key. Not gated on debug. */
241
+ warn(key, message) {
242
+ const now = Date.now();
243
+ const last = this.lastWarned.get(key);
244
+ if (last !== undefined && now - last < WARN_INTERVAL_MS)
245
+ return;
246
+ this.lastWarned.set(key, now);
247
+ console.warn(message);
248
+ }
249
+ // ---------------------------------------------------------------------------
90
250
  // Agent tool-use capture
91
251
  // ---------------------------------------------------------------------------
92
252
  /**
@@ -110,7 +270,11 @@ class SignalVaultClient {
110
270
  */
111
271
  tool(name, fn, options) {
112
272
  return (0, tools_1.wrapTool)({
113
- recordFn: (opts) => this.sendToolCallEvent(opts),
273
+ recordFn: (opts) => {
274
+ const promise = this.sendToolCallEvent(opts);
275
+ this.track(promise);
276
+ return promise;
277
+ },
114
278
  debug: this.debugMode,
115
279
  }, name, fn, options);
116
280
  }
@@ -130,7 +294,7 @@ class SignalVaultClient {
130
294
  */
131
295
  get tools() {
132
296
  return {
133
- record: (opts) => this.sendToolCallEvent(opts),
297
+ record: (opts) => this.sendToolCallEvent(opts, false),
134
298
  };
135
299
  }
136
300
  /**
@@ -149,11 +313,12 @@ class SignalVaultClient {
149
313
  withContext(ctx, fn) {
150
314
  return (0, tools_1.runWithContext)(ctx, fn);
151
315
  }
152
- async sendToolCallEvent(opts) {
316
+ async sendToolCallEvent(opts, retry = true) {
153
317
  const metadata = { ...this.defaultMetadata, ...(opts.metadata ?? {}) };
154
- // Validate + sanitize before constructing the body. validateToolName throws
155
- // on missing/empty name; surfacing that to the caller is intentional —
156
- // the manual API should refuse malformed input rather than silently fail.
318
+ // Validate + sanitize before the first await, so the payload reflects the
319
+ // arguments at call time even if the caller mutates them afterwards.
320
+ // validateToolName throws on missing/empty name; surfacing that to the
321
+ // caller is intentional — the manual API should refuse malformed input.
157
322
  const safeName = (0, tools_1.validateToolName)(opts.toolName);
158
323
  const payload = {
159
324
  tool_name: safeName,
@@ -174,9 +339,8 @@ class SignalVaultClient {
174
339
  };
175
340
  if (opts.requestId)
176
341
  body.request_id = opts.requestId;
177
- let serialized;
178
342
  try {
179
- serialized = JSON.stringify(body);
343
+ JSON.stringify(body);
180
344
  }
181
345
  catch (error) {
182
346
  // sanitizePayload should make this impossible, but guard the outer
@@ -184,30 +348,12 @@ class SignalVaultClient {
184
348
  console.warn('[SignalVault] tool_call serialization failed, event dropped:', error);
185
349
  return;
186
350
  }
187
- try {
188
- const response = await fetch(`${this.svBaseUrl}/v1/events`, {
189
- method: 'POST',
190
- headers: this.authHeaders(),
191
- signal: AbortSignal.timeout(this.bgTimeout),
192
- body: serialized,
193
- });
351
+ const result = await this.postBackground(body, { retry });
352
+ if (result && !result.ok && result.status >= 400 && result.status < 500 && result.status !== 429) {
194
353
  // Surface 4xx unconditionally — those are usually misconfigurations
195
- // (bad key, malformed payload) the user must know about. 5xx are noisy
196
- // and only logged in debug mode.
197
- if (!response.ok) {
198
- if (response.status >= 400 && response.status < 500) {
199
- console.warn(`[SignalVault] tool_call rejected with ${response.status}. ` +
200
- `Check your apiKey and event payload.`);
201
- }
202
- else if (this.debugMode) {
203
- console.error('[SignalVault] tool_call event failed:', response.status);
204
- }
205
- }
206
- }
207
- catch (error) {
208
- if (this.debugMode) {
209
- console.error('[SignalVault] tool_call event error:', error);
210
- }
354
+ // (bad key, malformed payload) the user must know about.
355
+ console.warn(`[SignalVault] tool_call rejected with ${result.status}. ` +
356
+ `Check your apiKey and event payload.`);
211
357
  }
212
358
  }
213
359
  // ---------------------------------------------------------------------------
@@ -221,7 +367,7 @@ class SignalVaultClient {
221
367
  return {
222
368
  completions: {
223
369
  async create(params, options) {
224
- const requestId = (0, uuid_1.v4)();
370
+ const requestId = (0, crypto_1.randomUUID)();
225
371
  const metadata = { ...self.defaultMetadata, ...(options?.metadata ?? {}) };
226
372
  if (self.debugMode) {
227
373
  console.log('[SignalVault] Processing request:', requestId, self.mirrorMode ? '(MIRROR MODE)' : '', params.stream ? '(STREAMING)' : '');
@@ -244,7 +390,7 @@ class SignalVaultClient {
244
390
  const self = this;
245
391
  return {
246
392
  async create(params, options) {
247
- const requestId = (0, uuid_1.v4)();
393
+ const requestId = (0, crypto_1.randomUUID)();
248
394
  const metadata = { ...self.defaultMetadata, ...(options?.metadata ?? {}) };
249
395
  if (self.debugMode) {
250
396
  console.log('[SignalVault] Anthropic request:', requestId, self.mirrorMode ? '(MIRROR MODE)' : '', params.stream ? '(STREAMING)' : '');
@@ -260,215 +406,334 @@ class SignalVaultClient {
260
406
  // OpenAI — mirror mode
261
407
  // ---------------------------------------------------------------------------
262
408
  async handleOpenAIMirror(requestId, params, metadata) {
263
- try {
264
- const response = await this.openai.chat.completions.create(params);
265
- if (params.stream) {
266
- return this.wrapStream(requestId, params.model, params.messages, response, async (output, promptTokens, completionTokens) => {
267
- await this.sendAuditEvents(requestId, params.model, params.messages, output, promptTokens, completionTokens, metadata, 'openai');
268
- });
269
- }
270
- const completion = response;
271
- this.sendAuditEvents(requestId, params.model, params.messages, completion.choices[0]?.message?.content || '', completion.usage?.prompt_tokens || 0, completion.usage?.completion_tokens || 0, metadata, 'openai').catch((err) => {
272
- if (this.debugMode)
273
- console.error('[SignalVault] Audit failed (non-blocking):', err);
274
- });
275
- return completion;
276
- }
277
- catch (error) {
278
- if (this.debugMode)
279
- console.error('[SignalVault] OpenAI error:', error);
280
- throw error;
409
+ const response = await this.openai.chat.completions.create(params);
410
+ const model = params.model;
411
+ const messages = params.messages;
412
+ if (params.stream) {
413
+ return this.wrapStream(response, (output, promptTokens, completionTokens) => this.sendAuditEvents(requestId, model, messages, output, promptTokens, completionTokens, metadata, 'openai'));
281
414
  }
415
+ const completion = response;
416
+ this.track(this.sendAuditEvents(requestId, model, messages, completion.choices[0]?.message?.content || '', completion.usage?.prompt_tokens || 0, completion.usage?.completion_tokens || 0, metadata, 'openai'));
417
+ return completion;
282
418
  }
283
419
  // ---------------------------------------------------------------------------
284
420
  // OpenAI — normal mode
285
421
  // ---------------------------------------------------------------------------
286
422
  async handleOpenAINormal(requestId, params, metadata) {
287
- try {
288
- const decision = await this.sendRequest(requestId, params.model, params.messages, metadata, 'openai');
289
- if (decision.decision === 'block') {
290
- throw new Error(`[SignalVault] Request blocked: ${JSON.stringify(decision.violations)}`);
291
- }
292
- if (decision.decision === 'warn' && this.debugMode) {
293
- console.warn('[SignalVault] Warnings:', decision.violations);
294
- }
295
- const response = await this.openai.chat.completions.create(params);
296
- if (params.stream) {
297
- return this.wrapStream(requestId, params.model, params.messages, response, async (output, promptTokens, completionTokens) => {
298
- await this.sendResponseEvent(requestId, params.model, output, promptTokens, completionTokens, metadata, 'openai');
299
- });
300
- }
301
- const completion = response;
302
- await this.sendResponseEvent(requestId, params.model, completion.choices[0]?.message?.content || '', completion.usage?.prompt_tokens || 0, completion.usage?.completion_tokens || 0, metadata, 'openai');
303
- return completion;
304
- }
305
- catch (error) {
306
- if (this.debugMode)
307
- console.error('[SignalVault] Error:', error);
308
- throw error;
423
+ const model = params.model;
424
+ const messages = params.messages;
425
+ const decision = await this.enforce(requestId, model, messages, metadata, 'openai');
426
+ const fallback = this.fallbackRequest(decision, requestId, model, messages, metadata, 'openai');
427
+ const response = await this.openai.chat.completions.create(params);
428
+ if (params.stream) {
429
+ return this.wrapStream(response, (output, promptTokens, completionTokens) => this.sendResponseEvent(requestId, model, output, promptTokens, completionTokens, metadata, 'openai', fallback));
309
430
  }
431
+ const completion = response;
432
+ // Background: the audit of the response must not delay returning it.
433
+ this.track(this.sendResponseEvent(requestId, model, completion.choices[0]?.message?.content || '', completion.usage?.prompt_tokens || 0, completion.usage?.completion_tokens || 0, metadata, 'openai', fallback));
434
+ return completion;
310
435
  }
311
436
  // ---------------------------------------------------------------------------
312
437
  // Anthropic — mirror mode
313
438
  // ---------------------------------------------------------------------------
314
439
  async handleAnthropicMirror(requestId, params, metadata) {
315
- try {
316
- const response = await this.anthropic.messages.create(params);
317
- if (params.stream) {
318
- return this.wrapAnthropicStream(requestId, params.model, params.messages, response, async (output, inputTokens, outputTokens) => {
319
- await this.sendAuditEvents(requestId, params.model, params.messages, output, inputTokens, outputTokens, metadata, 'anthropic');
320
- });
321
- }
322
- const output = response.content?.[0]?.text || '';
323
- this.sendAuditEvents(requestId, params.model, params.messages, output, response.usage?.input_tokens || 0, response.usage?.output_tokens || 0, metadata, 'anthropic').catch((err) => {
324
- if (this.debugMode)
325
- console.error('[SignalVault] Anthropic audit failed (non-blocking):', err);
326
- });
327
- return response;
328
- }
329
- catch (error) {
330
- if (this.debugMode)
331
- console.error('[SignalVault] Anthropic error:', error);
332
- throw error;
440
+ const response = await this.anthropic.messages.create(params);
441
+ const messages = anthropicMessagesForAudit(params);
442
+ if (params.stream) {
443
+ return this.wrapAnthropicStream(response, (output, inputTokens, outputTokens) => this.sendAuditEvents(requestId, params.model, messages, output, inputTokens, outputTokens, metadata, 'anthropic'));
333
444
  }
445
+ this.track(this.sendAuditEvents(requestId, params.model, messages, anthropicOutputText(response), response.usage?.input_tokens || 0, response.usage?.output_tokens || 0, metadata, 'anthropic'));
446
+ return response;
334
447
  }
335
448
  // ---------------------------------------------------------------------------
336
449
  // Anthropic — normal mode
337
450
  // ---------------------------------------------------------------------------
338
451
  async handleAnthropicNormal(requestId, params, metadata) {
339
- try {
340
- const decision = await this.sendRequest(requestId, params.model, params.messages, metadata, 'anthropic');
341
- if (decision.decision === 'block') {
342
- throw new Error(`[SignalVault] Request blocked: ${JSON.stringify(decision.violations)}`);
343
- }
344
- if (decision.decision === 'warn' && this.debugMode) {
345
- console.warn('[SignalVault] Warnings:', decision.violations);
346
- }
347
- const response = await this.anthropic.messages.create(params);
348
- if (params.stream) {
349
- return this.wrapAnthropicStream(requestId, params.model, params.messages, response, async (output, inputTokens, outputTokens) => {
350
- await this.sendResponseEvent(requestId, params.model, output, inputTokens, outputTokens, metadata, 'anthropic');
351
- });
352
- }
353
- const output = response.content?.[0]?.text || '';
354
- await this.sendResponseEvent(requestId, params.model, output, response.usage?.input_tokens || 0, response.usage?.output_tokens || 0, metadata, 'anthropic');
355
- return response;
356
- }
357
- catch (error) {
358
- if (this.debugMode)
359
- console.error('[SignalVault] Anthropic error:', error);
360
- throw error;
452
+ const messages = anthropicMessagesForAudit(params);
453
+ const decision = await this.enforce(requestId, params.model, messages, metadata, 'anthropic');
454
+ const fallback = this.fallbackRequest(decision, requestId, params.model, messages, metadata, 'anthropic');
455
+ const response = await this.anthropic.messages.create(params);
456
+ if (params.stream) {
457
+ return this.wrapAnthropicStream(response, (output, inputTokens, outputTokens) => this.sendResponseEvent(requestId, params.model, output, inputTokens, outputTokens, metadata, 'anthropic', fallback));
361
458
  }
459
+ this.track(this.sendResponseEvent(requestId, params.model, anthropicOutputText(response), response.usage?.input_tokens || 0, response.usage?.output_tokens || 0, metadata, 'anthropic', fallback));
460
+ return response;
362
461
  }
363
462
  // ---------------------------------------------------------------------------
364
- // Unified OpenAI stream wrapper
463
+ // Stream wrappers
365
464
  // ---------------------------------------------------------------------------
366
- async *wrapStream(requestId, model, messages, stream, onComplete) {
465
+ /**
466
+ * Yields chunks through unchanged and records the response when the stream
467
+ * ends — including when the consumer breaks out early or the stream throws,
468
+ * in which case the partial output is recorded.
469
+ */
470
+ async *wrapStream(stream, onComplete) {
367
471
  const chunks = [];
368
472
  let promptTokens = 0;
369
473
  let completionTokens = 0;
370
- for await (const chunk of stream) {
371
- const content = chunk.choices[0]?.delta?.content || '';
372
- if (content)
373
- chunks.push(content);
374
- if (chunk.usage) {
375
- promptTokens = chunk.usage.prompt_tokens || 0;
376
- completionTokens = chunk.usage.completion_tokens || 0;
474
+ try {
475
+ for await (const chunk of stream) {
476
+ const content = chunk.choices?.[0]?.delta?.content || '';
477
+ if (content)
478
+ chunks.push(content);
479
+ if (chunk.usage) {
480
+ promptTokens = chunk.usage.prompt_tokens || 0;
481
+ completionTokens = chunk.usage.completion_tokens || 0;
482
+ }
483
+ yield chunk;
377
484
  }
378
- yield chunk;
379
485
  }
380
- onComplete(chunks.join(''), promptTokens, completionTokens).catch((err) => {
381
- if (this.debugMode)
382
- console.error('[SignalVault] Post-stream callback failed:', err);
383
- });
486
+ finally {
487
+ this.track(onComplete(chunks.join(''), promptTokens, completionTokens));
488
+ }
384
489
  }
385
- // ---------------------------------------------------------------------------
386
- // Anthropic stream wrapper
387
- // ---------------------------------------------------------------------------
388
- async *wrapAnthropicStream(requestId, model, messages, stream, onComplete) {
490
+ async *wrapAnthropicStream(stream, onComplete) {
389
491
  const chunks = [];
390
492
  let inputTokens = 0;
391
493
  let outputTokens = 0;
392
- for await (const event of stream) {
393
- if (event.type === 'content_block_delta' && event.delta?.type === 'text_delta') {
394
- chunks.push(event.delta.text || '');
395
- }
396
- if (event.type === 'message_start' && event.message?.usage) {
397
- inputTokens = event.message.usage.input_tokens || 0;
398
- }
399
- if (event.type === 'message_delta' && event.usage) {
400
- outputTokens = event.usage.output_tokens || 0;
494
+ try {
495
+ for await (const event of stream) {
496
+ if (event.type === 'content_block_delta' && event.delta?.type === 'text_delta') {
497
+ chunks.push(event.delta.text || '');
498
+ }
499
+ if (event.type === 'message_start' && event.message?.usage) {
500
+ inputTokens = event.message.usage.input_tokens || 0;
501
+ }
502
+ if (event.type === 'message_delta' && event.usage) {
503
+ outputTokens = event.usage.output_tokens || 0;
504
+ }
505
+ yield event;
401
506
  }
402
- yield event;
403
507
  }
404
- onComplete(chunks.join(''), inputTokens, outputTokens).catch((err) => {
405
- if (this.debugMode)
406
- console.error('[SignalVault] Anthropic post-stream callback failed:', err);
407
- });
508
+ finally {
509
+ this.track(onComplete(chunks.join(''), inputTokens, outputTokens));
510
+ }
511
+ }
512
+ // ---------------------------------------------------------------------------
513
+ // Guardrail enforcement
514
+ // ---------------------------------------------------------------------------
515
+ /** Runs the pre-flight check and throws if the request must not proceed. */
516
+ async enforce(requestId, model, messages, metadata, provider) {
517
+ const decision = await this.sendRequest(requestId, model, messages, metadata, provider);
518
+ if (decision.decision === 'block') {
519
+ throw new SignalVaultBlockedError(requestId, decision.violations, decision.dashboard_url);
520
+ }
521
+ if (decision.decision === 'warn' && this.debugMode) {
522
+ console.warn('[SignalVault] Warnings:', decision.violations);
523
+ }
524
+ return decision;
525
+ }
526
+ /**
527
+ * Called when no decision could be obtained. Throws in failMode 'closed';
528
+ * otherwise warns (never silently) and allows.
529
+ */
530
+ unavailable(requestId, key, reason, status) {
531
+ if (this.failMode === 'closed') {
532
+ throw new SignalVaultUnavailableError(requestId, reason, status);
533
+ }
534
+ this.warn(`preflight:${key}`, `[SignalVault] ${reason}. Guardrails were NOT applied — requests are being sent ` +
535
+ `to the provider unchecked (failMode: 'open').`);
536
+ const decision = { ...ALLOW, violations: [], redactions: [] };
537
+ decision[PREFLIGHT_FAILED] =
538
+ RECORDABLE_CAUSES.has(key) ? 'record' : 'skip';
539
+ return decision;
540
+ }
541
+ /**
542
+ * The ai.request body to record in the background when the pre-flight got
543
+ * no decision, so an unchecked call is still in the audit log. Undefined
544
+ * when the pre-flight was recorded normally; null when the server refused
545
+ * it and no audit event should be sent.
546
+ */
547
+ fallbackRequest(decision, requestId, model, messages, metadata, provider) {
548
+ const failed = decision[PREFLIGHT_FAILED];
549
+ if (failed === undefined)
550
+ return undefined;
551
+ if (failed === 'skip')
552
+ return null;
553
+ return {
554
+ ...this.requestBody(requestId, model, messages, metadata, provider),
555
+ payload: { messages, preflight_unavailable: true },
556
+ };
557
+ }
558
+ requestBody(requestId, model, messages, metadata, provider) {
559
+ return {
560
+ type: 'ai.request',
561
+ request_id: requestId,
562
+ environment: this.svEnvironment,
563
+ provider,
564
+ model,
565
+ metadata,
566
+ payload: { messages },
567
+ };
568
+ }
569
+ async describeFailure(status, response) {
570
+ let body;
571
+ try {
572
+ body = await response?.json();
573
+ }
574
+ catch {
575
+ body = undefined;
576
+ }
577
+ const errorType = typeof body?.error === 'object' ? body.error?.type : body?.error;
578
+ switch (status) {
579
+ case 401:
580
+ return ['401', 'SignalVault rejected the API key (401: invalid or revoked)'];
581
+ case 402:
582
+ return ['402', `SignalVault account is not active (402: ${errorType || 'payment required'})`];
583
+ case 403:
584
+ return ['403', `SignalVault denied access (403: ${errorType || 'forbidden'})`];
585
+ case 429:
586
+ return errorType === 'trial_limit_exceeded'
587
+ ? ['429-trial', 'SignalVault trial limit reached (429)']
588
+ : ['429', 'SignalVault rate limit reached (429)'];
589
+ default:
590
+ if (status >= 300 && status < 400) {
591
+ return ['3xx', `SignalVault API redirected (${status}); check that baseUrl is the https:// API URL`];
592
+ }
593
+ return status >= 500
594
+ ? ['5xx', `SignalVault API error (${status})`]
595
+ : [`${status}`, `SignalVault rejected the pre-flight check (${status})`];
596
+ }
408
597
  }
409
598
  // ---------------------------------------------------------------------------
410
599
  // API communication
411
600
  // ---------------------------------------------------------------------------
412
- authHeaders() {
601
+ headers() {
413
602
  return {
414
603
  'Content-Type': 'application/json',
604
+ Accept: 'application/json',
415
605
  Authorization: `Bearer ${this.svApiKey}`,
606
+ 'User-Agent': `signalvault-node/${exports.SDK_VERSION} node/${process.versions.node}`,
416
607
  };
417
608
  }
418
- async sendRequest(requestId, model, messages, metadata, provider) {
609
+ /** Single POST. Never follows redirects, so the API key is never re-sent elsewhere. */
610
+ async post(body, timeoutMs) {
611
+ const response = await fetch(`${this.svBaseUrl}/v1/events`, {
612
+ method: 'POST',
613
+ headers: this.headers(),
614
+ redirect: 'manual',
615
+ signal: AbortSignal.timeout(timeoutMs),
616
+ body: JSON.stringify(body),
617
+ });
618
+ return { ok: response.ok, status: response.status, response };
619
+ }
620
+ /**
621
+ * POST for background events. Adds an `event_id` so a retry is never
622
+ * double-counted, and retries once on connection errors, 5xx, and 429 with a
623
+ * short Retry-After. Timeouts are not retried: the server may be slow rather
624
+ * than down, and a retry would double the time the event is held. Returns
625
+ * the last response, or null if nothing was received.
626
+ */
627
+ async postBackground(body, { eventId = true, retry = true } = {}) {
628
+ const event = eventId ? { event_id: (0, crypto_1.randomUUID)(), ...body } : body;
629
+ let result = null;
630
+ let retryDelayMs = null;
419
631
  try {
420
- const response = await fetch(`${this.svBaseUrl}/v1/events`, {
421
- method: 'POST',
422
- headers: this.authHeaders(),
423
- signal: AbortSignal.timeout(this.preflightTimeout),
424
- body: JSON.stringify({
425
- type: 'ai.request',
426
- request_id: requestId,
427
- environment: this.svEnvironment,
428
- provider,
429
- model,
430
- metadata,
431
- payload: { messages },
432
- }),
433
- });
434
- if (!response.ok) {
435
- if (this.debugMode)
436
- console.error('[SignalVault] API error:', response.status);
437
- return { decision: 'allow', violations: [], redactions: [] };
632
+ result = await this.post(event, this.bgTimeout);
633
+ if (result.ok)
634
+ return result;
635
+ if (result.status === 429) {
636
+ const retryAfter = parseRetryAfterMs(result.response);
637
+ retryDelayMs = retryAfter !== null && retryAfter <= MAX_RETRY_AFTER_MS ? retryAfter : null;
638
+ }
639
+ else if (result.status >= 500) {
640
+ retryDelayMs = 250 + Math.random() * 500;
438
641
  }
439
- return (await response.json());
440
642
  }
441
643
  catch (error) {
442
644
  if (this.debugMode)
443
- console.error('[SignalVault] Pre-flight failed (fail-open):', error);
444
- return { decision: 'allow', violations: [], redactions: [] };
645
+ console.error('[SignalVault] Event send failed:', error);
646
+ if (!isTimeout(error))
647
+ retryDelayMs = 250 + Math.random() * 500;
648
+ }
649
+ if (retry && retryDelayMs !== null) {
650
+ await sleep(retryDelayMs);
651
+ try {
652
+ result = await this.post(event, this.bgTimeout);
653
+ if (result.ok)
654
+ return result;
655
+ }
656
+ catch (error) {
657
+ if (this.debugMode)
658
+ console.error('[SignalVault] Event retry failed:', error);
659
+ }
660
+ }
661
+ this.logUndelivered(result);
662
+ return result;
663
+ }
664
+ logUndelivered(result) {
665
+ if (result?.status === 429) {
666
+ this.warn('bg-429', '[SignalVault] Rate limited (429): audit events are being dropped.');
667
+ }
668
+ else if (this.debugMode) {
669
+ console.error('[SignalVault] Event not delivered:', result ? result.status : 'no response');
445
670
  }
446
671
  }
447
- async sendResponseEvent(requestId, model, output, promptTokens, completionTokens, metadata, provider) {
672
+ async sendRequest(requestId, model, messages, metadata, provider) {
673
+ let result;
448
674
  try {
449
- await fetch(`${this.svBaseUrl}/v1/events`, {
450
- method: 'POST',
451
- headers: this.authHeaders(),
452
- signal: AbortSignal.timeout(this.bgTimeout),
453
- body: JSON.stringify({
454
- type: 'ai.response',
455
- request_id: requestId,
456
- environment: this.svEnvironment,
457
- provider,
458
- model,
459
- metadata,
460
- payload: {
461
- output,
462
- usage: { prompt_tokens: promptTokens, completion_tokens: completionTokens },
463
- },
464
- }),
465
- });
675
+ result = await this.post(this.requestBody(requestId, model, messages, metadata, provider), this.preflightTimeout);
466
676
  }
467
677
  catch (error) {
678
+ const timedOut = isTimeout(error);
468
679
  if (this.debugMode)
469
- console.error('[SignalVault] Failed to send response event:', error);
680
+ console.error('[SignalVault] Pre-flight failed:', error);
681
+ return this.unavailable(requestId, timedOut ? 'timeout' : 'network', timedOut
682
+ ? `SignalVault pre-flight check timed out after ${this.preflightTimeout}ms`
683
+ : 'SignalVault API is unreachable');
684
+ }
685
+ if (!result.ok) {
686
+ const [key, reason] = await this.describeFailure(result.status, result.response);
687
+ return this.unavailable(requestId, key, reason, result.status);
688
+ }
689
+ let data;
690
+ try {
691
+ data = await result.response.json();
692
+ }
693
+ catch (error) {
694
+ if (isTimeout(error)) {
695
+ return this.unavailable(requestId, 'timeout', `SignalVault pre-flight check timed out after ${this.preflightTimeout}ms`);
696
+ }
697
+ return this.unavailable(requestId, 'invalid-response', 'SignalVault returned an invalid response');
470
698
  }
699
+ if (!data || typeof data !== 'object' || !DECISIONS.has(data.decision)) {
700
+ return this.unavailable(requestId, 'invalid-response', 'SignalVault returned an invalid decision');
701
+ }
702
+ return {
703
+ decision: data.decision,
704
+ violations: Array.isArray(data.violations)
705
+ ? data.violations.filter((v) => v !== null && typeof v === 'object')
706
+ : [],
707
+ redactions: Array.isArray(data.redactions) ? data.redactions : [],
708
+ dashboard_url: typeof data.dashboard_url === 'string' ? data.dashboard_url : undefined,
709
+ };
710
+ }
711
+ async sendResponseEvent(requestId, model, output, promptTokens, completionTokens, metadata, provider, fallbackRequest) {
712
+ if (fallbackRequest === null)
713
+ return;
714
+ if (fallbackRequest) {
715
+ // Must land before the response: the server rejects an ai.response whose
716
+ // ai.request it has not stored.
717
+ await this.postBackground(fallbackRequest, { eventId: false });
718
+ }
719
+ await this.postBackground({
720
+ type: 'ai.response',
721
+ request_id: requestId,
722
+ environment: this.svEnvironment,
723
+ provider,
724
+ model,
725
+ metadata,
726
+ payload: {
727
+ output,
728
+ usage: { prompt_tokens: promptTokens, completion_tokens: completionTokens },
729
+ },
730
+ });
471
731
  }
732
+ /**
733
+ * Mirror mode: records the request, then the response. Sequential on
734
+ * purpose — the server rejects an ai.response whose ai.request it has not
735
+ * stored yet, so sending both at once can lose the response.
736
+ */
472
737
  async sendAuditEvents(requestId, model, messages, output, promptTokens, completionTokens, metadata, provider) {
473
738
  const base = {
474
739
  request_id: requestId,
@@ -477,32 +742,20 @@ class SignalVaultClient {
477
742
  model,
478
743
  metadata,
479
744
  };
480
- await Promise.all([
481
- fetch(`${this.svBaseUrl}/v1/events`, {
482
- method: 'POST',
483
- headers: this.authHeaders(),
484
- signal: AbortSignal.timeout(this.bgTimeout),
485
- body: JSON.stringify({
486
- ...base,
487
- type: 'ai.request',
488
- payload: { messages, monitor_mode: true },
489
- }),
490
- }),
491
- fetch(`${this.svBaseUrl}/v1/events`, {
492
- method: 'POST',
493
- headers: this.authHeaders(),
494
- signal: AbortSignal.timeout(this.bgTimeout),
495
- body: JSON.stringify({
496
- ...base,
497
- type: 'ai.response',
498
- payload: {
499
- output,
500
- usage: { prompt_tokens: promptTokens, completion_tokens: completionTokens },
501
- monitor_mode: true,
502
- },
503
- }),
504
- }),
505
- ]);
745
+ await this.postBackground({
746
+ ...base,
747
+ type: 'ai.request',
748
+ payload: { messages, monitor_mode: true },
749
+ });
750
+ await this.postBackground({
751
+ ...base,
752
+ type: 'ai.response',
753
+ payload: {
754
+ output,
755
+ usage: { prompt_tokens: promptTokens, completion_tokens: completionTokens },
756
+ monitor_mode: true,
757
+ },
758
+ });
506
759
  }
507
760
  }
508
761
  exports.SignalVaultClient = SignalVaultClient;