agentid-sdk 0.1.18 → 0.1.20

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/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- export { A as AgentID, a as AgentIDCallbackHandler, G as GuardParams, b as GuardResponse, L as LogParams, P as PreparedInput, R as RequestOptions } from './langchain-BykeB2WB.mjs';
1
+ export { A as AgentID, a as AgentIDCallbackHandler, G as GuardParams, b as GuardResponse, L as LogParams, P as PreparedInput, R as RequestOptions, S as SecurityBlockError } from './langchain-BipmisU1.mjs';
2
2
  import '@langchain/core/callbacks/base';
3
3
 
4
4
  type PIIMapping = Record<string, string>;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { A as AgentID, a as AgentIDCallbackHandler, G as GuardParams, b as GuardResponse, L as LogParams, P as PreparedInput, R as RequestOptions } from './langchain-BykeB2WB.js';
1
+ export { A as AgentID, a as AgentIDCallbackHandler, G as GuardParams, b as GuardResponse, L as LogParams, P as PreparedInput, R as RequestOptions, S as SecurityBlockError } from './langchain-BipmisU1.js';
2
2
  import '@langchain/core/callbacks/base';
3
3
 
4
4
  type PIIMapping = Record<string, string>;
package/dist/index.js CHANGED
@@ -35,6 +35,7 @@ __export(index_exports, {
35
35
  InjectionScanner: () => InjectionScanner,
36
36
  OpenAIAdapter: () => OpenAIAdapter,
37
37
  PIIManager: () => PIIManager,
38
+ SecurityBlockError: () => SecurityBlockError,
38
39
  getInjectionScanner: () => getInjectionScanner,
39
40
  scanWithRegex: () => scanWithRegex
40
41
  });
