promptguard-sdk 1.2.0 → 1.6.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 (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +226 -78
  3. package/dist/auto.d.ts +39 -0
  4. package/dist/auto.d.ts.map +1 -0
  5. package/dist/auto.js +105 -0
  6. package/dist/auto.js.map +1 -0
  7. package/dist/client.d.ts +219 -0
  8. package/dist/client.d.ts.map +1 -0
  9. package/dist/client.js +198 -0
  10. package/dist/client.js.map +1 -0
  11. package/dist/guard.d.ts +80 -0
  12. package/dist/guard.d.ts.map +1 -0
  13. package/dist/guard.js +106 -0
  14. package/dist/guard.js.map +1 -0
  15. package/dist/index.d.ts +23 -146
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +42 -150
  18. package/dist/index.js.map +1 -1
  19. package/dist/integrations/langchain.d.ts +51 -0
  20. package/dist/integrations/langchain.d.ts.map +1 -0
  21. package/dist/integrations/langchain.js +193 -0
  22. package/dist/integrations/langchain.js.map +1 -0
  23. package/dist/integrations/vercel-ai.d.ts +49 -0
  24. package/dist/integrations/vercel-ai.d.ts.map +1 -0
  25. package/dist/integrations/vercel-ai.js +153 -0
  26. package/dist/integrations/vercel-ai.js.map +1 -0
  27. package/dist/patches/anthropic.d.ts +13 -0
  28. package/dist/patches/anthropic.d.ts.map +1 -0
  29. package/dist/patches/anthropic.js +146 -0
  30. package/dist/patches/anthropic.js.map +1 -0
  31. package/dist/patches/base.d.ts +20 -0
  32. package/dist/patches/base.d.ts.map +1 -0
  33. package/dist/patches/base.js +63 -0
  34. package/dist/patches/base.js.map +1 -0
  35. package/dist/patches/bedrock.d.ts +14 -0
  36. package/dist/patches/bedrock.d.ts.map +1 -0
  37. package/dist/patches/bedrock.js +205 -0
  38. package/dist/patches/bedrock.js.map +1 -0
  39. package/dist/patches/cohere.d.ts +14 -0
  40. package/dist/patches/cohere.d.ts.map +1 -0
  41. package/dist/patches/cohere.js +143 -0
  42. package/dist/patches/cohere.js.map +1 -0
  43. package/dist/patches/google.d.ts +13 -0
  44. package/dist/patches/google.d.ts.map +1 -0
  45. package/dist/patches/google.js +127 -0
  46. package/dist/patches/google.js.map +1 -0
  47. package/dist/patches/openai.d.ts +14 -0
  48. package/dist/patches/openai.d.ts.map +1 -0
  49. package/dist/patches/openai.js +122 -0
  50. package/dist/patches/openai.js.map +1 -0
  51. package/dist/resolve.d.ts +9 -0
  52. package/dist/resolve.d.ts.map +1 -0
  53. package/dist/resolve.js +19 -0
  54. package/dist/resolve.js.map +1 -0
  55. package/dist/version.d.ts +2 -0
  56. package/dist/version.d.ts.map +1 -0
  57. package/dist/version.js +5 -0
  58. package/dist/version.js.map +1 -0
  59. package/package.json +84 -12
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ /**
3
+ * OpenAI SDK patch - wraps the `create` method on the Chat Completions
4
+ * prototype (sync and async).
5
+ *
6
+ * This single patch covers every framework built on the `openai` npm
7
+ * package: LangChain.js (ChatOpenAI), Vercel AI SDK (openai provider),
8
+ * AutoGen.js, CrewAI.js, and direct usage.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.messagesToGuardFormat = messagesToGuardFormat;
12
+ exports.extractResponseContent = extractResponseContent;
13
+ exports.apply = apply;
14
+ exports.revert = revert;
15
+ const base_1 = require("./base");
16
+ let originalCreate = null;
17
+ let patched = false;
18
+ // ---------------------------------------------------------------------------
19
+ // Message conversion
20
+ // ---------------------------------------------------------------------------
21
+ function messagesToGuardFormat(messages) {
22
+ if (!messages)
23
+ return [];
24
+ const result = [];
25
+ for (const raw of messages) {
26
+ if (!raw)
27
+ continue;
28
+ const msg = raw;
29
+ const role = String(msg.role ?? "user");
30
+ let content;
31
+ if (typeof msg.content === "string") {
32
+ content = msg.content;
33
+ }
34
+ else if (Array.isArray(msg.content)) {
35
+ const textParts = [];
36
+ for (const part of msg.content) {
37
+ if (typeof part === "string")
38
+ textParts.push(part);
39
+ else if (part?.type === "text")
40
+ textParts.push(part.text ?? "");
41
+ }
42
+ content = textParts.join("\n");
43
+ }
44
+ else {
45
+ content = String(msg.content ?? "");
46
+ }
47
+ result.push({ role, content });
48
+ }
49
+ return result;
50
+ }
51
+ function extractResponseContent(response) {
52
+ try {
53
+ const r = response;
54
+ if (r?.choices?.[0]?.message?.content) {
55
+ return r.choices[0].message.content;
56
+ }
57
+ }
58
+ catch {
59
+ // ignore
60
+ }
61
+ return null;
62
+ }
63
+ function applyRedaction(messages, redacted) {
64
+ return messages.map((msg, i) => {
65
+ if (i < redacted.length && typeof msg === "object" && msg !== null) {
66
+ return { ...msg, content: redacted[i].content };
67
+ }
68
+ return msg;
69
+ });
70
+ }
71
+ // ---------------------------------------------------------------------------
72
+ // Apply / revert
73
+ // ---------------------------------------------------------------------------
74
+ function apply() {
75
+ if (patched)
76
+ return true;
77
+ let Completions;
78
+ try {
79
+ const mod = require("openai/resources/chat/completions");
80
+ Completions = mod.Completions;
81
+ if (!Completions?.prototype?.create)
82
+ return false;
83
+ }
84
+ catch {
85
+ return false;
86
+ }
87
+ const original = Completions.prototype.create;
88
+ originalCreate = original;
89
+ Completions.prototype.create = (0, base_1.createPatchedMethod)(original, {
90
+ framework: "openai",
91
+ extractMessages: (args) => {
92
+ const params = (args[0] ?? {});
93
+ const messages = params.messages;
94
+ return {
95
+ messages: messages ? messagesToGuardFormat(messages) : [],
96
+ model: params.model ? String(params.model) : undefined,
97
+ };
98
+ },
99
+ extractResponseText: (response) => extractResponseContent(response),
100
+ applyRedaction: (args, redactedMessages) => {
101
+ const params = (args[0] ?? {});
102
+ const messages = params.messages;
103
+ return [{ ...params, messages: applyRedaction(messages, redactedMessages) }, ...args.slice(1)];
104
+ },
105
+ });
106
+ patched = true;
107
+ return true;
108
+ }
109
+ function revert() {
110
+ if (!patched || !originalCreate)
111
+ return;
112
+ try {
113
+ const mod = require("openai/resources/chat/completions");
114
+ mod.Completions.prototype.create = originalCreate;
115
+ }
116
+ catch {
117
+ // ignore
118
+ }
119
+ originalCreate = null;
120
+ patched = false;
121
+ }
122
+ //# sourceMappingURL=openai.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/patches/openai.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AAYH,sDA2BC;AAED,wDAUC;AAeD,sBAmCC;AAED,wBAYC;AAhHD,iCAA4C;AAE5C,IAAI,cAAc,GAA6C,IAAI,CAAA;AACnE,IAAI,OAAO,GAAG,KAAK,CAAA;AAEnB,8EAA8E;AAC9E,qBAAqB;AACrB,8EAA8E;AAE9E,SAAgB,qBAAqB,CAAC,QAAmB;IACvD,IAAI,CAAC,QAAQ;QAAE,OAAO,EAAE,CAAA;IACxB,MAAM,MAAM,GAAmB,EAAE,CAAA;IAEjC,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;QAC3B,IAAI,CAAC,GAAG;YAAE,SAAQ;QAClB,MAAM,GAAG,GAAG,GAA8B,CAAA;QAC1C,MAAM,IAAI,GAAW,MAAM,CAAC,GAAG,CAAC,IAAI,IAAI,MAAM,CAAC,CAAA;QAC/C,IAAI,OAAe,CAAA;QAEnB,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;YACpC,OAAO,GAAG,GAAG,CAAC,OAAO,CAAA;QACvB,CAAC;aAAM,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YACtC,MAAM,SAAS,GAAa,EAAE,CAAA;YAC9B,KAAK,MAAM,IAAI,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;gBAC/B,IAAI,OAAO,IAAI,KAAK,QAAQ;oBAAE,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;qBAC7C,IAAI,IAAI,EAAE,IAAI,KAAK,MAAM;oBAAE,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,CAAA;YACjE,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QAChG,CAAC;KACF,CAAC,CAAA;IAEF,OAAO,GAAG,IAAI,CAAA;IACd,OAAO,IAAI,CAAA;AACb,CAAC;AAED,SAAgB,MAAM;IACpB,IAAI,CAAC,OAAO,IAAI,CAAC,cAAc;QAAE,OAAM;IAEvC,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,OAAO,CAAC,mCAAmC,CAAC,CAAA;QACxD,GAAG,CAAC,WAAW,CAAC,SAAS,CAAC,MAAM,GAAG,cAAc,CAAA;IACnD,CAAC;IAAC,MAAM,CAAC;QACP,SAAS;IACX,CAAC;IAED,cAAc,GAAG,IAAI,CAAA;IACrB,OAAO,GAAG,KAAK,CAAA;AACjB,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Shared credential resolution for all PromptGuard entry points.
3
+ */
4
+ export declare const DEFAULT_BASE_URL = "https://api.promptguard.co/api/v1";
5
+ export declare function resolveCredentials(apiKey?: string, baseUrl?: string, defaultBaseUrl?: string): {
6
+ apiKey: string;
7
+ baseUrl: string;
8
+ };
9
+ //# sourceMappingURL=resolve.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../src/resolve.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,eAAO,MAAM,gBAAgB,sCAAsC,CAAA;AAEnE,wBAAgB,kBAAkB,CAChC,MAAM,CAAC,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,MAAM,EAChB,cAAc,SAAmB,GAChC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAWrC"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ /**
3
+ * Shared credential resolution for all PromptGuard entry points.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DEFAULT_BASE_URL = void 0;
7
+ exports.resolveCredentials = resolveCredentials;
8
+ exports.DEFAULT_BASE_URL = "https://api.promptguard.co/api/v1";
9
+ function resolveCredentials(apiKey, baseUrl, defaultBaseUrl = exports.DEFAULT_BASE_URL) {
10
+ const key = apiKey ?? process.env.PROMPTGUARD_API_KEY ?? "";
11
+ if (!key) {
12
+ throw new Error("PromptGuard API key required. Pass apiKey or set the PROMPTGUARD_API_KEY environment variable.");
13
+ }
14
+ return {
15
+ apiKey: key,
16
+ baseUrl: baseUrl ?? process.env.PROMPTGUARD_BASE_URL ?? defaultBaseUrl,
17
+ };
18
+ }
19
+ //# sourceMappingURL=resolve.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolve.js","sourceRoot":"","sources":["../src/resolve.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAIH,gDAeC;AAjBY,QAAA,gBAAgB,GAAG,mCAAmC,CAAA;AAEnE,SAAgB,kBAAkB,CAChC,MAAe,EACf,OAAgB,EAChB,cAAc,GAAG,wBAAgB;IAEjC,MAAM,GAAG,GAAG,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,EAAE,CAAA;IAC3D,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CACb,gGAAgG,CACjG,CAAA;IACH,CAAC;IACD,OAAO;QACL,MAAM,EAAE,GAAG;QACX,OAAO,EAAE,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,oBAAoB,IAAI,cAAc;KACvE,CAAA;AACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const SDK_VERSION = "1.6.0";
2
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,UAAU,CAAA"}
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SDK_VERSION = void 0;
4
+ exports.SDK_VERSION = "1.6.0";
5
+ //# sourceMappingURL=version.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":";;;AAAa,QAAA,WAAW,GAAG,OAAO,CAAA"}
package/package.json CHANGED
@@ -1,13 +1,38 @@
1
1
  {
2
2
  "name": "promptguard-sdk",
3
- "version": "1.2.0",
4
- "description": "Drop-in security for AI applications - AI Firewall SDK",
3
+ "version": "1.6.0",
4
+ "description": "Drop-in security for AI applications - AI Firewall SDK with auto-instrumentation",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ },
12
+ "./auto": {
13
+ "require": "./dist/auto.js",
14
+ "types": "./dist/auto.d.ts"
15
+ },
16
+ "./guard": {
17
+ "require": "./dist/guard.js",
18
+ "types": "./dist/guard.d.ts"
19
+ },
20
+ "./integrations/langchain": {
21
+ "require": "./dist/integrations/langchain.js",
22
+ "types": "./dist/integrations/langchain.d.ts"
23
+ },
24
+ "./integrations/vercel-ai": {
25
+ "require": "./dist/integrations/vercel-ai.js",
26
+ "types": "./dist/integrations/vercel-ai.d.ts"
27
+ }
28
+ },
7
29
  "scripts": {
8
30
  "build": "tsc",
9
- "test": "jest",
10
- "lint": "eslint src/",
31
+ "test": "node --experimental-vm-modules node_modules/.bin/jest",
32
+ "lint": "tsc --noEmit && npx biome check .",
33
+ "format": "npx biome format --write .",
34
+ "format:check": "npx biome format .",
35
+ "check": "npx biome check .",
11
36
  "prepublishOnly": "npm run build"
12
37
  },
