agentid-sdk 0.1.20 → 0.1.22
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 +1 -0
- package/dist/{langchain-BipmisU1.d.mts → agentid-BmsXTOCc.d.mts} +3 -29
- package/dist/{langchain-BipmisU1.d.ts → agentid-BmsXTOCc.d.ts} +3 -29
- package/dist/{chunk-LOZUJLLF.mjs → chunk-FVTL572H.mjs} +655 -1046
- package/dist/index.d.mts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +37 -427
- package/dist/index.mjs +1 -3
- package/dist/langchain.d.mts +30 -2
- package/dist/langchain.d.ts +30 -2
- package/dist/langchain.js +1 -1
- package/dist/langchain.mjs +423 -2
- package/package.json +1 -1
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,7 +31,6 @@ 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,
|
|
@@ -85,7 +84,7 @@ var OpenAIAdapter = class {
|
|
|
85
84
|
|
|
86
85
|
// src/sdk-version.ts
|
|
87
86
|
var FALLBACK_SDK_VERSION = "js-0.0.0-dev";
|
|
88
|
-
var AGENTID_SDK_VERSION_HEADER = "js-0.1.
|
|
87
|
+
var AGENTID_SDK_VERSION_HEADER = "js-0.1.22".trim().length > 0 ? "js-0.1.22" : FALLBACK_SDK_VERSION;
|
|
89
88
|
|
|
90
89
|
// src/pii-national-identifiers.ts
|
|
91
90
|
var MAX_CANDIDATES_PER_RULE = 256;
|
|
@@ -1120,6 +1119,7 @@ var DEFAULT_FAIL_OPEN_CONFIG = {
|
|
|
1120
1119
|
shadow_mode: false,
|
|
1121
1120
|
strict_security_mode: false,
|
|
1122
1121
|
failure_mode: "fail_open",
|
|
1122
|
+
block_on_heuristic: false,
|
|
1123
1123
|
block_pii_leakage: false,
|
|
1124
1124
|
block_db_access: false,
|
|
1125
1125
|
block_code_execution: false,
|
|
@@ -1246,6 +1246,19 @@ function readOptionalBooleanField(body, key, fallback) {
|
|
|
1246
1246
|
}
|
|
1247
1247
|
throw new Error(`Invalid config field: ${key}`);
|
|
1248
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
|
+
}
|
|
1249
1262
|
function readOptionalFailureModeField(body, fallback) {
|
|
1250
1263
|
const value = body.failure_mode;
|
|
1251
1264
|
if (value === "fail_open" || value === "fail_close") {
|
|
@@ -1264,10 +1277,16 @@ function normalizeCapabilityConfig(payload) {
|
|
|
1264
1277
|
strictSecurityMode ? "fail_close" : "fail_open"
|
|
1265
1278
|
);
|
|
1266
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
|
+
);
|
|
1267
1285
|
return {
|
|
1268
1286
|
shadow_mode: readOptionalBooleanField(body, "shadow_mode", false),
|
|
1269
1287
|
strict_security_mode: effectiveStrictMode,
|
|
1270
1288
|
failure_mode: effectiveStrictMode ? "fail_close" : "fail_open",
|
|
1289
|
+
block_on_heuristic: blockOnHeuristic,
|
|
1271
1290
|
block_pii_leakage: readBooleanField(body, "block_pii_leakage", "block_pii"),
|
|
1272
1291
|
block_db_access: readBooleanField(body, "block_db_access", "block_db"),
|
|
1273
1292
|
block_code_execution: readBooleanField(
|
|
@@ -2148,9 +2167,19 @@ var AgentID = class {
|
|
|
2148
2167
|
const config = await this.getCapabilityConfig(false, options);
|
|
2149
2168
|
return config.strict_security_mode || config.failure_mode === "fail_close";
|
|
2150
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
|
+
}
|
|
2151
2179
|
async prepareInputForDispatch(params, options) {
|
|
2152
2180
|
const effectiveApiKey = this.resolveApiKey(options?.apiKey);
|
|
2153
|
-
|
|
2181
|
+
const capabilityConfig = await this.getCapabilityConfig(false, options);
|
|
2182
|
+
if (!params.skipInjectionScan && params.input && this.shouldRunLocalInjectionScan(capabilityConfig)) {
|
|
2154
2183
|
await this.injectionScanner.scan({
|
|
2155
2184
|
prompt: params.input,
|
|
2156
2185
|
apiKey: effectiveApiKey,
|
|
@@ -2161,7 +2190,6 @@ var AgentID = class {
|
|
|
2161
2190
|
source: "js_sdk"
|
|
2162
2191
|
});
|
|
2163
2192
|
}
|
|
2164
|
-
const capabilityConfig = await this.getCapabilityConfig(false, options);
|
|
2165
2193
|
try {
|
|
2166
2194
|
const enforced = this.localEnforcer.enforce({
|
|
2167
2195
|
input: params.input,
|
|
@@ -2193,7 +2221,11 @@ var AgentID = class {
|
|
|
2193
2221
|
}
|
|
2194
2222
|
}
|
|
2195
2223
|
async scanPromptInjection(input, options) {
|
|
2196
|
-
if (!
|
|
2224
|
+
if (!input) {
|
|
2225
|
+
return;
|
|
2226
|
+
}
|
|
2227
|
+
const capabilityConfig = await this.getCapabilityConfig(false, options);
|
|
2228
|
+
if (!this.shouldRunLocalInjectionScan(capabilityConfig)) {
|
|
2197
2229
|
return;
|
|
2198
2230
|
}
|
|
2199
2231
|
const effectiveApiKey = this.resolveApiKey(options?.apiKey);
|
|
@@ -2829,431 +2861,9 @@ var AgentID = class {
|
|
|
2829
2861
|
});
|
|
2830
2862
|
}
|
|
2831
2863
|
};
|
|
2832
|
-
|
|
2833
|
-
// src/langchain.ts
|
|
2834
|
-
var import_base = require("@langchain/core/callbacks/base");
|
|
2835
|
-
function safeString(val) {
|
|
2836
|
-
return typeof val === "string" ? val : "";
|
|
2837
|
-
}
|
|
2838
|
-
function callbackDebugEnabled() {
|
|
2839
|
-
try {
|
|
2840
|
-
return typeof process !== "undefined" && process?.env?.AGENTID_DEBUG_CALLBACK === "1";
|
|
2841
|
-
} catch {
|
|
2842
|
-
return false;
|
|
2843
|
-
}
|
|
2844
|
-
}
|
|
2845
|
-
function logCallbackDebug(message, details) {
|
|
2846
|
-
if (!callbackDebugEnabled()) return;
|
|
2847
|
-
if (details) {
|
|
2848
|
-
console.log(`[AgentID][LC] ${message}`, details);
|
|
2849
|
-
return;
|
|
2850
|
-
}
|
|
2851
|
-
console.log(`[AgentID][LC] ${message}`);
|
|
2852
|
-
}
|
|
2853
|
-
function extractTextFromContent(content) {
|
|
2854
|
-
if (typeof content === "string") {
|
|
2855
|
-
return content;
|
|
2856
|
-
}
|
|
2857
|
-
if (Array.isArray(content)) {
|
|
2858
|
-
const parts = content.map((item) => {
|
|
2859
|
-
if (typeof item === "string") return item;
|
|
2860
|
-
if (!item || typeof item !== "object") return "";
|
|
2861
|
-
const record = item;
|
|
2862
|
-
if (typeof record.text === "string") return record.text;
|
|
2863
|
-
if (typeof record.content === "string") return record.content;
|
|
2864
|
-
return "";
|
|
2865
|
-
}).filter((part) => part.length > 0);
|
|
2866
|
-
return parts.join("\n");
|
|
2867
|
-
}
|
|
2868
|
-
if (content && typeof content === "object") {
|
|
2869
|
-
const record = content;
|
|
2870
|
-
if (typeof record.text === "string") return record.text;
|
|
2871
|
-
if (typeof record.content === "string") return record.content;
|
|
2872
|
-
}
|
|
2873
|
-
return "";
|
|
2874
|
-
}
|
|
2875
|
-
function getMessageRole(msg) {
|
|
2876
|
-
if (!msg || typeof msg !== "object") return null;
|
|
2877
|
-
const typed = msg;
|
|
2878
|
-
if (typeof typed.role === "string") return typed.role;
|
|
2879
|
-
if (typeof typed.type === "string") return typed.type;
|
|
2880
|
-
if (typeof typed._getType === "function") {
|
|
2881
|
-
try {
|
|
2882
|
-
const role = typed._getType();
|
|
2883
|
-
if (typeof role === "string") return role;
|
|
2884
|
-
} catch {
|
|
2885
|
-
}
|
|
2886
|
-
}
|
|
2887
|
-
if (typeof typed.getType === "function") {
|
|
2888
|
-
try {
|
|
2889
|
-
const role = typed.getType();
|
|
2890
|
-
if (typeof role === "string") return role;
|
|
2891
|
-
} catch {
|
|
2892
|
-
}
|
|
2893
|
-
}
|
|
2894
|
-
return null;
|
|
2895
|
-
}
|
|
2896
|
-
function extractPromptFromPrompts(prompts) {
|
|
2897
|
-
if (Array.isArray(prompts) && prompts.length > 0) {
|
|
2898
|
-
return safeString(prompts[prompts.length - 1]);
|
|
2899
|
-
}
|
|
2900
|
-
return "";
|
|
2901
|
-
}
|
|
2902
|
-
function extractPromptFromMessages(messages) {
|
|
2903
|
-
const flat = [];
|
|
2904
|
-
if (Array.isArray(messages)) {
|
|
2905
|
-
for (const item of messages) {
|
|
2906
|
-
if (Array.isArray(item)) {
|
|
2907
|
-
flat.push(...item);
|
|
2908
|
-
} else {
|
|
2909
|
-
flat.push(item);
|
|
2910
|
-
}
|
|
2911
|
-
}
|
|
2912
|
-
}
|
|
2913
|
-
let last = null;
|
|
2914
|
-
for (const msg of flat) {
|
|
2915
|
-
const typed = msg;
|
|
2916
|
-
const role = getMessageRole(msg);
|
|
2917
|
-
if (role === "user" || role === "human") {
|
|
2918
|
-
last = typed;
|
|
2919
|
-
}
|
|
2920
|
-
}
|
|
2921
|
-
if (!last || typeof last !== "object") {
|
|
2922
|
-
return "";
|
|
2923
|
-
}
|
|
2924
|
-
const typedLast = last;
|
|
2925
|
-
return extractTextFromContent(typedLast.content ?? typedLast.text);
|
|
2926
|
-
}
|
|
2927
|
-
function setPromptInPrompts(prompts, sanitizedInput) {
|
|
2928
|
-
if (!Array.isArray(prompts) || prompts.length === 0) {
|
|
2929
|
-
return false;
|
|
2930
|
-
}
|
|
2931
|
-
prompts[prompts.length - 1] = sanitizedInput;
|
|
2932
|
-
return true;
|
|
2933
|
-
}
|
|
2934
|
-
function setPromptInMessages(messages, sanitizedInput) {
|
|
2935
|
-
if (!Array.isArray(messages)) {
|
|
2936
|
-
return false;
|
|
2937
|
-
}
|
|
2938
|
-
const flat = [];
|
|
2939
|
-
for (const item of messages) {
|
|
2940
|
-
if (Array.isArray(item)) {
|
|
2941
|
-
flat.push(...item);
|
|
2942
|
-
} else {
|
|
2943
|
-
flat.push(item);
|
|
2944
|
-
}
|
|
2945
|
-
}
|
|
2946
|
-
for (let i = flat.length - 1; i >= 0; i -= 1) {
|
|
2947
|
-
const candidate = flat[i];
|
|
2948
|
-
if (!candidate || typeof candidate !== "object") {
|
|
2949
|
-
continue;
|
|
2950
|
-
}
|
|
2951
|
-
const typed = candidate;
|
|
2952
|
-
const role = typed.role ?? typed.type;
|
|
2953
|
-
if (role !== "user" && role !== "human") {
|
|
2954
|
-
continue;
|
|
2955
|
-
}
|
|
2956
|
-
if ("content" in typed) {
|
|
2957
|
-
typed.content = sanitizedInput;
|
|
2958
|
-
return true;
|
|
2959
|
-
}
|
|
2960
|
-
if ("text" in typed) {
|
|
2961
|
-
typed.text = sanitizedInput;
|
|
2962
|
-
return true;
|
|
2963
|
-
}
|
|
2964
|
-
typed.content = sanitizedInput;
|
|
2965
|
-
return true;
|
|
2966
|
-
}
|
|
2967
|
-
return false;
|
|
2968
|
-
}
|
|
2969
|
-
function extractModel(serialized, kwargs) {
|
|
2970
|
-
const kw = (kwargs && typeof kwargs === "object" ? kwargs : null) ?? null;
|
|
2971
|
-
const directModel = kw?.model ?? kw?.model_name ?? kw?.modelName;
|
|
2972
|
-
if (typeof directModel === "string" && directModel) return directModel;
|
|
2973
|
-
const invocationModel = kw?.invocation_params?.model ?? kw?.invocation_params?.model_name ?? kw?.invocation_params?.modelName;
|
|
2974
|
-
if (typeof invocationModel === "string" && invocationModel) return invocationModel;
|
|
2975
|
-
const nestedModel = kw?.options?.model ?? kw?.options?.model_name ?? kw?.options?.modelName ?? kw?.kwargs?.model ?? kw?.kwargs?.model_name ?? kw?.kwargs?.modelName;
|
|
2976
|
-
if (typeof nestedModel === "string" && nestedModel) return nestedModel;
|
|
2977
|
-
const ser = (serialized && typeof serialized === "object" ? serialized : null) ?? null;
|
|
2978
|
-
const serKw = (ser?.kwargs && typeof ser.kwargs === "object" ? ser.kwargs : null) ?? null;
|
|
2979
|
-
const serModel = serKw?.model ?? serKw?.model_name ?? serKw?.modelName;
|
|
2980
|
-
if (typeof serModel === "string" && serModel) return serModel;
|
|
2981
|
-
const name = ser?.name ?? ser?.id;
|
|
2982
|
-
if (typeof name === "string" && name) return name;
|
|
2983
|
-
return void 0;
|
|
2984
|
-
}
|
|
2985
|
-
function extractModelFromOutput(output) {
|
|
2986
|
-
const llmOutput = output?.llmOutput ?? output?.llm_output;
|
|
2987
|
-
const llmModel = llmOutput?.model ?? llmOutput?.model_name ?? llmOutput?.modelName;
|
|
2988
|
-
if (typeof llmModel === "string" && llmModel) return llmModel;
|
|
2989
|
-
const first = output?.generations?.[0]?.[0];
|
|
2990
|
-
const responseMeta = first?.message?.response_metadata ?? first?.message?.responseMetadata;
|
|
2991
|
-
const responseModel = responseMeta?.model_name ?? responseMeta?.model ?? responseMeta?.modelName;
|
|
2992
|
-
if (typeof responseModel === "string" && responseModel) return responseModel;
|
|
2993
|
-
const generationInfo = first?.generation_info ?? first?.generationInfo;
|
|
2994
|
-
const genModel = generationInfo?.model_name ?? generationInfo?.model ?? generationInfo?.modelName;
|
|
2995
|
-
if (typeof genModel === "string" && genModel) return genModel;
|
|
2996
|
-
return void 0;
|
|
2997
|
-
}
|
|
2998
|
-
function extractOutputText(output) {
|
|
2999
|
-
const gens = output?.generations;
|
|
3000
|
-
const first = gens?.[0]?.[0];
|
|
3001
|
-
const text = first?.text ?? first?.message?.content;
|
|
3002
|
-
return typeof text === "string" ? text : "";
|
|
3003
|
-
}
|
|
3004
|
-
function extractTokenUsage(output) {
|
|
3005
|
-
const llmOutput = output?.llmOutput ?? output?.llm_output;
|
|
3006
|
-
const usage = llmOutput?.tokenUsage ?? llmOutput?.token_usage ?? llmOutput?.usage ?? void 0;
|
|
3007
|
-
if (usage && typeof usage === "object") {
|
|
3008
|
-
return usage;
|
|
3009
|
-
}
|
|
3010
|
-
const first = output?.generations?.[0]?.[0];
|
|
3011
|
-
const usageMetadata = first?.message?.usage_metadata;
|
|
3012
|
-
if (usageMetadata && typeof usageMetadata === "object") {
|
|
3013
|
-
return usageMetadata;
|
|
3014
|
-
}
|
|
3015
|
-
const responseTokenUsage = first?.message?.response_metadata?.token_usage ?? first?.message?.response_metadata?.tokenUsage ?? void 0;
|
|
3016
|
-
if (responseTokenUsage && typeof responseTokenUsage === "object") {
|
|
3017
|
-
return responseTokenUsage;
|
|
3018
|
-
}
|
|
3019
|
-
const generationTokenUsage = first?.generation_info?.token_usage ?? first?.generation_info?.tokenUsage ?? void 0;
|
|
3020
|
-
if (generationTokenUsage && typeof generationTokenUsage === "object") {
|
|
3021
|
-
return generationTokenUsage;
|
|
3022
|
-
}
|
|
3023
|
-
return void 0;
|
|
3024
|
-
}
|
|
3025
|
-
function isUuidLike2(value) {
|
|
3026
|
-
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());
|
|
3027
|
-
}
|
|
3028
|
-
function createClientEventId() {
|
|
3029
|
-
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
3030
|
-
return crypto.randomUUID();
|
|
3031
|
-
}
|
|
3032
|
-
const segment = () => Math.floor((1 + Math.random()) * 65536).toString(16).slice(1);
|
|
3033
|
-
return `${segment()}${segment()}-${segment()}-4${segment().slice(1)}-a${segment().slice(1)}-${segment()}${segment()}${segment()}`;
|
|
3034
|
-
}
|
|
3035
|
-
function readBooleanField2(value) {
|
|
3036
|
-
return typeof value === "boolean" ? value : null;
|
|
3037
|
-
}
|
|
3038
|
-
function extractStreamFlag(serialized, extraParams) {
|
|
3039
|
-
const extras = extraParams && typeof extraParams === "object" ? extraParams : null;
|
|
3040
|
-
const direct = readBooleanField2(extras?.stream) ?? readBooleanField2(extras?.streaming);
|
|
3041
|
-
if (direct !== null) {
|
|
3042
|
-
return direct;
|
|
3043
|
-
}
|
|
3044
|
-
const invocation = extras?.invocation_params && typeof extras.invocation_params === "object" ? extras.invocation_params : null;
|
|
3045
|
-
const invocationStream = readBooleanField2(invocation?.stream) ?? readBooleanField2(invocation?.streaming);
|
|
3046
|
-
if (invocationStream !== null) {
|
|
3047
|
-
return invocationStream;
|
|
3048
|
-
}
|
|
3049
|
-
const serializedRecord = serialized && typeof serialized === "object" ? serialized : null;
|
|
3050
|
-
const kwargs = serializedRecord?.kwargs && typeof serializedRecord.kwargs === "object" ? serializedRecord.kwargs : null;
|
|
3051
|
-
return readBooleanField2(kwargs?.stream) ?? readBooleanField2(kwargs?.streaming) ?? false;
|
|
3052
|
-
}
|
|
3053
|
-
var AgentIDCallbackHandler = class extends import_base.BaseCallbackHandler {
|
|
3054
|
-
constructor(agent, options) {
|
|
3055
|
-
super();
|
|
3056
|
-
this.name = "agentid_callback_handler";
|
|
3057
|
-
this.runs = /* @__PURE__ */ new Map();
|
|
3058
|
-
this.agent = agent;
|
|
3059
|
-
this.systemId = options.system_id;
|
|
3060
|
-
this.apiKeyOverride = options.apiKey?.trim() || options.api_key?.trim() || void 0;
|
|
3061
|
-
}
|
|
3062
|
-
get requestOptions() {
|
|
3063
|
-
return this.apiKeyOverride ? { apiKey: this.apiKeyOverride } : void 0;
|
|
3064
|
-
}
|
|
3065
|
-
getLangchainCapabilities() {
|
|
3066
|
-
const piiMaskingEnabled = Boolean(
|
|
3067
|
-
this.agent.piiMasking
|
|
3068
|
-
);
|
|
3069
|
-
return {
|
|
3070
|
-
capabilities: {
|
|
3071
|
-
has_feedback_handler: true,
|
|
3072
|
-
pii_masking_enabled: piiMaskingEnabled,
|
|
3073
|
-
framework: "langchain"
|
|
3074
|
-
}
|
|
3075
|
-
};
|
|
3076
|
-
}
|
|
3077
|
-
async preflight(input, stream) {
|
|
3078
|
-
await this.agent.scanPromptInjection(input, this.requestOptions);
|
|
3079
|
-
const prepared = await this.agent.prepareInputForDispatch({
|
|
3080
|
-
input,
|
|
3081
|
-
systemId: this.systemId,
|
|
3082
|
-
stream,
|
|
3083
|
-
skipInjectionScan: true
|
|
3084
|
-
}, this.requestOptions);
|
|
3085
|
-
return prepared.sanitizedInput;
|
|
3086
|
-
}
|
|
3087
|
-
async handleLLMStart(serialized, prompts, runId, _parentRunId, extraParams) {
|
|
3088
|
-
const input = extractPromptFromPrompts(prompts);
|
|
3089
|
-
const id = String(runId ?? "");
|
|
3090
|
-
logCallbackDebug("handleLLMStart", { runId: id, hasInput: input.length > 0 });
|
|
3091
|
-
if (!input) {
|
|
3092
|
-
return;
|
|
3093
|
-
}
|
|
3094
|
-
const stream = extractStreamFlag(serialized, extraParams);
|
|
3095
|
-
const sanitizedInput = await this.preflight(input, stream);
|
|
3096
|
-
if (sanitizedInput !== input) {
|
|
3097
|
-
const mutated = setPromptInPrompts(prompts, sanitizedInput);
|
|
3098
|
-
if (!mutated) {
|
|
3099
|
-
throw new Error(
|
|
3100
|
-
"AgentID: Strict PII mode requires mutable LangChain prompt payload."
|
|
3101
|
-
);
|
|
3102
|
-
}
|
|
3103
|
-
}
|
|
3104
|
-
const requestedClientEventId = isUuidLike2(id) ? id.trim() : createClientEventId();
|
|
3105
|
-
const modelName = extractModel(serialized, extraParams);
|
|
3106
|
-
const verdict = await this.agent.guard({
|
|
3107
|
-
input: sanitizedInput,
|
|
3108
|
-
system_id: this.systemId,
|
|
3109
|
-
model: modelName,
|
|
3110
|
-
client_event_id: requestedClientEventId,
|
|
3111
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
3112
|
-
}, this.requestOptions);
|
|
3113
|
-
if (!verdict.allowed) {
|
|
3114
|
-
throw new SecurityBlockError(verdict.reason ?? "guard_denied");
|
|
3115
|
-
}
|
|
3116
|
-
const canonicalClientEventId = isUuidLike2(verdict.client_event_id) ? verdict.client_event_id.trim() : requestedClientEventId;
|
|
3117
|
-
const guardEventId = typeof verdict.guard_event_id === "string" && verdict.guard_event_id.length > 0 ? verdict.guard_event_id : void 0;
|
|
3118
|
-
let transformedInput = typeof verdict.transformed_input === "string" && verdict.transformed_input.length > 0 ? verdict.transformed_input : sanitizedInput;
|
|
3119
|
-
if (transformedInput !== sanitizedInput) {
|
|
3120
|
-
const mutated = setPromptInPrompts(prompts, transformedInput);
|
|
3121
|
-
if (!mutated) {
|
|
3122
|
-
transformedInput = sanitizedInput;
|
|
3123
|
-
}
|
|
3124
|
-
}
|
|
3125
|
-
this.runs.set(id, {
|
|
3126
|
-
input: transformedInput,
|
|
3127
|
-
startedAtMs: Date.now(),
|
|
3128
|
-
model: modelName,
|
|
3129
|
-
clientEventId: canonicalClientEventId,
|
|
3130
|
-
guardEventId
|
|
3131
|
-
});
|
|
3132
|
-
logCallbackDebug("handleLLMStart state_set", {
|
|
3133
|
-
runId: id,
|
|
3134
|
-
clientEventId: canonicalClientEventId,
|
|
3135
|
-
guardEventId: guardEventId ?? null
|
|
3136
|
-
});
|
|
3137
|
-
}
|
|
3138
|
-
async handleChatModelStart(serialized, messages, runId, _parentRunId, extraParams) {
|
|
3139
|
-
const input = extractPromptFromMessages(messages);
|
|
3140
|
-
const id = String(runId ?? "");
|
|
3141
|
-
logCallbackDebug("handleChatModelStart", { runId: id, hasInput: input.length > 0 });
|
|
3142
|
-
if (!input) {
|
|
3143
|
-
return;
|
|
3144
|
-
}
|
|
3145
|
-
const stream = extractStreamFlag(serialized, extraParams);
|
|
3146
|
-
const sanitizedInput = await this.preflight(input, stream);
|
|
3147
|
-
if (sanitizedInput !== input) {
|
|
3148
|
-
const mutated = setPromptInMessages(messages, sanitizedInput);
|
|
3149
|
-
if (!mutated) {
|
|
3150
|
-
throw new Error(
|
|
3151
|
-
"AgentID: Strict PII mode requires mutable LangChain message payload."
|
|
3152
|
-
);
|
|
3153
|
-
}
|
|
3154
|
-
}
|
|
3155
|
-
const requestedClientEventId = isUuidLike2(id) ? id.trim() : createClientEventId();
|
|
3156
|
-
const modelName = extractModel(serialized, extraParams);
|
|
3157
|
-
const verdict = await this.agent.guard({
|
|
3158
|
-
input: sanitizedInput,
|
|
3159
|
-
system_id: this.systemId,
|
|
3160
|
-
model: modelName,
|
|
3161
|
-
client_event_id: requestedClientEventId,
|
|
3162
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
3163
|
-
}, this.requestOptions);
|
|
3164
|
-
if (!verdict.allowed) {
|
|
3165
|
-
throw new SecurityBlockError(verdict.reason ?? "guard_denied");
|
|
3166
|
-
}
|
|
3167
|
-
const canonicalClientEventId = isUuidLike2(verdict.client_event_id) ? verdict.client_event_id.trim() : requestedClientEventId;
|
|
3168
|
-
const guardEventId = typeof verdict.guard_event_id === "string" && verdict.guard_event_id.length > 0 ? verdict.guard_event_id : void 0;
|
|
3169
|
-
let transformedInput = typeof verdict.transformed_input === "string" && verdict.transformed_input.length > 0 ? verdict.transformed_input : sanitizedInput;
|
|
3170
|
-
if (transformedInput !== sanitizedInput) {
|
|
3171
|
-
const mutated = setPromptInMessages(messages, transformedInput);
|
|
3172
|
-
if (!mutated) {
|
|
3173
|
-
transformedInput = sanitizedInput;
|
|
3174
|
-
}
|
|
3175
|
-
}
|
|
3176
|
-
this.runs.set(id, {
|
|
3177
|
-
input: transformedInput,
|
|
3178
|
-
startedAtMs: Date.now(),
|
|
3179
|
-
model: modelName,
|
|
3180
|
-
clientEventId: canonicalClientEventId,
|
|
3181
|
-
guardEventId
|
|
3182
|
-
});
|
|
3183
|
-
logCallbackDebug("handleChatModelStart state_set", {
|
|
3184
|
-
runId: id,
|
|
3185
|
-
clientEventId: canonicalClientEventId,
|
|
3186
|
-
guardEventId: guardEventId ?? null
|
|
3187
|
-
});
|
|
3188
|
-
}
|
|
3189
|
-
async handleLLMEnd(output, runId) {
|
|
3190
|
-
const id = String(runId ?? "");
|
|
3191
|
-
logCallbackDebug("handleLLMEnd", { runId: id });
|
|
3192
|
-
const state = this.runs.get(id);
|
|
3193
|
-
if (!state) {
|
|
3194
|
-
logCallbackDebug("handleLLMEnd missing_state", { runId: id });
|
|
3195
|
-
return;
|
|
3196
|
-
}
|
|
3197
|
-
this.runs.delete(id);
|
|
3198
|
-
const latency = Date.now() - state.startedAtMs;
|
|
3199
|
-
const outText = extractOutputText(output);
|
|
3200
|
-
const usage = extractTokenUsage(output);
|
|
3201
|
-
const metadata = {};
|
|
3202
|
-
if (state.clientEventId) {
|
|
3203
|
-
metadata.client_event_id = state.clientEventId;
|
|
3204
|
-
}
|
|
3205
|
-
if (state.guardEventId) {
|
|
3206
|
-
metadata.guard_event_id = state.guardEventId;
|
|
3207
|
-
}
|
|
3208
|
-
const resolvedModel = state.model ?? extractModelFromOutput(output) ?? "unknown";
|
|
3209
|
-
await this.agent.log({
|
|
3210
|
-
system_id: this.systemId,
|
|
3211
|
-
input: state.input,
|
|
3212
|
-
output: outText,
|
|
3213
|
-
event_id: state.clientEventId,
|
|
3214
|
-
model: resolvedModel,
|
|
3215
|
-
usage,
|
|
3216
|
-
latency,
|
|
3217
|
-
metadata: Object.keys(metadata).length > 0 ? metadata : void 0,
|
|
3218
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
3219
|
-
}, this.requestOptions);
|
|
3220
|
-
logCallbackDebug("handleLLMEnd logged", {
|
|
3221
|
-
runId: id,
|
|
3222
|
-
clientEventId: state.clientEventId ?? null
|
|
3223
|
-
});
|
|
3224
|
-
}
|
|
3225
|
-
async handleLLMError(err, runId) {
|
|
3226
|
-
const id = String(runId ?? "");
|
|
3227
|
-
logCallbackDebug("handleLLMError", { runId: id });
|
|
3228
|
-
const state = this.runs.get(id);
|
|
3229
|
-
if (state) this.runs.delete(id);
|
|
3230
|
-
const message = err && typeof err === "object" && "message" in err ? String(err.message) : String(err ?? "");
|
|
3231
|
-
const metadata = {
|
|
3232
|
-
error_message: message
|
|
3233
|
-
};
|
|
3234
|
-
if (state?.clientEventId) {
|
|
3235
|
-
metadata.client_event_id = state.clientEventId;
|
|
3236
|
-
}
|
|
3237
|
-
if (state?.guardEventId) {
|
|
3238
|
-
metadata.guard_event_id = state.guardEventId;
|
|
3239
|
-
}
|
|
3240
|
-
await this.agent.log({
|
|
3241
|
-
system_id: this.systemId,
|
|
3242
|
-
input: state?.input ?? "",
|
|
3243
|
-
output: "",
|
|
3244
|
-
event_id: state?.clientEventId,
|
|
3245
|
-
model: state?.model ?? "unknown",
|
|
3246
|
-
event_type: "error",
|
|
3247
|
-
severity: "error",
|
|
3248
|
-
metadata,
|
|
3249
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
3250
|
-
}, this.requestOptions);
|
|
3251
|
-
}
|
|
3252
|
-
};
|
|
3253
2864
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3254
2865
|
0 && (module.exports = {
|
|
3255
2866
|
AgentID,
|
|
3256
|
-
AgentIDCallbackHandler,
|
|
3257
2867
|
InjectionScanner,
|
|
3258
2868
|
OpenAIAdapter,
|
|
3259
2869
|
PIIManager,
|
package/dist/index.mjs
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AgentID,
|
|
3
|
-
AgentIDCallbackHandler,
|
|
4
3
|
InjectionScanner,
|
|
5
4
|
OpenAIAdapter,
|
|
6
5
|
PIIManager,
|
|
7
6
|
SecurityBlockError,
|
|
8
7
|
getInjectionScanner,
|
|
9
8
|
scanWithRegex
|
|
10
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-FVTL572H.mjs";
|
|
11
10
|
export {
|
|
12
11
|
AgentID,
|
|
13
|
-
AgentIDCallbackHandler,
|
|
14
12
|
InjectionScanner,
|
|
15
13
|
OpenAIAdapter,
|
|
16
14
|
PIIManager,
|
package/dist/langchain.d.mts
CHANGED
|
@@ -1,2 +1,30 @@
|
|
|
1
|
-
import '@langchain/core/callbacks/base';
|
|
2
|
-
|
|
1
|
+
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
|
|
2
|
+
import { A as AgentID } from './agentid-BmsXTOCc.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* LangChainJS callback handler (dependency-free shape).
|
|
6
|
+
*
|
|
7
|
+
* Usage (LangChain):
|
|
8
|
+
* callbacks: [new AgentIDCallbackHandler(agent, { system_id: "..." })]
|
|
9
|
+
*/
|
|
10
|
+
declare class AgentIDCallbackHandler extends BaseCallbackHandler {
|
|
11
|
+
name: string;
|
|
12
|
+
private agent;
|
|
13
|
+
private systemId;
|
|
14
|
+
private apiKeyOverride?;
|
|
15
|
+
private runs;
|
|
16
|
+
constructor(agent: AgentID, options: {
|
|
17
|
+
system_id: string;
|
|
18
|
+
apiKey?: string;
|
|
19
|
+
api_key?: string;
|
|
20
|
+
});
|
|
21
|
+
private get requestOptions();
|
|
22
|
+
private getLangchainCapabilities;
|
|
23
|
+
private preflight;
|
|
24
|
+
handleLLMStart(serialized: unknown, prompts: unknown, runId?: string, _parentRunId?: string, extraParams?: unknown): Promise<void>;
|
|
25
|
+
handleChatModelStart(serialized: unknown, messages: unknown, runId?: string, _parentRunId?: string, extraParams?: unknown): Promise<void>;
|
|
26
|
+
handleLLMEnd(output: unknown, runId?: string): Promise<void>;
|
|
27
|
+
handleLLMError(err: unknown, runId?: string): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { AgentIDCallbackHandler };
|
package/dist/langchain.d.ts
CHANGED
|
@@ -1,2 +1,30 @@
|
|
|
1
|
-
import '@langchain/core/callbacks/base';
|
|
2
|
-
|
|
1
|
+
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
|
|
2
|
+
import { A as AgentID } from './agentid-BmsXTOCc.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* LangChainJS callback handler (dependency-free shape).
|
|
6
|
+
*
|
|
7
|
+
* Usage (LangChain):
|
|
8
|
+
* callbacks: [new AgentIDCallbackHandler(agent, { system_id: "..." })]
|
|
9
|
+
*/
|
|
10
|
+
declare class AgentIDCallbackHandler extends BaseCallbackHandler {
|
|
11
|
+
name: string;
|
|
12
|
+
private agent;
|
|
13
|
+
private systemId;
|
|
14
|
+
private apiKeyOverride?;
|
|
15
|
+
private runs;
|
|
16
|
+
constructor(agent: AgentID, options: {
|
|
17
|
+
system_id: string;
|
|
18
|
+
apiKey?: string;
|
|
19
|
+
api_key?: string;
|
|
20
|
+
});
|
|
21
|
+
private get requestOptions();
|
|
22
|
+
private getLangchainCapabilities;
|
|
23
|
+
private preflight;
|
|
24
|
+
handleLLMStart(serialized: unknown, prompts: unknown, runId?: string, _parentRunId?: string, extraParams?: unknown): Promise<void>;
|
|
25
|
+
handleChatModelStart(serialized: unknown, messages: unknown, runId?: string, _parentRunId?: string, extraParams?: unknown): Promise<void>;
|
|
26
|
+
handleLLMEnd(output: unknown, runId?: string): Promise<void>;
|
|
27
|
+
handleLLMError(err: unknown, runId?: string): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { AgentIDCallbackHandler };
|
package/dist/langchain.js
CHANGED
|
@@ -27,7 +27,7 @@ var import_base = require("@langchain/core/callbacks/base");
|
|
|
27
27
|
|
|
28
28
|
// src/sdk-version.ts
|
|
29
29
|
var FALLBACK_SDK_VERSION = "js-0.0.0-dev";
|
|
30
|
-
var AGENTID_SDK_VERSION_HEADER = "js-0.1.
|
|
30
|
+
var AGENTID_SDK_VERSION_HEADER = "js-0.1.22".trim().length > 0 ? "js-0.1.22" : FALLBACK_SDK_VERSION;
|
|
31
31
|
|
|
32
32
|
// src/pii-national-identifiers.ts
|
|
33
33
|
var REGION_ANCHORS = {
|