@kya-os/checkpoint-nextjs 1.1.1 → 1.1.4

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +143 -0
  2. package/EDGE_RUNTIME_WASM_SETUP.md +4 -10
  3. package/README.md +13 -0
  4. package/bin/setup-edge-wasm.js +40 -32
  5. package/dist/api-client.d.mts +10 -10
  6. package/dist/api-client.d.ts +10 -10
  7. package/dist/create-middleware.d.mts +7 -2
  8. package/dist/create-middleware.d.ts +7 -2
  9. package/dist/edge/index.d.mts +3 -3
  10. package/dist/edge/index.d.ts +3 -3
  11. package/dist/edge/index.js +16 -3
  12. package/dist/edge/index.mjs +16 -3
  13. package/dist/edge-runtime-loader.d.mts +17 -28
  14. package/dist/edge-runtime-loader.d.ts +17 -28
  15. package/dist/edge-runtime-loader.js +43 -14
  16. package/dist/edge-runtime-loader.mjs +44 -15
  17. package/dist/edge-wasm-middleware.d.mts +28 -34
  18. package/dist/edge-wasm-middleware.d.ts +28 -34
  19. package/dist/edge-wasm-middleware.js +16 -306
  20. package/dist/edge-wasm-middleware.mjs +16 -307
  21. package/dist/index.js +3 -1
  22. package/dist/index.mjs +4 -2
  23. package/dist/nodejs-wasm-loader.d.mts +26 -9
  24. package/dist/nodejs-wasm-loader.d.ts +26 -9
  25. package/dist/nodejs-wasm-loader.js +21 -78
  26. package/dist/nodejs-wasm-loader.mjs +21 -74
  27. package/dist/session-tracker.d.mts +2 -2
  28. package/dist/session-tracker.d.ts +2 -2
  29. package/dist/session-tracker.js +3 -1
  30. package/dist/session-tracker.mjs +4 -2
  31. package/dist/wasm-middleware.d.mts +19 -3
  32. package/dist/wasm-middleware.d.ts +19 -3
  33. package/dist/wasm-middleware.js +32 -3
  34. package/dist/wasm-middleware.mjs +32 -4
  35. package/dist/wasm-setup.js +29 -81
  36. package/dist/wasm-setup.mjs +29 -76
  37. package/package.json +4 -4
  38. package/templates/middleware-wasm-100.ts +11 -3
  39. package/dist/.tsbuildinfo +0 -1
@@ -1,25 +1,42 @@
1
1
  /**
2
- * Node.js Runtime WASM Loader for AgentShield
2
+ * @deprecated Phase-D.9a — legacy Node.js WASM loader for the retired
3
+ * `agentshield-wasm` Rust crate. This file used `fs.readFileSync` to
4
+ * locate + load the legacy detector's WASM binary into the
5
+ * `@kya-os/checkpoint` `AgentDetector` class via `setWasmModule`. Both
6
+ * the WASM crate (Phase-D.9a/D.9b) and the AgentDetector class
7
+ * (AgentDetector-Deletion-2, next minor) are slated for deletion.
3
8
  *
4
- * This loader uses fs.readFileSync to load WASM in Node.js runtime.
5
- * It provides full cryptographic verification capabilities.
9
+ * Migrate to `withCheckpoint` from `@kya-os/checkpoint-nextjs` it
10
+ * loads the canonical `kya-os-engine` WASM automatically via
11
+ * `@kya-os/checkpoint-wasm-runtime`'s loaders. No manual `fs.readFileSync`
12
+ * needed; the runtime handles bundler resolution across Next.js Node +
13
+ * Edge runtimes.
6
14
  */
15
+ /** @internal — test-only reset for the one-shot warn latch. */
16
+ declare function __resetNodejsWasmWarningForTests(): void;
7
17
  /**
8
- * Load WASM module using Node.js fs module
9
- * This only works in Node.js runtime, not Edge Runtime
18
+ * @deprecated Removed in Phase-D.9a. Use `withCheckpoint` from
19
+ * `@kya-os/checkpoint-nextjs` it auto-loads `kya-os-engine` WASM
20
+ * via `@kya-os/checkpoint-wasm-runtime`. Throws on invocation; surface
21
+ * exists only so static analysis sees the historical export.
10
22
  */
