safeprompt 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.
package/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # SafePrompt JavaScript/TypeScript SDK
2
+
3
+ Official JavaScript/TypeScript SDK for SafePrompt API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install safeprompt
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```javascript
14
+ import SafePrompt from 'safeprompt';
15
+
16
+ const client = new SafePrompt({ apiKey: 'your-api-key' });
17
+
18
+ const result = await client.check('User input goes here');
19
+
20
+ if (result.safe) {
21
+ // Safe to proceed with LLM
22
+ } else {
23
+ // Handle malicious input
24
+ console.log('Threats detected:', result.threats);
25
+ }
26
+ ```
27
+
28
+ ## API Reference
29
+
30
+ ### `new SafePrompt(config)`
31
+
32
+ Create a new SafePrompt client.
33
+
34
+ **Parameters:**
35
+ - `config.apiKey` (string, required): Your SafePrompt API key
36
+ - `config.baseURL` (string, optional): Custom API base URL
37
+
38
+ ### `client.check(prompt)`
39
+
40
+ Validate a single prompt.
41
+
42
+ **Parameters:**
43
+ - `prompt` (string, required): The user input to validate
44
+
45
+ **Returns:**
46
+ ```typescript
47
+ {
48
+ safe: boolean;
49
+ threats: string[];
50
+ confidence: number;
51
+ processingTimeMs: number;
52
+ passesUsed: number;
53
+ }
54
+ ```
55
+
56
+ ### `client.checkBatch(prompts)`
57
+
58
+ Validate multiple prompts in one request.
59
+
60
+ **Parameters:**
61
+ - `prompts` (string[], required): Array of prompts to validate
62
+
63
+ **Returns:**
64
+ ```typescript
65
+ Array<ValidationResult>
66
+ ```
67
+
68
+ ### `client.getUsage()`
69
+
70
+ Get API usage statistics.
71
+
72
+ **Returns:**
73
+ ```typescript
74
+ {
75
+ requestsThisMonth: number;
76
+ requestsRemaining: number;
77
+ plan: string;
78
+ }
79
+ ```
80
+
81
+ ## Error Handling
82
+
83
+ ```javascript
84
+ try {
85
+ const result = await client.check(userInput);
86
+ } catch (error) {
87
+ if (error instanceof SafePromptError) {
88
+ console.error('API Error:', error.message, error.statusCode);
89
+ } else {
90
+ console.error('Unexpected error:', error);
91
+ }
92
+ }
93
+ ```
94
+
95
+ ## TypeScript Support
96
+
97
+ Full TypeScript support included with type definitions.
98
+
99
+ ## License
100
+
101
+ MIT
@@ -0,0 +1,47 @@
1
+ /**
2
+ * SafePrompt JavaScript/TypeScript SDK
3
+ * API-first prompt injection protection
4
+ */
5
+ export interface SafePromptConfig {
6
+ apiKey: string;
7
+ baseURL?: string;
8
+ }
9
+ export interface ValidationResult {
10
+ safe: boolean;
11
+ threats: string[];
12
+ confidence: number;
13
+ processingTimeMs: number;
14
+ passesUsed: number;
15
+ }
16
+ export interface ValidationError {
17
+ error: string;
18
+ message: string;
19
+ statusCode: number;
20
+ }
21
+ export declare class SafePromptError extends Error {
22
+ statusCode: number;
23
+ constructor(message: string, statusCode: number);
24
+ }
25
+ export default class SafePrompt {
26
+ private apiKey;
27
+ private baseURL;
28
+ constructor(config: SafePromptConfig);
29
+ /**
30
+ * Check if a prompt is safe
31
+ * @param prompt - The user input to validate
32
+ * @returns Validation result indicating if the prompt is safe
33
+ */
34
+ check(prompt: string): Promise<ValidationResult>;
35
+ /**
36
+ * Batch validate multiple prompts
37
+ * @param prompts - Array of prompts to validate
38
+ * @returns Array of validation results
39
+ */
40
+ checkBatch(prompts: string[]): Promise<ValidationResult[]>;
41
+ /**
42
+ * Get API usage statistics
43
+ * @returns Usage statistics for your API key
44
+ */
45
+ getUsage(): Promise<any>;
46
+ }
47
+ export { SafePrompt };
package/dist/index.js ADDED
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ /**
3
+ * SafePrompt JavaScript/TypeScript SDK
4
+ * API-first prompt injection protection
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.SafePrompt = exports.SafePromptError = void 0;
8
+ class SafePromptError extends Error {
9
+ constructor(message, statusCode) {
10
+ super(message);
11
+ this.name = 'SafePromptError';
12
+ this.statusCode = statusCode;
13
+ }
14
+ }
15
+ exports.SafePromptError = SafePromptError;
16
+ class SafePrompt {
17
+ constructor(config) {
18
+ if (!config.apiKey) {
19
+ throw new Error('SafePrompt API key is required');
20
+ }
21
+ this.apiKey = config.apiKey;
22
+ this.baseURL = config.baseURL || 'https://api.safeprompt.dev';
23
+ }
24
+ /**
25
+ * Check if a prompt is safe
26
+ * @param prompt - The user input to validate
27
+ * @returns Validation result indicating if the prompt is safe
28
+ */
29
+ async check(prompt) {
30
+ if (!prompt || typeof prompt !== 'string') {
31
+ throw new Error('Prompt must be a non-empty string');
32
+ }
33
+ try {
34
+ const response = await fetch(`${this.baseURL}/v1/validate`, {
35
+ method: 'POST',
36
+ headers: {
37
+ 'Content-Type': 'application/json',
38
+ 'Authorization': `Bearer ${this.apiKey}`,
39
+ 'User-Agent': 'safeprompt-js/1.0.0'
40
+ },
41
+ body: JSON.stringify({ prompt })
42
+ });
43
+ if (!response.ok) {
44
+ const errorData = await response.json().catch(() => ({}));
45
+ throw new SafePromptError(errorData.message || `API request failed with status ${response.status}`, response.status);
46
+ }
47
+ const data = await response.json();
48
+ return data;
49
+ }
50
+ catch (error) {
51
+ if (error instanceof SafePromptError) {
52
+ throw error;
53
+ }
54
+ if (error instanceof Error) {
55
+ throw new SafePromptError(error.message, 500);
56
+ }
57
+ throw new SafePromptError('Unknown error occurred', 500);
58
+ }
59
+ }
60
+ /**
61
+ * Batch validate multiple prompts
62
+ * @param prompts - Array of prompts to validate
63
+ * @returns Array of validation results
64
+ */
65
+ async checkBatch(prompts) {
66
+ if (!Array.isArray(prompts) || prompts.length === 0) {
67
+ throw new Error('Prompts must be a non-empty array');
68
+ }
69
+ try {
70
+ const response = await fetch(`${this.baseURL}/v1/validate/batch`, {
71
+ method: 'POST',
72
+ headers: {
73
+ 'Content-Type': 'application/json',
74
+ 'Authorization': `Bearer ${this.apiKey}`,
75
+ 'User-Agent': 'safeprompt-js/1.0.0'
76
+ },
77
+ body: JSON.stringify({ prompts })
78
+ });
79
+ if (!response.ok) {
80
+ const errorData = await response.json().catch(() => ({}));
81
+ throw new SafePromptError(errorData.message || `API request failed with status ${response.status}`, response.status);
82
+ }
83
+ const data = await response.json();
84
+ return data.results;
85
+ }
86
+ catch (error) {
87
+ if (error instanceof SafePromptError) {
88
+ throw error;
89
+ }
90
+ if (error instanceof Error) {
91
+ throw new SafePromptError(error.message, 500);
92
+ }
93
+ throw new SafePromptError('Unknown error occurred', 500);
94
+ }
95
+ }
96
+ /**
97
+ * Get API usage statistics
98
+ * @returns Usage statistics for your API key
99
+ */
100
+ async getUsage() {
101
+ try {
102
+ const response = await fetch(`${this.baseURL}/v1/usage`, {
103
+ method: 'GET',
104
+ headers: {
105
+ 'Authorization': `Bearer ${this.apiKey}`,
106
+ 'User-Agent': 'safeprompt-js/1.0.0'
107
+ }
108
+ });
109
+ if (!response.ok) {
110
+ const errorData = await response.json().catch(() => ({}));
111
+ throw new SafePromptError(errorData.message || `API request failed with status ${response.status}`, response.status);
112
+ }
113
+ return await response.json();
114
+ }
115
+ catch (error) {
116
+ if (error instanceof SafePromptError) {
117
+ throw error;
118
+ }
119
+ if (error instanceof Error) {
120
+ throw new SafePromptError(error.message, 500);
121
+ }
122
+ throw new SafePromptError('Unknown error occurred', 500);
123
+ }
124
+ }
125
+ }
126
+ exports.default = SafePrompt;
127
+ exports.SafePrompt = SafePrompt;
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "safeprompt",
3
+ "version": "1.0.0",
4
+ "description": "API-first prompt injection protection for AI applications",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "prepublishOnly": "npm run build"
10
+ },
11
+ "keywords": [
12
+ "prompt-injection",
13
+ "ai-security",
14
+ "llm",
15
+ "openai",
16
+ "security",
17
+ "ai",
18
+ "chatbot",
19
+ "prompt",
20
+ "validation"
21
+ ],
22
+ "author": "SafePrompt <support@safeprompt.dev>",
23
+ "license": "MIT",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/ianreboot/safeprompt"
27
+ },
28
+ "homepage": "https://safeprompt.dev",
29
+ "bugs": {
30
+ "url": "https://github.com/ianreboot/safeprompt/issues"
31
+ },
32
+ "devDependencies": {
33
+ "typescript": "^5.0.0"
34
+ },
35
+ "files": [
36
+ "dist"
37
+ ]
38
+ }