@kya-os/agentshield-nextjs 0.1.9 → 0.1.10

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,73 +1,6 @@
1
- import { D as DetectionContext } from './middleware-OvcbG6s2.mjs';
2
- export { N as NextJSMiddlewareConfig, a as agentShield, c as createAgentShieldMiddleware } from './middleware-OvcbG6s2.mjs';
3
- import { AgentShieldConfig, AgentDetector, DetectionResult } from '@kya-os/agentshield';
1
+ export { D as DetectionContext, N as NextJSMiddlewareConfig, a as agentShield, c as createAgentShieldMiddleware } from './middleware-OvcbG6s2.mjs';
4
2
  import 'next/server';
5
-
6
- /**
7
- * Hook for client-side agent detection
8
- */
9
- declare function useAgentDetection(config?: Partial<AgentShieldConfig>): {
10
- detect: () => Promise<{
11
- isAgent: boolean;
12
- confidence: number;
13
- reasons: string[];
14
- timestamp: Date;
15
- confidenceLevel?: "low" | "medium" | "high" | undefined;
16
- metadata?: Record<string, unknown> | undefined;
17
- detectedAgent?: {
18
- type: string;
19
- name: string;
20
- } | undefined;
21
- verificationMethod?: string | undefined;
22
- riskLevel?: string | undefined;
23
- forgeabilityRisk?: "low" | "medium" | "high" | undefined;
24
- }>;
25
- isDetecting: boolean;
26
- lastResult: {
27
- isAgent: boolean;
28
- confidence: number;
29
- reasons: string[];
30
- timestamp: Date;
31
- confidenceLevel?: "low" | "medium" | "high" | undefined;
32
- metadata?: Record<string, unknown> | undefined;
33
- detectedAgent?: {
34
- type: string;
35
- name: string;
36
- } | undefined;
37
- verificationMethod?: string | undefined;
38
- riskLevel?: string | undefined;
39
- forgeabilityRisk?: "low" | "medium" | "high" | undefined;
40
- } | null;
41
- detector: AgentDetector;
42
- };
43
- /**
44
- * Hook for monitoring detection results
45
- */
46
- declare function useDetectionMonitor(onDetection?: (context: DetectionContext) => void): {
47
- detectionHistory: {
48
- isAgent: boolean;
49
- confidence: number;
50
- reasons: string[];
51
- timestamp: Date;
52
- confidenceLevel?: "low" | "medium" | "high" | undefined;
53
- metadata?: Record<string, unknown> | undefined;
54
- detectedAgent?: {
55
- type: string;
56
- name: string;
57
- } | undefined;
58
- verificationMethod?: string | undefined;
59
- riskLevel?: string | undefined;
60
- forgeabilityRisk?: "low" | "medium" | "high" | undefined;
61
- }[];
62
- addDetection: (result: DetectionResult, request?: any) => void;
63
- clearHistory: () => void;
64
- getStats: () => {
65
- total: number;
66
- detected: number;
67
- avgConfidence: number;
68
- detectionRate: number;
69
- };
70
- };
3
+ import '@kya-os/agentshield';
71
4
 
72
5
  /**
73
6
  * @fileoverview AgentShield Next.js Integration
@@ -80,4 +13,4 @@ declare function useDetectionMonitor(onDetection?: (context: DetectionContext) =
80
13
  */
81
14
  declare const VERSION = "0.1.0";
82
15
 
83
- export { DetectionContext, VERSION, useAgentDetection, useDetectionMonitor };
16
+ export { VERSION };
package/dist/index.d.ts CHANGED
@@ -1,73 +1,6 @@
1
- import { D as DetectionContext } from './middleware-OvcbG6s2.js';
2
- export { N as NextJSMiddlewareConfig, a as agentShield, c as createAgentShieldMiddleware } from './middleware-OvcbG6s2.js';
3
- import { AgentShieldConfig, AgentDetector, DetectionResult } from '@kya-os/agentshield';
1
+ export { D as DetectionContext, N as NextJSMiddlewareConfig, a as agentShield, c as createAgentShieldMiddleware } from './middleware-OvcbG6s2.js';
4
2
  import 'next/server';
5
-
6
- /**
7
- * Hook for client-side agent detection
8
- */
9
- declare function useAgentDetection(config?: Partial<AgentShieldConfig>): {
10
- detect: () => Promise<{
11
- isAgent: boolean;
12
- confidence: number;
13
- reasons: string[];
14
- timestamp: Date;
15
- confidenceLevel?: "low" | "medium" | "high" | undefined;
16
- metadata?: Record<string, unknown> | undefined;
17
- detectedAgent?: {
18
- type: string;
19
- name: string;
20
- } | undefined;
21
- verificationMethod?: string | undefined;
22
- riskLevel?: string | undefined;
23
- forgeabilityRisk?: "low" | "medium" | "high" | undefined;
24
- }>;
25
- isDetecting: boolean;
26
- lastResult: {
27
- isAgent: boolean;
28
- confidence: number;
29
- reasons: string[];
30
- timestamp: Date;
31
- confidenceLevel?: "low" | "medium" | "high" | undefined;
32
- metadata?: Record<string, unknown> | undefined;
33
- detectedAgent?: {
34
- type: string;
35
- name: string;
36
- } | undefined;
37
- verificationMethod?: string | undefined;
38
- riskLevel?: string | undefined;
39
- forgeabilityRisk?: "low" | "medium" | "high" | undefined;
40
- } | null;
41
- detector: AgentDetector;
42
- };
43
- /**
44
- * Hook for monitoring detection results
45
- */
46
- declare function useDetectionMonitor(onDetection?: (context: DetectionContext) => void): {
47
- detectionHistory: {
48
- isAgent: boolean;
49
- confidence: number;
50
- reasons: string[];
51
- timestamp: Date;
52
- confidenceLevel?: "low" | "medium" | "high" | undefined;
53
- metadata?: Record<string, unknown> | undefined;
54
- detectedAgent?: {
55
- type: string;
56
- name: string;
57
- } | undefined;
58
- verificationMethod?: string | undefined;
59
- riskLevel?: string | undefined;
60
- forgeabilityRisk?: "low" | "medium" | "high" | undefined;
61
- }[];
62
- addDetection: (result: DetectionResult, request?: any) => void;
63
- clearHistory: () => void;
64
- getStats: () => {
65
- total: number;
66
- detected: number;
67
- avgConfidence: number;
68
- detectionRate: number;
69
- };
70
- };
3
+ import '@kya-os/agentshield';
71
4
 