11
23
  declare function loadWasmNodejs(): Promise<boolean>;
12
24
  /**
13
- * Check if we're in Node.js runtime
25
+ * @deprecated Removed in Phase-D.9a. The runtime guard is no longer
26
+ * needed; `@kya-os/checkpoint-wasm-runtime`'s loaders auto-detect
27
+ * runtime via the `"node"` / `"edge-runtime"` export conditions.
28
+ * Throws on invocation; surface exists only for export-compatibility.
14
29
  */
15
30
  declare function isNodejsRuntime(): boolean;
16
31
  /**
17
- * Get the loaded WASM module
32
+ * @deprecated Removed in Phase-D.9a. Migrate to `withCheckpoint`.
33
+ * Throws on invocation; surface exists only for export-compatibility.
18
34
  */
19
35
  declare function getWasmModule(): WebAssembly.Module | null;
20
36
  /**
21
- * Check if WASM is initialized
37
+ * @deprecated Removed in Phase-D.9a. Migrate to `withCheckpoint`.
38
+ * Throws on invocation; surface exists only for export-compatibility.
22
39
  */
23
40
  declare function isWasmInitialized(): boolean;
24
41
 
25
- export { getWasmModule, isNodejsRuntime, isWasmInitialized, loadWasmNodejs };
42
+ export { __resetNodejsWasmWarningForTests, getWasmModule, isNodejsRuntime, isWasmInitialized, loadWasmNodejs };
@@ -1,25 +1,42 @@
1
1
  /**
2
- * Node.js Runtime WASM Loader for AgentShield
2
+ * @deprecated Phase-D.9a — legacy Node.js WASM loader for the retired
3
+ * `agentshield-wasm` Rust crate. This file used `fs.readFileSync` to
4
+ * locate + load the legacy detector's WASM binary into the
5
+ * `@kya-os/checkpoint` `AgentDetector` class via `setWasmModule`. Both
6
+ * the WASM crate (Phase-D.9a/D.9b) and the AgentDetector class
7
+ * (AgentDetector-Deletion-2, next minor) are slated for deletion.
3
8
  *
4
- * This loader uses fs.readFileSync to load WASM in Node.js runtime.
5
- * It provides full cryptographic verification capabilities.
9
+ * Migrate to `withCheckpoint` from `@kya-os/checkpoint-nextjs` it
10
+ * loads the canonical `kya-os-engine` WASM automatically via
11
+ * `@kya-os/checkpoint-wasm-runtime`'s loaders. No manual `fs.readFileSync`
12
+ * needed; the runtime handles bundler resolution across Next.js Node +
13
+ * Edge runtimes.
6
14
  */
15
+ /** @internal — test-only reset for the one-shot warn latch. */
16
+ declare function __resetNodejsWasmWarningForTests(): void;
7
17
  /**
8
- * Load WASM module using Node.js fs module
9
- * This only works in Node.js runtime, not Edge Runtime
18
+ * @deprecated Removed in Phase-D.9a. Use `withCheckpoint` from
19
+ * `@kya-os/checkpoint-nextjs` it auto-loads `kya-os-engine` WASM
20
+ * via `@kya-os/checkpoint-wasm-runtime`. Throws on invocation; surface
21
+ * exists only so static analysis sees the historical export.
10
22
  */
11
23
  declare function loadWasmNodejs(): Promise<boolean>;
12
24
  /**
13
- * Check if we're in Node.js runtime
25
+ * @deprecated Removed in Phase-D.9a. The runtime guard is no longer
26
+ * needed; `@kya-os/checkpoint-wasm-runtime`'s loaders auto-detect
27
+ * runtime via the `"node"` / `"edge-runtime"` export conditions.
28
+ * Throws on invocation; surface exists only for export-compatibility.
14
29
  */
15
30
  declare function isNodejsRuntime(): boolean;
16
31
  /**
17
- * Get the loaded WASM module
32
+ * @deprecated Removed in Phase-D.9a. Migrate to `withCheckpoint`.
33
+ * Throws on invocation; surface exists only for export-compatibility.
18
34
  */
19
35
  declare function getWasmModule(): WebAssembly.Module | null;
