@tenova/swt3-ai 0.3.4 → 0.4.1
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 +150 -18
- package/dist/adapters/anthropic.d.ts +32 -0
- package/dist/adapters/anthropic.d.ts.map +1 -0
- package/dist/adapters/anthropic.js +301 -0
- package/dist/adapters/anthropic.js.map +1 -0
- package/dist/adapters/bedrock.d.ts +33 -0
- package/dist/adapters/bedrock.d.ts.map +1 -0
- package/dist/adapters/bedrock.js +269 -0
- package/dist/adapters/bedrock.js.map +1 -0
- package/dist/adapters/openai.d.ts +19 -0
- package/dist/adapters/openai.d.ts.map +1 -0
- package/dist/adapters/openai.js +289 -0
- package/dist/adapters/openai.js.map +1 -0
- package/dist/adapters/vercel-ai.d.ts +64 -0
- package/dist/adapters/vercel-ai.d.ts.map +1 -0
- package/dist/adapters/vercel-ai.js +68 -0
- package/dist/adapters/vercel-ai.js.map +1 -0
- package/dist/buffer.d.ts +43 -0
- package/dist/buffer.d.ts.map +1 -0
- package/dist/buffer.js +172 -0
- package/dist/buffer.js.map +1 -0
- package/dist/clearing.d.ts +39 -0
- package/dist/clearing.d.ts.map +1 -0
- package/dist/clearing.js +338 -0
- package/dist/clearing.js.map +1 -0
- package/dist/demo.d.ts +11 -0
- package/dist/demo.d.ts.map +1 -0
- package/dist/demo.js +238 -0
- package/dist/demo.js.map +1 -0
- package/dist/exporters/otel.d.ts +36 -0
- package/dist/exporters/otel.d.ts.map +1 -0
- package/dist/exporters/otel.js +94 -0
- package/dist/exporters/otel.js.map +1 -0
- package/dist/fingerprint.d.ts +29 -0
- package/dist/fingerprint.d.ts.map +1 -0
- package/dist/fingerprint.js +57 -0
- package/dist/fingerprint.js.map +1 -0
- package/dist/handoff.d.ts +17 -0
- package/dist/handoff.d.ts.map +1 -0
- package/dist/handoff.js +82 -0
- package/dist/handoff.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/signing.d.ts +20 -0
- package/dist/signing.d.ts.map +1 -0
- package/dist/signing.js +28 -0
- package/dist/signing.js.map +1 -0
- package/dist/types.d.ts +108 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +20 -0
- package/dist/types.js.map +1 -0
- package/dist/witness.d.ts +193 -0
- package/dist/witness.d.ts.map +1 -0
- package/dist/witness.js +487 -0
- package/dist/witness.js.map +1 -0
- package/package.json +10 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vercel-ai.d.ts","sourceRoot":"","sources":["../../src/adapters/vercel-ai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAIH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAE7C;;;GAGG;AACH,UAAU,oBAAoB;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE;QACL,YAAY,EAAE,MAAM,CAAC;QACrB,gBAAgB,EAAE,MAAM,CAAC;KAC1B,CAAC;IACF,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE;QACR,EAAE,CAAC,EAAE,MAAM,CAAC;QACZ,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,SAAS,CAAC,EAAE,IAAI,CAAC;QACjB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KAClC,CAAC;IACF,6BAA6B,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzD;AAED,MAAM,WAAW,qBAAqB;IACpC,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,0DAA0D;IAC1D,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,qBAA0B,GAClC,CAAC,MAAM,EAAE,oBAAoB,KAAK,IAAI,CAoCxC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SWT3 AI Witness SDK — Vercel AI SDK Integration.
|
|
3
|
+
*
|
|
4
|
+
* Provides an `onFinish` callback factory for the Vercel AI SDK's
|
|
5
|
+
* `streamText()` and `generateText()` functions. This is the most
|
|
6
|
+
* idiomatic integration for Next.js / React developers.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* import { streamText } from "ai";
|
|
10
|
+
* import { openai } from "@ai-sdk/openai";
|
|
11
|
+
*
|
|
12
|
+
* const result = await streamText({
|
|
13
|
+
* model: openai("gpt-4o"),
|
|
14
|
+
* prompt: "Summarize this contract...",
|
|
15
|
+
* onFinish: witness.vercelOnFinish(),
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* The `onFinish` callback receives a normalized result regardless of
|
|
19
|
+
* provider (OpenAI, Anthropic, Google, custom), so this single hook
|
|
20
|
+
* works with any Vercel AI SDK provider — no per-provider adapters needed.
|
|
21
|
+
*
|
|
22
|
+
* Vercel AI SDK onFinish payload:
|
|
23
|
+
* {
|
|
24
|
+
* text: string, // Complete response text
|
|
25
|
+
* usage: { promptTokens, completionTokens },
|
|
26
|
+
* finishReason: "stop" | "length" | "content-filter" | "tool-calls" | ...,
|
|
27
|
+
* response: { id, model, timestamp, headers },
|
|
28
|
+
* experimental_providerMetadata?: { ... },
|
|
29
|
+
* }
|
|
30
|
+
*/
|
|
31
|
+
import { sha256Truncated } from "../fingerprint.js";
|
|
32
|
+
/**
|
|
33
|
+
* Create a Vercel AI SDK `onFinish` callback that witnesses the inference.
|
|
34
|
+
*
|
|
35
|
+
* Works with both `streamText()` and `generateText()`.
|
|
36
|
+
*/
|
|
37
|
+
export function createVercelOnFinish(witness, options = {}) {
|
|
38
|
+
const capturedStart = performance.now();
|
|
39
|
+
return (result) => {
|
|
40
|
+
const elapsedMs = Math.round(performance.now() - capturedStart);
|
|
41
|
+
const model = result.response?.model ?? options.modelId ?? "unknown";
|
|
42
|
+
const responseText = result.text ?? "";
|
|
43
|
+
// Prompt text: caller can provide it, or we hash empty
|
|
44
|
+
// (The Vercel AI SDK doesn't expose the prompt in onFinish,
|
|
45
|
+
// so the caller should pass it via options for full provenance)
|
|
46
|
+
const promptText = options.promptText ?? "";
|
|
47
|
+
// Detect content filtering / refusal
|
|
48
|
+
const hasRefusal = result.finishReason === "content-filter" ||
|
|
49
|
+
result.finishReason === "error";
|
|
50
|
+
const record = {
|
|
51
|
+
modelId: model,
|
|
52
|
+
modelHash: sha256Truncated(model),
|
|
53
|
+
promptHash: sha256Truncated(promptText),
|
|
54
|
+
responseHash: sha256Truncated(responseText),
|
|
55
|
+
latencyMs: elapsedMs,
|
|
56
|
+
inputTokens: result.usage?.promptTokens,
|
|
57
|
+
outputTokens: result.usage?.completionTokens,
|
|
58
|
+
guardrailsActive: 0,
|
|
59
|
+
guardrailsRequired: 0,
|
|
60
|
+
guardrailPassed: true,
|
|
61
|
+
hasRefusal,
|
|
62
|
+
provider: "vercel-ai",
|
|
63
|
+
guardrailNames: [],
|
|
64
|
+
};
|
|
65
|
+
witness.record(record);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=vercel-ai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vercel-ai.js","sourceRoot":"","sources":["../../src/adapters/vercel-ai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AA+BpD;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAAgB,EAChB,UAAiC,EAAE;IAEnC,MAAM,aAAa,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;IAExC,OAAO,CAAC,MAA4B,EAAE,EAAE;QACtC,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,aAAa,CAAC,CAAC;QAChE,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,EAAE,KAAK,IAAI,OAAO,CAAC,OAAO,IAAI,SAAS,CAAC;QACrE,MAAM,YAAY,GAAG,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;QAEvC,uDAAuD;QACvD,4DAA4D;QAC5D,iEAAiE;QACjE,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,EAAE,CAAC;QAE5C,qCAAqC;QACrC,MAAM,UAAU,GACd,MAAM,CAAC,YAAY,KAAK,gBAAgB;YACxC,MAAM,CAAC,YAAY,KAAK,OAAO,CAAC;QAElC,MAAM,MAAM,GAAoB;YAC9B,OAAO,EAAE,KAAK;YACd,SAAS,EAAE,eAAe,CAAC,KAAK,CAAC;YACjC,UAAU,EAAE,eAAe,CAAC,UAAU,CAAC;YACvC,YAAY,EAAE,eAAe,CAAC,YAAY,CAAC;YAC3C,SAAS,EAAE,SAAS;YACpB,WAAW,EAAE,MAAM,CAAC,KAAK,EAAE,YAAY;YACvC,YAAY,EAAE,MAAM,CAAC,KAAK,EAAE,gBAAgB;YAC5C,gBAAgB,EAAE,CAAC;YACnB,kBAAkB,EAAE,CAAC;YACrB,eAAe,EAAE,IAAI;YACrB,UAAU;YACV,QAAQ,EAAE,WAAW;YACrB,cAAc,EAAE,EAAE;SACnB,CAAC;QAEF,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/buffer.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SWT3 AI Witness SDK — Flush Buffer.
|
|
3
|
+
*
|
|
4
|
+
* Non-blocking buffer that collects witness payloads and flushes them
|
|
5
|
+
* to the /api/v1/witness/batch endpoint. Uses setTimeout for periodic
|
|
6
|
+
* flushing and a dead-letter array for resilience.
|
|
7
|
+
*
|
|
8
|
+
* Flight Recorder: When the endpoint is unreachable, payloads move to
|
|
9
|
+
* a dead-letter array and retry on the next cycle. Configurable cap
|
|
10
|
+
* prevents unbounded memory growth.
|
|
11
|
+
*/
|
|
12
|
+
import type { WitnessConfig, WitnessPayload, WitnessReceipt } from "./types.js";
|
|
13
|
+
export declare class WitnessBuffer {
|
|
14
|
+
private config;
|
|
15
|
+
private queue;
|
|
16
|
+
private deadLetter;
|
|
17
|
+
private maxRetryBuffer;
|
|
18
|
+
private allReceipts;
|
|
19
|
+
private timer;
|
|
20
|
+
private stopped;
|
|
21
|
+
private consecutiveFailures;
|
|
22
|
+
private ctaShown;
|
|
23
|
+
private onFlush?;
|
|
24
|
+
constructor(config: WitnessConfig, maxRetryBuffer?: number);
|
|
25
|
+
/** Add a single payload to the buffer. */
|
|
26
|
+
enqueue(payload: WitnessPayload): void;
|
|
27
|
+
/** Add multiple payloads. */
|
|
28
|
+
enqueueMany(payloads: WitnessPayload[]): void;
|
|
29
|
+
/** Force-flush all buffered payloads. */
|
|
30
|
+
flush(): Promise<WitnessReceipt[]>;
|
|
31
|
+
/** Stop the buffer and flush remaining payloads. */
|
|
32
|
+
stop(): Promise<WitnessReceipt[]>;
|
|
33
|
+
/** Number of payloads waiting (includes dead-letter). */
|
|
34
|
+
get pending(): number;
|
|
35
|
+
/** Payloads in dead-letter queue awaiting retry. */
|
|
36
|
+
get deadLetterCount(): number;
|
|
37
|
+
/** All receipts from completed flushes. */
|
|
38
|
+
get receipts(): WitnessReceipt[];
|
|
39
|
+
private startTimer;
|
|
40
|
+
private flushInternal;
|
|
41
|
+
private sendBatch;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=buffer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffer.d.ts","sourceRoot":"","sources":["../src/buffer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAiB,MAAM,YAAY,CAAC;AAI/F,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,KAAK,CAAwB;IACrC,OAAO,CAAC,UAAU,CAAwB;IAC1C,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,WAAW,CAAwB;IAC3C,OAAO,CAAC,KAAK,CAA8C;IAC3D,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,mBAAmB,CAAK;IAChC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAC,CAAmE;gBAEvE,MAAM,EAAE,aAAa,EAAE,cAAc,SAA2B;IAO5E,0CAA0C;IAC1C,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI;IAQtC,6BAA6B;IAC7B,WAAW,CAAC,QAAQ,EAAE,cAAc,EAAE,GAAG,IAAI;IAI7C,yCAAyC;IACnC,KAAK,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAIxC,oDAAoD;IAC9C,IAAI,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAgBvC,yDAAyD;IACzD,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,oDAAoD;IACpD,IAAI,eAAe,IAAI,MAAM,CAE5B;IAED,2CAA2C;IAC3C,IAAI,QAAQ,IAAI,cAAc,EAAE,CAE/B;IAED,OAAO,CAAC,UAAU;YAYJ,aAAa;YAWb,SAAS;CAiGxB"}
|
package/dist/buffer.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SWT3 AI Witness SDK — Flush Buffer.
|
|
3
|
+
*
|
|
4
|
+
* Non-blocking buffer that collects witness payloads and flushes them
|
|
5
|
+
* to the /api/v1/witness/batch endpoint. Uses setTimeout for periodic
|
|
6
|
+
* flushing and a dead-letter array for resilience.
|
|
7
|
+
*
|
|
8
|
+
* Flight Recorder: When the endpoint is unreachable, payloads move to
|
|
9
|
+
* a dead-letter array and retry on the next cycle. Configurable cap
|
|
10
|
+
* prevents unbounded memory growth.
|
|
11
|
+
*/
|
|
12
|
+
const DEFAULT_MAX_RETRY_BUFFER = 5000;
|
|
13
|
+
export class WitnessBuffer {
|
|
14
|
+
config;
|
|
15
|
+
queue = [];
|
|
16
|
+
deadLetter = [];
|
|
17
|
+
maxRetryBuffer;
|
|
18
|
+
allReceipts = [];
|
|
19
|
+
timer = null;
|
|
20
|
+
stopped = false;
|
|
21
|
+
consecutiveFailures = 0;
|
|
22
|
+
ctaShown = false;
|
|
23
|
+
onFlush;
|
|
24
|
+
constructor(config, maxRetryBuffer = DEFAULT_MAX_RETRY_BUFFER) {
|
|
25
|
+
this.config = config;
|
|
26
|
+
this.maxRetryBuffer = maxRetryBuffer;
|
|
27
|
+
this.onFlush = config.onFlush;
|
|
28
|
+
this.startTimer();
|
|
29
|
+
}
|
|
30
|
+
/** Add a single payload to the buffer. */
|
|
31
|
+
enqueue(payload) {
|
|
32
|
+
if (this.stopped)
|
|
33
|
+
return;
|
|
34
|
+
this.queue.push(payload);
|
|
35
|
+
if (this.queue.length >= this.config.bufferSize) {
|
|
36
|
+
this.flushInternal();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Add multiple payloads. */
|
|
40
|
+
enqueueMany(payloads) {
|
|
41
|
+
for (const p of payloads)
|
|
42
|
+
this.enqueue(p);
|
|
43
|
+
}
|
|
44
|
+
/** Force-flush all buffered payloads. */
|
|
45
|
+
async flush() {
|
|
46
|
+
return this.flushInternal();
|
|
47
|
+
}
|
|
48
|
+
/** Stop the buffer and flush remaining payloads. */
|
|
49
|
+
async stop() {
|
|
50
|
+
if (this.stopped)
|
|
51
|
+
return [];
|
|
52
|
+
this.stopped = true;
|
|
53
|
+
if (this.timer) {
|
|
54
|
+
clearTimeout(this.timer);
|
|
55
|
+
this.timer = null;
|
|
56
|
+
}
|
|
57
|
+
const receipts = await this.flushInternal();
|
|
58
|
+
if (this.deadLetter.length > 0) {
|
|
59
|
+
console.warn(`[swt3-ai] Buffer stopped with ${this.deadLetter.length} payloads in dead-letter queue`);
|
|
60
|
+
}
|
|
61
|
+
return receipts;
|
|
62
|
+
}
|
|
63
|
+
/** Number of payloads waiting (includes dead-letter). */
|
|
64
|
+
get pending() {
|
|
65
|
+
return this.queue.length + this.deadLetter.length;
|
|
66
|
+
}
|
|
67
|
+
/** Payloads in dead-letter queue awaiting retry. */
|
|
68
|
+
get deadLetterCount() {
|
|
69
|
+
return this.deadLetter.length;
|
|
70
|
+
}
|
|
71
|
+
/** All receipts from completed flushes. */
|
|
72
|
+
get receipts() {
|
|
73
|
+
return [...this.allReceipts];
|
|
74
|
+
}
|
|
75
|
+
startTimer() {
|
|
76
|
+
if (this.stopped)
|
|
77
|
+
return;
|
|
78
|
+
this.timer = setTimeout(() => {
|
|
79
|
+
this.flushInternal().catch(() => { });
|
|
80
|
+
this.startTimer();
|
|
81
|
+
}, this.config.flushInterval * 1000);
|
|
82
|
+
// Unref so the timer doesn't keep the process alive
|
|
83
|
+
if (typeof this.timer === "object" && "unref" in this.timer) {
|
|
84
|
+
this.timer.unref();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async flushInternal() {
|
|
88
|
+
// Drain queue
|
|
89
|
+
const payloads = [...this.deadLetter, ...this.queue];
|
|
90
|
+
this.deadLetter = [];
|
|
91
|
+
this.queue = [];
|
|
92
|
+
if (payloads.length === 0)
|
|
93
|
+
return [];
|
|
94
|
+
return this.sendBatch(payloads);
|
|
95
|
+
}
|
|
96
|
+
async sendBatch(payloads) {
|
|
97
|
+
if (payloads.length === 0)
|
|
98
|
+
return [];
|
|
99
|
+
const url = `${this.config.endpoint}/api/v1/witness/batch`;
|
|
100
|
+
const body = JSON.stringify({ witnesses: payloads });
|
|
101
|
+
const headers = {
|
|
102
|
+
"Content-Type": "application/json",
|
|
103
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
104
|
+
};
|
|
105
|
+
let lastError = null;
|
|
106
|
+
for (let attempt = 0; attempt < this.config.maxRetries; attempt++) {
|
|
107
|
+
try {
|
|
108
|
+
const controller = new AbortController();
|
|
109
|
+
const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
|
|
110
|
+
const resp = await fetch(url, {
|
|
111
|
+
method: "POST",
|
|
112
|
+
headers,
|
|
113
|
+
body,
|
|
114
|
+
signal: controller.signal,
|
|
115
|
+
});
|
|
116
|
+
clearTimeout(timeoutId);
|
|
117
|
+
if (resp.status >= 400 && resp.status < 500) {
|
|
118
|
+
// Client error — don't retry, don't dead-letter
|
|
119
|
+
const text = await resp.text();
|
|
120
|
+
console.error(`[swt3-ai] Batch flush failed (${resp.status}): ${text.slice(0, 200)}`);
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
const result = (await resp.json());
|
|
124
|
+
const receipts = result.receipts ?? [];
|
|
125
|
+
this.allReceipts.push(...receipts);
|
|
126
|
+
this.consecutiveFailures = 0;
|
|
127
|
+
if (result.rejected > 0) {
|
|
128
|
+
console.warn(`[swt3-ai] Batch flush: ${result.accepted} accepted, ${result.rejected} rejected`);
|
|
129
|
+
}
|
|
130
|
+
if (!this.ctaShown && (result.accepted ?? 0) > 0) {
|
|
131
|
+
this.ctaShown = true;
|
|
132
|
+
console.info(`\n [SWT3] ${result.accepted} anchors delivered to ${this.config.endpoint}` +
|
|
133
|
+
`\n [SWT3] Dashboard & audit reports \u2192 https://sovereign.tenova.io/signup?ref=sdk` +
|
|
134
|
+
`\n [SWT3] EU AI Act high-risk deadline: Dec 2, 2027. GPAI obligations enforceable now.\n`);
|
|
135
|
+
}
|
|
136
|
+
// Invoke on_flush callback (never let callback errors break the pipeline)
|
|
137
|
+
if (this.onFlush && receipts.length > 0) {
|
|
138
|
+
try {
|
|
139
|
+
this.onFlush(payloads, receipts);
|
|
140
|
+
}
|
|
141
|
+
catch (cbErr) {
|
|
142
|
+
console.warn(`[swt3-ai] onFlush callback error (non-fatal):`, cbErr);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return receipts;
|
|
146
|
+
}
|
|
147
|
+
catch (err) {
|
|
148
|
+
lastError = err instanceof Error ? err.message : String(err);
|
|
149
|
+
console.warn(`[swt3-ai] Batch flush attempt ${attempt + 1} failed: ${lastError}`);
|
|
150
|
+
// Exponential backoff: 1s, 2s, 4s
|
|
151
|
+
if (attempt < this.config.maxRetries - 1) {
|
|
152
|
+
await new Promise((r) => setTimeout(r, 2 ** attempt * 1000));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// All retries exhausted — move to dead-letter queue
|
|
157
|
+
this.consecutiveFailures++;
|
|
158
|
+
const before = this.deadLetter.length;
|
|
159
|
+
this.deadLetter.push(...payloads);
|
|
160
|
+
// Cap the dead-letter queue
|
|
161
|
+
if (this.deadLetter.length > this.maxRetryBuffer) {
|
|
162
|
+
const dropped = this.deadLetter.length - this.maxRetryBuffer;
|
|
163
|
+
this.deadLetter = this.deadLetter.slice(dropped);
|
|
164
|
+
console.error(`[swt3-ai] Dead-letter queue full: ${dropped} oldest payloads dropped (cap: ${this.maxRetryBuffer})`);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
console.warn(`[swt3-ai] Endpoint unreachable — ${payloads.length} payloads moved to dead-letter (total: ${this.deadLetter.length}). Error: ${lastError}`);
|
|
168
|
+
}
|
|
169
|
+
return [];
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=buffer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffer.js","sourceRoot":"","sources":["../src/buffer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,MAAM,wBAAwB,GAAG,IAAI,CAAC;AAEtC,MAAM,OAAO,aAAa;IAChB,MAAM,CAAgB;IACtB,KAAK,GAAqB,EAAE,CAAC;IAC7B,UAAU,GAAqB,EAAE,CAAC;IAClC,cAAc,CAAS;IACvB,WAAW,GAAqB,EAAE,CAAC;IACnC,KAAK,GAAyC,IAAI,CAAC;IACn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IACZ,CAAC;CACF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SWT3 AI Witness SDK — Clearing Engine (Levels 0-3).
|
|
3
|
+
*
|
|
4
|
+
* The "Sovereign Wire" protocol: controls what leaves the developer's
|
|
5
|
+
* infrastructure. Raw prompts/responses NEVER appear in payloads.
|
|
6
|
+
* Clearing operates on the wire payload, not the developer's response.
|
|
7
|
+
*
|
|
8
|
+
* Level 0 — Analytics: All metadata
|
|
9
|
+
* Level 1 — Standard: Hashes + model_id + ai_context
|
|
10
|
+
* Level 2 — Sensitive: Hashes + model_id only. ai_context DELETED.
|
|
11
|
+
* Level 3 — Classified: Factors only. model_id hashed. Everything else DELETED.
|
|
12
|
+
*/
|
|
13
|
+
import type { InferenceRecord, WitnessPayload } from "./types.js";
|
|
14
|
+
/**
|
|
15
|
+
* Extract witness payloads from an inference record.
|
|
16
|
+
* Applies clearing level to each payload via object destructuring (Level 2+
|
|
17
|
+
* fields are simply never assigned, guaranteeing they don't exist on the wire).
|
|
18
|
+
*/
|
|
19
|
+
export declare function extractPayloads(record: InferenceRecord, tenantId: string, clearingLevel: 0 | 1 | 2 | 3, latencyThresholdMs?: number, guardrailsRequired?: number, procedures?: string[], agentId?: string, signingKey?: string, cycleId?: string, policyVersionHash?: string, jurisdiction?: string, legalBasis?: string, purposeClass?: string, authorizationId?: string): WitnessPayload[];
|
|
20
|
+
/**
|
|
21
|
+
* Mint an AI-GRD.3 (Gatekeeper Gate) payload.
|
|
22
|
+
*
|
|
23
|
+
* factor_a = required guardrail count
|
|
24
|
+
* factor_b = actual guardrail count at call time
|
|
25
|
+
* factor_c = 1 if gate passed, 0 if blocked
|
|
26
|
+
* Verdict: PASS if b >= a AND c == 1
|
|
27
|
+
*/
|
|
28
|
+
export declare function extractGatekeeperPayload(tenantId: string, required: number, active: number, gatePassed: boolean, clearingLevel: 0 | 1 | 2 | 3, agentId?: string, signingKey?: string, cycleId?: string, policyVersionHash?: string, jurisdiction?: string, legalBasis?: string, purposeClass?: string): WitnessPayload;
|
|
29
|
+
/** Revocation reason code mapping. */
|
|
30
|
+
export declare const REVOCATION_REASONS: Record<string, number>;
|
|
31
|
+
/**
|
|
32
|
+
* Mint an AI-REV.1 (Anchor Revocation) payload.
|
|
33
|
+
*
|
|
34
|
+
* factor_a = 1 (revocation event occurred)
|
|
35
|
+
* factor_b = 1 (target declared valid by caller)
|
|
36
|
+
* factor_c = reason code (integer from REVOCATION_REASONS)
|
|
37
|
+
*/
|
|
38
|
+
export declare function extractRevocationPayload(tenantId: string, targetFingerprint: string, reason: string, clearingLevel: 0 | 1 | 2 | 3, agentId?: string, signingKey?: string, cycleId?: string, policyVersionHash?: string, jurisdiction?: string, legalBasis?: string, purposeClass?: string): WitnessPayload;
|
|
39
|
+
//# sourceMappingURL=clearing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clearing.d.ts","sourceRoot":"","sources":["../src/clearing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAElE;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,MAAM,EAChB,aAAa,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAC5B,kBAAkB,GAAE,MAAc,EAClC,kBAAkB,GAAE,MAAU,EAC9B,UAAU,CAAC,EAAE,MAAM,EAAE,EACrB,OAAO,CAAC,EAAE,MAAM,EAChB,UAAU,CAAC,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,MAAM,EAChB,iBAAiB,CAAC,EAAE,MAAM,EAC1B,YAAY,CAAC,EAAE,MAAM,EACrB,UAAU,CAAC,EAAE,MAAM,EACnB,YAAY,CAAC,EAAE,MAAM,EACrB,eAAe,CAAC,EAAE,MAAM,GACvB,cAAc,EAAE,CA8MlB;AAoFD;;;;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