|
| 1 | +import type { Context, Config } from "@netlify/functions"; |
| 2 | + |
| 3 | +const EL_BASE = "https://api.elevenlabs.io/v1"; |
| 4 | + |
| 5 | +const CORS_HEADERS = { |
| 6 | + "Access-Control-Allow-Origin": "*", |
| 7 | + "Access-Control-Allow-Methods": "POST, OPTIONS", |
| 8 | + "Access-Control-Allow-Headers": "Content-Type", |
| 9 | + "Content-Type": "application/json", |
| 10 | +}; |
| 11 | + |
| 12 | +export default async (req: Request, _context: Context) => { |
| 13 | + // Handle CORS preflight |
| 14 | + if (req.method === "OPTIONS") { |
| 15 | + return new Response(null, { status: 204, headers: CORS_HEADERS }); |
| 16 | + } |
| 17 | + |
| 18 | + if (req.method !== "POST") { |
| 19 | + return new Response(JSON.stringify({ error: "Method not allowed" }), { |
| 20 | + status: 405, |
| 21 | + headers: CORS_HEADERS, |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + // Read env vars |
| 26 | + const apiKey = |
| 27 | + Netlify.env.get("ELEVENLABS_API_KEY") || |
| 28 | + Netlify.env.get("VITE_ELEVENLABS_API_KEY") || |
| 29 | + process.env.ELEVENLABS_API_KEY || |
| 30 | + process.env.VITE_ELEVENLABS_API_KEY; |
| 31 | + |
| 32 | + if (!apiKey) { |
| 33 | + return new Response( |
| 34 | + JSON.stringify({ error: "ELEVENLABS_API_KEY not configured" }), |
| 35 | + { status: 500, headers: CORS_HEADERS } |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + const agentId = |
| 40 | + Netlify.env.get("ELEVENLABS_AGENT_ID") || |
| 41 | + Netlify.env.get("VITE_ELEVENLABS_AGENT_ID") || |
| 42 | + process.env.ELEVENLABS_AGENT_ID || |
| 43 | + process.env.VITE_ELEVENLABS_AGENT_ID; |
| 44 | + |
| 45 | + if (!agentId) { |
| 46 | + return new Response( |
| 47 | + JSON.stringify({ |
| 48 | + error: |
| 49 | + "ELEVENLABS_AGENT_ID not configured. Set it as an env var.", |
| 50 | + }), |
| 51 | + { status: 500, headers: CORS_HEADERS } |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + const supabaseUrl = |
| 56 | + Netlify.env.get("VITE_SUPABASE_URL") || |
| 57 | + Netlify.env.get("SUPABASE_URL") || |
| 58 | + process.env.VITE_SUPABASE_URL || |
| 59 | + process.env.SUPABASE_URL; |
| 60 | + |
| 61 | + if (!supabaseUrl) { |
| 62 | + return new Response( |
| 63 | + JSON.stringify({ error: "SUPABASE_URL not configured" }), |
| 64 | + { status: 500, headers: CORS_HEADERS } |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + // Never use VITE_ prefix for service role key — Vite would inline it into the client bundle |
| 69 | + const supabaseServiceKey = |
| 70 | + Netlify.env.get("SUPABASE_SERVICE_ROLE_KEY") || |
| 71 | + process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 72 | + |
| 73 | + if (!supabaseServiceKey) { |
| 74 | + return new Response( |
| 75 | + JSON.stringify({ error: "SUPABASE_SERVICE_ROLE_KEY not configured" }), |
| 76 | + { status: 500, headers: CORS_HEADERS } |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + // Parse request body |
| 81 | + let kbId: string | undefined; |
| 82 | + try { |
| 83 | + const body = await req.json(); |
| 84 | + kbId = body.kbId; |
| 85 | + } catch { |
| 86 | + return new Response( |
| 87 | + JSON.stringify({ error: "Invalid or missing JSON body" }), |
| 88 | + { status: 400, headers: CORS_HEADERS } |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + if (!kbId) { |
| 93 | + return new Response( |
| 94 | + JSON.stringify({ error: "Missing required field: kbId" }), |
| 95 | + { status: 400, headers: CORS_HEADERS } |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + // Step 1: Fetch all chunks for this kbId from Supabase |
| 101 | + const supabaseRes = await fetch( |
| 102 | + `${supabaseUrl}/rest/v1/kb_documents?kb_id=eq.${encodeURIComponent(kbId)}&select=content,chunk_index&order=chunk_index.asc`, |
| 103 | + { |
| 104 | + headers: { |
| 105 | + apikey: supabaseServiceKey, |
| 106 | + Authorization: `Bearer ${supabaseServiceKey}`, |
| 107 | + "Content-Type": "application/json", |
| 108 | + }, |
| 109 | + } |
| 110 | + ); |
| 111 | + |
| 112 | + if (!supabaseRes.ok) { |
| 113 | + const errorText = await supabaseRes.text(); |
| 114 | + throw new Error( |
| 115 | + `Supabase fetch failed (HTTP ${supabaseRes.status}): ${errorText}` |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + const chunks: { content: string; chunk_index: number }[] = |
| 120 | + await supabaseRes.json(); |
| 121 | + |
| 122 | + // Step 2: Fetch current agent config to find existing KB item IDs |
| 123 | + const agentRes = await fetch(`${EL_BASE}/convai/agents/${agentId}`, { |
| 124 | + headers: { "xi-api-key": apiKey }, |
| 125 | + }); |
| 126 | + |
| 127 | + if (!agentRes.ok) { |
| 128 | + const errorText = await agentRes.text(); |
| 129 | + throw new Error( |
| 130 | + `Failed to fetch ElevenLabs agent config (HTTP ${agentRes.status}): ${errorText}` |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + const agentData = await agentRes.json(); |
| 135 | + const existingKbItems: { id: string }[] = |
| 136 | + agentData?.conversation_config?.agent?.prompt?.knowledge_base ?? []; |
| 137 | + |
| 138 | + // Step 3: Delete all existing KB items from the agent |
| 139 | + for (const item of existingKbItems) { |
| 140 | + const delRes = await fetch( |
| 141 | + `${EL_BASE}/convai/agents/${agentId}/knowledge-base/${item.id}`, |
| 142 | + { |
| 143 | + method: "DELETE", |
| 144 | + headers: { "xi-api-key": apiKey }, |
| 145 | + } |
| 146 | + ); |
| 147 | + if (!delRes.ok) { |
| 148 | + const errorText = await delRes.text(); |
| 149 | + throw new Error( |
| 150 | + `Failed to delete KB item ${item.id} (HTTP ${delRes.status}): ${errorText}` |
| 151 | + ); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // Step 4: If no chunks, return early |
| 156 | + if (chunks.length === 0) { |
| 157 | + return new Response( |
| 158 | + JSON.stringify({ ok: true, chunks_synced: 0 }), |
| 159 | + { status: 200, headers: CORS_HEADERS } |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + // Step 5: Combine all chunks into one text blob |
| 164 | + const combinedText = chunks.map((c) => c.content).join("\n\n"); |
| 165 | + |
| 166 | + // Step 6: Upload combined text as a new KB document to ElevenLabs |
| 167 | + const textBlob = new Blob([combinedText], { type: "text/plain" }); |
| 168 | + const formData = new FormData(); |
| 169 | + formData.append("file", textBlob, `kb-${kbId}.txt`); |
| 170 | + formData.append("name", `kb-${kbId}`); |
| 171 | + |
| 172 | + const uploadRes = await fetch(`${EL_BASE}/convai/knowledge-base`, { |
| 173 | + method: "POST", |
| 174 | + headers: { "xi-api-key": apiKey }, |
| 175 | + body: formData, |
| 176 | + }); |
| 177 | + |
| 178 | + if (!uploadRes.ok) { |
| 179 | + const errorText = await uploadRes.text(); |
| 180 | + throw new Error( |
| 181 | + `Failed to upload KB document to ElevenLabs (HTTP ${uploadRes.status}): ${errorText}` |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + const uploadData = await uploadRes.json(); |
| 186 | + const kbDocId: string = uploadData.id; |
| 187 | + |
| 188 | + // Step 7: Attach the new KB doc to the agent |
| 189 | + const patchRes = await fetch(`${EL_BASE}/convai/agents/${agentId}`, { |
| 190 | + method: "PATCH", |
| 191 | + headers: { |
| 192 | + "xi-api-key": apiKey, |
| 193 | + "Content-Type": "application/json", |
| 194 | + }, |
| 195 | + body: JSON.stringify({ |
| 196 | + conversation_config: { |
| 197 | + agent: { |
| 198 | + prompt: { |
| 199 | + knowledge_base: [{ type: "file", id: kbDocId }], |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + }), |
| 204 | + }); |
| 205 | + |
| 206 | + if (!patchRes.ok) { |
| 207 | + const errorText = await patchRes.text(); |
| 208 | + throw new Error( |
| 209 | + `Failed to attach KB document to ElevenLabs agent (HTTP ${patchRes.status}): ${errorText}` |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + return new Response( |
| 214 | + JSON.stringify({ ok: true, chunks_synced: chunks.length, kb_doc_id: kbDocId }), |
| 215 | + { status: 200, headers: CORS_HEADERS } |
| 216 | + ); |
| 217 | + } catch (error) { |
| 218 | + console.error("elevenlabs-kb-sync error:", error); |
| 219 | + return new Response( |
| 220 | + JSON.stringify({ error: "Internal server error", details: String(error) }), |
| 221 | + { status: 500, headers: CORS_HEADERS } |
| 222 | + ); |
| 223 | + } |
| 224 | +}; |
| 225 | + |
| 226 | +export const config: Config = { |
| 227 | + path: "/api/elevenlabs-kb-sync", |
| 228 | +}; |
0 commit comments