20
36
  /**
21
- * Check if WASM is initialized
37
+ * @deprecated Removed in Phase-D.9a. Migrate to `withCheckpoint`.
38
+ * Throws on invocation; surface exists only for export-compatibility.
22
39
  */
23
40
  declare function isWasmInitialized(): boolean;
24
41
 
25
- export { getWasmModule, isNodejsRuntime, isWasmInitialized, loadWasmNodejs };
42
+ export { __resetNodejsWasmWarningForTests, getWasmModule, isNodejsRuntime, isWasmInitialized, loadWasmNodejs };
@@ -1,92 +1,35 @@
1
1
  'use strict';
2
2
 
3
- var fs = require('fs');
4
- var path = require('path');
5
- var checkpoint = require('@kya-os/checkpoint');
6
-
7
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
-
9
- var fs__default = /*#__PURE__*/_interopDefault(fs);
10
- var path__default = /*#__PURE__*/_interopDefault(path);
11
-
12
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
13
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
14
- }) : x)(function(x) {
15
- if (typeof require !== "undefined") return require.apply(this, arguments);
16
- throw Error('Dynamic require of "' + x + '" is not supported');
17
- });
18
- var wasmInitialized = false;
19
- var wasmModule = null;
3
+ // src/nodejs-wasm-loader.ts
4
+ var MIGRATION_ERROR = "`@kya-os/checkpoint-nextjs`'s `loadWasmNodejs` / `isNodejsRuntime` / `getWasmModule` / `isWasmInitialized` were deprecated in Phase-D.9a (legacy `agentshield-wasm` Rust crate retirement). The legacy `AgentDetector` class they fed is slated for deletion in AgentDetector-Deletion-2 (next minor). Migrate to `withCheckpoint` from `@kya-os/checkpoint-nextjs` \u2014 engine-backed via the Rust `kya-os-engine` crate, with automatic WASM loading via `@kya-os/checkpoint-wasm-runtime`. See packages/checkpoint-nextjs/README.md for the canonical recipe.";
5
+ var _nodejsWasmWarned = false;
6
+ function warnNodejsWasmDeprecated() {
7
+ if (_nodejsWasmWarned) return;
8
+ _nodejsWasmWarned = true;
9
+ if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") return;
10
+ console.warn(`[Checkpoint] ${MIGRATION_ERROR}`);
11
+ }
12
+ function __resetNodejsWasmWarningForTests() {
13
+ _nodejsWasmWarned = false;
14
+ }
20
15
  async function loadWasmNodejs() {
21
- if (wasmInitialized) {
22
- return true;
23
- }
24
- try {
25
- const possiblePaths = [
26
- // In node_modules (most likely)
27
- path__default.default.join(
28
- process.cwd(),
29
- "node_modules",
30
- "@kya-os",
31
- "agentshield",
32
- "dist",
33
- "wasm",
34
- "agentshield_wasm_bg.wasm"
35
- ),
36
- // In project root (if user copied it)
37
- path__default.default.join(process.cwd(), "agentshield_wasm_bg.wasm"),
38
- // Relative to current file
39
- path__default.default.join(
40
- __dirname,
41
- "..",
42
- "..",
43
- "..",
44
- "agentshield",
45
- "dist",
46
- "wasm",
47
- "agentshield_wasm_bg.wasm"
48
- )
49
- ];
50
- let wasmBuffer = null;
51
- let loadedPath = null;
52
- for (const wasmPath of possiblePaths) {
53
- try {
54
- if (fs__default.default.existsSync(wasmPath)) {
55
- wasmBuffer = fs__default.default.readFileSync(wasmPath);
56
- loadedPath = wasmPath;
57
- break;
58
- }
59
- } catch (e) {
60
- continue;
61
- }
62
- }
63
- if (!wasmBuffer) {
64
- console.warn("AgentShield: WASM file not found in any expected location");
65
- return false;
66
- }
67
- const bytes = new Uint8Array(wasmBuffer);
68
- wasmModule = await WebAssembly.compile(bytes);
69
- checkpoint.setWasmModule(wasmModule);
70
- wasmInitialized = true;
71
- console.log(`\u2705 AgentShield: WASM loaded successfully from ${loadedPath} (Node.js runtime)`);
72
- console.log("\u{1F510} Cryptographic verification enabled (95-100% confidence)");
73
- return true;
74
- } catch (error) {
75
- console.warn("\u26A0\uFE0F AgentShield: Failed to load WASM in Node.js runtime:", error);
76
- console.log("\u{1F4CA} Falling back to pattern detection (85% confidence)");
77
- return false;
78
- }
16
+ warnNodejsWasmDeprecated();
17
+ throw new Error(MIGRATION_ERROR);
79
18
  }
