From 664bbdad57e430d8066cb9857ba73d2ca6a8ee06 Mon Sep 17 00:00:00 2001 From: Ajax Davis Date: Sun, 30 Nov 2025 01:50:49 +1000 Subject: [PATCH] fix: use dynamic import for AI SDK to resolve tiktoken WASM build error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Convert static import of executeToolWithAgent to dynamic import inside the POST handler. This prevents the AI SDK (and its tiktoken dependency) from being loaded at build time, which was causing WASM loading errors. **Why this fix works:** - Next.js 16 + Turbopack tries to analyze routes at build time - tiktoken requires tiktoken_bg.wasm which can't load during static analysis - Dynamic imports defer loading until runtime, avoiding build-time WASM issues **Changes:** - Remove: `import { executeToolWithAgent } from '@/lib/ai-agent/tool-executor-agent'` - Add: `const { executeToolWithAgent } = await import('@/lib/ai-agent/tool-executor-agent')` inside the stream start() handler Build now completes successfully. Route is properly marked as dynamic (ƒ). Resolves: "Error: Missing tiktoken_bg.wasm" during Next.js build 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- apps/web/src/app/api/tools/execute/[...slug]/route.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/web/src/app/api/tools/execute/[...slug]/route.ts b/apps/web/src/app/api/tools/execute/[...slug]/route.ts index a9e54d0..2cc2632 100644 --- a/apps/web/src/app/api/tools/execute/[...slug]/route.ts +++ b/apps/web/src/app/api/tools/execute/[...slug]/route.ts @@ -3,7 +3,6 @@ * Executes TPMJS tools with AI agents and streams real-time progress */ -import { executeToolWithAgent } from '@/lib/ai-agent/tool-executor-agent'; import { checkRateLimit, getClientIP } from '@/lib/rate-limiter'; import { prisma } from '@tpmjs/db'; import { type NextRequest, NextResponse } from 'next/server'; @@ -100,6 +99,9 @@ export async function POST( try { const startTime = Date.now(); + // Dynamically import AI agent to avoid loading tiktoken at build time + const { executeToolWithAgent } = await import('@/lib/ai-agent/tool-executor-agent'); + // Execute tool with AI agent const result = await executeToolWithAgent( tool,