@safekeylab/sdk 1.0.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.
Files changed (34) hide show
  1. package/README.md +212 -0
  2. package/dist/index.d.mts +100 -0
  3. package/dist/index.d.ts +100 -0
  4. package/dist/index.js +241 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +231 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/dist/integrations/anthropic.d.mts +65 -0
  9. package/dist/integrations/anthropic.d.ts +65 -0
  10. package/dist/integrations/anthropic.js +347 -0
  11. package/dist/integrations/anthropic.js.map +1 -0
  12. package/dist/integrations/anthropic.mjs +341 -0
  13. package/dist/integrations/anthropic.mjs.map +1 -0
  14. package/dist/integrations/langchain.d.mts +106 -0
  15. package/dist/integrations/langchain.d.ts +106 -0
  16. package/dist/integrations/langchain.js +415 -0
  17. package/dist/integrations/langchain.js.map +1 -0
  18. package/dist/integrations/langchain.mjs +406 -0
  19. package/dist/integrations/langchain.mjs.map +1 -0
  20. package/dist/integrations/openai.d.mts +72 -0
  21. package/dist/integrations/openai.d.ts +72 -0
  22. package/dist/integrations/openai.js +327 -0
  23. package/dist/integrations/openai.js.map +1 -0
  24. package/dist/integrations/openai.mjs +321 -0
  25. package/dist/integrations/openai.mjs.map +1 -0
  26. package/dist/integrations/vercel-ai.d.mts +80 -0
  27. package/dist/integrations/vercel-ai.d.ts +80 -0
  28. package/dist/integrations/vercel-ai.js +377 -0
  29. package/dist/integrations/vercel-ai.js.map +1 -0
  30. package/dist/integrations/vercel-ai.mjs +371 -0
  31. package/dist/integrations/vercel-ai.mjs.map +1 -0
  32. package/dist/types-CtDYLaOX.d.mts +129 -0
  33. package/dist/types-CtDYLaOX.d.ts +129 -0
  34. package/package.json +106 -0
