Files
wws-functions/chat/index.js
christiankrag c8a8ea007d Initial wws serverless functions setup
- Add health check API endpoint (/api)
- Add chat proxy to n8n workflow (/chat)
- Add webhook receiver for external integrations (/webhook)
- Add Dockerfile for container deployment
- Add wws.toml configuration

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 05:43:11 +01:00

72 lines
1.9 KiB
JavaScript

// Chat proxy - routes requests to n8n workflow
// Path: /chat
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
}
if (request.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders })
}
if (request.method !== 'POST') {
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
status: 405,
headers: { 'Content-Type': 'application/json', ...corsHeaders }
})
}
try {
const body = await request.json()
const { message, project_id, user_id, provider = 'zai' } = body
if (!message) {
return new Response(JSON.stringify({ error: 'Message is required' }), {
status: 400,
headers: { 'Content-Type': 'application/json', ...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,
project_id: project_id || null,
user_id: user_id || null,
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,
timestamp: new Date().toISOString()
}), {
headers: { 'Content-Type': 'application/json', ...corsHeaders }
})
} catch (error) {
return new Response(JSON.stringify({
success: false,
error: error.message || 'Internal server error'
}), {
status: 500,
headers: { 'Content-Type': 'application/json', ...corsHeaders }
})
}
}