80
19
  function isNodejsRuntime() {
81
- return typeof process !== "undefined" && typeof process.versions !== "undefined" && typeof process.versions.node !== "undefined" && typeof __require !== "undefined";
20
+ warnNodejsWasmDeprecated();
21
+ throw new Error(MIGRATION_ERROR);
82
22
  }
83
23
  function getWasmModule() {
84
- return wasmModule;
24
+ warnNodejsWasmDeprecated();
25
+ throw new Error(MIGRATION_ERROR);
85
26
  }
86
27
  function isWasmInitialized() {
87
- return wasmInitialized;
28
+ warnNodejsWasmDeprecated();
29
+ throw new Error(MIGRATION_ERROR);
88
30
  }
89
31
 
32
+ exports.__resetNodejsWasmWarningForTests = __resetNodejsWasmWarningForTests;
90
33
  exports.getWasmModule = getWasmModule;
91
34
  exports.isNodejsRuntime = isNodejsRuntime;
92
35
  exports.isWasmInitialized = isWasmInitialized;
@@ -1,83 +1,30 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { setWasmModule } from '@kya-os/checkpoint';
4
-
5
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
6
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
7
- }) : x)(function(x) {
8
- if (typeof require !== "undefined") return require.apply(this, arguments);
9
- throw Error('Dynamic require of "' + x + '" is not supported');
10
- });
11
- var wasmInitialized = false;
12
- var wasmModule = null;
1
+ // src/nodejs-wasm-loader.ts
2
+ var MIGRATION_ERROR = "`@kya-os/checkpoint-nextjs`'s `loadWasmNodejs` / `isNodejsRuntime` / `getWasmModule` / `isWasmInitialized` were deprecated in Phase-D.9a (legacy `agentshield-wasm` Rust crate retirement). The legacy `AgentDetector` class they fed is slated for deletion in AgentDetector-Deletion-2 (next minor). Migrate to `withCheckpoint` from `@kya-os/checkpoint-nextjs` \u2014 engine-backed via the Rust `kya-os-engine` crate, with automatic WASM loading via `@kya-os/checkpoint-wasm-runtime`. See packages/checkpoint-nextjs/README.md for the canonical recipe.";
3
+ var _nodejsWasmWarned = false;
4
+ function warnNodejsWasmDeprecated() {
5
+ if (_nodejsWasmWarned) return;
6
+ _nodejsWasmWarned = true;
7
+ if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") return;
8
+ console.warn(`[Checkpoint] ${MIGRATION_ERROR}`);
9
+ }
10
+ function __resetNodejsWasmWarningForTests() {
11
+ _nodejsWasmWarned = false;
12
+ }
13
13
  async function loadWasmNodejs() {
14
- if (wasmInitialized) {
15
- return true;
16
- }
17
- try {
18
- const possiblePaths = [
19
- // In node_modules (most likely)
20
- path.join(
21
- process.cwd(),
22
- "node_modules",
23
- "@kya-os",
24
- "agentshield",
25
- "dist",
26
- "wasm",
27
- "agentshield_wasm_bg.wasm"
28
- ),
29
- // In project root (if user copied it)
30
- path.join(process.cwd(), "agentshield_wasm_bg.wasm"),
31
- // Relative to current file
32
- path.join(
33
- __dirname,
34
- "..",
35
- "..",
36
- "..",
37
- "agentshield",
38
- "dist",
39
- "wasm",
40
- "agentshield_wasm_bg.wasm"
41
- )
42
- ];
43
- let wasmBuffer = null;
44
- let loadedPath = null;
45
- for (const wasmPath of possiblePaths) {
46
- try {
47
- if (fs.existsSync(wasmPath)) {
48
- wasmBuffer = fs.readFileSync(wasmPath);
49
- loadedPath = wasmPath;
50
- break;
51
- }
52
- } catch (e) {
53
- continue;
54
- }
55
- }
56
- if (!wasmBuffer) {
57
- console.warn("AgentShield: WASM file not found in any expected location");
58
- return false;
59
- }
60
- const bytes = new Uint8Array(wasmBuffer);
61
- wasmModule = await WebAssembly.compile(bytes);
62
- setWasmModule(wasmModule);
63
- wasmInitialized = true;
64
- console.log(`\u2705 AgentShield: WASM loaded successfully from ${loadedPath} (Node.js runtime)`);
65
- console.log("\u{1F510} Cryptographic verification enabled (95-100% confidence)");
66
- return true;
67
- } catch (error) {
68
- console.warn("\u26A0\uFE0F AgentShield: Failed to load WASM in Node.js runtime:", error);
69
- console.log("\u{1F4CA} Falling back to pattern detection (85% confidence)");
70
- return false;
71
- }
14
+ warnNodejsWasmDeprecated();
15
+ throw new Error(MIGRATION_ERROR);
72
16
  }
