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