@veroq/sdk 2.0.0 → 2.1.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 +49 -2
- package/dist/cjs/agent.js +1 -1
- package/dist/cjs/agent.js.map +1 -1
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/cli.js.map +1 -1
- package/dist/cjs/client.d.ts +53 -0
- package/dist/cjs/client.d.ts.map +1 -1
- package/dist/cjs/client.js +116 -1
- package/dist/cjs/client.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +9 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/middleware.d.ts +46 -0
- package/dist/cjs/middleware.d.ts.map +1 -0
- package/dist/cjs/middleware.js +133 -0
- package/dist/cjs/middleware.js.map +1 -0
- package/dist/cjs/shield.d.ts +169 -0
- package/dist/cjs/shield.d.ts.map +1 -0
- package/dist/cjs/shield.js +245 -0
- package/dist/cjs/shield.js.map +1 -0
- package/dist/esm/agent.js +1 -1
- package/dist/esm/agent.js.map +1 -1
- package/dist/esm/cli.js +1 -1
- package/dist/esm/cli.js.map +1 -1
- package/dist/esm/client.d.ts +53 -0
- package/dist/esm/client.d.ts.map +1 -1
- package/dist/esm/client.js +116 -1
- package/dist/esm/client.js.map +1 -1
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/middleware.d.ts +46 -0
- package/dist/esm/middleware.d.ts.map +1 -0
- package/dist/esm/middleware.js +128 -0
- package/dist/esm/middleware.js.map +1 -0
- package/dist/esm/shield.d.ts +169 -0
- package/dist/esm/shield.d.ts.map +1 -0
- package/dist/esm/shield.js +239 -0
- package/dist/esm/shield.js.map +1 -0
- package/package.json +3 -3
package/dist/cjs/index.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.APIError = exports.RateLimitError = exports.NotFoundError = exports.AuthenticationError = exports.PolarisError = exports.VeroqError = exports.Agent = exports.PolarisClient = exports.VeroqClient = void 0;
|
|
3
|
+
exports.APIError = exports.RateLimitError = exports.NotFoundError = exports.AuthenticationError = exports.PolarisError = exports.VeroqError = exports.shieldMiddleware = exports.shieldAnthropic = exports.shieldOpenAI = exports.CachedShield = exports.ShieldResult = exports.shield = exports.Agent = exports.PolarisClient = exports.VeroqClient = void 0;
|
|
4
4
|
var client_js_1 = require("./client.js");
|
|
5
5
|
Object.defineProperty(exports, "VeroqClient", { enumerable: true, get: function () { return client_js_1.VeroqClient; } });
|
|
6
6
|
Object.defineProperty(exports, "PolarisClient", { enumerable: true, get: function () { return client_js_1.PolarisClient; } });
|
|
7
7
|
var agent_js_1 = require("./agent.js");
|
|
8
8
|
Object.defineProperty(exports, "Agent", { enumerable: true, get: function () { return agent_js_1.Agent; } });
|
|
9
|
+
var shield_js_1 = require("./shield.js");
|
|
10
|
+
Object.defineProperty(exports, "shield", { enumerable: true, get: function () { return shield_js_1.shield; } });
|
|
11
|
+
Object.defineProperty(exports, "ShieldResult", { enumerable: true, get: function () { return shield_js_1.ShieldResult; } });
|
|
12
|
+
Object.defineProperty(exports, "CachedShield", { enumerable: true, get: function () { return shield_js_1.CachedShield; } });
|
|
13
|
+
var middleware_js_1 = require("./middleware.js");
|
|
14
|
+
Object.defineProperty(exports, "shieldOpenAI", { enumerable: true, get: function () { return middleware_js_1.shieldOpenAI; } });
|
|
15
|
+
Object.defineProperty(exports, "shieldAnthropic", { enumerable: true, get: function () { return middleware_js_1.shieldAnthropic; } });
|
|
16
|
+
Object.defineProperty(exports, "shieldMiddleware", { enumerable: true, get: function () { return middleware_js_1.shieldMiddleware; } });
|
|
9
17
|
var errors_js_1 = require("./errors.js");
|
|
10
18
|
Object.defineProperty(exports, "VeroqError", { enumerable: true, get: function () { return errors_js_1.VeroqError; } });
|
|
11
19
|
Object.defineProperty(exports, "PolarisError", { enumerable: true, get: function () { return errors_js_1.PolarisError; } });
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,yCAAyD;AAAhD,wGAAA,WAAW,OAAA;AAAE,0GAAA,aAAa,OAAA;AACnC,uCAAmC;AAA1B,iGAAA,KAAK,OAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,yCAAyD;AAAhD,wGAAA,WAAW,OAAA;AAAE,0GAAA,aAAa,OAAA;AACnC,uCAAmC;AAA1B,iGAAA,KAAK,OAAA;AACd,yCAAyJ;AAAhJ,mGAAA,MAAM,OAAA;AAAE,yGAAA,YAAY,OAAA;AAAE,yGAAA,YAAY,OAAA;AAC3C,iDAAkF;AAAzE,6GAAA,YAAY,OAAA;AAAE,gHAAA,eAAe,OAAA;AAAE,iHAAA,gBAAgB,OAAA;AAExD,yCAOqB;AANnB,uGAAA,UAAU,OAAA;AACV,yGAAA,YAAY,OAAA;AACZ,gHAAA,mBAAmB,OAAA;AACnB,0GAAA,aAAa,OAAA;AACb,2GAAA,cAAc,OAAA;AACd,qGAAA,QAAQ,OAAA"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VeroQ Middleware — Auto-shield LLM responses in any framework.
|
|
3
|
+
*
|
|
4
|
+
* @example OpenAI wrapper
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { shieldOpenAI } from "@veroq/sdk";
|
|
7
|
+
* import OpenAI from "openai";
|
|
8
|
+
*
|
|
9
|
+
* const client = shieldOpenAI(new OpenAI());
|
|
10
|
+
* const response = await client.chat.completions.create({
|
|
11
|
+
* model: "gpt-4o",
|
|
12
|
+
* messages: [{ role: "user", content: "What's NVIDIA's revenue?" }],
|
|
13
|
+
* });
|
|
14
|
+
* console.log(response.choices[0].message.content);
|
|
15
|
+
* console.log(response.veroqShield.trustScore); // 0.85
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example Express middleware
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { shieldMiddleware } from "@veroq/sdk";
|
|
21
|
+
* app.use("/api/ai", shieldMiddleware({ threshold: 0.7 }));
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
import { ShieldResult } from "./shield.js";
|
|
25
|
+
export interface ShieldedResponse<T = any> {
|
|
26
|
+
original: T;
|
|
27
|
+
veroqShield: ShieldResult;
|
|
28
|
+
}
|
|
29
|
+
export declare function shieldOpenAI(client: any, options?: {
|
|
30
|
+
source?: string;
|
|
31
|
+
agentId?: string;
|
|
32
|
+
maxClaims?: number;
|
|
33
|
+
}): any;
|
|
34
|
+
export declare function shieldAnthropic(client: any, options?: {
|
|
35
|
+
source?: string;
|
|
36
|
+
agentId?: string;
|
|
37
|
+
maxClaims?: number;
|
|
38
|
+
}): any;
|
|
39
|
+
export declare function shieldMiddleware(options?: {
|
|
40
|
+
threshold?: number;
|
|
41
|
+
source?: string;
|
|
42
|
+
agentId?: string;
|
|
43
|
+
maxClaims?: number;
|
|
44
|
+
blockIfUntrusted?: boolean;
|
|
45
|
+
}): (req: any, res: any, next: any) => void;
|
|
46
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/middleware.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAU,YAAY,EAAE,MAAM,aAAa,CAAC;AAInD,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,GAAG;IACvC,QAAQ,EAAE,CAAC,CAAC;IACZ,WAAW,EAAE,YAAY,CAAC;CAC3B;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,GAAE;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;CACf,GAAG,GAAG,CA8BX;AAID,wBAAgB,eAAe,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,GAAE;IACpD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;CACf,GAAG,GAAG,CA8BX;AAID,wBAAgB,gBAAgB,CAAC,OAAO,GAAE;IACxC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB,CAAC,EAAE,OAAO,CAAC;CACvB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,KAAK,IAAI,CA+C/C"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* VeroQ Middleware — Auto-shield LLM responses in any framework.
|
|
4
|
+
*
|
|
5
|
+
* @example OpenAI wrapper
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { shieldOpenAI } from "@veroq/sdk";
|
|
8
|
+
* import OpenAI from "openai";
|
|
9
|
+
*
|
|
10
|
+
* const client = shieldOpenAI(new OpenAI());
|
|
11
|
+
* const response = await client.chat.completions.create({
|
|
12
|
+
* model: "gpt-4o",
|
|
13
|
+
* messages: [{ role: "user", content: "What's NVIDIA's revenue?" }],
|
|
14
|
+
* });
|
|
15
|
+
* console.log(response.choices[0].message.content);
|
|
16
|
+
* console.log(response.veroqShield.trustScore); // 0.85
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @example Express middleware
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { shieldMiddleware } from "@veroq/sdk";
|
|
22
|
+
* app.use("/api/ai", shieldMiddleware({ threshold: 0.7 }));
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.shieldOpenAI = shieldOpenAI;
|
|
27
|
+
exports.shieldAnthropic = shieldAnthropic;
|
|
28
|
+
exports.shieldMiddleware = shieldMiddleware;
|
|
29
|
+
const shield_js_1 = require("./shield.js");
|
|
30
|
+
function shieldOpenAI(client, options = {}) {
|
|
31
|
+
const source = options.source || "openai";
|
|
32
|
+
const originalCreate = client.chat.completions.create.bind(client.chat.completions);
|
|
33
|
+
client.chat.completions.create = async function (...args) {
|
|
34
|
+
const response = await originalCreate(...args);
|
|
35
|
+
try {
|
|
36
|
+
const text = response.choices?.[0]?.message?.content || "";
|
|
37
|
+
const model = response.model || source;
|
|
38
|
+
if (text.trim().length >= 20) {
|
|
39
|
+
response.veroqShield = await (0, shield_js_1.shield)(text, {
|
|
40
|
+
source: model,
|
|
41
|
+
agentId: options.agentId,
|
|
42
|
+
maxClaims: options.maxClaims,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
response.veroqShield = new shield_js_1.ShieldResult({
|
|
47
|
+
text, claims: [], overall_confidence: 1.0, overall_verdict: "no_claims",
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
response.veroqShield = new shield_js_1.ShieldResult({
|
|
53
|
+
text: "", claims: [], overall_confidence: 0, overall_verdict: "error",
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return response;
|
|
57
|
+
};
|
|
58
|
+
return client;
|
|
59
|
+
}
|
|
60
|
+
// ── Anthropic Wrapper ──
|
|
61
|
+
function shieldAnthropic(client, options = {}) {
|
|
62
|
+
const source = options.source || "anthropic";
|
|
63
|
+
const originalCreate = client.messages.create.bind(client.messages);
|
|
64
|
+
client.messages.create = async function (...args) {
|
|
65
|
+
const response = await originalCreate(...args);
|
|
66
|
+
try {
|
|
67
|
+
const text = response.content?.[0]?.text || "";
|
|
68
|
+
const model = response.model || source;
|
|
69
|
+
if (text.trim().length >= 20) {
|
|
70
|
+
response.veroqShield = await (0, shield_js_1.shield)(text, {
|
|
71
|
+
source: model,
|
|
72
|
+
agentId: options.agentId,
|
|
73
|
+
maxClaims: options.maxClaims,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
response.veroqShield = new shield_js_1.ShieldResult({
|
|
78
|
+
text, claims: [], overall_confidence: 1.0, overall_verdict: "no_claims",
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
response.veroqShield = new shield_js_1.ShieldResult({
|
|
84
|
+
text: "", claims: [], overall_confidence: 0, overall_verdict: "error",
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return response;
|
|
88
|
+
};
|
|
89
|
+
return client;
|
|
90
|
+
}
|
|
91
|
+
// ── Express Middleware ──
|
|
92
|
+
function shieldMiddleware(options = {}) {
|
|
93
|
+
const threshold = options.threshold ?? 0;
|
|
94
|
+
return (req, res, next) => {
|
|
95
|
+
const originalJson = res.json.bind(res);
|
|
96
|
+
res.json = async function (body) {
|
|
97
|
+
// Only shield responses that have text content
|
|
98
|
+
const text = typeof body === "string" ? body
|
|
99
|
+
: body?.text || body?.content || body?.message || body?.summary || body?.answer;
|
|
100
|
+
if (text && typeof text === "string" && text.length >= 20) {
|
|
101
|
+
try {
|
|
102
|
+
const result = await (0, shield_js_1.shield)(text, {
|
|
103
|
+
source: options.source,
|
|
104
|
+
agentId: options.agentId,
|
|
105
|
+
maxClaims: options.maxClaims,
|
|
106
|
+
});
|
|
107
|
+
body._veroqShield = {
|
|
108
|
+
trustScore: result.trustScore,
|
|
109
|
+
isTrusted: result.isTrusted,
|
|
110
|
+
claimsChecked: result.claimsExtracted,
|
|
111
|
+
corrections: result.corrections,
|
|
112
|
+
receiptIds: result.receiptIds,
|
|
113
|
+
};
|
|
114
|
+
if (options.blockIfUntrusted && !result.isTrusted) {
|
|
115
|
+
return originalJson.call(this, {
|
|
116
|
+
error: "Response blocked by VeroQ Shield — claims contradicted",
|
|
117
|
+
shield: body._veroqShield,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (threshold > 0 && result.trustScore < threshold) {
|
|
121
|
+
body._veroqShield.belowThreshold = true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// Shield failure is non-blocking
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return originalJson.call(this, body);
|
|
129
|
+
};
|
|
130
|
+
next();
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/middleware.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;AAWH,oCAkCC;AAID,0CAkCC;AAID,4CAqDC;AA1ID,2CAAmD;AASnD,SAAgB,YAAY,CAAC,MAAW,EAAE,UAItC,EAAE;IACJ,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,QAAQ,CAAC;IAE1C,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAEpF,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,GAAG,KAAK,WAAW,GAAG,IAAW;QAC7D,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;QAC/C,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,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QACvC,CAAC,CAAC;QAEF,IAAI,EAAE,CAAC;IACT,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VeroQ Prompt Shield — One-line verification for any LLM output.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { shield } from "@veroq/sdk";
|
|
7
|
+
*
|
|
8
|
+
* const result = await shield("NVIDIA reported $22B in Q4 revenue");
|
|
9
|
+
* console.log(result.trustScore); // 0.73
|
|
10
|
+
* console.log(result.isTrusted); // false
|
|
11
|
+
* console.log(result.corrections); // [{claim, correction, confidence}]
|
|
12
|
+
* console.log(result.verifiedText); // text with corrections inline
|
|
13
|
+
*
|
|
14
|
+
* // Wrap any LLM call
|
|
15
|
+
* const response = await openai.chat.completions.create({...});
|
|
16
|
+
* const verified = await shield(response.choices[0].message.content);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export interface ShieldOptions {
|
|
20
|
+
/** Source identifier (e.g., "gpt-4o", "claude-3") */
|
|
21
|
+
source?: string;
|
|
22
|
+
/** Agent ID for memory integration */
|
|
23
|
+
agentId?: string;
|
|
24
|
+
/** Max claims to extract (1-10, default 5) */
|
|
25
|
+
maxClaims?: number;
|
|
26
|
+
/** API key override */
|
|
27
|
+
apiKey?: string;
|
|
28
|
+
/** API base URL override */
|
|
29
|
+
baseUrl?: string;
|
|
30
|
+
/** If true, throws VeroqError when claims are contradicted */
|
|
31
|
+
blockIfUntrusted?: boolean;
|
|
32
|
+
}
|
|
33
|
+
export interface ShieldClaim {
|
|
34
|
+
text: string;
|
|
35
|
+
category: string;
|
|
36
|
+
verdict: string;
|
|
37
|
+
confidence: number;
|
|
38
|
+
correction: string | null;
|
|
39
|
+
receiptId: string | null;
|
|
40
|
+
evidenceChain: Array<{
|
|
41
|
+
source: string;
|
|
42
|
+
position: string;
|
|
43
|
+
snippet: string;
|
|
44
|
+
}>;
|
|
45
|
+
}
|
|
46
|
+
export declare class ShieldResult {
|
|
47
|
+
/** Original text (truncated) */
|
|
48
|
+
readonly text: string;
|
|
49
|
+
/** Source identifier */
|
|
50
|
+
readonly source: string;
|
|
51
|
+
/** All extracted claims with verdicts */
|
|
52
|
+
readonly claims: ShieldClaim[];
|
|
53
|
+
/** Number of claims extracted */
|
|
54
|
+
readonly claimsExtracted: number;
|
|
55
|
+
/** Number of claims verified */
|
|
56
|
+
readonly claimsVerified: number;
|
|
57
|
+
/** Number of claims supported */
|
|
58
|
+
readonly claimsSupported: number;
|
|
59
|
+
/** Number of claims contradicted */
|
|
60
|
+
readonly claimsContradicted: number;
|
|
61
|
+
/** Overall trust score (0-1) */
|
|
62
|
+
readonly trustScore: number;
|
|
63
|
+
/** Overall verdict */
|
|
64
|
+
readonly overallVerdict: string;
|
|
65
|
+
/** Summary */
|
|
66
|
+
readonly summary: string;
|
|
67
|
+
/** Credits used */
|
|
68
|
+
readonly creditsUsed: number;
|
|
69
|
+
/** Processing time in ms */
|
|
70
|
+
readonly processingTimeMs: number;
|
|
71
|
+
/** Full original text (before server truncation) */
|
|
72
|
+
private readonly _fullText;
|
|
73
|
+
constructor(raw: Record<string, any>, fullText?: string);
|
|
74
|
+
/** True if no claims were contradicted */
|
|
75
|
+
get isTrusted(): boolean;
|
|
76
|
+
/** Corrections for contradicted claims */
|
|
77
|
+
get corrections(): Array<{
|
|
78
|
+
claim: string;
|
|
79
|
+
correction: string;
|
|
80
|
+
confidence: number;
|
|
81
|
+
}>;
|
|
82
|
+
/** Text with contradicted claims annotated */
|
|
83
|
+
get verifiedText(): string;
|
|
84
|
+
/** Verification receipt IDs */
|
|
85
|
+
get receiptIds(): string[];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Shield any LLM output with VeroQ verification.
|
|
89
|
+
*
|
|
90
|
+
* Extracts claims, verifies each against evidence, returns trust score
|
|
91
|
+
* with corrections for any contradicted claims.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* import { shield } from "@veroq/sdk";
|
|
96
|
+
*
|
|
97
|
+
* // Basic
|
|
98
|
+
* const result = await shield("NVIDIA's Q4 revenue exceeded $22B");
|
|
99
|
+
* console.log(result.trustScore, result.corrections);
|
|
100
|
+
*
|
|
101
|
+
* // With options
|
|
102
|
+
* const result = await shield(llmOutput, {
|
|
103
|
+
* source: "gpt-4o",
|
|
104
|
+
* agentId: "my-bot",
|
|
105
|
+
* blockIfUntrusted: true,
|
|
106
|
+
* });
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare function shield(text: string, options?: ShieldOptions): Promise<ShieldResult>;
|
|
110
|
+
export interface CachedShieldOptions {
|
|
111
|
+
/** Maximum entries in the LRU cache (default 500) */
|
|
112
|
+
maxCache?: number;
|
|
113
|
+
/** Time-to-live for cached results in milliseconds (default 3_600_000 = 1 hour) */
|
|
114
|
+
ttlMs?: number;
|
|
115
|
+
/** API key override */
|
|
116
|
+
apiKey?: string;
|
|
117
|
+
/** API base URL override */
|
|
118
|
+
baseUrl?: string;
|
|
119
|
+
}
|
|
120
|
+
export interface CachedShieldStats {
|
|
121
|
+
hits: number;
|
|
122
|
+
misses: number;
|
|
123
|
+
hitRate: number;
|
|
124
|
+
size: number;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Lightweight shield with local caching for repeated verifications.
|
|
128
|
+
*
|
|
129
|
+
* Caches results by SHA-256 hash of text. Reduces API calls for repeated or
|
|
130
|
+
* similar content. Useful for high-volume pipelines or edge/offline scenarios.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* import { CachedShield } from "@veroq/sdk";
|
|
135
|
+
*
|
|
136
|
+
* const cached = new CachedShield({ maxCache: 1000, ttlMs: 30 * 60_000 });
|
|
137
|
+
* const r1 = await cached.shield("NVIDIA reported $22B in Q4 revenue"); // API call
|
|
138
|
+
* const r2 = await cached.shield("NVIDIA reported $22B in Q4 revenue"); // instant cache hit
|
|
139
|
+
* console.log(cached.stats()); // { hits: 1, misses: 1, hitRate: 0.5, size: 1 }
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export declare class CachedShield {
|
|
143
|
+
private readonly _maxCache;
|
|
144
|
+
private readonly _ttlMs;
|
|
145
|
+
private readonly _apiKey?;
|
|
146
|
+
private readonly _baseUrl?;
|
|
147
|
+
private readonly _cache;
|
|
148
|
+
private readonly _inFlight;
|
|
149
|
+
private _hits;
|
|
150
|
+
private _misses;
|
|
151
|
+
private _lastEvict;
|
|
152
|
+
constructor(options?: CachedShieldOptions);
|
|
153
|
+
private static _hashText;
|
|
154
|
+
private _evictExpired;
|
|
155
|
+
private _evictLru;
|
|
156
|
+
/**
|
|
157
|
+
* Verify text using cached shield.
|
|
158
|
+
*
|
|
159
|
+
* Checks local cache first (by SHA-256 of text). On miss, calls the
|
|
160
|
+
* regular shield() function and stores the result. Deduplicates
|
|
161
|
+
* concurrent calls for the same text.
|
|
162
|
+
*/
|
|
163
|
+
shield(text: string, options?: ShieldOptions): Promise<ShieldResult>;
|
|
164
|
+
/** Return cache hit/miss statistics. */
|
|
165
|
+
stats(): CachedShieldStats;
|
|
166
|
+
/** Clear the cache and reset stats. */
|
|
167
|
+
clear(): void;
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=shield.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shield.d.ts","sourceRoot":"","sources":["../../src/shield.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAMH,MAAM,WAAW,aAAa;IAC5B,qDAAqD;IACrD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,sCAAsC;IACtC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,8CAA8C;IAC9C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,uBAAuB;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,4BAA4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AC,MAAM,CAAsC;IAC7D,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAiD;IAC3E,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,OAAO,CAAK;IACpB,OAAO,CAAC,UAAU,CAAK;gBAEX,OAAO,GAAE,mBAAwB;IAO7C,OAAO,CAAC,MAAM,CAAC,SAAS;IAIxB,OAAO,CAAC,aAAa;IASrB,OAAO,CAAC,SAAS;IAQjB;;;;;;OAMG;IACG,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,aAAkB,GAAG,OAAO,CAAC,YAAY,CAAC;IAwC9E,wCAAwC;IACxC,KAAK,IAAI,iBAAiB;IAU1B,uCAAuC;IACvC,KAAK,IAAI,IAAI;CAMd"}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* VeroQ Prompt Shield — One-line verification for any LLM output.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { shield } from "@veroq/sdk";
|
|
8
|
+
*
|
|
9
|
+
* const result = await shield("NVIDIA reported $22B in Q4 revenue");
|
|
10
|
+
* console.log(result.trustScore); // 0.73
|
|
11
|
+
* console.log(result.isTrusted); // false
|
|
12
|
+
* console.log(result.corrections); // [{claim, correction, confidence}]
|
|
13
|
+
* console.log(result.verifiedText); // text with corrections inline
|
|
14
|
+
*
|
|
15
|
+
* // Wrap any LLM call
|
|
16
|
+
* const response = await openai.chat.completions.create({...});
|
|
17
|
+
* const verified = await shield(response.choices[0].message.content);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.CachedShield = exports.ShieldResult = void 0;
|
|
22
|
+
exports.shield = shield;
|
|
23
|
+
const node_crypto_1 = require("node:crypto");
|
|
24
|
+
const client_js_1 = require("./client.js");
|
|
25
|
+
const errors_js_1 = require("./errors.js");
|
|
26
|
+
class ShieldResult {
|
|
27
|
+
constructor(raw, fullText) {
|
|
28
|
+
this._fullText = fullText || raw.text || "";
|
|
29
|
+
this.text = raw.text || "";
|
|
30
|
+
this.source = raw.source || "unknown";
|
|
31
|
+
this.claims = (raw.claims || []).map((c) => ({
|
|
32
|
+
text: c.text,
|
|
33
|
+
category: c.category,
|
|
34
|
+
verdict: c.verdict,
|
|
35
|
+
confidence: c.confidence || 0,
|
|
36
|
+
correction: c.correction || null,
|
|
37
|
+
receiptId: c.receipt_id || null,
|
|
38
|
+
evidenceChain: c.evidence_chain || [],
|
|
39
|
+
}));
|
|
40
|
+
this.claimsExtracted = raw.claims_extracted || 0;
|
|
41
|
+
this.claimsVerified = raw.claims_verified || 0;
|
|
42
|
+
this.claimsSupported = raw.claims_supported || 0;
|
|
43
|
+
this.claimsContradicted = raw.claims_contradicted || 0;
|
|
44
|
+
this.trustScore = raw.overall_confidence || 0;
|
|
45
|
+
this.overallVerdict = raw.overall_verdict || "unknown";
|
|
46
|
+
this.summary = raw.summary || "";
|
|
47
|
+
this.creditsUsed = raw.credits_used || 0;
|
|
48
|
+
this.processingTimeMs = raw.processing_time_ms || 0;
|
|
49
|
+
}
|
|
50
|
+
/** True if no claims were contradicted */
|
|
51
|
+
get isTrusted() {
|
|
52
|
+
return this.claimsContradicted === 0;
|
|
53
|
+
}
|
|
54
|
+
/** Corrections for contradicted claims */
|
|
55
|
+
get corrections() {
|
|
56
|
+
return this.claims
|
|
57
|
+
.filter(c => c.verdict === "contradicted" && c.correction)
|
|
58
|
+
.map(c => ({ claim: c.text, correction: c.correction, confidence: c.confidence }));
|
|
59
|
+
}
|
|
60
|
+
/** Text with contradicted claims annotated */
|
|
61
|
+
get verifiedText() {
|
|
62
|
+
let result = this._fullText;
|
|
63
|
+
for (const c of this.claims) {
|
|
64
|
+
if (c.verdict === "contradicted" && c.correction && result.includes(c.text)) {
|
|
65
|
+
result = result.replace(c.text, `[CORRECTED: ${c.correction.slice(0, 200)}]`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
/** Verification receipt IDs */
|
|
71
|
+
get receiptIds() {
|
|
72
|
+
return this.claims.map(c => c.receiptId).filter((id) => !!id);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.ShieldResult = ShieldResult;
|
|
76
|
+
// Module-level client
|
|
77
|
+
let _client = null;
|
|
78
|
+
function getClient(apiKey, baseUrl) {
|
|
79
|
+
if (!_client || apiKey) {
|
|
80
|
+
_client = new client_js_1.VeroqClient({ apiKey, baseUrl });
|
|
81
|
+
}
|
|
82
|
+
return _client;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Shield any LLM output with VeroQ verification.
|
|
86
|
+
*
|
|
87
|
+
* Extracts claims, verifies each against evidence, returns trust score
|
|
88
|
+
* with corrections for any contradicted claims.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* import { shield } from "@veroq/sdk";
|
|
93
|
+
*
|
|
94
|
+
* // Basic
|
|
95
|
+
* const result = await shield("NVIDIA's Q4 revenue exceeded $22B");
|
|
96
|
+
* console.log(result.trustScore, result.corrections);
|
|
97
|
+
*
|
|
98
|
+
* // With options
|
|
99
|
+
* const result = await shield(llmOutput, {
|
|
100
|
+
* source: "gpt-4o",
|
|
101
|
+
* agentId: "my-bot",
|
|
102
|
+
* blockIfUntrusted: true,
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
async function shield(text, options = {}) {
|
|
107
|
+
if (!text || text.trim().length < 20) {
|
|
108
|
+
return new ShieldResult({
|
|
109
|
+
text: text || "",
|
|
110
|
+
claims: [],
|
|
111
|
+
claims_extracted: 0,
|
|
112
|
+
overall_confidence: 1.0,
|
|
113
|
+
overall_verdict: "no_claims",
|
|
114
|
+
summary: "Text too short to extract verifiable claims.",
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const client = getClient(options.apiKey, options.baseUrl);
|
|
118
|
+
const raw = await client.verifyOutput(text, {
|
|
119
|
+
source: options.source,
|
|
120
|
+
maxClaims: options.maxClaims,
|
|
121
|
+
});
|
|
122
|
+
// Store to memory if agentId provided
|
|
123
|
+
if (options.agentId && raw.claims?.length) {
|
|
124
|
+
client.memoryStore(options.agentId, `shield:${text.slice(0, 50).replace(/\s+/g, "_")}`, {
|
|
125
|
+
trust_score: raw.overall_confidence,
|
|
126
|
+
verdict: raw.overall_verdict,
|
|
127
|
+
claims_extracted: raw.claims_extracted,
|
|
128
|
+
claims_contradicted: raw.claims_contradicted,
|
|
129
|
+
}, { category: "verification", queryText: text.slice(0, 200) }).catch(() => { });
|
|
130
|
+
}
|
|
131
|
+
const result = new ShieldResult(raw, text);
|
|
132
|
+
if (options.blockIfUntrusted && !result.isTrusted) {
|
|
133
|
+
throw new errors_js_1.VeroqError(`Shield blocked: ${result.claimsContradicted} of ${result.claimsExtracted} claims contradicted. ` +
|
|
134
|
+
`Corrections: ${result.corrections.map(c => c.claim.slice(0, 80)).join("; ")}`);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Lightweight shield with local caching for repeated verifications.
|
|
140
|
+
*
|
|
141
|
+
* Caches results by SHA-256 hash of text. Reduces API calls for repeated or
|
|
142
|
+
* similar content. Useful for high-volume pipelines or edge/offline scenarios.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* import { CachedShield } from "@veroq/sdk";
|
|
147
|
+
*
|
|
148
|
+
* const cached = new CachedShield({ maxCache: 1000, ttlMs: 30 * 60_000 });
|
|
149
|
+
* const r1 = await cached.shield("NVIDIA reported $22B in Q4 revenue"); // API call
|
|
150
|
+
* const r2 = await cached.shield("NVIDIA reported $22B in Q4 revenue"); // instant cache hit
|
|
151
|
+
* console.log(cached.stats()); // { hits: 1, misses: 1, hitRate: 0.5, size: 1 }
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
class CachedShield {
|
|
155
|
+
constructor(options = {}) {
|
|
156
|
+
this._cache = new Map();
|
|
157
|
+
this._inFlight = new Map();
|
|
158
|
+
this._hits = 0;
|
|
159
|
+
this._misses = 0;
|
|
160
|
+
this._lastEvict = 0;
|
|
161
|
+
this._maxCache = Math.max(1, options.maxCache ?? 500);
|
|
162
|
+
this._ttlMs = Math.max(1, options.ttlMs ?? 3600000);
|
|
163
|
+
this._apiKey = options.apiKey;
|
|
164
|
+
this._baseUrl = options.baseUrl;
|
|
165
|
+
}
|
|
166
|
+
static _hashText(text) {
|
|
167
|
+
return (0, node_crypto_1.createHash)("sha256").update(text, "utf8").digest("hex");
|
|
168
|
+
}
|
|
169
|
+
_evictExpired() {
|
|
170
|
+
const now = Date.now();
|
|
171
|
+
for (const [key, entry] of this._cache) {
|
|
172
|
+
if (now - entry.timestamp > this._ttlMs) {
|
|
173
|
+
this._cache.delete(key);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
_evictLru() {
|
|
178
|
+
// Map iteration order is insertion order; oldest entries are first.
|
|
179
|
+
while (this._cache.size > this._maxCache) {
|
|
180
|
+
const firstKey = this._cache.keys().next().value;
|
|
181
|
+
this._cache.delete(firstKey);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Verify text using cached shield.
|
|
186
|
+
*
|
|
187
|
+
* Checks local cache first (by SHA-256 of text). On miss, calls the
|
|
188
|
+
* regular shield() function and stores the result. Deduplicates
|
|
189
|
+
* concurrent calls for the same text.
|
|
190
|
+
*/
|
|
191
|
+
async shield(text, options = {}) {
|
|
192
|
+
const key = CachedShield._hashText(text);
|
|
193
|
+
// Amortize TTL eviction — run at most once per second
|
|
194
|
+
const now = Date.now();
|
|
195
|
+
if (now - this._lastEvict > 1000) {
|
|
196
|
+
this._evictExpired();
|
|
197
|
+
this._lastEvict = now;
|
|
198
|
+
}
|
|
199
|
+
const cached = this._cache.get(key);
|
|
200
|
+
if (cached) {
|
|
201
|
+
// Move to end (most recently used) by re-inserting
|
|
202
|
+
this._cache.delete(key);
|
|
203
|
+
this._cache.set(key, cached);
|
|
204
|
+
this._hits++;
|
|
205
|
+
return cached.result;
|
|
206
|
+
}
|
|
207
|
+
// Deduplicate concurrent calls for the same text (counts as a miss, not a hit)
|
|
208
|
+
const inflight = this._inFlight.get(key);
|
|
209
|
+
if (inflight) {
|
|
210
|
+
return inflight;
|
|
211
|
+
}
|
|
212
|
+
// Cache miss — call API and share the Promise with any concurrent callers
|
|
213
|
+
this._misses++;
|
|
214
|
+
const promise = shield(text, { ...options, apiKey: this._apiKey ?? options.apiKey, baseUrl: this._baseUrl ?? options.baseUrl });
|
|
215
|
+
this._inFlight.set(key, promise);
|
|
216
|
+
try {
|
|
217
|
+
const result = await promise;
|
|
218
|
+
this._cache.set(key, { result, timestamp: Date.now() });
|
|
219
|
+
this._evictLru();
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
this._inFlight.delete(key);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/** Return cache hit/miss statistics. */
|
|
227
|
+
stats() {
|
|
228
|
+
const total = this._hits + this._misses;
|
|
229
|
+
return {
|
|
230
|
+
hits: this._hits,
|
|
231
|
+
misses: this._misses,
|
|
232
|
+
hitRate: total > 0 ? this._hits / total : 0,
|
|
233
|
+
size: this._cache.size,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
/** Clear the cache and reset stats. */
|
|
237
|
+
clear() {
|
|
238
|
+
this._cache.clear();
|
|
239
|
+
this._inFlight.clear();
|
|
240
|
+
this._hits = 0;
|
|
241
|
+
this._misses = 0;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
exports.CachedShield = CachedShield;
|
|
245
|
+
//# sourceMappingURL=shield.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shield.js","sourceRoot":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,CAAC,CAAC;YACxD,IAAI,CAAC,SAAS,EAAE,CAAC;YACjB,OAAO,MAAM,CAAC;QAChB,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC;IAED,wCAAwC;IACxC,KAAK;QACH,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC;QACxC,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,KAAK;YAChB,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,OAAO,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;YAC3C,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI;SACvB,CAAC;IACJ,CAAC;IAED,uCAAuC;IACvC,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACpB,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;QACf,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;IACnB,CAAC;CACF;AAxGD,oCAwGC"}
|
package/dist/esm/agent.js
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
export class Agent {
|
|
16
16
|
constructor(options = {}) {
|
|
17
17
|
this.apiKey = options.apiKey || process.env.VEROQ_API_KEY || process.env.POLARIS_API_KEY || '';
|
|
18
|
-
this.baseUrl = (options.baseUrl || 'https://api.
|
|
18
|
+
this.baseUrl = (options.baseUrl || 'https://api.veroq.ai').replace(/\/$/, '');
|
|
19
19
|
this.timeout = options.timeout || 30000;
|
|
20
20
|
}
|
|
21
21
|
async request(method, path, body) {
|