13
38
  "keywords": [
@@ -17,31 +42,78 @@
17
42
  "prompt-injection",
18
43
  "openai",
19
44
  "anthropic",
45
+ "google-generativeai",
20
46
  "ai-firewall",
21
47
  "pii-detection",
22
- "llm-security"
48
+ "llm-security",
49
+ "ai-security",
50
+ "langchain",
51
+ "vercel-ai",
52
+ "auto-instrumentation",
53
+ "genai-security",
54
+ "cohere",
55
+ "aws-bedrock"
23
56
  ],
24
57
  "author": "PromptGuard <support@promptguard.co>",
25
58
  "license": "MIT",
26
59
  "repository": {
27
60
  "type": "git",
28
- "url": "https://github.com/acebot712/promptguard.git",
29
- "directory": "packages/sdk-node"
61
+ "url": "https://github.com/acebot712/promptguard-node.git"
30
62
  },
31
63
  "homepage": "https://promptguard.co",
32
64
  "bugs": {
33
- "url": "https://github.com/acebot712/promptguard/issues"
65
+ "url": "https://github.com/acebot712/promptguard-node/issues"
34
66
  },
35
67
  "engines": {
36
- "node": ">=16.0.0"
68
+ "node": ">=20.0.0"
37
69
  },
38
70
  "publishConfig": {
39
71
  "access": "public"
40
72
  },
