@kya-os/agentshield-nextjs 0.2.9 → 0.2.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.mjs CHANGED
@@ -2065,6 +2065,69 @@ var AgentShieldClient = class {
2065
2065
  action: result.data.decision.action
2066
2066
  };
2067
2067
  }
2068
+ /**
2069
+ * Check if this client is using edge detection (Gateway Worker)
2070
+ */
2071
+ isUsingEdge() {
2072
+ return this.useEdge;
2073
+ }
2074
+ /**
2075
+ * Log a detection result to AgentShield database.
2076
+ * Use after Gateway Worker detection to persist results.
2077
+ * Fire-and-forget - returns immediately without waiting for DB write.
2078
+ *
2079
+ * @example
2080
+ * ```typescript
2081
+ * // After receiving Gateway response
2082
+ * if (client.isUsingEdge() && response.data?.detection) {
2083
+ * client.logDetection({
2084
+ * detection: response.data.detection,
2085
+ * context: { userAgent, ipAddress, path, url, method }
2086
+ * }).catch(err => console.error('Log failed:', err));
2087
+ * }
2088
+ * ```
2089
+ */
2090
+ async logDetection(input) {
2091
+ const logEndpoint = this.useEdge ? `${DEFAULT_BASE_URL}/api/v1/log-detection` : `${this.baseUrl}/api/v1/log-detection`;
2092
+ try {
2093
+ const controller = new AbortController();
2094
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
2095
+ try {
2096
+ const response = await fetch(logEndpoint, {
2097
+ method: "POST",
2098
+ headers: {
2099
+ "Content-Type": "application/json",
2100
+ Authorization: `Bearer ${this.apiKey}`
2101
+ },
2102
+ body: JSON.stringify({
2103
+ detection: {
2104
+ isAgent: input.detection.isAgent,
2105
+ confidence: input.detection.confidence,
2106
+ agentName: input.detection.agentName,
2107
+ agentType: input.detection.agentType,
2108
+ verificationMethod: input.detection.verificationMethod,
2109
+ reasons: input.detection.reasons
2110
+ },
2111
+ context: input.context,
2112
+ source: input.source || "gateway"
2113
+ }),
2114
+ signal: controller.signal
2115
+ });
2116
+ clearTimeout(timeoutId);
2117
+ if (!response.ok && this.debug) {
2118
+ console.warn("[AgentShield] Log detection returned non-2xx:", response.status);
2119
+ }
2120
+ } catch (error) {
2121
+ clearTimeout(timeoutId);
2122
+ throw error;
2123
+ }
2124
+ } catch (error) {
2125
+ if (this.debug) {
2126
+ console.error("[AgentShield] Log detection failed:", error);
2127
+ }
2128
+ throw error;
2129
+ }
2130
+ }
2068
2131
  };
2069
2132
  var clientInstance = null;
2070
2133
  function getAgentShieldClient(config) {
@@ -2172,16 +2235,20 @@ function withAgentShield(config = {}) {
2172
2235
  return NextResponse.next();
2173
2236
  }
2174
2237
  try {
2175
- const result = await getClient().enforce({
2238
+ const client2 = getClient();
2239
+ const userAgent = request.headers.get("user-agent") || void 0;
2240
+ const ipAddress = request.ip || request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || request.headers.get("x-real-ip") || void 0;
2241
+ const result = await client2.enforce({
2176
2242
  headers: Object.fromEntries(request.headers.entries()),
2177
- userAgent: request.headers.get("user-agent") || void 0,
2178
- ipAddress: request.ip || request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || request.headers.get("x-real-ip") || void 0,
2243
+ userAgent,
2244
+ ipAddress,
2179
2245
  path,
2180
2246
  url: request.url,
2181
2247
  method: request.method,
2182
2248
  requestId: request.headers.get("x-request-id") || void 0,
2183
2249
  options: {
2184
- includeDetectionResult: config.debug
2250
+ // Always include detection results for logging (needed when using edge)
2251
+ includeDetectionResult: true
2185
2252
  }
2186
2253
  });
2187
2254
  if (!result.success || !result.data) {
@@ -2208,6 +2275,16 @@ function withAgentShield(config = {}) {
2208
2275
  processingTimeMs: Date.now() - startTime
2209
2276
  });
2210
2277
  }
2278
+ if (client2.isUsingEdge() && result.data.detection) {
2279
+ client2.logDetection({
2280
+ detection: result.data.detection,
2281
+ context: { userAgent, ipAddress, path, url: request.url, method: request.method }
2282
+ }).catch((err) => {
2283
+ if (config.debug) {
2284
+ console.error("[AgentShield] Log detection failed:", err);
2285
+ }
2286
+ });
2287
+ }
2211
2288
  if (decision.isAgent && config.onAgentDetected) {
2212
2289
  await config.onAgentDetected(request, decision);
2213
2290
  }