MCPcopy Create free account
hub / github.com/Superflows-AI/superflows / handler

Function handler

pages/api/embed-text.ts:49–107  ·  view source on GitHub ↗
(req: NextRequest)

Source from the content-addressed store, hash-verified

47type EmbedTextType = z.infer<typeof EmbedTextZod>;
48
49export default async function handler(req: NextRequest) {
50 if (req.method !== "POST") {
51 return new Response(
52 JSON.stringify({
53 error: "Only POST requests allowed",
54 }),
55 { status: 405, headers },
56 );
57 }
58 const requestData = await req.json();
59 if (!isValidBody<EmbedTextType>(requestData, EmbedTextZod)) {
60 return new Response(JSON.stringify({ message: "Invalid request body" }), {
61 status: 400,
62 headers,
63 });
64 }
65 const { session } = await getSessionFromCookie(req);
66 if (!session) {
67 return new Response(JSON.stringify({ error: "Unauthorized" }), {
68 status: 401,
69 headers,
70 });
71 }
72 const profileRes = await supabase
73 .from("profiles")
74 .select("org_id")
75 .eq("id", session.user.id)
76 .single();
77 if (profileRes.error) {
78 return new Response(JSON.stringify({ error: "Server error" }), {
79 status: 500,
80 headers,
81 });
82 }
83 if (profileRes.data.org_id === null) {
84 return new Response(JSON.stringify({ error: "No organization found" }), {
85 status: 400,
86 headers,
87 });
88 }
89 const embedInserts = await embedText(
90 requestData.docsText,
91 requestData.title,
92 requestData.sectionName,
93 requestData.url,
94 requestData.createdAt,
95 );
96 await supabase.from("doc_chunks").insert(
97 embedInserts.map((insert) => ({
98 ...insert,
99 org_id: profileRes.data.org_id!,
100 })),
101 );
102
103 return new Response(JSON.stringify({ success: true }), {
104 status: 200,
105 headers,
106 });

Callers

nothing calls this directly

Calls 2

getSessionFromCookieFunction · 0.90
embedTextFunction · 0.90

Tested by

no test coverage detected