Created
October 11, 2025 13:41
-
-
Save chihebnabil/94ddc87f334653197e15ef660e188ecd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { serve } from "https://deno.land/std@0.168.0/http/server.ts" | |
| import { createClient } from 'https://esm.sh/@supabase/supabase-js@2' | |
| const corsHeaders = { | |
| 'Access-Control-Allow-Origin': '*', | |
| 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', | |
| } | |
| serve(async (req) => { | |
| // Handle CORS preflight requests | |
| if (req.method === 'OPTIONS') { | |
| return new Response('ok', { headers: corsHeaders }) | |
| } | |
| try { | |
| // Initialize Supabase client | |
| const supabaseClient = createClient( | |
| Deno.env.get('SUPABASE_URL') ?? '', | |
| Deno.env.get('SUPABASE_ANON_KEY') ?? '', | |
| { | |
| global: { | |
| headers: { Authorization: req.headers.get('Authorization')! }, | |
| }, | |
| } | |
| ) | |
| // Verify user authentication | |
| const { | |
| data: { user }, | |
| error: authError, | |
| } = await supabaseClient.auth.getUser() | |
| if (authError || !user) { | |
| return new Response( | |
| JSON.stringify({ error: 'Unauthorized' }), | |
| { | |
| status: 401, | |
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | |
| } | |
| ) | |
| } | |
| // Parse request body | |
| const { messages, model = 'openai/gpt-3.5-turbo', temperature = 0.7 } = await req.json() | |
| if (!messages || !Array.isArray(messages)) { | |
| return new Response( | |
| JSON.stringify({ error: 'Messages array is required' }), | |
| { | |
| status: 400, | |
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | |
| } | |
| ) | |
| } | |
| // Get OpenRouter API key from environment | |
| const openRouterApiKey = Deno.env.get('OPENROUTER_API_KEY') | |
| if (!openRouterApiKey) { | |
| return new Response( | |
| JSON.stringify({ error: 'OpenRouter API key not configured' }), | |
| { | |
| status: 500, | |
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | |
| } | |
| ) | |
| } | |
| // Call OpenRouter API with streaming | |
| const response = await fetch('https://openrouter.ai/api/v1/chat/completions', { | |
| method: 'POST', | |
| headers: { | |
| 'Authorization': `Bearer ${openRouterApiKey}`, | |
| 'Content-Type': 'application/json', | |
| 'HTTP-Referer': Deno.env.get('APP_URL') ?? 'https://your-app.com', | |
| 'X-Title': 'Supabase Chat App', | |
| }, | |
| body: JSON.stringify({ | |
| model, | |
| messages, | |
| temperature, | |
| stream: true, | |
| }), | |
| }) | |
| if (!response.ok) { | |
| const error = await response.text() | |
| return new Response( | |
| JSON.stringify({ error: `OpenRouter API error: ${error}` }), | |
| { | |
| status: response.status, | |
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | |
| } | |
| ) | |
| } | |
| // Create a TransformStream to handle SSE formatting | |
| const encoder = new TextEncoder() | |
| const decoder = new TextDecoder() | |
| const stream = new ReadableStream({ | |
| async start(controller) { | |
| const reader = response.body?.getReader() | |
| if (!reader) { | |
| controller.close() | |
| return | |
| } | |
| try { | |
| while (true) { | |
| const { done, value } = await reader.read() | |
| if (done) { | |
| controller.close() | |
| break | |
| } | |
| // Decode the chunk and forward it | |
| const chunk = decoder.decode(value) | |
| controller.enqueue(encoder.encode(chunk)) | |
| } | |
| } catch (error) { | |
| console.error('Stream error:', error) | |
| controller.error(error) | |
| } | |
| }, | |
| }) | |
| // Return streaming response | |
| return new Response(stream, { | |
| headers: { | |
| ...corsHeaders, | |
| 'Content-Type': 'text/event-stream', | |
| 'Cache-Control': 'no-cache', | |
| 'Connection': 'keep-alive', | |
| }, | |
| }) | |
| } catch (error) { | |
| console.error('Function error:', error) | |
| return new Response( | |
| JSON.stringify({ error: error.message }), | |
| { | |
| status: 500, | |
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | |
| } | |
| ) | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment