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.
- package/LICENSE +21 -0
- package/README.md +226 -78
- package/dist/auto.d.ts +39 -0
- package/dist/auto.d.ts.map +1 -0
- package/dist/auto.js +105 -0
- package/dist/auto.js.map +1 -0
- package/dist/client.d.ts +219 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +198 -0
- package/dist/client.js.map +1 -0
- package/dist/guard.d.ts +80 -0
- package/dist/guard.d.ts.map +1 -0
- package/dist/guard.js +106 -0
- package/dist/guard.js.map +1 -0
- package/dist/index.d.ts +23 -146
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +42 -150
- package/dist/index.js.map +1 -1
- package/dist/integrations/langchain.d.ts +51 -0
- package/dist/integrations/langchain.d.ts.map +1 -0
- package/dist/integrations/langchain.js +193 -0
- package/dist/integrations/langchain.js.map +1 -0
- package/dist/integrations/vercel-ai.d.ts +49 -0
- package/dist/integrations/vercel-ai.d.ts.map +1 -0
- package/dist/integrations/vercel-ai.js +153 -0
- package/dist/integrations/vercel-ai.js.map +1 -0
- package/dist/patches/anthropic.d.ts +13 -0
- package/dist/patches/anthropic.d.ts.map +1 -0
- package/dist/patches/anthropic.js +146 -0
- package/dist/patches/anthropic.js.map +1 -0
- package/dist/patches/base.d.ts +20 -0
- package/dist/patches/base.d.ts.map +1 -0
- package/dist/patches/base.js +63 -0
- package/dist/patches/base.js.map +1 -0
- package/dist/patches/bedrock.d.ts +14 -0
- package/dist/patches/bedrock.d.ts.map +1 -0
- package/dist/patches/bedrock.js +205 -0
- package/dist/patches/bedrock.js.map +1 -0
- package/dist/patches/cohere.d.ts +14 -0
- package/dist/patches/cohere.d.ts.map +1 -0
- package/dist/patches/cohere.js +143 -0
- package/dist/patches/cohere.js.map +1 -0
- package/dist/patches/google.d.ts +13 -0
- package/dist/patches/google.d.ts.map +1 -0
- package/dist/patches/google.js +127 -0
- package/dist/patches/google.js.map +1 -0
- package/dist/patches/openai.d.ts +14 -0
- package/dist/patches/openai.d.ts.map +1 -0
- package/dist/patches/openai.js +122 -0
- package/dist/patches/openai.js.map +1 -0
- package/dist/resolve.d.ts +9 -0
- package/dist/resolve.d.ts.map +1 -0
- package/dist/resolve.js +19 -0
- package/dist/resolve.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +5 -0
- package/dist/version.js.map +1 -0
- package/package.json +84 -12
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* AWS Bedrock Runtime patch - wraps the `send` method on
|
|
4
|
+
* `BedrockRuntimeClient` from `@aws-sdk/client-bedrock-runtime`.
|
|
5
|
+
*
|
|
6
|
+
* Unlike the other patches which target LLM-specific SDKs, this wraps
|
|
7
|
+
* the AWS SDK v3 command pattern: `client.send(new InvokeModelCommand(...))`
|
|
8
|
+
* and `client.send(new ConverseCommand(...))`.
|
|
9
|
+
*
|
|
10
|
+
* Covers all Bedrock-hosted models: Claude, Titan, Llama, Mistral, Cohere,
|
|
11
|
+
* and any model accessible via the Bedrock Runtime API.
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.apply = apply;
|
|
15
|
+
exports.revert = revert;
|
|
16
|
+
const base_1 = require("./base");
|
|
17
|
+
let originalSend = null;
|
|
18
|
+
let patched = false;
|
|
19
|
+
const BEDROCK_COMMANDS = new Set(["InvokeModelCommand", "ConverseCommand", "ConverseStreamCommand"]);
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Message extraction (Bedrock-specific, handles multiple model formats)
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
function flattenContentBlocks(content) {
|
|
24
|
+
if (typeof content === "string")
|
|
25
|
+
return content;
|
|
26
|
+
if (Array.isArray(content)) {
|
|
27
|
+
const texts = [];
|
|
28
|
+
for (const block of content) {
|
|
29
|
+
if (typeof block === "object" && block !== null) {
|
|
30
|
+
if (block.type === "text" || "text" in block)
|
|
31
|
+
texts.push(String(block.text ?? ""));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return texts.join("\n");
|
|
35
|
+
}
|
|
36
|
+
return String(content ?? "");
|
|
37
|
+
}
|
|
38
|
+
function extractSystem(system, result) {
|
|
39
|
+
if (!system)
|
|
40
|
+
return;
|
|
41
|
+
if (typeof system === "string") {
|
|
42
|
+
result.push({ role: "system", content: system });
|
|
43
|
+
}
|
|
44
|
+
else if (Array.isArray(system)) {
|
|
45
|
+
const texts = [];
|
|
46
|
+
for (const block of system) {
|
|
47
|
+
if (typeof block === "object" && block !== null && "text" in block) {
|
|
48
|
+
texts.push(String(block.text ?? ""));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (texts.length)
|
|
52
|
+
result.push({ role: "system", content: texts.join("\n") });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function extractMessagesFromBody(raw) {
|
|
56
|
+
let body = raw;
|
|
57
|
+
if (body instanceof Uint8Array || Buffer.isBuffer(body)) {
|
|
58
|
+
try {
|
|
59
|
+
body = JSON.parse(new TextDecoder().decode(body));
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (typeof body === "string") {
|
|
66
|
+
try {
|
|
67
|
+
body = JSON.parse(body);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return [{ role: "user", content: body }];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (typeof body !== "object" || body === null)
|
|
74
|
+
return [];
|
|
75
|
+
const obj = body;
|
|
76
|
+
const result = [];
|
|
77
|
+
if (obj.messages && Array.isArray(obj.messages)) {
|
|
78
|
+
extractSystem(obj.system, result);
|
|
79
|
+
for (const msg of obj.messages) {
|
|
80
|
+
if (typeof msg === "object" && msg !== null) {
|
|
81
|
+
result.push({
|
|
82
|
+
role: String(msg.role ?? "user"),
|
|
83
|
+
content: flattenContentBlocks(msg.content ?? ""),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
if (obj.Messages && Array.isArray(obj.Messages)) {
|
|
90
|
+
extractSystem(obj.System ?? obj.system, result);
|
|
91
|
+
for (const msg of obj.Messages) {
|
|
92
|
+
if (typeof msg === "object" && msg !== null) {
|
|
93
|
+
result.push({
|
|
94
|
+
role: String((msg.Role ?? msg.role ?? "user")).toLowerCase(),
|
|
95
|
+
content: flattenContentBlocks(msg.Content ?? msg.content ?? ""),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
if (obj.inputText)
|
|
102
|
+
return [{ role: "user", content: String(obj.inputText) }];
|
|
103
|
+
if (obj.prompt)
|
|
104
|
+
return [{ role: "user", content: String(obj.prompt) }];
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
function extractBedrockResponseText(response, commandName) {
|
|
108
|
+
if (!response || typeof response !== "object")
|
|
109
|
+
return "";
|
|
110
|
+
const obj = response;
|
|
111
|
+
if (commandName === "ConverseCommand") {
|
|
112
|
+
const output = obj.output;
|
|
113
|
+
const message = output?.message;
|
|
114
|
+
const content = message?.content;
|
|
115
|
+
if (Array.isArray(content)) {
|
|
116
|
+
return content
|
|
117
|
+
.map((block) => {
|
|
118
|
+
if (typeof block === "object" && block !== null && "text" in block) {
|
|
119
|
+
return String(block.text);
|
|
120
|
+
}
|
|
121
|
+
return "";
|
|
122
|
+
})
|
|
123
|
+
.join("");
|
|
124
|
+
}
|
|
125
|
+
return "";
|
|
126
|
+
}
|
|
127
|
+
const body = obj.body;
|
|
128
|
+
if (body instanceof Uint8Array || (typeof Buffer !== "undefined" && Buffer.isBuffer(body))) {
|
|
129
|
+
try {
|
|
130
|
+
const parsed = JSON.parse(new TextDecoder().decode(body));
|
|
131
|
+
if (typeof parsed.completion === "string")
|
|
132
|
+
return parsed.completion;
|
|
133
|
+
if (typeof parsed.generation === "string")
|
|
134
|
+
return parsed.generation;
|
|
135
|
+
const results = parsed.results;
|
|
136
|
+
if (Array.isArray(results) && results.length > 0) {
|
|
137
|
+
return String(results[0].outputText ?? results[0].text ?? "");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return "";
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return "";
|
|
145
|
+
}
|
|
146
|
+
// ---------------------------------------------------------------------------
|
|
147
|
+
// Apply / revert
|
|
148
|
+
// ---------------------------------------------------------------------------
|
|
149
|
+
function apply() {
|
|
150
|
+
if (patched)
|
|
151
|
+
return true;
|
|
152
|
+
let BedrockRuntimeClient;
|
|
153
|
+
try {
|
|
154
|
+
const mod = require("@aws-sdk/client-bedrock-runtime");
|
|
155
|
+
BedrockRuntimeClient = mod.BedrockRuntimeClient;
|
|
156
|
+
if (!BedrockRuntimeClient?.prototype?.send)
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
const original = BedrockRuntimeClient.prototype.send;
|
|
163
|
+
originalSend = original;
|
|
164
|
+
BedrockRuntimeClient.prototype.send = (0, base_1.createPatchedMethod)(original, {
|
|
165
|
+
framework: "aws-bedrock",
|
|
166
|
+
shouldIntercept: (args) => {
|
|
167
|
+
const command = args[0];
|
|
168
|
+
const commandName = command?.constructor?.name ?? "";
|
|
169
|
+
return BEDROCK_COMMANDS.has(commandName);
|
|
170
|
+
},
|
|
171
|
+
extractMessages: (args) => {
|
|
172
|
+
const command = args[0];
|
|
173
|
+
const commandName = command?.constructor?.name ?? "";
|
|
174
|
+
const input = (command.input ?? {});
|
|
175
|
+
const modelId = String(input.modelId ?? input.ModelId ?? "bedrock");
|
|
176
|
+
const messages = commandName === "InvokeModelCommand"
|
|
177
|
+
? extractMessagesFromBody(input.body)
|
|
178
|
+
: extractMessagesFromBody(input);
|
|
179
|
+
return { messages, model: modelId };
|
|
180
|
+
},
|
|
181
|
+
extractResponseText: (response, args) => {
|
|
182
|
+
const command = args[0];
|
|
183
|
+
const commandName = command?.constructor?.name ?? "";
|
|
184
|
+
return extractBedrockResponseText(response, commandName) || null;
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
patched = true;
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
function revert() {
|
|
191
|
+
if (!patched || !originalSend)
|
|
192
|
+
return;
|
|
193
|
+
try {
|
|
194
|
+
const mod = require("@aws-sdk/client-bedrock-runtime");
|
|
195
|
+
if (mod.BedrockRuntimeClient?.prototype) {
|
|
196
|
+
mod.BedrockRuntimeClient.prototype.send = originalSend;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
// ignore
|
|
201
|
+
}
|
|
202
|
+
originalSend = null;
|
|
203
|
+
patched = false;
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=bedrock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bedrock.js","sourceRoot":"","sources":["../../src/patches/bedrock.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;AA8IH,sBA0CC;AAED,wBAcC;AArMD,iCAA4C;AAE5C,IAAI,YAAY,GAA6C,IAAI,CAAA;AACjE,IAAI,OAAO,GAAG,KAAK,CAAA;AAEnB,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,oBAAoB,EAAE,iBAAiB,EAAE,uBAAuB,CAAC,CAAC,CAAA;AAEpG,8EAA8E;AAC9E,wEAAwE;AACxE,8EAA8E;AAE9E,SAAS,oBAAoB,CAAC,OAAgB;IAC5C,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,OAAO,CAAA;IAC/C,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC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
2
|
+
* Cohere SDK patch - wraps `Client.prototype.chat` and
|
|
3
|
+
* `ClientV2.prototype.chat` from the `cohere-ai` npm package.
|
|
4
|
+
*
|
|
5
|
+
* Covers direct Cohere usage and frameworks that use it (LangChain.js
|
|
6
|
+
* ChatCohere, Haystack, etc.).
|
|
7
|
+
*
|
|
8
|
+
* Cohere has two client generations:
|
|
9
|
+
* - V1 (`Client`): `chat({ message, chatHistory })` returning `{ text }`
|
|
10
|
+
* - V2 (`ClientV2`): `chat({ messages })` returning `{ message.content }`
|
|
11
|
+
*/
|
|
12
|
+
export declare function apply(): boolean;
|
|
13
|
+
export declare function revert(): void;
|
|
14
|
+
//# sourceMappingURL=cohere.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cohere.d.ts","sourceRoot":"","sources":["../../src/patches/cohere.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAmFH,wBAAgB,KAAK,IAAI,OAAO,CA8B/B;AAED,wBAAgB,MAAM,IAAI,IAAI,CAoB7B"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Cohere SDK patch - wraps `Client.prototype.chat` and
|
|
4
|
+
* `ClientV2.prototype.chat` from the `cohere-ai` npm package.
|
|
5
|
+
*
|
|
6
|
+
* Covers direct Cohere usage and frameworks that use it (LangChain.js
|
|
7
|
+
* ChatCohere, Haystack, etc.).
|
|
8
|
+
*
|
|
9
|
+
* Cohere has two client generations:
|
|
10
|
+
* - V1 (`Client`): `chat({ message, chatHistory })` returning `{ text }`
|
|
11
|
+
* - V2 (`ClientV2`): `chat({ messages })` returning `{ message.content }`
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.apply = apply;
|
|
15
|
+
exports.revert = revert;
|
|
16
|
+
const base_1 = require("./base");
|
|
17
|
+
const originals = new Map();
|
|
18
|
+
let patched = false;
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Message conversion
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
function messagesToGuardFormat(params) {
|
|
23
|
+
const result = [];
|
|
24
|
+
if (params.messages && Array.isArray(params.messages)) {
|
|
25
|
+
for (const msg of params.messages) {
|
|
26
|
+
if (!msg)
|
|
27
|
+
continue;
|
|
28
|
+
const role = String(msg.role ?? "user");
|
|
29
|
+
const content = String(msg.content ?? "");
|
|
30
|
+
result.push({ role, content });
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
if (params.chatHistory && Array.isArray(params.chatHistory)) {
|
|
35
|
+
for (const msg of params.chatHistory) {
|
|
36
|
+
if (!msg)
|
|
37
|
+
continue;
|
|
38
|
+
const role = String(msg.role ?? "user");
|
|
39
|
+
const content = String(msg.message ?? msg.content ?? "");
|
|
40
|
+
result.push({ role, content });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (params.message) {
|
|
44
|
+
result.push({ role: "user", content: String(params.message) });
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
function extractResponseText(response) {
|
|
49
|
+
try {
|
|
50
|
+
const r = response;
|
|
51
|
+
if (typeof r?.text === "string")
|
|
52
|
+
return r.text;
|
|
53
|
+
const message = r?.message;
|
|
54
|
+
if (message?.content) {
|
|
55
|
+
const parts = message.content;
|
|
56
|
+
if (Array.isArray(parts)) {
|
|
57
|
+
const texts = [];
|
|
58
|
+
for (const p of parts) {
|
|
59
|
+
if (p?.text)
|
|
60
|
+
texts.push(String(p.text));
|
|
61
|
+
}
|
|
62
|
+
return texts.length ? texts.join("\n") : null;
|
|
63
|
+
}
|
|
64
|
+
return String(parts);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// ignore
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// Shared config
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
const cohereConfig = {
|
|
76
|
+
framework: "cohere",
|
|
77
|
+
extractMessages: (args) => {
|
|
78
|
+
const params = (args[0] ?? {});
|
|
79
|
+
return {
|
|
80
|
+
messages: messagesToGuardFormat(params),
|
|
81
|
+
model: params.model ? String(params.model) : "cohere",
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
extractResponseText: (response) => extractResponseText(response),
|
|
85
|
+
};
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
// Apply / revert
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
function apply() {
|
|
90
|
+
if (patched)
|
|
91
|
+
return true;
|
|
92
|
+
let cohere;
|
|
93
|
+
try {
|
|
94
|
+
cohere = require("cohere-ai");
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
let patchedAny = false;
|
|
100
|
+
for (const className of ["CohereClientV2", "ClientV2", "CohereClient", "Client"]) {
|
|
101
|
+
try {
|
|
102
|
+
const cls = cohere[className];
|
|
103
|
+
if (!cls?.prototype?.chat)
|
|
104
|
+
continue;
|
|
105
|
+
const key = `${className}.chat`;
|
|
106
|
+
if (originals.has(key))
|
|
107
|
+
continue;
|
|
108
|
+
const original = cls.prototype.chat;
|
|
109
|
+
originals.set(key, original);
|
|
110
|
+
cls.prototype.chat = (0, base_1.createPatchedMethod)(original, cohereConfig);
|
|
111
|
+
patchedAny = true;
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
// class not available in this version
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
patched = patchedAny;
|
|
118
|
+
return patched;
|
|
119
|
+
}
|
|
120
|
+
function revert() {
|
|
121
|
+
if (!patched)
|
|
122
|
+
return;
|
|
123
|
+
try {
|
|
124
|
+
const cohere = require("cohere-ai");
|
|
125
|
+
for (const [key, original] of originals) {
|
|
126
|
+
const className = key.split(".")[0];
|
|
127
|
+
try {
|
|
128
|
+
const cls = cohere[className];
|
|
129
|
+
if (cls?.prototype)
|
|
130
|
+
cls.prototype.chat = original;
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
// ignore
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// ignore
|
|
139
|
+
}
|
|
140
|
+
originals.clear();
|
|
141
|
+
patched = false;
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=cohere.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cohere.js","sourceRoot":"","sources":["../../src/patches/cohere.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;AAmFH,sBA8BC;AAED,wBAoBC;AApID,iCAA8D;AAE9D,MAAM,SAAS,GAAiD,IAAI,GAAG,EAAE,CAAA;AACzE,IAAI,OAAO,GAAG,KAAK,CAAA;AAEnB,8EAA8E;AAC9E,qBAAqB;AACrB,8EAA8E;AAE9E,SAAS,qBAAqB,CAAC,MAA+B;IAC5D,MAAM,MAAM,GAAmB,EAAE,CAAA;IAEjC,IAAI,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;QACtD,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;YAClC,IAAI,CAAC,GAAG;gBAAE,SAAQ;YAClB,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,IAAI,MAAM,CAAC,CAAA;YACvC,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC,CAAA;YACzC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAA;QAChC,CAAC;QACD,OAAO,MAAM,CAAA;IACf,CAAC;IAED,IAAI,MAAM,CAAC,WAAW,IAAI,KAAK,CAAC,OAAO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
|
2
|
+
* Google Generative AI SDK patch - wraps
|
|
3
|
+
* `GenerativeModel.prototype.generateContent` from `@google/generative-ai`.
|
|
4
|
+
*
|
|
5
|
+
* Covers direct Google AI usage and frameworks built on top (LangChain.js
|
|
6
|
+
* ChatGoogleGenerativeAI, etc.).
|
|
7
|
+
*/
|
|
8
|
+
import type { GuardMessage } from "../guard";
|
|
9
|
+
export declare function contentToGuardFormat(contents: unknown): GuardMessage[];
|
|
10
|
+
export declare function extractResponseText(response: unknown): string | null;
|
|
11
|
+
export declare function apply(): boolean;
|
|
12
|
+
export declare function revert(): void;
|
|
13
|
+
//# sourceMappingURL=google.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../../src/patches/google.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAU5C,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,OAAO,GAAG,YAAY,EAAE,CAwBtE;AAcD,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAcpE;AAMD,wBAAgB,KAAK,IAAI,OAAO,CA+B/B;AAED,wBAAgB,MAAM,IAAI,IAAI,CAc7B"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Google Generative AI SDK patch - wraps
|
|
4
|
+
* `GenerativeModel.prototype.generateContent` from `@google/generative-ai`.
|
|
5
|
+
*
|
|
6
|
+
* Covers direct Google AI usage and frameworks built on top (LangChain.js
|
|
7
|
+
* ChatGoogleGenerativeAI, etc.).
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.contentToGuardFormat = contentToGuardFormat;
|
|
11
|
+
exports.extractResponseText = extractResponseText;
|
|
12
|
+
exports.apply = apply;
|
|
13
|
+
exports.revert = revert;
|
|
14
|
+
const base_1 = require("./base");
|
|
15
|
+
let originalGenerate = null;
|
|
16
|
+
let patched = false;
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Message conversion
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
function contentToGuardFormat(contents) {
|
|
21
|
+
if (typeof contents === "string") {
|
|
22
|
+
return [{ role: "user", content: contents }];
|
|
23
|
+
}
|
|
24
|
+
if (!Array.isArray(contents)) {
|
|
25
|
+
return [{ role: "user", content: String(contents ?? "") }];
|
|
26
|
+
}
|
|
27
|
+
const result = [];
|
|
28
|
+
for (const item of contents) {
|
|
29
|
+
if (typeof item === "string") {
|
|
30
|
+
result.push({ role: "user", content: item });
|
|
31
|
+
}
|
|
32
|
+
else if (item?.role && item?.parts) {
|
|
33
|
+
const text = extractTextFromParts(item.parts);
|
|
34
|
+
result.push({ role: item.role ?? "user", content: text });
|
|
35
|
+
}
|
|
36
|
+
else if (item?.parts) {
|
|
37
|
+
result.push({ role: "user", content: extractTextFromParts(item.parts) });
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
result.push({ role: "user", content: String(item) });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
function extractTextFromParts(parts) {
|
|
46
|
+
if (typeof parts === "string")
|
|
47
|
+
return parts;
|
|
48
|
+
if (!Array.isArray(parts))
|
|
49
|
+
return String(parts ?? "");
|
|
50
|
+
const texts = [];
|
|
51
|
+
for (const part of parts) {
|
|
52
|
+
if (typeof part === "string")
|
|
53
|
+
texts.push(part);
|
|
54
|
+
else if (part?.text != null)
|
|
55
|
+
texts.push(String(part.text));
|
|
56
|
+
}
|
|
57
|
+
return texts.join("\n");
|
|
58
|
+
}
|
|
59
|
+
function extractResponseText(response) {
|
|
60
|
+
try {
|
|
61
|
+
const r = response;
|
|
62
|
+
if (typeof r?.text === "function")
|
|
63
|
+
return r.text();
|
|
64
|
+
if (typeof r?.text === "string")
|
|
65
|
+
return r.text;
|
|
66
|
+
const candidates = r?.candidates;
|
|
67
|
+
if (candidates?.[0]?.content) {
|
|
68
|
+
const content = candidates[0].content;
|
|
69
|
+
if (content?.parts)
|
|
70
|
+
return extractTextFromParts(content.parts);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// ignore
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// Apply / revert
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
function apply() {
|
|
82
|
+
if (patched)
|
|
83
|
+
return true;
|
|
84
|
+
let GenerativeModel;
|
|
85
|
+
try {
|
|
86
|
+
const mod = require("@google/generative-ai");
|
|
87
|
+
GenerativeModel = mod.GenerativeModel;
|
|
88
|
+
if (!GenerativeModel?.prototype?.generateContent)
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
const original = GenerativeModel.prototype.generateContent;
|
|
95
|
+
originalGenerate = original;
|
|
96
|
+
GenerativeModel.prototype.generateContent = (0, base_1.createPatchedMethod)(original, {
|
|
97
|
+
framework: "google-generativeai",
|
|
98
|
+
extractMessages: (args, thisArg) => {
|
|
99
|
+
const contents = args[0];
|
|
100
|
+
const self = thisArg;
|
|
101
|
+
const modelName = String(self.model ?? self.modelName ?? "gemini");
|
|
102
|
+
return {
|
|
103
|
+
messages: contents ? contentToGuardFormat(contents) : [],
|
|
104
|
+
model: modelName,
|
|
105
|
+
};
|
|
106
|
+
},
|
|
107
|
+
extractResponseText: (response) => extractResponseText(response),
|
|
108
|
+
});
|
|
109
|
+
patched = true;
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
function revert() {
|
|
113
|
+
if (!patched || !originalGenerate)
|
|
114
|
+
return;
|
|
115
|
+
try {
|
|
116
|
+
const mod = require("@google/generative-ai");
|
|
117
|
+
if (mod.GenerativeModel) {
|
|
118
|
+
mod.GenerativeModel.prototype.generateContent = originalGenerate;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
// ignore
|
|
123
|
+
}
|
|
124
|
+
originalGenerate = null;
|
|
125
|
+
patched = false;
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=google.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/patches/google.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AAYH,oDAwBC;AAcD,kDAcC;AAMD,sBA+BC;AAED,wBAcC;AAlHD,iCAA4C;AAE5C,IAAI,gBAAgB,GAA6C,IAAI,CAAA;AACrE,IAAI,OAAO,GAAG,KAAK,CAAA;AAEnB,8EAA8E;AAC9E,qBAAqB;AACrB,8EAA8E;AAE9E,SAAgB,oBAAoB,CAAC,QAAiB;IACpD,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QACjC,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAA;IAC9C,CAAC;IAED,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC7B,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAC,EAAE,CAAC,CAAA;IAC5D,CAAC;IAED,MAAM,MAAM,GAAmB,EAAE,CAAA;IACjC,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC5B,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAA;QAC9C,CAAC;aAAM,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,KAAK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C,CAAC,CAAC,CAAA;YACxB,MAAM,IAAI,GAAG,OAAkC,CAAA;YAC/C,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,SAAS,IAAI,QAAQ,CAAC,CAAA;YAClE,OAAO;gBACL,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE;gBACxD,KAAK,EAAE,SAAS;aACjB,CAAA;QACH,CAAC;QACD,mBAAmB,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,mBAAmB,CAAC,QAAQ,CAAC;KACjE,CAAC,CAAA;IAEF,OAAO,GAAG,IAAI,CAAA;IACd,OAAO,IAAI,CAAA;AACb,CAAC;AAED,SAAgB,MAAM;IACpB,IAAI,CAAC,OAAO,IAAI,CAAC,gBAAgB;QAAE,OAAM;IAEzC,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAA;QAC5C,IAAI,GAAG,CAAC,eAAe,EAAE,CAAC;YACxB,GAAG,CAAC,eAAe,CAAC,SAAS,CAAC,eAAe,GAAG,gBAAgB,CAAA;QAClE,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,SAAS;IACX,CAAC;IAED,gBAAgB,GAAG,IAAI,CAAA;IACvB,OAAO,GAAG,KAAK,CAAA;AACjB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI SDK patch - wraps the `create` method on the Chat Completions
|
|
3
|
+
* prototype (sync and async).
|
|
4
|
+
*
|
|
5
|
+
* This single patch covers every framework built on the `openai` npm
|
|
6
|
+
* package: LangChain.js (ChatOpenAI), Vercel AI SDK (openai provider),
|
|
7
|
+
* AutoGen.js, CrewAI.js, and direct usage.
|
|
8
|
+
*/
|
|
9
|
+
import type { GuardMessage } from "../guard";
|
|
10
|
+
export declare function messagesToGuardFormat(messages: unknown[]): GuardMessage[];
|
|
11
|
+
export declare function extractResponseContent(response: unknown): string | null;
|
|
12
|
+
export declare function apply(): boolean;
|
|
13
|
+
export declare function revert(): void;
|
|
14
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/patches/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAU5C,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,YAAY,EAAE,CA2BzE;AAED,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAUvE;AAeD,wBAAgB,KAAK,IAAI,OAAO,CAmC/B;AAED,wBAAgB,MAAM,IAAI,IAAI,CAY7B"}
|