agentid-sdk 0.1.11 → 0.1.12

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-BGP3qxvW.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-BGP3qxvW.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,24 @@ 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 createCorrelationId(seed) {
1886
+ if (isUuidLike(seed)) return seed;
1887
+ if (typeof globalThis.crypto?.randomUUID === "function") {
1888
+ return globalThis.crypto.randomUUID();
1889
+ }
1890
+ return `cid_${Date.now()}_${Math.random().toString(36).slice(2)}`;
1891
+ }
1892
+ async function waitForRetry(attemptIndex) {
1893
+ const delay = GUARD_RETRY_DELAYS_MS[attemptIndex];
1894
+ if (!delay) return;
1895
+ await new Promise((resolve) => setTimeout(resolve, delay));
1896
+ }
1877
1897
  async function safeReadJson2(response) {
1878
1898
  try {
1879
1899
  return await response.json();
@@ -2133,79 +2153,124 @@ var AgentID = class {
2133
2153
  ...params,
2134
2154
  client_capabilities: params.client_capabilities ?? this.buildClientCapabilities()
2135
2155
  };
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) {
2156
+ const correlationId = createCorrelationId(payload.client_event_id);
2157
+ let lastStatusCode = null;
2158
+ let lastAbort = false;
2159
+ let lastError = null;
2160
+ for (let attempt = 0; attempt < GUARD_MAX_ATTEMPTS; attempt += 1) {
2161
+ const controller = new AbortController();
2162
+ const timeoutId = setTimeout(() => controller.abort(), this.guardTimeoutMs);
2163
+ try {
2164
+ const res = await fetch(`${this.baseUrl}/guard`, {
2165
+ method: "POST",
2166
+ headers: {
2167
+ "Content-Type": "application/json",
2168
+ "x-agentid-api-key": effectiveApiKey,
2169
+ "X-AgentID-SDK-Version": AGENTID_SDK_VERSION_HEADER2,
2170
+ "x-correlation-id": correlationId
2171
+ },
2172
+ body: JSON.stringify(payload),
2173
+ signal: controller.signal
2174
+ });
2175
+ lastStatusCode = res.status;
2176
+ const responseBody = await safeReadJson2(res);
2177
+ if (responseBody && typeof responseBody.allowed === "boolean") {
2178
+ const verdict = responseBody;
2179
+ const infrastructureFailure = verdict.allowed === false && (isInfrastructureGuardReason(verdict.reason) || res.status >= 500);
2180
+ if (infrastructureFailure) {
2181
+ if (attempt < GUARD_MAX_ATTEMPTS - 1) {
2182
+ await waitForRetry(attempt);
2183
+ continue;
2184
+ }
2185
+ if (this.strictMode) {
2186
+ console.warn(
2187
+ `[AgentID] Guard API infrastructure failure in strict mode (${verdict.reason ?? `http_${res.status}`}). Blocking request.`
2188
+ );
2189
+ return { allowed: false, reason: verdict.reason ?? "network_error_strict_mode" };
2190
+ }
2154
2191
  console.warn(
2155
- `[AgentID] Guard API infrastructure failure in strict mode (${verdict.reason ?? `http_${res.status}`}). Blocking request.`
2192
+ `[AgentID] Guard API infrastructure fallback in fail-open mode (${verdict.reason ?? `http_${res.status}`}).`
2156
2193
  );
2157
- return { allowed: false, reason: verdict.reason ?? "network_error_strict_mode" };
2194
+ this.logGuardFallback({
2195
+ reason: verdict.reason ?? `http_${res.status}`,
2196
+ status: "upstream_error",
2197
+ guardParams: params,
2198
+ apiKey: effectiveApiKey
2199
+ });
2200
+ return { allowed: true, reason: "system_failure_fail_open" };
2158
2201
  }
2159
- console.warn(
2160
- `[AgentID] Guard API infrastructure fallback in fail-open mode (${verdict.reason ?? `http_${res.status}`}).`
2161
- );
2202
+ return verdict;
2203
+ }
2204
+ if (!res.ok) {
2205
+ if (res.status >= 500 && attempt < GUARD_MAX_ATTEMPTS - 1) {
2206
+ await waitForRetry(attempt);
2207
+ continue;
2208
+ }
2209
+ throw new Error(`API Error ${res.status}`);
2210
+ }
2211
+ throw new Error("Invalid guard response");
2212
+ } catch (error) {
2213
+ lastError = error;
2214
+ const isAbortError2 = Boolean(
2215
+ error && typeof error === "object" && error.name === "AbortError"
2216
+ );
2217
+ lastAbort = isAbortError2;
2218
+ if (attempt < GUARD_MAX_ATTEMPTS - 1) {
2219
+ await waitForRetry(attempt);
2220
+ continue;
2221
+ }
2222
+ if (isAbortError2) {
2223
+ const timeoutMessage = "AgentID API Warning: Connection timeout exceeded.";
2224
+ console.warn(timeoutMessage);
2162
2225
  this.logGuardFallback({
2163
- reason: verdict.reason ?? `http_${res.status}`,
2164
- status: "upstream_error",
2226
+ reason: "timeout_fallback",
2227
+ status: "latency_timeout",
2165
2228
  guardParams: params,
2166
2229
  apiKey: effectiveApiKey
2167
2230
  });
2168
- return { allowed: true, reason: "system_failure_fail_open" };
2231
+ if (this.strictMode) {
2232
+ return { allowed: false, reason: "network_error_strict_mode" };
2233
+ }
2234
+ return { allowed: true, reason: "timeout_fallback" };
2169
2235
  }
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);
2236
+ console.warn(
2237
+ this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2238
+ error
2239
+ );
2181
2240
  this.logGuardFallback({
2182
- reason: "timeout_fallback",
2183
- status: "latency_timeout",
2241
+ reason: "guard_unreachable",
2242
+ status: "guard_unreachable",
2184
2243
  guardParams: params,
2185
2244
  apiKey: effectiveApiKey
2186
2245
  });
2187
2246
  if (this.strictMode) {
2188
2247
  return { allowed: false, reason: "network_error_strict_mode" };
2189
2248
  }
2190
- return { allowed: true, reason: "timeout_fallback" };
2249
+ return { allowed: true, reason: "guard_unreachable" };
2250
+ } finally {
2251
+ clearTimeout(timeoutId);
2191
2252
  }
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
- });
2253
+ }
2254
+ if (lastAbort) {
2202
2255
  if (this.strictMode) {
2203
2256
  return { allowed: false, reason: "network_error_strict_mode" };
2204
2257
  }
2205
- return { allowed: true, reason: "guard_unreachable" };
2206
- } finally {
2207
- clearTimeout(timeoutId);
2258
+ return { allowed: true, reason: "timeout_fallback" };
2259
+ }
2260
+ if (typeof lastStatusCode === "number" && lastStatusCode >= 500) {
2261
+ if (this.strictMode) {
2262
+ return { allowed: false, reason: "server_error" };
2263
+ }
2264
+ return { allowed: true, reason: "system_failure_fail_open" };
2265
+ }
2266
+ console.warn(
2267
+ this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2268
+ lastError
2269
+ );
2270
+ if (this.strictMode) {
2271
+ return { allowed: false, reason: "network_error_strict_mode" };
2208
2272
  }
