agentid-sdk 0.1.11 → 0.1.13

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-DVPOWfCC.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-CUyC7jmr.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-DVPOWfCC.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-CUyC7jmr.js';
2
2
 
3
3
  type PIIMapping = Record<string, string>;
4
4
  declare class PIIManager {
package/dist/index.js CHANGED
@@ -1807,6 +1807,8 @@ var AGENTID_SDK_VERSION_HEADER2 = "js-1.0.8";
1807
1807
  var DEFAULT_GUARD_TIMEOUT_MS = 1e4;
1808
1808
  var MIN_GUARD_TIMEOUT_MS = 500;
1809
1809
  var MAX_GUARD_TIMEOUT_MS = 15e3;
1810
+ var GUARD_MAX_ATTEMPTS = 3;
1811
+ var GUARD_RETRY_DELAYS_MS = [250, 500];
1810
1812
  function normalizeBaseUrl3(baseUrl) {
1811
1813
  return baseUrl.replace(/\/+$/, "");
1812
1814
  }
@@ -1874,6 +1876,31 @@ function isInfrastructureGuardReason(reason) {
1874
1876
  if (!reason) return false;
1875
1877
  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";
1876
1878
  }
1879
+ function isUuidLike(value) {
1880
+ if (!value) return false;
1881
+ return /^[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(
1882
+ value
1883
+ );
1884
+ }
1885
+ function createEventId(seed) {
1886
+ if (isUuidLike(seed)) return seed;
1887
+ if (typeof globalThis.crypto?.randomUUID === "function") {
1888
+ return globalThis.crypto.randomUUID();
1889
+ }
1890
+ return `evt_${Date.now()}_${Math.random().toString(36).slice(2)}`;
1891
+ }
1892
+ function createCorrelationId(seed) {
1893
+ if (isUuidLike(seed)) return seed;
1894
+ if (typeof globalThis.crypto?.randomUUID === "function") {
1895
+ return globalThis.crypto.randomUUID();
1896
+ }
1897
+ return `cid_${Date.now()}_${Math.random().toString(36).slice(2)}`;
1898
+ }
1899
+ async function waitForRetry(attemptIndex) {
1900
+ const delay = GUARD_RETRY_DELAYS_MS[attemptIndex];
1901
+ if (!delay) return;
1902
+ await new Promise((resolve) => setTimeout(resolve, delay));
1903
+ }
1877
1904
  async function safeReadJson2(response) {
1878
1905
  try {
1879
1906
  return await response.json();
@@ -1948,6 +1975,7 @@ function createCompletionChunkCollector() {
1948
1975
  var AgentID = class {
1949
1976
  constructor(config) {
1950
1977
  this.injectionScanner = getInjectionScanner();
1978
+ this.requestClientEventIds = /* @__PURE__ */ new WeakMap();
1951
1979
  this.apiKey = config.apiKey.trim();
1952
1980
  this.baseUrl = normalizeBaseUrl3(config.baseUrl ?? "https://app.getagentid.com/api/v1");
1953
1981
  this.piiMasking = Boolean(config.piiMasking);
@@ -1976,6 +2004,26 @@ var AgentID = class {
1976
2004
  }
1977
2005
  return this.apiKey;
1978
2006
  }
2007
+ resolveClientEventId(requestBody) {
2008
+ const directClientEventId = requestBody.client_event_id;
2009
+ if (typeof directClientEventId === "string" && isUuidLike(directClientEventId)) {
2010
+ return directClientEventId;
2011
+ }
2012
+ const metadata = requestBody.metadata;
2013
+ if (metadata && typeof metadata === "object" && !Array.isArray(metadata)) {
2014
+ const metadataClientEventId = metadata.client_event_id;
2015
+ if (typeof metadataClientEventId === "string" && isUuidLike(metadataClientEventId)) {
2016
+ return metadataClientEventId;
2017
+ }
2018
+ }
2019
+ const cached = this.requestClientEventIds.get(requestBody);
2020
+ if (cached) {
2021
+ return cached;
2022
+ }
2023
+ const generated = createEventId();
2024
+ this.requestClientEventIds.set(requestBody, generated);
2025
+ return generated;
2026
+ }
1979
2027
  async getCapabilityConfig(force = false, options) {
1980
2028
  const effectiveApiKey = this.resolveApiKey(options?.apiKey);
1981
2029
  return ensureCapabilityConfig({
@@ -2133,79 +2181,124 @@ var AgentID = class {
2133
2181
  ...params,
2134
2182
  client_capabilities: params.client_capabilities ?? this.buildClientCapabilities()
2135
2183
  };
2136
- const controller = new AbortController();
2137
- const timeoutId = setTimeout(() => controller.abort(), this.guardTimeoutMs);
2138
- try {
2139
- const res = await fetch(`${this.baseUrl}/guard`, {
2140
- method: "POST",
2141
- headers: {
2142
- "Content-Type": "application/json",
2143
- "x-agentid-api-key": effectiveApiKey,
2144
- "X-AgentID-SDK-Version": AGENTID_SDK_VERSION_HEADER2
2145
- },
2146
- body: JSON.stringify(payload),
2147
- signal: controller.signal
2148
- });
2149
- const responseBody = await safeReadJson2(res);
2150
- if (responseBody && typeof responseBody.allowed === "boolean") {
2151
- const verdict = responseBody;
2152
- if (verdict.allowed === false && (isInfrastructureGuardReason(verdict.reason) || res.status >= 500)) {
2153
- if (this.strictMode) {
2184
+ const correlationId = createCorrelationId(payload.client_event_id);
2185
+ let lastStatusCode = null;
2186
+ let lastAbort = false;
2187
+ let lastError = null;
2188
+ for (let attempt = 0; attempt < GUARD_MAX_ATTEMPTS; attempt += 1) {
2189
+ const controller = new AbortController();
2190
+ const timeoutId = setTimeout(() => controller.abort(), this.guardTimeoutMs);
2191
+ try {
2192
+ const res = await fetch(`${this.baseUrl}/guard`, {
2193
+ method: "POST",
2194
+ headers: {
2195
+ "Content-Type": "application/json",
2196
+ "x-agentid-api-key": effectiveApiKey,
2197
+ "X-AgentID-SDK-Version": AGENTID_SDK_VERSION_HEADER2,
2198
+ "x-correlation-id": correlationId
2199
+ },
2200
+ body: JSON.stringify(payload),
2201
+ signal: controller.signal
2202
+ });
2203
+ lastStatusCode = res.status;
2204
+ const responseBody = await safeReadJson2(res);
2205
+ if (responseBody && typeof responseBody.allowed === "boolean") {
2206
+ const verdict = responseBody;
2207
+ const infrastructureFailure = verdict.allowed === false && (isInfrastructureGuardReason(verdict.reason) || res.status >= 500);
2208
+ if (infrastructureFailure) {
2209
+ if (attempt < GUARD_MAX_ATTEMPTS - 1) {
2210
+ await waitForRetry(attempt);
2211
+ continue;
2212
+ }
2213
+ if (this.strictMode) {
2214
+ console.warn(
2215
+ `[AgentID] Guard API infrastructure failure in strict mode (${verdict.reason ?? `http_${res.status}`}). Blocking request.`
2216
+ );
2217
+ return { allowed: false, reason: verdict.reason ?? "network_error_strict_mode" };
2218
+ }
2154
2219
  console.warn(
2155
- `[AgentID] Guard API infrastructure failure in strict mode (${verdict.reason ?? `http_${res.status}`}). Blocking request.`
2220
+ `[AgentID] Guard API infrastructure fallback in fail-open mode (${verdict.reason ?? `http_${res.status}`}).`
2156
2221
  );
2157
- return { allowed: false, reason: verdict.reason ?? "network_error_strict_mode" };
2222
+ this.logGuardFallback({
2223
+ reason: verdict.reason ?? `http_${res.status}`,
2224
+ status: "upstream_error",
2225
+ guardParams: params,
2226
+ apiKey: effectiveApiKey
2227
+ });
2228
+ return { allowed: true, reason: "system_failure_fail_open" };
2158
2229
  }
2159
- console.warn(
2160
- `[AgentID] Guard API infrastructure fallback in fail-open mode (${verdict.reason ?? `http_${res.status}`}).`
2161
- );
2230
+ return verdict;
2231
+ }
2232
+ if (!res.ok) {
2233
+ if (res.status >= 500 && attempt < GUARD_MAX_ATTEMPTS - 1) {
2234
+ await waitForRetry(attempt);
2235
+ continue;
2236
+ }
2237
+ throw new Error(`API Error ${res.status}`);
2238
+ }
2239
+ throw new Error("Invalid guard response");
2240
+ } catch (error) {
2241
+ lastError = error;
2242
+ const isAbortError2 = Boolean(
2243
+ error && typeof error === "object" && error.name === "AbortError"
2244
+ );
2245
+ lastAbort = isAbortError2;
2246
+ if (attempt < GUARD_MAX_ATTEMPTS - 1) {
2247
+ await waitForRetry(attempt);
2248
+ continue;
2249
+ }
2250
+ if (isAbortError2) {
2251
+ const timeoutMessage = "AgentID API Warning: Connection timeout exceeded.";
2252
+ console.warn(timeoutMessage);
2162
2253
  this.logGuardFallback({
2163
- reason: verdict.reason ?? `http_${res.status}`,
2164
- status: "upstream_error",
2254
+ reason: "timeout_fallback",
2255
+ status: "latency_timeout",
2165
2256
  guardParams: params,
2166
2257
  apiKey: effectiveApiKey
2167
2258
  });
2168
- return { allowed: true, reason: "system_failure_fail_open" };
2259
+ if (this.strictMode) {
2260
+ return { allowed: false, reason: "network_error_strict_mode" };
2261
+ }
2262
+ return { allowed: true, reason: "timeout_fallback" };
2169
2263
  }
2170
- return verdict;
2171
- }
2172
- if (!res.ok) {
2173
- throw new Error(`API Error ${res.status}`);
2174
- }
2175
- throw new Error("Invalid guard response");
2176
- } catch (error) {
2177
- const isAbortError2 = error && typeof error === "object" && error.name === "AbortError";
2178
- if (isAbortError2) {
2179
- const timeoutMessage = "AgentID API Warning: Connection timeout exceeded.";
2180
- console.warn(timeoutMessage);
2264
+ console.warn(
2265
+ this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2266
+ error
2267
+ );
2181
2268
  this.logGuardFallback({
2182
- reason: "timeout_fallback",
2183
- status: "latency_timeout",
2269
+ reason: "guard_unreachable",
2270
+ status: "guard_unreachable",
2184
2271
  guardParams: params,
2185
2272
  apiKey: effectiveApiKey
2186
2273
  });
2187
2274
  if (this.strictMode) {
2188
2275
  return { allowed: false, reason: "network_error_strict_mode" };
2189
2276
  }
2190
- return { allowed: true, reason: "timeout_fallback" };
2277
+ return { allowed: true, reason: "guard_unreachable" };
2278
+ } finally {
2279
+ clearTimeout(timeoutId);
2191
2280
  }
2192
- console.warn(
2193
- this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2194
- error
2195
- );
2196
- this.logGuardFallback({
2197
- reason: "guard_unreachable",
2198
- status: "guard_unreachable",
2199
- guardParams: params,
2200
- apiKey: effectiveApiKey
2201
- });
2281
+ }
2282
+ if (lastAbort) {
2202
2283
  if (this.strictMode) {
2203
2284
  return { allowed: false, reason: "network_error_strict_mode" };
2204
2285
  }
2205
- return { allowed: true, reason: "guard_unreachable" };
2206
- } finally {
2207
- clearTimeout(timeoutId);
2286
+ return { allowed: true, reason: "timeout_fallback" };
2287
+ }
2288
+ if (typeof lastStatusCode === "number" && lastStatusCode >= 500) {
2289
+ if (this.strictMode) {
2290
+ return { allowed: false, reason: "server_error" };
2291
+ }
2292
+ return { allowed: true, reason: "system_failure_fail_open" };
2293
+ }
2294
+ console.warn(
2295
+ this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2296
+ lastError
2297
+ );
2298
+ if (this.strictMode) {
2299
+ return { allowed: false, reason: "network_error_strict_mode" };
2208
2300
  }
2301
+ return { allowed: true, reason: "guard_unreachable" };
2209
2302
  }
2210
2303
  extractStreamChunkText(chunk) {
2211
2304
  if (typeof chunk === "string") {
@@ -2310,7 +2403,7 @@ var AgentID = class {
2310
2403
  void (async () => {
2311
2404
  try {
2312
2405
  const effectiveApiKey = this.resolveApiKey(options?.apiKey);
2313
- const eventId = params.event_id ?? (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function" ? crypto.randomUUID() : `evt_${Date.now()}_${Math.random().toString(36).slice(2)}`);
2406
+ const eventId = params.event_id ?? createEventId();
2314
2407
  const timestamp = params.timestamp ?? (/* @__PURE__ */ new Date()).toISOString();
2315
2408
  void this.getCapabilityConfig(false, { apiKey: effectiveApiKey }).catch(() => void 0);
2316
2409
  const metadata = {
@@ -2436,7 +2529,7 @@ var AgentID = class {
2436
2529
  "AgentID: No user message found. Security guard requires string input."
2437
2530
  );
2438
2531
  }
2439
- const clientEventId = typeof crypto !== "undefined" && typeof crypto.randomUUID === "function" ? crypto.randomUUID() : `evt_${Date.now()}_${Math.random().toString(36).slice(2)}`;
2532
+ const clientEventId = this.resolveClientEventId(req);
2440
2533
  const verdict = await this.guard({
2441
2534
  input: maskedText,
2442
2535
  system_id: systemId,
package/dist/index.mjs CHANGED
@@ -1769,6 +1769,8 @@ var AGENTID_SDK_VERSION_HEADER2 = "js-1.0.8";
1769
1769
  var DEFAULT_GUARD_TIMEOUT_MS = 1e4;
1770
1770
  var MIN_GUARD_TIMEOUT_MS = 500;
1771
1771
  var MAX_GUARD_TIMEOUT_MS = 15e3;
1772
+ var GUARD_MAX_ATTEMPTS = 3;
1773
+ var GUARD_RETRY_DELAYS_MS = [250, 500];
1772
1774
  function normalizeBaseUrl3(baseUrl) {
1773
1775
  return baseUrl.replace(/\/+$/, "");
1774
1776
  }
@@ -1836,6 +1838,31 @@ function isInfrastructureGuardReason(reason) {
1836
1838
  if (!reason) return false;
1837
1839
  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";
1838
1840
  }
1841
+ function isUuidLike(value) {
1842
+ if (!value) return false;
1843
+ return /^[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(
1844
+ value
1845
+ );
1846
+ }
1847
+ function createEventId(seed) {
1848
+ if (isUuidLike(seed)) return seed;
1849
+ if (typeof globalThis.crypto?.randomUUID === "function") {
1850
+ return globalThis.crypto.randomUUID();
1851
+ }
1852
+ return `evt_${Date.now()}_${Math.random().toString(36).slice(2)}`;
1853
+ }
1854
+ function createCorrelationId(seed) {
1855
+ if (isUuidLike(seed)) return seed;
1856
+ if (typeof globalThis.crypto?.randomUUID === "function") {
1857
+ return globalThis.crypto.randomUUID();
1858
+ }
1859
+ return `cid_${Date.now()}_${Math.random().toString(36).slice(2)}`;
1860
+ }
1861
+ async function waitForRetry(attemptIndex) {
1862
+ const delay = GUARD_RETRY_DELAYS_MS[attemptIndex];
1863
+ if (!delay) return;
1864
+ await new Promise((resolve) => setTimeout(resolve, delay));
1865
+ }
1839
1866
  async function safeReadJson2(response) {
1840
1867
  try {
1841
1868
  return await response.json();
@@ -1910,6 +1937,7 @@ function createCompletionChunkCollector() {
1910
1937
  var AgentID = class {
1911
1938
  constructor(config) {
1912
1939
  this.injectionScanner = getInjectionScanner();
1940
+ this.requestClientEventIds = /* @__PURE__ */ new WeakMap();
1913
1941
  this.apiKey = config.apiKey.trim();
1914
1942
  this.baseUrl = normalizeBaseUrl3(config.baseUrl ?? "https://app.getagentid.com/api/v1");
1915
1943
  this.piiMasking = Boolean(config.piiMasking);
@@ -1938,6 +1966,26 @@ var AgentID = class {
1938
1966
  }
1939
1967
  return this.apiKey;
1940
1968
  }
1969
+ resolveClientEventId(requestBody) {
1970
+ const directClientEventId = requestBody.client_event_id;
1971
+ if (typeof directClientEventId === "string" && isUuidLike(directClientEventId)) {
1972
+ return directClientEventId;
1973
+ }
1974
+ const metadata = requestBody.metadata;
1975
+ if (metadata && typeof metadata === "object" && !Array.isArray(metadata)) {
1976
+ const metadataClientEventId = metadata.client_event_id;
1977
+ if (typeof metadataClientEventId === "string" && isUuidLike(metadataClientEventId)) {
1978
+ return metadataClientEventId;
1979
+ }
1980
+ }
1981
+ const cached = this.requestClientEventIds.get(requestBody);
1982
+ if (cached) {
1983
+ return cached;
1984
+ }
1985
+ const generated = createEventId();
1986
+ this.requestClientEventIds.set(requestBody, generated);
1987
+ return generated;
1988
+ }
1941
1989
  async getCapabilityConfig(force = false, options) {
1942
1990
  const effectiveApiKey = this.resolveApiKey(options?.apiKey);
1943
1991
  return ensureCapabilityConfig({
@@ -2095,79 +2143,124 @@ var AgentID = class {
2095
2143
  ...params,
2096
2144
  client_capabilities: params.client_capabilities ?? this.buildClientCapabilities()
2097
2145
  };
2098
- const controller = new AbortController();
2099
- const timeoutId = setTimeout(() => controller.abort(), this.guardTimeoutMs);
2100
- try {
2101
- const res = await fetch(`${this.baseUrl}/guard`, {
2102
- method: "POST",
2103
- headers: {
2104
- "Content-Type": "application/json",
2105
- "x-agentid-api-key": effectiveApiKey,
2106
- "X-AgentID-SDK-Version": AGENTID_SDK_VERSION_HEADER2
2107
- },
2108
- body: JSON.stringify(payload),
2109
- signal: controller.signal
2110
- });
2111
- const responseBody = await safeReadJson2(res);
2112
- if (responseBody && typeof responseBody.allowed === "boolean") {
2113
- const verdict = responseBody;
2114
- if (verdict.allowed === false && (isInfrastructureGuardReason(verdict.reason) || res.status >= 500)) {
2115
- if (this.strictMode) {
2146
+ const correlationId = createCorrelationId(payload.client_event_id);
2147
+ let lastStatusCode = null;
2148
+ let lastAbort = false;
2149
+ let lastError = null;
2150
+ for (let attempt = 0; attempt < GUARD_MAX_ATTEMPTS; attempt += 1) {
2151
+ const controller = new AbortController();
2152
+ const timeoutId = setTimeout(() => controller.abort(), this.guardTimeoutMs);
2153
+ try {
2154
+ const res = await fetch(`${this.baseUrl}/guard`, {
2155
+ method: "POST",
2156
+ headers: {
2157
+ "Content-Type": "application/json",
2158
+ "x-agentid-api-key": effectiveApiKey,
2159
+ "X-AgentID-SDK-Version": AGENTID_SDK_VERSION_HEADER2,
2160
+ "x-correlation-id": correlationId
2161
+ },
2162
+ body: JSON.stringify(payload),
2163
+ signal: controller.signal
2164
+ });
2165
+ lastStatusCode = res.status;
2166
+ const responseBody = await safeReadJson2(res);
2167
+ if (responseBody && typeof responseBody.allowed === "boolean") {
2168
+ const verdict = responseBody;
2169
+ const infrastructureFailure = verdict.allowed === false && (isInfrastructureGuardReason(verdict.reason) || res.status >= 500);
2170
+ if (infrastructureFailure) {
2171
+ if (attempt < GUARD_MAX_ATTEMPTS - 1) {
2172
+ await waitForRetry(attempt);
2173
+ continue;
2174
+ }
2175
+ if (this.strictMode) {
2176
+ console.warn(
2177
+ `[AgentID] Guard API infrastructure failure in strict mode (${verdict.reason ?? `http_${res.status}`}). Blocking request.`
2178
+ );
2179
+ return { allowed: false, reason: verdict.reason ?? "network_error_strict_mode" };
2180
+ }
2116
2181
  console.warn(
2117
- `[AgentID] Guard API infrastructure failure in strict mode (${verdict.reason ?? `http_${res.status}`}). Blocking request.`
2182
+ `[AgentID] Guard API infrastructure fallback in fail-open mode (${verdict.reason ?? `http_${res.status}`}).`
2118
2183
  );
2119
- return { allowed: false, reason: verdict.reason ?? "network_error_strict_mode" };
2184
+ this.logGuardFallback({
2185
+ reason: verdict.reason ?? `http_${res.status}`,
2186
+ status: "upstream_error",
2187
+ guardParams: params,
2188
+ apiKey: effectiveApiKey
2189
+ });
2190
+ return { allowed: true, reason: "system_failure_fail_open" };
2191
+ }
2192
+ return verdict;
2193
+ }
2194
+ if (!res.ok) {
2195
+ if (res.status >= 500 && attempt < GUARD_MAX_ATTEMPTS - 1) {
2196
+ await waitForRetry(attempt);
2197
+ continue;
2120
2198
  }
2121
- console.warn(
2122
- `[AgentID] Guard API infrastructure fallback in fail-open mode (${verdict.reason ?? `http_${res.status}`}).`
2123
- );
2199
+ throw new Error(`API Error ${res.status}`);
2200
+ }
2201
+ throw new Error("Invalid guard response");
2202
+ } catch (error) {
2203
+ lastError = error;
2204
+ const isAbortError2 = Boolean(
2205
+ error && typeof error === "object" && error.name === "AbortError"
2206
+ );
2207
+ lastAbort = isAbortError2;
2208
+ if (attempt < GUARD_MAX_ATTEMPTS - 1) {
2209
+ await waitForRetry(attempt);
2210
+ continue;
2211
+ }
2212
+ if (isAbortError2) {
2213
+ const timeoutMessage = "AgentID API Warning: Connection timeout exceeded.";
2214
+ console.warn(timeoutMessage);
2124
2215
  this.logGuardFallback({
2125
- reason: verdict.reason ?? `http_${res.status}`,
2126
- status: "upstream_error",
2216
+ reason: "timeout_fallback",
2217
+ status: "latency_timeout",
2127
2218
  guardParams: params,
2128
2219
  apiKey: effectiveApiKey
2129
2220
  });
2130
- return { allowed: true, reason: "system_failure_fail_open" };
2221
+ if (this.strictMode) {
2222
+ return { allowed: false, reason: "network_error_strict_mode" };
2223
+ }
2224
+ return { allowed: true, reason: "timeout_fallback" };
2131
2225
  }
2132
- return verdict;
2133
- }
2134
- if (!res.ok) {
2135
- throw new Error(`API Error ${res.status}`);
2136
- }
2137
- throw new Error("Invalid guard response");
2138
- } catch (error) {
2139
- const isAbortError2 = error && typeof error === "object" && error.name === "AbortError";
2140
- if (isAbortError2) {
2141
- const timeoutMessage = "AgentID API Warning: Connection timeout exceeded.";
2142
- console.warn(timeoutMessage);
2226
+ console.warn(
2227
+ this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2228
+ error
2229
+ );
2143
2230
  this.logGuardFallback({
2144
- reason: "timeout_fallback",
2145
- status: "latency_timeout",
2231
+ reason: "guard_unreachable",
2232
+ status: "guard_unreachable",
2146
2233
  guardParams: params,
2147
2234
  apiKey: effectiveApiKey
2148
2235
  });
2149
2236
  if (this.strictMode) {
2150
2237
  return { allowed: false, reason: "network_error_strict_mode" };
2151
2238
  }
2152
- return { allowed: true, reason: "timeout_fallback" };
2239
+ return { allowed: true, reason: "guard_unreachable" };
2240
+ } finally {
2241
+ clearTimeout(timeoutId);
2153
2242
  }
2154
- console.warn(
2155
- this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2156
- error
2157
- );
2158
- this.logGuardFallback({
2159
- reason: "guard_unreachable",
2160
- status: "guard_unreachable",
2161
- guardParams: params,
2162
- apiKey: effectiveApiKey
2163
- });
2243
+ }
2244
+ if (lastAbort) {
2164
2245
  if (this.strictMode) {
2165
2246
  return { allowed: false, reason: "network_error_strict_mode" };
2166
2247
  }
2167
- return { allowed: true, reason: "guard_unreachable" };
2168
- } finally {
2169
- clearTimeout(timeoutId);
2248
+ return { allowed: true, reason: "timeout_fallback" };
2249
+ }
2250
+ if (typeof lastStatusCode === "number" && lastStatusCode >= 500) {
2251
+ if (this.strictMode) {
2252
+ return { allowed: false, reason: "server_error" };
2253
+ }
2254
+ return { allowed: true, reason: "system_failure_fail_open" };
2255
+ }
2256
+ console.warn(
2257
+ this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2258
+ lastError
2259
+ );
2260
+ if (this.strictMode) {
2261
+ return { allowed: false, reason: "network_error_strict_mode" };
2170
2262
  }
2263
+ return { allowed: true, reason: "guard_unreachable" };
2171
2264
  }
2172
2265
  extractStreamChunkText(chunk) {
2173
2266
  if (typeof chunk === "string") {
@@ -2272,7 +2365,7 @@ var AgentID = class {
2272
2365
  void (async () => {
2273
2366
  try {
2274
2367
  const effectiveApiKey = this.resolveApiKey(options?.apiKey);
2275
- const eventId = params.event_id ?? (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function" ? crypto.randomUUID() : `evt_${Date.now()}_${Math.random().toString(36).slice(2)}`);
2368
+ const eventId = params.event_id ?? createEventId();
2276
2369
  const timestamp = params.timestamp ?? (/* @__PURE__ */ new Date()).toISOString();
2277
2370
  void this.getCapabilityConfig(false, { apiKey: effectiveApiKey }).catch(() => void 0);
2278
2371
  const metadata = {
@@ -2398,7 +2491,7 @@ var AgentID = class {
2398
2491
  "AgentID: No user message found. Security guard requires string input."
2399
2492
  );
2400
2493
  }
2401
- const clientEventId = typeof crypto !== "undefined" && typeof crypto.randomUUID === "function" ? crypto.randomUUID() : `evt_${Date.now()}_${Math.random().toString(36).slice(2)}`;
2494
+ const clientEventId = this.resolveClientEventId(req);
2402
2495
  const verdict = await this.guard({
2403
2496
  input: maskedText,
2404
2497
  system_id: systemId,
@@ -25,6 +25,8 @@ interface GuardResponse {
25
25
  reason?: string;
26
26
  detected_pii?: boolean;
27
27
  transformed_input?: string;
28
+ guard_event_id?: string;
29
+ client_event_id?: string;
28
30
  shadow_mode?: boolean;
29
31
  simulated_decision?: "allowed" | "masked" | "blocked";
30
32
  }
@@ -80,9 +82,11 @@ declare class AgentID {
80
82
  private pii;
81
83
  private localEnforcer;
82
84
  private injectionScanner;
85
+ private requestClientEventIds;
83
86
  constructor(config: AgentIDConfig);
84
87
  private buildClientCapabilities;
85
88
  private resolveApiKey;
89
+ private resolveClientEventId;
86
90
  getCapabilityConfig(force?: boolean, options?: RequestOptions): Promise<CapabilityConfig>;
87
91
  private getCachedCapabilityConfig;
88
92
  prepareInputForDispatch(params: {
@@ -25,6 +25,8 @@ interface GuardResponse {
25
25
  reason?: string;
26
26
  detected_pii?: boolean;
27
27
  transformed_input?: string;
28
+ guard_event_id?: string;
29
+ client_event_id?: string;
28
30
  shadow_mode?: boolean;
29
31
  simulated_decision?: "allowed" | "masked" | "blocked";
30
32
  }
@@ -80,9 +82,11 @@ declare class AgentID {
80
82
  private pii;
81
83
  private localEnforcer;
82
84
  private injectionScanner;
85
+ private requestClientEventIds;
83
86
  constructor(config: AgentIDConfig);
84
87
  private buildClientCapabilities;
85
88
  private resolveApiKey;
89
+ private resolveClientEventId;
86
90
  getCapabilityConfig(force?: boolean, options?: RequestOptions): Promise<CapabilityConfig>;
87
91
  private getCachedCapabilityConfig;
88
92
  prepareInputForDispatch(params: {
@@ -1 +1 @@
1
- export { a as AgentIDCallbackHandler } from './langchain-DVPOWfCC.mjs';
1
+ export { a as AgentIDCallbackHandler } from './langchain-CUyC7jmr.mjs';
@@ -1 +1 @@
1
- export { a as AgentIDCallbackHandler } from './langchain-DVPOWfCC.js';
1
+ export { a as AgentIDCallbackHandler } from './langchain-CUyC7jmr.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentid-sdk",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "AgentID JavaScript/TypeScript SDK for guard, ingest, tracing, and analytics.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://agentid.ai",