@@ -84,7 +85,7 @@ var OpenAIAdapter = class {
84
85
 
85
86
  // src/sdk-version.ts
86
87
  var FALLBACK_SDK_VERSION = "js-0.0.0-dev";
87
- var AGENTID_SDK_VERSION_HEADER = "js-0.1.18".trim().length > 0 ? "js-0.1.18" : FALLBACK_SDK_VERSION;
88
+ var AGENTID_SDK_VERSION_HEADER = "js-0.1.20".trim().length > 0 ? "js-0.1.20" : FALLBACK_SDK_VERSION;
88
89
 
89
90
  // src/pii-national-identifiers.ts
90
91
  var MAX_CANDIDATES_PER_RULE = 256;
@@ -1115,12 +1116,14 @@ var PIIManager = class {
1115
1116
  };
1116
1117
 
1117
1118
  // src/local-security-enforcer.ts
1118
- var DEFAULT_STRICT_CONFIG = {
1119
+ var DEFAULT_FAIL_OPEN_CONFIG = {
1119
1120
  shadow_mode: false,
1120
- block_pii_leakage: true,
1121
- block_db_access: true,
1122
- block_code_execution: true,
1123
- block_toxicity: true
1121
+ strict_security_mode: false,
1122
+ failure_mode: "fail_open",
1123
+ block_pii_leakage: false,
1124
+ block_db_access: false,
1125
+ block_code_execution: false,
1126
+ block_toxicity: false
1124
1127
  };
1125
1128
  var SQL_DATABASE_ACCESS_PATTERN = /\b(SELECT|INSERT|UPDATE|DELETE|DROP|UNION|ALTER)\b[\s\S]+?\b(FROM|INTO|TABLE|DATABASE|VIEW|INDEX)\b/i;
1126
1129
  var PYTHON_GENERAL_RCE_PATTERN = /(import\s+(os|sys|subprocess)|from\s+(os|sys|subprocess)\s+import|exec\s*\(|eval\s*\(|__import__)/i;
@@ -1243,13 +1246,28 @@ function readOptionalBooleanField(body, key, fallback) {
1243
1246
  }
1244
1247
  throw new Error(`Invalid config field: ${key}`);
1245
1248
  }
1249
+ function readOptionalFailureModeField(body, fallback) {
1250
+ const value = body.failure_mode;
1251
+ if (value === "fail_open" || value === "fail_close") {
1252
+ return value;
1253
+ }
1254
+ return fallback;
1255
+ }
1246
1256
  function normalizeCapabilityConfig(payload) {
1247
1257
  if (!payload || typeof payload !== "object") {
1248
1258
  throw new Error("Invalid config payload");
1249
1259
  }
1250
1260
  const body = payload;
1261
+ const strictSecurityMode = readOptionalBooleanField(body, "strict_security_mode", false);
1262
+ const failureMode = readOptionalFailureModeField(
1263
+ body,
1264
+ strictSecurityMode ? "fail_close" : "fail_open"
1265
+ );
1266
+ const effectiveStrictMode = strictSecurityMode || failureMode === "fail_close";
1251
1267
  return {
1252
1268
  shadow_mode: readOptionalBooleanField(body, "shadow_mode", false),
1269
+ strict_security_mode: effectiveStrictMode,
1270
+ failure_mode: effectiveStrictMode ? "fail_close" : "fail_open",
1253
1271
  block_pii_leakage: readBooleanField(body, "block_pii_leakage", "block_pii"),
1254
1272
  block_db_access: readBooleanField(body, "block_db_access", "block_db"),
1255
1273
  block_code_execution: readBooleanField(
@@ -1353,7 +1371,7 @@ async function fetchCapabilityConfigWithTimeout(params) {
1353
1371
  function getCachedCapabilityConfig(params) {
1354
1372
  const key = getCacheKey(params.apiKey, params.baseUrl);
1355
1373
  const entry = getGlobalCache().get(key);
1356
- return entry?.config ?? DEFAULT_STRICT_CONFIG;
1374
+ return entry?.config ?? DEFAULT_FAIL_OPEN_CONFIG;
1357
1375
  }
1358
1376
  async function ensureCapabilityConfig(params) {
1359
1377
  const ttlMs = params.ttlMs ?? CONFIG_TTL_MS;
@@ -1382,14 +1400,15 @@ async function ensureCapabilityConfig(params) {
1382
1400
  return resolved;
1383
1401
  }).catch((error) => {
1384
1402
  const message = error instanceof Error ? error.message : String(error);
1385
- console.warn("AgentID Config unreachable. Defaulting to STRICT MODE.", message);
1403
+ const fallbackConfig = existing?.config ?? DEFAULT_FAIL_OPEN_CONFIG;
1404
+ console.warn("AgentID Config unreachable. Defaulting to FAIL-OPEN MODE.", message);
1386
1405
  cache.set(key, {
1387
- config: DEFAULT_STRICT_CONFIG,
1406
+ config: fallbackConfig,
1388
1407
  expiresAt: Date.now() + ttlMs,
1389
1408
  promise: null
1390
1409
  });
1391
1410
  enforceCacheBound(cache);
1392
- return DEFAULT_STRICT_CONFIG;
1411
+ return fallbackConfig;
1393
1412
  }).finally(() => {
1394
1413
  const latest = cache.get(key);
1395
1414
  if (!latest) {
@@ -1405,7 +1424,7 @@ async function ensureCapabilityConfig(params) {
1405
1424
  }
1406
1425
  });
1407
1426
  cache.set(key, {
1408
- config: existing?.config ?? DEFAULT_STRICT_CONFIG,
1427
+ config: existing?.config ?? DEFAULT_FAIL_OPEN_CONFIG,
1409
1428
  expiresAt: existing?.expiresAt ?? 0,
1410
1429
  promise: pending
1411
1430
  });
@@ -1809,6 +1828,9 @@ function getInjectionScanner() {
1809
1828
  var DEFAULT_GUARD_TIMEOUT_MS = 1e4;
1810
1829
  var MIN_GUARD_TIMEOUT_MS = 500;
1811
1830
  var MAX_GUARD_TIMEOUT_MS = 15e3;
1831
+ var DEFAULT_INGEST_TIMEOUT_MS = 1e4;
1832
+ var MIN_INGEST_TIMEOUT_MS = 500;
1833
+ var MAX_INGEST_TIMEOUT_MS = 15e3;
1812
1834
  var GUARD_MAX_ATTEMPTS = 3;
1813
1835
  var GUARD_RETRY_DELAYS_MS = [250, 500];
1814
1836
  var INGEST_MAX_ATTEMPTS = 3;
@@ -1878,6 +1900,28 @@ function normalizeGuardTimeoutMs(value) {
1878
1900
  }
1879
1901
  return rounded;
1880
1902
  }
1903
+ function normalizeIngestTimeoutMs(value) {
1904
+ if (!Number.isFinite(value)) {
1905
+ return DEFAULT_INGEST_TIMEOUT_MS;
1906
+ }
1907
+ const rounded = Math.trunc(value);
1908
+ if (rounded < MIN_INGEST_TIMEOUT_MS) {
1909
+ return MIN_INGEST_TIMEOUT_MS;
1910
+ }
1911
+ if (rounded > MAX_INGEST_TIMEOUT_MS) {
1912
+ return MAX_INGEST_TIMEOUT_MS;
1913
+ }
1914
+ return rounded;
1915
+ }
1916
+ function resolveConfiguredApiKey(value) {
1917
+ const explicit = typeof value === "string" ? value.trim() : "";
1918
+ const fromEnv = globalThis.process?.env?.AGENTID_API_KEY ?? "";
1919
+ const resolved = explicit || fromEnv.trim();
1920
+ if (!resolved) {
1921
+ throw new Error("AgentID API key missing. Pass apiKey or set AGENTID_API_KEY.");
1922
+ }
1923
+ return resolved;
1924
+ }
1881
1925
  function isInfrastructureGuardReason(reason) {
1882
1926
  if (!reason) return false;
1883
1927
  return reason === "system_failure" || reason === "system_failure_db_unavailable" || reason === "logging_failed" || reason === "server_error" || reason === "guard_unreachable" || reason === "api_key_pepper_missing" || reason === "encryption_key_missing";
@@ -1994,11 +2038,18 @@ function createCompletionChunkCollector() {
1994
2038
  result
1995
2039
  };
1996
2040
  }
2041
+ var SecurityBlockError = class extends Error {
2042
+ constructor(reason = "guard_denied") {
2043
+ super(`AgentID: Security Blocked (${reason})`);
2044
+ this.name = "SecurityBlockError";
2045
+ this.reason = reason;
2046
+ }
2047
+ };
1997
2048
  var AgentID = class {
1998
- constructor(config) {
2049
+ constructor(config = {}) {
1999
2050
  this.injectionScanner = getInjectionScanner();
2000
2051
  this.recentGuardVerdicts = /* @__PURE__ */ new Map();
2001
- this.apiKey = config.apiKey.trim();
2052
+ this.apiKey = resolveConfiguredApiKey(config.apiKey);
2002
2053
  this.baseUrl = normalizeBaseUrl3(config.baseUrl ?? "https://app.getagentid.com/api/v1");
2003
2054
  this.piiMasking = Boolean(config.piiMasking);
2004
2055
  this.checkInjection = config.checkInjection !== false;
@@ -2006,6 +2057,7 @@ var AgentID = class {
2006
2057
  this.storePii = config.storePii === true;
2007
2058
  this.strictMode = config.strictMode === true;
2008
2059
  this.guardTimeoutMs = normalizeGuardTimeoutMs(config.guardTimeoutMs);
2060
+ this.ingestTimeoutMs = normalizeIngestTimeoutMs(config.ingestTimeoutMs);
2009
2061
  this.pii = new PIIManager();
2010
2062
  this.localEnforcer = new LocalSecurityEnforcer(this.pii);
2011
2063
  void this.getCapabilityConfig();
@@ -2089,6 +2141,13 @@ var AgentID = class {
2089
2141
  baseUrl: this.baseUrl
2090
2142
  });
2091
2143
  }
2144
+ async resolveEffectiveStrictMode(options) {
2145
+ if (this.strictMode) {
2146
+ return true;
2147
+ }
2148
+ const config = await this.getCapabilityConfig(false, options);
2149
+ return config.strict_security_mode || config.failure_mode === "fail_close";
2150
+ }
2092
2151
  async prepareInputForDispatch(params, options) {
2093
2152
  const effectiveApiKey = this.resolveApiKey(options?.apiKey);
2094
2153
  if (this.checkInjection && !params.skipInjectionScan && params.input) {
@@ -2227,6 +2286,9 @@ var AgentID = class {
2227
2286
  */
2228
2287
  async guard(params, options) {
2229
2288
  const effectiveApiKey = this.resolveApiKey(options?.apiKey);
2289
+ const effectiveStrictMode = await this.resolveEffectiveStrictMode({
2290
+ apiKey: effectiveApiKey
2291
+ });
2230
2292
  const payload = {
2231
2293
  ...params,
2232
2294
  client_capabilities: params.client_capabilities ?? this.buildClientCapabilities()
@@ -2265,7 +2327,7 @@ var AgentID = class {
2265
2327
  await waitForRetry(attempt);
2266
2328
  continue;
2267
2329
  }
2268
- if (this.strictMode) {
2330
+ if (effectiveStrictMode) {
2269
2331
  console.warn(
2270
2332
  `[AgentID] Guard API infrastructure failure in strict mode (${verdict.reason ?? `http_${res.status}`}). Blocking request.`
2271
2333
  );
@@ -2312,13 +2374,13 @@ var AgentID = class {
2312
2374
  guardParams: params,
2313
2375
  apiKey: effectiveApiKey
2314
2376
  });
2315
- if (this.strictMode) {
2377
+ if (effectiveStrictMode) {
2316
2378
  return { allowed: false, reason: "network_error_strict_mode" };
2317
2379
  }
2318
2380
  return { allowed: true, reason: "timeout_fallback" };
2319
2381
  }
2320
2382
  console.warn(
2321
- this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2383
+ effectiveStrictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2322
2384
  error
2323
2385
  );
2324
2386
  this.logGuardFallback({
@@ -2327,7 +2389,7 @@ var AgentID = class {
2327
2389
  guardParams: params,
2328
2390
  apiKey: effectiveApiKey
2329
2391
  });
2330
- if (this.strictMode) {
2392
+ if (effectiveStrictMode) {
2331
2393
  return { allowed: false, reason: "network_error_strict_mode" };
2332
2394
  }
2333
2395
  return { allowed: true, reason: "guard_unreachable" };
@@ -2336,22 +2398,22 @@ var AgentID = class {
2336
2398
  }
2337
2399
  }
2338
2400
  if (lastAbort) {
2339
- if (this.strictMode) {
2401
+ if (effectiveStrictMode) {
2340
2402
  return { allowed: false, reason: "network_error_strict_mode" };
2341
2403
  }
2342
2404
  return { allowed: true, reason: "timeout_fallback" };
2343
2405
  }
2344
2406
  if (typeof lastStatusCode === "number" && lastStatusCode >= 500) {
2345
- if (this.strictMode) {
2407
+ if (effectiveStrictMode) {
2346
2408
  return { allowed: false, reason: "server_error" };
2347
2409
  }
2348
2410
  return { allowed: true, reason: "system_failure_fail_open" };
2349
2411
  }
2350
2412
  console.warn(
2351
- this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2413
+ effectiveStrictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2352
2414
  lastError
2353
2415
  );
2354
- if (this.strictMode) {
2416
+ if (effectiveStrictMode) {
2355
2417
  return { allowed: false, reason: "network_error_strict_mode" };
2356
2418
  }
2357
2419
  return { allowed: true, reason: "guard_unreachable" };
@@ -2377,6 +2439,8 @@ var AgentID = class {
2377
2439
  client_capabilities: params.client_capabilities ?? this.buildClientCapabilities()
2378
2440
  };
2379
2441
  for (let attempt = 0; attempt < INGEST_MAX_ATTEMPTS; attempt += 1) {
2442
+ const controller = new AbortController();
2443
+ const timeoutId = setTimeout(() => controller.abort(), this.ingestTimeoutMs);
2380
2444
  try {
2381
2445
  const response = await fetch(`${this.baseUrl}/ingest`, {
2382
2446
  method: "POST",
@@ -2386,7 +2450,8 @@ var AgentID = class {
2386
2450
  "x-agentid-api-key": effectiveApiKey,
2387
2451
  "X-AgentID-SDK-Version": AGENTID_SDK_VERSION_HEADER
2388
2452
  },
2389
- body: JSON.stringify(payload)
2453
+ body: JSON.stringify(payload),
2454
+ signal: controller.signal
2390
2455
  });
2391
2456
  const responseBody = await safeReadJson2(response);
2392
2457
  if (response.ok) {
@@ -2402,12 +2467,17 @@ var AgentID = class {
2402
2467
  continue;
2403
2468
  }
2404
2469
  return { ok: false, status: response.status, reason };
2405
- } catch {
2470
+ } catch (error) {
2471
+ const isAbortError2 = Boolean(
2472
+ error && typeof error === "object" && error.name === "AbortError"
2473
+ );
2406
2474
  if (attempt < INGEST_MAX_ATTEMPTS - 1) {
2407
2475
  await waitForIngestRetry(attempt);
2408
2476
  continue;
2409
2477
  }
2410
- return { ok: false, status: null, reason: "network_error" };
2478
+ return { ok: false, status: null, reason: isAbortError2 ? "timeout" : "network_error" };
2479
+ } finally {
2480
+ clearTimeout(timeoutId);
2411
2481
  }
2412
2482
  }
2413
2483
  return { ok: false, status: null, reason: "unknown_ingest_failure" };
@@ -2624,9 +2694,7 @@ var AgentID = class {
2624
2694
  client_capabilities: this.buildClientCapabilities("openai", false)
2625
2695
  }, requestOptions);
2626
2696
  if (!verdict.allowed) {
2627
- throw new Error(
2628
- `AgentID: Security Blocked (${verdict.reason ?? "guard_denied"})`
2629
- );
2697
+ throw new SecurityBlockError(verdict.reason ?? "guard_denied");
2630
2698
  }
2631
2699
  const canonicalClientEventId = typeof verdict.client_event_id === "string" && isUuidLike(verdict.client_event_id) ? verdict.client_event_id : clientEventId;
2632
2700
  const guardEventId = typeof verdict.guard_event_id === "string" && verdict.guard_event_id.length > 0 ? verdict.guard_event_id : null;
@@ -3043,7 +3111,7 @@ var AgentIDCallbackHandler = class extends import_base.BaseCallbackHandler {
3043
3111
  client_capabilities: this.getLangchainCapabilities()
3044
3112
  }, this.requestOptions);
3045
3113
  if (!verdict.allowed) {
3046
- throw new Error(`AgentID: Security Blocked (${verdict.reason ?? "guard_denied"})`);
3114
+ throw new SecurityBlockError(verdict.reason ?? "guard_denied");
3047
3115
  }
3048
3116
  const canonicalClientEventId = isUuidLike2(verdict.client_event_id) ? verdict.client_event_id.trim() : requestedClientEventId;
3049
3117
  const guardEventId = typeof verdict.guard_event_id === "string" && verdict.guard_event_id.length > 0 ? verdict.guard_event_id : void 0;
@@ -3094,7 +3162,7 @@ var AgentIDCallbackHandler = class extends import_base.BaseCallbackHandler {
3094
3162
  client_capabilities: this.getLangchainCapabilities()
3095
3163
  }, this.requestOptions);
3096
3164
  if (!verdict.allowed) {
3097
- throw new Error(`AgentID: Security Blocked (${verdict.reason ?? "guard_denied"})`);
3165
+ throw new SecurityBlockError(verdict.reason ?? "guard_denied");
3098
3166
  }
3099
3167
  const canonicalClientEventId = isUuidLike2(verdict.client_event_id) ? verdict.client_event_id.trim() : requestedClientEventId;
3100
3168
  const guardEventId = typeof verdict.guard_event_id === "string" && verdict.guard_event_id.length > 0 ? verdict.guard_event_id : void 0;
@@ -3189,6 +3257,7 @@ var AgentIDCallbackHandler = class extends import_base.BaseCallbackHandler {
3189
3257
  InjectionScanner,
3190
3258
  OpenAIAdapter,
3191
3259
  PIIManager,
3260
+ SecurityBlockError,
3192
3261
  getInjectionScanner,
3193
3262
  scanWithRegex
3194
3263
  });