@neuraltrust/trustguard-sdk 0.1.3 → 0.1.5
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 +58 -0
- package/dist/ai-sdk/index.cjs +357 -0
- package/dist/ai-sdk/index.cjs.map +1 -0
- package/dist/ai-sdk/index.d.cts +81 -0
- package/dist/ai-sdk/index.d.ts +81 -0
- package/dist/ai-sdk/index.js +329 -0
- package/dist/ai-sdk/index.js.map +1 -0
- package/dist/client-ZoD3M6yg.d.cts +99 -0
- package/dist/client-ZoD3M6yg.d.ts +99 -0
- package/dist/index.cjs +12 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -98
- package/dist/index.d.ts +2 -98
- package/dist/index.js +9 -2
- package/dist/index.js.map +1 -1
- package/package.json +29 -3
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
// src/ai-sdk/trustguard.ts
|
|
2
|
+
var TrustGuardBlockedError = class extends Error {
|
|
3
|
+
phase;
|
|
4
|
+
response;
|
|
5
|
+
constructor(phase, response, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "TrustGuardBlockedError";
|
|
8
|
+
this.phase = phase;
|
|
9
|
+
this.response = response;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var SOURCE_APPLICATION = "vercel-ai-sdk";
|
|
13
|
+
var DEFAULT_BLOCKED_MESSAGE = "This response was blocked by policy.";
|
|
14
|
+
function trustguard(client, options = {}) {
|
|
15
|
+
const guard = new Guard(client, options);
|
|
16
|
+
return {
|
|
17
|
+
middleware: guard.middleware(),
|
|
18
|
+
toolApproval: (approval) => guard.approveToolCall(approval.toolCall),
|
|
19
|
+
tools: (tools) => guard.wrapTools(tools)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
var Guard = class {
|
|
23
|
+
#client;
|
|
24
|
+
#options;
|
|
25
|
+
constructor(client, options) {
|
|
26
|
+
this.#client = client;
|
|
27
|
+
this.#options = options;
|
|
28
|
+
}
|
|
29
|
+
middleware() {
|
|
30
|
+
return {
|
|
31
|
+
specificationVersion: "v4",
|
|
32
|
+
transformParams: async ({ params, model }) => this.#guardPrompt(params, model),
|
|
33
|
+
wrapGenerate: async ({ doGenerate, model }) => {
|
|
34
|
+
const result = await doGenerate();
|
|
35
|
+
return { ...result, content: await this.#guardContent(result.content, model) };
|
|
36
|
+
},
|
|
37
|
+
wrapStream: async ({ doStream, model }) => {
|
|
38
|
+
const result = await doStream();
|
|
39
|
+
const transform = this.#options.stream === "buffer" ? this.#bufferStream(model) : this.#monitorStream(model);
|
|
40
|
+
return { ...result, stream: result.stream.pipeThrough(transform) };
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
async approveToolCall(call) {
|
|
45
|
+
let response;
|
|
46
|
+
try {
|
|
47
|
+
response = await this.#evaluate("tool-call", {
|
|
48
|
+
payload: toolsCall(call.toolName, call.input),
|
|
49
|
+
direction: "input",
|
|
50
|
+
protocol: "mcp",
|
|
51
|
+
attributes: { tool: { name: call.toolName } }
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (this.#failOpen("tool-call", error)) return void 0;
|
|
55
|
+
return { type: "denied", reason: "TrustGuard could not evaluate this tool call." };
|
|
56
|
+
}
|
|
57
|
+
switch (response.status) {
|
|
58
|
+
case "block":
|
|
59
|
+
return { type: "denied", reason: reasonFor(response, "Blocked by TrustGuard") };
|
|
60
|
+
case "ask":
|
|
61
|
+
return this.#options.toolAsk === "deny" ? { type: "denied", reason: reasonFor(response, "TrustGuard requires approval") } : { type: "user-approval", reason: reasonFor(response, "TrustGuard requires approval") };
|
|
62
|
+
case "transform":
|
|
63
|
+
return {
|
|
64
|
+
type: "denied",
|
|
65
|
+
reason: reasonFor(response, "Blocked by TrustGuard because the arguments contain data the policy masks")
|
|
66
|
+
};
|
|
67
|
+
default:
|
|
68
|
+
return void 0;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
wrapTools(tools) {
|
|
72
|
+
const wrapped = {};
|
|
73
|
+
for (const [name, tool] of Object.entries(tools)) {
|
|
74
|
+
const execute = tool.execute;
|
|
75
|
+
if (typeof execute !== "function") {
|
|
76
|
+
wrapped[name] = tool;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
wrapped[name] = {
|
|
80
|
+
...tool,
|
|
81
|
+
execute: (input, executeOptions) => {
|
|
82
|
+
const output = execute.call(tool, input, executeOptions);
|
|
83
|
+
return isAsyncIterable(output) ? this.#guardIterableResult(name, output) : Promise.resolve(output).then((value) => this.#guardResult(name, value));
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
return wrapped;
|
|
88
|
+
}
|
|
89
|
+
async #guardPrompt(params, model) {
|
|
90
|
+
const index = lastUserTurn(params.prompt);
|
|
91
|
+
if (index < 0) return params;
|
|
92
|
+
const message = params.prompt[index];
|
|
93
|
+
const text = joinText(message.content);
|
|
94
|
+
if (!text.trim()) return params;
|
|
95
|
+
let response;
|
|
96
|
+
try {
|
|
97
|
+
response = await this.#evaluate("input", {
|
|
98
|
+
payload: { input: text },
|
|
99
|
+
direction: "input",
|
|
100
|
+
protocol: "llm",
|
|
101
|
+
attributes: modelAttributes(model)
|
|
102
|
+
});
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (this.#failOpen("input", error)) return params;
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
const status = response.status === "ask" && this.#options.promptAsk !== "allow" ? "block" : response.status;
|
|
108
|
+
if (status === "block") {
|
|
109
|
+
throw new TrustGuardBlockedError("input", response, reasonFor(response, "Prompt blocked by TrustGuard"));
|
|
110
|
+
}
|
|
111
|
+
if (status !== "transform") return params;
|
|
112
|
+
const masked = transformedText(response);
|
|
113
|
+
if (masked === void 0) {
|
|
114
|
+
throw new TrustGuardBlockedError(
|
|
115
|
+
"input",
|
|
116
|
+
response,
|
|
117
|
+
"Prompt blocked by TrustGuard: the policy masked it but returned no masked text"
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const prompt = [...params.prompt];
|
|
121
|
+
prompt[index] = { ...message, content: replaceText(message.content, masked) };
|
|
122
|
+
return { ...params, prompt };
|
|
123
|
+
}
|
|
124
|
+
async #guardContent(content, model) {
|
|
125
|
+
const text = joinText(content);
|
|
126
|
+
if (!text.trim()) return content;
|
|
127
|
+
const replacement = await this.#outputReplacement(text, model);
|
|
128
|
+
return replacement === void 0 ? content : replaceText(content, replacement);
|
|
129
|
+
}
|
|
130
|
+
/** The text that must replace a response, or undefined when it can go out as is. Throws when failing closed. */
|
|
131
|
+
async #outputReplacement(text, model) {
|
|
132
|
+
let response;
|
|
133
|
+
try {
|
|
134
|
+
response = await this.#evaluate("output", {
|
|
135
|
+
payload: { input: text },
|
|
136
|
+
direction: "output",
|
|
137
|
+
protocol: "llm",
|
|
138
|
+
attributes: modelAttributes(model)
|
|
139
|
+
});
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (this.#failOpen("output", error)) return void 0;
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
if (response.status === "block") return this.#blockedMessage();
|
|
145
|
+
if (response.status === "transform") return transformedText(response) ?? this.#blockedMessage();
|
|
146
|
+
return void 0;
|
|
147
|
+
}
|
|
148
|
+
#monitorStream(model) {
|
|
149
|
+
let text = "";
|
|
150
|
+
const evaluate = async () => {
|
|
151
|
+
if (!text.trim()) return;
|
|
152
|
+
try {
|
|
153
|
+
await this.#evaluate("output", {
|
|
154
|
+
payload: { input: text },
|
|
155
|
+
direction: "output",
|
|
156
|
+
protocol: "llm",
|
|
157
|
+
attributes: modelAttributes(model)
|
|
158
|
+
});
|
|
159
|
+
} catch (error) {
|
|
160
|
+
this.#options.onError?.({ phase: "output", error });
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
return new TransformStream({
|
|
164
|
+
transform(part, controller) {
|
|
165
|
+
if (part.type === "text-delta") text += part.delta;
|
|
166
|
+
controller.enqueue(part);
|
|
167
|
+
},
|
|
168
|
+
// The stream does not close until this resolves, so a serverless function stays alive until the evaluation is sent.
|
|
169
|
+
async flush() {
|
|
170
|
+
await evaluate();
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
#bufferStream(model) {
|
|
175
|
+
const open = /* @__PURE__ */ new Map();
|
|
176
|
+
const release = async (id, end, controller) => {
|
|
177
|
+
const parts = open.get(id) ?? [];
|
|
178
|
+
open.delete(id);
|
|
179
|
+
const text = parts.map((p) => p.type === "text-delta" ? p.delta : "").join("");
|
|
180
|
+
const replacement = text.trim() ? await this.#outputReplacement(text, model) : void 0;
|
|
181
|
+
if (replacement === void 0) {
|
|
182
|
+
for (const p of parts) controller.enqueue(p);
|
|
183
|
+
} else {
|
|
184
|
+
for (const p of parts) if (p.type !== "text-delta") controller.enqueue(p);
|
|
185
|
+
controller.enqueue({ type: "text-delta", id, delta: replacement });
|
|
186
|
+
}
|
|
187
|
+
if (end) controller.enqueue(end);
|
|
188
|
+
};
|
|
189
|
+
return new TransformStream({
|
|
190
|
+
async transform(part, controller) {
|
|
191
|
+
switch (part.type) {
|
|
192
|
+
case "text-start":
|
|
193
|
+
open.set(part.id, [part]);
|
|
194
|
+
return;
|
|
195
|
+
case "text-delta": {
|
|
196
|
+
const parts = open.get(part.id);
|
|
197
|
+
if (parts) parts.push(part);
|
|
198
|
+
else controller.enqueue(part);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
case "text-end":
|
|
202
|
+
if (open.has(part.id)) await release(part.id, part, controller);
|
|
203
|
+
else controller.enqueue(part);
|
|
204
|
+
return;
|
|
205
|
+
default:
|
|
206
|
+
controller.enqueue(part);
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
async flush(controller) {
|
|
210
|
+
for (const id of [...open.keys()]) await release(id, void 0, controller);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
async #guardResult(toolName, output) {
|
|
215
|
+
const text = typeof output === "string" ? output : JSON.stringify(output);
|
|
216
|
+
if (text === void 0 || !text.trim()) return output;
|
|
217
|
+
let response;
|
|
218
|
+
try {
|
|
219
|
+
response = await this.#evaluate("tool-result", {
|
|
220
|
+
payload: { input: text },
|
|
221
|
+
direction: "output",
|
|
222
|
+
protocol: "mcp",
|
|
223
|
+
attributes: { tool: { name: toolName } }
|
|
224
|
+
});
|
|
225
|
+
} catch (error) {
|
|
226
|
+
if (this.#failOpen("tool-result", error)) return output;
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
if (response.status === "block") {
|
|
230
|
+
throw new TrustGuardBlockedError(
|
|
231
|
+
"tool-result",
|
|
232
|
+
response,
|
|
233
|
+
reasonFor(response, `TrustGuard withheld the result of ${toolName}`)
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
if (response.status !== "transform") return output;
|
|
237
|
+
const masked = transformedText(response);
|
|
238
|
+
if (masked === void 0) {
|
|
239
|
+
throw new TrustGuardBlockedError(
|
|
240
|
+
"tool-result",
|
|
241
|
+
response,
|
|
242
|
+
`TrustGuard withheld the result of ${toolName}: the policy masked it but returned no masked text`
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
if (typeof output === "string") return masked;
|
|
246
|
+
try {
|
|
247
|
+
return JSON.parse(masked);
|
|
248
|
+
} catch {
|
|
249
|
+
return masked;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
async *#guardIterableResult(toolName, output) {
|
|
253
|
+
let last;
|
|
254
|
+
let seen = false;
|
|
255
|
+
for await (const value of output) {
|
|
256
|
+
last = value;
|
|
257
|
+
seen = true;
|
|
258
|
+
yield value;
|
|
259
|
+
}
|
|
260
|
+
if (!seen) return;
|
|
261
|
+
const guarded = await this.#guardResult(toolName, last);
|
|
262
|
+
if (guarded !== last) yield guarded;
|
|
263
|
+
}
|
|
264
|
+
async #evaluate(phase, request) {
|
|
265
|
+
const response = await this.#client.guard({
|
|
266
|
+
...request,
|
|
267
|
+
sessionId: this.#options.sessionId,
|
|
268
|
+
consumerId: this.#options.consumerId,
|
|
269
|
+
attributes: {
|
|
270
|
+
...this.#options.attributes,
|
|
271
|
+
...request.attributes,
|
|
272
|
+
source: { application: SOURCE_APPLICATION }
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
this.#options.onVerdict?.({ phase, response });
|
|
276
|
+
return response;
|
|
277
|
+
}
|
|
278
|
+
#failOpen(phase, error) {
|
|
279
|
+
this.#options.onError?.({ phase, error });
|
|
280
|
+
return this.#options.failMode === "open";
|
|
281
|
+
}
|
|
282
|
+
#blockedMessage() {
|
|
283
|
+
return this.#options.blockedMessage ?? DEFAULT_BLOCKED_MESSAGE;
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
function lastUserTurn(prompt) {
|
|
287
|
+
const last = prompt.length - 1;
|
|
288
|
+
return last >= 0 && prompt[last]?.role === "user" ? last : -1;
|
|
289
|
+
}
|
|
290
|
+
function joinText(parts) {
|
|
291
|
+
return parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("\n");
|
|
292
|
+
}
|
|
293
|
+
function replaceText(parts, text) {
|
|
294
|
+
let placed = false;
|
|
295
|
+
const out = [];
|
|
296
|
+
for (const part of parts) {
|
|
297
|
+
if (part.type !== "text") {
|
|
298
|
+
out.push(part);
|
|
299
|
+
} else if (!placed) {
|
|
300
|
+
out.push({ ...part, text });
|
|
301
|
+
placed = true;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return out;
|
|
305
|
+
}
|
|
306
|
+
function transformedText(response) {
|
|
307
|
+
const input = response.transformedPayload?.input;
|
|
308
|
+
return typeof input === "string" ? input : void 0;
|
|
309
|
+
}
|
|
310
|
+
function toolsCall(name, input) {
|
|
311
|
+
const args = input !== null && typeof input === "object" && !Array.isArray(input) ? input : { input };
|
|
312
|
+
return { jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } };
|
|
313
|
+
}
|
|
314
|
+
function modelAttributes(model) {
|
|
315
|
+
return { model: { name: model.modelId, provider: model.provider } };
|
|
316
|
+
}
|
|
317
|
+
function reasonFor(response, reason) {
|
|
318
|
+
const finding = response.findings.find((f) => f.outcome?.action === response.status) ?? response.findings.find((f) => f.outcome);
|
|
319
|
+
const name = finding?.source.gateName || finding?.source.detectorName || finding?.signal?.type;
|
|
320
|
+
return name ? `${reason}: ${name}` : reason;
|
|
321
|
+
}
|
|
322
|
+
function isAsyncIterable(value) {
|
|
323
|
+
return value !== null && typeof value === "object" && Symbol.asyncIterator in value;
|
|
324
|
+
}
|
|
325
|
+
export {
|
|
326
|
+
TrustGuardBlockedError,
|
|
327
|
+
trustguard
|
|
328
|
+
};
|
|
329
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/ai-sdk/trustguard.ts"],"sourcesContent":["import type {\n LanguageModelV4,\n LanguageModelV4CallOptions,\n LanguageModelV4Content,\n LanguageModelV4Middleware,\n LanguageModelV4Prompt,\n LanguageModelV4StreamPart,\n} from \"@ai-sdk/provider\";\nimport type { ToolApprovalStatus, ToolSet } from \"ai\";\n\nimport type { TrustGuard } from \"../client.js\";\nimport type { GuardRequest, GuardResponse } from \"../types.js\";\n\n/** Where in the agent loop an evaluation ran. */\nexport type GuardPhase = \"input\" | \"output\" | \"tool-call\" | \"tool-result\";\n\n/** Options for {@link trustguard}. Create one instance per request so every evaluation carries the right user. */\nexport interface TrustGuardAISDKOptions {\n /** The end user the request runs for. Findings are grouped under it and gates match it as `consumer.id`. */\n consumerId?: string;\n /** The conversation key. Without it TrustGuard synthesises one per evaluation and turns lose their grouping. */\n sessionId?: string;\n /** Extra attributes merged into every evaluation, for gates and rule conditions. */\n attributes?: Record<string, unknown>;\n /**\n * How streamed responses are guarded.\n * - `\"monitor\"` (default): text streams untouched and is evaluated when the response finishes. Findings are recorded, nothing is enforced.\n * - `\"buffer\"`: each text block is held until it ends, evaluated, then released, redacted or replaced. Enforces the policy at the cost of streaming.\n */\n stream?: \"monitor\" | \"buffer\";\n /** What an `ask` verdict on the prompt does. The model call has no one to ask, so the default is `\"block\"`. */\n promptAsk?: \"block\" | \"allow\";\n /** What an `ask` verdict on a tool call does. The default, `\"user-approval\"`, asks the user through the AI SDK approval flow. */\n toolAsk?: \"user-approval\" | \"deny\";\n /** Text that replaces a response the policy blocked. */\n blockedMessage?: string;\n /**\n * What happens when TrustGuard cannot be reached or returns an error.\n * `\"closed\"` (default) throws on the prompt and the response and denies the tool call. `\"open\"` lets the traffic through.\n * Monitor-mode stream evaluations never fail the stream.\n */\n failMode?: \"closed\" | \"open\";\n /** Called with every verdict, for logging. */\n onVerdict?: (event: { phase: GuardPhase; response: GuardResponse }) => void;\n /** Called with every evaluation error, including the ones `failMode: \"open\"` swallows. */\n onError?: (event: { phase: GuardPhase; error: unknown }) => void;\n}\n\n/** Thrown when the policy blocks the prompt or the response, and by guarded tools whose result was withheld. */\nexport class TrustGuardBlockedError extends Error {\n readonly phase: GuardPhase;\n readonly response: GuardResponse;\n\n constructor(phase: GuardPhase, response: GuardResponse, message: string) {\n super(message);\n this.name = \"TrustGuardBlockedError\";\n this.phase = phase;\n this.response = response;\n }\n}\n\n/** The tool call the approval function receives. Structural, so it matches static and dynamic tool calls alike. */\nexport interface ApprovalToolCall {\n toolName: string;\n toolCallId: string;\n input: unknown;\n}\n\nexport interface TrustGuardAISDK {\n /** Language model middleware for `wrapLanguageModel`: guards the prompt and the response. */\n middleware: LanguageModelV4Middleware;\n /** A `toolApproval` function for `generateText` / `streamText`: guards each tool call before it runs. */\n toolApproval: (options: { toolCall: ApprovalToolCall }) => Promise<ToolApprovalStatus>;\n /** Wraps a tool set so every result is evaluated before the model reads it. */\n tools: <TOOLS extends ToolSet>(tools: TOOLS) => TOOLS;\n}\n\nconst SOURCE_APPLICATION = \"vercel-ai-sdk\";\nconst DEFAULT_BLOCKED_MESSAGE = \"This response was blocked by policy.\";\n\n/**\n * Connects a TrustGuard client to the Vercel AI SDK.\n *\n * ```ts\n * const tg = trustguard(client, { consumerId: user.id, sessionId: chatId });\n * streamText({\n * model: wrapLanguageModel({ model, middleware: tg.middleware }),\n * tools: tg.tools(tools),\n * toolApproval: tg.toolApproval,\n * messages,\n * });\n * ```\n */\nexport function trustguard(client: TrustGuard, options: TrustGuardAISDKOptions = {}): TrustGuardAISDK {\n const guard = new Guard(client, options);\n return {\n middleware: guard.middleware(),\n toolApproval: (approval) => guard.approveToolCall(approval.toolCall),\n tools: (tools) => guard.wrapTools(tools),\n };\n}\n\nclass Guard {\n readonly #client: TrustGuard;\n readonly #options: TrustGuardAISDKOptions;\n\n constructor(client: TrustGuard, options: TrustGuardAISDKOptions) {\n this.#client = client;\n this.#options = options;\n }\n\n middleware(): LanguageModelV4Middleware {\n return {\n specificationVersion: \"v4\",\n transformParams: async ({ params, model }) => this.#guardPrompt(params, model),\n wrapGenerate: async ({ doGenerate, model }) => {\n const result = await doGenerate();\n return { ...result, content: await this.#guardContent(result.content, model) };\n },\n wrapStream: async ({ doStream, model }) => {\n const result = await doStream();\n const transform =\n this.#options.stream === \"buffer\" ? this.#bufferStream(model) : this.#monitorStream(model);\n return { ...result, stream: result.stream.pipeThrough(transform) };\n },\n };\n }\n\n async approveToolCall(call: ApprovalToolCall): Promise<ToolApprovalStatus> {\n let response: GuardResponse | undefined;\n try {\n response = await this.#evaluate(\"tool-call\", {\n payload: toolsCall(call.toolName, call.input),\n direction: \"input\",\n protocol: \"mcp\",\n attributes: { tool: { name: call.toolName } },\n });\n } catch (error) {\n if (this.#failOpen(\"tool-call\", error)) return undefined;\n return { type: \"denied\", reason: \"TrustGuard could not evaluate this tool call.\" };\n }\n\n switch (response.status) {\n case \"block\":\n return { type: \"denied\", reason: reasonFor(response, \"Blocked by TrustGuard\") };\n case \"ask\":\n return this.#options.toolAsk === \"deny\"\n ? { type: \"denied\", reason: reasonFor(response, \"TrustGuard requires approval\") }\n : { type: \"user-approval\", reason: reasonFor(response, \"TrustGuard requires approval\") };\n case \"transform\":\n // Approval cannot rewrite the arguments, and running the call unredacted would leak what the policy masks.\n return {\n type: \"denied\",\n reason: reasonFor(response, \"Blocked by TrustGuard because the arguments contain data the policy masks\"),\n };\n default:\n return undefined;\n }\n }\n\n wrapTools<TOOLS extends ToolSet>(tools: TOOLS): TOOLS {\n const wrapped: Record<string, unknown> = {};\n for (const [name, tool] of Object.entries(tools)) {\n const execute = tool.execute as ((input: unknown, options: unknown) => unknown) | undefined;\n if (typeof execute !== \"function\") {\n wrapped[name] = tool;\n continue;\n }\n wrapped[name] = {\n ...tool,\n execute: (input: unknown, executeOptions: unknown) => {\n const output = execute.call(tool, input, executeOptions);\n return isAsyncIterable(output)\n ? this.#guardIterableResult(name, output)\n : Promise.resolve(output).then((value) => this.#guardResult(name, value));\n },\n };\n }\n return wrapped as TOOLS;\n }\n\n async #guardPrompt(params: LanguageModelV4CallOptions, model: LanguageModelV4): Promise<LanguageModelV4CallOptions> {\n const index = lastUserTurn(params.prompt);\n if (index < 0) return params;\n const message = params.prompt[index] as Extract<LanguageModelV4Prompt[number], { role: \"user\" }>;\n const text = joinText(message.content);\n if (!text.trim()) return params;\n\n let response: GuardResponse;\n try {\n response = await this.#evaluate(\"input\", {\n payload: { input: text },\n direction: \"input\",\n protocol: \"llm\",\n attributes: modelAttributes(model),\n });\n } catch (error) {\n if (this.#failOpen(\"input\", error)) return params;\n throw error;\n }\n\n const status = response.status === \"ask\" && this.#options.promptAsk !== \"allow\" ? \"block\" : response.status;\n if (status === \"block\") {\n throw new TrustGuardBlockedError(\"input\", response, reasonFor(response, \"Prompt blocked by TrustGuard\"));\n }\n if (status !== \"transform\") return params;\n\n const masked = transformedText(response);\n if (masked === undefined) {\n throw new TrustGuardBlockedError(\n \"input\",\n response,\n \"Prompt blocked by TrustGuard: the policy masked it but returned no masked text\",\n );\n }\n const prompt = [...params.prompt];\n prompt[index] = { ...message, content: replaceText(message.content, masked) };\n return { ...params, prompt };\n }\n\n async #guardContent(content: LanguageModelV4Content[], model: LanguageModelV4): Promise<LanguageModelV4Content[]> {\n const text = joinText(content);\n if (!text.trim()) return content;\n\n const replacement = await this.#outputReplacement(text, model);\n return replacement === undefined ? content : replaceText(content, replacement);\n }\n\n /** The text that must replace a response, or undefined when it can go out as is. Throws when failing closed. */\n async #outputReplacement(text: string, model: LanguageModelV4): Promise<string | undefined> {\n let response: GuardResponse;\n try {\n response = await this.#evaluate(\"output\", {\n payload: { input: text },\n direction: \"output\",\n protocol: \"llm\",\n attributes: modelAttributes(model),\n });\n } catch (error) {\n if (this.#failOpen(\"output\", error)) return undefined;\n throw error;\n }\n if (response.status === \"block\") return this.#blockedMessage();\n if (response.status === \"transform\") return transformedText(response) ?? this.#blockedMessage();\n return undefined;\n }\n\n #monitorStream(model: LanguageModelV4): TransformStream<LanguageModelV4StreamPart, LanguageModelV4StreamPart> {\n let text = \"\";\n const evaluate = async () => {\n if (!text.trim()) return;\n try {\n await this.#evaluate(\"output\", {\n payload: { input: text },\n direction: \"output\",\n protocol: \"llm\",\n attributes: modelAttributes(model),\n });\n } catch (error) {\n this.#options.onError?.({ phase: \"output\", error });\n }\n };\n return new TransformStream({\n transform(part, controller) {\n if (part.type === \"text-delta\") text += part.delta;\n controller.enqueue(part);\n },\n // The stream does not close until this resolves, so a serverless function stays alive until the evaluation is sent.\n async flush() {\n await evaluate();\n },\n });\n }\n\n #bufferStream(model: LanguageModelV4): TransformStream<LanguageModelV4StreamPart, LanguageModelV4StreamPart> {\n const open = new Map<string, LanguageModelV4StreamPart[]>();\n const release = async (\n id: string,\n end: LanguageModelV4StreamPart | undefined,\n controller: TransformStreamDefaultController<LanguageModelV4StreamPart>,\n ) => {\n const parts = open.get(id) ?? [];\n open.delete(id);\n const text = parts.map((p) => (p.type === \"text-delta\" ? p.delta : \"\")).join(\"\");\n const replacement = text.trim() ? await this.#outputReplacement(text, model) : undefined;\n if (replacement === undefined) {\n for (const p of parts) controller.enqueue(p);\n } else {\n for (const p of parts) if (p.type !== \"text-delta\") controller.enqueue(p);\n controller.enqueue({ type: \"text-delta\", id, delta: replacement });\n }\n if (end) controller.enqueue(end);\n };\n return new TransformStream({\n async transform(part, controller) {\n switch (part.type) {\n case \"text-start\":\n open.set(part.id, [part]);\n return;\n case \"text-delta\": {\n const parts = open.get(part.id);\n if (parts) parts.push(part);\n else controller.enqueue(part);\n return;\n }\n case \"text-end\":\n if (open.has(part.id)) await release(part.id, part, controller);\n else controller.enqueue(part);\n return;\n default:\n controller.enqueue(part);\n }\n },\n async flush(controller) {\n // A stream that ends without text-end still goes through the policy before anything is released.\n for (const id of [...open.keys()]) await release(id, undefined, controller);\n },\n });\n }\n\n async #guardResult(toolName: string, output: unknown): Promise<unknown> {\n const text = typeof output === \"string\" ? output : JSON.stringify(output);\n if (text === undefined || !text.trim()) return output;\n\n let response: GuardResponse;\n try {\n response = await this.#evaluate(\"tool-result\", {\n payload: { input: text },\n direction: \"output\",\n protocol: \"mcp\",\n attributes: { tool: { name: toolName } },\n });\n } catch (error) {\n if (this.#failOpen(\"tool-result\", error)) return output;\n throw error;\n }\n\n if (response.status === \"block\") {\n throw new TrustGuardBlockedError(\n \"tool-result\",\n response,\n reasonFor(response, `TrustGuard withheld the result of ${toolName}`),\n );\n }\n if (response.status !== \"transform\") return output;\n\n const masked = transformedText(response);\n if (masked === undefined) {\n throw new TrustGuardBlockedError(\n \"tool-result\",\n response,\n `TrustGuard withheld the result of ${toolName}: the policy masked it but returned no masked text`,\n );\n }\n if (typeof output === \"string\") return masked;\n try {\n return JSON.parse(masked) as unknown;\n } catch {\n return masked;\n }\n }\n\n async *#guardIterableResult(toolName: string, output: AsyncIterable<unknown>): AsyncGenerator<unknown> {\n // Preliminary outputs reach the UI only. The last one is what the model reads, so that is the one guarded:\n // when the policy changes it, the guarded value is yielded after it and becomes the final output.\n let last: unknown;\n let seen = false;\n for await (const value of output) {\n last = value;\n seen = true;\n yield value;\n }\n if (!seen) return;\n const guarded = await this.#guardResult(toolName, last);\n if (guarded !== last) yield guarded;\n }\n\n async #evaluate(phase: GuardPhase, request: GuardRequest): Promise<GuardResponse> {\n const response = await this.#client.guard({\n ...request,\n sessionId: this.#options.sessionId,\n consumerId: this.#options.consumerId,\n attributes: {\n ...this.#options.attributes,\n ...request.attributes,\n source: { application: SOURCE_APPLICATION },\n },\n });\n this.#options.onVerdict?.({ phase, response });\n return response;\n }\n\n #failOpen(phase: GuardPhase, error: unknown): boolean {\n this.#options.onError?.({ phase, error });\n return this.#options.failMode === \"open\";\n }\n\n #blockedMessage(): string {\n return this.#options.blockedMessage ?? DEFAULT_BLOCKED_MESSAGE;\n }\n}\n\n/** Index of the user message that starts this model call, or -1 when the call continues a tool loop. */\nfunction lastUserTurn(prompt: LanguageModelV4Prompt): number {\n const last = prompt.length - 1;\n return last >= 0 && prompt[last]?.role === \"user\" ? last : -1;\n}\n\ntype TextLike = { type: string; text?: string };\n\nfunction joinText(parts: readonly TextLike[]): string {\n return parts\n .filter((p) => p.type === \"text\" && typeof p.text === \"string\")\n .map((p) => p.text)\n .join(\"\\n\");\n}\n\n/** Puts `text` in place of the first text part and drops the others, keeping every non-text part where it was. */\nfunction replaceText<T extends TextLike>(parts: readonly T[], text: string): T[] {\n let placed = false;\n const out: T[] = [];\n for (const part of parts) {\n if (part.type !== \"text\") {\n out.push(part);\n } else if (!placed) {\n out.push({ ...part, text });\n placed = true;\n }\n }\n return out;\n}\n\nfunction transformedText(response: GuardResponse): string | undefined {\n const input = response.transformedPayload?.input;\n return typeof input === \"string\" ? input : undefined;\n}\n\nfunction toolsCall(name: string, input: unknown): Record<string, unknown> {\n const args = input !== null && typeof input === \"object\" && !Array.isArray(input) ? input : { input };\n return { jsonrpc: \"2.0\", id: 1, method: \"tools/call\", params: { name, arguments: args } };\n}\n\nfunction modelAttributes(model: LanguageModelV4): Record<string, unknown> {\n return { model: { name: model.modelId, provider: model.provider } };\n}\n\n/** `reason`, followed by the gate or detector that produced the verdict when the response names one. */\nfunction reasonFor(response: GuardResponse, reason: string): string {\n const finding =\n response.findings.find((f) => f.outcome?.action === response.status) ?? response.findings.find((f) => f.outcome);\n const name = finding?.source.gateName || finding?.source.detectorName || finding?.signal?.type;\n return name ? `${reason}: ${name}` : reason;\n}\n\nfunction isAsyncIterable(value: unknown): value is AsyncIterable<unknown> {\n return value !== null && typeof value === \"object\" && Symbol.asyncIterator in value;\n}\n\n"],"mappings":";AAiDO,IAAM,yBAAN,cAAqC,MAAM;AAAA,EACvC;AAAA,EACA;AAAA,EAET,YAAY,OAAmB,UAAyB,SAAiB;AACvE,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,QAAQ;AACb,SAAK,WAAW;AAAA,EAClB;AACF;AAkBA,IAAM,qBAAqB;AAC3B,IAAM,0BAA0B;AAezB,SAAS,WAAW,QAAoB,UAAkC,CAAC,GAAoB;AACpG,QAAM,QAAQ,IAAI,MAAM,QAAQ,OAAO;AACvC,SAAO;AAAA,IACL,YAAY,MAAM,WAAW;AAAA,IAC7B,cAAc,CAAC,aAAa,MAAM,gBAAgB,SAAS,QAAQ;AAAA,IACnE,OAAO,CAAC,UAAU,MAAM,UAAU,KAAK;AAAA,EACzC;AACF;AAEA,IAAM,QAAN,MAAY;AAAA,EACD;AAAA,EACA;AAAA,EAET,YAAY,QAAoB,SAAiC;AAC/D,SAAK,UAAU;AACf,SAAK,WAAW;AAAA,EAClB;AAAA,EAEA,aAAwC;AACtC,WAAO;AAAA,MACL,sBAAsB;AAAA,MACtB,iBAAiB,OAAO,EAAE,QAAQ,MAAM,MAAM,KAAK,aAAa,QAAQ,KAAK;AAAA,MAC7E,cAAc,OAAO,EAAE,YAAY,MAAM,MAAM;AAC7C,cAAM,SAAS,MAAM,WAAW;AAChC,eAAO,EAAE,GAAG,QAAQ,SAAS,MAAM,KAAK,cAAc,OAAO,SAAS,KAAK,EAAE;AAAA,MAC/E;AAAA,MACA,YAAY,OAAO,EAAE,UAAU,MAAM,MAAM;AACzC,cAAM,SAAS,MAAM,SAAS;AAC9B,cA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;AACA,SAAO;AACT;AAEA,SAAS,gBAAgB,UAA6C;AACpE,QAAM,QAAQ,SAAS,oBAAoB;AAC3C,SAAO,OAAO,UAAU,WAAW,QAAQ;AAC7C;AAEA,SAAS,UAAU,MAAc,OAAyC;AACxE,QAAM,OAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,IAAI,QAAQ,EAAE,MAAM;AACpG,SAAO,EAAE,SAAS,OAAO,IAAI,GAAG,QAAQ,cAAc,QAAQ,EAAE,MAAM,WAAW,KAAK,EAAE;AAC1F;AAEA,SAAS,gBAAgB,OAAiD;AACxE,SAAO,EAAE,OAAO,EAAE,MAAM,MAAM,SAAS,UAAU,MAAM,SAAS,EAAE;AACpE;AAGA,SAAS,UAAU,UAAyB,QAAwB;AAClE,QAAM,UACJ,SAAS,SAAS,KAAK,CAAC,MAAM,EAAE,SAAS,WAAW,SAAS,MAAM,KAAK,SAAS,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO;AACjH,QAAM,OAAO,SAAS,OAAO,YAAY,SAAS,OAAO,gBAAgB,SAAS,QAAQ;AAC1F,SAAO,OAAO,GAAG,MAAM,KAAK,IAAI,KAAK;AACvC;AAEA,SAAS,gBAAgB,OAAiD;AACxE,SAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,OAAO,iBAAiB;AAChF;","names":[]}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/** Traffic side being evaluated; rules are scoped per side. Defaults to "input" server-side. */
|
|
2
|
+
type Direction = "input" | "output";
|
|
3
|
+
/** Traffic protocol the policy rules are scoped to. Defaults to "all" server-side. */
|
|
4
|
+
type Protocol = "all" | "llm" | "mcp" | "a2a";
|
|
5
|
+
/** A document evaluated alongside the payload by file-aware plugins. */
|
|
6
|
+
interface Attachment {
|
|
7
|
+
filename: string;
|
|
8
|
+
contentType: string;
|
|
9
|
+
/** Raw file content; strings are encoded as UTF-8, then base64 on the wire. Provide `data` or `url`. */
|
|
10
|
+
data?: Uint8Array | string;
|
|
11
|
+
/** URL for the server to fetch instead of inlining `data`. */
|
|
12
|
+
url?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Payload for a guard evaluation (`POST /v1/evaluate`). */
|
|
15
|
+
interface GuardRequest {
|
|
16
|
+
/** The content to evaluate, e.g. `{ input: "..." }` or provider-shaped fields. Required. */
|
|
17
|
+
payload: Record<string, unknown>;
|
|
18
|
+
/** Traffic side; the server defaults to "input" when omitted. */
|
|
19
|
+
direction?: Direction;
|
|
20
|
+
/** Traffic protocol; the server defaults to "all" when omitted. */
|
|
21
|
+
protocol?: Protocol;
|
|
22
|
+
/** Groups multi-turn traffic; the server synthesises one when omitted. */
|
|
23
|
+
sessionId?: string;
|
|
24
|
+
/** Identifies the end user on whose behalf the request runs. */
|
|
25
|
+
consumerId?: string;
|
|
26
|
+
/** Optional routing hints. Server-side key: `content_type` (overrides the payload content type). */
|
|
27
|
+
attributes?: Record<string, unknown>;
|
|
28
|
+
/** Documents to evaluate; folded into `payload.attachments` on the wire. */
|
|
29
|
+
attachments?: Attachment[];
|
|
30
|
+
}
|
|
31
|
+
/** Who produced the finding — a detector or a gate. */
|
|
32
|
+
interface FindingSource {
|
|
33
|
+
kind: string;
|
|
34
|
+
plugin?: string;
|
|
35
|
+
detectorId?: string;
|
|
36
|
+
detectorName?: string;
|
|
37
|
+
policyId?: string;
|
|
38
|
+
gateName?: string;
|
|
39
|
+
}
|
|
40
|
+
/** Detection signal when the finding fired (omitted for observational-only runs). */
|
|
41
|
+
interface FindingSignal {
|
|
42
|
+
type: string;
|
|
43
|
+
confidence?: number;
|
|
44
|
+
}
|
|
45
|
+
/** Applied execution-rule action when the finding enforced an outcome. */
|
|
46
|
+
interface FindingOutcome {
|
|
47
|
+
action: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A single finding from the evaluate response.
|
|
51
|
+
* Observational (below-threshold) runs keep `source` + `evidence` only.
|
|
52
|
+
*/
|
|
53
|
+
interface Finding {
|
|
54
|
+
source: FindingSource;
|
|
55
|
+
signal?: FindingSignal;
|
|
56
|
+
outcome?: FindingOutcome;
|
|
57
|
+
evidence?: Record<string, unknown>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Verdict returned by POST /v1/evaluate. TrustGuard detects; the caller enforces:
|
|
61
|
+
* block when `status` is "block" (`isBlocked` is the convenience signal).
|
|
62
|
+
*/
|
|
63
|
+
interface GuardResponse {
|
|
64
|
+
/** Most restrictive verdict: "allow", "block", "transform", "report", or "" when omitted. */
|
|
65
|
+
status: string;
|
|
66
|
+
/** Convenience flag: true when `status === "block"`. */
|
|
67
|
+
isBlocked: boolean;
|
|
68
|
+
/** The payload as rewritten by in-flight masking, null when untouched. */
|
|
69
|
+
transformedPayload: Record<string, unknown> | null;
|
|
70
|
+
/** What every plugin / gate in the policy chain reported. */
|
|
71
|
+
findings: Finding[];
|
|
72
|
+
/** Correlation ids for TrustGuard telemetry. */
|
|
73
|
+
traceId: string;
|
|
74
|
+
requestId: string;
|
|
75
|
+
}
|
|
76
|
+
/** Configuration for the TrustGuard client. */
|
|
77
|
+
interface TrustGuardOptions {
|
|
78
|
+
/** Base URL of the TrustGuard deployment, e.g. https://trustguard.neuraltrust.ai */
|
|
79
|
+
baseUrl: string;
|
|
80
|
+
/** Collector API key, sent as a Bearer token. */
|
|
81
|
+
apiKey: string;
|
|
82
|
+
/** Per-request timeout in milliseconds. Default 10000. */
|
|
83
|
+
timeoutMs?: number;
|
|
84
|
+
/** Custom fetch implementation (proxies, test doubles). Default globalThis.fetch. */
|
|
85
|
+
fetch?: typeof fetch;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Client for the TrustGuard runtime evaluate API. */
|
|
89
|
+
declare class TrustGuard {
|
|
90
|
+
#private;
|
|
91
|
+
constructor(options: TrustGuardOptions);
|
|
92
|
+
/**
|
|
93
|
+
* Evaluates the request against the policy attached to the API key and
|
|
94
|
+
* returns the verdict. Throws TrustGuardAPIError on non-2xx responses.
|
|
95
|
+
*/
|
|
96
|
+
guard(request: GuardRequest): Promise<GuardResponse>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { type Attachment as A, type Direction as D, type Finding as F, type GuardResponse as G, type Protocol as P, TrustGuard as T, type FindingOutcome as a, type FindingSignal as b, type FindingSource as c, type GuardRequest as d, type TrustGuardOptions as e };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/** Traffic side being evaluated; rules are scoped per side. Defaults to "input" server-side. */
|
|
2
|
+
type Direction = "input" | "output";
|
|
3
|
+
/** Traffic protocol the policy rules are scoped to. Defaults to "all" server-side. */
|
|
4
|
+
type Protocol = "all" | "llm" | "mcp" | "a2a";
|
|
5
|
+
/** A document evaluated alongside the payload by file-aware plugins. */
|
|
6
|
+
interface Attachment {
|
|
7
|
+
filename: string;
|
|
8
|
+
contentType: string;
|
|
9
|
+
/** Raw file content; strings are encoded as UTF-8, then base64 on the wire. Provide `data` or `url`. */
|
|
10
|
+
data?: Uint8Array | string;
|
|
11
|
+
/** URL for the server to fetch instead of inlining `data`. */
|
|
12
|
+
url?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Payload for a guard evaluation (`POST /v1/evaluate`). */
|
|
15
|
+
interface GuardRequest {
|
|
16
|
+
/** The content to evaluate, e.g. `{ input: "..." }` or provider-shaped fields. Required. */
|
|
17
|
+
payload: Record<string, unknown>;
|
|
18
|
+
/** Traffic side; the server defaults to "input" when omitted. */
|
|
19
|
+
direction?: Direction;
|
|
20
|
+
/** Traffic protocol; the server defaults to "all" when omitted. */
|
|
21
|
+
protocol?: Protocol;
|
|
22
|
+
/** Groups multi-turn traffic; the server synthesises one when omitted. */
|
|
23
|
+
sessionId?: string;
|
|
24
|
+
/** Identifies the end user on whose behalf the request runs. */
|
|
25
|
+
consumerId?: string;
|
|
26
|
+
/** Optional routing hints. Server-side key: `content_type` (overrides the payload content type). */
|
|
27
|
+
attributes?: Record<string, unknown>;
|
|
28
|
+
/** Documents to evaluate; folded into `payload.attachments` on the wire. */
|
|
29
|
+
attachments?: Attachment[];
|
|
30
|
+
}
|
|
31
|
+
/** Who produced the finding — a detector or a gate. */
|
|
32
|
+
interface FindingSource {
|
|
33
|
+
kind: string;
|
|
34
|
+
plugin?: string;
|
|
35
|
+
detectorId?: string;
|
|
36
|
+
detectorName?: string;
|
|
37
|
+
policyId?: string;
|
|
38
|
+
gateName?: string;
|
|
39
|
+
}
|
|
40
|
+
/** Detection signal when the finding fired (omitted for observational-only runs). */
|
|
41
|
+
interface FindingSignal {
|
|
42
|
+
type: string;
|
|
43
|
+
confidence?: number;
|
|
44
|
+
}
|
|
45
|
+
/** Applied execution-rule action when the finding enforced an outcome. */
|
|
46
|
+
interface FindingOutcome {
|
|
47
|
+
action: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A single finding from the evaluate response.
|
|
51
|
+
* Observational (below-threshold) runs keep `source` + `evidence` only.
|
|
52
|
+
*/
|
|
53
|
+
interface Finding {
|
|
54
|
+
source: FindingSource;
|
|
55
|
+
signal?: FindingSignal;
|
|
56
|
+
outcome?: FindingOutcome;
|
|
57
|
+
evidence?: Record<string, unknown>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Verdict returned by POST /v1/evaluate. TrustGuard detects; the caller enforces:
|
|
61
|
+
* block when `status` is "block" (`isBlocked` is the convenience signal).
|
|
62
|
+
*/
|
|
63
|
+
interface GuardResponse {
|
|
64
|
+
/** Most restrictive verdict: "allow", "block", "transform", "report", or "" when omitted. */
|
|
65
|
+
status: string;
|
|
66
|
+
/** Convenience flag: true when `status === "block"`. */
|
|
67
|
+
isBlocked: boolean;
|
|
68
|
+
/** The payload as rewritten by in-flight masking, null when untouched. */
|
|
69
|
+
transformedPayload: Record<string, unknown> | null;
|
|
70
|
+
/** What every plugin / gate in the policy chain reported. */
|
|
71
|
+
findings: Finding[];
|
|
72
|
+
/** Correlation ids for TrustGuard telemetry. */
|
|
73
|
+
traceId: string;
|
|
74
|
+
requestId: string;
|
|
75
|
+
}
|
|
76
|
+
/** Configuration for the TrustGuard client. */
|
|
77
|
+
interface TrustGuardOptions {
|
|
78
|
+
/** Base URL of the TrustGuard deployment, e.g. https://trustguard.neuraltrust.ai */
|
|
79
|
+
baseUrl: string;
|
|
80
|
+
/** Collector API key, sent as a Bearer token. */
|
|
81
|
+
apiKey: string;
|
|
82
|
+
/** Per-request timeout in milliseconds. Default 10000. */
|
|
83
|
+
timeoutMs?: number;
|
|
84
|
+
/** Custom fetch implementation (proxies, test doubles). Default globalThis.fetch. */
|
|
85
|
+
fetch?: typeof fetch;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Client for the TrustGuard runtime evaluate API. */
|
|
89
|
+
declare class TrustGuard {
|
|
90
|
+
#private;
|
|
91
|
+
constructor(options: TrustGuardOptions);
|
|
92
|
+
/**
|
|
93
|
+
* Evaluates the request against the policy attached to the API key and
|
|
94
|
+
* returns the verdict. Throws TrustGuardAPIError on non-2xx responses.
|
|
95
|
+
*/
|
|
96
|
+
guard(request: GuardRequest): Promise<GuardResponse>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { type Attachment as A, type Direction as D, type Finding as F, type GuardResponse as G, type Protocol as P, TrustGuard as T, type FindingOutcome as a, type FindingSignal as b, type FindingSource as c, type GuardRequest as d, type TrustGuardOptions as e };
|
package/dist/index.cjs
CHANGED
|
@@ -18,12 +18,12 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
20
|
// src/index.ts
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
23
|
TrustGuard: () => TrustGuard,
|
|
24
24
|
TrustGuardAPIError: () => TrustGuardAPIError
|
|
25
25
|
});
|
|
26
|
-
module.exports = __toCommonJS(
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
27
|
|
|
28
28
|
// src/errors.ts
|
|
29
29
|
var TrustGuardAPIError = class extends Error {
|
|
@@ -103,12 +103,19 @@ function serializeAttachment(attachment) {
|
|
|
103
103
|
content_type: attachment.contentType
|
|
104
104
|
};
|
|
105
105
|
if (attachment.data !== void 0) {
|
|
106
|
-
const bytes = typeof attachment.data === "string" ?
|
|
107
|
-
entry.data = bytes
|
|
106
|
+
const bytes = typeof attachment.data === "string" ? new TextEncoder().encode(attachment.data) : attachment.data;
|
|
107
|
+
entry.data = toBase64(bytes);
|
|
108
108
|
}
|
|
109
109
|
if (attachment.url !== void 0) entry.url = attachment.url;
|
|
110
110
|
return entry;
|
|
111
111
|
}
|
|
112
|
+
function toBase64(bytes) {
|
|
113
|
+
let binary = "";
|
|
114
|
+
for (let i = 0; i < bytes.length; i += 32768) {
|
|
115
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + 32768));
|
|
116
|
+
}
|
|
117
|
+
return btoa(binary);
|
|
118
|
+
}
|
|
112
119
|
function deserializeResponse(wire) {
|
|
113
120
|
const status = wire.status ?? "";
|
|
114
121
|
return {
|