73
17
  function isNodejsRuntime() {
74
- return typeof process !== "undefined" && typeof process.versions !== "undefined" && typeof process.versions.node !== "undefined" && typeof __require !== "undefined";
18
+ warnNodejsWasmDeprecated();
19
+ throw new Error(MIGRATION_ERROR);
75
20
  }
76
21
  function getWasmModule() {
77
- return wasmModule;
22
+ warnNodejsWasmDeprecated();
23
+ throw new Error(MIGRATION_ERROR);
78
24
  }
79
25
  function isWasmInitialized() {
80
- return wasmInitialized;
26
+ warnNodejsWasmDeprecated();
27
+ throw new Error(MIGRATION_ERROR);
81
28
  }
82
29
 
83
- export { getWasmModule, isNodejsRuntime, isWasmInitialized, loadWasmNodejs };
30
+ export { __resetNodejsWasmWarningForTests, getWasmModule, isNodejsRuntime, isWasmInitialized, loadWasmNodejs };
@@ -1,5 +1,5 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
- import { DetectionResult } from '@kya-os/checkpoint-shared';
2
+ import { DetectionDetail } from '@kya-os/checkpoint-shared';
3
3
 
4
4
  /**
5
5
  * Edge-compatible session tracking for AI agents
@@ -25,7 +25,7 @@ declare class EdgeSessionTracker {
25
25
  /**
26
26
  * Track a new AI agent session
27
27
  */
28
- track(_request: NextRequest, response: NextResponse, result: DetectionResult): Promise<NextResponse>;
28
+ track(_request: NextRequest, response: NextResponse, result: DetectionDetail): Promise<NextResponse>;
29
29
  /**
30
30
  * Check for existing AI agent session
31
31
  */
@@ -1,5 +1,5 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
- import { DetectionResult } from '@kya-os/checkpoint-shared';
2
+ import { DetectionDetail } from '@kya-os/checkpoint-shared';
3
3
 
4
4
  /**
5
5
  * Edge-compatible session tracking for AI agents
@@ -25,7 +25,7 @@ declare class EdgeSessionTracker {
25
25
  /**
26
26
  * Track a new AI agent session
27
27
  */
28
- track(_request: NextRequest, response: NextResponse, result: DetectionResult): Promise<NextResponse>;
28
+ track(_request: NextRequest, response: NextResponse, result: DetectionDetail): Promise<NextResponse>;
29
29
  /**
30
30
  * Check for existing AI agent session
31
31
  */
