|
1 | | -import { createClient } from "@supabase/supabase-js"; |
2 | | -import { NextRequest, NextResponse } from "next/server"; |
3 | | -import { randomBytes } from "crypto"; |
| 1 | +// import { createClient } from "@supabase/supabase-js"; |
| 2 | +// import { NextRequest, NextResponse } from "next/server"; |
| 3 | +// import { randomBytes } from "crypto"; |
4 | 4 |
|
5 | | -// Generate unique message ID |
6 | | -function generateMessageId(): string { |
7 | | - return `msg_${randomBytes(8).toString("hex")}`; |
8 | | -} |
| 5 | +// // Generate unique message ID |
| 6 | +// function generateMessageId(): string { |
| 7 | +// return `msg_${randomBytes(8).toString("hex")}`; |
| 8 | +// } |
9 | 9 |
|
10 | | -export async function POST(request: NextRequest) { |
11 | | - try { |
12 | | - const { sessionId, question } = await request.json(); |
| 10 | +// export async function POST(request: NextRequest) { |
| 11 | +// try { |
| 12 | +// const { sessionId, question } = await request.json(); |
13 | 13 |
|
14 | | - console.log("📥 API received chat request:", { sessionId, question }); |
| 14 | +// console.log("📥 API received chat request:", { sessionId, question }); |
15 | 15 |
|
16 | | - // Validate inputs |
17 | | - if (!sessionId || typeof sessionId !== "string") { |
18 | | - console.error("❌ Missing sessionId"); |
19 | | - return NextResponse.json( |
20 | | - { error: "Session ID is required" }, |
21 | | - { status: 400 } |
22 | | - ); |
23 | | - } |
| 16 | +// // Validate inputs |
| 17 | +// if (!sessionId || typeof sessionId !== "string") { |
| 18 | +// console.error("❌ Missing sessionId"); |
| 19 | +// return NextResponse.json( |
| 20 | +// { error: "Session ID is required" }, |
| 21 | +// { status: 400 } |
| 22 | +// ); |
| 23 | +// } |
24 | 24 |
|
25 | | - if (!question || typeof question !== "string") { |
26 | | - console.error("❌ Missing question"); |
27 | | - return NextResponse.json( |
28 | | - { error: "Question is required" }, |
29 | | - { status: 400 } |
30 | | - ); |
31 | | - } |
| 25 | +// if (!question || typeof question !== "string") { |
| 26 | +// console.error("❌ Missing question"); |
| 27 | +// return NextResponse.json( |
| 28 | +// { error: "Question is required" }, |
| 29 | +// { status: 400 } |
| 30 | +// ); |
| 31 | +// } |
32 | 32 |
|
33 | | - // Initialize Supabase client with private key for secure backend communication |
34 | | - const supabase = createClient( |
35 | | - process.env.SUPABASE_URL!, |
36 | | - process.env.SUPABASE_PRIVATE_KEY!, |
37 | | - { |
38 | | - realtime: { |
39 | | - params: { |
40 | | - apikey: process.env.SUPABASE_PRIVATE_KEY!, // Explicitly pass the API key |
41 | | - }, |
42 | | - }, |
43 | | - auth: { |
44 | | - persistSession: false, |
45 | | - }, |
46 | | - } |
47 | | - ); |
| 33 | +// // Initialize Supabase client with private key for secure backend communication |
| 34 | +// const supabase = createClient( |
| 35 | +// process.env.SUPABASE_URL!, |
| 36 | +// process.env.SUPABASE_PRIVATE_KEY!, |
| 37 | +// { |
| 38 | +// realtime: { |
| 39 | +// params: { |
| 40 | +// apikey: process.env.SUPABASE_PRIVATE_KEY!, // Explicitly pass the API key |
| 41 | +// }, |
| 42 | +// }, |
| 43 | +// auth: { |
| 44 | +// persistSession: false, |
| 45 | +// }, |
| 46 | +// } |
| 47 | +// ); |
48 | 48 |
|
49 | | - // Create channel for this session with matching configuration |
50 | | - const channel = supabase.channel(`session:${sessionId}`, { |
51 | | - config: { |
52 | | - broadcast: { |
53 | | - self: false, // Don't receive own broadcasts |
54 | | - ack: true, // Wait for acknowledgment |
55 | | - }, |
56 | | - private: false, // Use public channel (must match backend configuration) |
57 | | - }, |
58 | | - }); |
59 | | - const messageId = generateMessageId(); |
| 49 | +// // Create channel for this session with matching configuration |
| 50 | +// const channel = supabase.channel(`session:${sessionId}`, { |
| 51 | +// config: { |
| 52 | +// broadcast: { |
| 53 | +// self: false, // Don't receive own broadcasts |
| 54 | +// ack: true, // Wait for acknowledgment |
| 55 | +// }, |
| 56 | +// private: false, // Use public channel (must match backend configuration) |
| 57 | +// }, |
| 58 | +// }); |
| 59 | +// const messageId = generateMessageId(); |
60 | 60 |
|
61 | | - console.log("📡 Subscribing to Supabase channel:", `session:${sessionId}`); |
| 61 | +// console.log("📡 Subscribing to Supabase channel:", `session:${sessionId}`); |
62 | 62 |
|
63 | | - // Subscribe first |
64 | | - await channel.subscribe(); |
| 63 | +// // Subscribe first |
| 64 | +// await channel.subscribe(); |
65 | 65 |
|
66 | | - console.log("📤 Broadcasting question to channel with messageId:", messageId); |
| 66 | +// console.log("📤 Broadcasting question to channel with messageId:", messageId); |
67 | 67 |
|
68 | | - // Send question via Supabase Broadcast (control signal only) |
69 | | - await channel.send({ |
70 | | - type: 'broadcast', |
71 | | - event: 'question', |
72 | | - payload: { |
73 | | - question, |
74 | | - messageId |
75 | | - } |
76 | | - }); |
| 68 | +// // Send question via Supabase Broadcast (control signal only) |
| 69 | +// await channel.send({ |
| 70 | +// type: 'broadcast', |
| 71 | +// event: 'question', |
| 72 | +// payload: { |
| 73 | +// question, |
| 74 | +// messageId |
| 75 | +// } |
| 76 | +// }); |
77 | 77 |
|
78 | | - console.log("✅ Question broadcast successful"); |
| 78 | +// console.log("✅ Question broadcast successful"); |
79 | 79 |
|
80 | | - // Unsubscribe after sending |
81 | | - await channel.unsubscribe(); |
| 80 | +// // Unsubscribe after sending |
| 81 | +// await channel.unsubscribe(); |
82 | 82 |
|
83 | | - // Return the message ID for tracking |
84 | | - return NextResponse.json({ |
85 | | - messageId, |
86 | | - sessionId |
87 | | - }); |
| 83 | +// // Return the message ID for tracking |
| 84 | +// return NextResponse.json({ |
| 85 | +// messageId, |
| 86 | +// sessionId |
| 87 | +// }); |
88 | 88 |
|
89 | | - } catch (error: any) { |
90 | | - console.error("Failed to trigger chat task:", error); |
91 | | - return NextResponse.json( |
92 | | - { error: error.message || "Failed to start chat" }, |
93 | | - { status: 500 } |
94 | | - ); |
95 | | - } |
96 | | -} |
| 89 | +// } catch (error: any) { |
| 90 | +// console.error("Failed to trigger chat task:", error); |
| 91 | +// return NextResponse.json( |
| 92 | +// { error: error.message || "Failed to start chat" }, |
| 93 | +// { status: 500 } |
| 94 | +// ); |
| 95 | +// } |
| 96 | +// } |
0 commit comments