// Chat proxy - routes requests to n8n workflow // Path: /chat async function handler(request) { const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type, Authorization', 'Content-Type': 'application/json' } if (request.method === 'OPTIONS') { return new Response('', { headers: corsHeaders }) } if (request.method !== 'POST') { return new Response(JSON.stringify({ error: 'Method not allowed' }), { status: 405, headers: corsHeaders }) } try { const body = await request.json() const message = body.message const project_id = body.project_id const user_id = body.user_id const provider = body.provider || 'zai' if (!message) { return new Response(JSON.stringify({ error: 'Message is required' }), { status: 400, headers: corsHeaders }) } // Forward to n8n webhook const n8nResponse = await fetch('https://n8n.mylder.io/webhook/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: message, project_id: project_id || null, user_id: user_id || null, provider: provider, source: 'wws-proxy', timestamp: new Date().toISOString() }) }) const result = await n8nResponse.json() return new Response(JSON.stringify({ success: true, response: result.response || result, provider: provider, timestamp: new Date().toISOString() }), { headers: corsHeaders }) } catch (error) { return new Response(JSON.stringify({ success: false, error: error.message || 'Internal server error' }), { status: 500, headers: corsHeaders }) } } // ES Module export for wws export default handler