@@ -19,7 +19,9 @@ var EdgeSessionTracker = class {
19
19
  */
20
20
  async track(_request, response, result) {
21
21
  try {
22
- if (!this.config.enabled || !checkpointShared.shouldEnforce(result)) {
22
+ const detectedName = result.detectedAgent?.name;
23
+ const isEnforceable = result.isAgent || result.isAiCrawler || checkpointShared.isKnownAiCrawler(detectedName);
24
+ if (!this.config.enabled || !isEnforceable) {
23
25
  return response;
24
26
  }
25
27
  const sessionData = {
@@ -1,4 +1,4 @@
1
- import { shouldEnforce } from '@kya-os/checkpoint-shared';
1
+ import { isKnownAiCrawler } from '@kya-os/checkpoint-shared';
2
2
 
3
3
  // src/session-tracker.ts
4
4
  var EdgeSessionTracker = class {
@@ -17,7 +17,9 @@ var EdgeSessionTracker = class {
17
17
  */
18
18
  async track(_request, response, result) {
19
19
  try {
20
- if (!this.config.enabled || !shouldEnforce(result)) {
20
+ const detectedName = result.detectedAgent?.name;
21
+ const isEnforceable = result.isAgent || result.isAiCrawler || isKnownAiCrawler(detectedName);
22
+ if (!this.config.enabled || !isEnforceable) {
21
23
  return response;
22
24
  }
23
25
  const sessionData = {
@@ -1,10 +1,21 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
2
 
3
3
  /**
4
- * WASM-enabled middleware for Next.js with AgentShield
5
- * Following official Next.js documentation for WebAssembly in Edge Runtime
4
+ * WASM-enabled middleware for Next.js with Checkpoint.
5
+ *
6
+ * **Deprecation notice (AgentDetector-Deletion-1):**
7
+ * `createWasmAgentShieldMiddleware` is deprecated as of this patch and
8
+ * slated for removal in the next minor. It internally constructs a
9
+ * legacy `AgentDetector` and never actually uses the WASM instance for
10
+ * detection (the `wasmInstance` arg only bumps confidence by 15%).
11
+ * Stage 1 detection now lives in the Rust `kya-os-engine` (PDM-1
12
+ * #2560). Migrate to `withCheckpoint` from `@kya-os/checkpoint-nextjs`
13
+ * — engine-backed, runs the orchestrator including envelope
14
+ * verification.
6
15
  */
7
16
 
17
+ /** @internal — test-only reset for the one-shot warn latch. */
18
+ declare function __resetCreateWasmAgentShieldWarningForTests(): void;
8
19
  interface WasmDetectionResult {
9
20
  isAgent: boolean;
10
21
  isAiCrawler?: boolean;
@@ -26,6 +37,11 @@ interface AgentShieldConfig {
26
37
  };
27
38
  }
28
39
  /**
40
+ * @deprecated Wraps the legacy `AgentDetector` class. Will be removed
41
+ * in the next minor (AgentDetector-Deletion-2). Migrate to
42
+ * `withCheckpoint` from `@kya-os/checkpoint-nextjs` — engine-backed,
43
+ * runs the orchestrator including envelope verification.
44
+ *
29
45
  * Create a WASM-enabled Checkpoint middleware (**pattern-detection only**).
30
46
  *
31
47
  * **This factory runs UA/header pattern matching only.** It does NOT
@@ -79,4 +95,4 @@ declare function createWasmAgentShieldMiddleware(config: AgentShieldConfig & {
79
95
  */
80
96
  declare function instantiateWasm(wasmModule: WebAssembly.Module): Promise<WebAssembly.Instance>;
81
97
 
82
- export { type AgentShieldConfig, type WasmDetectionResult, createWasmAgentShieldMiddleware, instantiateWasm };
98
+ export { type AgentShieldConfig, type WasmDetectionResult, __resetCreateWasmAgentShieldWarningForTests, createWasmAgentShieldMiddleware, instantiateWasm };
@@ -1,10 +1,21 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
2
 
3
3
  /**
4
- * WASM-enabled middleware for Next.js with AgentShield
5
- * Following official Next.js documentation for WebAssembly in Edge Runtime
4
+ * WASM-enabled middleware for Next.js with Checkpoint.
5
+ *
6
+ * **Deprecation notice (AgentDetector-Deletion-1):**
7
+ * `createWasmAgentShieldMiddleware` is deprecated as of this patch and
8
+ * slated for removal in the next minor. It internally constructs a
9
+ * legacy `AgentDetector` and never actually uses the WASM instance for
10
+ * detection (the `wasmInstance` arg only bumps confidence by 15%).
11
+ * Stage 1 detection now lives in the Rust `kya-os-engine` (PDM-1
12
+ * #2560). Migrate to `withCheckpoint` from `@kya-os/checkpoint-nextjs`
13
+ * — engine-backed, runs the orchestrator including envelope
14
+ * verification.
6
15
  */
7
16
 
17
+ /** @internal — test-only reset for the one-shot warn latch. */
18
+ declare function __resetCreateWasmAgentShieldWarningForTests(): void;
8
19
  interface WasmDetectionResult {
9
20
  isAgent: boolean;
10
21
  isAiCrawler?: boolean;
@@ -26,6 +37,11 @@ interface AgentShieldConfig {
26
37
  };
27
38
  }
28
39
  /**
40
+ * @deprecated Wraps the legacy `AgentDetector` class. Will be removed
41
+ * in the next minor (AgentDetector-Deletion-2). Migrate to
42
+ * `withCheckpoint` from `@kya-os/checkpoint-nextjs` — engine-backed,
43
+ * runs the orchestrator including envelope verification.
44
+ *
29
45
  * Create a WASM-enabled Checkpoint middleware (**pattern-detection only**).
30
46
  *
31
47
  * **This factory runs UA/header pattern matching only.** It does NOT
@@ -79,4 +95,4 @@ declare function createWasmAgentShieldMiddleware(config: AgentShieldConfig & {
79
95
  */
80
96
  declare function instantiateWasm(wasmModule: WebAssembly.Module): Promise<WebAssembly.Instance>;
81
97
 
82
- export { type AgentShieldConfig, type WasmDetectionResult, createWasmAgentShieldMiddleware, instantiateWasm };
98
+ export { type AgentShieldConfig, type WasmDetectionResult, __resetCreateWasmAgentShieldWarningForTests, createWasmAgentShieldMiddleware, instantiateWasm };
@@ -2,10 +2,38 @@
2
2
 
3
3
  var server = require('next/server');
4
4
  var checkpoint = require('@kya-os/checkpoint');
5
- var checkpointShared = require('@kya-os/checkpoint-shared');
6
5
 
7
6
  // src/wasm-middleware.ts
7
+
8
+ // src/local-detection-gate.ts
9
+ function isDetectedAgentForLocalGate(result) {
10
+ return result.isAgent === true;
11
+ }
12
+ function evaluateLocalDetectionGate(result, config) {
13
+ if (!isDetectedAgentForLocalGate(result)) {
14
+ return { action: "allow", shouldNotify: false };
15
+ }
16
+ if ((result.confidence ?? 0) >= config.confidenceThreshold) {
17
+ return { action: config.defaultAction, shouldNotify: true };
18
+ }
19
+ return { action: "allow", shouldNotify: false };
20
+ }
21
+
22
+ // src/wasm-middleware.ts
23
+ var _createWasmAgentShieldWarned = false;
24
+ function warnCreateWasmAgentShieldDeprecated() {
25
+ if (_createWasmAgentShieldWarned) return;
26
+ _createWasmAgentShieldWarned = true;
27
+ if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") return;
28
+ console.warn(
29
+ "[Checkpoint] createWasmAgentShieldMiddleware is deprecated and will be removed in the next minor. It wraps the legacy AgentDetector class; Stage 1 detection now lives in the Rust kya-os-engine (PDM-1). Migrate to `withCheckpoint` from @kya-os/checkpoint-nextjs \u2014 engine-backed and runs envelope verification. See packages/checkpoint-nextjs/CHANGELOG.md for the recipe."
30
+ );
31
+ }
32
+ function __resetCreateWasmAgentShieldWarningForTests() {
33
+ _createWasmAgentShieldWarned = false;
34
+ }
8
35
  function createWasmAgentShieldMiddleware(config) {
36
+ warnCreateWasmAgentShieldDeprecated();
9
37
  const {
10
38
  onAgentDetected,
11
39
  blockOnHighConfidence = false,
@@ -45,11 +73,11 @@ function createWasmAgentShieldMiddleware(config) {
45
73
  // Updated to 0-100 scale (was 0.7)
46
74
  timestamp: result.timestamp instanceof Date ? result.timestamp.toISOString() : new Date(result.timestamp).toISOString()
47
75
  };
48
- const decision = checkpointShared.evaluateEnforcement(enhancedResult, {
76
+ const decision = evaluateLocalDetectionGate(enhancedResult, {
49
77
  confidenceThreshold,
50
78
  defaultAction: blockOnHighConfidence ? "block" : "allow"
51
79
  });
52
- if (onAgentDetected && checkpointShared.shouldEnforce(enhancedResult)) {
80
+ if (onAgentDetected && isDetectedAgentForLocalGate(enhancedResult)) {
53
81
  await onAgentDetected(enhancedResult);
54
82
  }
55
83
  if (decision.action === "block") {
@@ -92,5 +120,6 @@ async function instantiateWasm(wasmModule) {
92
120
  }
93
121
  }
94
122
 
123
+ exports.__resetCreateWasmAgentShieldWarningForTests = __resetCreateWasmAgentShieldWarningForTests;
95
124
  exports.createWasmAgentShieldMiddleware = createWasmAgentShieldMiddleware;
96
125
  exports.instantiateWasm = instantiateWasm;
@@ -1,9 +1,37 @@
1
1
  import { NextResponse } from 'next/server';
2
2
  import { AgentDetector } from '@kya-os/checkpoint';
3
- import { evaluateEnforcement, shouldEnforce } from '@kya-os/checkpoint-shared';
4
3
 
5
4
  // src/wasm-middleware.ts
5
+
6
+ // src/local-detection-gate.ts
7
+ function isDetectedAgentForLocalGate(result) {
8
+ return result.isAgent === true;
9
+ }
10
+ function evaluateLocalDetectionGate(result, config) {
11
+ if (!isDetectedAgentForLocalGate(result)) {
12
+ return { action: "allow", shouldNotify: false };
13
+ }
14
+ if ((result.confidence ?? 0) >= config.confidenceThreshold) {
15
+ return { action: config.defaultAction, shouldNotify: true };
16
+ }
17
+ return { action: "allow", shouldNotify: false };
18
+ }
19
+
20
+ // src/wasm-middleware.ts
21
+ var _createWasmAgentShieldWarned = false;
22
+ function warnCreateWasmAgentShieldDeprecated() {
23
+ if (_createWasmAgentShieldWarned) return;
24
+ _createWasmAgentShieldWarned = true;
25
+ if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") return;
26
+ console.warn(
27
+ "[Checkpoint] createWasmAgentShieldMiddleware is deprecated and will be removed in the next minor. It wraps the legacy AgentDetector class; Stage 1 detection now lives in the Rust kya-os-engine (PDM-1). Migrate to `withCheckpoint` from @kya-os/checkpoint-nextjs \u2014 engine-backed and runs envelope verification. See packages/checkpoint-nextjs/CHANGELOG.md for the recipe."
28
+ );
29
+ }
30
+ function __resetCreateWasmAgentShieldWarningForTests() {
31
+ _createWasmAgentShieldWarned = false;
32
+ }
6
33
  function createWasmAgentShieldMiddleware(config) {
34
+ warnCreateWasmAgentShieldDeprecated();
7
35
  const {
8
36
  onAgentDetected,
9
37
  blockOnHighConfidence = false,
@@ -43,11 +71,11 @@ function createWasmAgentShieldMiddleware(config) {
43
71
  // Updated to 0-100 scale (was 0.7)
44
72
  timestamp: result.timestamp instanceof Date ? result.timestamp.toISOString() : new Date(result.timestamp).toISOString()
45
73
  };
46
- const decision = evaluateEnforcement(enhancedResult, {
74
+ const decision = evaluateLocalDetectionGate(enhancedResult, {
47
75
  confidenceThreshold,
48
76
  defaultAction: blockOnHighConfidence ? "block" : "allow"
49
77
  });
50
- if (onAgentDetected && shouldEnforce(enhancedResult)) {
78
+ if (onAgentDetected && isDetectedAgentForLocalGate(enhancedResult)) {
51
79
  await onAgentDetected(enhancedResult);
52
80
  }
53
81
  if (decision.action === "block") {
@@ -90,4 +118,4 @@ async function instantiateWasm(wasmModule) {
90
118
  }
91
119
  }
92
120
 
93
- export { createWasmAgentShieldMiddleware, instantiateWasm };
121
+ export { __resetCreateWasmAgentShieldWarningForTests, createWasmAgentShieldMiddleware, instantiateWasm };