41
- "files": ["dist", "README.md", "LICENSE"],
42
- "dependencies": {},
73
+ "files": [
74
+ "dist",
75
+ "README.md",
76
+ "LICENSE"
77
+ ],
43
78
  "devDependencies": {
44
- "@types/node": "^20.0.0",
79
+ "@biomejs/biome": "^2.4.4",
80
+ "@types/jest": "^30.0.0",
81
+ "@types/node": "^25.5.0",
82
+ "jest": "^30.3.0",
83
+ "ts-jest": "^29.4.6",
84
+ "ts-node": "^10.9.2",
45
85
  "typescript": "^5.0.0"
86
+ },
87
+ "peerDependencies": {
88
+ "@anthropic-ai/sdk": ">=0.20.0",
89
+ "@aws-sdk/client-bedrock-runtime": ">=3.0.0",
90
+ "@google/generative-ai": ">=0.5.0",
91
+ "@langchain/core": ">=0.1.0",
92
+ "ai": ">=3.0.0",
93
+ "cohere-ai": ">=7.0.0",
94
+ "openai": ">=4.0.0"
95
+ },
96
+ "peerDependenciesMeta": {
97
+ "openai": {
98
+ "optional": true
99
+ },
100
+ "@anthropic-ai/sdk": {
101
+ "optional": true
102
+ },
103
+ "@aws-sdk/client-bedrock-runtime": {
104
+ "optional": true
105
+ },
106
+ "@google/generative-ai": {
107
+ "optional": true
108
+ },
109
+ "@langchain/core": {
110
+ "optional": true
111
+ },
112
+ "ai": {
113
+ "optional": true
114
+ },
115
+ "cohere-ai": {
116
+ "optional": true
117
+ }
46
118
  }
47
119
  }