@kya-os/agentshield-nextjs 0.1.34 → 0.1.36

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.mjs CHANGED
@@ -1061,18 +1061,9 @@ async function checkWasmAvailability() {
1061
1061
  if (typeof WebAssembly === "undefined") {
1062
1062
  return false;
1063
1063
  }
1064
- const wasmCode = new Uint8Array([
1065
- 0,
1066
- 97,
1067
- 115,
1068
- 109,
1069
- 1,
1070
- 0,
1071
- 0,
1072
- 0
1073
- ]);
1074
- const module = await WebAssembly.compile(wasmCode);
1075
- await WebAssembly.instantiate(module);
1064
+ if (!WebAssembly.instantiate || !WebAssembly.Module) {
1065
+ return false;
1066
+ }
1076
1067
  return true;
1077
1068
  } catch {
1078
1069
  return false;
@@ -1378,14 +1369,32 @@ function createEnhancedAgentShieldMiddleware(config = {}) {
1378
1369
  });
1379
1370
  return storageInitPromise;
1380
1371
  };
1381
- let wasmAvailable = false;
1382
- checkWasmAvailability().then((available) => {
1383
- wasmAvailable = available;
1384
- if (available) {
1385
- console.log("[AgentShield] \u2705 WASM support detected - enhanced detection enabled");
1372
+ let detector = null;
1373
+ let detectorInitPromise = null;
1374
+ const getDetector = async () => {
1375
+ if (detector) return detector;
1376
+ if (detectorInitPromise) {
1377
+ await detectorInitPromise;
1378
+ return detector;
1386
1379
  }
1387
- });
1388
- const detector = wasmAvailable ? new EdgeAgentDetectorWrapperWithWasm({ enableWasm: true }) : new EdgeAgentDetectorWrapper({});
1380
+ detectorInitPromise = (async () => {
1381
+ try {
1382
+ const wasmAvailable = await checkWasmAvailability();
1383
+ if (wasmAvailable) {
1384
+ console.log("[AgentShield] \u2705 WASM support detected - enhanced detection enabled");
1385
+ detector = new EdgeAgentDetectorWrapperWithWasm({ enableWasm: true });
1386
+ } else {
1387
+ console.log("[AgentShield] \u2139\uFE0F Using pattern-based detection (WASM not available)");
1388
+ detector = new EdgeAgentDetectorWrapper({});
1389
+ }
1390
+ } catch (error) {
1391
+ console.warn("[AgentShield] Failed to initialize WASM, using fallback:", error);
1392
+ detector = new EdgeAgentDetectorWrapper({});
1393
+ }
1394
+ })();
1395
+ await detectorInitPromise;
1396
+ return detector;
1397
+ };
1389
1398
  const sessionManager = new SessionManager();
1390
1399
  const sessionTrackingEnabled = config.sessionTracking?.enabled !== false;
1391
1400
  return async (request) => {
@@ -1405,7 +1414,8 @@ function createEnhancedAgentShieldMiddleware(config = {}) {
1405
1414
  method: request.method,
1406
1415
  timestamp: /* @__PURE__ */ new Date()
1407
1416
  };
1408
- const result = await detector.analyze(context);
1417
+ const activeDetector = await getDetector();
1418
+ const result = await activeDetector.analyze(context);
1409
1419
  let finalConfidence = result.confidence;
1410
1420
  let verificationMethod = result.verificationMethod || "pattern";
1411
1421
  if (result.isAgent) {
@@ -1490,12 +1500,15 @@ function createEnhancedAgentShieldMiddleware(config = {}) {
1490
1500
  return response2;
1491
1501
  }
1492
1502
  case "log":
1493
- console.log(`[AgentShield] \u{1F916} AI Agent detected (${verificationMethod}):`, {
1494
- agent: result.detectedAgent?.name,
1495
- confidence: `${(finalConfidence * 100).toFixed(0)}%`,
1496
- path: pathWithQuery,
1497
- verification: verificationMethod
1498
- });
1503
+ const isInteresting = finalConfidence >= 0.9 || result.detectedAgent?.name?.toLowerCase().includes("chatgpt") || result.detectedAgent?.name?.toLowerCase().includes("perplexity") || verificationMethod === "signature";
1504
+ if (isInteresting) {
1505
+ console.log(`[AgentShield] \u{1F916} AI Agent detected (${verificationMethod}):`, {
1506
+ agent: result.detectedAgent?.name,
1507
+ confidence: `${(finalConfidence * 100).toFixed(0)}%`,
1508
+ path: pathWithQuery,
1509
+ verification: verificationMethod
1510
+ });
1511
+ }
1499
1512
  break;
1500
1513
  }
1501
1514
  }