2273
+ return { allowed: true, reason: "guard_unreachable" };
2209
2274
  }
2210
2275
  extractStreamChunkText(chunk) {
2211
2276
  if (typeof chunk === "string") {
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,24 @@ 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 createCorrelationId(seed) {
1848
+ if (isUuidLike(seed)) return seed;
1849
+ if (typeof globalThis.crypto?.randomUUID === "function") {
1850
+ return globalThis.crypto.randomUUID();
1851
+ }
1852
+ return `cid_${Date.now()}_${Math.random().toString(36).slice(2)}`;
1853
+ }
1854
+ async function waitForRetry(attemptIndex) {
1855
+ const delay = GUARD_RETRY_DELAYS_MS[attemptIndex];
1856
+ if (!delay) return;
1857
+ await new Promise((resolve) => setTimeout(resolve, delay));
1858
+ }
1839
1859
  async function safeReadJson2(response) {
1840
1860
  try {
1841
1861
  return await response.json();
@@ -2095,79 +2115,124 @@ var AgentID = class {
2095
2115
  ...params,
2096
2116
  client_capabilities: params.client_capabilities ?? this.buildClientCapabilities()
2097
2117
  };
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) {
2118
+ const correlationId = createCorrelationId(payload.client_event_id);
2119
+ let lastStatusCode = null;
2120
+ let lastAbort = false;
2121
+ let lastError = null;
2122
+ for (let attempt = 0; attempt < GUARD_MAX_ATTEMPTS; attempt += 1) {
2123
+ const controller = new AbortController();
2124
+ const timeoutId = setTimeout(() => controller.abort(), this.guardTimeoutMs);
2125
+ try {
2126
+ const res = await fetch(`${this.baseUrl}/guard`, {
2127
+ method: "POST",
2128
+ headers: {
2129
+ "Content-Type": "application/json",
2130
+ "x-agentid-api-key": effectiveApiKey,
2131
+ "X-AgentID-SDK-Version": AGENTID_SDK_VERSION_HEADER2,
2132
+ "x-correlation-id": correlationId
2133
+ },
2134
+ body: JSON.stringify(payload),
2135
+ signal: controller.signal
2136
+ });
2137
+ lastStatusCode = res.status;
2138
+ const responseBody = await safeReadJson2(res);
2139
+ if (responseBody && typeof responseBody.allowed === "boolean") {
2140
+ const verdict = responseBody;
2141
+ const infrastructureFailure = verdict.allowed === false && (isInfrastructureGuardReason(verdict.reason) || res.status >= 500);
2142
+ if (infrastructureFailure) {
2143
+ if (attempt < GUARD_MAX_ATTEMPTS - 1) {
2144
+ await waitForRetry(attempt);
2145
+ continue;
2146
+ }
2147
+ if (this.strictMode) {
2148
+ console.warn(
2149
+ `[AgentID] Guard API infrastructure failure in strict mode (${verdict.reason ?? `http_${res.status}`}). Blocking request.`
2150
+ );
2151
+ return { allowed: false, reason: verdict.reason ?? "network_error_strict_mode" };
2152
+ }
2116
2153
  console.warn(
2117
- `[AgentID] Guard API infrastructure failure in strict mode (${verdict.reason ?? `http_${res.status}`}). Blocking request.`
2154
+ `[AgentID] Guard API infrastructure fallback in fail-open mode (${verdict.reason ?? `http_${res.status}`}).`
2118
2155
  );
2119
- return { allowed: false, reason: verdict.reason ?? "network_error_strict_mode" };
2156
+ this.logGuardFallback({
2157
+ reason: verdict.reason ?? `http_${res.status}`,
2158
+ status: "upstream_error",
2159
+ guardParams: params,
2160
+ apiKey: effectiveApiKey
2161
+ });
2162
+ return { allowed: true, reason: "system_failure_fail_open" };
2163
+ }
2164
+ return verdict;
2165
+ }
2166
+ if (!res.ok) {
2167
+ if (res.status >= 500 && attempt < GUARD_MAX_ATTEMPTS - 1) {
2168
+ await waitForRetry(attempt);
2169
+ continue;
2120
2170
  }
2121
- console.warn(
2122
- `[AgentID] Guard API infrastructure fallback in fail-open mode (${verdict.reason ?? `http_${res.status}`}).`
2123
- );
2171
+ throw new Error(`API Error ${res.status}`);
2172
+ }
2173
+ throw new Error("Invalid guard response");
2174
+ } catch (error) {
2175
+ lastError = error;
2176
+ const isAbortError2 = Boolean(
2177
+ error && typeof error === "object" && error.name === "AbortError"
2178
+ );
2179
+ lastAbort = isAbortError2;
2180
+ if (attempt < GUARD_MAX_ATTEMPTS - 1) {
2181
+ await waitForRetry(attempt);
2182
+ continue;
2183
+ }
2184
+ if (isAbortError2) {
2185
+ const timeoutMessage = "AgentID API Warning: Connection timeout exceeded.";
2186
+ console.warn(timeoutMessage);
2124
2187
  this.logGuardFallback({
2125
- reason: verdict.reason ?? `http_${res.status}`,
2126
- status: "upstream_error",
2188
+ reason: "timeout_fallback",
2189
+ status: "latency_timeout",
2127
2190
  guardParams: params,
2128
2191
  apiKey: effectiveApiKey
2129
2192
  });
2130
- return { allowed: true, reason: "system_failure_fail_open" };
2193
+ if (this.strictMode) {
2194
+ return { allowed: false, reason: "network_error_strict_mode" };
2195
+ }
2196
+ return { allowed: true, reason: "timeout_fallback" };
2131
2197
  }
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);
2198
+ console.warn(
2199
+ this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2200
+ error
2201
+ );
2143
2202
  this.logGuardFallback({
2144
- reason: "timeout_fallback",
2145
- status: "latency_timeout",
2203
+ reason: "guard_unreachable",
2204
+ status: "guard_unreachable",
2146
2205
  guardParams: params,
2147
2206
  apiKey: effectiveApiKey
2148
2207
  });
2149
2208
  if (this.strictMode) {
2150
2209
  return { allowed: false, reason: "network_error_strict_mode" };
2151
2210
  }
2152
- return { allowed: true, reason: "timeout_fallback" };
2211
+ return { allowed: true, reason: "guard_unreachable" };
2212
+ } finally {
2213
+ clearTimeout(timeoutId);
2153
2214
  }
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
- });
2215
+ }
2216
+ if (lastAbort) {
2164
2217
  if (this.strictMode) {
2165
2218
  return { allowed: false, reason: "network_error_strict_mode" };
2166
2219
  }
2167
- return { allowed: true, reason: "guard_unreachable" };
2168
- } finally {
2169
- clearTimeout(timeoutId);
2220
+ return { allowed: true, reason: "timeout_fallback" };
2221
+ }
2222
+ if (typeof lastStatusCode === "number" && lastStatusCode >= 500) {
2223
+ if (this.strictMode) {
2224
+ return { allowed: false, reason: "server_error" };
2225
+ }
2226
+ return { allowed: true, reason: "system_failure_fail_open" };
2227
+ }
2228
+ console.warn(
2229
+ this.strictMode ? "[AgentID] Guard check failed (Strict mode active):" : "[AgentID] Guard check failed (Fail-Open active):",
2230
+ lastError
2231
+ );
2232
+ if (this.strictMode) {
2233
+ return { allowed: false, reason: "network_error_strict_mode" };
2170
2234
  }
2235
+ return { allowed: true, reason: "guard_unreachable" };
2171
2236
  }
2172
2237
  extractStreamChunkText(chunk) {
2173
2238
  if (typeof chunk === "string") {
@@ -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
  }
@@ -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
  }
@@ -1 +1 @@
1
- export { a as AgentIDCallbackHandler } from './langchain-DVPOWfCC.mjs';
1
+ export { a as AgentIDCallbackHandler } from './langchain-BGP3qxvW.mjs';
@@ -1 +1 @@
1
- export { a as AgentIDCallbackHandler } from './langchain-DVPOWfCC.js';
1
+ export { a as AgentIDCallbackHandler } from './langchain-BGP3qxvW.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.12",
4
4
  "description": "AgentID JavaScript/TypeScript SDK for guard, ingest, tracing, and analytics.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://agentid.ai",