fix: use dynamic import for AI SDK to resolve tiktoken WASM build error

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 <noreply@anthropic.com>
This commit is contained in:
Ajax Davis 2025-11-30 01:50:49 +10:00
parent 140a2fa218
commit 664bbdad57

View file

@ -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,