@tenova/swt3-ai 0.3.3 → 0.3.4
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/package.json +1 -1
- package/dist/adapters/anthropic.d.ts +0 -32
- package/dist/adapters/anthropic.d.ts.map +0 -1
- package/dist/adapters/anthropic.js +0 -299
- package/dist/adapters/anthropic.js.map +0 -1
- package/dist/adapters/bedrock.d.ts +0 -33
- package/dist/adapters/bedrock.d.ts.map +0 -1
- package/dist/adapters/bedrock.js +0 -267
- package/dist/adapters/bedrock.js.map +0 -1
- package/dist/adapters/openai.d.ts +0 -19
- package/dist/adapters/openai.d.ts.map +0 -1
- package/dist/adapters/openai.js +0 -288
- package/dist/adapters/openai.js.map +0 -1
- package/dist/adapters/vercel-ai.d.ts +0 -64
- package/dist/adapters/vercel-ai.d.ts.map +0 -1
- package/dist/adapters/vercel-ai.js +0 -68
- package/dist/adapters/vercel-ai.js.map +0 -1
- package/dist/buffer.d.ts +0 -42
- package/dist/buffer.d.ts.map +0 -1
- package/dist/buffer.js +0 -161
- package/dist/buffer.js.map +0 -1
- package/dist/clearing.d.ts +0 -29
- package/dist/clearing.d.ts.map +0 -1
- package/dist/clearing.js +0 -308
- package/dist/clearing.js.map +0 -1
- package/dist/demo.d.ts +0 -11
- package/dist/demo.d.ts.map +0 -1
- package/dist/demo.js +0 -238
- package/dist/demo.js.map +0 -1
- package/dist/fingerprint.d.ts +0 -29
- package/dist/fingerprint.d.ts.map +0 -1
- package/dist/fingerprint.js +0 -57
- package/dist/fingerprint.js.map +0 -1
- package/dist/handoff.d.ts +0 -17
- package/dist/handoff.d.ts.map +0 -1
- package/dist/handoff.js +0 -82
- package/dist/handoff.js.map +0 -1
- package/dist/index.d.ts +0 -22
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -20
- package/dist/index.js.map +0 -1
- package/dist/signing.d.ts +0 -20
- package/dist/signing.d.ts.map +0 -1
- package/dist/signing.js +0 -28
- package/dist/signing.js.map +0 -1
- package/dist/types.d.ts +0 -98
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -17
- package/dist/types.js.map +0 -1
- package/dist/witness.d.ts +0 -142
- package/dist/witness.d.ts.map +0 -1
- package/dist/witness.js +0 -345
- package/dist/witness.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SWT3 AI Witness SDK — Anthropic Adapter (ES6 Proxy).
|
|
3
|
-
*
|
|
4
|
-
* Wraps the Anthropic client so that `client.messages.create()` is
|
|
5
|
-
* intercepted for witnessing. Two levels deep (simpler than OpenAI).
|
|
6
|
-
*
|
|
7
|
-
* Handles both:
|
|
8
|
-
* - Non-streaming: Message response object
|
|
9
|
-
* - Streaming: MessageStream — accumulates content_block_delta events
|
|
10
|
-
* for hashing, witnesses after stream_message_stop
|
|
11
|
-
*
|
|
12
|
-
* Anthropic response structure:
|
|
13
|
-
* response.content → ContentBlock[] (text, tool_use, etc.)
|
|
14
|
-
* response.model → string
|
|
15
|
-
* response.stop_reason → "end_turn" | "max_tokens" | "stop_sequence" | "tool_use"
|
|
16
|
-
* response.usage → { input_tokens, output_tokens }
|
|
17
|
-
*
|
|
18
|
-
* Anthropic streaming:
|
|
19
|
-
* The SDK returns a MessageStream with:
|
|
20
|
-
* - Symbol.asyncIterator → yields MessageStreamEvent objects
|
|
21
|
-
* - .on("message", cb) → fires when complete message is assembled
|
|
22
|
-
* - .finalMessage() → Promise<Message> (the assembled message)
|
|
23
|
-
*
|
|
24
|
-
* Events: message_start, content_block_start, content_block_delta,
|
|
25
|
-
* content_block_stop, message_delta, message_stop
|
|
26
|
-
*/
|
|
27
|
-
import type { Witness } from "../witness.js";
|
|
28
|
-
/**
|
|
29
|
-
* Wrap an Anthropic client with an ES6 Proxy for transparent witnessing.
|
|
30
|
-
*/
|
|
31
|
-
export declare function wrapAnthropic(client: unknown, witness: Witness): unknown;
|
|
32
|
-
//# sourceMappingURL=anthropic.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/adapters/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAIH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAE7C;;GAEG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAYxE"}
|
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SWT3 AI Witness SDK — Anthropic Adapter (ES6 Proxy).
|
|
3
|
-
*
|
|
4
|
-
* Wraps the Anthropic client so that `client.messages.create()` is
|
|
5
|
-
* intercepted for witnessing. Two levels deep (simpler than OpenAI).
|
|
6
|
-
*
|
|
7
|
-
* Handles both:
|
|
8
|
-
* - Non-streaming: Message response object
|
|
9
|
-
* - Streaming: MessageStream — accumulates content_block_delta events
|
|
10
|
-
* for hashing, witnesses after stream_message_stop
|
|
11
|
-
*
|
|
12
|
-
* Anthropic response structure:
|
|
13
|
-
* response.content → ContentBlock[] (text, tool_use, etc.)
|
|
14
|
-
* response.model → string
|
|
15
|
-
* response.stop_reason → "end_turn" | "max_tokens" | "stop_sequence" | "tool_use"
|
|
16
|
-
* response.usage → { input_tokens, output_tokens }
|
|
17
|
-
*
|
|
18
|
-
* Anthropic streaming:
|
|
19
|
-
* The SDK returns a MessageStream with:
|
|
20
|
-
* - Symbol.asyncIterator → yields MessageStreamEvent objects
|
|
21
|
-
* - .on("message", cb) → fires when complete message is assembled
|
|
22
|
-
* - .finalMessage() → Promise<Message> (the assembled message)
|
|
23
|
-
*
|
|
24
|
-
* Events: message_start, content_block_start, content_block_delta,
|
|
25
|
-
* content_block_stop, message_delta, message_stop
|
|
26
|
-
*/
|
|
27
|
-
import { sha256Truncated } from "../fingerprint.js";
|
|
28
|
-
/**
|
|
29
|
-
* Wrap an Anthropic client with an ES6 Proxy for transparent witnessing.
|
|
30
|
-
*/
|
|
31
|
-
export function wrapAnthropic(client, witness) {
|
|
32
|
-
return new Proxy(client, {
|
|
33
|
-
get(target, prop) {
|
|
34
|
-
if (typeof prop === "symbol")
|
|
35
|
-
return Reflect.get(target, prop);
|
|
36
|
-
const real = Reflect.get(target, prop);
|
|
37
|
-
if (prop === "messages") {
|
|
38
|
-
return createMessagesProxy(real, witness);
|
|
39
|
-
}
|
|
40
|
-
return real;
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
function createMessagesProxy(messages, witness) {
|
|
45
|
-
return new Proxy(messages, {
|
|
46
|
-
get(target, prop) {
|
|
47
|
-
if (typeof prop === "symbol")
|
|
48
|
-
return Reflect.get(target, prop);
|
|
49
|
-
const real = Reflect.get(target, prop);
|
|
50
|
-
if (prop === "create") {
|
|
51
|
-
return createInterceptor(real, witness);
|
|
52
|
-
}
|
|
53
|
-
// Anthropic also has messages.stream() — intercept that too
|
|
54
|
-
if (prop === "stream") {
|
|
55
|
-
return createStreamInterceptor(real, witness);
|
|
56
|
-
}
|
|
57
|
-
return real;
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
// ── Non-streaming / auto-detect interceptor ─────────────────────────
|
|
62
|
-
function createInterceptor(realMethod, witness) {
|
|
63
|
-
return function interceptedCreate(...args) {
|
|
64
|
-
const kwargs = (args[0] ?? {});
|
|
65
|
-
const messages = kwargs.messages;
|
|
66
|
-
const system = kwargs.system;
|
|
67
|
-
const model = kwargs.model ?? "unknown";
|
|
68
|
-
const isStreaming = kwargs.stream === true;
|
|
69
|
-
const promptText = extractPromptText(messages, system);
|
|
70
|
-
const promptHash = sha256Truncated(promptText);
|
|
71
|
-
// Hash system prompt separately (instruction drift detection)
|
|
72
|
-
const systemPromptText = extractSystemOnly(system);
|
|
73
|
-
const systemPromptHash = systemPromptText ? sha256Truncated(systemPromptText) : undefined;
|
|
74
|
-
// Gatekeeper pre-call check (strict mode only)
|
|
75
|
-
if (witness.strict) {
|
|
76
|
-
witness.gateCheck(messages, model);
|
|
77
|
-
}
|
|
78
|
-
const start = performance.now();
|
|
79
|
-
const result = realMethod.call(this, ...args);
|
|
80
|
-
if (isStreaming) {
|
|
81
|
-
// Streaming via create({ stream: true }) — returns a Stream
|
|
82
|
-
return handleStreaming(result, witness, model, promptHash, start, systemPromptHash);
|
|
83
|
-
}
|
|
84
|
-
// Non-streaming — result is Promise<Message>
|
|
85
|
-
return result.then((response) => {
|
|
86
|
-
const elapsedMs = Math.round(performance.now() - start);
|
|
87
|
-
const record = extractRecord(response, model, promptHash, elapsedMs, systemPromptHash);
|
|
88
|
-
witness.record(record);
|
|
89
|
-
return response;
|
|
90
|
-
});
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
// ── Explicit .stream() interceptor ──────────────────────────────────
|
|
94
|
-
function createStreamInterceptor(realMethod, witness) {
|
|
95
|
-
return function interceptedStream(...args) {
|
|
96
|
-
const kwargs = (args[0] ?? {});
|
|
97
|
-
const messages = kwargs.messages;
|
|
98
|
-
const system = kwargs.system;
|
|
99
|
-
const model = kwargs.model ?? "unknown";
|
|
100
|
-
const promptText = extractPromptText(messages, system);
|
|
101
|
-
const promptHash = sha256Truncated(promptText);
|
|
102
|
-
const systemPromptText = extractSystemOnly(system);
|
|
103
|
-
const systemPromptHash = systemPromptText ? sha256Truncated(systemPromptText) : undefined;
|
|
104
|
-
// Gatekeeper pre-call check (strict mode only)
|
|
105
|
-
if (witness.strict) {
|
|
106
|
-
witness.gateCheck(messages, model);
|
|
107
|
-
}
|
|
108
|
-
const start = performance.now();
|
|
109
|
-
const result = realMethod.call(this, ...args);
|
|
110
|
-
return handleStreaming(result, witness, model, promptHash, start, systemPromptHash);
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
// ── Streaming Handler ───────────────────────────────────────────────
|
|
114
|
-
async function* streamAccumulator(stream, witness, model, promptHash, startTime, systemPromptHash) {
|
|
115
|
-
const textParts = [];
|
|
116
|
-
let actualModel = model;
|
|
117
|
-
let stopReason = "";
|
|
118
|
-
let inputTokens;
|
|
119
|
-
let outputTokens;
|
|
120
|
-
for await (const event of stream) {
|
|
121
|
-
// Yield to developer immediately
|
|
122
|
-
yield event;
|
|
123
|
-
const e = event;
|
|
124
|
-
const type = e.type;
|
|
125
|
-
if (type === "message_start") {
|
|
126
|
-
const msg = e.message;
|
|
127
|
-
if (msg?.model)
|
|
128
|
-
actualModel = msg.model;
|
|
129
|
-
const usage = msg?.usage;
|
|
130
|
-
if (usage?.input_tokens)
|
|
131
|
-
inputTokens = usage.input_tokens;
|
|
132
|
-
}
|
|
133
|
-
if (type === "content_block_delta") {
|
|
134
|
-
const delta = e.delta;
|
|
135
|
-
if (delta?.type === "text_delta" && delta?.text) {
|
|
136
|
-
textParts.push(delta.text);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
if (type === "message_delta") {
|
|
140
|
-
const delta = e.delta;
|
|
141
|
-
if (delta?.stop_reason)
|
|
142
|
-
stopReason = delta.stop_reason;
|
|
143
|
-
const usage = e.usage;
|
|
144
|
-
if (usage?.output_tokens)
|
|
145
|
-
outputTokens = usage.output_tokens;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
// Stream complete — witness
|
|
149
|
-
const elapsedMs = Math.round(performance.now() - startTime);
|
|
150
|
-
const responseText = textParts.join("");
|
|
151
|
-
const hasRefusal = !["end_turn", "max_tokens", "stop_sequence", "tool_use"].includes(stopReason);
|
|
152
|
-
const record = {
|
|
153
|
-
modelId: actualModel,
|
|
154
|
-
modelHash: sha256Truncated(actualModel),
|
|
155
|
-
promptHash,
|
|
156
|
-
responseHash: sha256Truncated(responseText),
|
|
157
|
-
latencyMs: elapsedMs,
|
|
158
|
-
inputTokens,
|
|
159
|
-
outputTokens,
|
|
160
|
-
guardrailsActive: 0,
|
|
161
|
-
guardrailsRequired: 0,
|
|
162
|
-
guardrailPassed: true,
|
|
163
|
-
hasRefusal,
|
|
164
|
-
provider: "anthropic",
|
|
165
|
-
systemPromptHash,
|
|
166
|
-
guardrailNames: [],
|
|
167
|
-
};
|
|
168
|
-
witness.record(record);
|
|
169
|
-
}
|
|
170
|
-
function handleStreaming(streamResult, witness, model, promptHash, startTime, systemPromptHash) {
|
|
171
|
-
// Anthropic's stream() returns a MessageStream directly (not a Promise)
|
|
172
|
-
// But create({ stream: true }) may return a Promise<Stream>
|
|
173
|
-
if (streamResult && typeof streamResult.then === "function") {
|
|
174
|
-
return streamResult.then((stream) => wrapAnthropicStream(stream, witness, model, promptHash, startTime, systemPromptHash));
|
|
175
|
-
}
|
|
176
|
-
return wrapAnthropicStream(streamResult, witness, model, promptHash, startTime, systemPromptHash);
|
|
177
|
-
}
|
|
178
|
-
function wrapAnthropicStream(stream, witness, model, promptHash, startTime, systemPromptHash) {
|
|
179
|
-
const s = stream;
|
|
180
|
-
const gen = streamAccumulator(s, witness, model, promptHash, startTime, systemPromptHash);
|
|
181
|
-
return new Proxy(s, {
|
|
182
|
-
get(target, prop) {
|
|
183
|
-
if (prop === Symbol.asyncIterator) {
|
|
184
|
-
return () => gen;
|
|
185
|
-
}
|
|
186
|
-
const value = Reflect.get(target, prop);
|
|
187
|
-
if (typeof value === "function") {
|
|
188
|
-
// Wrap finalMessage() to also witness the complete response
|
|
189
|
-
if (prop === "finalMessage") {
|
|
190
|
-
return async function wrappedFinalMessage() {
|
|
191
|
-
const msg = await value.call(target);
|
|
192
|
-
// The stream accumulator will have already witnessed via iteration,
|
|
193
|
-
// but if someone calls finalMessage() WITHOUT iterating, we need
|
|
194
|
-
// to witness from the assembled message.
|
|
195
|
-
return msg;
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
return value.bind(target);
|
|
199
|
-
}
|
|
200
|
-
return value;
|
|
201
|
-
},
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
// ── Factor Extraction ──────────────────────────────────────────────
|
|
205
|
-
function extractSystemOnly(system) {
|
|
206
|
-
if (typeof system === "string" && system)
|
|
207
|
-
return system;
|
|
208
|
-
if (Array.isArray(system)) {
|
|
209
|
-
const parts = [];
|
|
210
|
-
for (const block of system) {
|
|
211
|
-
if (typeof block === "object" && block !== null) {
|
|
212
|
-
const b = block;
|
|
213
|
-
if (b.type === "text" && typeof b.text === "string") {
|
|
214
|
-
parts.push(b.text);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
return parts.length > 0 ? parts.join("\n") : undefined;
|
|
219
|
-
}
|
|
220
|
-
return undefined;
|
|
221
|
-
}
|
|
222
|
-
function extractPromptText(messages, system = "") {
|
|
223
|
-
const parts = [];
|
|
224
|
-
// System prompt
|
|
225
|
-
if (typeof system === "string" && system) {
|
|
226
|
-
parts.push(system);
|
|
227
|
-
}
|
|
228
|
-
else if (Array.isArray(system)) {
|
|
229
|
-
for (const block of system) {
|
|
230
|
-
if (typeof block === "object" && block !== null) {
|
|
231
|
-
const b = block;
|
|
232
|
-
if (b.type === "text" && typeof b.text === "string") {
|
|
233
|
-
parts.push(b.text);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
// Messages
|
|
239
|
-
if (Array.isArray(messages)) {
|
|
240
|
-
for (const msg of messages) {
|
|
241
|
-
if (typeof msg === "object" && msg !== null) {
|
|
242
|
-
const m = msg;
|
|
243
|
-
const content = m.content;
|
|
244
|
-
if (typeof content === "string") {
|
|
245
|
-
parts.push(content);
|
|
246
|
-
}
|
|
247
|
-
else if (Array.isArray(content)) {
|
|
248
|
-
for (const block of content) {
|
|
249
|
-
if (typeof block === "object" && block !== null) {
|
|
250
|
-
const b = block;
|
|
251
|
-
if (b.type === "text" && typeof b.text === "string") {
|
|
252
|
-
parts.push(b.text);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
return parts.join("\n");
|
|
261
|
-
}
|
|
262
|
-
function extractRecord(response, model, promptHash, elapsedMs, systemPromptHash) {
|
|
263
|
-
const r = response;
|
|
264
|
-
// Extract text from content blocks
|
|
265
|
-
let responseText = "";
|
|
266
|
-
const contentBlocks = r.content;
|
|
267
|
-
if (Array.isArray(contentBlocks)) {
|
|
268
|
-
const texts = [];
|
|
269
|
-
for (const block of contentBlocks) {
|
|
270
|
-
if (block.type === "text" && typeof block.text === "string") {
|
|
271
|
-
texts.push(block.text);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
responseText = texts.join("\n");
|
|
275
|
-
}
|
|
276
|
-
const stopReason = r.stop_reason ?? "";
|
|
277
|
-
const hasRefusal = !["end_turn", "max_tokens", "stop_sequence", "tool_use"].includes(stopReason);
|
|
278
|
-
const usage = r.usage;
|
|
279
|
-
const inputTokens = usage?.input_tokens;
|
|
280
|
-
const outputTokens = usage?.output_tokens;
|
|
281
|
-
const actualModel = r.model ?? model;
|
|
282
|
-
return {
|
|
283
|
-
modelId: actualModel,
|
|
284
|
-
modelHash: sha256Truncated(actualModel),
|
|
285
|
-
promptHash,
|
|
286
|
-
responseHash: sha256Truncated(responseText),
|
|
287
|
-
latencyMs: elapsedMs,
|
|
288
|
-
inputTokens,
|
|
289
|
-
outputTokens,
|
|
290
|
-
guardrailsActive: 0,
|
|
291
|
-
guardrailsRequired: 0,
|
|
292
|
-
guardrailPassed: true,
|
|
293
|
-
hasRefusal,
|
|
294
|
-
provider: "anthropic",
|
|
295
|
-
systemPromptHash,
|
|
296
|
-
guardrailNames: [],
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
//# sourceMappingURL=anthropic.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/adapters/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,m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K — AWS Bedrock Adapter (ES6 Proxy).
|
|
3
|
-
*
|
|
4
|
-
* Wraps the @aws-sdk/client-bedrock-runtime client so that
|
|
5
|
-
* `client.send(new ConverseCommand(...))` and
|
|
6
|
-
* `client.send(new InvokeModelCommand(...))` are intercepted.
|
|
7
|
-
*
|
|
8
|
-
* AWS Bedrock uses the Command pattern:
|
|
9
|
-
* const response = await client.send(new ConverseCommand({ modelId, messages }));
|
|
10
|
-
*
|
|
11
|
-
* The adapter intercepts `send()` and checks if the command is a
|
|
12
|
-
* ConverseCommand or InvokeModelCommand to extract factors.
|
|
13
|
-
*
|
|
14
|
-
* Copyright (c) 2026 Tenable Nova LLC. Apache 2.0. Patent pending.
|
|
15
|
-
*/
|
|
16
|
-
import type { Witness } from "../witness.js";
|
|
17
|
-
/**
|
|
18
|
-
* Wrap a BedrockRuntimeClient with transparent witnessing.
|
|
19
|
-
*
|
|
20
|
-
* Usage:
|
|
21
|
-
* import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
|
|
22
|
-
* import { Witness } from "@tenova/swt3-ai";
|
|
23
|
-
*
|
|
24
|
-
* const witness = new Witness({ endpoint, apiKey, tenantId });
|
|
25
|
-
* const client = witness.wrap(new BedrockRuntimeClient({ region: "us-east-1" }));
|
|
26
|
-
*
|
|
27
|
-
* const response = await client.send(new ConverseCommand({
|
|
28
|
-
* modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
|
|
29
|
-
* messages: [{ role: "user", content: [{ text: "Hello" }] }],
|
|
30
|
-
* }));
|
|
31
|
-
*/
|
|
32
|
-
export declare function wrapBedrock(client: unknown, witness: Witness): unknown;
|
|
33
|
-
//# sourceMappingURL=bedrock.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bedrock.d.ts","sourceRoot":"","sources":["../../src/adapters/bedrock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAE7C;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAatE"}
|
package/dist/adapters/bedrock.js
DELETED
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SWT3 AI Witness SDK — AWS Bedrock Adapter (ES6 Proxy).
|
|
3
|
-
*
|
|
4
|
-
* Wraps the @aws-sdk/client-bedrock-runtime client so that
|
|
5
|
-
* `client.send(new ConverseCommand(...))` and
|
|
6
|
-
* `client.send(new InvokeModelCommand(...))` are intercepted.
|
|
7
|
-
*
|
|
8
|
-
* AWS Bedrock uses the Command pattern:
|
|
9
|
-
* const response = await client.send(new ConverseCommand({ modelId, messages }));
|
|
10
|
-
*
|
|
11
|
-
* The adapter intercepts `send()` and checks if the command is a
|
|
12
|
-
* ConverseCommand or InvokeModelCommand to extract factors.
|
|
13
|
-
*
|
|
14
|
-
* Copyright (c) 2026 Tenable Nova LLC. Apache 2.0. Patent pending.
|
|
15
|
-
*/
|
|
16
|
-
import { sha256Truncated } from "../fingerprint.js";
|
|
17
|
-
/**
|
|
18
|
-
* Wrap a BedrockRuntimeClient with transparent witnessing.
|
|
19
|
-
*
|
|
20
|
-
* Usage:
|
|
21
|
-
* import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
|
|
22
|
-
* import { Witness } from "@tenova/swt3-ai";
|
|
23
|
-
*
|
|
24
|
-
* const witness = new Witness({ endpoint, apiKey, tenantId });
|
|
25
|
-
* const client = witness.wrap(new BedrockRuntimeClient({ region: "us-east-1" }));
|
|
26
|
-
*
|
|
27
|
-
* const response = await client.send(new ConverseCommand({
|
|
28
|
-
* modelId: "anthropic.claude-3-5-sonnet-20241022-v2:0",
|
|
29
|
-
* messages: [{ role: "user", content: [{ text: "Hello" }] }],
|
|
30
|
-
* }));
|
|
31
|
-
*/
|
|
32
|
-
export function wrapBedrock(client, witness) {
|
|
33
|
-
return new Proxy(client, {
|
|
34
|
-
get(target, prop) {
|
|
35
|
-
if (typeof prop === "symbol")
|
|
36
|
-
return Reflect.get(target, prop);
|
|
37
|
-
const real = Reflect.get(target, prop);
|
|
38
|
-
if (prop === "send" && typeof real === "function") {
|
|
39
|
-
return createSendInterceptor(real, witness, target);
|
|
40
|
-
}
|
|
41
|
-
return real;
|
|
42
|
-
},
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
function createSendInterceptor(realSend, witness, thisArg) {
|
|
46
|
-
return async function interceptedSend(...args) {
|
|
47
|
-
const command = args[0];
|
|
48
|
-
if (!command)
|
|
49
|
-
return realSend.call(thisArg, ...args);
|
|
50
|
-
// Detect command type by constructor name
|
|
51
|
-
const commandName = command.constructor?.name ?? "";
|
|
52
|
-
if (commandName === "ConverseCommand") {
|
|
53
|
-
return handleConverse(realSend, thisArg, command, witness, args);
|
|
54
|
-
}
|
|
55
|
-
if (commandName === "InvokeModelCommand") {
|
|
56
|
-
return handleInvokeModel(realSend, thisArg, command, witness, args);
|
|
57
|
-
}
|
|
58
|
-
// Not a command we intercept — pass through
|
|
59
|
-
return realSend.call(thisArg, ...args);
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
// ── Converse Command Handler ──────────────────────────────────────
|
|
63
|
-
async function handleConverse(realSend, thisArg, command, witness, args) {
|
|
64
|
-
const input = command.input ?? command;
|
|
65
|
-
const modelId = input.modelId ?? "unknown";
|
|
66
|
-
const messages = input.messages ?? [];
|
|
67
|
-
const promptText = extractConversePrompt(messages);
|
|
68
|
-
const promptHash = sha256Truncated(promptText);
|
|
69
|
-
// Gatekeeper pre-call check (strict mode only)
|
|
70
|
-
if (witness.strict) {
|
|
71
|
-
witness.gateCheck(messages, modelId);
|
|
72
|
-
}
|
|
73
|
-
const start = performance.now();
|
|
74
|
-
const response = await realSend.call(thisArg, ...args);
|
|
75
|
-
const elapsedMs = Math.round(performance.now() - start);
|
|
76
|
-
const resp = response;
|
|
77
|
-
const record = extractConverseRecord(resp, modelId, promptHash, elapsedMs);
|
|
78
|
-
witness.record(record);
|
|
79
|
-
return response;
|
|
80
|
-
}
|
|
81
|
-
// ── InvokeModel Command Handler ──────────────────────────────────
|
|
82
|
-
async function handleInvokeModel(realSend, thisArg, command, witness, args) {
|
|
83
|
-
const input = command.input ?? command;
|
|
84
|
-
const modelId = input.modelId ?? "unknown";
|
|
85
|
-
// Parse the body to get prompt
|
|
86
|
-
let body = {};
|
|
87
|
-
const rawBody = input.body;
|
|
88
|
-
if (typeof rawBody === "string") {
|
|
89
|
-
try {
|
|
90
|
-
body = JSON.parse(rawBody);
|
|
91
|
-
}
|
|
92
|
-
catch { /* empty */ }
|
|
93
|
-
}
|
|
94
|
-
else if (rawBody instanceof Uint8Array) {
|
|
95
|
-
try {
|
|
96
|
-
body = JSON.parse(new TextDecoder().decode(rawBody));
|
|
97
|
-
}
|
|
98
|
-
catch { /* empty */ }
|
|
99
|
-
}
|
|
100
|
-
const promptText = extractInvokePrompt(body, modelId);
|
|
101
|
-
const promptHash = sha256Truncated(promptText);
|
|
102
|
-
// Gatekeeper pre-call check (strict mode only)
|
|
103
|
-
if (witness.strict) {
|
|
104
|
-
witness.gateCheck(body, modelId);
|
|
105
|
-
}
|
|
106
|
-
const start = performance.now();
|
|
107
|
-
const response = await realSend.call(thisArg, ...args);
|
|
108
|
-
const elapsedMs = Math.round(performance.now() - start);
|
|
109
|
-
const resp = response;
|
|
110
|
-
const record = extractInvokeRecord(resp, modelId, promptHash, elapsedMs);
|
|
111
|
-
witness.record(record);
|
|
112
|
-
return response;
|
|
113
|
-
}
|
|
114
|
-
// ── Prompt Extraction ─────────────────────────────────────────────
|
|
115
|
-
function extractConversePrompt(messages) {
|
|
116
|
-
const parts = [];
|
|
117
|
-
for (const msg of messages) {
|
|
118
|
-
const m = msg;
|
|
119
|
-
const content = m.content;
|
|
120
|
-
if (Array.isArray(content)) {
|
|
121
|
-
for (const block of content) {
|
|
122
|
-
const b = block;
|
|
123
|
-
if (b.text)
|
|
124
|
-
parts.push(b.text);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
else if (typeof content === "string") {
|
|
128
|
-
parts.push(content);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return parts.join("\n");
|
|
132
|
-
}
|
|
133
|
-
function extractInvokePrompt(body, modelId) {
|
|
134
|
-
// Anthropic on Bedrock
|
|
135
|
-
const messages = body.messages;
|
|
136
|
-
if (messages?.length) {
|
|
137
|
-
const parts = [];
|
|
138
|
-
for (const msg of messages) {
|
|
139
|
-
const m = msg;
|
|
140
|
-
const content = m.content;
|
|
141
|
-
if (typeof content === "string") {
|
|
142
|
-
parts.push(content);
|
|
143
|
-
}
|
|
144
|
-
else if (Array.isArray(content)) {
|
|
145
|
-
for (const block of content) {
|
|
146
|
-
const b = block;
|
|
147
|
-
if (b.type === "text" && b.text)
|
|
148
|
-
parts.push(b.text);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (parts.length)
|
|
153
|
-
return parts.join("\n");
|
|
154
|
-
}
|
|
155
|
-
// Amazon Titan
|
|
156
|
-
if (body.inputText)
|
|
157
|
-
return body.inputText;
|
|
158
|
-
// Meta Llama
|
|
159
|
-
if (body.prompt)
|
|
160
|
-
return body.prompt;
|
|
161
|
-
return JSON.stringify(body);
|
|
162
|
-
}
|
|
163
|
-
// ── Record Extraction ─────────────────────────────────────────────
|
|
164
|
-
function extractConverseRecord(response, modelId, promptHash, elapsedMs) {
|
|
165
|
-
let responseText = "";
|
|
166
|
-
let hasRefusal = false;
|
|
167
|
-
const output = response.output;
|
|
168
|
-
const message = output?.message;
|
|
169
|
-
const content = message?.content;
|
|
170
|
-
if (content) {
|
|
171
|
-
for (const block of content) {
|
|
172
|
-
if (block.text)
|
|
173
|
-
responseText += block.text;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
const stopReason = response.stopReason;
|
|
177
|
-
if (stopReason === "content_filtered")
|
|
178
|
-
hasRefusal = true;
|
|
179
|
-
const usage = response.usage;
|
|
180
|
-
const inputTokens = usage?.inputTokens;
|
|
181
|
-
const outputTokens = usage?.outputTokens;
|
|
182
|
-
return {
|
|
183
|
-
modelId: modelId,
|
|
184
|
-
modelHash: sha256Truncated(modelId),
|
|
185
|
-
promptHash: promptHash,
|
|
186
|
-
responseHash: sha256Truncated(responseText || ""),
|
|
187
|
-
latencyMs: elapsedMs,
|
|
188
|
-
inputTokens: inputTokens ?? undefined,
|
|
189
|
-
outputTokens: outputTokens ?? undefined,
|
|
190
|
-
hasRefusal: hasRefusal,
|
|
191
|
-
provider: "bedrock",
|
|
192
|
-
systemFingerprint: undefined,
|
|
193
|
-
guardrailsActive: 0,
|
|
194
|
-
guardrailsRequired: 0,
|
|
195
|
-
guardrailPassed: true,
|
|
196
|
-
guardrailNames: [],
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
function extractInvokeRecord(response, modelId, promptHash, elapsedMs) {
|
|
200
|
-
let responseText = "";
|
|
201
|
-
let hasRefusal = false;
|
|
202
|
-
let inputTokens = null;
|
|
203
|
-
let outputTokens = null;
|
|
204
|
-
try {
|
|
205
|
-
let body = {};
|
|
206
|
-
const rawBody = response.body;
|
|
207
|
-
if (rawBody instanceof Uint8Array) {
|
|
208
|
-
body = JSON.parse(new TextDecoder().decode(rawBody));
|
|
209
|
-
}
|
|
210
|
-
else if (typeof rawBody === "string") {
|
|
211
|
-
body = JSON.parse(rawBody);
|
|
212
|
-
}
|
|
213
|
-
// Anthropic on Bedrock
|
|
214
|
-
if (body.content) {
|
|
215
|
-
const content = body.content;
|
|
216
|
-
if (Array.isArray(content)) {
|
|
217
|
-
for (const block of content) {
|
|
218
|
-
if (block.type === "text" && block.text)
|
|
219
|
-
responseText += block.text;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
else if (typeof content === "string") {
|
|
223
|
-
responseText = content;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
// Amazon Titan
|
|
227
|
-
else if (body.results) {
|
|
228
|
-
const results = body.results;
|
|
229
|
-
if (results[0]?.outputText)
|
|
230
|
-
responseText = results[0].outputText;
|
|
231
|
-
}
|
|
232
|
-
// Meta Llama
|
|
233
|
-
else if (body.generation) {
|
|
234
|
-
responseText = body.generation;
|
|
235
|
-
}
|
|
236
|
-
// Usage
|
|
237
|
-
const usage = body.usage;
|
|
238
|
-
if (usage) {
|
|
239
|
-
inputTokens = (usage.input_tokens ?? usage.inputTokens ?? null);
|
|
240
|
-
outputTokens = (usage.output_tokens ?? usage.outputTokens ?? null);
|
|
241
|
-
}
|
|
242
|
-
// Refusal
|
|
243
|
-
const stopReason = (body.stop_reason ?? body.stopReason ?? "");
|
|
244
|
-
if (stopReason.toLowerCase().includes("content_filter"))
|
|
245
|
-
hasRefusal = true;
|
|
246
|
-
}
|
|
247
|
-
catch {
|
|
248
|
-
// Parse failure — still witness with what we have
|
|
249
|
-
}
|
|
250
|
-
return {
|
|
251
|
-
modelId: modelId,
|
|
252
|
-
modelHash: sha256Truncated(modelId),
|
|
253
|
-
promptHash: promptHash,
|
|
254
|
-
responseHash: sha256Truncated(responseText || ""),
|
|
255
|
-
latencyMs: elapsedMs,
|
|
256
|
-
inputTokens: inputTokens ?? undefined,
|
|
257
|
-
outputTokens: outputTokens ?? undefined,
|
|
258
|
-
hasRefusal: hasRefusal,
|
|
259
|
-
provider: "bedrock",
|
|
260
|
-
systemFingerprint: undefined,
|
|
261
|
-
guardrailsActive: 0,
|
|
262
|
-
guardrailsRequired: 0,
|
|
263
|
-
guardrailPassed: true,
|
|
264
|
-
guardrailNames: [],
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
//# sourceMappingURL=bedrock.js.map
|