agentid-sdk 0.1.19 → 0.1.21
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 +34 -4
- package/dist/{langchain-DJDqqpbT.d.mts → agentid-BmsXTOCc.d.mts} +9 -29
- package/dist/{langchain-DJDqqpbT.d.ts → agentid-BmsXTOCc.d.ts} +9 -29
- package/dist/chunk-4FSHABTE.mjs +2831 -0
- package/dist/index.d.mts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +75 -433
- package/dist/index.mjs +9 -2762
- package/dist/langchain.d.mts +30 -2
- package/dist/langchain.d.ts +30 -2
- package/dist/langchain.js +190 -2
- package/dist/langchain.mjs +423 -2
- package/package.json +1 -1
- package/dist/chunk-6YR4ECGB.mjs +0 -424
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
export { A as AgentID,
|
|
2
|
-
import '@langchain/core/callbacks/base';
|
|
1
|
+
export { A as AgentID, G as GuardParams, a as GuardResponse, L as LogParams, P as PreparedInput, R as RequestOptions, S as SecurityBlockError } from './agentid-BmsXTOCc.mjs';
|
|
3
2
|
|
|
4
3
|
type PIIMapping = Record<string, string>;
|
|
5
4
|
declare class PIIManager {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
export { A as AgentID,
|
|
2
|
-
import '@langchain/core/callbacks/base';
|
|
1
|
+
export { A as AgentID, G as GuardParams, a as GuardResponse, L as LogParams, P as PreparedInput, R as RequestOptions, S as SecurityBlockError } from './agentid-BmsXTOCc.js';
|
|
3
2
|
|
|
4
3
|
type PIIMapping = Record<string, string>;
|
|
5
4
|
declare class PIIManager {
|
package/dist/index.js
CHANGED
|
@@ -31,10 +31,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
AgentID: () => AgentID,
|
|
34
|
-
AgentIDCallbackHandler: () => AgentIDCallbackHandler,
|
|
35
34
|
InjectionScanner: () => InjectionScanner,
|
|
36
35
|
OpenAIAdapter: () => OpenAIAdapter,
|
|
37
36
|
PIIManager: () => PIIManager,
|
|
37
|
+
SecurityBlockError: () => SecurityBlockError,
|
|
38
38
|
getInjectionScanner: () => getInjectionScanner,
|
|
39
39
|
scanWithRegex: () => scanWithRegex
|
|
40
40
|
});
|
|
@@ -84,7 +84,7 @@ var OpenAIAdapter = class {
|
|
|
84
84
|
|
|
85
85
|
// src/sdk-version.ts
|
|
86
86
|
var FALLBACK_SDK_VERSION = "js-0.0.0-dev";
|
|
87
|
-
var AGENTID_SDK_VERSION_HEADER = "js-0.1.
|
|
87
|
+
var AGENTID_SDK_VERSION_HEADER = "js-0.1.21".trim().length > 0 ? "js-0.1.21" : FALLBACK_SDK_VERSION;
|
|
88
88
|
|
|
89
89
|
// src/pii-national-identifiers.ts
|
|
90
90
|
var MAX_CANDIDATES_PER_RULE = 256;
|
|
@@ -1119,6 +1119,7 @@ var DEFAULT_FAIL_OPEN_CONFIG = {
|
|
|
1119
1119
|
shadow_mode: false,
|
|
1120
1120
|
strict_security_mode: false,
|
|
1121
1121
|
failure_mode: "fail_open",
|
|
1122
|
+
block_on_heuristic: false,
|
|
1122
1123
|
block_pii_leakage: false,
|
|
1123
1124
|
block_db_access: false,
|
|
1124
1125
|
block_code_execution: false,
|
|
@@ -1245,6 +1246,19 @@ function readOptionalBooleanField(body, key, fallback) {
|
|
|
1245
1246
|
}
|
|
1246
1247
|
throw new Error(`Invalid config field: ${key}`);
|
|
1247
1248
|
}
|
|
1249
|
+
function readOptionalBooleanAliases(body, keys, fallback) {
|
|
1250
|
+
for (const key of keys) {
|
|
1251
|
+
if (!(key in body)) {
|
|
1252
|
+
continue;
|
|
1253
|
+
}
|
|
1254
|
+
const value = body[key];
|
|
1255
|
+
if (typeof value === "boolean") {
|
|
1256
|
+
return value;
|
|
1257
|
+
}
|
|
1258
|
+
throw new Error(`Invalid config field: ${key}`);
|
|
1259
|
+
}
|
|
1260
|
+
return fallback;
|
|
1261
|
+
}
|
|
1248
1262
|
function readOptionalFailureModeField(body, fallback) {
|
|
1249
1263
|
const value = body.failure_mode;
|
|
1250
1264
|
if (value === "fail_open" || value === "fail_close") {
|
|
@@ -1263,10 +1277,16 @@ function normalizeCapabilityConfig(payload) {
|
|
|
1263
1277
|
strictSecurityMode ? "fail_close" : "fail_open"
|
|
1264
1278
|
);
|
|
1265
1279
|
const effectiveStrictMode = strictSecurityMode || failureMode === "fail_close";
|
|
1280
|
+
const blockOnHeuristic = readOptionalBooleanAliases(
|
|
1281
|
+
body,
|
|
1282
|
+
["block_on_heuristic", "block_on_injection", "block_on_jailbreak"],
|
|
1283
|
+
false
|
|
1284
|
+
);
|
|
1266
1285
|
return {
|
|
1267
1286
|
shadow_mode: readOptionalBooleanField(body, "shadow_mode", false),
|
|
1268
1287
|
strict_security_mode: effectiveStrictMode,
|
|
1269
1288
|
failure_mode: effectiveStrictMode ? "fail_close" : "fail_open",
|
|
1289
|
+
block_on_heuristic: blockOnHeuristic,
|
|
1270
1290
|
block_pii_leakage: readBooleanField(body, "block_pii_leakage", "block_pii"),
|
|
1271
1291
|
block_db_access: readBooleanField(body, "block_db_access", "block_db"),
|
|
1272
1292
|
block_code_execution: readBooleanField(
|
|
@@ -1827,6 +1847,9 @@ function getInjectionScanner() {
|
|
|
1827
1847
|
var DEFAULT_GUARD_TIMEOUT_MS = 1e4;
|
|
1828
1848
|
var MIN_GUARD_TIMEOUT_MS = 500;
|
|
1829
1849
|
var MAX_GUARD_TIMEOUT_MS = 15e3;
|
|
1850
|
+
var DEFAULT_INGEST_TIMEOUT_MS = 1e4;
|
|
1851
|
+
var MIN_INGEST_TIMEOUT_MS = 500;
|
|
1852
|
+
var MAX_INGEST_TIMEOUT_MS = 15e3;
|
|
1830
1853
|
var GUARD_MAX_ATTEMPTS = 3;
|
|
1831
1854
|
var GUARD_RETRY_DELAYS_MS = [250, 500];
|
|
1832
1855
|
var INGEST_MAX_ATTEMPTS = 3;
|
|
@@ -1896,6 +1919,19 @@ function normalizeGuardTimeoutMs(value) {
|
|
|
1896
1919
|
}
|
|
1897
1920
|
return rounded;
|
|
1898
1921
|
}
|
|
1922
|
+
function normalizeIngestTimeoutMs(value) {
|
|
1923
|
+
if (!Number.isFinite(value)) {
|
|
1924
|
+
return DEFAULT_INGEST_TIMEOUT_MS;
|
|
1925
|
+
}
|
|
1926
|
+
const rounded = Math.trunc(value);
|
|
1927
|
+
if (rounded < MIN_INGEST_TIMEOUT_MS) {
|
|
1928
|
+
return MIN_INGEST_TIMEOUT_MS;
|
|
1929
|
+
}
|
|
1930
|
+
if (rounded > MAX_INGEST_TIMEOUT_MS) {
|
|
1931
|
+
return MAX_INGEST_TIMEOUT_MS;
|
|
1932
|
+
}
|
|
1933
|
+
return rounded;
|
|
1934
|
+
}
|
|
1899
1935
|
function resolveConfiguredApiKey(value) {
|
|
1900
1936
|
const explicit = typeof value === "string" ? value.trim() : "";
|
|
1901
1937
|
const fromEnv = globalThis.process?.env?.AGENTID_API_KEY ?? "";
|
|
@@ -2021,6 +2057,13 @@ function createCompletionChunkCollector() {
|
|
|
2021
2057
|
result
|
|
2022
2058
|
};
|
|
2023
2059
|
}
|
|
2060
|
+
var SecurityBlockError = class extends Error {
|
|
2061
|
+
constructor(reason = "guard_denied") {
|
|
2062
|
+
super(`AgentID: Security Blocked (${reason})`);
|
|
2063
|
+
this.name = "SecurityBlockError";
|
|
2064
|
+
this.reason = reason;
|
|
2065
|
+
}
|
|
2066
|
+
};
|
|
2024
2067
|
var AgentID = class {
|
|
2025
2068
|
constructor(config = {}) {
|
|
2026
2069
|
this.injectionScanner = getInjectionScanner();
|
|
@@ -2033,6 +2076,7 @@ var AgentID = class {
|
|
|
2033
2076
|
this.storePii = config.storePii === true;
|
|
2034
2077
|
this.strictMode = config.strictMode === true;
|
|
2035
2078
|
this.guardTimeoutMs = normalizeGuardTimeoutMs(config.guardTimeoutMs);
|
|
2079
|
+
this.ingestTimeoutMs = normalizeIngestTimeoutMs(config.ingestTimeoutMs);
|
|
2036
2080
|
this.pii = new PIIManager();
|
|
2037
2081
|
this.localEnforcer = new LocalSecurityEnforcer(this.pii);
|
|
2038
2082
|
void this.getCapabilityConfig();
|
|
@@ -2123,9 +2167,19 @@ var AgentID = class {
|
|
|
2123
2167
|
const config = await this.getCapabilityConfig(false, options);
|
|
2124
2168
|
return config.strict_security_mode || config.failure_mode === "fail_close";
|
|
2125
2169
|
}
|
|
2170
|
+
shouldRunLocalInjectionScan(config) {
|
|
2171
|
+
if (!this.checkInjection) {
|
|
2172
|
+
return false;
|
|
2173
|
+
}
|
|
2174
|
+
if (config.shadow_mode) {
|
|
2175
|
+
return false;
|
|
2176
|
+
}
|
|
2177
|
+
return config.block_on_heuristic;
|
|
2178
|
+
}
|
|
2126
2179
|
async prepareInputForDispatch(params, options) {
|
|
2127
2180
|
const effectiveApiKey = this.resolveApiKey(options?.apiKey);
|
|
2128
|
-
|
|
2181
|
+
const capabilityConfig = await this.getCapabilityConfig(false, options);
|
|
2182
|
+
if (!params.skipInjectionScan && params.input && this.shouldRunLocalInjectionScan(capabilityConfig)) {
|
|
2129
2183
|
await this.injectionScanner.scan({
|
|
2130
2184
|
prompt: params.input,
|
|
2131
2185
|
apiKey: effectiveApiKey,
|
|
@@ -2136,7 +2190,6 @@ var AgentID = class {
|
|
|
2136
2190
|
source: "js_sdk"
|
|
2137
2191
|
});
|
|
2138
2192
|
}
|
|
2139
|
-
const capabilityConfig = await this.getCapabilityConfig(false, options);
|
|
2140
2193
|
try {
|
|
2141
2194
|
const enforced = this.localEnforcer.enforce({
|
|
2142
2195
|
input: params.input,
|
|
@@ -2168,7 +2221,11 @@ var AgentID = class {
|
|
|
2168
2221
|
}
|
|
2169
2222
|
}
|
|
2170
2223
|
async scanPromptInjection(input, options) {
|
|
2171
|
-
if (!
|
|
2224
|
+
if (!input) {
|
|
2225
|
+
return;
|
|
2226
|
+
}
|
|
2227
|
+
const capabilityConfig = await this.getCapabilityConfig(false, options);
|
|
2228
|
+
if (!this.shouldRunLocalInjectionScan(capabilityConfig)) {
|
|
2172
2229
|
return;
|
|
2173
2230
|
}
|
|
2174
2231
|
const effectiveApiKey = this.resolveApiKey(options?.apiKey);
|
|
@@ -2414,6 +2471,8 @@ var AgentID = class {
|
|
|
2414
2471
|
client_capabilities: params.client_capabilities ?? this.buildClientCapabilities()
|
|
2415
2472
|
};
|
|
2416
2473
|
for (let attempt = 0; attempt < INGEST_MAX_ATTEMPTS; attempt += 1) {
|
|
2474
|
+
const controller = new AbortController();
|
|
2475
|
+
const timeoutId = setTimeout(() => controller.abort(), this.ingestTimeoutMs);
|
|
2417
2476
|
try {
|
|
2418
2477
|
const response = await fetch(`${this.baseUrl}/ingest`, {
|
|
2419
2478
|
method: "POST",
|
|
@@ -2423,7 +2482,8 @@ var AgentID = class {
|
|
|
2423
2482
|
"x-agentid-api-key": effectiveApiKey,
|
|
2424
2483
|
"X-AgentID-SDK-Version": AGENTID_SDK_VERSION_HEADER
|
|
2425
2484
|
},
|
|
2426
|
-
body: JSON.stringify(payload)
|
|
2485
|
+
body: JSON.stringify(payload),
|
|
2486
|
+
signal: controller.signal
|
|
2427
2487
|
});
|
|
2428
2488
|
const responseBody = await safeReadJson2(response);
|
|
2429
2489
|
if (response.ok) {
|
|
@@ -2439,12 +2499,17 @@ var AgentID = class {
|
|
|
2439
2499
|
continue;
|
|
2440
2500
|
}
|
|
2441
2501
|
return { ok: false, status: response.status, reason };
|
|
2442
|
-
} catch {
|
|
2502
|
+
} catch (error) {
|
|
2503
|
+
const isAbortError2 = Boolean(
|
|
2504
|
+
error && typeof error === "object" && error.name === "AbortError"
|
|
2505
|
+
);
|
|
2443
2506
|
if (attempt < INGEST_MAX_ATTEMPTS - 1) {
|
|
2444
2507
|
await waitForIngestRetry(attempt);
|
|
2445
2508
|
continue;
|
|
2446
2509
|
}
|
|
2447
|
-
return { ok: false, status: null, reason: "network_error" };
|
|
2510
|
+
return { ok: false, status: null, reason: isAbortError2 ? "timeout" : "network_error" };
|
|
2511
|
+
} finally {
|
|
2512
|
+
clearTimeout(timeoutId);
|
|
2448
2513
|
}
|
|
2449
2514
|
}
|
|
2450
2515
|
return { ok: false, status: null, reason: "unknown_ingest_failure" };
|
|
@@ -2661,9 +2726,7 @@ var AgentID = class {
|
|
|
2661
2726
|
client_capabilities: this.buildClientCapabilities("openai", false)
|
|
2662
2727
|
}, requestOptions);
|
|
2663
2728
|
if (!verdict.allowed) {
|
|
2664
|
-
throw new
|
|
2665
|
-
`AgentID: Security Blocked (${verdict.reason ?? "guard_denied"})`
|
|
2666
|
-
);
|
|
2729
|
+
throw new SecurityBlockError(verdict.reason ?? "guard_denied");
|
|
2667
2730
|
}
|
|
2668
2731
|
const canonicalClientEventId = typeof verdict.client_event_id === "string" && isUuidLike(verdict.client_event_id) ? verdict.client_event_id : clientEventId;
|
|
2669
2732
|
const guardEventId = typeof verdict.guard_event_id === "string" && verdict.guard_event_id.length > 0 ? verdict.guard_event_id : null;
|
|
@@ -2798,434 +2861,13 @@ var AgentID = class {
|
|
|
2798
2861
|
});
|
|
2799
2862
|
}
|
|
2800
2863
|
};
|
|
2801
|
-
|
|
2802
|
-
// src/langchain.ts
|
|
2803
|
-
var import_base = require("@langchain/core/callbacks/base");
|
|
2804
|
-
function safeString(val) {
|
|
2805
|
-
return typeof val === "string" ? val : "";
|
|
2806
|
-
}
|
|
2807
|
-
function callbackDebugEnabled() {
|
|
2808
|
-
try {
|
|
2809
|
-
return typeof process !== "undefined" && process?.env?.AGENTID_DEBUG_CALLBACK === "1";
|
|
2810
|
-
} catch {
|
|
2811
|
-
return false;
|
|
2812
|
-
}
|
|
2813
|
-
}
|
|
2814
|
-
function logCallbackDebug(message, details) {
|
|
2815
|
-
if (!callbackDebugEnabled()) return;
|
|
2816
|
-
if (details) {
|
|
2817
|
-
console.log(`[AgentID][LC] ${message}`, details);
|
|
2818
|
-
return;
|
|
2819
|
-
}
|
|
2820
|
-
console.log(`[AgentID][LC] ${message}`);
|
|
2821
|
-
}
|
|
2822
|
-
function extractTextFromContent(content) {
|
|
2823
|
-
if (typeof content === "string") {
|
|
2824
|
-
return content;
|
|
2825
|
-
}
|
|
2826
|
-
if (Array.isArray(content)) {
|
|
2827
|
-
const parts = content.map((item) => {
|
|
2828
|
-
if (typeof item === "string") return item;
|
|
2829
|
-
if (!item || typeof item !== "object") return "";
|
|
2830
|
-
const record = item;
|
|
2831
|
-
if (typeof record.text === "string") return record.text;
|
|
2832
|
-
if (typeof record.content === "string") return record.content;
|
|
2833
|
-
return "";
|
|
2834
|
-
}).filter((part) => part.length > 0);
|
|
2835
|
-
return parts.join("\n");
|
|
2836
|
-
}
|
|
2837
|
-
if (content && typeof content === "object") {
|
|
2838
|
-
const record = content;
|
|
2839
|
-
if (typeof record.text === "string") return record.text;
|
|
2840
|
-
if (typeof record.content === "string") return record.content;
|
|
2841
|
-
}
|
|
2842
|
-
return "";
|
|
2843
|
-
}
|
|
2844
|
-
function getMessageRole(msg) {
|
|
2845
|
-
if (!msg || typeof msg !== "object") return null;
|
|
2846
|
-
const typed = msg;
|
|
2847
|
-
if (typeof typed.role === "string") return typed.role;
|
|
2848
|
-
if (typeof typed.type === "string") return typed.type;
|
|
2849
|
-
if (typeof typed._getType === "function") {
|
|
2850
|
-
try {
|
|
2851
|
-
const role = typed._getType();
|
|
2852
|
-
if (typeof role === "string") return role;
|
|
2853
|
-
} catch {
|
|
2854
|
-
}
|
|
2855
|
-
}
|
|
2856
|
-
if (typeof typed.getType === "function") {
|
|
2857
|
-
try {
|
|
2858
|
-
const role = typed.getType();
|
|
2859
|
-
if (typeof role === "string") return role;
|
|
2860
|
-
} catch {
|
|
2861
|
-
}
|
|
2862
|
-
}
|
|
2863
|
-
return null;
|
|
2864
|
-
}
|
|
2865
|
-
function extractPromptFromPrompts(prompts) {
|
|
2866
|
-
if (Array.isArray(prompts) && prompts.length > 0) {
|
|
2867
|
-
return safeString(prompts[prompts.length - 1]);
|
|
2868
|
-
}
|
|
2869
|
-
return "";
|
|
2870
|
-
}
|
|
2871
|
-
function extractPromptFromMessages(messages) {
|
|
2872
|
-
const flat = [];
|
|
2873
|
-
if (Array.isArray(messages)) {
|
|
2874
|
-
for (const item of messages) {
|
|
2875
|
-
if (Array.isArray(item)) {
|
|
2876
|
-
flat.push(...item);
|
|
2877
|
-
} else {
|
|
2878
|
-
flat.push(item);
|
|
2879
|
-
}
|
|
2880
|
-
}
|
|
2881
|
-
}
|
|
2882
|
-
let last = null;
|
|
2883
|
-
for (const msg of flat) {
|
|
2884
|
-
const typed = msg;
|
|
2885
|
-
const role = getMessageRole(msg);
|
|
2886
|
-
if (role === "user" || role === "human") {
|
|
2887
|
-
last = typed;
|
|
2888
|
-
}
|
|
2889
|
-
}
|
|
2890
|
-
if (!last || typeof last !== "object") {
|
|
2891
|
-
return "";
|
|
2892
|
-
}
|
|
2893
|
-
const typedLast = last;
|
|
2894
|
-
return extractTextFromContent(typedLast.content ?? typedLast.text);
|
|
2895
|
-
}
|
|
2896
|
-
function setPromptInPrompts(prompts, sanitizedInput) {
|
|
2897
|
-
if (!Array.isArray(prompts) || prompts.length === 0) {
|
|
2898
|
-
return false;
|
|
2899
|
-
}
|
|
2900
|
-
prompts[prompts.length - 1] = sanitizedInput;
|
|
2901
|
-
return true;
|
|
2902
|
-
}
|
|
2903
|
-
function setPromptInMessages(messages, sanitizedInput) {
|
|
2904
|
-
if (!Array.isArray(messages)) {
|
|
2905
|
-
return false;
|
|
2906
|
-
}
|
|
2907
|
-
const flat = [];
|
|
2908
|
-
for (const item of messages) {
|
|
2909
|
-
if (Array.isArray(item)) {
|
|
2910
|
-
flat.push(...item);
|
|
2911
|
-
} else {
|
|
2912
|
-
flat.push(item);
|
|
2913
|
-
}
|
|
2914
|
-
}
|
|
2915
|
-
for (let i = flat.length - 1; i >= 0; i -= 1) {
|
|
2916
|
-
const candidate = flat[i];
|
|
2917
|
-
if (!candidate || typeof candidate !== "object") {
|
|
2918
|
-
continue;
|
|
2919
|
-
}
|
|
2920
|
-
const typed = candidate;
|
|
2921
|
-
const role = typed.role ?? typed.type;
|
|
2922
|
-
if (role !== "user" && role !== "human") {
|
|
2923
|
-
continue;
|
|
2924
|
-
}
|
|
2925
|
-
if ("content" in typed) {
|
|
2926
|
-
typed.content = sanitizedInput;
|
|
2927
|
-
return true;
|
|
2928
|
-
}
|
|
2929
|
-
if ("text" in typed) {
|
|
2930
|
-
typed.text = sanitizedInput;
|
|
2931
|
-
return true;
|
|
2932
|
-
}
|
|
2933
|
-
typed.content = sanitizedInput;
|
|
2934
|
-
return true;
|
|
2935
|
-
}
|
|
2936
|
-
return false;
|
|
2937
|
-
}
|
|
2938
|
-
function extractModel(serialized, kwargs) {
|
|
2939
|
-
const kw = (kwargs && typeof kwargs === "object" ? kwargs : null) ?? null;
|
|
2940
|
-
const directModel = kw?.model ?? kw?.model_name ?? kw?.modelName;
|
|
2941
|
-
if (typeof directModel === "string" && directModel) return directModel;
|
|
2942
|
-
const invocationModel = kw?.invocation_params?.model ?? kw?.invocation_params?.model_name ?? kw?.invocation_params?.modelName;
|
|
2943
|
-
if (typeof invocationModel === "string" && invocationModel) return invocationModel;
|
|
2944
|
-
const nestedModel = kw?.options?.model ?? kw?.options?.model_name ?? kw?.options?.modelName ?? kw?.kwargs?.model ?? kw?.kwargs?.model_name ?? kw?.kwargs?.modelName;
|
|
2945
|
-
if (typeof nestedModel === "string" && nestedModel) return nestedModel;
|
|
2946
|
-
const ser = (serialized && typeof serialized === "object" ? serialized : null) ?? null;
|
|
2947
|
-
const serKw = (ser?.kwargs && typeof ser.kwargs === "object" ? ser.kwargs : null) ?? null;
|
|
2948
|
-
const serModel = serKw?.model ?? serKw?.model_name ?? serKw?.modelName;
|
|
2949
|
-
if (typeof serModel === "string" && serModel) return serModel;
|
|
2950
|
-
const name = ser?.name ?? ser?.id;
|
|
2951
|
-
if (typeof name === "string" && name) return name;
|
|
2952
|
-
return void 0;
|
|
2953
|
-
}
|
|
2954
|
-
function extractModelFromOutput(output) {
|
|
2955
|
-
const llmOutput = output?.llmOutput ?? output?.llm_output;
|
|
2956
|
-
const llmModel = llmOutput?.model ?? llmOutput?.model_name ?? llmOutput?.modelName;
|
|
2957
|
-
if (typeof llmModel === "string" && llmModel) return llmModel;
|
|
2958
|
-
const first = output?.generations?.[0]?.[0];
|
|
2959
|
-
const responseMeta = first?.message?.response_metadata ?? first?.message?.responseMetadata;
|
|
2960
|
-
const responseModel = responseMeta?.model_name ?? responseMeta?.model ?? responseMeta?.modelName;
|
|
2961
|
-
if (typeof responseModel === "string" && responseModel) return responseModel;
|
|
2962
|
-
const generationInfo = first?.generation_info ?? first?.generationInfo;
|
|
2963
|
-
const genModel = generationInfo?.model_name ?? generationInfo?.model ?? generationInfo?.modelName;
|
|
2964
|
-
if (typeof genModel === "string" && genModel) return genModel;
|
|
2965
|
-
return void 0;
|
|
2966
|
-
}
|
|
2967
|
-
function extractOutputText(output) {
|
|
2968
|
-
const gens = output?.generations;
|
|
2969
|
-
const first = gens?.[0]?.[0];
|
|
2970
|
-
const text = first?.text ?? first?.message?.content;
|
|
2971
|
-
return typeof text === "string" ? text : "";
|
|
2972
|
-
}
|
|
2973
|
-
function extractTokenUsage(output) {
|
|
2974
|
-
const llmOutput = output?.llmOutput ?? output?.llm_output;
|
|
2975
|
-
const usage = llmOutput?.tokenUsage ?? llmOutput?.token_usage ?? llmOutput?.usage ?? void 0;
|
|
2976
|
-
if (usage && typeof usage === "object") {
|
|
2977
|
-
return usage;
|
|
2978
|
-
}
|
|
2979
|
-
const first = output?.generations?.[0]?.[0];
|
|
2980
|
-
const usageMetadata = first?.message?.usage_metadata;
|
|
2981
|
-
if (usageMetadata && typeof usageMetadata === "object") {
|
|
2982
|
-
return usageMetadata;
|
|
2983
|
-
}
|
|
2984
|
-
const responseTokenUsage = first?.message?.response_metadata?.token_usage ?? first?.message?.response_metadata?.tokenUsage ?? void 0;
|
|
2985
|
-
if (responseTokenUsage && typeof responseTokenUsage === "object") {
|
|
2986
|
-
return responseTokenUsage;
|
|
2987
|
-
}
|
|
2988
|
-
const generationTokenUsage = first?.generation_info?.token_usage ?? first?.generation_info?.tokenUsage ?? void 0;
|
|
2989
|
-
if (generationTokenUsage && typeof generationTokenUsage === "object") {
|
|
2990
|
-
return generationTokenUsage;
|
|
2991
|
-
}
|
|
2992
|
-
return void 0;
|
|
2993
|
-
}
|
|
2994
|
-
function isUuidLike2(value) {
|
|
2995
|
-
return typeof value === "string" && /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value.trim());
|
|
2996
|
-
}
|
|
2997
|
-
function createClientEventId() {
|
|
2998
|
-
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
2999
|
-
return crypto.randomUUID();
|
|
3000
|
-
}
|
|
3001
|
-
const segment = () => Math.floor((1 + Math.random()) * 65536).toString(16).slice(1);
|
|
3002
|
-
return `${segment()}${segment()}-${segment()}-4${segment().slice(1)}-a${segment().slice(1)}-${segment()}${segment()}${segment()}`;
|
|
3003
|
-
}
|
|
3004
|
-
function readBooleanField2(value) {
|
|
3005
|
-
return typeof value === "boolean" ? value : null;
|
|
3006
|
-
}
|
|
3007
|
-
function extractStreamFlag(serialized, extraParams) {
|
|
3008
|
-
const extras = extraParams && typeof extraParams === "object" ? extraParams : null;
|
|
3009
|
-
const direct = readBooleanField2(extras?.stream) ?? readBooleanField2(extras?.streaming);
|
|
3010
|
-
if (direct !== null) {
|
|
3011
|
-
return direct;
|
|
3012
|
-
}
|
|
3013
|
-
const invocation = extras?.invocation_params && typeof extras.invocation_params === "object" ? extras.invocation_params : null;
|
|
3014
|
-
const invocationStream = readBooleanField2(invocation?.stream) ?? readBooleanField2(invocation?.streaming);
|
|
3015
|
-
if (invocationStream !== null) {
|
|
3016
|
-
return invocationStream;
|
|
3017
|
-
}
|
|
3018
|
-
const serializedRecord = serialized && typeof serialized === "object" ? serialized : null;
|
|
3019
|
-
const kwargs = serializedRecord?.kwargs && typeof serializedRecord.kwargs === "object" ? serializedRecord.kwargs : null;
|
|
3020
|
-
return readBooleanField2(kwargs?.stream) ?? readBooleanField2(kwargs?.streaming) ?? false;
|
|
3021
|
-
}
|
|
3022
|
-
var AgentIDCallbackHandler = class extends import_base.BaseCallbackHandler {
|
|
3023
|
-
constructor(agent, options) {
|
|
3024
|
-
super();
|
|
3025
|
-
this.name = "agentid_callback_handler";
|
|
3026
|
-
this.runs = /* @__PURE__ */ new Map();
|
|
3027
|
-
this.agent = agent;
|
|
3028
|
-
this.systemId = options.system_id;
|
|
3029
|
-
this.apiKeyOverride = options.apiKey?.trim() || options.api_key?.trim() || void 0;
|
|
3030
|
-
}
|
|
3031
|
-
get requestOptions() {
|
|
3032
|
-
return this.apiKeyOverride ? { apiKey: this.apiKeyOverride } : void 0;
|
|
3033
|
-
}
|
|
3034
|
-
getLangchainCapabilities() {
|
|
3035
|
-
const piiMaskingEnabled = Boolean(
|
|
3036
|
-
this.agent.piiMasking
|
|
3037
|
-
);
|
|
3038
|
-
return {
|
|
3039
|
-
capabilities: {
|
|
3040
|
-
has_feedback_handler: true,
|
|
3041
|
-
pii_masking_enabled: piiMaskingEnabled,
|
|
3042
|
-
framework: "langchain"
|
|
3043
|
-
}
|
|
3044
|
-
};
|
|
3045
|
-
}
|
|
3046
|
-
async preflight(input, stream) {
|
|
3047
|
-
await this.agent.scanPromptInjection(input, this.requestOptions);
|
|
3048
|
-
const prepared = await this.agent.prepareInputForDispatch({
|
|
3049
|
-
input,
|
|
3050
|
-
systemId: this.systemId,
|
|
3051
|
-
stream,
|
|
3052
|
-
skipInjectionScan: true
|
|
3053
|
-
}, this.requestOptions);
|
|
3054
|
-
return prepared.sanitizedInput;
|
|
3055
|
-
}
|
|
3056
|
-
async handleLLMStart(serialized, prompts, runId, _parentRunId, extraParams) {
|
|
3057
|
-
const input = extractPromptFromPrompts(prompts);
|
|
3058
|
-
const id = String(runId ?? "");
|
|
3059
|
-
logCallbackDebug("handleLLMStart", { runId: id, hasInput: input.length > 0 });
|
|
3060
|
-
if (!input) {
|
|
3061
|
-
return;
|
|
3062
|
-
}
|
|
3063
|
-
const stream = extractStreamFlag(serialized, extraParams);
|
|
3064
|
-
const sanitizedInput = await this.preflight(input, stream);
|
|
3065
|
-
if (sanitizedInput !== input) {
|
|
3066
|
-
const mutated = setPromptInPrompts(prompts, sanitizedInput);
|
|
3067
|
-
if (!mutated) {
|
|
3068
|
-
throw new Error(
|
|
3069
|
-
"AgentID: Strict PII mode requires mutable LangChain prompt payload."
|
|
3070
|
-
);
|
|
3071
|
-
}
|
|
3072
|
-
}
|
|
3073
|
-
const requestedClientEventId = isUuidLike2(id) ? id.trim() : createClientEventId();
|
|
3074
|
-
const modelName = extractModel(serialized, extraParams);
|
|
3075
|
-
const verdict = await this.agent.guard({
|
|
3076
|
-
input: sanitizedInput,
|
|
3077
|
-
system_id: this.systemId,
|
|
3078
|
-
model: modelName,
|
|
3079
|
-
client_event_id: requestedClientEventId,
|
|
3080
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
3081
|
-
}, this.requestOptions);
|
|
3082
|
-
if (!verdict.allowed) {
|
|
3083
|
-
throw new Error(`AgentID: Security Blocked (${verdict.reason ?? "guard_denied"})`);
|
|
3084
|
-
}
|
|
3085
|
-
const canonicalClientEventId = isUuidLike2(verdict.client_event_id) ? verdict.client_event_id.trim() : requestedClientEventId;
|
|
3086
|
-
const guardEventId = typeof verdict.guard_event_id === "string" && verdict.guard_event_id.length > 0 ? verdict.guard_event_id : void 0;
|
|
3087
|
-
let transformedInput = typeof verdict.transformed_input === "string" && verdict.transformed_input.length > 0 ? verdict.transformed_input : sanitizedInput;
|
|
3088
|
-
if (transformedInput !== sanitizedInput) {
|
|
3089
|
-
const mutated = setPromptInPrompts(prompts, transformedInput);
|
|
3090
|
-
if (!mutated) {
|
|
3091
|
-
transformedInput = sanitizedInput;
|
|
3092
|
-
}
|
|
3093
|
-
}
|
|
3094
|
-
this.runs.set(id, {
|
|
3095
|
-
input: transformedInput,
|
|
3096
|
-
startedAtMs: Date.now(),
|
|
3097
|
-
model: modelName,
|
|
3098
|
-
clientEventId: canonicalClientEventId,
|
|
3099
|
-
guardEventId
|
|
3100
|
-
});
|
|
3101
|
-
logCallbackDebug("handleLLMStart state_set", {
|
|
3102
|
-
runId: id,
|
|
3103
|
-
clientEventId: canonicalClientEventId,
|
|
3104
|
-
guardEventId: guardEventId ?? null
|
|
3105
|
-
});
|
|
3106
|
-
}
|
|
3107
|
-
async handleChatModelStart(serialized, messages, runId, _parentRunId, extraParams) {
|
|
3108
|
-
const input = extractPromptFromMessages(messages);
|
|
3109
|
-
const id = String(runId ?? "");
|
|
3110
|
-
logCallbackDebug("handleChatModelStart", { runId: id, hasInput: input.length > 0 });
|
|
3111
|
-
if (!input) {
|
|
3112
|
-
return;
|
|
3113
|
-
}
|
|
3114
|
-
const stream = extractStreamFlag(serialized, extraParams);
|
|
3115
|
-
const sanitizedInput = await this.preflight(input, stream);
|
|
3116
|
-
if (sanitizedInput !== input) {
|
|
3117
|
-
const mutated = setPromptInMessages(messages, sanitizedInput);
|
|
3118
|
-
if (!mutated) {
|
|
3119
|
-
throw new Error(
|
|
3120
|
-
"AgentID: Strict PII mode requires mutable LangChain message payload."
|
|
3121
|
-
);
|
|
3122
|
-
}
|
|
3123
|
-
}
|
|
3124
|
-
const requestedClientEventId = isUuidLike2(id) ? id.trim() : createClientEventId();
|
|
3125
|
-
const modelName = extractModel(serialized, extraParams);
|
|
3126
|
-
const verdict = await this.agent.guard({
|
|
3127
|
-
input: sanitizedInput,
|
|
3128
|
-
system_id: this.systemId,
|
|
3129
|
-
model: modelName,
|
|
3130
|
-
client_event_id: requestedClientEventId,
|
|
3131
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
3132
|
-
}, this.requestOptions);
|
|
3133
|
-
if (!verdict.allowed) {
|
|
3134
|
-
throw new Error(`AgentID: Security Blocked (${verdict.reason ?? "guard_denied"})`);
|
|
3135
|
-
}
|
|
3136
|
-
const canonicalClientEventId = isUuidLike2(verdict.client_event_id) ? verdict.client_event_id.trim() : requestedClientEventId;
|
|
3137
|
-
const guardEventId = typeof verdict.guard_event_id === "string" && verdict.guard_event_id.length > 0 ? verdict.guard_event_id : void 0;
|
|
3138
|
-
let transformedInput = typeof verdict.transformed_input === "string" && verdict.transformed_input.length > 0 ? verdict.transformed_input : sanitizedInput;
|
|
3139
|
-
if (transformedInput !== sanitizedInput) {
|
|
3140
|
-
const mutated = setPromptInMessages(messages, transformedInput);
|
|
3141
|
-
if (!mutated) {
|
|
3142
|
-
transformedInput = sanitizedInput;
|
|
3143
|
-
}
|
|
3144
|
-
}
|
|
3145
|
-
this.runs.set(id, {
|
|
3146
|
-
input: transformedInput,
|
|
3147
|
-
startedAtMs: Date.now(),
|
|
3148
|
-
model: modelName,
|
|
3149
|
-
clientEventId: canonicalClientEventId,
|
|
3150
|
-
guardEventId
|
|
3151
|
-
});
|
|
3152
|
-
logCallbackDebug("handleChatModelStart state_set", {
|
|
3153
|
-
runId: id,
|
|
3154
|
-
clientEventId: canonicalClientEventId,
|
|
3155
|
-
guardEventId: guardEventId ?? null
|
|
3156
|
-
});
|
|
3157
|
-
}
|
|
3158
|
-
async handleLLMEnd(output, runId) {
|
|
3159
|
-
const id = String(runId ?? "");
|
|
3160
|
-
logCallbackDebug("handleLLMEnd", { runId: id });
|
|
3161
|
-
const state = this.runs.get(id);
|
|
3162
|
-
if (!state) {
|
|
3163
|
-
logCallbackDebug("handleLLMEnd missing_state", { runId: id });
|
|
3164
|
-
return;
|
|
3165
|
-
}
|
|
3166
|
-
this.runs.delete(id);
|
|
3167
|
-
const latency = Date.now() - state.startedAtMs;
|
|
3168
|
-
const outText = extractOutputText(output);
|
|
3169
|
-
const usage = extractTokenUsage(output);
|
|
3170
|
-
const metadata = {};
|
|
3171
|
-
if (state.clientEventId) {
|
|
3172
|
-
metadata.client_event_id = state.clientEventId;
|
|
3173
|
-
}
|
|
3174
|
-
if (state.guardEventId) {
|
|
3175
|
-
metadata.guard_event_id = state.guardEventId;
|
|
3176
|
-
}
|
|
3177
|
-
const resolvedModel = state.model ?? extractModelFromOutput(output) ?? "unknown";
|
|
3178
|
-
await this.agent.log({
|
|
3179
|
-
system_id: this.systemId,
|
|
3180
|
-
input: state.input,
|
|
3181
|
-
output: outText,
|
|
3182
|
-
event_id: state.clientEventId,
|
|
3183
|
-
model: resolvedModel,
|
|
3184
|
-
usage,
|
|
3185
|
-
latency,
|
|
3186
|
-
metadata: Object.keys(metadata).length > 0 ? metadata : void 0,
|
|
3187
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
3188
|
-
}, this.requestOptions);
|
|
3189
|
-
logCallbackDebug("handleLLMEnd logged", {
|
|
3190
|
-
runId: id,
|
|
3191
|
-
clientEventId: state.clientEventId ?? null
|
|
3192
|
-
});
|
|
3193
|
-
}
|
|
3194
|
-
async handleLLMError(err, runId) {
|
|
3195
|
-
const id = String(runId ?? "");
|
|
3196
|
-
logCallbackDebug("handleLLMError", { runId: id });
|
|
3197
|
-
const state = this.runs.get(id);
|
|
3198
|
-
if (state) this.runs.delete(id);
|
|
3199
|
-
const message = err && typeof err === "object" && "message" in err ? String(err.message) : String(err ?? "");
|
|
3200
|
-
const metadata = {
|
|
3201
|
-
error_message: message
|
|
3202
|
-
};
|
|
3203
|
-
if (state?.clientEventId) {
|
|
3204
|
-
metadata.client_event_id = state.clientEventId;
|
|
3205
|
-
}
|
|
3206
|
-
if (state?.guardEventId) {
|
|
3207
|
-
metadata.guard_event_id = state.guardEventId;
|
|
3208
|
-
}
|
|
3209
|
-
await this.agent.log({
|
|
3210
|
-
system_id: this.systemId,
|
|
3211
|
-
input: state?.input ?? "",
|
|
3212
|
-
output: "",
|
|
3213
|
-
event_id: state?.clientEventId,
|
|
3214
|
-
model: state?.model ?? "unknown",
|
|
3215
|
-
event_type: "error",
|
|
3216
|
-
severity: "error",
|
|
3217
|
-
metadata,
|
|
3218
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
3219
|
-
}, this.requestOptions);
|
|
3220
|
-
}
|
|
3221
|
-
};
|
|
3222
2864
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3223
2865
|
0 && (module.exports = {
|
|
3224
2866
|
AgentID,
|
|
3225
|
-
AgentIDCallbackHandler,
|
|
3226
2867
|
InjectionScanner,
|
|
3227
2868
|
OpenAIAdapter,
|
|
3228
2869
|
PIIManager,
|
|
2870
|
+
SecurityBlockError,
|
|
3229
2871
|
getInjectionScanner,
|
|
3230
2872
|
scanWithRegex
|
|
3231
2873
|
});
|