72
5
  /**
73
6
  * @fileoverview AgentShield Next.js Integration
@@ -80,4 +13,4 @@ declare function useDetectionMonitor(onDetection?: (context: DetectionContext) =
80
13
  */
81
14
  declare const VERSION = "0.1.0";
82
15
 
83
- export { DetectionContext, VERSION, useAgentDetection, useDetectionMonitor };
16
+ export { VERSION };
package/dist/index.js CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  var server = require('next/server');
4
4
  var agentshield = require('@kya-os/agentshield');
5
- var react = require('react');
6
5
 
7
6
  // src/middleware.ts
8
7
  function createAgentShieldMiddleware(config = {}) {
@@ -114,72 +113,6 @@ function createAgentShieldMiddleware(config = {}) {
114
113
  function agentShield(config = {}) {
115
114
  return createAgentShieldMiddleware(config);
116
115
  }
117
- function useAgentDetection(config = {}) {
118
- const [detector] = react.useState(() => new agentshield.AgentDetector(config));
119
- const [isDetecting, setIsDetecting] = react.useState(false);
120
- const [lastResult, setLastResult] = react.useState(null);
121
- const detect = react.useCallback(async () => {
122
- setIsDetecting(true);
123
- try {
124
- const context = {
125
- userAgent: navigator.userAgent,
126
- headers: {},
127
- // Client-side headers are limited
128
- timestamp: /* @__PURE__ */ new Date()
129
- };
130
- const result = await detector.analyze(context);
131
- setLastResult(result);
132
- return result;
133
- } finally {
134
- setIsDetecting(false);
135
- }
136
- }, [detector]);
137
- return {
138
- detect,
139
- isDetecting,
140
- lastResult,
141
- detector
142
- };
143
- }
144
- function useDetectionMonitor(onDetection) {
145
- const [detectionHistory, setDetectionHistory] = react.useState(
146
- []
147
- );
148
- const addDetection = react.useCallback(
149
- (result, request) => {
150
- setDetectionHistory((prev) => [...prev, result].slice(-100));
151
- if (onDetection && request) {
152
- onDetection({
153
- result,
154
- request,
155
- userAgent: request.headers?.get?.("user-agent"),
156
- ip: request.ip
157
- });
158
- }
159
- },
160
- [onDetection]
161
- );
162
- const clearHistory = react.useCallback(() => {
163
- setDetectionHistory([]);
164
- }, []);
165
- const getStats = react.useCallback(() => {
166
- const total = detectionHistory.length;
167
- const detected = detectionHistory.filter((r) => r.isAgent).length;
168
- const avgConfidence = total > 0 ? detectionHistory.reduce((sum, r) => sum + r.confidence, 0) / total : 0;
169
- return {
170
- total,
171
- detected,
172
- avgConfidence,
173
- detectionRate: total > 0 ? detected / total : 0
174
- };
175
- }, [detectionHistory]);
176
- return {
177
- detectionHistory,
178
- addDetection,
179
- clearHistory,
180
- getStats
181
- };
182
- }
183
116
 
184
117
  // src/index.ts
185
118
  var VERSION = "0.1.0";
@@ -192,7 +125,5 @@ var VERSION = "0.1.0";
192
125
  exports.VERSION = VERSION;
193
126
  exports.agentShield = agentShield;
194
127
  exports.createAgentShieldMiddleware = createAgentShieldMiddleware;
195
- exports.useAgentDetection = useAgentDetection;
196
- exports.useDetectionMonitor = useDetectionMonitor;
197
128
  //# sourceMappingURL=index.js.map
198
129
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/middleware.ts","../src/hooks.ts","../src/index.ts"],"names":["AgentDetector","NextResponse","response","useState","useCallback"],"mappings":";;;;;;;AAWO,SAAS,2BAAA,CACd,MAAA,GAA0C,EAAC,EAC3C;AACA,EAAA,MAAM,QAAA,GAAW,IAAIA,yBAAA,CAAc,MAAM,CAAA;AAGzC,EAAA,IAAI,OAAO,MAAA,EAAQ;AACjB,IAAA,MAAA,CAAO,OAAA,CAAQ,OAAO,MAAM,CAAA,CAAE,QAAQ,CAAC,CAAC,KAAA,EAAO,OAAO,CAAA,KAAM;AAC1D,MAAA,QAAA,CAAS,EAAA,CAAG,OAAc,OAAc,CAAA;AAAA,IAC1C,CAAC,CAAA;AAAA,EACH;AAEA,EAAA,MAAM;AAAA,IACJ,eAAA,GAAkB,KAAA;AAAA,IAClB,WAAA;AAAA,IACA,YAAY,EAAC;AAAA,IACb,eAAA,GAAkB;AAAA,MAChB,MAAA,EAAQ,GAAA;AAAA,MACR,OAAA,EAAS,yCAAA;AAAA,MACT,OAAA,EAAS,EAAE,cAAA,EAAgB,kBAAA;AAAmB,KAChD;AAAA,IACA,WAAA,GAAc,UAAA;AAAA,IACd,UAAA,GAAa;AAAA,GACf,GAAI,MAAA;AAEJ,EAAA,OAAO,OAAO,OAAA,KAAgD;AAC5D,IAAA,IAAI;AAEF,MAAA,MAAM,UAAA,GAAa,SAAA,CAAU,IAAA,CAAK,CAAA,OAAA,KAAW;AAC3C,QAAA,IAAI,OAAO,YAAY,QAAA,EAAU;AAC/B,UAAA,OAAO,OAAA,CAAQ,OAAA,CAAQ,QAAA,CAAS,UAAA,CAAW,OAAO,CAAA;AAAA,QACpD;AACA,QAAA,OAAO,OAAA,CAAQ,IAAA,CAAK,OAAA,CAAQ,OAAA,CAAQ,QAAQ,CAAA;A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B,MAAA,CAAO,CAAC,GAAA,EAAK,CAAA,KAAM,GAAA,GAAM,CAAA,CAAE,UAAA,EAAY,CAAC,IAAI,KAAA,GAC7D,CAAA;AAEN,IAAA,OAAO;AAAA,MACL,KAAA;AAAA,MACA,QAAA;AAAA,MACA,aAAA;AAAA,MACA,aAAA,EAAe,KAAA,GAAQ,CAAA,GAAI,QAAA,GAAW,KAAA,GAAQ;AAAA,KAChD;AAAA,EACF,CAAA,EAAG,CAAC,gBAAgB,CAAC,CAAA;AAErB,EAAA,OAAO;AAAA,IACL,gBAAA;AAAA,IACA,YAAA;AAAA,IACA,YAAA;AAAA,IACA;AAAA,GACF;AACF;;;ACnFO,IAAM,OAAA,GAAU","file":"index.js","sourcesContent":["/**\n * Next.js middleware for AgentShield\n */\n\nimport { NextRequest, NextResponse } from 'next/server';\nimport { AgentDetector } from '@kya-os/agentshield';\nimport type { NextJSMiddlewareConfig } from './types';\n\n/**\n * Create AgentShield middleware for Next.js\n */\nexport function createAgentShieldMiddleware(\n config: Partial<NextJSMiddlewareConfig> = {}\n) {\n const detector = new AgentDetector(config);\n \n // Wire up event handlers if provided\n if (config.events) {\n Object.entries(config.events).forEach(([event, handler]) => {\n detector.on(event as any, handler as any);\n });\n }\n\n const {\n onAgentDetected = 'log',\n onDetection,\n skipPaths = [],\n blockedResponse = {\n status: 403,\n message: 'Access denied: Automated agent detected',\n headers: { 'Content-Type': 'application/json' },\n },\n redirectUrl = '/blocked',\n rewriteUrl = '/blocked',\n } = config;\n\n return async (request: NextRequest): Promise<NextResponse> => {\n try {\n // Check if path should be skipped\n const shouldSkip = skipPaths.some(pattern => {\n if (typeof pattern === 'string') {\n return request.nextUrl.pathname.startsWith(pattern);\n }\n return pattern.test(request.nextUrl.pathname);\n });\n\n if (shouldSkip) {\n // Mark as skipped in request\n (request as any).agentShield = { skipped: true };\n return NextResponse.next();\n }\n\n // Prepare request context\n const context = {\n userAgent: request.headers.get('user-agent') ?? undefined,\n ipAddress: request.ip ?? request.headers.get('x-forwarded-for') ?? undefined,\n headers: Object.fromEntries(request.headers.entries()),\n url: request.url,\n method: request.method,\n timestamp: new Date(),\n };\n\n // Analyze request\n const result = await detector.analyze(context);\n\n // Handle detection result\n if (\n result.isAgent &&\n result.confidence >= (config.confidenceThreshold ?? 0.7)\n ) {\n // Call custom detection handler if provided\n if (onDetection) {\n const customResponse = await onDetection(request, result);\n if (customResponse) {\n return customResponse;\n }\n }\n\n // Handle based on configuration\n switch (onAgentDetected) {\n case 'block': {\n const response = NextResponse.json(\n {\n error: blockedResponse.message,\n detected: true,\n confidence: result.confidence,\n timestamp: result.timestamp,\n },\n { status: blockedResponse.status }\n );\n\n if (blockedResponse.headers) {\n Object.entries(blockedResponse.headers).forEach(\n ([key, value]) => {\n response.headers.set(key, value);\n }\n );\n }\n \n // Emit blocked event\n detector.emit('agent.blocked', result, context);\n\n return response;\n }\n\n case 'redirect':\n return NextResponse.redirect(new URL(redirectUrl, request.url));\n\n case 'rewrite':\n return NextResponse.rewrite(new URL(rewriteUrl, request.url));\n\n case 'log':\n console.warn('AgentShield: Agent detected', {\n ipAddress: context.ipAddress,\n userAgent: context.userAgent,\n confidence: result.confidence,\n reasons: result.reasons,\n pathname: request.nextUrl.pathname,\n });\n break;\n\n case 'allow':\n default:\n // Emit allowed event for high-confidence agents\n if (result.isAgent && result.confidence >= (config.confidenceThreshold ?? 0.7)) {\n detector.emit('agent.allowed', result, context);\n }\n // Continue processing\n break;\n }\n }\n\n // Add detection result to request for API routes\n (request as any).agentShield = {\n result,\n skipped: false,\n };\n\n // Add detection result to response headers for debugging\n const response = NextResponse.next();\n response.headers.set('x-agentshield-detected', result.isAgent.toString());\n response.headers.set(\n 'x-agentshield-confidence',\n result.confidence.toString()\n );\n\n return response;\n } catch (error) {\n console.error('AgentShield middleware error:', error);\n return NextResponse.next(); // Continue on error\n }\n };\n}\n\n/**\n * Convenience function for basic setup\n */\nexport function agentShield(config: Partial<NextJSMiddlewareConfig> = {}) {\n return createAgentShieldMiddleware(config);\n}\n","'use client';\n\n/**\n * React hooks for AgentShield in Next.js applications\n */\n\nimport { useState, useCallback } from 'react';\nimport { AgentDetector } from '@kya-os/agentshield';\nimport type { DetectionResult, AgentShieldConfig } from '@kya-os/agentshield';\nimport type { DetectionContext } from './types';\n\n/**\n * Hook for client-side agent detection\n */\nexport function useAgentDetection(config: Partial<AgentShieldConfig> = {}) {\n const [detector] = useState(() => new AgentDetector(config));\n const [isDetecting, setIsDetecting] = useState(false);\n const [lastResult, setLastResult] = useState<DetectionResult | null>(null);\n\n const detect = useCallback(async () => {\n setIsDetecting(true);\n try {\n const context = {\n userAgent: navigator.userAgent,\n headers: {}, // Client-side headers are limited\n timestamp: new Date(),\n };\n\n const result = await detector.analyze(context);\n setLastResult(result);\n return result;\n } finally {\n setIsDetecting(false);\n }\n }, [detector]);\n\n return {\n detect,\n isDetecting,\n lastResult,\n detector,\n };\n}\n\n/**\n * Hook for monitoring detection results\n */\nexport function useDetectionMonitor(\n onDetection?: (context: DetectionContext) => void\n) {\n const [detectionHistory, setDetectionHistory] = useState<DetectionResult[]>(\n []\n );\n\n const addDetection = useCallback(\n (result: DetectionResult, request?: any) => {\n setDetectionHistory(prev => [...prev, result].slice(-100)); // Keep last 100\n\n if (onDetection && request) {\n onDetection({\n result,\n request,\n userAgent: request.headers?.get?.('user-agent'),\n ip: request.ip,\n });\n }\n },\n [onDetection]\n );\n\n const clearHistory = useCallback(() => {\n setDetectionHistory([]);\n }, []);\n\n const getStats = useCallback(() => {\n const total = detectionHistory.length;\n const detected = detectionHistory.filter(r => r.isAgent).length;\n const avgConfidence =\n total > 0\n ? detectionHistory.reduce((sum, r) => sum + r.confidence, 0) / total\n : 0;\n\n return {\n total,\n detected,\n avgConfidence,\n detectionRate: total > 0 ? detected / total : 0,\n };\n }, [detectionHistory]);\n\n return {\n detectionHistory,\n addDetection,\n clearHistory,\n getStats,\n };\n}\n","/**\n * @fileoverview AgentShield Next.js Integration\n * @version 0.1.0\n * @license MIT OR Apache-2.0\n */\n\nexport * from './middleware';\nexport * from './types';\nexport * from './hooks';\n\n/**\n * Library version\n */\nexport const VERSION = '0.1.0';\n"]}
1
+ {"version":3,"sources":["../src/middleware.ts","../src/index.ts"],"names":["AgentDetector","NextResponse","response"],"mappings":";;;;;;AAWO,SAAS,2BAAA,CACd,MAAA,GAA0C,EAAC,EAC3C;AACA,EAAA,MAAM,QAAA,GAAW,IAAIA,yBAAA,CAAc,MAAM,CAAA;AAGzC,EAAA,IAAI,OAAO,MAAA,EAAQ;AACjB,IAAA,MAAA,CAAO,OAAA,CAAQ,OAAO,MAAM,CAAA,CAAE,QAAQ,CAAC,CAAC,KAAA,EAAO,OAAO,CAAA,KAAM;AAC1D,MAAA,QAAA,CAAS,EAAA,CAAG,OAAc,OAAc,CAAA;AAAA,IAC1C,CAAC,CAAA;AAAA,EACH;AAEA,EAAA,MAAM;AAAA,IACJ,eAAA,GAAkB,KAAA;AAAA,IAClB,WAAA;AAAA,IACA,YAAY,EAAC;AAAA,IACb,eAAA,GAAkB;AAAA,MAChB,MAAA,EAAQ,GAAA;AAAA,MACR,OAAA,EAAS,yCAAA;AAAA,MACT,OAAA,EAAS,EAAE,cAAA,EAAgB,kBAAA;AAAmB,KAChD;AAAA,IACA,WAAA,GAAc,UAAA;AAAA,IACd,UAAA,GAAa;AAAA,GACf,GAAI,MAAA;AAEJ,EAAA,OAAO,OAAO,OAAA,KAAgD;AAC5D,IAAA,IAAI;AAEF,MAAA,MAAM,UAAA,GAAa,SAAA,CAAU,IAAA,CAAK,CAAA,OAAA,KAAW;AAC3C,QAAA,IAAI,OAAO,YAAY,QAAA,EAAU;AAC/B,UAAA,OAAO,OAAA,CAAQ,OAAA,CAAQ,QAAA,CAAS,UAAA,CAAW,OAAO,CAAA;AAAA,QACpD;AACA,QAAA,OAAO,OAAA,CAAQ,IAAA,CAAK,OAAA,CAAQ,OAAA,CAAQ,QAAQ,CAAA;AAAA,MAC9C,CAAC,CAAA;AAED,MAAA,IAA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AA,QACA,OAAA,EAAS;AAAA,OACX;AAGA,MAAA,MAAM,QAAA,GAAWA,oBAAa,IAAA,EAAK;AACnC,MAAA,QAAA,CAAS,QAAQ,GAAA,CAAI,wBAAA,EAA0B,MAAA,CAAO,OAAA,CAAQ,UAAU,CAAA;AACxE,MAAA,QAAA,CAAS,OAAA,CAAQ,GAAA;AAAA,QACf,0BAAA;AAAA,QACA,MAAA,CAAO,WAAW,QAAA;AAAS,OAC7B;AAEA,MAAA,OAAO,QAAA;AAAA,IACT,SAAS,KAAA,EAAO;AACd,MAAA,OAAA,CAAQ,KAAA,CAAM,iCAAiC,KAAK,CAAA;AACpD,MAAA,OAAOA,oBAAa,IAAA,EAAK;AAAA,IAC3B;AAAA,EACF,CAAA;AACF;AAKO,SAAS,WAAA,CAAY,MAAA,GAA0C,EAAC,EAAG;AACxE,EAAA,OAAO,4BAA4B,MAAM,CAAA;AAC3C;;;ACjJO,IAAM,OAAA,GAAU","file":"index.js","sourcesContent":["/**\n * Next.js middleware for AgentShield\n */\n\nimport { NextRequest, NextResponse } from 'next/server';\nimport { AgentDetector } from '@kya-os/agentshield';\nimport type { NextJSMiddlewareConfig } from './types';\n\n/**\n * Create AgentShield middleware for Next.js\n */\nexport function createAgentShieldMiddleware(\n config: Partial<NextJSMiddlewareConfig> = {}\n) {\n const detector = new AgentDetector(config);\n \n // Wire up event handlers if provided\n if (config.events) {\n Object.entries(config.events).forEach(([event, handler]) => {\n detector.on(event as any, handler as any);\n });\n }\n\n const {\n onAgentDetected = 'log',\n onDetection,\n skipPaths = [],\n blockedResponse = {\n status: 403,\n message: 'Access denied: Automated agent detected',\n headers: { 'Content-Type': 'application/json' },\n },\n redirectUrl = '/blocked',\n rewriteUrl = '/blocked',\n } = config;\n\n return async (request: NextRequest): Promise<NextResponse> => {\n try {\n // Check if path should be skipped\n const shouldSkip = skipPaths.some(pattern => {\n if (typeof pattern === 'string') {\n return request.nextUrl.pathname.startsWith(pattern);\n }\n return pattern.test(request.nextUrl.pathname);\n });\n\n if (shouldSkip) {\n // Mark as skipped in request\n (request as any).agentShield = { skipped: true };\n return NextResponse.next();\n }\n\n // Prepare request context\n const context = {\n userAgent: request.headers.get('user-agent') ?? undefined,\n ipAddress: request.ip ?? request.headers.get('x-forwarded-for') ?? undefined,\n headers: Object.fromEntries(request.headers.entries()),\n url: request.url,\n method: request.method,\n timestamp: new Date(),\n };\n\n // Analyze request\n const result = await detector.analyze(context);\n\n // Handle detection result\n if (\n result.isAgent &&\n result.confidence >= (config.confidenceThreshold ?? 0.7)\n ) {\n // Call custom detection handler if provided\n if (onDetection) {\n const customResponse = await onDetection(request, result);\n if (customResponse) {\n return customResponse;\n }\n }\n\n // Handle based on configuration\n switch (onAgentDetected) {\n case 'block': {\n const response = NextResponse.json(\n {\n error: blockedResponse.message,\n detected: true,\n confidence: result.confidence,\n timestamp: result.timestamp,\n },\n { status: blockedResponse.status }\n );\n\n if (blockedResponse.headers) {\n Object.entries(blockedResponse.headers).forEach(\n ([key, value]) => {\n response.headers.set(key, value);\n }\n );\n }\n \n // Emit blocked event\n detector.emit('agent.blocked', result, context);\n\n return response;\n }\n\n case 'redirect':\n return NextResponse.redirect(new URL(redirectUrl, request.url));\n\n case 'rewrite':\n return NextResponse.rewrite(new URL(rewriteUrl, request.url));\n\n case 'log':\n console.warn('AgentShield: Agent detected', {\n ipAddress: context.ipAddress,\n userAgent: context.userAgent,\n confidence: result.confidence,\n reasons: result.reasons,\n pathname: request.nextUrl.pathname,\n });\n break;\n\n case 'allow':\n default:\n // Emit allowed event for high-confidence agents\n if (result.isAgent && result.confidence >= (config.confidenceThreshold ?? 0.7)) {\n detector.emit('agent.allowed', result, context);\n }\n // Continue processing\n break;\n }\n }\n\n // Add detection result to request for API routes\n (request as any).agentShield = {\n result,\n skipped: false,\n };\n\n // Add detection result to response headers for debugging\n const response = NextResponse.next();\n response.headers.set('x-agentshield-detected', result.isAgent.toString());\n response.headers.set(\n 'x-agentshield-confidence',\n result.confidence.toString()\n );\n\n return response;\n } catch (error) {\n console.error('AgentShield middleware error:', error);\n return NextResponse.next(); // Continue on error\n }\n };\n}\n\n/**\n * Convenience function for basic setup\n */\nexport function agentShield(config: Partial<NextJSMiddlewareConfig> = {}) {\n return createAgentShieldMiddleware(config);\n}\n","/**\n * @fileoverview AgentShield Next.js Integration\n * @version 0.1.0\n * @license MIT OR Apache-2.0\n */\n\nexport * from './middleware';\nexport * from './types';\n// Hooks are not exported from main entry - they're client-only\n// If someone needs hooks, they should import from '@kya-os/agentshield-nextjs/hooks'\n\n/**\n * Library version\n */\nexport const VERSION = '0.1.0';\n"]}
package/dist/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  import { NextResponse } from 'next/server';
2
2
  import { AgentDetector } from '@kya-os/agentshield';
3
- import { useState, useCallback } from 'react';
4
3
 
5
4
  // src/middleware.ts
6
5
  function createAgentShieldMiddleware(config = {}) {
@@ -112,72 +111,6 @@ function createAgentShieldMiddleware(config = {}) {
112
111
  function agentShield(config = {}) {
113
112
  return createAgentShieldMiddleware(config);
114
113
  }
115
- function useAgentDetection(config = {}) {
116
- const [detector] = useState(() => new AgentDetector(config));
117
- const [isDetecting, setIsDetecting] = useState(false);
118
- const [lastResult, setLastResult] = useState(null);
119
- const detect = useCallback(async () => {
120
- setIsDetecting(true);
121
- try {
122
- const context = {
123
- userAgent: navigator.userAgent,
124
- headers: {},
125
- // Client-side headers are limited
126
- timestamp: /* @__PURE__ */ new Date()
127
- };
128
- const result = await detector.analyze(context);
129
- setLastResult(result);
130
- return result;
131
- } finally {
132
- setIsDetecting(false);
133
- }
134
- }, [detector]);
135
- return {
136
- detect,
137
- isDetecting,
138
- lastResult,
139
- detector
140
- };
141
- }
142
- function useDetectionMonitor(onDetection) {
143
- const [detectionHistory, setDetectionHistory] = useState(
144
- []
145
- );
146
- const addDetection = useCallback(
147
- (result, request) => {
148
- setDetectionHistory((prev) => [...prev, result].slice(-100));
149
- if (onDetection && request) {
150
- onDetection({
151
- result,
152
- request,
153
- userAgent: request.headers?.get?.("user-agent"),
154
- ip: request.ip
155
- });
156
- }
157
- },
158
- [onDetection]
159
- );
160
- const clearHistory = useCallback(() => {
161
- setDetectionHistory([]);
162
- }, []);
163
- const getStats = useCallback(() => {
164
- const total = detectionHistory.length;
165
- const detected = detectionHistory.filter((r) => r.isAgent).length;
166
- const avgConfidence = total > 0 ? detectionHistory.reduce((sum, r) => sum + r.confidence, 0) / total : 0;
167
- return {
168
- total,
169
- detected,
170
- avgConfidence,
171
- detectionRate: total > 0 ? detected / total : 0
172
- };
173
- }, [detectionHistory]);
174
- return {
175
- detectionHistory,
176
- addDetection,
177
- clearHistory,
178
- getStats
179
- };
180
- }
181
114
 
182
115
  // src/index.ts
183
116
  var VERSION = "0.1.0";
@@ -187,6 +120,6 @@ var VERSION = "0.1.0";
187
120
  * @license MIT OR Apache-2.0
188
121
  */
189
122
 
190
- export { VERSION, agentShield, createAgentShieldMiddleware, useAgentDetection, useDetectionMonitor };
123
+ export { VERSION, agentShield, createAgentShieldMiddleware };
191
124
  //# sourceMappingURL=index.mjs.map
192
125
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/middleware.ts","../src/hooks.ts","../src/index.ts"],"names":["response","AgentDetector"],"mappings":";;;;;AAWO,SAAS,2BAAA,CACd,MAAA,GAA0C,EAAC,EAC3C;AACA,EAAA,MAAM,QAAA,GAAW,IAAI,aAAA,CAAc,MAAM,CAAA;AAGzC,EAAA,IAAI,OAAO,MAAA,EAAQ;AACjB,IAAA,MAAA,CAAO,OAAA,CAAQ,OAAO,MAAM,CAAA,CAAE,QAAQ,CAAC,CAAC,KAAA,EAAO,OAAO,CAAA,KAAM;AAC1D,MAAA,QAAA,CAAS,EAAA,CAAG,OAAc,OAAc,CAAA;AAAA,IAC1C,CAAC,CAAA;AAAA,EACH;AAEA,EAAA,MAAM;AAAA,IACJ,eAAA,GAAkB,KAAA;AAAA,IAClB,WAAA;AAAA,IACA,YAAY,EAAC;A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EAAW,OAAA,CAAQ,OAAA,EAAS,GAAA,GAAM,YAAY,CAAA;AAAA,UAC9C,IAAI,OAAA,CAAQ;AAAA,SACb,CAAA;AAAA,MACH;AAAA,IACF,CAAA;AAAA,IACA,CAAC,WAAW;AAAA,GACd;AAEA,EAAA,MAAM,YAAA,GAAe,YAAY,MAAM;AACrC,IAAA,mBAAA,CAAoB,EAAE,CAAA;AAAA,EACxB,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,MAAM,QAAA,GAAW,YAAY,MAAM;AACjC,IAAA,MAAM,QAAQ,gBAAA,CAAiB,MAAA;AAC/B,IAAA,MAAM,WAAW,gBAAA,CAAiB,MAAA,CAAO,CAAA,CAAA,KAAK,CAAA,CAAE,OAAO,CAAA,CAAE,MAAA;AACzD,IAAA,MAAM,aAAA,GACJ,KAAA,GAAQ,CAAA,GACJ,gBAAA,CAAiB,MAAA,CAAO,CAAC,GAAA,EAAK,CAAA,KAAM,GAAA,GAAM,CAAA,CAAE,UAAA,EAAY,CAAC,IAAI,KAAA,GAC7D,CAAA;AAEN,IAAA,OAAO;AAAA,MACL,KAAA;AAAA,MACA,QAAA;AAAA,MACA,aAAA;AAAA,MACA,aAAA,EAAe,KAAA,GAAQ,CAAA,GAAI,QAAA,GAAW,KAAA,GAAQ;AAAA,KAChD;AAAA,EACF,CAAA,EAAG,CAAC,gBAAgB,CAAC,CAAA;AAErB,EAAA,OAAO;AAAA,IACL,gBAAA;AAAA,IACA,YAAA;AAAA,IACA,YAAA;AAAA,IACA;AAAA,GACF;AACF;;;ACnFO,IAAM,OAAA,GAAU","file":"index.mjs","sourcesContent":["/**\n * Next.js middleware for AgentShield\n */\n\nimport { NextRequest, NextResponse } from 'next/server';\nimport { AgentDetector } from '@kya-os/agentshield';\nimport type { NextJSMiddlewareConfig } from './types';\n\n/**\n * Create AgentShield middleware for Next.js\n */\nexport function createAgentShieldMiddleware(\n config: Partial<NextJSMiddlewareConfig> = {}\n) {\n const detector = new AgentDetector(config);\n \n // Wire up event handlers if provided\n if (config.events) {\n Object.entries(config.events).forEach(([event, handler]) => {\n detector.on(event as any, handler as any);\n });\n }\n\n const {\n onAgentDetected = 'log',\n onDetection,\n skipPaths = [],\n blockedResponse = {\n status: 403,\n message: 'Access denied: Automated agent detected',\n headers: { 'Content-Type': 'application/json' },\n },\n redirectUrl = '/blocked',\n rewriteUrl = '/blocked',\n } = config;\n\n return async (request: NextRequest): Promise<NextResponse> => {\n try {\n // Check if path should be skipped\n const shouldSkip = skipPaths.some(pattern => {\n if (typeof pattern === 'string') {\n return request.nextUrl.pathname.startsWith(pattern);\n }\n return pattern.test(request.nextUrl.pathname);\n });\n\n if (shouldSkip) {\n // Mark as skipped in request\n (request as any).agentShield = { skipped: true };\n return NextResponse.next();\n }\n\n // Prepare request context\n const context = {\n userAgent: request.headers.get('user-agent') ?? undefined,\n ipAddress: request.ip ?? request.headers.get('x-forwarded-for') ?? undefined,\n headers: Object.fromEntries(request.headers.entries()),\n url: request.url,\n method: request.method,\n timestamp: new Date(),\n };\n\n // Analyze request\n const result = await detector.analyze(context);\n\n // Handle detection result\n if (\n result.isAgent &&\n result.confidence >= (config.confidenceThreshold ?? 0.7)\n ) {\n // Call custom detection handler if provided\n if (onDetection) {\n const customResponse = await onDetection(request, result);\n if (customResponse) {\n return customResponse;\n }\n }\n\n // Handle based on configuration\n switch (onAgentDetected) {\n case 'block': {\n const response = NextResponse.json(\n {\n error: blockedResponse.message,\n detected: true,\n confidence: result.confidence,\n timestamp: result.timestamp,\n },\n { status: blockedResponse.status }\n );\n\n if (blockedResponse.headers) {\n Object.entries(blockedResponse.headers).forEach(\n ([key, value]) => {\n response.headers.set(key, value);\n }\n );\n }\n \n // Emit blocked event\n detector.emit('agent.blocked', result, context);\n\n return response;\n }\n\n case 'redirect':\n return NextResponse.redirect(new URL(redirectUrl, request.url));\n\n case 'rewrite':\n return NextResponse.rewrite(new URL(rewriteUrl, request.url));\n\n case 'log':\n console.warn('AgentShield: Agent detected', {\n ipAddress: context.ipAddress,\n userAgent: context.userAgent,\n confidence: result.confidence,\n reasons: result.reasons,\n pathname: request.nextUrl.pathname,\n });\n break;\n\n case 'allow':\n default:\n // Emit allowed event for high-confidence agents\n if (result.isAgent && result.confidence >= (config.confidenceThreshold ?? 0.7)) {\n detector.emit('agent.allowed', result, context);\n }\n // Continue processing\n break;\n }\n }\n\n // Add detection result to request for API routes\n (request as any).agentShield = {\n result,\n skipped: false,\n };\n\n // Add detection result to response headers for debugging\n const response = NextResponse.next();\n response.headers.set('x-agentshield-detected', result.isAgent.toString());\n response.headers.set(\n 'x-agentshield-confidence',\n result.confidence.toString()\n );\n\n return response;\n } catch (error) {\n console.error('AgentShield middleware error:', error);\n return NextResponse.next(); // Continue on error\n }\n };\n}\n\n/**\n * Convenience function for basic setup\n */\nexport function agentShield(config: Partial<NextJSMiddlewareConfig> = {}) {\n return createAgentShieldMiddleware(config);\n}\n","'use client';\n\n/**\n * React hooks for AgentShield in Next.js applications\n */\n\nimport { useState, useCallback } from 'react';\nimport { AgentDetector } from '@kya-os/agentshield';\nimport type { DetectionResult, AgentShieldConfig } from '@kya-os/agentshield';\nimport type { DetectionContext } from './types';\n\n/**\n * Hook for client-side agent detection\n */\nexport function useAgentDetection(config: Partial<AgentShieldConfig> = {}) {\n const [detector] = useState(() => new AgentDetector(config));\n const [isDetecting, setIsDetecting] = useState(false);\n const [lastResult, setLastResult] = useState<DetectionResult | null>(null);\n\n const detect = useCallback(async () => {\n setIsDetecting(true);\n try {\n const context = {\n userAgent: navigator.userAgent,\n headers: {}, // Client-side headers are limited\n timestamp: new Date(),\n };\n\n const result = await detector.analyze(context);\n setLastResult(result);\n return result;\n } finally {\n setIsDetecting(false);\n }\n }, [detector]);\n\n return {\n detect,\n isDetecting,\n lastResult,\n detector,\n };\n}\n\n/**\n * Hook for monitoring detection results\n */\nexport function useDetectionMonitor(\n onDetection?: (context: DetectionContext) => void\n) {\n const [detectionHistory, setDetectionHistory] = useState<DetectionResult[]>(\n []\n );\n\n const addDetection = useCallback(\n (result: DetectionResult, request?: any) => {\n setDetectionHistory(prev => [...prev, result].slice(-100)); // Keep last 100\n\n if (onDetection && request) {\n onDetection({\n result,\n request,\n userAgent: request.headers?.get?.('user-agent'),\n ip: request.ip,\n });\n }\n },\n [onDetection]\n );\n\n const clearHistory = useCallback(() => {\n setDetectionHistory([]);\n }, []);\n\n const getStats = useCallback(() => {\n const total = detectionHistory.length;\n const detected = detectionHistory.filter(r => r.isAgent).length;\n const avgConfidence =\n total > 0\n ? detectionHistory.reduce((sum, r) => sum + r.confidence, 0) / total\n : 0;\n\n return {\n total,\n detected,\n avgConfidence,\n detectionRate: total > 0 ? detected / total : 0,\n };\n }, [detectionHistory]);\n\n return {\n detectionHistory,\n addDetection,\n clearHistory,\n getStats,\n };\n}\n","/**\n * @fileoverview AgentShield Next.js Integration\n * @version 0.1.0\n * @license MIT OR Apache-2.0\n */\n\nexport * from './middleware';\nexport * from './types';\nexport * from './hooks';\n\n/**\n * Library version\n */\nexport const VERSION = '0.1.0';\n"]}
1
+ {"version":3,"sources":["../src/middleware.ts","../src/index.ts"],"names":["response"],"mappings":";;;;AAWO,SAAS,2BAAA,CACd,MAAA,GAA0C,EAAC,EAC3C;AACA,EAAA,MAAM,QAAA,GAAW,IAAI,aAAA,CAAc,MAAM,CAAA;AAGzC,EAAA,IAAI,OAAO,MAAA,EAAQ;AACjB,IAAA,MAAA,CAAO,OAAA,CAAQ,OAAO,MAAM,CAAA,CAAE,QAAQ,CAAC,CAAC,KAAA,EAAO,OAAO,CAAA,KAAM;AAC1D,MAAA,QAAA,CAAS,EAAA,CAAG,OAAc,OAAc,CAAA;AAAA,IAC1C,CAAC,CAAA;AAAA,EACH;AAEA,EAAA,MAAM;AAAA,IACJ,eAAA,GAAkB,KAAA;AAAA,IAClB,WAAA;AAAA,IACA,YAAY,EAAC;AAAA,IACb,eAAA,GAAkB;AAAA,MAChB,MAAA,EAAQ,GAAA;AAAA,MACR,OAAA,EAAS,yCAAA;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1C,WAAW,OAAA,CAAQ,SAAA;AAAA,cACnB,WAAW,OAAA,CAAQ,SAAA;AAAA,cACnB,YAAY,MAAA,CAAO,UAAA;AAAA,cACnB,SAAS,MAAA,CAAO,OAAA;AAAA,cAChB,QAAA,EAAU,QAAQ,OAAA,CAAQ;AAAA,aAC3B,CAAA;AACD,YAAA;AAAA,UAEF,KAAK,OAAA;AAAA,UACL;AAEE,YAAA,IAAI,OAAO,OAAA,IAAW,MAAA,CAAO,UAAA,KAAe,MAAA,CAAO,uBAAuB,GAAA,CAAA,EAAM;AAC9E,cAAA,QAAA,CAAS,IAAA,CAAK,eAAA,EAAiB,MAAA,EAAQ,OAAO,CAAA;AAAA,YAChD;AAEA,YAAA;AAAA;AACJ,MACF;AAGA,MAAC,QAAgB,WAAA,GAAc;AAAA,QAC7B,MAAA;AAAA,QACA,OAAA,EAAS;AAAA,OACX;AAGA,MAAA,MAAM,QAAA,GAAW,aAAa,IAAA,EAAK;AACnC,MAAA,QAAA,CAAS,QAAQ,GAAA,CAAI,wBAAA,EAA0B,MAAA,CAAO,OAAA,CAAQ,UAAU,CAAA;AACxE,MAAA,QAAA,CAAS,OAAA,CAAQ,GAAA;AAAA,QACf,0BAAA;AAAA,QACA,MAAA,CAAO,WAAW,QAAA;AAAS,OAC7B;AAEA,MAAA,OAAO,QAAA;AAAA,IACT,SAAS,KAAA,EAAO;AACd,MAAA,OAAA,CAAQ,KAAA,CAAM,iCAAiC,KAAK,CAAA;AACpD,MAAA,OAAO,aAAa,IAAA,EAAK;AAAA,IAC3B;AAAA,EACF,CAAA;AACF;AAKO,SAAS,WAAA,CAAY,MAAA,GAA0C,EAAC,EAAG;AACxE,EAAA,OAAO,4BAA4B,MAAM,CAAA;AAC3C;;;ACjJO,IAAM,OAAA,GAAU","file":"index.mjs","sourcesContent":["/**\n * Next.js middleware for AgentShield\n */\n\nimport { NextRequest, NextResponse } from 'next/server';\nimport { AgentDetector } from '@kya-os/agentshield';\nimport type { NextJSMiddlewareConfig } from './types';\n\n/**\n * Create AgentShield middleware for Next.js\n */\nexport function createAgentShieldMiddleware(\n config: Partial<NextJSMiddlewareConfig> = {}\n) {\n const detector = new AgentDetector(config);\n \n // Wire up event handlers if provided\n if (config.events) {\n Object.entries(config.events).forEach(([event, handler]) => {\n detector.on(event as any, handler as any);\n });\n }\n\n const {\n onAgentDetected = 'log',\n onDetection,\n skipPaths = [],\n blockedResponse = {\n status: 403,\n message: 'Access denied: Automated agent detected',\n headers: { 'Content-Type': 'application/json' },\n },\n redirectUrl = '/blocked',\n rewriteUrl = '/blocked',\n } = config;\n\n return async (request: NextRequest): Promise<NextResponse> => {\n try {\n // Check if path should be skipped\n const shouldSkip = skipPaths.some(pattern => {\n if (typeof pattern === 'string') {\n return request.nextUrl.pathname.startsWith(pattern);\n }\n return pattern.test(request.nextUrl.pathname);\n });\n\n if (shouldSkip) {\n // Mark as skipped in request\n (request as any).agentShield = { skipped: true };\n return NextResponse.next();\n }\n\n // Prepare request context\n const context = {\n userAgent: request.headers.get('user-agent') ?? undefined,\n ipAddress: request.ip ?? request.headers.get('x-forwarded-for') ?? undefined,\n headers: Object.fromEntries(request.headers.entries()),\n url: request.url,\n method: request.method,\n timestamp: new Date(),\n };\n\n // Analyze request\n const result = await detector.analyze(context);\n\n // Handle detection result\n if (\n result.isAgent &&\n result.confidence >= (config.confidenceThreshold ?? 0.7)\n ) {\n // Call custom detection handler if provided\n if (onDetection) {\n const customResponse = await onDetection(request, result);\n if (customResponse) {\n return customResponse;\n }\n }\n\n // Handle based on configuration\n switch (onAgentDetected) {\n case 'block': {\n const response = NextResponse.json(\n {\n error: blockedResponse.message,\n detected: true,\n confidence: result.confidence,\n timestamp: result.timestamp,\n },\n { status: blockedResponse.status }\n );\n\n if (blockedResponse.headers) {\n Object.entries(blockedResponse.headers).forEach(\n ([key, value]) => {\n response.headers.set(key, value);\n }\n );\n }\n \n // Emit blocked event\n detector.emit('agent.blocked', result, context);\n\n return response;\n }\n\n case 'redirect':\n return NextResponse.redirect(new URL(redirectUrl, request.url));\n\n case 'rewrite':\n return NextResponse.rewrite(new URL(rewriteUrl, request.url));\n\n case 'log':\n console.warn('AgentShield: Agent detected', {\n ipAddress: context.ipAddress,\n userAgent: context.userAgent,\n confidence: result.confidence,\n reasons: result.reasons,\n pathname: request.nextUrl.pathname,\n });\n break;\n\n case 'allow':\n default:\n // Emit allowed event for high-confidence agents\n if (result.isAgent && result.confidence >= (config.confidenceThreshold ?? 0.7)) {\n detector.emit('agent.allowed', result, context);\n }\n // Continue processing\n break;\n }\n }\n\n // Add detection result to request for API routes\n (request as any).agentShield = {\n result,\n skipped: false,\n };\n\n // Add detection result to response headers for debugging\n const response = NextResponse.next();\n response.headers.set('x-agentshield-detected', result.isAgent.toString());\n response.headers.set(\n 'x-agentshield-confidence',\n result.confidence.toString()\n );\n\n return response;\n } catch (error) {\n console.error('AgentShield middleware error:', error);\n return NextResponse.next(); // Continue on error\n }\n };\n}\n\n/**\n * Convenience function for basic setup\n */\nexport function agentShield(config: Partial<NextJSMiddlewareConfig> = {}) {\n return createAgentShieldMiddleware(config);\n}\n","/**\n * @fileoverview AgentShield Next.js Integration\n * @version 0.1.0\n * @license MIT OR Apache-2.0\n */\n\nexport * from './middleware';\nexport * from './types';\n// Hooks are not exported from main entry - they're client-only\n// If someone needs hooks, they should import from '@kya-os/agentshield-nextjs/hooks'\n\n/**\n * Library version\n */\nexport const VERSION = '0.1.0';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kya-os/agentshield-nextjs",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "Next.js middleware for AgentShield AI agent detection",
5
5
  "keywords": [
6
6
  "nextjs",
@@ -85,6 +85,6 @@
85
85
  },
86
86
  "sideEffects": false,
87
87
  "dependencies": {
88
- "@kya-os/agentshield": "^0.1.9"
88
+ "@kya-os/agentshield": "^0.1.10"
89
89
  }
90
90
  }