- Update api/index.js to use handler export format
- Update chat/index.js to use handler export format
- Update webhook/index.js to use handler export format
- Remove addEventListener('fetch') pattern incompatible with wws
- Remove URL API usage (not available in wws JS runtime)
- Remove spread operator for better compatibility
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
// 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, project_id, user_id, provider } = body
|
|
const selectedProvider = 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: selectedProvider,
|
|
source: 'wws-proxy',
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
})
|
|
|
|
const result = await n8nResponse.json()
|
|
|
|
return new Response(JSON.stringify({
|
|
success: true,
|
|
response: result.response || result,
|
|
provider: selectedProvider,
|
|
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
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = { handler }
|