agentid-sdk 0.1.4 → 0.1.6

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 CHANGED
@@ -42,10 +42,14 @@ const openai = new OpenAI();
42
42
  const agent = new AgentID({
43
43
  apiKey: "ag_prod_...",
44
44
  strictMode: false, // default (fail-open on timeout/unreachable AgentID API)
45
+ guardTimeoutMs: 6000, // optional: timeout for AgentID /guard call in ms
45
46
  // strictMode: true, // fail-closed for high-risk workloads
46
47
  });
47
48
 
48
- const proxiedOpenAI = agent.wrapOpenAI(openai, { system_id: "sys_..." });
49
+ const proxiedOpenAI = agent.wrapOpenAI(openai, {
50
+ system_id: "sys_...",
51
+ user_id: "system-auto-summary", // optional service/user identity
52
+ });
49
53
  ```
50
54
 
51
55
  ## 🔒 Klíčové vlastnosti
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-Cn7doPo2.mjs';
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-C6HJAK2b.mjs';
2
2
 
3
3
  type PIIMapping = Record<string, string>;
4
4
  declare class PIIManager {
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-Cn7doPo2.js';
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-C6HJAK2b.js';
2
2
 
3
3
  type PIIMapping = Record<string, string>;
4
4
  declare class PIIManager {
package/dist/index.js CHANGED
@@ -392,7 +392,7 @@ var CONFIG_TTL_MS = 5 * 60 * 1e3;
392
392
  var CONFIG_TIMEOUT_MS = 8e3;
393
393
  var CONFIG_RETRY_DELAY_MS = 1e3;
394
394
  var MAX_CAPABILITY_CACHE_ENTRIES = 500;
395
- var AGENTID_SDK_VERSION_HEADER = "js-1.0.4";
395
+ var AGENTID_SDK_VERSION_HEADER = "js-1.0.6";
396
396
  var CapabilityConfigFetchError = class extends Error {
397
397
  constructor(message, params) {
398
398
  super(message);
@@ -980,10 +980,71 @@ function getInjectionScanner() {
980
980
  }
981
981
 
982
982
  // src/agentid.ts
983
- var AGENTID_SDK_VERSION_HEADER2 = "js-1.0.4";
983
+ var AGENTID_SDK_VERSION_HEADER2 = "js-1.0.6";
984
+ var DEFAULT_GUARD_TIMEOUT_MS = 6e3;
985
+ var MIN_GUARD_TIMEOUT_MS = 1e3;
986
+ var MAX_GUARD_TIMEOUT_MS = 3e4;
984
987
  function normalizeBaseUrl3(baseUrl) {
985
988
  return baseUrl.replace(/\/+$/, "");
986
989
  }
990
+ function isAbortSignalLike(value) {
991
+ if (!value || typeof value !== "object") return false;
992
+ const candidate = value;
993
+ return typeof candidate.aborted === "boolean" && typeof candidate.addEventListener === "function";
994
+ }
995
+ function normalizeOpenAICreateArgs(rawArgs) {
996
+ if (!Array.isArray(rawArgs) || rawArgs.length === 0) {
997
+ return rawArgs;
998
+ }
999
+ const nextArgs = [...rawArgs];
1000
+ const firstArg = nextArgs[0];
1001
+ if (!firstArg || typeof firstArg !== "object" || Array.isArray(firstArg)) {
1002
+ return nextArgs;
1003
+ }
1004
+ const requestBody = { ...firstArg };
1005
+ const hasSignalInBody = Object.prototype.hasOwnProperty.call(requestBody, "signal");
1006
+ if (!hasSignalInBody) {
1007
+ nextArgs[0] = requestBody;
1008
+ return nextArgs;
1009
+ }
1010
+ const bodySignal = requestBody.signal;
1011
+ delete requestBody.signal;
1012
+ nextArgs[0] = requestBody;
1013
+ if (!isAbortSignalLike(bodySignal)) {
1014
+ return nextArgs;
1015
+ }
1016
+ const secondArg = nextArgs[1];
1017
+ if (typeof secondArg === "undefined") {
1018
+ nextArgs[1] = { signal: bodySignal };
1019
+ return nextArgs;
1020
+ }
1021
+ if (!secondArg || typeof secondArg !== "object" || Array.isArray(secondArg)) {
1022
+ return nextArgs;
1023
+ }
1024
+ const requestOptions = { ...secondArg };
1025
+ if (!Object.prototype.hasOwnProperty.call(requestOptions, "signal")) {
1026
+ requestOptions.signal = bodySignal;
1027
+ }
1028
+ nextArgs[1] = requestOptions;
1029
+ return nextArgs;
1030
+ }
1031
+ function normalizeGuardTimeoutMs(value) {
1032
+ if (!Number.isFinite(value)) {
1033
+ return DEFAULT_GUARD_TIMEOUT_MS;
1034
+ }
1035
+ const rounded = Math.trunc(value);
1036
+ if (rounded < MIN_GUARD_TIMEOUT_MS) {
1037
+ return MIN_GUARD_TIMEOUT_MS;
1038
+ }
1039
+ if (rounded > MAX_GUARD_TIMEOUT_MS) {
1040
+ return MAX_GUARD_TIMEOUT_MS;
1041
+ }
1042
+ return rounded;
1043
+ }
1044
+ function isInfrastructureGuardReason(reason) {
1045
+ if (!reason) return false;
1046
+ 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";
1047
+ }
987
1048
  async function safeReadJson2(response) {
988
1049
  try {
989
1050
  return await response.json();
@@ -1001,6 +1062,7 @@ var AgentID = class {
1001
1062
  this.aiScanEnabled = config.aiScanEnabled !== false;
1002
1063
  this.storePii = config.storePii === true;
1003
1064
  this.strictMode = config.strictMode === true;
1065
+ this.guardTimeoutMs = normalizeGuardTimeoutMs(config.guardTimeoutMs);
1004
1066
  this.pii = new PIIManager();
1005
1067
  this.localEnforcer = new LocalSecurityEnforcer(this.pii);
1006
1068
  void this.getCapabilityConfig();
@@ -1157,7 +1219,7 @@ var AgentID = class {
1157
1219
  client_capabilities: params.client_capabilities ?? this.buildClientCapabilities()
1158
1220
  };
1159
1221
  const controller = new AbortController();
1160
- const timeoutId = setTimeout(() => controller.abort(), 2e3);
1222
+ const timeoutId = setTimeout(() => controller.abort(), this.guardTimeoutMs);
1161
1223
  try {
1162
1224
  const res = await fetch(`${this.baseUrl}/guard`, {
1163
1225
  method: "POST",
@@ -1171,7 +1233,14 @@ var AgentID = class {
1171
1233
  });
1172
1234
  const responseBody = await safeReadJson2(res);
1173
1235
  if (responseBody && typeof responseBody.allowed === "boolean") {
1174
- return responseBody;
1236
+ const verdict = responseBody;
1237
+ if (!this.strictMode && verdict.allowed === false && (isInfrastructureGuardReason(verdict.reason) || res.status >= 500)) {
1238
+ console.warn(
1239
+ `[AgentID] Guard API infrastructure fallback in fail-open mode (${verdict.reason ?? `http_${res.status}`}).`
1240
+ );
1241
+ return { allowed: true, reason: "system_failure_fail_open" };
1242
+ }
1243
+ return verdict;
1175
1244
  }
1176
1245
  if (!res.ok) {
1177
1246
  throw new Error(`API Error ${res.status}`);
@@ -1272,7 +1341,8 @@ var AgentID = class {
1272
1341
  const originalCreate = Reflect.get(compTarget, compProp, compReceiver);
1273
1342
  if (typeof originalCreate !== "function") return originalCreate;
1274
1343
  return async (...args) => {
1275
- const req = args?.[0] ?? {};
1344
+ const normalizedCreateArgs = normalizeOpenAICreateArgs(args);
1345
+ const req = normalizedCreateArgs?.[0] ?? {};
1276
1346
  const requestLevelApiKey = options.resolveApiKey?.(req) ?? options.apiKey ?? options.api_key;
1277
1347
  const effectiveApiKey = this.resolveApiKey(requestLevelApiKey);
1278
1348
  const requestOptions = { apiKey: effectiveApiKey };
@@ -1281,7 +1351,7 @@ var AgentID = class {
1281
1351
  const userText = adapter.extractInput(req);
1282
1352
  let maskedText = userText;
1283
1353
  let maskedReq = req;
1284
- let createArgs = args;
1354
+ let createArgs = normalizedCreateArgs;
1285
1355
  let mapping = {};
1286
1356
  let shouldDeanonymize = false;
1287
1357
  if (userText) {
@@ -1299,7 +1369,9 @@ var AgentID = class {
1299
1369
  req,
1300
1370
  maskedText
1301
1371
  );
1302
- createArgs = [maskedReq, ...args.slice(1)];
1372
+ const nextCreateArgs = [...normalizedCreateArgs];
1373
+ nextCreateArgs[0] = maskedReq;
1374
+ createArgs = nextCreateArgs;
1303
1375
  }
1304
1376
  if (!capabilityConfig.block_pii_leakage && this.piiMasking) {
1305
1377
  if (stream) {
@@ -1313,7 +1385,9 @@ var AgentID = class {
1313
1385
  req,
1314
1386
  maskedText
1315
1387
  );
1316
- createArgs = [maskedReq, ...args.slice(1)];
1388
+ const nextCreateArgs = [...normalizedCreateArgs];
1389
+ nextCreateArgs[0] = maskedReq;
1390
+ createArgs = nextCreateArgs;
1317
1391
  }
1318
1392
  }
1319
1393
  }
@@ -1325,6 +1399,7 @@ var AgentID = class {
1325
1399
  const verdict = await this.guard({
1326
1400
  input: maskedText,
1327
1401
  system_id: systemId,
1402
+ user_id: options.user_id,
1328
1403
  client_capabilities: this.buildClientCapabilities("openai", false)
1329
1404
  }, requestOptions);
1330
1405
  if (!verdict.allowed) {
@@ -1347,6 +1422,7 @@ var AgentID = class {
1347
1422
  const usage = adapter.getTokenUsage(res);
1348
1423
  this.log({
1349
1424
  system_id: systemId,
1425
+ user_id: options.user_id,
1350
1426
  input: maskedText,
1351
1427
  output,
1352
1428
  model,
package/dist/index.mjs CHANGED
@@ -354,7 +354,7 @@ var CONFIG_TTL_MS = 5 * 60 * 1e3;
354
354
  var CONFIG_TIMEOUT_MS = 8e3;
355
355
  var CONFIG_RETRY_DELAY_MS = 1e3;
356
356
  var MAX_CAPABILITY_CACHE_ENTRIES = 500;
357
- var AGENTID_SDK_VERSION_HEADER = "js-1.0.4";
357
+ var AGENTID_SDK_VERSION_HEADER = "js-1.0.6";
358
358
  var CapabilityConfigFetchError = class extends Error {
359
359
  constructor(message, params) {
360
360
  super(message);
@@ -942,10 +942,71 @@ function getInjectionScanner() {
942
942
  }
943
943
 
944
944
  // src/agentid.ts
945
- var AGENTID_SDK_VERSION_HEADER2 = "js-1.0.4";
945
+ var AGENTID_SDK_VERSION_HEADER2 = "js-1.0.6";
946
+ var DEFAULT_GUARD_TIMEOUT_MS = 6e3;
947
+ var MIN_GUARD_TIMEOUT_MS = 1e3;
948
+ var MAX_GUARD_TIMEOUT_MS = 3e4;
946
949
  function normalizeBaseUrl3(baseUrl) {
947
950
  return baseUrl.replace(/\/+$/, "");
948
951
  }
952
+ function isAbortSignalLike(value) {
953
+ if (!value || typeof value !== "object") return false;
954
+ const candidate = value;
955
+ return typeof candidate.aborted === "boolean" && typeof candidate.addEventListener === "function";
956
+ }
957
+ function normalizeOpenAICreateArgs(rawArgs) {
958
+ if (!Array.isArray(rawArgs) || rawArgs.length === 0) {
959
+ return rawArgs;
960
+ }
961
+ const nextArgs = [...rawArgs];
962
+ const firstArg = nextArgs[0];
963
+ if (!firstArg || typeof firstArg !== "object" || Array.isArray(firstArg)) {
964
+ return nextArgs;
965
+ }
966
+ const requestBody = { ...firstArg };
967
+ const hasSignalInBody = Object.prototype.hasOwnProperty.call(requestBody, "signal");
968
+ if (!hasSignalInBody) {
969
+ nextArgs[0] = requestBody;
970
+ return nextArgs;
971
+ }
972
+ const bodySignal = requestBody.signal;
973
+ delete requestBody.signal;
974
+ nextArgs[0] = requestBody;
975
+ if (!isAbortSignalLike(bodySignal)) {
976
+ return nextArgs;
977
+ }
978
+ const secondArg = nextArgs[1];
979
+ if (typeof secondArg === "undefined") {
980
+ nextArgs[1] = { signal: bodySignal };
981
+ return nextArgs;
982
+ }
983
+ if (!secondArg || typeof secondArg !== "object" || Array.isArray(secondArg)) {
984
+ return nextArgs;
985
+ }
986
+ const requestOptions = { ...secondArg };
987
+ if (!Object.prototype.hasOwnProperty.call(requestOptions, "signal")) {
988
+ requestOptions.signal = bodySignal;
989
+ }
990
+ nextArgs[1] = requestOptions;
991
+ return nextArgs;
992
+ }
993
+ function normalizeGuardTimeoutMs(value) {
994
+ if (!Number.isFinite(value)) {
995
+ return DEFAULT_GUARD_TIMEOUT_MS;
996
+ }
997
+ const rounded = Math.trunc(value);
998
+ if (rounded < MIN_GUARD_TIMEOUT_MS) {
999
+ return MIN_GUARD_TIMEOUT_MS;
1000
+ }
1001
+ if (rounded > MAX_GUARD_TIMEOUT_MS) {
1002
+ return MAX_GUARD_TIMEOUT_MS;
1003
+ }
1004
+ return rounded;
1005
+ }
1006
+ function isInfrastructureGuardReason(reason) {
1007
+ if (!reason) return false;
1008
+ 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";
1009
+ }
949
1010
  async function safeReadJson2(response) {
950
1011
  try {
951
1012
  return await response.json();
@@ -963,6 +1024,7 @@ var AgentID = class {
963
1024
  this.aiScanEnabled = config.aiScanEnabled !== false;
964
1025
  this.storePii = config.storePii === true;
965
1026
  this.strictMode = config.strictMode === true;
1027
+ this.guardTimeoutMs = normalizeGuardTimeoutMs(config.guardTimeoutMs);
966
1028
  this.pii = new PIIManager();
967
1029
  this.localEnforcer = new LocalSecurityEnforcer(this.pii);
968
1030
  void this.getCapabilityConfig();
@@ -1119,7 +1181,7 @@ var AgentID = class {
1119
1181
  client_capabilities: params.client_capabilities ?? this.buildClientCapabilities()
1120
1182
  };
1121
1183
  const controller = new AbortController();
1122
- const timeoutId = setTimeout(() => controller.abort(), 2e3);
1184
+ const timeoutId = setTimeout(() => controller.abort(), this.guardTimeoutMs);
1123
1185
  try {
1124
1186
  const res = await fetch(`${this.baseUrl}/guard`, {
1125
1187
  method: "POST",
@@ -1133,7 +1195,14 @@ var AgentID = class {
1133
1195
  });
1134
1196
  const responseBody = await safeReadJson2(res);
1135
1197
  if (responseBody && typeof responseBody.allowed === "boolean") {
1136
- return responseBody;
1198
+ const verdict = responseBody;
1199
+ if (!this.strictMode && verdict.allowed === false && (isInfrastructureGuardReason(verdict.reason) || res.status >= 500)) {
1200
+ console.warn(
1201
+ `[AgentID] Guard API infrastructure fallback in fail-open mode (${verdict.reason ?? `http_${res.status}`}).`
1202
+ );
1203
+ return { allowed: true, reason: "system_failure_fail_open" };
1204
+ }
1205
+ return verdict;
1137
1206
  }
1138
1207
  if (!res.ok) {
1139
1208
  throw new Error(`API Error ${res.status}`);
@@ -1234,7 +1303,8 @@ var AgentID = class {
1234
1303
  const originalCreate = Reflect.get(compTarget, compProp, compReceiver);
1235
1304
  if (typeof originalCreate !== "function") return originalCreate;
1236
1305
  return async (...args) => {
1237
- const req = args?.[0] ?? {};
1306
+ const normalizedCreateArgs = normalizeOpenAICreateArgs(args);
1307
+ const req = normalizedCreateArgs?.[0] ?? {};
1238
1308
  const requestLevelApiKey = options.resolveApiKey?.(req) ?? options.apiKey ?? options.api_key;
1239
1309
  const effectiveApiKey = this.resolveApiKey(requestLevelApiKey);
1240
1310
  const requestOptions = { apiKey: effectiveApiKey };
@@ -1243,7 +1313,7 @@ var AgentID = class {
1243
1313
  const userText = adapter.extractInput(req);
1244
1314
  let maskedText = userText;
1245
1315
  let maskedReq = req;
1246
- let createArgs = args;
1316
+ let createArgs = normalizedCreateArgs;
1247
1317
  let mapping = {};
1248
1318
  let shouldDeanonymize = false;
1249
1319
  if (userText) {
@@ -1261,7 +1331,9 @@ var AgentID = class {
1261
1331
  req,
1262
1332
  maskedText
1263
1333
  );
1264
- createArgs = [maskedReq, ...args.slice(1)];
1334
+ const nextCreateArgs = [...normalizedCreateArgs];
1335
+ nextCreateArgs[0] = maskedReq;
1336
+ createArgs = nextCreateArgs;
1265
1337
  }
1266
1338
  if (!capabilityConfig.block_pii_leakage && this.piiMasking) {
1267
1339
  if (stream) {
@@ -1275,7 +1347,9 @@ var AgentID = class {
1275
1347
  req,
1276
1348
  maskedText
1277
1349
  );
1278
- createArgs = [maskedReq, ...args.slice(1)];
1350
+ const nextCreateArgs = [...normalizedCreateArgs];
1351
+ nextCreateArgs[0] = maskedReq;
1352
+ createArgs = nextCreateArgs;
1279
1353
  }
1280
1354
  }
1281
1355
  }
@@ -1287,6 +1361,7 @@ var AgentID = class {
1287
1361
  const verdict = await this.guard({
1288
1362
  input: maskedText,
1289
1363
  system_id: systemId,
1364
+ user_id: options.user_id,
1290
1365
  client_capabilities: this.buildClientCapabilities("openai", false)
1291
1366
  }, requestOptions);
1292
1367
  if (!verdict.allowed) {
@@ -1309,6 +1384,7 @@ var AgentID = class {
1309
1384
  const usage = adapter.getTokenUsage(res);
1310
1385
  this.log({
1311
1386
  system_id: systemId,
1387
+ user_id: options.user_id,
1312
1388
  input: maskedText,
1313
1389
  output,
1314
1390
  model,
@@ -55,6 +55,7 @@ type AgentIDConfig = {
55
55
  aiScanEnabled?: boolean;
56
56
  storePii?: boolean;
57
57
  strictMode?: boolean;
58
+ guardTimeoutMs?: number;
58
59
  };
59
60
 
60
61
  type PreparedInput = {
@@ -69,6 +70,7 @@ declare class AgentID {
69
70
  private aiScanEnabled;
70
71
  private storePii;
71
72
  private strictMode;
73
+ private guardTimeoutMs;
72
74
  private pii;
73
75
  private localEnforcer;
74
76
  private injectionScanner;
@@ -113,6 +115,7 @@ declare class AgentID {
113
115
  */
114
116
  wrapOpenAI<T>(openai: T, options: {
115
117
  system_id: string;
118
+ user_id?: string;
116
119
  apiKey?: string;
117
120
  api_key?: string;
118
121
  resolveApiKey?: (request: Record<string, unknown>) => string | undefined;
@@ -55,6 +55,7 @@ type AgentIDConfig = {
55
55
  aiScanEnabled?: boolean;
56
56
  storePii?: boolean;
57
57
  strictMode?: boolean;
58
+ guardTimeoutMs?: number;
58
59
  };
59
60
 
60
61
  type PreparedInput = {
@@ -69,6 +70,7 @@ declare class AgentID {
69
70
  private aiScanEnabled;
70
71
  private storePii;
71
72
  private strictMode;
73
+ private guardTimeoutMs;
72
74
  private pii;
73
75
  private localEnforcer;
74
76
  private injectionScanner;
@@ -113,6 +115,7 @@ declare class AgentID {
113
115
  */
114
116
  wrapOpenAI<T>(openai: T, options: {
115
117
  system_id: string;
118
+ user_id?: string;
116
119
  apiKey?: string;
117
120
  api_key?: string;
118
121
  resolveApiKey?: (request: Record<string, unknown>) => string | undefined;
@@ -1 +1 @@
1
- export { a as AgentIDCallbackHandler } from './langchain-Cn7doPo2.mjs';
1
+ export { a as AgentIDCallbackHandler } from './langchain-C6HJAK2b.mjs';
@@ -1 +1 @@
1
- export { a as AgentIDCallbackHandler } from './langchain-Cn7doPo2.js';
1
+ export { a as AgentIDCallbackHandler } from './langchain-C6HJAK2b.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentid-sdk",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "AgentID JavaScript/TypeScript SDK for guard, ingest, tracing, and analytics.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://agentid.ai",