@signalvaultio/node 0.1.0 → 0.3.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.d.ts CHANGED
@@ -1,11 +1,32 @@
1
1
  import OpenAI from 'openai';
2
+ import { ToolsAPI } from './tools';
3
+ export type { ToolRecordOptions, ToolsAPI } from './tools';
2
4
  export interface SignalVaultConfig {
3
5
  apiKey: string;
4
- openaiApiKey: string;
6
+ /** Required for OpenAI (chat.completions). */
7
+ openaiApiKey?: string;
8
+ /** Required for Anthropic (messages). Install @anthropic-ai/sdk separately. */
9
+ anthropicApiKey?: string;
5
10
  baseUrl?: string;
6
11
  environment?: 'development' | 'staging' | 'production';
7
12
  debug?: boolean;
8
13
  mirrorMode?: boolean;
14
+ /**
15
+ * 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
18
+ */
19
+ preflightTimeout?: number;
20
+ /**
21
+ * Timeout (ms) for all background / post-flight event calls.
22
+ * Default: 10000
23
+ */
24
+ timeout?: number;
25
+ /**
26
+ * Default metadata attached to every event. Can be overridden per-call.
27
+ * Use for user_id, feature, workspace_id, etc.
28
+ */
29
+ metadata?: Record<string, unknown>;
9
30
  }
10
31
  export interface SignalVaultDecision {
11
32
  decision: 'allow' | 'warn' | 'block' | 'redact';
@@ -22,50 +43,118 @@ export interface SignalVaultDecision {
22
43
  replacement: string;
23
44
  }>;
24
45
  }
46
+ export interface CreateOptions {
47
+ /** Per-call metadata, merged over config-level metadata. */
48
+ metadata?: Record<string, unknown>;
49
+ }
25
50
  /**
26
- * SignalVault Client — wraps OpenAI with guardrails and audit logging.
51
+ * SignalVault Client — wraps OpenAI and/or Anthropic with guardrails and audit logging.
27
52
  *
28
- * @example
53
+ * @example OpenAI
29
54
  * ```typescript
30
55
  * const client = new SignalVaultClient({
31
56
  * apiKey: 'sk_live_...',
32
57
  * openaiApiKey: process.env.OPENAI_API_KEY!,
33
58
  * baseUrl: 'https://api.signalvault.io',
59
+ * metadata: { user_id: '123' },
34
60
  * });
35
- *
36
- * // Non-streaming
37
61
  * const res = await client.chat.completions.create({ model: 'gpt-4', messages: [...] });
62
+ * ```
38
63
  *
39
- * // Streaming
40
- * const stream = await client.chat.completions.create({ model: 'gpt-4', messages: [...], stream: true });
41
- * for await (const chunk of stream) { ... }
64
+ * @example Anthropic
65
+ * ```typescript
66
+ * const client = new SignalVaultClient({
67
+ * apiKey: 'sk_live_...',
68
+ * anthropicApiKey: process.env.ANTHROPIC_API_KEY!,
69
+ * baseUrl: 'https://api.signalvault.io',
70
+ * });
71
+ * const res = await client.messages.create({ model: 'claude-3-5-sonnet-20241022', messages: [...], max_tokens: 1024 });
42
72
  * ```
43
73
  */
44
74
  export declare class SignalVaultClient {
45
- private signalvaultApiKey;
46
- private signalvaultBaseUrl;
47
- private signalvaultEnvironment;
48
- private debugMode;
49
- private mirrorMode;
50
- private openai;
75
+ private readonly svApiKey;
76
+ private readonly svBaseUrl;
77
+ private readonly svEnvironment;
78
+ private readonly debugMode;
79
+ private readonly mirrorMode;
80
+ private readonly preflightTimeout;
81
+ private readonly bgTimeout;
82
+ private readonly defaultMetadata;
83
+ private readonly openai;
84
+ private readonly anthropic;
51
85
  constructor(config: SignalVaultConfig);
52
86
  /**
53
- * Chat completions with SignalVault guardrails.
54
- * Supports both streaming and non-streaming.
87
+ * Wraps a function so every call records an `agent.tool_call` event with
88
+ * timing, input, output, and any error. The wrapper auto-times, runs the
89
+ * inner function, fires the audit event in the background, and returns the
90
+ * inner result (or rethrows the inner error).
91
+ *
92
+ * Tool calls invoked inside a `withContext({ requestId })` block auto-pick
93
+ * up the requestId for correlation with the parent ai.request.
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * const fetchWeather = signalvault.tool('fetch_weather', async (city: string) => {
98
+ * const res = await fetch(`https://api.example.com/weather?city=${city}`);
99
+ * return res.json();
100
+ * });
101
+ *
102
+ * const weather = await fetchWeather('London');
103
+ * ```
55
104
  */
105
+ tool<TArgs extends readonly unknown[], TReturn>(name: string, fn: (...args: TArgs) => TReturn | Promise<TReturn>, options?: {
106
+ metadata?: Record<string, unknown>;
107
+ }): (...args: TArgs) => Promise<TReturn>;
108
+ /**
109
+ * Manual tool recording API. Use when the wrapper isn't a fit (streaming
110
+ * tools, custom timing, post-hoc capture from logs).
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * await signalvault.tools.record({
115
+ * toolName: 'fetch_weather',
116
+ * toolInput: { city: 'London' },
117
+ * toolOutput: { temp: 12.3 },
118
+ * durationMs: 142,
119
+ * });
120
+ * ```
121
+ */
122
+ get tools(): ToolsAPI;
123
+ /**
124
+ * Runs the given async function inside a context where tool calls are
125
+ * automatically correlated to the provided `requestId`. Useful for grouping
126
+ * a multi-step agent loop under one logical turn.
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * await signalvault.withContext({ requestId: 'agent-turn-abc' }, async () => {
131
+ * const llm = await signalvault.chat.completions.create({...});
132
+ * await fetchWeather('London'); // tool call auto-linked to 'agent-turn-abc'
133
+ * });
134
+ * ```
135
+ */
136
+ withContext<T>(ctx: {
137
+ requestId?: string;
138
+ }, fn: () => Promise<T>): Promise<T>;
139
+ private sendToolCallEvent;
56
140
  get chat(): {
57
141
  completions: {
58
- create: (params: OpenAI.Chat.ChatCompletionCreateParams) => Promise<any>;
142
+ create(params: OpenAI.Chat.ChatCompletionCreateParamsNonStreaming, options?: CreateOptions): Promise<OpenAI.Chat.ChatCompletion>;
143
+ create(params: OpenAI.Chat.ChatCompletionCreateParamsStreaming, options?: CreateOptions): Promise<AsyncGenerator<OpenAI.Chat.Completions.ChatCompletionChunk>>;
59
144
  };
60
145
  };
61
- private handleMirrorMode;
62
- private handleNormalMode;
63
- private wrapStreamForAudit;
64
- private wrapStreamForPostFlight;
146
+ get messages(): {
147
+ create(params: any, options?: CreateOptions): Promise<any>;
148
+ };
149
+ private handleOpenAIMirror;
150
+ private handleOpenAINormal;
151
+ private handleAnthropicMirror;
152
+ private handleAnthropicNormal;
153
+ private wrapStream;
154
+ private wrapAnthropicStream;
155
+ private authHeaders;
65
156
  private sendRequest;
66
- private sendResponse;
67
- private sendStreamResponse;
68
- private sendAudit;
69
- private sendStreamAudit;
157
+ private sendResponseEvent;
158
+ private sendAuditEvents;
70
159
  }
71
160
  export default SignalVaultClient;