@@ -0,0 +1,321 @@
1
+ // src/types.ts
2
+ var SafeKeyLabError = class extends Error {
3
+ constructor(message, code, statusCode) {
4
+ super(message);
5
+ this.code = code;
6
+ this.statusCode = statusCode;
7
+ this.name = "SafeKeyLabError";
8
+ }
9
+ };
10
+ var PIIBlockedError = class extends SafeKeyLabError {
11
+ constructor(detections) {
12
+ super(
13
+ `Request blocked: PII detected (${detections.map((d) => d.type).join(", ")})`,
14
+ "PII_BLOCKED"
15
+ );
16
+ this.detections = detections;
17
+ this.name = "PIIBlockedError";
18
+ }
19
+ };
20
+ var ThreatBlockedError = class extends SafeKeyLabError {
21
+ constructor(threats) {
22
+ super(
23
+ `Request blocked: Threat detected (${threats.map((t) => t.type).join(", ")})`,
24
+ "THREAT_BLOCKED"
25
+ );
26
+ this.threats = threats;
27
+ this.name = "ThreatBlockedError";
28
+ }
29
+ };
30
+
31
+ // src/client.ts
32
+ var DEFAULT_BASE_URL = "https://api.safekeylab.com";
33
+ var DEFAULT_TIMEOUT = 3e4;
34
+ var SafeKeyLab = class {
35
+ constructor(config) {
36
+ if (!config.apiKey) {
37
+ throw new SafeKeyLabError("API key is required", "MISSING_API_KEY");
38
+ }
39
+ this.apiKey = config.apiKey;
40
+ this.baseUrl = config.baseUrl || DEFAULT_BASE_URL;
41
+ this.timeout = config.timeout || DEFAULT_TIMEOUT;
42
+ this.debug = config.debug || false;
43
+ }
44
+ log(...args) {
45
+ if (this.debug) {
46
+ console.log("[SafeKeyLab]", ...args);
47
+ }
48
+ }
49
+ async request(method, endpoint, body) {
50
+ const url = `${this.baseUrl}${endpoint}`;
51
+ const controller = new AbortController();
52
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
53
+ this.log(`${method} ${url}`, body);
54
+ try {
55
+ const response = await fetch(url, {
56
+ method,
57
+ headers: {
58
+ "Authorization": `Bearer ${this.apiKey}`,
59
+ "Content-Type": "application/json",
60
+ "User-Agent": "@safekeylab/sdk/1.0.0"
61
+ },
62
+ body: body ? JSON.stringify(body) : void 0,
63
+ signal: controller.signal
64
+ });
65
+ clearTimeout(timeoutId);
66
+ const data = await response.json();
67
+ if (!response.ok) {
68
+ throw new SafeKeyLabError(
69
+ data.error || data.message || "Request failed",
70
+ data.code || "API_ERROR",
71
+ response.status
72
+ );
73
+ }
74
+ this.log("Response:", data);
75
+ return data;
76
+ } catch (error) {
77
+ clearTimeout(timeoutId);
78
+ if (error instanceof SafeKeyLabError) {
79
+ throw error;
80
+ }
81
+ if (error instanceof Error) {
82
+ if (error.name === "AbortError") {
83
+ throw new SafeKeyLabError("Request timeout", "TIMEOUT");
84
+ }
85
+ throw new SafeKeyLabError(error.message, "NETWORK_ERROR");
86
+ }
87
+ throw new SafeKeyLabError("Unknown error", "UNKNOWN_ERROR");
88
+ }
89
+ }
90
+ /**
91
+ * Detect PII in text
92
+ */
93
+ async detect(request) {
94
+ return this.request("POST", "/v1/detect", request);
95
+ }
96
+ /**
97
+ * Detect PII in text (convenience method)
98
+ */
99
+ async detectPII(text, options) {
100
+ return this.detect({ text, ...options });
101
+ }
102
+ /**
103
+ * Redact/protect PII in text
104
+ */
105
+ async protect(request) {
106
+ return this.request("POST", "/v1/protect", request);
107
+ }
108
+ /**
109
+ * Redact PII from text (convenience method)
110
+ */
111
+ async redact(text, options) {
112
+ const response = await this.protect({ text, ...options });
113
+ return response.redacted_text;
114
+ }
115
+ /**
116
+ * Validate a prompt for security threats
117
+ */
118
+ async validatePrompt(request) {
119
+ return this.request("POST", "/v1/validate-prompt", request);
120
+ }
121
+ /**
122
+ * Check if a prompt is safe (convenience method)
123
+ */
124
+ async isPromptSafe(prompt, context) {
125
+ const response = await this.validatePrompt({ prompt, context });
126
+ return response.is_safe;
127
+ }
128
+ /**
129
+ * Scan LLM output for issues
130
+ */
131
+ async scanOutput(request) {
132
+ return this.request("POST", "/v1/scan-output", request);
133
+ }
134
+ /**
135
+ * Full protection pipeline: validate input, redact PII, return protected text
136
+ */
137
+ async protectInput(text) {
138
+ const [detectResult, validateResult] = await Promise.all([
139
+ this.detect({ text }),
140
+ this.validatePrompt({ prompt: text })
141
+ ]);
142
+ let protectedText = text;
143
+ let wasModified = false;
144
+ if (detectResult.count > 0) {
145
+ const protectResult = await this.protect({ text });
146
+ protectedText = protectResult.redacted_text;
147
+ wasModified = true;
148
+ }
149
+ return {
150
+ text: protectedText,
151
+ wasModified,
152
+ detections: detectResult.detections,
153
+ threats: validateResult.threats
154
+ };
155
+ }
156
+ /**
157
+ * Create a sandbox client (no API key required, rate limited)
158
+ */
159
+ static sandbox(options) {
160
+ return new SafeKeyLabSandbox(options);
161
+ }
162
+ };
163
+ var SafeKeyLabSandbox = class {
164
+ constructor(config) {
165
+ this.baseUrl = config?.baseUrl || DEFAULT_BASE_URL;
166
+ this.timeout = config?.timeout || DEFAULT_TIMEOUT;
167
+ this.debug = config?.debug || false;
168
+ }
169
+ log(...args) {
170
+ if (this.debug) {
171
+ console.log("[SafeKeyLab:Sandbox]", ...args);
172
+ }
173
+ }
174
+ async request(method, endpoint, body) {
175
+ const url = `${this.baseUrl}${endpoint}`;
176
+ const controller = new AbortController();
177
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
178
+ this.log(`${method} ${url}`, body);
179
+ try {
180
+ const response = await fetch(url, {
181
+ method,
182
+ headers: {
183
+ "Content-Type": "application/json",
184
+ "User-Agent": "@safekeylab/sdk/1.0.0"
185
+ },
186
+ body: body ? JSON.stringify(body) : void 0,
187
+ signal: controller.signal
188
+ });
189
+ clearTimeout(timeoutId);
190
+ const data = await response.json();
191
+ if (!response.ok) {
192
+ throw new SafeKeyLabError(
193
+ data.error || data.message || "Request failed",
194
+ data.code || "API_ERROR",
195
+ response.status
196
+ );
197
+ }
198
+ this.log("Response:", data);
199
+ return data;
200
+ } catch (error) {
201
+ clearTimeout(timeoutId);
202
+ if (error instanceof SafeKeyLabError) {
203
+ throw error;
204
+ }
205
+ if (error instanceof Error) {
206
+ if (error.name === "AbortError") {
207
+ throw new SafeKeyLabError("Request timeout", "TIMEOUT");
208
+ }
209
+ throw new SafeKeyLabError(error.message, "NETWORK_ERROR");
210
+ }
211
+ throw new SafeKeyLabError("Unknown error", "UNKNOWN_ERROR");
212
+ }
213
+ }
214
+ async detect(request) {
215
+ return this.request("POST", "/sandbox/v1/detect", request);
216
+ }
217
+ async protect(request) {
218
+ return this.request("POST", "/sandbox/v1/protect", request);
219
+ }
220
+ async validatePrompt(request) {
221
+ return this.request("POST", "/sandbox/v1/validate-prompt", request);
222
+ }
223
+ };
224
+
225
+ // src/integrations/openai.ts
226
+ function wrapOpenAI(openai, options) {
227
+ const safekeylab = new SafeKeyLab({
228
+ apiKey: options.apiKey,
229
+ baseUrl: options.baseUrl,
230
+ timeout: options.timeout,
231
+ debug: options.debug
232
+ });
233
+ const config = {
234
+ blockOnPII: options.blockOnPII ?? false,
235
+ blockOnThreat: options.blockOnThreat ?? true,
236
+ piiTypes: options.piiTypes,
237
+ scanOutputs: options.scanOutputs ?? true,
238
+ onError: options.onError,
239
+ onPIIDetected: options.onPIIDetected,
240
+ onThreatDetected: options.onThreatDetected
241
+ };
242
+ const originalCreate = openai.chat.completions.create.bind(openai.chat.completions);
243
+ const protectedCreate = async (params) => {
244
+ const allDetections = [];
245
+ const allThreats = [];
246
+ const protectedMessages = await Promise.all(
247
+ params.messages.map(async (message) => {
248
+ const content = extractContent(message.content);
249
+ if (!content) return message;
250
+ const [detectResult, validateResult] = await Promise.all([
251
+ safekeylab.detect({ text: content, types: config.piiTypes }),
252
+ safekeylab.validatePrompt({ prompt: content })
253
+ ]);
254
+ if (detectResult.detections.length > 0) {
255
+ allDetections.push(...detectResult.detections);
256
+ config.onPIIDetected?.(detectResult.detections);
257
+ if (config.blockOnPII) {
258
+ throw new PIIBlockedError(detectResult.detections);
259
+ }
260
+ }
261
+ if (validateResult.threats.length > 0) {
262
+ allThreats.push(...validateResult.threats);
263
+ config.onThreatDetected?.(validateResult.threats);
264
+ if (config.blockOnThreat) {
265
+ throw new ThreatBlockedError(validateResult.threats);
266
+ }
267
+ }
268
+ if (detectResult.detections.length > 0) {
269
+ const protectResult = await safekeylab.protect({ text: content });
270
+ return {
271
+ ...message,
272
+ content: protectResult.redacted_text
273
+ };
274
+ }
275
+ return message;
276
+ })
277
+ );
278
+ const response = await originalCreate({
279
+ ...params,
280
+ messages: protectedMessages
281
+ });
282
+ if (config.scanOutputs) {
283
+ for (const choice of response.choices) {
284
+ const outputContent = choice.message?.content;
285
+ if (outputContent) {
286
+ const scanResult = await safekeylab.scanOutput({ output: outputContent });
287
+ if (!scanResult.is_safe) {
288
+ choice._safekeylab = {
289
+ output_issues: scanResult.issues
290
+ };
291
+ }
292
+ }
293
+ }
294
+ }
295
+ response._safekeylab = {
296
+ pii_detected: allDetections,
297
+ threats_detected: allThreats,
298
+ inputs_modified: allDetections.length > 0
299
+ };
300
+ return response;
301
+ };
302
+ openai.chat.completions.create = protectedCreate;
303
+ return openai;
304
+ }
305
+ function extractContent(content) {
306
+ if (typeof content === "string") {
307
+ return content;
308
+ }
309
+ if (Array.isArray(content)) {
310
+ return content.filter((part) => part.type === "text" && part.text).map((part) => part.text).join("\n");
311
+ }
312
+ return null;
313
+ }
314
+ function createProtectedOpenAI(options) {
315
+ return (openai) => wrapOpenAI(openai, options);
316
+ }
317
+ var openai_default = wrapOpenAI;
318
+
319
+ export { createProtectedOpenAI, openai_default as default, wrapOpenAI };
320
+ //# sourceMappingURL=openai.mjs.map
321
+ //# sourceMappingURL=openai.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/types.ts","../../src/client.ts","../../src/integrations/openai.ts"],"names":[],"mappings":";AAgIO,IAAM,eAAA,GAAN,cAA8B,KAAA,CAAM;AAAA,EACzC,WAAA,CACE,OAAA,EACO,IAAA,EACA,UAAA,EACP;AACA,IAAA,KAAA,CAAM,OAAO,CAAA;AAHN,IAAA,IAAA,CAAA,IAAA,GAAA,IAAA;AACA,IAAA,IAAA,CAAA,UAAA,GAAA,UAAA;AAGP,IAAA,IAAA,CAAK,IAAA,GAAO,iBAAA;AAAA,EACd;AACF,CAAA;AAEO,IAAM,eAAA,GAAN,cAA8B,eAAA,CAAgB;AAAA,EACnD,YACS,UAAA,EACP;AACA,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,UAAA,MAAM,aAAa,MAAM,UAAA,CAAW,WAAW,EAAE,MAAA,EAAQ,eAAe,CAAA;AACxE,UAAA,IAAI,CAAC,WAAW,OAAA,EAAS;AAEvB,YAAC,OAAmC,WAAA,GAAc;AAAA,cAChD,eAAe,UAAA,CAAW;AAAA,aAC5B;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAGA,IAAC,SAAqC,WAAA,GAAc;AAAA,MAClD,YAAA,EAAc,aAAA;AAAA,MACd,gBAAA,EAAkB,UAAA;AAAA,MAClB,eAAA,EAAiB,cAAc,MAAA,GAAS;AAAA,KAC1C;AAEA,IAAA,OAAO,QAAA;AAAA,EACT,CAAA;AAGA,EAAA,MAAA,CAAO,IAAA,CAAK,YAAY,MAAA,GAAS,eAAA;AAEjC,EAAA,OAAO,MAAA;AACT;AAKA,SAAS,eACP,OAAA,EACe;AACf,EAAA,IAAI,OAAO,YAAY,QAAA,EAAU;AAC/B,IAAA,OAAO,OAAA;AAAA,EACT;AACA,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,OAAO,CAAA,EAAG;AAC1B,IAAA,OAAO,QACJ,MAAA,CAAO,CAAC,IAAA,KAAS,IAAA,CAAK,SAAS,MAAA,IAAU,IAAA,CAAK,IAAI,CAAA,CAClD,IAAI,CAAC,IAAA,KAAS,KAAK,IAAI,CAAA,CACvB,KAAK,IAAI,CAAA;AAAA,EACd;AACA,EAAA,OAAO,IAAA;AACT;AAKO,SAAS,sBAAsB,OAAA,EAA+B;AACnE,EAAA,OAAO,CAAyB,MAAA,KAAiB,UAAA,CAAW,MAAA,EAAQ,OAAO,CAAA;AAC7E;AAEA,IAAO,cAAA,GAAQ","file":"openai.mjs","sourcesContent":["/**\n * SafeKeyLab SDK Types\n */\n\nexport interface SafeKeyLabConfig {\n /** API key for authentication */\n apiKey: string;\n /** Base URL for the API (default: https://api.safekeylab.com) */\n baseUrl?: string;\n /** Request timeout in milliseconds (default: 30000) */\n timeout?: number;\n /** Enable debug logging (default: false) */\n debug?: boolean;\n}\n\nexport interface PIIDetection {\n /** Type of PII detected (EMAIL, PHONE, SSN, etc.) */\n type: string;\n /** The detected value */\n value: string;\n /** Start position in text */\n start: number;\n /** End position in text */\n end: number;\n /** Confidence score (0-1) */\n confidence: number;\n}\n\nexport interface DetectRequest {\n /** Text to scan for PII */\n text: string;\n /** Types of PII to detect (default: all) */\n types?: string[];\n /** Minimum confidence threshold (default: 0.5) */\n minConfidence?: number;\n}\n\nexport interface DetectResponse {\n success: boolean;\n detections: PIIDetection[];\n count: number;\n processing_time_ms: number;\n}\n\nexport interface ProtectRequest {\n /** Text to redact PII from */\n text: string;\n /** Types of PII to redact (default: all) */\n types?: string[];\n /** Redaction style: 'placeholder' | 'mask' | 'hash' (default: placeholder) */\n style?: 'placeholder' | 'mask' | 'hash';\n}\n\nexport interface Redaction {\n type: string;\n original: string;\n start: number;\n end: number;\n}\n\nexport interface ProtectResponse {\n success: boolean;\n redacted_text: string;\n redactions: Redaction[];\n redactions_count: number;\n original_length: number;\n redacted_length: number;\n processing_time_ms: number;\n}\n\nexport interface ValidatePromptRequest {\n /** Prompt to validate */\n prompt: string;\n /** Additional context for validation */\n context?: string;\n}\n\nexport interface Threat {\n type: string;\n severity: 'low' | 'medium' | 'high' | 'critical';\n description: string;\n matched?: string;\n}\n\nexport interface ValidatePromptResponse {\n success: boolean;\n is_safe: boolean;\n threats: Threat[];\n threats_detected: number;\n recommendation: 'allow' | 'review' | 'block';\n processing_time_ms: number;\n}\n\nexport interface ScanOutputRequest {\n /** LLM output to scan */\n output: string;\n /** Original prompt for context */\n prompt?: string;\n}\n\nexport interface ScanOutputResponse {\n success: boolean;\n is_safe: boolean;\n issues: Array<{\n type: string;\n severity: string;\n description: string;\n }>;\n processing_time_ms: number;\n}\n\nexport interface WrapperConfig {\n /** Block requests containing PII (default: false, will redact instead) */\n blockOnPII?: boolean;\n /** Block requests with detected threats (default: true) */\n blockOnThreat?: boolean;\n /** Types of PII to detect/redact */\n piiTypes?: string[];\n /** Scan LLM outputs for issues (default: true) */\n scanOutputs?: boolean;\n /** Custom error handler */\n onError?: (error: Error) => void;\n /** Callback when PII is detected */\n onPIIDetected?: (detections: PIIDetection[]) => void;\n /** Callback when threat is detected */\n onThreatDetected?: (threats: Threat[]) => void;\n}\n\nexport class SafeKeyLabError extends Error {\n constructor(\n message: string,\n public code: string,\n public statusCode?: number\n ) {\n super(message);\n this.name = 'SafeKeyLabError';\n }\n}\n\nexport class PIIBlockedError extends SafeKeyLabError {\n constructor(\n public detections: PIIDetection[]\n ) {\n super(\n `Request blocked: PII detected (${detections.map(d => d.type).join(', ')})`,\n 'PII_BLOCKED'\n );\n this.name = 'PIIBlockedError';\n }\n}\n\nexport class ThreatBlockedError extends SafeKeyLabError {\n constructor(\n public threats: Threat[]\n ) {\n super(\n `Request blocked: Threat detected (${threats.map(t => t.type).join(', ')})`,\n 'THREAT_BLOCKED'\n );\n this.name = 'ThreatBlockedError';\n }\n}\n","/**\n * SafeKeyLab API Client\n */\n\nimport {\n SafeKeyLabConfig,\n DetectRequest,\n DetectResponse,\n ProtectRequest,\n ProtectResponse,\n ValidatePromptRequest,\n ValidatePromptResponse,\n ScanOutputRequest,\n ScanOutputResponse,\n SafeKeyLabError,\n} from './types';\n\nconst DEFAULT_BASE_URL = 'https://api.safekeylab.com';\nconst DEFAULT_TIMEOUT = 30000;\n\nexport class SafeKeyLab {\n private apiKey: string;\n private baseUrl: string;\n private timeout: number;\n private debug: boolean;\n\n constructor(config: SafeKeyLabConfig) {\n if (!config.apiKey) {\n throw new SafeKeyLabError('API key is required', 'MISSING_API_KEY');\n }\n this.apiKey = config.apiKey;\n this.baseUrl = config.baseUrl || DEFAULT_BASE_URL;\n this.timeout = config.timeout || DEFAULT_TIMEOUT;\n this.debug = config.debug || false;\n }\n\n private log(...args: unknown[]): void {\n if (this.debug) {\n console.log('[SafeKeyLab]', ...args);\n }\n }\n\n private async request<T>(\n method: string,\n endpoint: string,\n body?: unknown\n ): Promise<T> {\n const url = `${this.baseUrl}${endpoint}`;\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), this.timeout);\n\n this.log(`${method} ${url}`, body);\n\n try {\n const response = await fetch(url, {\n method,\n headers: {\n 'Authorization': `Bearer ${this.apiKey}`,\n 'Content-Type': 'application/json',\n 'User-Agent': '@safekeylab/sdk/1.0.0',\n },\n body: body ? JSON.stringify(body) : undefined,\n signal: controller.signal,\n });\n\n clearTimeout(timeoutId);\n\n const data = await response.json() as Record<string, unknown>;\n\n if (!response.ok) {\n throw new SafeKeyLabError(\n (data.error as string) || (data.message as string) || 'Request failed',\n (data.code as string) || 'API_ERROR',\n response.status\n );\n }\n\n this.log('Response:', data);\n return data as T;\n } catch (error) {\n clearTimeout(timeoutId);\n if (error instanceof SafeKeyLabError) {\n throw error;\n }\n if (error instanceof Error) {\n if (error.name === 'AbortError') {\n throw new SafeKeyLabError('Request timeout', 'TIMEOUT');\n }\n throw new SafeKeyLabError(error.message, 'NETWORK_ERROR');\n }\n throw new SafeKeyLabError('Unknown error', 'UNKNOWN_ERROR');\n }\n }\n\n /**\n * Detect PII in text\n */\n async detect(request: DetectRequest): Promise<DetectResponse> {\n return this.request<DetectResponse>('POST', '/v1/detect', request);\n }\n\n /**\n * Detect PII in text (convenience method)\n */\n async detectPII(text: string, options?: Omit<DetectRequest, 'text'>): Promise<DetectResponse> {\n return this.detect({ text, ...options });\n }\n\n /**\n * Redact/protect PII in text\n */\n async protect(request: ProtectRequest): Promise<ProtectResponse> {\n return this.request<ProtectResponse>('POST', '/v1/protect', request);\n }\n\n /**\n * Redact PII from text (convenience method)\n */\n async redact(text: string, options?: Omit<ProtectRequest, 'text'>): Promise<string> {\n const response = await this.protect({ text, ...options });\n return response.redacted_text;\n }\n\n /**\n * Validate a prompt for security threats\n */\n async validatePrompt(request: ValidatePromptRequest): Promise<ValidatePromptResponse> {\n return this.request<ValidatePromptResponse>('POST', '/v1/validate-prompt', request);\n }\n\n /**\n * Check if a prompt is safe (convenience method)\n */\n async isPromptSafe(prompt: string, context?: string): Promise<boolean> {\n const response = await this.validatePrompt({ prompt, context });\n return response.is_safe;\n }\n\n /**\n * Scan LLM output for issues\n */\n async scanOutput(request: ScanOutputRequest): Promise<ScanOutputResponse> {\n return this.request<ScanOutputResponse>('POST', '/v1/scan-output', request);\n }\n\n /**\n * Full protection pipeline: validate input, redact PII, return protected text\n */\n async protectInput(text: string): Promise<{\n text: string;\n wasModified: boolean;\n detections: DetectResponse['detections'];\n threats: ValidatePromptResponse['threats'];\n }> {\n // Run detection and validation in parallel\n const [detectResult, validateResult] = await Promise.all([\n this.detect({ text }),\n this.validatePrompt({ prompt: text }),\n ]);\n\n let protectedText = text;\n let wasModified = false;\n\n // Redact if PII found\n if (detectResult.count > 0) {\n const protectResult = await this.protect({ text });\n protectedText = protectResult.redacted_text;\n wasModified = true;\n }\n\n return {\n text: protectedText,\n wasModified,\n detections: detectResult.detections,\n threats: validateResult.threats,\n };\n }\n\n /**\n * Create a sandbox client (no API key required, rate limited)\n */\n static sandbox(options?: Omit<SafeKeyLabConfig, 'apiKey'>): SafeKeyLabSandbox {\n return new SafeKeyLabSandbox(options);\n }\n}\n\n/**\n * Sandbox client for testing (no API key required)\n */\nexport class SafeKeyLabSandbox {\n private baseUrl: string;\n private timeout: number;\n private debug: boolean;\n\n constructor(config?: Omit<SafeKeyLabConfig, 'apiKey'>) {\n this.baseUrl = config?.baseUrl || DEFAULT_BASE_URL;\n this.timeout = config?.timeout || DEFAULT_TIMEOUT;\n this.debug = config?.debug || false;\n }\n\n private log(...args: unknown[]): void {\n if (this.debug) {\n console.log('[SafeKeyLab:Sandbox]', ...args);\n }\n }\n\n private async request<T>(\n method: string,\n endpoint: string,\n body?: unknown\n ): Promise<T> {\n const url = `${this.baseUrl}${endpoint}`;\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), this.timeout);\n\n this.log(`${method} ${url}`, body);\n\n try {\n const response = await fetch(url, {\n method,\n headers: {\n 'Content-Type': 'application/json',\n 'User-Agent': '@safekeylab/sdk/1.0.0',\n },\n body: body ? JSON.stringify(body) : undefined,\n signal: controller.signal,\n });\n\n clearTimeout(timeoutId);\n const data = await response.json() as Record<string, unknown>;\n\n if (!response.ok) {\n throw new SafeKeyLabError(\n (data.error as string) || (data.message as string) || 'Request failed',\n (data.code as string) || 'API_ERROR',\n response.status\n );\n }\n\n this.log('Response:', data);\n return data as T;\n } catch (error) {\n clearTimeout(timeoutId);\n if (error instanceof SafeKeyLabError) {\n throw error;\n }\n if (error instanceof Error) {\n if (error.name === 'AbortError') {\n throw new SafeKeyLabError('Request timeout', 'TIMEOUT');\n }\n throw new SafeKeyLabError(error.message, 'NETWORK_ERROR');\n }\n throw new SafeKeyLabError('Unknown error', 'UNKNOWN_ERROR');\n }\n }\n\n async detect(request: DetectRequest): Promise<DetectResponse> {\n return this.request<DetectResponse>('POST', '/sandbox/v1/detect', request);\n }\n\n async protect(request: ProtectRequest): Promise<ProtectResponse> {\n return this.request<ProtectResponse>('POST', '/sandbox/v1/protect', request);\n }\n\n async validatePrompt(request: ValidatePromptRequest): Promise<ValidatePromptResponse> {\n return this.request<ValidatePromptResponse>('POST', '/sandbox/v1/validate-prompt', request);\n }\n}\n\nexport default SafeKeyLab;\n","/**\n * SafeKeyLab OpenAI Integration\n *\n * Wraps the OpenAI client to automatically protect inputs and scan outputs\n *\n * @example\n * ```typescript\n * import OpenAI from 'openai';\n * import { wrapOpenAI } from '@safekeylab/sdk/openai';\n *\n * const openai = wrapOpenAI(new OpenAI(), {\n * apiKey: 'sk_live_...',\n * });\n *\n * // All calls are now protected\n * const response = await openai.chat.completions.create({\n * model: 'gpt-4',\n * messages: [{ role: 'user', content: 'My email is john@example.com' }],\n * });\n * // Input is automatically redacted to: \"My email is [EMAIL]\"\n * ```\n */\n\nimport { SafeKeyLab } from '../client';\nimport {\n SafeKeyLabConfig,\n WrapperConfig,\n PIIDetection,\n Threat,\n PIIBlockedError,\n ThreatBlockedError,\n} from '../types';\n\n// Type definitions for OpenAI SDK\ninterface OpenAIMessage {\n role: string;\n content: string | null | Array<{ type: string; text?: string; [key: string]: unknown }>;\n [key: string]: unknown;\n}\n\ninterface OpenAIChatCompletionParams {\n model: string;\n messages: OpenAIMessage[];\n [key: string]: unknown;\n}\n\ninterface OpenAIChoice {\n message: {\n content: string | null;\n [key: string]: unknown;\n };\n [key: string]: unknown;\n}\n\ninterface OpenAIChatCompletion {\n choices: OpenAIChoice[];\n [key: string]: unknown;\n}\n\ninterface OpenAIChatCompletions {\n create(params: OpenAIChatCompletionParams): Promise<OpenAIChatCompletion>;\n}\n\ninterface OpenAIChat {\n completions: OpenAIChatCompletions;\n}\n\ninterface OpenAIClient {\n chat: OpenAIChat;\n [key: string]: unknown;\n}\n\nexport interface OpenAIWrapperOptions extends Partial<SafeKeyLabConfig>, WrapperConfig {}\n\n/**\n * Wrap an OpenAI client with SafeKeyLab protection\n */\nexport function wrapOpenAI<T extends OpenAIClient>(\n openai: T,\n options: OpenAIWrapperOptions\n): T {\n const safekeylab = new SafeKeyLab({\n apiKey: options.apiKey!,\n baseUrl: options.baseUrl,\n timeout: options.timeout,\n debug: options.debug,\n });\n\n const config: WrapperConfig = {\n blockOnPII: options.blockOnPII ?? false,\n blockOnThreat: options.blockOnThreat ?? true,\n piiTypes: options.piiTypes,\n scanOutputs: options.scanOutputs ?? true,\n onError: options.onError,\n onPIIDetected: options.onPIIDetected,\n onThreatDetected: options.onThreatDetected,\n };\n\n // Create a proxy for the chat.completions.create method\n const originalCreate = openai.chat.completions.create.bind(openai.chat.completions);\n\n const protectedCreate = async (\n params: OpenAIChatCompletionParams\n ): Promise<OpenAIChatCompletion> => {\n const allDetections: PIIDetection[] = [];\n const allThreats: Threat[] = [];\n\n // Process each message\n const protectedMessages = await Promise.all(\n params.messages.map(async (message) => {\n const content = extractContent(message.content);\n if (!content) return message;\n\n // Detect PII and validate prompt in parallel\n const [detectResult, validateResult] = await Promise.all([\n safekeylab.detect({ text: content, types: config.piiTypes }),\n safekeylab.validatePrompt({ prompt: content }),\n ]);\n\n // Collect detections\n if (detectResult.detections.length > 0) {\n allDetections.push(...detectResult.detections);\n config.onPIIDetected?.(detectResult.detections);\n\n if (config.blockOnPII) {\n throw new PIIBlockedError(detectResult.detections);\n }\n }\n\n // Collect threats\n if (validateResult.threats.length > 0) {\n allThreats.push(...validateResult.threats);\n config.onThreatDetected?.(validateResult.threats);\n\n if (config.blockOnThreat) {\n throw new ThreatBlockedError(validateResult.threats);\n }\n }\n\n // Redact PII if found\n if (detectResult.detections.length > 0) {\n const protectResult = await safekeylab.protect({ text: content });\n return {\n ...message,\n content: protectResult.redacted_text,\n };\n }\n\n return message;\n })\n );\n\n // Make the API call with protected messages\n const response = await originalCreate({\n ...params,\n messages: protectedMessages,\n });\n\n // Scan outputs if enabled\n if (config.scanOutputs) {\n for (const choice of response.choices) {\n const outputContent = choice.message?.content;\n if (outputContent) {\n const scanResult = await safekeylab.scanOutput({ output: outputContent });\n if (!scanResult.is_safe) {\n // Attach metadata to response\n (choice as Record<string, unknown>)._safekeylab = {\n output_issues: scanResult.issues,\n };\n }\n }\n }\n }\n\n // Attach metadata to response\n (response as Record<string, unknown>)._safekeylab = {\n pii_detected: allDetections,\n threats_detected: allThreats,\n inputs_modified: allDetections.length > 0,\n };\n\n return response;\n };\n\n // Replace the create method\n openai.chat.completions.create = protectedCreate as typeof openai.chat.completions.create;\n\n return openai;\n}\n\n/**\n * Extract text content from OpenAI message content\n */\nfunction extractContent(\n content: string | null | Array<{ type: string; text?: string; [key: string]: unknown }>\n): string | null {\n if (typeof content === 'string') {\n return content;\n }\n if (Array.isArray(content)) {\n return content\n .filter((part) => part.type === 'text' && part.text)\n .map((part) => part.text)\n .join('\\n');\n }\n return null;\n}\n\n/**\n * Create a protected OpenAI client factory\n */\nexport function createProtectedOpenAI(options: OpenAIWrapperOptions) {\n return <T extends OpenAIClient>(openai: T): T => wrapOpenAI(openai, options);\n}\n\nexport default wrapOpenAI;\n"]}
@@ -0,0 +1,80 @@
1
+ import { S as SafeKeyLabConfig, W as WrapperConfig } from '../types-CtDYLaOX.mjs';
2
+
3
+ /**
4
+ * SafeKeyLab Vercel AI SDK Integration
5
+ *
6
+ * Provides middleware and wrappers for the Vercel AI SDK
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { openai } from '@ai-sdk/openai';
11
+ * import { generateText } from 'ai';
12
+ * import { withSafeKeyLab } from '@safekeylab/sdk/vercel-ai';
13
+ *
14
+ * // Wrap the model with SafeKeyLab protection
15
+ * const protectedModel = withSafeKeyLab(openai('gpt-4'), {
16
+ * apiKey: 'sk_live_...',
17
+ * });
18
+ *
19
+ * const { text } = await generateText({
20
+ * model: protectedModel,
21
+ * prompt: 'My email is john@example.com',
22
+ * });
23
+ * // Input is automatically protected
24
+ * ```
25
+ */
26
+
27
+ interface LanguageModelV1 {
28
+ specificationVersion: 'v1';
29
+ provider: string;
30
+ modelId: string;
31
+ defaultObjectGenerationMode?: 'json' | 'tool' | 'grammar';
32
+ doGenerate(options: DoGenerateOptions): Promise<DoGenerateResult>;
33
+ doStream?(options: DoGenerateOptions): Promise<DoStreamResult>;
34
+ }
35
+ interface DoGenerateOptions {
36
+ inputFormat: 'prompt' | 'messages';
37
+ mode: {
38
+ type: string;
39
+ [key: string]: unknown;
40
+ };
41
+ prompt: LanguageModelPrompt;
42
+ [key: string]: unknown;
43
+ }
44
+ interface LanguageModelPrompt {
45
+ system?: string;
46
+ messages?: Array<{
47
+ role: 'user' | 'assistant' | 'system' | 'tool';
48
+ content: string | Array<{
49
+ type: string;
50
+ text?: string;
51
+ [key: string]: unknown;
52
+ }>;
53
+ }>;
54
+ }
55
+ interface DoGenerateResult {
56
+ text?: string;
57
+ toolCalls?: unknown[];
58
+ finishReason: string;
59
+ usage: {
60
+ promptTokens: number;
61
+ completionTokens: number;
62
+ };
63
+ [key: string]: unknown;
64
+ }
65
+ interface DoStreamResult {
66
+ stream: ReadableStream;
67
+ [key: string]: unknown;
68
+ }
69
+ interface VercelAIWrapperOptions extends Partial<SafeKeyLabConfig>, WrapperConfig {
70
+ }
71
+ /**
72
+ * Wrap a Vercel AI SDK model with SafeKeyLab protection
73
+ */
74
+ declare function withSafeKeyLab<T extends LanguageModelV1>(model: T, options: VercelAIWrapperOptions): T;
75
+ /**
76
+ * Create a middleware function for Vercel AI SDK
77
+ */
78
+ declare function createSafeKeyLabMiddleware(options: VercelAIWrapperOptions): <T extends LanguageModelV1>(model: T) => T;
79
+
80
+ export { type VercelAIWrapperOptions, createSafeKeyLabMiddleware, withSafeKeyLab as default, withSafeKeyLab };
@@ -0,0 +1,80 @@
1
+ import { S as SafeKeyLabConfig, W as WrapperConfig } from '../types-CtDYLaOX.js';
2
+
3
+ /**
4
+ * SafeKeyLab Vercel AI SDK Integration
5
+ *
6
+ * Provides middleware and wrappers for the Vercel AI SDK
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { openai } from '@ai-sdk/openai';
11
+ * import { generateText } from 'ai';
12
+ * import { withSafeKeyLab } from '@safekeylab/sdk/vercel-ai';
13
+ *
14
+ * // Wrap the model with SafeKeyLab protection
15
+ * const protectedModel = withSafeKeyLab(openai('gpt-4'), {
16
+ * apiKey: 'sk_live_...',
17
+ * });
18
+ *
19
+ * const { text } = await generateText({
20
+ * model: protectedModel,
21
+ * prompt: 'My email is john@example.com',
22
+ * });
23
+ * // Input is automatically protected
24
+ * ```
25
+ */
26
+
27
+ interface LanguageModelV1 {
28
+ specificationVersion: 'v1';
29
+ provider: string;
30
+ modelId: string;
31
+ defaultObjectGenerationMode?: 'json' | 'tool' | 'grammar';
32
+ doGenerate(options: DoGenerateOptions): Promise<DoGenerateResult>;
33
+ doStream?(options: DoGenerateOptions): Promise<DoStreamResult>;
34
+ }
35
+ interface DoGenerateOptions {
36
+ inputFormat: 'prompt' | 'messages';
37
+ mode: {
38
+ type: string;
39
+ [key: string]: unknown;
40
+ };
41
+ prompt: LanguageModelPrompt;
42
+ [key: string]: unknown;
43
+ }
44
+ interface LanguageModelPrompt {
45
+ system?: string;
46
+ messages?: Array<{
47
+ role: 'user' | 'assistant' | 'system' | 'tool';
48
+ content: string | Array<{
49
+ type: string;
50
+ text?: string;
51
+ [key: string]: unknown;
52
+ }>;
53
+ }>;
54
+ }
55
+ interface DoGenerateResult {
56
+ text?: string;
57
+ toolCalls?: unknown[];
58
+ finishReason: string;
59
+ usage: {
60
+ promptTokens: number;
61
+ completionTokens: number;
62
+ };
63
+ [key: string]: unknown;
64
+ }
65
+ interface DoStreamResult {
66
+ stream: ReadableStream;
67
+ [key: string]: unknown;
68
+ }
69
+ interface VercelAIWrapperOptions extends Partial<SafeKeyLabConfig>, WrapperConfig {
70
+ }
71
+ /**
72
+ * Wrap a Vercel AI SDK model with SafeKeyLab protection
73
+ */
74
+ declare function withSafeKeyLab<T extends LanguageModelV1>(model: T, options: VercelAIWrapperOptions): T;
75
+ /**
76
+ * Create a middleware function for Vercel AI SDK
77
+ */
78
+ declare function createSafeKeyLabMiddleware(options: VercelAIWrapperOptions): <T extends LanguageModelV1>(model: T) => T;
79
+
80
+ export { type VercelAIWrapperOptions, createSafeKeyLabMiddleware, withSafeKeyLab as default, withSafeKeyLab };